Annotation of imach/src/imach.c, revision 1.239
1.239 ! brouard 1: /* $Id: imach.c,v 1.238 2016/08/26 14:23:35 brouard Exp $
1.126 brouard 2: $State: Exp $
1.163 brouard 3: $Log: imach.c,v $
1.239 ! brouard 4: Revision 1.238 2016/08/26 14:23:35 brouard
! 5: Summary: Starting tests of 0.99
! 6:
1.238 brouard 7: Revision 1.237 2016/08/26 09:20:19 brouard
8: Summary: to valgrind
9:
1.237 brouard 10: Revision 1.236 2016/08/25 10:50:18 brouard
11: *** empty log message ***
12:
1.236 brouard 13: Revision 1.235 2016/08/25 06:59:23 brouard
14: *** empty log message ***
15:
1.235 brouard 16: Revision 1.234 2016/08/23 16:51:20 brouard
17: *** empty log message ***
18:
1.234 brouard 19: Revision 1.233 2016/08/23 07:40:50 brouard
20: Summary: not working
21:
1.233 brouard 22: Revision 1.232 2016/08/22 14:20:21 brouard
23: Summary: not working
24:
1.232 brouard 25: Revision 1.231 2016/08/22 07:17:15 brouard
26: Summary: not working
27:
1.231 brouard 28: Revision 1.230 2016/08/22 06:55:53 brouard
29: Summary: Not working
30:
1.230 brouard 31: Revision 1.229 2016/07/23 09:45:53 brouard
32: Summary: Completing for func too
33:
1.229 brouard 34: Revision 1.228 2016/07/22 17:45:30 brouard
35: Summary: Fixing some arrays, still debugging
36:
1.227 brouard 37: Revision 1.226 2016/07/12 18:42:34 brouard
38: Summary: temp
39:
1.226 brouard 40: Revision 1.225 2016/07/12 08:40:03 brouard
41: Summary: saving but not running
42:
1.225 brouard 43: Revision 1.224 2016/07/01 13:16:01 brouard
44: Summary: Fixes
45:
1.224 brouard 46: Revision 1.223 2016/02/19 09:23:35 brouard
47: Summary: temporary
48:
1.223 brouard 49: Revision 1.222 2016/02/17 08:14:50 brouard
50: Summary: Probably last 0.98 stable version 0.98r6
51:
1.222 brouard 52: Revision 1.221 2016/02/15 23:35:36 brouard
53: Summary: minor bug
54:
1.220 brouard 55: Revision 1.219 2016/02/15 00:48:12 brouard
56: *** empty log message ***
57:
1.219 brouard 58: Revision 1.218 2016/02/12 11:29:23 brouard
59: Summary: 0.99 Back projections
60:
1.218 brouard 61: Revision 1.217 2015/12/23 17:18:31 brouard
62: Summary: Experimental backcast
63:
1.217 brouard 64: Revision 1.216 2015/12/18 17:32:11 brouard
65: Summary: 0.98r4 Warning and status=-2
66:
67: Version 0.98r4 is now:
68: - displaying an error when status is -1, date of interview unknown and date of death known;
69: - permitting a status -2 when the vital status is unknown at a known date of right truncation.
70: Older changes concerning s=-2, dating from 2005 have been supersed.
71:
1.216 brouard 72: Revision 1.215 2015/12/16 08:52:24 brouard
73: Summary: 0.98r4 working
74:
1.215 brouard 75: Revision 1.214 2015/12/16 06:57:54 brouard
76: Summary: temporary not working
77:
1.214 brouard 78: Revision 1.213 2015/12/11 18:22:17 brouard
79: Summary: 0.98r4
80:
1.213 brouard 81: Revision 1.212 2015/11/21 12:47:24 brouard
82: Summary: minor typo
83:
1.212 brouard 84: Revision 1.211 2015/11/21 12:41:11 brouard
85: Summary: 0.98r3 with some graph of projected cross-sectional
86:
87: Author: Nicolas Brouard
88:
1.211 brouard 89: Revision 1.210 2015/11/18 17:41:20 brouard
90: Summary: Start working on projected prevalences
91:
1.210 brouard 92: Revision 1.209 2015/11/17 22:12:03 brouard
93: Summary: Adding ftolpl parameter
94: Author: N Brouard
95:
96: We had difficulties to get smoothed confidence intervals. It was due
97: to the period prevalence which wasn't computed accurately. The inner
98: parameter ftolpl is now an outer parameter of the .imach parameter
99: file after estepm. If ftolpl is small 1.e-4 and estepm too,
100: computation are long.
101:
1.209 brouard 102: Revision 1.208 2015/11/17 14:31:57 brouard
103: Summary: temporary
104:
1.208 brouard 105: Revision 1.207 2015/10/27 17:36:57 brouard
106: *** empty log message ***
107:
1.207 brouard 108: Revision 1.206 2015/10/24 07:14:11 brouard
109: *** empty log message ***
110:
1.206 brouard 111: Revision 1.205 2015/10/23 15:50:53 brouard
112: Summary: 0.98r3 some clarification for graphs on likelihood contributions
113:
1.205 brouard 114: Revision 1.204 2015/10/01 16:20:26 brouard
115: Summary: Some new graphs of contribution to likelihood
116:
1.204 brouard 117: Revision 1.203 2015/09/30 17:45:14 brouard
118: Summary: looking at better estimation of the hessian
119:
120: Also a better criteria for convergence to the period prevalence And
121: therefore adding the number of years needed to converge. (The
122: prevalence in any alive state shold sum to one
123:
1.203 brouard 124: Revision 1.202 2015/09/22 19:45:16 brouard
125: Summary: Adding some overall graph on contribution to likelihood. Might change
126:
1.202 brouard 127: Revision 1.201 2015/09/15 17:34:58 brouard
128: Summary: 0.98r0
129:
130: - Some new graphs like suvival functions
131: - Some bugs fixed like model=1+age+V2.
132:
1.201 brouard 133: Revision 1.200 2015/09/09 16:53:55 brouard
134: Summary: Big bug thanks to Flavia
135:
136: Even model=1+age+V2. did not work anymore
137:
1.200 brouard 138: Revision 1.199 2015/09/07 14:09:23 brouard
139: Summary: 0.98q6 changing default small png format for graph to vectorized svg.
140:
1.199 brouard 141: Revision 1.198 2015/09/03 07:14:39 brouard
142: Summary: 0.98q5 Flavia
143:
1.198 brouard 144: Revision 1.197 2015/09/01 18:24:39 brouard
145: *** empty log message ***
146:
1.197 brouard 147: Revision 1.196 2015/08/18 23:17:52 brouard
148: Summary: 0.98q5
149:
1.196 brouard 150: Revision 1.195 2015/08/18 16:28:39 brouard
151: Summary: Adding a hack for testing purpose
152:
153: After reading the title, ftol and model lines, if the comment line has
154: a q, starting with #q, the answer at the end of the run is quit. It
155: permits to run test files in batch with ctest. The former workaround was
156: $ echo q | imach foo.imach
157:
1.195 brouard 158: Revision 1.194 2015/08/18 13:32:00 brouard
159: Summary: Adding error when the covariance matrix doesn't contain the exact number of lines required by the model line.
160:
1.194 brouard 161: Revision 1.193 2015/08/04 07:17:42 brouard
162: Summary: 0.98q4
163:
1.193 brouard 164: Revision 1.192 2015/07/16 16:49:02 brouard
165: Summary: Fixing some outputs
166:
1.192 brouard 167: Revision 1.191 2015/07/14 10:00:33 brouard
168: Summary: Some fixes
169:
1.191 brouard 170: Revision 1.190 2015/05/05 08:51:13 brouard
171: Summary: Adding digits in output parameters (7 digits instead of 6)
172:
173: Fix 1+age+.
174:
1.190 brouard 175: Revision 1.189 2015/04/30 14:45:16 brouard
176: Summary: 0.98q2
177:
1.189 brouard 178: Revision 1.188 2015/04/30 08:27:53 brouard
179: *** empty log message ***
180:
1.188 brouard 181: Revision 1.187 2015/04/29 09:11:15 brouard
182: *** empty log message ***
183:
1.187 brouard 184: Revision 1.186 2015/04/23 12:01:52 brouard
185: Summary: V1*age is working now, version 0.98q1
186:
187: Some codes had been disabled in order to simplify and Vn*age was
188: working in the optimization phase, ie, giving correct MLE parameters,
189: but, as usual, outputs were not correct and program core dumped.
190:
1.186 brouard 191: Revision 1.185 2015/03/11 13:26:42 brouard
192: Summary: Inclusion of compile and links command line for Intel Compiler
193:
1.185 brouard 194: Revision 1.184 2015/03/11 11:52:39 brouard
195: Summary: Back from Windows 8. Intel Compiler
196:
1.184 brouard 197: Revision 1.183 2015/03/10 20:34:32 brouard
198: Summary: 0.98q0, trying with directest, mnbrak fixed
199:
200: We use directest instead of original Powell test; probably no
201: incidence on the results, but better justifications;
202: We fixed Numerical Recipes mnbrak routine which was wrong and gave
203: wrong results.
204:
1.183 brouard 205: Revision 1.182 2015/02/12 08:19:57 brouard
206: Summary: Trying to keep directest which seems simpler and more general
207: Author: Nicolas Brouard
208:
1.182 brouard 209: Revision 1.181 2015/02/11 23:22:24 brouard
210: Summary: Comments on Powell added
211:
212: Author:
213:
1.181 brouard 214: Revision 1.180 2015/02/11 17:33:45 brouard
215: Summary: Finishing move from main to function (hpijx and prevalence_limit)
216:
1.180 brouard 217: Revision 1.179 2015/01/04 09:57:06 brouard
218: Summary: back to OS/X
219:
1.179 brouard 220: Revision 1.178 2015/01/04 09:35:48 brouard
221: *** empty log message ***
222:
1.178 brouard 223: Revision 1.177 2015/01/03 18:40:56 brouard
224: Summary: Still testing ilc32 on OSX
225:
1.177 brouard 226: Revision 1.176 2015/01/03 16:45:04 brouard
227: *** empty log message ***
228:
1.176 brouard 229: Revision 1.175 2015/01/03 16:33:42 brouard
230: *** empty log message ***
231:
1.175 brouard 232: Revision 1.174 2015/01/03 16:15:49 brouard
233: Summary: Still in cross-compilation
234:
1.174 brouard 235: Revision 1.173 2015/01/03 12:06:26 brouard
236: Summary: trying to detect cross-compilation
237:
1.173 brouard 238: Revision 1.172 2014/12/27 12:07:47 brouard
239: Summary: Back from Visual Studio and Intel, options for compiling for Windows XP
240:
1.172 brouard 241: Revision 1.171 2014/12/23 13:26:59 brouard
242: Summary: Back from Visual C
243:
244: Still problem with utsname.h on Windows
245:
1.171 brouard 246: Revision 1.170 2014/12/23 11:17:12 brouard
247: Summary: Cleaning some \%% back to %%
248:
249: The escape was mandatory for a specific compiler (which one?), but too many warnings.
250:
1.170 brouard 251: Revision 1.169 2014/12/22 23:08:31 brouard
252: Summary: 0.98p
253:
254: Outputs some informations on compiler used, OS etc. Testing on different platforms.
255:
1.169 brouard 256: Revision 1.168 2014/12/22 15:17:42 brouard
1.170 brouard 257: Summary: update
1.169 brouard 258:
1.168 brouard 259: Revision 1.167 2014/12/22 13:50:56 brouard
260: Summary: Testing uname and compiler version and if compiled 32 or 64
261:
262: Testing on Linux 64
263:
1.167 brouard 264: Revision 1.166 2014/12/22 11:40:47 brouard
265: *** empty log message ***
266:
1.166 brouard 267: Revision 1.165 2014/12/16 11:20:36 brouard
268: Summary: After compiling on Visual C
269:
270: * imach.c (Module): Merging 1.61 to 1.162
271:
1.165 brouard 272: Revision 1.164 2014/12/16 10:52:11 brouard
273: Summary: Merging with Visual C after suppressing some warnings for unused variables. Also fixing Saito's bug 0.98Xn
274:
275: * imach.c (Module): Merging 1.61 to 1.162
276:
1.164 brouard 277: Revision 1.163 2014/12/16 10:30:11 brouard
278: * imach.c (Module): Merging 1.61 to 1.162
279:
1.163 brouard 280: Revision 1.162 2014/09/25 11:43:39 brouard
281: Summary: temporary backup 0.99!
282:
1.162 brouard 283: Revision 1.1 2014/09/16 11:06:58 brouard
284: Summary: With some code (wrong) for nlopt
285:
286: Author:
287:
288: Revision 1.161 2014/09/15 20:41:41 brouard
289: Summary: Problem with macro SQR on Intel compiler
290:
1.161 brouard 291: Revision 1.160 2014/09/02 09:24:05 brouard
292: *** empty log message ***
293:
1.160 brouard 294: Revision 1.159 2014/09/01 10:34:10 brouard
295: Summary: WIN32
296: Author: Brouard
297:
1.159 brouard 298: Revision 1.158 2014/08/27 17:11:51 brouard
299: *** empty log message ***
300:
1.158 brouard 301: Revision 1.157 2014/08/27 16:26:55 brouard
302: Summary: Preparing windows Visual studio version
303: Author: Brouard
304:
305: In order to compile on Visual studio, time.h is now correct and time_t
306: and tm struct should be used. difftime should be used but sometimes I
307: just make the differences in raw time format (time(&now).
308: Trying to suppress #ifdef LINUX
309: Add xdg-open for __linux in order to open default browser.
310:
1.157 brouard 311: Revision 1.156 2014/08/25 20:10:10 brouard
312: *** empty log message ***
313:
1.156 brouard 314: Revision 1.155 2014/08/25 18:32:34 brouard
315: Summary: New compile, minor changes
316: Author: Brouard
317:
1.155 brouard 318: Revision 1.154 2014/06/20 17:32:08 brouard
319: Summary: Outputs now all graphs of convergence to period prevalence
320:
1.154 brouard 321: Revision 1.153 2014/06/20 16:45:46 brouard
322: Summary: If 3 live state, convergence to period prevalence on same graph
323: Author: Brouard
324:
1.153 brouard 325: Revision 1.152 2014/06/18 17:54:09 brouard
326: Summary: open browser, use gnuplot on same dir than imach if not found in the path
327:
1.152 brouard 328: Revision 1.151 2014/06/18 16:43:30 brouard
329: *** empty log message ***
330:
1.151 brouard 331: Revision 1.150 2014/06/18 16:42:35 brouard
332: Summary: If gnuplot is not in the path try on same directory than imach binary (OSX)
333: Author: brouard
334:
1.150 brouard 335: Revision 1.149 2014/06/18 15:51:14 brouard
336: Summary: Some fixes in parameter files errors
337: Author: Nicolas Brouard
338:
1.149 brouard 339: Revision 1.148 2014/06/17 17:38:48 brouard
340: Summary: Nothing new
341: Author: Brouard
342:
343: Just a new packaging for OS/X version 0.98nS
344:
1.148 brouard 345: Revision 1.147 2014/06/16 10:33:11 brouard
346: *** empty log message ***
347:
1.147 brouard 348: Revision 1.146 2014/06/16 10:20:28 brouard
349: Summary: Merge
350: Author: Brouard
351:
352: Merge, before building revised version.
353:
1.146 brouard 354: Revision 1.145 2014/06/10 21:23:15 brouard
355: Summary: Debugging with valgrind
356: Author: Nicolas Brouard
357:
358: Lot of changes in order to output the results with some covariates
359: After the Edimburgh REVES conference 2014, it seems mandatory to
360: improve the code.
361: No more memory valgrind error but a lot has to be done in order to
362: continue the work of splitting the code into subroutines.
363: Also, decodemodel has been improved. Tricode is still not
364: optimal. nbcode should be improved. Documentation has been added in
365: the source code.
366:
1.144 brouard 367: Revision 1.143 2014/01/26 09:45:38 brouard
368: Summary: Version 0.98nR (to be improved, but gives same optimization results as 0.98k. Nice, promising
369:
370: * imach.c (Module): Trying to merge old staffs together while being at Tokyo. Not tested...
371: (Module): Version 0.98nR Running ok, but output format still only works for three covariates.
372:
1.143 brouard 373: Revision 1.142 2014/01/26 03:57:36 brouard
374: Summary: gnuplot changed plot w l 1 has to be changed to plot w l lt 2
375:
376: * imach.c (Module): Trying to merge old staffs together while being at Tokyo. Not tested...
377:
1.142 brouard 378: Revision 1.141 2014/01/26 02:42:01 brouard
379: * imach.c (Module): Trying to merge old staffs together while being at Tokyo. Not tested...
380:
1.141 brouard 381: Revision 1.140 2011/09/02 10:37:54 brouard
382: Summary: times.h is ok with mingw32 now.
383:
1.140 brouard 384: Revision 1.139 2010/06/14 07:50:17 brouard
385: After the theft of my laptop, I probably lost some lines of codes which were not uploaded to the CVS tree.
386: I remember having already fixed agemin agemax which are pointers now but not cvs saved.
387:
1.139 brouard 388: Revision 1.138 2010/04/30 18:19:40 brouard
389: *** empty log message ***
390:
1.138 brouard 391: Revision 1.137 2010/04/29 18:11:38 brouard
392: (Module): Checking covariates for more complex models
393: than V1+V2. A lot of change to be done. Unstable.
394:
1.137 brouard 395: Revision 1.136 2010/04/26 20:30:53 brouard
396: (Module): merging some libgsl code. Fixing computation
397: of likelione (using inter/intrapolation if mle = 0) in order to
398: get same likelihood as if mle=1.
399: Some cleaning of code and comments added.
400:
1.136 brouard 401: Revision 1.135 2009/10/29 15:33:14 brouard
402: (Module): Now imach stops if date of birth, at least year of birth, is not given. Some cleaning of the code.
403:
1.135 brouard 404: Revision 1.134 2009/10/29 13:18:53 brouard
405: (Module): Now imach stops if date of birth, at least year of birth, is not given. Some cleaning of the code.
406:
1.134 brouard 407: Revision 1.133 2009/07/06 10:21:25 brouard
408: just nforces
409:
1.133 brouard 410: Revision 1.132 2009/07/06 08:22:05 brouard
411: Many tings
412:
1.132 brouard 413: Revision 1.131 2009/06/20 16:22:47 brouard
414: Some dimensions resccaled
415:
1.131 brouard 416: Revision 1.130 2009/05/26 06:44:34 brouard
417: (Module): Max Covariate is now set to 20 instead of 8. A
418: lot of cleaning with variables initialized to 0. Trying to make
419: V2+V3*age+V1+V4 strb=V3*age+V1+V4 working better.
420:
1.130 brouard 421: Revision 1.129 2007/08/31 13:49:27 lievre
422: Modification of the way of exiting when the covariate is not binary in order to see on the window the error message before exiting
423:
1.129 lievre 424: Revision 1.128 2006/06/30 13:02:05 brouard
425: (Module): Clarifications on computing e.j
426:
1.128 brouard 427: Revision 1.127 2006/04/28 18:11:50 brouard
428: (Module): Yes the sum of survivors was wrong since
429: imach-114 because nhstepm was no more computed in the age
430: loop. Now we define nhstepma in the age loop.
431: (Module): In order to speed up (in case of numerous covariates) we
432: compute health expectancies (without variances) in a first step
433: and then all the health expectancies with variances or standard
434: deviation (needs data from the Hessian matrices) which slows the
435: computation.
436: In the future we should be able to stop the program is only health
437: expectancies and graph are needed without standard deviations.
438:
1.127 brouard 439: Revision 1.126 2006/04/28 17:23:28 brouard
440: (Module): Yes the sum of survivors was wrong since
441: imach-114 because nhstepm was no more computed in the age
442: loop. Now we define nhstepma in the age loop.
443: Version 0.98h
444:
1.126 brouard 445: Revision 1.125 2006/04/04 15:20:31 lievre
446: Errors in calculation of health expectancies. Age was not initialized.
447: Forecasting file added.
448:
449: Revision 1.124 2006/03/22 17:13:53 lievre
450: Parameters are printed with %lf instead of %f (more numbers after the comma).
451: The log-likelihood is printed in the log file
452:
453: Revision 1.123 2006/03/20 10:52:43 brouard
454: * imach.c (Module): <title> changed, corresponds to .htm file
455: name. <head> headers where missing.
456:
457: * imach.c (Module): Weights can have a decimal point as for
458: English (a comma might work with a correct LC_NUMERIC environment,
459: otherwise the weight is truncated).
460: Modification of warning when the covariates values are not 0 or
461: 1.
462: Version 0.98g
463:
464: Revision 1.122 2006/03/20 09:45:41 brouard
465: (Module): Weights can have a decimal point as for
466: English (a comma might work with a correct LC_NUMERIC environment,
467: otherwise the weight is truncated).
468: Modification of warning when the covariates values are not 0 or
469: 1.
470: Version 0.98g
471:
472: Revision 1.121 2006/03/16 17:45:01 lievre
473: * imach.c (Module): Comments concerning covariates added
474:
475: * imach.c (Module): refinements in the computation of lli if
476: status=-2 in order to have more reliable computation if stepm is
477: not 1 month. Version 0.98f
478:
479: Revision 1.120 2006/03/16 15:10:38 lievre
480: (Module): refinements in the computation of lli if
481: status=-2 in order to have more reliable computation if stepm is
482: not 1 month. Version 0.98f
483:
484: Revision 1.119 2006/03/15 17:42:26 brouard
485: (Module): Bug if status = -2, the loglikelihood was
486: computed as likelihood omitting the logarithm. Version O.98e
487:
488: Revision 1.118 2006/03/14 18:20:07 brouard
489: (Module): varevsij Comments added explaining the second
490: table of variances if popbased=1 .
491: (Module): Covariances of eij, ekl added, graphs fixed, new html link.
492: (Module): Function pstamp added
493: (Module): Version 0.98d
494:
495: Revision 1.117 2006/03/14 17:16:22 brouard
496: (Module): varevsij Comments added explaining the second
497: table of variances if popbased=1 .
498: (Module): Covariances of eij, ekl added, graphs fixed, new html link.
499: (Module): Function pstamp added
500: (Module): Version 0.98d
501:
502: Revision 1.116 2006/03/06 10:29:27 brouard
503: (Module): Variance-covariance wrong links and
504: varian-covariance of ej. is needed (Saito).
505:
506: Revision 1.115 2006/02/27 12:17:45 brouard
507: (Module): One freematrix added in mlikeli! 0.98c
508:
509: Revision 1.114 2006/02/26 12:57:58 brouard
510: (Module): Some improvements in processing parameter
511: filename with strsep.
512:
513: Revision 1.113 2006/02/24 14:20:24 brouard
514: (Module): Memory leaks checks with valgrind and:
515: datafile was not closed, some imatrix were not freed and on matrix
516: allocation too.
517:
518: Revision 1.112 2006/01/30 09:55:26 brouard
519: (Module): Back to gnuplot.exe instead of wgnuplot.exe
520:
521: Revision 1.111 2006/01/25 20:38:18 brouard
522: (Module): Lots of cleaning and bugs added (Gompertz)
523: (Module): Comments can be added in data file. Missing date values
524: can be a simple dot '.'.
525:
526: Revision 1.110 2006/01/25 00:51:50 brouard
527: (Module): Lots of cleaning and bugs added (Gompertz)
528:
529: Revision 1.109 2006/01/24 19:37:15 brouard
530: (Module): Comments (lines starting with a #) are allowed in data.
531:
532: Revision 1.108 2006/01/19 18:05:42 lievre
533: Gnuplot problem appeared...
534: To be fixed
535:
536: Revision 1.107 2006/01/19 16:20:37 brouard
537: Test existence of gnuplot in imach path
538:
539: Revision 1.106 2006/01/19 13:24:36 brouard
540: Some cleaning and links added in html output
541:
542: Revision 1.105 2006/01/05 20:23:19 lievre
543: *** empty log message ***
544:
545: Revision 1.104 2005/09/30 16:11:43 lievre
546: (Module): sump fixed, loop imx fixed, and simplifications.
547: (Module): If the status is missing at the last wave but we know
548: that the person is alive, then we can code his/her status as -2
549: (instead of missing=-1 in earlier versions) and his/her
550: contributions to the likelihood is 1 - Prob of dying from last
551: health status (= 1-p13= p11+p12 in the easiest case of somebody in
552: the healthy state at last known wave). Version is 0.98
553:
554: Revision 1.103 2005/09/30 15:54:49 lievre
555: (Module): sump fixed, loop imx fixed, and simplifications.
556:
557: Revision 1.102 2004/09/15 17:31:30 brouard
558: Add the possibility to read data file including tab characters.
559:
560: Revision 1.101 2004/09/15 10:38:38 brouard
561: Fix on curr_time
562:
563: Revision 1.100 2004/07/12 18:29:06 brouard
564: Add version for Mac OS X. Just define UNIX in Makefile
565:
566: Revision 1.99 2004/06/05 08:57:40 brouard
567: *** empty log message ***
568:
569: Revision 1.98 2004/05/16 15:05:56 brouard
570: New version 0.97 . First attempt to estimate force of mortality
571: directly from the data i.e. without the need of knowing the health
572: state at each age, but using a Gompertz model: log u =a + b*age .
573: This is the basic analysis of mortality and should be done before any
574: other analysis, in order to test if the mortality estimated from the
575: cross-longitudinal survey is different from the mortality estimated
576: from other sources like vital statistic data.
577:
578: The same imach parameter file can be used but the option for mle should be -3.
579:
1.133 brouard 580: Agnès, who wrote this part of the code, tried to keep most of the
1.126 brouard 581: former routines in order to include the new code within the former code.
582:
583: The output is very simple: only an estimate of the intercept and of
584: the slope with 95% confident intervals.
585:
586: Current limitations:
587: A) Even if you enter covariates, i.e. with the
588: model= V1+V2 equation for example, the programm does only estimate a unique global model without covariates.
589: B) There is no computation of Life Expectancy nor Life Table.
590:
591: Revision 1.97 2004/02/20 13:25:42 lievre
592: Version 0.96d. Population forecasting command line is (temporarily)
593: suppressed.
594:
595: Revision 1.96 2003/07/15 15:38:55 brouard
596: * imach.c (Repository): Errors in subdirf, 2, 3 while printing tmpout is
597: rewritten within the same printf. Workaround: many printfs.
598:
599: Revision 1.95 2003/07/08 07:54:34 brouard
600: * imach.c (Repository):
601: (Repository): Using imachwizard code to output a more meaningful covariance
602: matrix (cov(a12,c31) instead of numbers.
603:
604: Revision 1.94 2003/06/27 13:00:02 brouard
605: Just cleaning
606:
607: Revision 1.93 2003/06/25 16:33:55 brouard
608: (Module): On windows (cygwin) function asctime_r doesn't
609: exist so I changed back to asctime which exists.
610: (Module): Version 0.96b
611:
612: Revision 1.92 2003/06/25 16:30:45 brouard
613: (Module): On windows (cygwin) function asctime_r doesn't
614: exist so I changed back to asctime which exists.
615:
616: Revision 1.91 2003/06/25 15:30:29 brouard
617: * imach.c (Repository): Duplicated warning errors corrected.
618: (Repository): Elapsed time after each iteration is now output. It
619: helps to forecast when convergence will be reached. Elapsed time
620: is stamped in powell. We created a new html file for the graphs
621: concerning matrix of covariance. It has extension -cov.htm.
622:
623: Revision 1.90 2003/06/24 12:34:15 brouard
624: (Module): Some bugs corrected for windows. Also, when
625: mle=-1 a template is output in file "or"mypar.txt with the design
626: of the covariance matrix to be input.
627:
628: Revision 1.89 2003/06/24 12:30:52 brouard
629: (Module): Some bugs corrected for windows. Also, when
630: mle=-1 a template is output in file "or"mypar.txt with the design
631: of the covariance matrix to be input.
632:
633: Revision 1.88 2003/06/23 17:54:56 brouard
634: * 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.
635:
636: Revision 1.87 2003/06/18 12:26:01 brouard
637: Version 0.96
638:
639: Revision 1.86 2003/06/17 20:04:08 brouard
640: (Module): Change position of html and gnuplot routines and added
641: routine fileappend.
642:
643: Revision 1.85 2003/06/17 13:12:43 brouard
644: * imach.c (Repository): Check when date of death was earlier that
645: current date of interview. It may happen when the death was just
646: prior to the death. In this case, dh was negative and likelihood
647: was wrong (infinity). We still send an "Error" but patch by
648: assuming that the date of death was just one stepm after the
649: interview.
650: (Repository): Because some people have very long ID (first column)
651: we changed int to long in num[] and we added a new lvector for
652: memory allocation. But we also truncated to 8 characters (left
653: truncation)
654: (Repository): No more line truncation errors.
655:
656: Revision 1.84 2003/06/13 21:44:43 brouard
657: * imach.c (Repository): Replace "freqsummary" at a correct
658: place. It differs from routine "prevalence" which may be called
659: many times. Probs is memory consuming and must be used with
660: parcimony.
661: Version 0.95a3 (should output exactly the same maximization than 0.8a2)
662:
663: Revision 1.83 2003/06/10 13:39:11 lievre
664: *** empty log message ***
665:
666: Revision 1.82 2003/06/05 15:57:20 brouard
667: Add log in imach.c and fullversion number is now printed.
668:
669: */
670: /*
671: Interpolated Markov Chain
672:
673: Short summary of the programme:
674:
1.227 brouard 675: This program computes Healthy Life Expectancies or State-specific
676: (if states aren't health statuses) Expectancies from
677: cross-longitudinal data. Cross-longitudinal data consist in:
678:
679: -1- a first survey ("cross") where individuals from different ages
680: are interviewed on their health status or degree of disability (in
681: the case of a health survey which is our main interest)
682:
683: -2- at least a second wave of interviews ("longitudinal") which
684: measure each change (if any) in individual health status. Health
685: expectancies are computed from the time spent in each health state
686: according to a model. More health states you consider, more time is
687: necessary to reach the Maximum Likelihood of the parameters involved
688: in the model. The simplest model is the multinomial logistic model
689: where pij is the probability to be observed in state j at the second
690: wave conditional to be observed in state i at the first
691: wave. Therefore the model is: log(pij/pii)= aij + bij*age+ cij*sex +
692: etc , where 'age' is age and 'sex' is a covariate. If you want to
693: have a more complex model than "constant and age", you should modify
694: the program where the markup *Covariates have to be included here
695: again* invites you to do it. More covariates you add, slower the
1.126 brouard 696: convergence.
697:
698: The advantage of this computer programme, compared to a simple
699: multinomial logistic model, is clear when the delay between waves is not
700: identical for each individual. Also, if a individual missed an
701: intermediate interview, the information is lost, but taken into
702: account using an interpolation or extrapolation.
703:
704: hPijx is the probability to be observed in state i at age x+h
705: conditional to the observed state i at age x. The delay 'h' can be
706: split into an exact number (nh*stepm) of unobserved intermediate
707: states. This elementary transition (by month, quarter,
708: semester or year) is modelled as a multinomial logistic. The hPx
709: matrix is simply the matrix product of nh*stepm elementary matrices
710: and the contribution of each individual to the likelihood is simply
711: hPijx.
712:
713: Also this programme outputs the covariance matrix of the parameters but also
1.218 brouard 714: of the life expectancies. It also computes the period (stable) prevalence.
715:
716: Back prevalence and projections:
1.227 brouard 717:
718: - back_prevalence_limit(double *p, double **bprlim, double ageminpar,
719: double agemaxpar, double ftolpl, int *ncvyearp, double
720: dateprev1,double dateprev2, int firstpass, int lastpass, int
721: mobilavproj)
722:
723: Computes the back prevalence limit for any combination of
724: covariate values k at any age between ageminpar and agemaxpar and
725: returns it in **bprlim. In the loops,
726:
727: - **bprevalim(**bprlim, ***mobaverage, nlstate, *p, age, **oldm,
728: **savm, **dnewm, **doldm, **dsavm, ftolpl, ncvyearp, k);
729:
730: - hBijx Back Probability to be in state i at age x-h being in j at x
1.218 brouard 731: Computes for any combination of covariates k and any age between bage and fage
732: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
733: oldm=oldms;savm=savms;
1.227 brouard 734:
735: - hbxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k);
1.218 brouard 736: Computes the transition matrix starting at age 'age' over
737: 'nhstepm*hstepm*stepm' months (i.e. until
738: age (in years) age+nhstepm*hstepm*stepm/12) by multiplying
1.227 brouard 739: nhstepm*hstepm matrices.
740:
741: Returns p3mat[i][j][h] after calling
742: p3mat[i][j][h]=matprod2(newm,
743: bmij(pmmij,cov,ncovmodel,x,nlstate,prevacurrent, dnewm, doldm,
744: dsavm,ij),\ 1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath,
745: oldm);
1.226 brouard 746:
747: Important routines
748:
749: - func (or funcone), computes logit (pij) distinguishing
750: o fixed variables (single or product dummies or quantitative);
751: o varying variables by:
752: (1) wave (single, product dummies, quantitative),
753: (2) by age (can be month) age (done), age*age (done), age*Vn where Vn can be:
754: % fixed dummy (treated) or quantitative (not done because time-consuming);
755: % varying dummy (not done) or quantitative (not done);
756: - Tricode which tests the modality of dummy variables (in order to warn with wrong or empty modalities)
757: and returns the number of efficient covariates cptcoveff and modalities nbcode[Tvar[k]][1]= 0 and nbcode[Tvar[k]][2]= 1 usually.
758: - printinghtml which outputs results like life expectancy in and from a state for a combination of modalities of dummy variables
759: o There are 2*cptcoveff combinations of (0,1) for cptcoveff variables. Outputting only combinations with people, éliminating 1 1 if
760: race White (0 0), Black vs White (1 0), Hispanic (0 1) and 1 1 being meaningless.
1.218 brouard 761:
1.226 brouard 762:
763:
1.133 brouard 764: Authors: Nicolas Brouard (brouard@ined.fr) and Agnès Lièvre (lievre@ined.fr).
765: Institut national d'études démographiques, Paris.
1.126 brouard 766: This software have been partly granted by Euro-REVES, a concerted action
767: from the European Union.
768: It is copyrighted identically to a GNU software product, ie programme and
769: software can be distributed freely for non commercial use. Latest version
770: can be accessed at http://euroreves.ined.fr/imach .
771:
772: Help to debug: LD_PRELOAD=/usr/local/lib/libnjamd.so ./imach foo.imach
773: or better on gdb : set env LD_PRELOAD=/usr/local/lib/libnjamd.so
774:
775: **********************************************************************/
776: /*
777: main
778: read parameterfile
779: read datafile
780: concatwav
781: freqsummary
782: if (mle >= 1)
783: mlikeli
784: print results files
785: if mle==1
786: computes hessian
787: read end of parameter file: agemin, agemax, bage, fage, estepm
788: begin-prev-date,...
789: open gnuplot file
790: open html file
1.145 brouard 791: period (stable) prevalence | pl_nom 1-1 2-2 etc by covariate
792: for age prevalim() | #****** V1=0 V2=1 V3=1 V4=0 ******
793: | 65 1 0 2 1 3 1 4 0 0.96326 0.03674
794: freexexit2 possible for memory heap.
795:
796: h Pij x | pij_nom ficrestpij
797: # Cov Agex agex+h hpijx with i,j= 1-1 1-2 1-3 2-1 2-2 2-3
798: 1 85 85 1.00000 0.00000 0.00000 0.00000 1.00000 0.00000
799: 1 85 86 0.68299 0.22291 0.09410 0.71093 0.00000 0.28907
800:
801: 1 65 99 0.00364 0.00322 0.99314 0.00350 0.00310 0.99340
802: 1 65 100 0.00214 0.00204 0.99581 0.00206 0.00196 0.99597
803: variance of p one-step probabilities varprob | prob_nom ficresprob #One-step probabilities and stand. devi in ()
804: Standard deviation of one-step probabilities | probcor_nom ficresprobcor #One-step probabilities and correlation matrix
805: Matrix of variance covariance of one-step probabilities | probcov_nom ficresprobcov #One-step probabilities and covariance matrix
806:
1.126 brouard 807: forecasting if prevfcast==1 prevforecast call prevalence()
808: health expectancies
809: Variance-covariance of DFLE
810: prevalence()
811: movingaverage()
812: varevsij()
813: if popbased==1 varevsij(,popbased)
814: total life expectancies
815: Variance of period (stable) prevalence
816: end
817: */
818:
1.187 brouard 819: /* #define DEBUG */
820: /* #define DEBUGBRENT */
1.203 brouard 821: /* #define DEBUGLINMIN */
822: /* #define DEBUGHESS */
823: #define DEBUGHESSIJ
1.224 brouard 824: /* #define LINMINORIGINAL /\* Don't use loop on scale in linmin (accepting nan) *\/ */
1.165 brouard 825: #define POWELL /* Instead of NLOPT */
1.224 brouard 826: #define POWELLNOF3INFF1TEST /* Skip test */
1.186 brouard 827: /* #define POWELLORIGINAL /\* Don't use Directest to decide new direction but original Powell test *\/ */
828: /* #define MNBRAKORIGINAL /\* Don't use mnbrak fix *\/ */
1.126 brouard 829:
830: #include <math.h>
831: #include <stdio.h>
832: #include <stdlib.h>
833: #include <string.h>
1.226 brouard 834: #include <ctype.h>
1.159 brouard 835:
836: #ifdef _WIN32
837: #include <io.h>
1.172 brouard 838: #include <windows.h>
839: #include <tchar.h>
1.159 brouard 840: #else
1.126 brouard 841: #include <unistd.h>
1.159 brouard 842: #endif
1.126 brouard 843:
844: #include <limits.h>
845: #include <sys/types.h>
1.171 brouard 846:
847: #if defined(__GNUC__)
848: #include <sys/utsname.h> /* Doesn't work on Windows */
849: #endif
850:
1.126 brouard 851: #include <sys/stat.h>
852: #include <errno.h>
1.159 brouard 853: /* extern int errno; */
1.126 brouard 854:
1.157 brouard 855: /* #ifdef LINUX */
856: /* #include <time.h> */
857: /* #include "timeval.h" */
858: /* #else */
859: /* #include <sys/time.h> */
860: /* #endif */
861:
1.126 brouard 862: #include <time.h>
863:
1.136 brouard 864: #ifdef GSL
865: #include <gsl/gsl_errno.h>
866: #include <gsl/gsl_multimin.h>
867: #endif
868:
1.167 brouard 869:
1.162 brouard 870: #ifdef NLOPT
871: #include <nlopt.h>
872: typedef struct {
873: double (* function)(double [] );
874: } myfunc_data ;
875: #endif
876:
1.126 brouard 877: /* #include <libintl.h> */
878: /* #define _(String) gettext (String) */
879:
1.141 brouard 880: #define MAXLINE 1024 /* Was 256. Overflow with 312 with 2 states and 4 covariates. Should be ok */
1.126 brouard 881:
882: #define GNUPLOTPROGRAM "gnuplot"
883: /*#define GNUPLOTPROGRAM "..\\gp37mgw\\wgnuplot"*/
884: #define FILENAMELENGTH 132
885:
886: #define GLOCK_ERROR_NOPATH -1 /* empty path */
887: #define GLOCK_ERROR_GETCWD -2 /* cannot get cwd */
888:
1.144 brouard 889: #define MAXPARM 128 /**< Maximum number of parameters for the optimization */
890: #define NPARMAX 64 /**< (nlstate+ndeath-1)*nlstate*ncovmodel */
1.126 brouard 891:
892: #define NINTERVMAX 8
1.144 brouard 893: #define NLSTATEMAX 8 /**< Maximum number of live states (for func) */
894: #define NDEATHMAX 8 /**< Maximum number of dead states (for func) */
895: #define NCOVMAX 20 /**< Maximum number of covariates, including generated covariates V1*V2 */
1.197 brouard 896: #define codtabm(h,k) (1 & (h-1) >> (k-1))+1
1.211 brouard 897: /*#define decodtabm(h,k,cptcoveff)= (h <= (1<<cptcoveff)?(((h-1) >> (k-1)) & 1) +1 : -1)*/
898: #define decodtabm(h,k,cptcoveff) (((h-1) >> (k-1)) & 1) +1
1.126 brouard 899: #define MAXN 20000
1.144 brouard 900: #define YEARM 12. /**< Number of months per year */
1.218 brouard 901: /* #define AGESUP 130 */
902: #define AGESUP 150
903: #define AGEMARGE 25 /* Marge for agemin and agemax for(iage=agemin-AGEMARGE; iage <= agemax+3+AGEMARGE; iage++) */
1.126 brouard 904: #define AGEBASE 40
1.194 brouard 905: #define AGEOVERFLOW 1.e20
1.164 brouard 906: #define AGEGOMP 10 /**< Minimal age for Gompertz adjustment */
1.157 brouard 907: #ifdef _WIN32
908: #define DIRSEPARATOR '\\'
909: #define CHARSEPARATOR "\\"
910: #define ODIRSEPARATOR '/'
911: #else
1.126 brouard 912: #define DIRSEPARATOR '/'
913: #define CHARSEPARATOR "/"
914: #define ODIRSEPARATOR '\\'
915: #endif
916:
1.239 ! brouard 917: /* $Id: imach.c,v 1.238 2016/08/26 14:23:35 brouard Exp $ */
1.126 brouard 918: /* $State: Exp $ */
1.196 brouard 919: #include "version.h"
920: char version[]=__IMACH_VERSION__;
1.224 brouard 921: char copyright[]="February 2016,INED-EUROREVES-Institut de longevite-Japan Society for the Promotion of Science (Grant-in-Aid for Scientific Research 25293121), Intel Software 2015-2018";
1.239 ! brouard 922: char fullversion[]="$Revision: 1.238 $ $Date: 2016/08/26 14:23:35 $";
1.126 brouard 923: char strstart[80];
924: char optionfilext[10], optionfilefiname[FILENAMELENGTH];
1.130 brouard 925: int erreur=0, nberr=0, nbwarn=0; /* Error number, number of errors number of warnings */
1.187 brouard 926: int nagesqr=0, nforce=0; /* nagesqr=1 if model is including age*age, number of forces */
1.145 brouard 927: /* Number of covariates model=V2+V1+ V3*age+V2*V4 */
928: int cptcovn=0; /**< cptcovn number of covariates added in the model (excepting constant and age and age*product) */
929: int cptcovt=0; /**< cptcovt number of covariates added in the model (excepting constant and age) */
1.225 brouard 930: int cptcovs=0; /**< cptcovs number of simple covariates in the model V2+V1 =2 */
931: int cptcovsnq=0; /**< cptcovsnq number of simple covariates in the model but non quantitative V2+V1 =2 */
1.145 brouard 932: int cptcovage=0; /**< Number of covariates with age: V3*age only =1 */
933: int cptcovprodnoage=0; /**< Number of covariate products without age */
934: int cptcoveff=0; /* Total number of covariates to vary for printing results */
1.233 brouard 935: int ncovf=0; /* Total number of effective fixed covariates (dummy or quantitative) in the model */
936: int ncovv=0; /* Total number of effective (wave) varying covariates (dummy or quantitative) in the model */
1.232 brouard 937: int ncova=0; /* Total number of effective (wave and stepm) varying with age covariates (dummy of quantitative) in the model */
1.234 brouard 938: int nsd=0; /**< Total number of single dummy variables (output) */
939: int nsq=0; /**< Total number of single quantitative variables (output) */
1.232 brouard 940: int ncoveff=0; /* Total number of effective fixed dummy covariates in the model */
1.225 brouard 941: int nqfveff=0; /**< nqfveff Number of Quantitative Fixed Variables Effective */
1.224 brouard 942: int ntveff=0; /**< ntveff number of effective time varying variables */
943: int nqtveff=0; /**< ntqveff number of effective time varying quantitative variables */
1.145 brouard 944: int cptcov=0; /* Working variable */
1.218 brouard 945: int ncovcombmax=NCOVMAX; /* Maximum calculated number of covariate combination = pow(2, cptcoveff) */
1.126 brouard 946: int npar=NPARMAX;
947: int nlstate=2; /* Number of live states */
948: int ndeath=1; /* Number of dead states */
1.130 brouard 949: int ncovmodel=0, ncovcol=0; /* Total number of covariables including constant a12*1 +b12*x ncovmodel=2 */
1.223 brouard 950: int nqv=0, ntv=0, nqtv=0; /* Total number of quantitative variables, time variable (dummy), quantitative and time variable */
1.126 brouard 951: int popbased=0;
952:
953: int *wav; /* Number of waves for this individuual 0 is possible */
1.130 brouard 954: int maxwav=0; /* Maxim number of waves */
955: int jmin=0, jmax=0; /* min, max spacing between 2 waves */
956: int ijmin=0, ijmax=0; /* Individuals having jmin and jmax */
957: int gipmx=0, gsw=0; /* Global variables on the number of contributions
1.126 brouard 958: to the likelihood and the sum of weights (done by funcone)*/
1.130 brouard 959: int mle=1, weightopt=0;
1.126 brouard 960: int **mw; /* mw[mi][i] is number of the mi wave for this individual */
961: int **dh; /* dh[mi][i] is number of steps between mi,mi+1 for this individual */
962: int **bh; /* bh[mi][i] is the bias (+ or -) for this individual if the delay between
963: * wave mi and wave mi+1 is not an exact multiple of stepm. */
1.162 brouard 964: int countcallfunc=0; /* Count the number of calls to func */
1.230 brouard 965: int selected(int kvar); /* Is covariate kvar selected for printing results */
966:
1.130 brouard 967: double jmean=1; /* Mean space between 2 waves */
1.145 brouard 968: double **matprod2(); /* test */
1.126 brouard 969: double **oldm, **newm, **savm; /* Working pointers to matrices */
970: double **oldms, **newms, **savms; /* Fixed working pointers to matrices */
1.218 brouard 971: double **ddnewms, **ddoldms, **ddsavms; /* for freeing later */
972:
1.136 brouard 973: /*FILE *fic ; */ /* Used in readdata only */
1.217 brouard 974: FILE *ficpar, *ficparo,*ficres, *ficresp, *ficresphtm, *ficresphtmfr, *ficrespl, *ficresplb,*ficrespij, *ficrespijb, *ficrest,*ficresf, *ficresfb,*ficrespop;
1.126 brouard 975: FILE *ficlog, *ficrespow;
1.130 brouard 976: int globpr=0; /* Global variable for printing or not */
1.126 brouard 977: double fretone; /* Only one call to likelihood */
1.130 brouard 978: long ipmx=0; /* Number of contributions */
1.126 brouard 979: double sw; /* Sum of weights */
980: char filerespow[FILENAMELENGTH];
981: char fileresilk[FILENAMELENGTH]; /* File of individual contributions to the likelihood */
982: FILE *ficresilk;
983: FILE *ficgp,*ficresprob,*ficpop, *ficresprobcov, *ficresprobcor;
984: FILE *ficresprobmorprev;
985: FILE *fichtm, *fichtmcov; /* Html File */
986: FILE *ficreseij;
987: char filerese[FILENAMELENGTH];
988: FILE *ficresstdeij;
989: char fileresstde[FILENAMELENGTH];
990: FILE *ficrescveij;
991: char filerescve[FILENAMELENGTH];
992: FILE *ficresvij;
993: char fileresv[FILENAMELENGTH];
994: FILE *ficresvpl;
995: char fileresvpl[FILENAMELENGTH];
996: char title[MAXLINE];
1.234 brouard 997: char model[MAXLINE]; /**< The model line */
1.217 brouard 998: char optionfile[FILENAMELENGTH], datafile[FILENAMELENGTH], filerespl[FILENAMELENGTH], fileresplb[FILENAMELENGTH];
1.126 brouard 999: char plotcmd[FILENAMELENGTH], pplotcmd[FILENAMELENGTH];
1000: char tmpout[FILENAMELENGTH], tmpout2[FILENAMELENGTH];
1001: char command[FILENAMELENGTH];
1002: int outcmd=0;
1003:
1.217 brouard 1004: char fileres[FILENAMELENGTH], filerespij[FILENAMELENGTH], filerespijb[FILENAMELENGTH], filereso[FILENAMELENGTH], rfileres[FILENAMELENGTH];
1.202 brouard 1005: char fileresu[FILENAMELENGTH]; /* fileres without r in front */
1.126 brouard 1006: char filelog[FILENAMELENGTH]; /* Log file */
1007: char filerest[FILENAMELENGTH];
1008: char fileregp[FILENAMELENGTH];
1009: char popfile[FILENAMELENGTH];
1010:
1011: char optionfilegnuplot[FILENAMELENGTH], optionfilehtm[FILENAMELENGTH], optionfilehtmcov[FILENAMELENGTH] ;
1012:
1.157 brouard 1013: /* struct timeval start_time, end_time, curr_time, last_time, forecast_time; */
1014: /* struct timezone tzp; */
1015: /* extern int gettimeofday(); */
1016: struct tm tml, *gmtime(), *localtime();
1017:
1018: extern time_t time();
1019:
1020: struct tm start_time, end_time, curr_time, last_time, forecast_time;
1021: time_t rstart_time, rend_time, rcurr_time, rlast_time, rforecast_time; /* raw time */
1022: struct tm tm;
1023:
1.126 brouard 1024: char strcurr[80], strfor[80];
1025:
1026: char *endptr;
1027: long lval;
1028: double dval;
1029:
1030: #define NR_END 1
1031: #define FREE_ARG char*
1032: #define FTOL 1.0e-10
1033:
1034: #define NRANSI
1035: #define ITMAX 200
1036:
1037: #define TOL 2.0e-4
1038:
1039: #define CGOLD 0.3819660
1040: #define ZEPS 1.0e-10
1041: #define SHFT(a,b,c,d) (a)=(b);(b)=(c);(c)=(d);
1042:
1043: #define GOLD 1.618034
1044: #define GLIMIT 100.0
1045: #define TINY 1.0e-20
1046:
1047: static double maxarg1,maxarg2;
1048: #define FMAX(a,b) (maxarg1=(a),maxarg2=(b),(maxarg1)>(maxarg2)? (maxarg1):(maxarg2))
1049: #define FMIN(a,b) (maxarg1=(a),maxarg2=(b),(maxarg1)<(maxarg2)? (maxarg1):(maxarg2))
1050:
1051: #define SIGN(a,b) ((b)>0.0 ? fabs(a) : -fabs(a))
1052: #define rint(a) floor(a+0.5)
1.166 brouard 1053: /* http://www.thphys.uni-heidelberg.de/~robbers/cmbeasy/doc/html/myutils_8h-source.html */
1.183 brouard 1054: #define mytinydouble 1.0e-16
1.166 brouard 1055: /* #define DEQUAL(a,b) (fabs((a)-(b))<mytinydouble) */
1056: /* http://www.thphys.uni-heidelberg.de/~robbers/cmbeasy/doc/html/mynrutils_8h-source.html */
1057: /* static double dsqrarg; */
1058: /* #define DSQR(a) (DEQUAL((dsqrarg=(a)),0.0) ? 0.0 : dsqrarg*dsqrarg) */
1.126 brouard 1059: static double sqrarg;
1060: #define SQR(a) ((sqrarg=(a)) == 0.0 ? 0.0 :sqrarg*sqrarg)
1061: #define SWAP(a,b) {temp=(a);(a)=(b);(b)=temp;}
1062: int agegomp= AGEGOMP;
1063:
1064: int imx;
1065: int stepm=1;
1066: /* Stepm, step in month: minimum step interpolation*/
1067:
1068: int estepm;
1069: /* Estepm, step in month to interpolate survival function in order to approximate Life Expectancy*/
1070:
1071: int m,nb;
1072: long *num;
1.197 brouard 1073: int firstpass=0, lastpass=4,*cod, *cens;
1.192 brouard 1074: int *ncodemax; /* ncodemax[j]= Number of modalities of the j th
1075: covariate for which somebody answered excluding
1076: undefined. Usually 2: 0 and 1. */
1077: int *ncodemaxwundef; /* ncodemax[j]= Number of modalities of the j th
1078: covariate for which somebody answered including
1079: undefined. Usually 3: -1, 0 and 1. */
1.126 brouard 1080: double **agev,*moisnais, *annais, *moisdc, *andc,**mint, **anint;
1.218 brouard 1081: double **pmmij, ***probs; /* Global pointer */
1.219 brouard 1082: double ***mobaverage, ***mobaverages; /* New global variable */
1.126 brouard 1083: double *ageexmed,*agecens;
1084: double dateintmean=0;
1085:
1086: double *weight;
1087: int **s; /* Status */
1.141 brouard 1088: double *agedc;
1.145 brouard 1089: double **covar; /**< covar[j,i], value of jth covariate for individual i,
1.141 brouard 1090: * covar=matrix(0,NCOVMAX,1,n);
1.187 brouard 1091: * cov[Tage[kk]+2]=covar[Tvar[Tage[kk]]][i]*age; */
1.225 brouard 1092: double **coqvar; /* Fixed quantitative covariate iqv */
1093: double ***cotvar; /* Time varying covariate itv */
1094: double ***cotqvar; /* Time varying quantitative covariate itqv */
1.141 brouard 1095: double idx;
1096: int **nbcode, *Tvar; /**< model=V2 => Tvar[1]= 2 */
1.234 brouard 1097: /* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
1098: /*k 1 2 3 4 5 6 7 8 9 */
1099: /*Tvar[k]= 5 4 3 6 5 2 7 1 1 */
1100: /* Tndvar[k] 1 2 3 4 5 */
1101: /*TDvar 4 3 6 7 1 */ /* For outputs only; combination of dummies fixed or varying */
1102: /* Tns[k] 1 2 2 4 5 */ /* Number of single cova */
1103: /* TvarsD[k] 1 2 3 */ /* Number of single dummy cova */
1104: /* TvarsDind 2 3 9 */ /* position K of single dummy cova */
1105: /* TvarsQ[k] 1 2 */ /* Number of single quantitative cova */
1106: /* TvarsQind 1 6 */ /* position K of single quantitative cova */
1107: /* Tprod[i]=k 4 7 */
1108: /* Tage[i]=k 5 8 */
1109: /* */
1110: /* Type */
1111: /* V 1 2 3 4 5 */
1112: /* F F V V V */
1113: /* D Q D D Q */
1114: /* */
1115: int *TvarsD;
1116: int *TvarsDind;
1117: int *TvarsQ;
1118: int *TvarsQind;
1119:
1.235 brouard 1120: #define MAXRESULTLINES 10
1121: int nresult=0;
1122: int TKresult[MAXRESULTLINES];
1.237 brouard 1123: int Tresult[MAXRESULTLINES][NCOVMAX];/* For dummy variable , value (output) */
1124: int Tinvresult[MAXRESULTLINES][NCOVMAX];/* For dummy variable , value (output) */
1.235 brouard 1125: int Tvresult[MAXRESULTLINES][NCOVMAX]; /* For dummy variable , variable # (output) */
1126: double Tqresult[MAXRESULTLINES][NCOVMAX]; /* For quantitative variable , value (output) */
1.237 brouard 1127: double Tqinvresult[MAXRESULTLINES][NCOVMAX]; /* For quantitative variable , value (output) */
1.235 brouard 1128: int Tvqresult[MAXRESULTLINES][NCOVMAX]; /* For quantitative variable , variable # (output) */
1129:
1.234 brouard 1130: /* int *TDvar; /\**< TDvar[1]=4, TDvarF[2]=3, TDvar[3]=6 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 *\/ */
1.232 brouard 1131: int *TvarF; /**< TvarF[1]=Tvar[6]=2, TvarF[2]=Tvar[7]=7, TvarF[3]=Tvar[9]=1 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
1132: int *TvarFind; /**< TvarFind[1]=6, TvarFind[2]=7, Tvarind[3]=9 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
1133: int *TvarV; /**< TvarV[1]=Tvar[1]=5, TvarV[2]=Tvar[2]=4 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
1134: int *TvarVind; /**< TvarVind[1]=1, TvarVind[2]=2 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
1135: int *TvarA; /**< TvarA[1]=Tvar[5]=5, TvarA[2]=Tvar[8]=1 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
1136: int *TvarAind; /**< TvarindA[1]=5, TvarAind[2]=8 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
1.231 brouard 1137: int *TvarFD; /**< TvarFD[1]=V1 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
1138: int *TvarFDind; /* TvarFDind[1]=9 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
1139: int *TvarFQ; /* TvarFQ[1]=V2 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ /* Only simple fixed quantitative variable */
1140: int *TvarFQind; /* TvarFQind[1]=6 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ /* Only simple fixed quantitative variable */
1141: int *TvarVD; /* TvarVD[1]=V5 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ /* Only simple fixed quantitative variable */
1142: int *TvarVDind; /* TvarVDind[1]=1 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ /* Only simple fixed quantitative variable */
1143: int *TvarVQ; /* TvarVQ[1]=V5 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ /* Only simple time varying quantitative variable */
1144: int *TvarVQind; /* TvarVQind[1]=1 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ /* Only simple time varying quantitative variable */
1145:
1.230 brouard 1146: int *Tvarsel; /**< Selected covariates for output */
1147: double *Tvalsel; /**< Selected modality value of covariate for output */
1.226 brouard 1148: int *Typevar; /**< 0 for simple covariate (dummy, quantitative, fixed or varying), 1 for age product, 2 for product */
1.227 brouard 1149: int *Fixed; /** Fixed[k] 0=fixed, 1 varying, 2 fixed with age product, 3 varying with age product */
1150: int *Dummy; /** Dummy[k] 0=dummy (0 1), 1 quantitative (single or product without age), 2 dummy with age product, 3 quant with age product */
1.238 brouard 1151: int *DummyV; /** Dummy[v] 0=dummy (0 1), 1 quantitative */
1152: int *FixedV; /** FixedV[v] 0 fixed, 1 varying */
1.197 brouard 1153: int *Tage;
1.227 brouard 1154: int anyvaryingduminmodel=0; /**< Any varying dummy in Model=1 yes, 0 no, to avoid a loop on waves in freq */
1.228 brouard 1155: int *Tmodelind; /** Tmodelind[Tvaraff[3]]=9 for V1 position,Tvaraff[1]@9={4, 3, 1, 0, 0, 0, 0, 0, 0}, model=V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1*/
1.230 brouard 1156: int *TmodelInvind; /** Tmodelind[Tvaraff[3]]=9 for V1 position,Tvaraff[1]@9={4, 3, 1, 0, 0, 0, 0, 0, 0}, model=V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1*/
1157: int *TmodelInvQind; /** Tmodelqind[1]=1 for V5(quantitative varying) position,Tvaraff[1]@9={4, 3, 1, 0, 0, 0, 0, 0, 0}, model=V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
1.145 brouard 1158: int *Ndum; /** Freq of modality (tricode */
1.200 brouard 1159: /* int **codtab;*/ /**< codtab=imatrix(1,100,1,10); */
1.227 brouard 1160: int **Tvard;
1161: int *Tprod;/**< Gives the k position of the k1 product */
1.238 brouard 1162: /* Tprod[k1=1]=3(=V1*V4) for V2+V1+V1*V4+age*V3 */
1.227 brouard 1163: int *Tposprod; /**< Gives the k1 product from the k position */
1.238 brouard 1164: /* if V2+V1+V1*V4+age*V3+V3*V2 TProd[k1=2]=5 (V3*V2) */
1165: /* Tposprod[k]=k1 , Tposprod[3]=1, Tposprod[5(V3*V2)]=2 (2nd product without age) */
1.227 brouard 1166: int cptcovprod, *Tvaraff, *invalidvarcomb;
1.126 brouard 1167: double *lsurv, *lpop, *tpop;
1168:
1.231 brouard 1169: #define FD 1; /* Fixed dummy covariate */
1170: #define FQ 2; /* Fixed quantitative covariate */
1171: #define FP 3; /* Fixed product covariate */
1172: #define FPDD 7; /* Fixed product dummy*dummy covariate */
1173: #define FPDQ 8; /* Fixed product dummy*quantitative covariate */
1174: #define FPQQ 9; /* Fixed product quantitative*quantitative covariate */
1175: #define VD 10; /* Varying dummy covariate */
1176: #define VQ 11; /* Varying quantitative covariate */
1177: #define VP 12; /* Varying product covariate */
1178: #define VPDD 13; /* Varying product dummy*dummy covariate */
1179: #define VPDQ 14; /* Varying product dummy*quantitative covariate */
1180: #define VPQQ 15; /* Varying product quantitative*quantitative covariate */
1181: #define APFD 16; /* Age product * fixed dummy covariate */
1182: #define APFQ 17; /* Age product * fixed quantitative covariate */
1183: #define APVD 18; /* Age product * varying dummy covariate */
1184: #define APVQ 19; /* Age product * varying quantitative covariate */
1185:
1186: #define FTYPE 1; /* Fixed covariate */
1187: #define VTYPE 2; /* Varying covariate (loop in wave) */
1188: #define ATYPE 2; /* Age product covariate (loop in dh within wave)*/
1189:
1190: struct kmodel{
1191: int maintype; /* main type */
1192: int subtype; /* subtype */
1193: };
1194: struct kmodel modell[NCOVMAX];
1195:
1.143 brouard 1196: double ftol=FTOL; /**< Tolerance for computing Max Likelihood */
1197: double ftolhess; /**< Tolerance for computing hessian */
1.126 brouard 1198:
1199: /**************** split *************************/
1200: static int split( char *path, char *dirc, char *name, char *ext, char *finame )
1201: {
1202: /* From a file name with (full) path (either Unix or Windows) we extract the directory (dirc)
1203: the name of the file (name), its extension only (ext) and its first part of the name (finame)
1204: */
1205: char *ss; /* pointer */
1.186 brouard 1206: int l1=0, l2=0; /* length counters */
1.126 brouard 1207:
1208: l1 = strlen(path ); /* length of path */
1209: if ( l1 == 0 ) return( GLOCK_ERROR_NOPATH );
1210: ss= strrchr( path, DIRSEPARATOR ); /* find last / */
1211: if ( ss == NULL ) { /* no directory, so determine current directory */
1212: strcpy( name, path ); /* we got the fullname name because no directory */
1213: /*if(strrchr(path, ODIRSEPARATOR )==NULL)
1214: printf("Warning you should use %s as a separator\n",DIRSEPARATOR);*/
1215: /* get current working directory */
1216: /* extern char* getcwd ( char *buf , int len);*/
1.184 brouard 1217: #ifdef WIN32
1218: if (_getcwd( dirc, FILENAME_MAX ) == NULL ) {
1219: #else
1220: if (getcwd(dirc, FILENAME_MAX) == NULL) {
1221: #endif
1.126 brouard 1222: return( GLOCK_ERROR_GETCWD );
1223: }
1224: /* got dirc from getcwd*/
1225: printf(" DIRC = %s \n",dirc);
1.205 brouard 1226: } else { /* strip directory from path */
1.126 brouard 1227: ss++; /* after this, the filename */
1228: l2 = strlen( ss ); /* length of filename */
1229: if ( l2 == 0 ) return( GLOCK_ERROR_NOPATH );
1230: strcpy( name, ss ); /* save file name */
1231: strncpy( dirc, path, l1 - l2 ); /* now the directory */
1.186 brouard 1232: dirc[l1-l2] = '\0'; /* add zero */
1.126 brouard 1233: printf(" DIRC2 = %s \n",dirc);
1234: }
1235: /* We add a separator at the end of dirc if not exists */
1236: l1 = strlen( dirc ); /* length of directory */
1237: if( dirc[l1-1] != DIRSEPARATOR ){
1238: dirc[l1] = DIRSEPARATOR;
1239: dirc[l1+1] = 0;
1240: printf(" DIRC3 = %s \n",dirc);
1241: }
1242: ss = strrchr( name, '.' ); /* find last / */
1243: if (ss >0){
1244: ss++;
1245: strcpy(ext,ss); /* save extension */
1246: l1= strlen( name);
1247: l2= strlen(ss)+1;
1248: strncpy( finame, name, l1-l2);
1249: finame[l1-l2]= 0;
1250: }
1251:
1252: return( 0 ); /* we're done */
1253: }
1254:
1255:
1256: /******************************************/
1257:
1258: void replace_back_to_slash(char *s, char*t)
1259: {
1260: int i;
1261: int lg=0;
1262: i=0;
1263: lg=strlen(t);
1264: for(i=0; i<= lg; i++) {
1265: (s[i] = t[i]);
1266: if (t[i]== '\\') s[i]='/';
1267: }
1268: }
1269:
1.132 brouard 1270: char *trimbb(char *out, char *in)
1.137 brouard 1271: { /* Trim multiple blanks in line but keeps first blanks if line starts with blanks */
1.132 brouard 1272: char *s;
1273: s=out;
1274: while (*in != '\0'){
1.137 brouard 1275: while( *in == ' ' && *(in+1) == ' '){ /* && *(in+1) != '\0'){*/
1.132 brouard 1276: in++;
1277: }
1278: *out++ = *in++;
1279: }
1280: *out='\0';
1281: return s;
1282: }
1283:
1.187 brouard 1284: /* char *substrchaine(char *out, char *in, char *chain) */
1285: /* { */
1286: /* /\* Substract chain 'chain' from 'in', return and output 'out' *\/ */
1287: /* char *s, *t; */
1288: /* t=in;s=out; */
1289: /* while ((*in != *chain) && (*in != '\0')){ */
1290: /* *out++ = *in++; */
1291: /* } */
1292:
1293: /* /\* *in matches *chain *\/ */
1294: /* while ((*in++ == *chain++) && (*in != '\0')){ */
1295: /* printf("*in = %c, *out= %c *chain= %c \n", *in, *out, *chain); */
1296: /* } */
1297: /* in--; chain--; */
1298: /* while ( (*in != '\0')){ */
1299: /* printf("Bef *in = %c, *out= %c *chain= %c \n", *in, *out, *chain); */
1300: /* *out++ = *in++; */
1301: /* printf("Aft *in = %c, *out= %c *chain= %c \n", *in, *out, *chain); */
1302: /* } */
1303: /* *out='\0'; */
1304: /* out=s; */
1305: /* return out; */
1306: /* } */
1307: char *substrchaine(char *out, char *in, char *chain)
1308: {
1309: /* Substract chain 'chain' from 'in', return and output 'out' */
1310: /* in="V1+V1*age+age*age+V2", chain="age*age" */
1311:
1312: char *strloc;
1313:
1314: strcpy (out, in);
1315: strloc = strstr(out, chain); /* strloc points to out at age*age+V2 */
1316: printf("Bef strloc=%s chain=%s out=%s \n", strloc, chain, out);
1317: if(strloc != NULL){
1318: /* will affect out */ /* strloc+strlenc(chain)=+V2 */ /* Will also work in Unicode */
1319: memmove(strloc,strloc+strlen(chain), strlen(strloc+strlen(chain))+1);
1320: /* strcpy (strloc, strloc +strlen(chain));*/
1321: }
1322: printf("Aft strloc=%s chain=%s in=%s out=%s \n", strloc, chain, in, out);
1323: return out;
1324: }
1325:
1326:
1.145 brouard 1327: char *cutl(char *blocc, char *alocc, char *in, char occ)
1328: {
1.187 brouard 1329: /* cuts string in into blocc and alocc where blocc ends before FIRST occurence of char 'occ'
1.145 brouard 1330: and alocc starts after first occurence of char 'occ' : ex cutv(blocc,alocc,"abcdef2ghi2j",'2')
1.187 brouard 1331: gives blocc="abcdef" and alocc="ghi2j".
1.145 brouard 1332: If occ is not found blocc is null and alocc is equal to in. Returns blocc
1333: */
1.160 brouard 1334: char *s, *t;
1.145 brouard 1335: t=in;s=in;
1336: while ((*in != occ) && (*in != '\0')){
1337: *alocc++ = *in++;
1338: }
1339: if( *in == occ){
1340: *(alocc)='\0';
1341: s=++in;
1342: }
1343:
1344: if (s == t) {/* occ not found */
1345: *(alocc-(in-s))='\0';
1346: in=s;
1347: }
1348: while ( *in != '\0'){
1349: *blocc++ = *in++;
1350: }
1351:
1352: *blocc='\0';
1353: return t;
1354: }
1.137 brouard 1355: char *cutv(char *blocc, char *alocc, char *in, char occ)
1356: {
1.187 brouard 1357: /* cuts string in into blocc and alocc where blocc ends before LAST occurence of char 'occ'
1.137 brouard 1358: and alocc starts after last occurence of char 'occ' : ex cutv(blocc,alocc,"abcdef2ghi2j",'2')
1359: gives blocc="abcdef2ghi" and alocc="j".
1360: If occ is not found blocc is null and alocc is equal to in. Returns alocc
1361: */
1362: char *s, *t;
1363: t=in;s=in;
1364: while (*in != '\0'){
1365: while( *in == occ){
1366: *blocc++ = *in++;
1367: s=in;
1368: }
1369: *blocc++ = *in++;
1370: }
1371: if (s == t) /* occ not found */
1372: *(blocc-(in-s))='\0';
1373: else
1374: *(blocc-(in-s)-1)='\0';
1375: in=s;
1376: while ( *in != '\0'){
1377: *alocc++ = *in++;
1378: }
1379:
1380: *alocc='\0';
1381: return s;
1382: }
1383:
1.126 brouard 1384: int nbocc(char *s, char occ)
1385: {
1386: int i,j=0;
1387: int lg=20;
1388: i=0;
1389: lg=strlen(s);
1390: for(i=0; i<= lg; i++) {
1.234 brouard 1391: if (s[i] == occ ) j++;
1.126 brouard 1392: }
1393: return j;
1394: }
1395:
1.137 brouard 1396: /* void cutv(char *u,char *v, char*t, char occ) */
1397: /* { */
1398: /* /\* cuts string t into u and v where u ends before last occurence of char 'occ' */
1399: /* and v starts after last occurence of char 'occ' : ex cutv(u,v,"abcdef2ghi2j",'2') */
1400: /* gives u="abcdef2ghi" and v="j" *\/ */
1401: /* int i,lg,j,p=0; */
1402: /* i=0; */
1403: /* lg=strlen(t); */
1404: /* for(j=0; j<=lg-1; j++) { */
1405: /* if((t[j]!= occ) && (t[j+1]== occ)) p=j+1; */
1406: /* } */
1.126 brouard 1407:
1.137 brouard 1408: /* for(j=0; j<p; j++) { */
1409: /* (u[j] = t[j]); */
1410: /* } */
1411: /* u[p]='\0'; */
1.126 brouard 1412:
1.137 brouard 1413: /* for(j=0; j<= lg; j++) { */
1414: /* if (j>=(p+1))(v[j-p-1] = t[j]); */
1415: /* } */
1416: /* } */
1.126 brouard 1417:
1.160 brouard 1418: #ifdef _WIN32
1419: char * strsep(char **pp, const char *delim)
1420: {
1421: char *p, *q;
1422:
1423: if ((p = *pp) == NULL)
1424: return 0;
1425: if ((q = strpbrk (p, delim)) != NULL)
1426: {
1427: *pp = q + 1;
1428: *q = '\0';
1429: }
1430: else
1431: *pp = 0;
1432: return p;
1433: }
1434: #endif
1435:
1.126 brouard 1436: /********************** nrerror ********************/
1437:
1438: void nrerror(char error_text[])
1439: {
1440: fprintf(stderr,"ERREUR ...\n");
1441: fprintf(stderr,"%s\n",error_text);
1442: exit(EXIT_FAILURE);
1443: }
1444: /*********************** vector *******************/
1445: double *vector(int nl, int nh)
1446: {
1447: double *v;
1448: v=(double *) malloc((size_t)((nh-nl+1+NR_END)*sizeof(double)));
1449: if (!v) nrerror("allocation failure in vector");
1450: return v-nl+NR_END;
1451: }
1452:
1453: /************************ free vector ******************/
1454: void free_vector(double*v, int nl, int nh)
1455: {
1456: free((FREE_ARG)(v+nl-NR_END));
1457: }
1458:
1459: /************************ivector *******************************/
1460: int *ivector(long nl,long nh)
1461: {
1462: int *v;
1463: v=(int *) malloc((size_t)((nh-nl+1+NR_END)*sizeof(int)));
1464: if (!v) nrerror("allocation failure in ivector");
1465: return v-nl+NR_END;
1466: }
1467:
1468: /******************free ivector **************************/
1469: void free_ivector(int *v, long nl, long nh)
1470: {
1471: free((FREE_ARG)(v+nl-NR_END));
1472: }
1473:
1474: /************************lvector *******************************/
1475: long *lvector(long nl,long nh)
1476: {
1477: long *v;
1478: v=(long *) malloc((size_t)((nh-nl+1+NR_END)*sizeof(long)));
1479: if (!v) nrerror("allocation failure in ivector");
1480: return v-nl+NR_END;
1481: }
1482:
1483: /******************free lvector **************************/
1484: void free_lvector(long *v, long nl, long nh)
1485: {
1486: free((FREE_ARG)(v+nl-NR_END));
1487: }
1488:
1489: /******************* imatrix *******************************/
1490: int **imatrix(long nrl, long nrh, long ncl, long nch)
1491: /* allocate a int matrix with subscript range m[nrl..nrh][ncl..nch] */
1492: {
1493: long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
1494: int **m;
1495:
1496: /* allocate pointers to rows */
1497: m=(int **) malloc((size_t)((nrow+NR_END)*sizeof(int*)));
1498: if (!m) nrerror("allocation failure 1 in matrix()");
1499: m += NR_END;
1500: m -= nrl;
1501:
1502:
1503: /* allocate rows and set pointers to them */
1504: m[nrl]=(int *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(int)));
1505: if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
1506: m[nrl] += NR_END;
1507: m[nrl] -= ncl;
1508:
1509: for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
1510:
1511: /* return pointer to array of pointers to rows */
1512: return m;
1513: }
1514:
1515: /****************** free_imatrix *************************/
1516: void free_imatrix(m,nrl,nrh,ncl,nch)
1517: int **m;
1518: long nch,ncl,nrh,nrl;
1519: /* free an int matrix allocated by imatrix() */
1520: {
1521: free((FREE_ARG) (m[nrl]+ncl-NR_END));
1522: free((FREE_ARG) (m+nrl-NR_END));
1523: }
1524:
1525: /******************* matrix *******************************/
1526: double **matrix(long nrl, long nrh, long ncl, long nch)
1527: {
1528: long i, nrow=nrh-nrl+1, ncol=nch-ncl+1;
1529: double **m;
1530:
1531: m=(double **) malloc((size_t)((nrow+NR_END)*sizeof(double*)));
1532: if (!m) nrerror("allocation failure 1 in matrix()");
1533: m += NR_END;
1534: m -= nrl;
1535:
1536: m[nrl]=(double *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(double)));
1537: if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
1538: m[nrl] += NR_END;
1539: m[nrl] -= ncl;
1540:
1541: for (i=nrl+1; i<=nrh; i++) m[i]=m[i-1]+ncol;
1542: return m;
1.145 brouard 1543: /* print *(*(m+1)+70) or print m[1][70]; print m+1 or print &(m[1]) or &(m[1][0])
1544: m[i] = address of ith row of the table. &(m[i]) is its value which is another adress
1545: that of m[i][0]. In order to get the value p m[i][0] but it is unitialized.
1.126 brouard 1546: */
1547: }
1548:
1549: /*************************free matrix ************************/
1550: void free_matrix(double **m, long nrl, long nrh, long ncl, long nch)
1551: {
1552: free((FREE_ARG)(m[nrl]+ncl-NR_END));
1553: free((FREE_ARG)(m+nrl-NR_END));
1554: }
1555:
1556: /******************* ma3x *******************************/
1557: double ***ma3x(long nrl, long nrh, long ncl, long nch, long nll, long nlh)
1558: {
1559: long i, j, nrow=nrh-nrl+1, ncol=nch-ncl+1, nlay=nlh-nll+1;
1560: double ***m;
1561:
1562: m=(double ***) malloc((size_t)((nrow+NR_END)*sizeof(double*)));
1563: if (!m) nrerror("allocation failure 1 in matrix()");
1564: m += NR_END;
1565: m -= nrl;
1566:
1567: m[nrl]=(double **) malloc((size_t)((nrow*ncol+NR_END)*sizeof(double)));
1568: if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
1569: m[nrl] += NR_END;
1570: m[nrl] -= ncl;
1571:
1572: for (i=nrl+1; i<=nrh; i++) m[i]=m[i-1]+ncol;
1573:
1574: m[nrl][ncl]=(double *) malloc((size_t)((nrow*ncol*nlay+NR_END)*sizeof(double)));
1575: if (!m[nrl][ncl]) nrerror("allocation failure 3 in matrix()");
1576: m[nrl][ncl] += NR_END;
1577: m[nrl][ncl] -= nll;
1578: for (j=ncl+1; j<=nch; j++)
1579: m[nrl][j]=m[nrl][j-1]+nlay;
1580:
1581: for (i=nrl+1; i<=nrh; i++) {
1582: m[i][ncl]=m[i-1l][ncl]+ncol*nlay;
1583: for (j=ncl+1; j<=nch; j++)
1584: m[i][j]=m[i][j-1]+nlay;
1585: }
1586: return m;
1587: /* gdb: p *(m+1) <=> p m[1] and p (m+1) <=> p (m+1) <=> p &(m[1])
1588: &(m[i][j][k]) <=> *((*(m+i) + j)+k)
1589: */
1590: }
1591:
1592: /*************************free ma3x ************************/
1593: void free_ma3x(double ***m, long nrl, long nrh, long ncl, long nch,long nll, long nlh)
1594: {
1595: free((FREE_ARG)(m[nrl][ncl]+ nll-NR_END));
1596: free((FREE_ARG)(m[nrl]+ncl-NR_END));
1597: free((FREE_ARG)(m+nrl-NR_END));
1598: }
1599:
1600: /*************** function subdirf ***********/
1601: char *subdirf(char fileres[])
1602: {
1603: /* Caution optionfilefiname is hidden */
1604: strcpy(tmpout,optionfilefiname);
1605: strcat(tmpout,"/"); /* Add to the right */
1606: strcat(tmpout,fileres);
1607: return tmpout;
1608: }
1609:
1610: /*************** function subdirf2 ***********/
1611: char *subdirf2(char fileres[], char *preop)
1612: {
1613:
1614: /* Caution optionfilefiname is hidden */
1615: strcpy(tmpout,optionfilefiname);
1616: strcat(tmpout,"/");
1617: strcat(tmpout,preop);
1618: strcat(tmpout,fileres);
1619: return tmpout;
1620: }
1621:
1622: /*************** function subdirf3 ***********/
1623: char *subdirf3(char fileres[], char *preop, char *preop2)
1624: {
1625:
1626: /* Caution optionfilefiname is hidden */
1627: strcpy(tmpout,optionfilefiname);
1628: strcat(tmpout,"/");
1629: strcat(tmpout,preop);
1630: strcat(tmpout,preop2);
1631: strcat(tmpout,fileres);
1632: return tmpout;
1633: }
1.213 brouard 1634:
1635: /*************** function subdirfext ***********/
1636: char *subdirfext(char fileres[], char *preop, char *postop)
1637: {
1638:
1639: strcpy(tmpout,preop);
1640: strcat(tmpout,fileres);
1641: strcat(tmpout,postop);
1642: return tmpout;
1643: }
1.126 brouard 1644:
1.213 brouard 1645: /*************** function subdirfext3 ***********/
1646: char *subdirfext3(char fileres[], char *preop, char *postop)
1647: {
1648:
1649: /* Caution optionfilefiname is hidden */
1650: strcpy(tmpout,optionfilefiname);
1651: strcat(tmpout,"/");
1652: strcat(tmpout,preop);
1653: strcat(tmpout,fileres);
1654: strcat(tmpout,postop);
1655: return tmpout;
1656: }
1657:
1.162 brouard 1658: char *asc_diff_time(long time_sec, char ascdiff[])
1659: {
1660: long sec_left, days, hours, minutes;
1661: days = (time_sec) / (60*60*24);
1662: sec_left = (time_sec) % (60*60*24);
1663: hours = (sec_left) / (60*60) ;
1664: sec_left = (sec_left) %(60*60);
1665: minutes = (sec_left) /60;
1666: sec_left = (sec_left) % (60);
1667: sprintf(ascdiff,"%ld day(s) %ld hour(s) %ld minute(s) %ld second(s)",days, hours, minutes, sec_left);
1668: return ascdiff;
1669: }
1670:
1.126 brouard 1671: /***************** f1dim *************************/
1672: extern int ncom;
1673: extern double *pcom,*xicom;
1674: extern double (*nrfunc)(double []);
1675:
1676: double f1dim(double x)
1677: {
1678: int j;
1679: double f;
1680: double *xt;
1681:
1682: xt=vector(1,ncom);
1683: for (j=1;j<=ncom;j++) xt[j]=pcom[j]+x*xicom[j];
1684: f=(*nrfunc)(xt);
1685: free_vector(xt,1,ncom);
1686: return f;
1687: }
1688:
1689: /*****************brent *************************/
1690: double brent(double ax, double bx, double cx, double (*f)(double), double tol, double *xmin)
1.187 brouard 1691: {
1692: /* Given a function f, and given a bracketing triplet of abscissas ax, bx, cx (such that bx is
1693: * between ax and cx, and f(bx) is less than both f(ax) and f(cx) ), this routine isolates
1694: * the minimum to a fractional precision of about tol using Brent’s method. The abscissa of
1695: * the minimum is returned as xmin, and the minimum function value is returned as brent , the
1696: * returned function value.
1697: */
1.126 brouard 1698: int iter;
1699: double a,b,d,etemp;
1.159 brouard 1700: double fu=0,fv,fw,fx;
1.164 brouard 1701: double ftemp=0.;
1.126 brouard 1702: double p,q,r,tol1,tol2,u,v,w,x,xm;
1703: double e=0.0;
1704:
1705: a=(ax < cx ? ax : cx);
1706: b=(ax > cx ? ax : cx);
1707: x=w=v=bx;
1708: fw=fv=fx=(*f)(x);
1709: for (iter=1;iter<=ITMAX;iter++) {
1710: xm=0.5*(a+b);
1711: tol2=2.0*(tol1=tol*fabs(x)+ZEPS);
1712: /* if (2.0*fabs(fp-(*fret)) <= ftol*(fabs(fp)+fabs(*fret)))*/
1713: printf(".");fflush(stdout);
1714: fprintf(ficlog,".");fflush(ficlog);
1.162 brouard 1715: #ifdef DEBUGBRENT
1.126 brouard 1716: 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);
1717: 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);
1718: /* if ((fabs(x-xm) <= (tol2-0.5*(b-a)))||(2.0*fabs(fu-ftemp) <= ftol*1.e-2*(fabs(fu)+fabs(ftemp)))) { */
1719: #endif
1720: if (fabs(x-xm) <= (tol2-0.5*(b-a))){
1721: *xmin=x;
1722: return fx;
1723: }
1724: ftemp=fu;
1725: if (fabs(e) > tol1) {
1726: r=(x-w)*(fx-fv);
1727: q=(x-v)*(fx-fw);
1728: p=(x-v)*q-(x-w)*r;
1729: q=2.0*(q-r);
1730: if (q > 0.0) p = -p;
1731: q=fabs(q);
1732: etemp=e;
1733: e=d;
1734: if (fabs(p) >= fabs(0.5*q*etemp) || p <= q*(a-x) || p >= q*(b-x))
1.224 brouard 1735: d=CGOLD*(e=(x >= xm ? a-x : b-x));
1.126 brouard 1736: else {
1.224 brouard 1737: d=p/q;
1738: u=x+d;
1739: if (u-a < tol2 || b-u < tol2)
1740: d=SIGN(tol1,xm-x);
1.126 brouard 1741: }
1742: } else {
1743: d=CGOLD*(e=(x >= xm ? a-x : b-x));
1744: }
1745: u=(fabs(d) >= tol1 ? x+d : x+SIGN(tol1,d));
1746: fu=(*f)(u);
1747: if (fu <= fx) {
1748: if (u >= x) a=x; else b=x;
1749: SHFT(v,w,x,u)
1.183 brouard 1750: SHFT(fv,fw,fx,fu)
1751: } else {
1752: if (u < x) a=u; else b=u;
1753: if (fu <= fw || w == x) {
1.224 brouard 1754: v=w;
1755: w=u;
1756: fv=fw;
1757: fw=fu;
1.183 brouard 1758: } else if (fu <= fv || v == x || v == w) {
1.224 brouard 1759: v=u;
1760: fv=fu;
1.183 brouard 1761: }
1762: }
1.126 brouard 1763: }
1764: nrerror("Too many iterations in brent");
1765: *xmin=x;
1766: return fx;
1767: }
1768:
1769: /****************** mnbrak ***********************/
1770:
1771: void mnbrak(double *ax, double *bx, double *cx, double *fa, double *fb, double *fc,
1772: double (*func)(double))
1.183 brouard 1773: { /* Given a function func , and given distinct initial points ax and bx , this routine searches in
1774: the downhill direction (defined by the function as evaluated at the initial points) and returns
1775: new points ax , bx , cx that bracket a minimum of the function. Also returned are the function
1776: values at the three points, fa, fb , and fc such that fa > fb and fb < fc.
1777: */
1.126 brouard 1778: double ulim,u,r,q, dum;
1779: double fu;
1.187 brouard 1780:
1781: double scale=10.;
1782: int iterscale=0;
1783:
1784: *fa=(*func)(*ax); /* xta[j]=pcom[j]+(*ax)*xicom[j]; fa=f(xta[j])*/
1785: *fb=(*func)(*bx); /* xtb[j]=pcom[j]+(*bx)*xicom[j]; fb=f(xtb[j]) */
1786:
1787:
1788: /* while(*fb != *fb){ /\* *ax should be ok, reducing distance to *ax *\/ */
1789: /* printf("Warning mnbrak *fb = %lf, *bx=%lf *ax=%lf *fa==%lf iter=%d\n",*fb, *bx, *ax, *fa, iterscale++); */
1790: /* *bx = *ax - (*ax - *bx)/scale; */
1791: /* *fb=(*func)(*bx); /\* xtb[j]=pcom[j]+(*bx)*xicom[j]; fb=f(xtb[j]) *\/ */
1792: /* } */
1793:
1.126 brouard 1794: if (*fb > *fa) {
1795: SHFT(dum,*ax,*bx,dum)
1.183 brouard 1796: SHFT(dum,*fb,*fa,dum)
1797: }
1.126 brouard 1798: *cx=(*bx)+GOLD*(*bx-*ax);
1799: *fc=(*func)(*cx);
1.183 brouard 1800: #ifdef DEBUG
1.224 brouard 1801: printf("mnbrak0 a=%lf *fa=%lf, b=%lf *fb=%lf, c=%lf *fc=%lf\n",*ax,*fa,*bx,*fb,*cx, *fc);
1802: fprintf(ficlog,"mnbrak0 a=%lf *fa=%lf, b=%lf *fb=%lf, c=%lf *fc=%lf\n",*ax,*fa,*bx,*fb,*cx, *fc);
1.183 brouard 1803: #endif
1.224 brouard 1804: while (*fb > *fc) { /* Declining a,b,c with fa> fb > fc. If fc=inf it exits and if flat fb=fc it exits too.*/
1.126 brouard 1805: r=(*bx-*ax)*(*fb-*fc);
1.224 brouard 1806: q=(*bx-*cx)*(*fb-*fa); /* What if fa=inf */
1.126 brouard 1807: u=(*bx)-((*bx-*cx)*q-(*bx-*ax)*r)/
1.183 brouard 1808: (2.0*SIGN(FMAX(fabs(q-r),TINY),q-r)); /* Minimum abscissa of a parabolic estimated from (a,fa), (b,fb) and (c,fc). */
1809: ulim=(*bx)+GLIMIT*(*cx-*bx); /* Maximum abscissa where function should be evaluated */
1810: if ((*bx-u)*(u-*cx) > 0.0) { /* if u_p is between b and c */
1.126 brouard 1811: fu=(*func)(u);
1.163 brouard 1812: #ifdef DEBUG
1813: /* f(x)=A(x-u)**2+f(u) */
1814: double A, fparabu;
1815: A= (*fb - *fa)/(*bx-*ax)/(*bx+*ax-2*u);
1816: fparabu= *fa - A*(*ax-u)*(*ax-u);
1.224 brouard 1817: printf("\nmnbrak (*ax=%.12f, *fa=%.12lf), (*bx=%.12f, *fb=%.12lf), (*cx=%.12f, *fc=%.12lf), (*u=%.12f, fu=%.12lf, fparabu=%.12f, q=%lf < %lf=r)\n",*ax,*fa,*bx,*fb,*cx,*fc,u,fu, fparabu,q,r);
1818: fprintf(ficlog,"\nmnbrak (*ax=%.12f, *fa=%.12lf), (*bx=%.12f, *fb=%.12lf), (*cx=%.12f, *fc=%.12lf), (*u=%.12f, fu=%.12lf, fparabu=%.12f, q=%lf < %lf=r)\n",*ax,*fa,*bx,*fb,*cx,*fc,u,fu, fparabu,q,r);
1.183 brouard 1819: /* And thus,it can be that fu > *fc even if fparabu < *fc */
1820: /* mnbrak (*ax=7.666299858533, *fa=299039.693133272231), (*bx=8.595447774979, *fb=298976.598289369489),
1821: (*cx=10.098840694817, *fc=298946.631474258087), (*u=9.852501168332, fu=298948.773013752128, fparabu=298945.434711494134) */
1822: /* In that case, there is no bracket in the output! Routine is wrong with many consequences.*/
1.163 brouard 1823: #endif
1.184 brouard 1824: #ifdef MNBRAKORIGINAL
1.183 brouard 1825: #else
1.191 brouard 1826: /* if (fu > *fc) { */
1827: /* #ifdef DEBUG */
1828: /* printf("mnbrak4 fu > fc \n"); */
1829: /* fprintf(ficlog, "mnbrak4 fu > fc\n"); */
1830: /* #endif */
1831: /* /\* 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 *\\/ *\/ */
1832: /* /\* SHFT(*fa,*fc,fu,*fc) /\\* (b, u, c) is a bracket while test fb > fc will be fu > fc will exit *\\/ *\/ */
1833: /* dum=u; /\* Shifting c and u *\/ */
1834: /* u = *cx; */
1835: /* *cx = dum; */
1836: /* dum = fu; */
1837: /* fu = *fc; */
1838: /* *fc =dum; */
1839: /* } else { /\* end *\/ */
1840: /* #ifdef DEBUG */
1841: /* printf("mnbrak3 fu < fc \n"); */
1842: /* fprintf(ficlog, "mnbrak3 fu < fc\n"); */
1843: /* #endif */
1844: /* dum=u; /\* Shifting c and u *\/ */
1845: /* u = *cx; */
1846: /* *cx = dum; */
1847: /* dum = fu; */
1848: /* fu = *fc; */
1849: /* *fc =dum; */
1850: /* } */
1.224 brouard 1851: #ifdef DEBUGMNBRAK
1852: double A, fparabu;
1853: A= (*fb - *fa)/(*bx-*ax)/(*bx+*ax-2*u);
1854: fparabu= *fa - A*(*ax-u)*(*ax-u);
1855: printf("\nmnbrak35 ax=%lf fa=%lf bx=%lf fb=%lf, u=%lf fp=%lf fu=%lf < or >= fc=%lf cx=%lf, q=%lf < %lf=r \n",*ax, *fa, *bx,*fb,u,fparabu,fu,*fc,*cx,q,r);
1856: fprintf(ficlog,"\nmnbrak35 ax=%lf fa=%lf bx=%lf fb=%lf, u=%lf fp=%lf fu=%lf < or >= fc=%lf cx=%lf, q=%lf < %lf=r \n",*ax, *fa, *bx,*fb,u,fparabu,fu,*fc,*cx,q,r);
1.183 brouard 1857: #endif
1.191 brouard 1858: dum=u; /* Shifting c and u */
1859: u = *cx;
1860: *cx = dum;
1861: dum = fu;
1862: fu = *fc;
1863: *fc =dum;
1.183 brouard 1864: #endif
1.162 brouard 1865: } else if ((*cx-u)*(u-ulim) > 0.0) { /* u is after c but before ulim */
1.183 brouard 1866: #ifdef DEBUG
1.224 brouard 1867: printf("\nmnbrak2 u=%lf after c=%lf but before ulim\n",u,*cx);
1868: fprintf(ficlog,"\nmnbrak2 u=%lf after c=%lf but before ulim\n",u,*cx);
1.183 brouard 1869: #endif
1.126 brouard 1870: fu=(*func)(u);
1871: if (fu < *fc) {
1.183 brouard 1872: #ifdef DEBUG
1.224 brouard 1873: printf("\nmnbrak2 u=%lf after c=%lf but before ulim=%lf AND fu=%lf < %lf=fc\n",u,*cx,ulim,fu, *fc);
1874: fprintf(ficlog,"\nmnbrak2 u=%lf after c=%lf but before ulim=%lf AND fu=%lf < %lf=fc\n",u,*cx,ulim,fu, *fc);
1875: #endif
1876: SHFT(*bx,*cx,u,*cx+GOLD*(*cx-*bx))
1877: SHFT(*fb,*fc,fu,(*func)(u))
1878: #ifdef DEBUG
1879: printf("\nmnbrak2 shift GOLD c=%lf",*cx+GOLD*(*cx-*bx));
1.183 brouard 1880: #endif
1881: }
1.162 brouard 1882: } else if ((u-ulim)*(ulim-*cx) >= 0.0) { /* u outside ulim (verifying that ulim is beyond c) */
1.183 brouard 1883: #ifdef DEBUG
1.224 brouard 1884: printf("\nmnbrak2 u=%lf outside ulim=%lf (verifying that ulim is beyond c=%lf)\n",u,ulim,*cx);
1885: fprintf(ficlog,"\nmnbrak2 u=%lf outside ulim=%lf (verifying that ulim is beyond c=%lf)\n",u,ulim,*cx);
1.183 brouard 1886: #endif
1.126 brouard 1887: u=ulim;
1888: fu=(*func)(u);
1.183 brouard 1889: } else { /* u could be left to b (if r > q parabola has a maximum) */
1890: #ifdef DEBUG
1.224 brouard 1891: printf("\nmnbrak2 u=%lf could be left to b=%lf (if r=%lf > q=%lf parabola has a maximum)\n",u,*bx,r,q);
1892: fprintf(ficlog,"\nmnbrak2 u=%lf could be left to b=%lf (if r=%lf > q=%lf parabola has a maximum)\n",u,*bx,r,q);
1.183 brouard 1893: #endif
1.126 brouard 1894: u=(*cx)+GOLD*(*cx-*bx);
1895: fu=(*func)(u);
1.224 brouard 1896: #ifdef DEBUG
1897: printf("\nmnbrak2 new u=%lf fu=%lf shifted gold left from c=%lf and b=%lf \n",u,fu,*cx,*bx);
1898: fprintf(ficlog,"\nmnbrak2 new u=%lf fu=%lf shifted gold left from c=%lf and b=%lf \n",u,fu,*cx,*bx);
1899: #endif
1.183 brouard 1900: } /* end tests */
1.126 brouard 1901: SHFT(*ax,*bx,*cx,u)
1.183 brouard 1902: SHFT(*fa,*fb,*fc,fu)
1903: #ifdef DEBUG
1.224 brouard 1904: printf("\nmnbrak2 shift (*ax=%.12f, *fa=%.12lf), (*bx=%.12f, *fb=%.12lf), (*cx=%.12f, *fc=%.12lf)\n",*ax,*fa,*bx,*fb,*cx,*fc);
1905: fprintf(ficlog, "\nmnbrak2 shift (*ax=%.12f, *fa=%.12lf), (*bx=%.12f, *fb=%.12lf), (*cx=%.12f, *fc=%.12lf)\n",*ax,*fa,*bx,*fb,*cx,*fc);
1.183 brouard 1906: #endif
1907: } /* 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 1908: }
1909:
1910: /*************** linmin ************************/
1.162 brouard 1911: /* Given an n -dimensional point p[1..n] and an n -dimensional direction xi[1..n] , moves and
1912: resets p to where the function func(p) takes on a minimum along the direction xi from p ,
1913: and replaces xi by the actual vector displacement that p was moved. Also returns as fret
1914: the value of func at the returned location p . This is actually all accomplished by calling the
1915: routines mnbrak and brent .*/
1.126 brouard 1916: int ncom;
1917: double *pcom,*xicom;
1918: double (*nrfunc)(double []);
1919:
1.224 brouard 1920: #ifdef LINMINORIGINAL
1.126 brouard 1921: void linmin(double p[], double xi[], int n, double *fret,double (*func)(double []))
1.224 brouard 1922: #else
1923: void linmin(double p[], double xi[], int n, double *fret,double (*func)(double []), int *flat)
1924: #endif
1.126 brouard 1925: {
1926: double brent(double ax, double bx, double cx,
1927: double (*f)(double), double tol, double *xmin);
1928: double f1dim(double x);
1929: void mnbrak(double *ax, double *bx, double *cx, double *fa, double *fb,
1930: double *fc, double (*func)(double));
1931: int j;
1932: double xx,xmin,bx,ax;
1933: double fx,fb,fa;
1.187 brouard 1934:
1.203 brouard 1935: #ifdef LINMINORIGINAL
1936: #else
1937: double scale=10., axs, xxs; /* Scale added for infinity */
1938: #endif
1939:
1.126 brouard 1940: ncom=n;
1941: pcom=vector(1,n);
1942: xicom=vector(1,n);
1943: nrfunc=func;
1944: for (j=1;j<=n;j++) {
1945: pcom[j]=p[j];
1.202 brouard 1946: xicom[j]=xi[j]; /* Former scale xi[j] of currrent direction i */
1.126 brouard 1947: }
1.187 brouard 1948:
1.203 brouard 1949: #ifdef LINMINORIGINAL
1950: xx=1.;
1951: #else
1952: axs=0.0;
1953: xxs=1.;
1954: do{
1955: xx= xxs;
1956: #endif
1.187 brouard 1957: ax=0.;
1958: mnbrak(&ax,&xx,&bx,&fa,&fx,&fb,f1dim); /* Outputs: xtx[j]=pcom[j]+(*xx)*xicom[j]; fx=f(xtx[j]) */
1959: /* brackets with inputs ax=0 and xx=1, but points, pcom=p, and directions values, xicom=xi, are sent via f1dim(x) */
1960: /* 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)) */
1961: /* Outputs: fa=f(p(j)) and fx=f(p(j) + xxs * xi(j) ) and f(bx)= f(p(j)+ bx* xi(j)) */
1962: /* Given input ax=axs and xx=xxs, xx might be too far from ax to get a finite f(xx) */
1963: /* Searches on line, outputs (ax, xx, bx) such that fx < min(fa and fb) */
1964: /* 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 1965: #ifdef LINMINORIGINAL
1966: #else
1967: if (fx != fx){
1.224 brouard 1968: xxs=xxs/scale; /* Trying a smaller xx, closer to initial ax=0 */
1969: printf("|");
1970: fprintf(ficlog,"|");
1.203 brouard 1971: #ifdef DEBUGLINMIN
1.224 brouard 1972: printf("\nLinmin NAN : input [axs=%lf:xxs=%lf], mnbrak outputs fx=%lf <(fb=%lf and fa=%lf) with xx=%lf in [ax=%lf:bx=%lf] \n", axs, xxs, fx,fb, fa, xx, ax, bx);
1.203 brouard 1973: #endif
1974: }
1.224 brouard 1975: }while(fx != fx && xxs > 1.e-5);
1.203 brouard 1976: #endif
1977:
1.191 brouard 1978: #ifdef DEBUGLINMIN
1979: 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 1980: 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 1981: #endif
1.224 brouard 1982: #ifdef LINMINORIGINAL
1983: #else
1984: if(fb == fx){ /* Flat function in the direction */
1985: xmin=xx;
1986: *flat=1;
1987: }else{
1988: *flat=0;
1989: #endif
1990: /*Flat mnbrak2 shift (*ax=0.000000000000, *fa=51626.272983130431), (*bx=-1.618034000000, *fb=51590.149499362531), (*cx=-4.236068025156, *fc=51590.149499362531) */
1.187 brouard 1991: *fret=brent(ax,xx,bx,f1dim,TOL,&xmin); /* Giving a bracketting triplet (ax, xx, bx), find a minimum, xmin, according to f1dim, *fret(xmin),*/
1992: /* fa = f(p[j] + ax * xi[j]), fx = f(p[j] + xx * xi[j]), fb = f(p[j] + bx * xi[j]) */
1993: /* fmin = f(p[j] + xmin * xi[j]) */
1994: /* P+lambda n in that direction (lambdamin), with TOL between abscisses */
1995: /* f1dim(xmin): for (j=1;j<=ncom;j++) xt[j]=pcom[j]+xmin*xicom[j]; */
1.126 brouard 1996: #ifdef DEBUG
1.224 brouard 1997: printf("retour brent from bracket (a=%lf fa=%lf, xx=%lf fx=%lf, b=%lf fb=%lf): fret=%lf xmin=%lf\n",ax,fa,xx,fx,bx,fb,*fret,xmin);
1998: fprintf(ficlog,"retour brent from bracket (a=%lf fa=%lf, xx=%lf fx=%lf, b=%lf fb=%lf): fret=%lf xmin=%lf\n",ax,fa,xx,fx,bx,fb,*fret,xmin);
1999: #endif
2000: #ifdef LINMINORIGINAL
2001: #else
2002: }
1.126 brouard 2003: #endif
1.191 brouard 2004: #ifdef DEBUGLINMIN
2005: printf("linmin end ");
1.202 brouard 2006: fprintf(ficlog,"linmin end ");
1.191 brouard 2007: #endif
1.126 brouard 2008: for (j=1;j<=n;j++) {
1.203 brouard 2009: #ifdef LINMINORIGINAL
2010: xi[j] *= xmin;
2011: #else
2012: #ifdef DEBUGLINMIN
2013: if(xxs <1.0)
2014: printf(" before xi[%d]=%12.8f", j,xi[j]);
2015: #endif
2016: 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) */
2017: #ifdef DEBUGLINMIN
2018: if(xxs <1.0)
2019: 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 );
2020: #endif
2021: #endif
1.187 brouard 2022: p[j] += xi[j]; /* Parameters values are updated accordingly */
1.126 brouard 2023: }
1.191 brouard 2024: #ifdef DEBUGLINMIN
1.203 brouard 2025: printf("\n");
1.191 brouard 2026: printf("Comparing last *frec(xmin=%12.8f)=%12.8f from Brent and frec(0.)=%12.8f \n", xmin, *fret, (*func)(p));
1.202 brouard 2027: 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 2028: for (j=1;j<=n;j++) {
1.202 brouard 2029: printf(" xi[%d]= %14.10f p[%d]= %12.7f",j,xi[j],j,p[j]);
2030: fprintf(ficlog," xi[%d]= %14.10f p[%d]= %12.7f",j,xi[j],j,p[j]);
2031: if(j % ncovmodel == 0){
1.191 brouard 2032: printf("\n");
1.202 brouard 2033: fprintf(ficlog,"\n");
2034: }
1.191 brouard 2035: }
1.203 brouard 2036: #else
1.191 brouard 2037: #endif
1.126 brouard 2038: free_vector(xicom,1,n);
2039: free_vector(pcom,1,n);
2040: }
2041:
2042:
2043: /*************** powell ************************/
1.162 brouard 2044: /*
2045: Minimization of a function func of n variables. Input consists of an initial starting point
2046: p[1..n] ; an initial matrix xi[1..n][1..n] , whose columns contain the initial set of di-
2047: rections (usually the n unit vectors); and ftol , the fractional tolerance in the function value
2048: such that failure to decrease by more than this amount on one iteration signals doneness. On
2049: output, p is set to the best point found, xi is the then-current direction set, fret is the returned
2050: function value at p , and iter is the number of iterations taken. The routine linmin is used.
2051: */
1.224 brouard 2052: #ifdef LINMINORIGINAL
2053: #else
2054: int *flatdir; /* Function is vanishing in that direction */
1.225 brouard 2055: int flat=0, flatd=0; /* Function is vanishing in that direction */
1.224 brouard 2056: #endif
1.126 brouard 2057: void powell(double p[], double **xi, int n, double ftol, int *iter, double *fret,
2058: double (*func)(double []))
2059: {
1.224 brouard 2060: #ifdef LINMINORIGINAL
2061: void linmin(double p[], double xi[], int n, double *fret,
1.126 brouard 2062: double (*func)(double []));
1.224 brouard 2063: #else
2064: void linmin(double p[], double xi[], int n, double *fret,
2065: double (*func)(double []),int *flat);
2066: #endif
1.239 ! brouard 2067: int i,ibig,j,jk,k;
1.126 brouard 2068: double del,t,*pt,*ptt,*xit;
1.181 brouard 2069: double directest;
1.126 brouard 2070: double fp,fptt;
2071: double *xits;
2072: int niterf, itmp;
1.224 brouard 2073: #ifdef LINMINORIGINAL
2074: #else
2075:
2076: flatdir=ivector(1,n);
2077: for (j=1;j<=n;j++) flatdir[j]=0;
2078: #endif
1.126 brouard 2079:
2080: pt=vector(1,n);
2081: ptt=vector(1,n);
2082: xit=vector(1,n);
2083: xits=vector(1,n);
2084: *fret=(*func)(p);
2085: for (j=1;j<=n;j++) pt[j]=p[j];
1.202 brouard 2086: rcurr_time = time(NULL);
1.126 brouard 2087: for (*iter=1;;++(*iter)) {
1.187 brouard 2088: fp=(*fret); /* From former iteration or initial value */
1.126 brouard 2089: ibig=0;
2090: del=0.0;
1.157 brouard 2091: rlast_time=rcurr_time;
2092: /* (void) gettimeofday(&curr_time,&tzp); */
2093: rcurr_time = time(NULL);
2094: curr_time = *localtime(&rcurr_time);
2095: printf("\nPowell iter=%d -2*LL=%.12f %ld sec. %ld sec.",*iter,*fret, rcurr_time-rlast_time, rcurr_time-rstart_time);fflush(stdout);
2096: fprintf(ficlog,"\nPowell iter=%d -2*LL=%.12f %ld sec. %ld sec.",*iter,*fret,rcurr_time-rlast_time, rcurr_time-rstart_time); fflush(ficlog);
2097: /* fprintf(ficrespow,"%d %.12f %ld",*iter,*fret,curr_time.tm_sec-start_time.tm_sec); */
1.192 brouard 2098: for (i=1;i<=n;i++) {
1.126 brouard 2099: fprintf(ficrespow," %.12lf", p[i]);
2100: }
1.239 ! brouard 2101: fprintf(ficrespow,"\n");fflush(ficrespow);
! 2102: printf("\n#model= 1 + age ");
! 2103: fprintf(ficlog,"\n#model= 1 + age ");
! 2104: if(nagesqr==1){
! 2105: printf(" + age*age ",Tvar[j]);
! 2106: fprintf(ficlog," + age*age ",Tvar[j]);
! 2107: }
! 2108: for(j=1;j <=ncovmodel-2;j++){
! 2109: if(Typevar[j]==0) {
! 2110: printf(" + V%d ",Tvar[j]);
! 2111: fprintf(ficlog," + V%d ",Tvar[j]);
! 2112: }else if(Typevar[j]==1) {
! 2113: printf(" + V%d*age ",Tvar[j]);
! 2114: fprintf(ficlog," + V%d*age ",Tvar[j]);
! 2115: }else if(Typevar[j]==2) {
! 2116: printf(" + V%d*V%d ",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
! 2117: fprintf(ficlog," + V%d*V%d ",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
! 2118: }
! 2119: }
1.126 brouard 2120: printf("\n");
1.239 ! brouard 2121: /* printf("12 47.0114589 0.0154322 33.2424412 0.3279905 2.3731903 */
! 2122: /* 13 -21.5392400 0.1118147 1.2680506 1.2973408 -1.0663662 */
1.126 brouard 2123: fprintf(ficlog,"\n");
1.239 ! brouard 2124: for(i=1,jk=1; i <=nlstate; i++){
! 2125: for(k=1; k <=(nlstate+ndeath); k++){
! 2126: if (k != i) {
! 2127: printf("%d%d ",i,k);
! 2128: fprintf(ficlog,"%d%d ",i,k);
! 2129: fprintf(ficres,"%1d%1d ",i,k);
! 2130: for(j=1; j <=ncovmodel; j++){
! 2131: printf("%12.7f ",p[jk]);
! 2132: fprintf(ficlog,"%12.7f ",p[jk]);
! 2133: fprintf(ficres,"%12.7f ",p[jk]);
! 2134: jk++;
! 2135: }
! 2136: printf("\n");
! 2137: fprintf(ficlog,"\n");
! 2138: fprintf(ficres,"\n");
! 2139: }
! 2140: }
! 2141: }
1.126 brouard 2142: if(*iter <=3){
1.157 brouard 2143: tml = *localtime(&rcurr_time);
2144: strcpy(strcurr,asctime(&tml));
2145: rforecast_time=rcurr_time;
1.126 brouard 2146: itmp = strlen(strcurr);
2147: if(strcurr[itmp-1]=='\n') /* Windows outputs with a new line */
1.224 brouard 2148: strcurr[itmp-1]='\0';
1.162 brouard 2149: printf("\nConsidering the time needed for the last iteration #%d: %ld seconds,\n",*iter,rcurr_time-rlast_time);
1.157 brouard 2150: fprintf(ficlog,"\nConsidering the time needed for this last iteration #%d: %ld seconds,\n",*iter,rcurr_time-rlast_time);
1.126 brouard 2151: for(niterf=10;niterf<=30;niterf+=10){
1.224 brouard 2152: rforecast_time=rcurr_time+(niterf-*iter)*(rcurr_time-rlast_time);
2153: forecast_time = *localtime(&rforecast_time);
2154: strcpy(strfor,asctime(&forecast_time));
2155: itmp = strlen(strfor);
2156: if(strfor[itmp-1]=='\n')
2157: strfor[itmp-1]='\0';
2158: 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);
2159: 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 2160: }
2161: }
1.187 brouard 2162: for (i=1;i<=n;i++) { /* For each direction i */
2163: for (j=1;j<=n;j++) xit[j]=xi[j][i]; /* Directions stored from previous iteration with previous scales */
1.126 brouard 2164: fptt=(*fret);
2165: #ifdef DEBUG
1.203 brouard 2166: printf("fret=%lf, %lf, %lf \n", *fret, *fret, *fret);
2167: fprintf(ficlog, "fret=%lf, %lf, %lf \n", *fret, *fret, *fret);
1.126 brouard 2168: #endif
1.203 brouard 2169: printf("%d",i);fflush(stdout); /* print direction (parameter) i */
1.126 brouard 2170: fprintf(ficlog,"%d",i);fflush(ficlog);
1.224 brouard 2171: #ifdef LINMINORIGINAL
1.188 brouard 2172: linmin(p,xit,n,fret,func); /* Point p[n]. xit[n] has been loaded for direction i as input.*/
1.224 brouard 2173: #else
2174: linmin(p,xit,n,fret,func,&flat); /* Point p[n]. xit[n] has been loaded for direction i as input.*/
2175: flatdir[i]=flat; /* Function is vanishing in that direction i */
2176: #endif
2177: /* Outputs are fret(new point p) p is updated and xit rescaled */
1.188 brouard 2178: if (fabs(fptt-(*fret)) > del) { /* We are keeping the max gain on each of the n directions */
1.224 brouard 2179: /* because that direction will be replaced unless the gain del is small */
2180: /* in comparison with the 'probable' gain, mu^2, with the last average direction. */
2181: /* Unless the n directions are conjugate some gain in the determinant may be obtained */
2182: /* with the new direction. */
2183: del=fabs(fptt-(*fret));
2184: ibig=i;
1.126 brouard 2185: }
2186: #ifdef DEBUG
2187: printf("%d %.12e",i,(*fret));
2188: fprintf(ficlog,"%d %.12e",i,(*fret));
2189: for (j=1;j<=n;j++) {
1.224 brouard 2190: xits[j]=FMAX(fabs(p[j]-pt[j]),1.e-5);
2191: printf(" x(%d)=%.12e",j,xit[j]);
2192: fprintf(ficlog," x(%d)=%.12e",j,xit[j]);
1.126 brouard 2193: }
2194: for(j=1;j<=n;j++) {
1.225 brouard 2195: printf(" p(%d)=%.12e",j,p[j]);
2196: fprintf(ficlog," p(%d)=%.12e",j,p[j]);
1.126 brouard 2197: }
2198: printf("\n");
2199: fprintf(ficlog,"\n");
2200: #endif
1.187 brouard 2201: } /* end loop on each direction i */
2202: /* Convergence test will use last linmin estimation (fret) and compare former iteration (fp) */
1.188 brouard 2203: /* But p and xit have been updated at the end of linmin, *fret corresponds to new p, xit */
1.187 brouard 2204: /* New value of last point Pn is not computed, P(n-1) */
1.224 brouard 2205: for(j=1;j<=n;j++) {
1.225 brouard 2206: if(flatdir[j] >0){
2207: printf(" p(%d)=%lf flat=%d ",j,p[j],flatdir[j]);
2208: fprintf(ficlog," p(%d)=%lf flat=%d ",j,p[j],flatdir[j]);
2209: }
2210: /* printf("\n"); */
2211: /* fprintf(ficlog,"\n"); */
2212: }
1.182 brouard 2213: if (2.0*fabs(fp-(*fret)) <= ftol*(fabs(fp)+fabs(*fret))) { /* Did we reach enough precision? */
1.188 brouard 2214: /* We could compare with a chi^2. chisquare(0.95,ddl=1)=3.84 */
2215: /* By adding age*age in a model, the new -2LL should be lower and the difference follows a */
2216: /* a chisquare statistics with 1 degree. To be significant at the 95% level, it should have */
2217: /* decreased of more than 3.84 */
2218: /* By adding age*age and V1*age the gain (-2LL) should be more than 5.99 (ddl=2) */
2219: /* By using V1+V2+V3, the gain should be 7.82, compared with basic 1+age. */
2220: /* By adding 10 parameters more the gain should be 18.31 */
1.224 brouard 2221:
1.188 brouard 2222: /* Starting the program with initial values given by a former maximization will simply change */
2223: /* the scales of the directions and the directions, because the are reset to canonical directions */
2224: /* Thus the first calls to linmin will give new points and better maximizations until fp-(*fret) is */
2225: /* under the tolerance value. If the tolerance is very small 1.e-9, it could last long. */
1.126 brouard 2226: #ifdef DEBUG
2227: int k[2],l;
2228: k[0]=1;
2229: k[1]=-1;
2230: printf("Max: %.12e",(*func)(p));
2231: fprintf(ficlog,"Max: %.12e",(*func)(p));
2232: for (j=1;j<=n;j++) {
2233: printf(" %.12e",p[j]);
2234: fprintf(ficlog," %.12e",p[j]);
2235: }
2236: printf("\n");
2237: fprintf(ficlog,"\n");
2238: for(l=0;l<=1;l++) {
2239: for (j=1;j<=n;j++) {
2240: ptt[j]=p[j]+(p[j]-pt[j])*k[l];
2241: printf("l=%d j=%d ptt=%.12e, xits=%.12e, p=%.12e, xit=%.12e", l,j,ptt[j],xits[j],p[j],xit[j]);
2242: fprintf(ficlog,"l=%d j=%d ptt=%.12e, xits=%.12e, p=%.12e, xit=%.12e", l,j,ptt[j],xits[j],p[j],xit[j]);
2243: }
2244: printf("func(ptt)=%.12e, deriv=%.12e\n",(*func)(ptt),(ptt[j]-p[j])/((*func)(ptt)-(*func)(p)));
2245: fprintf(ficlog,"func(ptt)=%.12e, deriv=%.12e\n",(*func)(ptt),(ptt[j]-p[j])/((*func)(ptt)-(*func)(p)));
2246: }
2247: #endif
2248:
1.224 brouard 2249: #ifdef LINMINORIGINAL
2250: #else
2251: free_ivector(flatdir,1,n);
2252: #endif
1.126 brouard 2253: free_vector(xit,1,n);
2254: free_vector(xits,1,n);
2255: free_vector(ptt,1,n);
2256: free_vector(pt,1,n);
2257: return;
1.192 brouard 2258: } /* enough precision */
1.126 brouard 2259: if (*iter == ITMAX) nrerror("powell exceeding maximum iterations.");
1.181 brouard 2260: for (j=1;j<=n;j++) { /* Computes the extrapolated point P_0 + 2 (P_n-P_0) */
1.126 brouard 2261: ptt[j]=2.0*p[j]-pt[j];
2262: xit[j]=p[j]-pt[j];
2263: pt[j]=p[j];
2264: }
1.181 brouard 2265: fptt=(*func)(ptt); /* f_3 */
1.224 brouard 2266: #ifdef NODIRECTIONCHANGEDUNTILNITER /* No change in drections until some iterations are done */
2267: if (*iter <=4) {
1.225 brouard 2268: #else
2269: #endif
1.224 brouard 2270: #ifdef POWELLNOF3INFF1TEST /* skips test F3 <F1 */
1.192 brouard 2271: #else
1.161 brouard 2272: if (fptt < fp) { /* If extrapolated point is better, decide if we keep that new direction or not */
1.192 brouard 2273: #endif
1.162 brouard 2274: /* (x1 f1=fp), (x2 f2=*fret), (x3 f3=fptt), (xm fm) */
1.161 brouard 2275: /* From x1 (P0) distance of x2 is at h and x3 is 2h */
1.162 brouard 2276: /* Let f"(x2) be the 2nd derivative equal everywhere. */
2277: /* Then the parabolic through (x1,f1), (x2,f2) and (x3,f3) */
2278: /* will reach at f3 = fm + h^2/2 f"m ; f" = (f1 -2f2 +f3 ) / h**2 */
1.224 brouard 2279: /* Conditional for using this new direction is that mu^2 = (f1-2f2+f3)^2 /2 < del or directest <0 */
2280: /* also lamda^2=(f1-f2)^2/mu² is a parasite solution of powell */
2281: /* For powell, inclusion of this average direction is only if t(del)<0 or del inbetween mu^2 and lambda^2 */
1.161 brouard 2282: /* t=2.0*(fp-2.0*(*fret)+fptt)*SQR(fp-(*fret)-del)-del*SQR(fp-fptt); */
1.224 brouard 2283: /* Even if f3 <f1, directest can be negative and t >0 */
2284: /* mu² and del² are equal when f3=f1 */
2285: /* f3 < f1 : mu² < del <= lambda^2 both test are equivalent */
2286: /* f3 < f1 : mu² < lambda^2 < del then directtest is negative and powell t is positive */
2287: /* f3 > f1 : lambda² < mu^2 < del then t is negative and directest >0 */
2288: /* f3 > f1 : lambda² < del < mu^2 then t is positive and directest >0 */
1.183 brouard 2289: #ifdef NRCORIGINAL
2290: t=2.0*(fp-2.0*(*fret)+fptt)*SQR(fp-(*fret)-del)- del*SQR(fp-fptt); /* Original Numerical Recipes in C*/
2291: #else
2292: 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 2293: t= t- del*SQR(fp-fptt);
1.183 brouard 2294: #endif
1.202 brouard 2295: directest = fp-2.0*(*fret)+fptt - 2.0 * del; /* If delta was big enough we change it for a new direction */
1.161 brouard 2296: #ifdef DEBUG
1.181 brouard 2297: 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);
2298: 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 2299: printf("t3= %.12lf, t4= %.12lf, t3*= %.12lf, t4*= %.12lf\n",SQR(fp-(*fret)-del),SQR(fp-fptt),
2300: (fp-(*fret)-del)*(fp-(*fret)-del),(fp-fptt)*(fp-fptt));
2301: fprintf(ficlog,"t3= %.12lf, t4= %.12lf, t3*= %.12lf, t4*= %.12lf\n",SQR(fp-(*fret)-del),SQR(fp-fptt),
2302: (fp-(*fret)-del)*(fp-(*fret)-del),(fp-fptt)*(fp-fptt));
2303: 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);
2304: 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);
2305: #endif
1.183 brouard 2306: #ifdef POWELLORIGINAL
2307: if (t < 0.0) { /* Then we use it for new direction */
2308: #else
1.182 brouard 2309: if (directest*t < 0.0) { /* Contradiction between both tests */
1.224 brouard 2310: 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 2311: printf("f1-2f2+f3= %.12lf, f1-f2-del= %.12lf, f1-f3= %.12lf\n",fp-2.0*(*fret)+fptt, fp -(*fret) -del, fp-fptt);
1.224 brouard 2312: fprintf(ficlog,"directest= %.12lf (if directest<0 or t<0 we include P0 Pn as new direction), t= %.12lf, f1= %.12lf,f2= %.12lf,f3= %.12lf, del= %.12lf\n",directest, t, fp,(*fret),fptt, del);
1.192 brouard 2313: fprintf(ficlog,"f1-2f2+f3= %.12lf, f1-f2-del= %.12lf, f1-f3= %.12lf\n",fp-2.0*(*fret)+fptt, fp -(*fret) -del, fp-fptt);
2314: }
1.181 brouard 2315: if (directest < 0.0) { /* Then we use it for new direction */
2316: #endif
1.191 brouard 2317: #ifdef DEBUGLINMIN
1.234 brouard 2318: printf("Before linmin in direction P%d-P0\n",n);
2319: for (j=1;j<=n;j++) {
2320: printf(" Before xit[%d]= %12.7f p[%d]= %12.7f",j,xit[j],j,p[j]);
2321: fprintf(ficlog," Before xit[%d]= %12.7f p[%d]= %12.7f",j,xit[j],j,p[j]);
2322: if(j % ncovmodel == 0){
2323: printf("\n");
2324: fprintf(ficlog,"\n");
2325: }
2326: }
1.224 brouard 2327: #endif
2328: #ifdef LINMINORIGINAL
1.234 brouard 2329: linmin(p,xit,n,fret,func); /* computes minimum on the extrapolated direction: changes p and rescales xit.*/
1.224 brouard 2330: #else
1.234 brouard 2331: linmin(p,xit,n,fret,func,&flat); /* computes minimum on the extrapolated direction: changes p and rescales xit.*/
2332: flatdir[i]=flat; /* Function is vanishing in that direction i */
1.191 brouard 2333: #endif
1.234 brouard 2334:
1.191 brouard 2335: #ifdef DEBUGLINMIN
1.234 brouard 2336: for (j=1;j<=n;j++) {
2337: printf("After xit[%d]= %12.7f p[%d]= %12.7f",j,xit[j],j,p[j]);
2338: fprintf(ficlog,"After xit[%d]= %12.7f p[%d]= %12.7f",j,xit[j],j,p[j]);
2339: if(j % ncovmodel == 0){
2340: printf("\n");
2341: fprintf(ficlog,"\n");
2342: }
2343: }
1.224 brouard 2344: #endif
1.234 brouard 2345: for (j=1;j<=n;j++) {
2346: xi[j][ibig]=xi[j][n]; /* Replace direction with biggest decrease by last direction n */
2347: xi[j][n]=xit[j]; /* and this nth direction by the by the average p_0 p_n */
2348: }
1.224 brouard 2349: #ifdef LINMINORIGINAL
2350: #else
1.234 brouard 2351: for (j=1, flatd=0;j<=n;j++) {
2352: if(flatdir[j]>0)
2353: flatd++;
2354: }
2355: if(flatd >0){
2356: printf("%d flat directions\n",flatd);
2357: fprintf(ficlog,"%d flat directions\n",flatd);
2358: for (j=1;j<=n;j++) {
2359: if(flatdir[j]>0){
2360: printf("%d ",j);
2361: fprintf(ficlog,"%d ",j);
2362: }
2363: }
2364: printf("\n");
2365: fprintf(ficlog,"\n");
2366: }
1.191 brouard 2367: #endif
1.234 brouard 2368: printf("Gaining to use new average direction of P0 P%d instead of biggest increase direction %d :\n",n,ibig);
2369: fprintf(ficlog,"Gaining to use new average direction of P0 P%d instead of biggest increase direction %d :\n",n,ibig);
2370:
1.126 brouard 2371: #ifdef DEBUG
1.234 brouard 2372: printf("Direction changed last moved %d in place of ibig=%d, new last is the average:\n",n,ibig);
2373: fprintf(ficlog,"Direction changed last moved %d in place of ibig=%d, new last is the average:\n",n,ibig);
2374: for(j=1;j<=n;j++){
2375: printf(" %lf",xit[j]);
2376: fprintf(ficlog," %lf",xit[j]);
2377: }
2378: printf("\n");
2379: fprintf(ficlog,"\n");
1.126 brouard 2380: #endif
1.192 brouard 2381: } /* end of t or directest negative */
1.224 brouard 2382: #ifdef POWELLNOF3INFF1TEST
1.192 brouard 2383: #else
1.234 brouard 2384: } /* end if (fptt < fp) */
1.192 brouard 2385: #endif
1.225 brouard 2386: #ifdef NODIRECTIONCHANGEDUNTILNITER /* No change in drections until some iterations are done */
1.234 brouard 2387: } /*NODIRECTIONCHANGEDUNTILNITER No change in drections until some iterations are done */
1.225 brouard 2388: #else
1.224 brouard 2389: #endif
1.234 brouard 2390: } /* loop iteration */
1.126 brouard 2391: }
1.234 brouard 2392:
1.126 brouard 2393: /**** Prevalence limit (stable or period prevalence) ****************/
1.234 brouard 2394:
1.235 brouard 2395: double **prevalim(double **prlim, int nlstate, double x[], double age, double **oldm, double **savm, double ftolpl, int *ncvyear, int ij, int nres)
1.234 brouard 2396: {
1.235 brouard 2397: /* Computes the prevalence limit in each live state at age x and for covariate combination ij
2398: (and selected quantitative values in nres)
2399: by left multiplying the unit
1.234 brouard 2400: matrix by transitions matrix until convergence is reached with precision ftolpl */
1.206 brouard 2401: /* Wx= Wx-1 Px-1= Wx-2 Px-2 Px-1 = Wx-n Px-n ... Px-2 Px-1 I */
2402: /* Wx is row vector: population in state 1, population in state 2, population dead */
2403: /* or prevalence in state 1, prevalence in state 2, 0 */
2404: /* newm is the matrix after multiplications, its rows are identical at a factor */
2405: /* Initial matrix pimij */
2406: /* {0.85204250825084937, 0.13044499163996345, 0.017512500109187184, */
2407: /* 0.090851990222114765, 0.88271245433047185, 0.026435555447413338, */
2408: /* 0, 0 , 1} */
2409: /*
2410: * and after some iteration: */
2411: /* {0.45504275246439968, 0.42731458730878791, 0.11764266022681241, */
2412: /* 0.45201005341706885, 0.42865420071559901, 0.11933574586733192, */
2413: /* 0, 0 , 1} */
2414: /* And prevalence by suppressing the deaths are close to identical rows in prlim: */
2415: /* {0.51571254859325999, 0.4842874514067399, */
2416: /* 0.51326036147820708, 0.48673963852179264} */
2417: /* If we start from prlim again, prlim tends to a constant matrix */
1.234 brouard 2418:
1.126 brouard 2419: int i, ii,j,k;
1.209 brouard 2420: double *min, *max, *meandiff, maxmax,sumnew=0.;
1.145 brouard 2421: /* double **matprod2(); */ /* test */
1.218 brouard 2422: double **out, cov[NCOVMAX+1], **pmij(); /* **pmmij is a global variable feeded with oldms etc */
1.126 brouard 2423: double **newm;
1.209 brouard 2424: double agefin, delaymax=200. ; /* 100 Max number of years to converge */
1.203 brouard 2425: int ncvloop=0;
1.169 brouard 2426:
1.209 brouard 2427: min=vector(1,nlstate);
2428: max=vector(1,nlstate);
2429: meandiff=vector(1,nlstate);
2430:
1.218 brouard 2431: /* Starting with matrix unity */
1.126 brouard 2432: for (ii=1;ii<=nlstate+ndeath;ii++)
2433: for (j=1;j<=nlstate+ndeath;j++){
2434: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
2435: }
1.169 brouard 2436:
2437: cov[1]=1.;
2438:
2439: /* Even if hstepm = 1, at least one multiplication by the unit matrix */
1.202 brouard 2440: /* Start at agefin= age, computes the matrix of passage and loops decreasing agefin until convergence is reached */
1.126 brouard 2441: for(agefin=age-stepm/YEARM; agefin>=age-delaymax; agefin=agefin-stepm/YEARM){
1.202 brouard 2442: ncvloop++;
1.126 brouard 2443: newm=savm;
2444: /* Covariates have to be included here again */
1.138 brouard 2445: cov[2]=agefin;
1.187 brouard 2446: if(nagesqr==1)
2447: cov[3]= agefin*agefin;;
1.234 brouard 2448: for (k=1; k<=nsd;k++) { /* For single dummy covariates only */
2449: /* Here comes the value of the covariate 'ij' after renumbering k with single dummy covariates */
2450: cov[2+nagesqr+TvarsDind[k]]=nbcode[TvarsD[k]][codtabm(ij,k)];
1.235 brouard 2451: /* printf("prevalim Dummy combi=%d k=%d TvarsD[%d]=V%d TvarsDind[%d]=%d nbcode=%d cov=%lf codtabm(%d,Tvar[%d])=%d \n",ij,k, k, TvarsD[k],k,TvarsDind[k],nbcode[TvarsD[k]][codtabm(ij,k)],cov[2+nagesqr+TvarsDind[k]], ij, k, codtabm(ij,k)); */
1.234 brouard 2452: }
2453: for (k=1; k<=nsq;k++) { /* For single varying covariates only */
2454: /* Here comes the value of quantitative after renumbering k with single quantitative covariates */
1.235 brouard 2455: cov[2+nagesqr+TvarsQind[k]]=Tqresult[nres][k];
2456: /* printf("prevalim Quantitative k=%d TvarsQind[%d]=%d, TvarsQ[%d]=V%d,Tqresult[%d][%d]=%f\n",k,k,TvarsQind[k],k,TvarsQ[k],nres,k,Tqresult[nres][k]); */
1.138 brouard 2457: }
1.237 brouard 2458: for (k=1; k<=cptcovage;k++){ /* For product with age */
1.234 brouard 2459: if(Dummy[Tvar[Tage[k]]]){
2460: cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,k)]*cov[2];
2461: } else{
1.235 brouard 2462: cov[2+nagesqr+Tage[k]]=Tqresult[nres][k];
1.234 brouard 2463: }
1.235 brouard 2464: /* printf("prevalim Age combi=%d k=%d Tage[%d]=V%d Tqresult[%d][%d]=%f\n",ij,k,k,Tage[k],nres,k,Tqresult[nres][k]); */
1.234 brouard 2465: }
1.237 brouard 2466: for (k=1; k<=cptcovprod;k++){ /* For product without age */
1.235 brouard 2467: /* printf("prevalim Prod ij=%d k=%d Tprod[%d]=%d Tvard[%d][1]=V%d, Tvard[%d][2]=V%d\n",ij,k,k,Tprod[k], k,Tvard[k][1], k,Tvard[k][2]); */
1.237 brouard 2468: if(Dummy[Tvard[k][1]==0]){
2469: if(Dummy[Tvard[k][2]==0]){
2470: cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)] * nbcode[Tvard[k][2]][codtabm(ij,k)];
2471: }else{
2472: cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)] * Tqresult[nres][k];
2473: }
2474: }else{
2475: if(Dummy[Tvard[k][2]==0]){
2476: cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][2]][codtabm(ij,k)] * Tqinvresult[nres][Tvard[k][1]];
2477: }else{
2478: cov[2+nagesqr+Tprod[k]]=Tqinvresult[nres][Tvard[k][1]]* Tqinvresult[nres][Tvard[k][2]];
2479: }
2480: }
1.234 brouard 2481: }
1.138 brouard 2482: /*printf("ij=%d cptcovprod=%d tvar=%d ", ij, cptcovprod, Tvar[1]);*/
2483: /*printf("ij=%d cov[3]=%lf cov[4]=%lf \n",ij, cov[3],cov[4]);*/
2484: /*printf("ij=%d cov[3]=%lf \n",ij, cov[3]);*/
1.145 brouard 2485: /* savm=pmij(pmmij,cov,ncovmodel,x,nlstate); */
2486: /* out=matprod2(newm, pmij(pmmij,cov,ncovmodel,x,nlstate),1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm); /\* Bug Valgrind *\/ */
1.218 brouard 2487: /* age and covariate values of ij are in 'cov' */
1.142 brouard 2488: out=matprod2(newm, pmij(pmmij,cov,ncovmodel,x,nlstate),1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm); /* Bug Valgrind */
1.138 brouard 2489:
1.126 brouard 2490: savm=oldm;
2491: oldm=newm;
1.209 brouard 2492:
2493: for(j=1; j<=nlstate; j++){
2494: max[j]=0.;
2495: min[j]=1.;
2496: }
2497: for(i=1;i<=nlstate;i++){
2498: sumnew=0;
2499: for(k=1; k<=ndeath; k++) sumnew+=newm[i][nlstate+k];
2500: for(j=1; j<=nlstate; j++){
2501: prlim[i][j]= newm[i][j]/(1-sumnew);
2502: max[j]=FMAX(max[j],prlim[i][j]);
2503: min[j]=FMIN(min[j],prlim[i][j]);
2504: }
2505: }
2506:
1.126 brouard 2507: maxmax=0.;
1.209 brouard 2508: for(j=1; j<=nlstate; j++){
2509: meandiff[j]=(max[j]-min[j])/(max[j]+min[j])*2.; /* mean difference for each column */
2510: maxmax=FMAX(maxmax,meandiff[j]);
2511: /* 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 2512: } /* j loop */
1.203 brouard 2513: *ncvyear= (int)age- (int)agefin;
1.208 brouard 2514: /* 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 2515: if(maxmax < ftolpl){
1.209 brouard 2516: /* printf("maxmax=%lf ncvloop=%ld, age=%d, agefin=%d ncvyear=%d \n", maxmax, ncvloop, (int)age, (int)agefin, *ncvyear); */
2517: free_vector(min,1,nlstate);
2518: free_vector(max,1,nlstate);
2519: free_vector(meandiff,1,nlstate);
1.126 brouard 2520: return prlim;
2521: }
1.169 brouard 2522: } /* age loop */
1.208 brouard 2523: /* After some age loop it doesn't converge */
1.209 brouard 2524: 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 2525: 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 2526: /* 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); */
2527: free_vector(min,1,nlstate);
2528: free_vector(max,1,nlstate);
2529: free_vector(meandiff,1,nlstate);
1.208 brouard 2530:
1.169 brouard 2531: return prlim; /* should not reach here */
1.126 brouard 2532: }
2533:
1.217 brouard 2534:
2535: /**** Back Prevalence limit (stable or period prevalence) ****************/
2536:
1.218 brouard 2537: /* 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) */
2538: /* 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) */
2539: double **bprevalim(double **bprlim, double ***prevacurrent, int nlstate, double x[], double age, double ftolpl, int *ncvyear, int ij)
1.217 brouard 2540: {
1.218 brouard 2541: /* Computes the prevalence limit in each live state at age x and covariate ij by left multiplying the unit
1.217 brouard 2542: matrix by transitions matrix until convergence is reached with precision ftolpl */
2543: /* Wx= Wx-1 Px-1= Wx-2 Px-2 Px-1 = Wx-n Px-n ... Px-2 Px-1 I */
2544: /* Wx is row vector: population in state 1, population in state 2, population dead */
2545: /* or prevalence in state 1, prevalence in state 2, 0 */
2546: /* newm is the matrix after multiplications, its rows are identical at a factor */
2547: /* Initial matrix pimij */
2548: /* {0.85204250825084937, 0.13044499163996345, 0.017512500109187184, */
2549: /* 0.090851990222114765, 0.88271245433047185, 0.026435555447413338, */
2550: /* 0, 0 , 1} */
2551: /*
2552: * and after some iteration: */
2553: /* {0.45504275246439968, 0.42731458730878791, 0.11764266022681241, */
2554: /* 0.45201005341706885, 0.42865420071559901, 0.11933574586733192, */
2555: /* 0, 0 , 1} */
2556: /* And prevalence by suppressing the deaths are close to identical rows in prlim: */
2557: /* {0.51571254859325999, 0.4842874514067399, */
2558: /* 0.51326036147820708, 0.48673963852179264} */
2559: /* If we start from prlim again, prlim tends to a constant matrix */
2560:
2561: int i, ii,j,k;
2562: double *min, *max, *meandiff, maxmax,sumnew=0.;
2563: /* double **matprod2(); */ /* test */
2564: double **out, cov[NCOVMAX+1], **bmij();
2565: double **newm;
1.218 brouard 2566: double **dnewm, **doldm, **dsavm; /* for use */
2567: double **oldm, **savm; /* for use */
2568:
1.217 brouard 2569: double agefin, delaymax=200. ; /* 100 Max number of years to converge */
2570: int ncvloop=0;
2571:
2572: min=vector(1,nlstate);
2573: max=vector(1,nlstate);
2574: meandiff=vector(1,nlstate);
2575:
1.218 brouard 2576: dnewm=ddnewms; doldm=ddoldms; dsavm=ddsavms;
2577: oldm=oldms; savm=savms;
2578:
2579: /* Starting with matrix unity */
2580: for (ii=1;ii<=nlstate+ndeath;ii++)
2581: for (j=1;j<=nlstate+ndeath;j++){
1.217 brouard 2582: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
2583: }
2584:
2585: cov[1]=1.;
2586:
2587: /* Even if hstepm = 1, at least one multiplication by the unit matrix */
2588: /* Start at agefin= age, computes the matrix of passage and loops decreasing agefin until convergence is reached */
1.218 brouard 2589: /* for(agefin=age+stepm/YEARM; agefin<=age+delaymax; agefin=agefin+stepm/YEARM){ /\* A changer en age *\/ */
2590: for(agefin=age; agefin<AGESUP; agefin=agefin+stepm/YEARM){ /* A changer en age */
1.217 brouard 2591: ncvloop++;
1.218 brouard 2592: newm=savm; /* oldm should be kept from previous iteration or unity at start */
2593: /* newm points to the allocated table savm passed by the function it can be written, savm could be reallocated */
1.217 brouard 2594: /* Covariates have to be included here again */
2595: cov[2]=agefin;
2596: if(nagesqr==1)
2597: cov[3]= agefin*agefin;;
2598: for (k=1; k<=cptcovn;k++) {
2599: /* cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,Tvar[k])]; */
2600: cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,k)];
2601: /* 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])]); */
2602: }
2603: for (k=1; k<=cptcovage;k++) cov[2+nagesqr+Tage[k]]=nbcode[Tvar[k]][codtabm(ij,k)]*cov[2];
2604: for (k=1; k<=cptcovprod;k++) /* Useless */
2605: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,Tvard[k][1])] * nbcode[Tvard[k][2]][codtabm(ij,Tvard[k][2])]; */
2606: cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)] * nbcode[Tvard[k][2]][codtabm(ij,k)];
2607:
2608: /*printf("ij=%d cptcovprod=%d tvar=%d ", ij, cptcovprod, Tvar[1]);*/
2609: /*printf("ij=%d cov[3]=%lf cov[4]=%lf \n",ij, cov[3],cov[4]);*/
2610: /*printf("ij=%d cov[3]=%lf \n",ij, cov[3]);*/
2611: /* savm=pmij(pmmij,cov,ncovmodel,x,nlstate); */
2612: /* out=matprod2(newm, pmij(pmmij,cov,ncovmodel,x,nlstate),1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm); /\* Bug Valgrind *\/ */
1.218 brouard 2613: /* ij should be linked to the correct index of cov */
2614: /* age and covariate values ij are in 'cov', but we need to pass
2615: * ij for the observed prevalence at age and status and covariate
2616: * number: prevacurrent[(int)agefin][ii][ij]
2617: */
2618: /* 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 *\/ */
2619: /* 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 *\/ */
2620: 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 2621: savm=oldm;
2622: oldm=newm;
2623: for(j=1; j<=nlstate; j++){
2624: max[j]=0.;
2625: min[j]=1.;
2626: }
2627: for(j=1; j<=nlstate; j++){
2628: for(i=1;i<=nlstate;i++){
1.234 brouard 2629: /* bprlim[i][j]= newm[i][j]/(1-sumnew); */
2630: bprlim[i][j]= newm[i][j];
2631: max[i]=FMAX(max[i],bprlim[i][j]); /* Max in line */
2632: min[i]=FMIN(min[i],bprlim[i][j]);
1.217 brouard 2633: }
2634: }
1.218 brouard 2635:
1.217 brouard 2636: maxmax=0.;
2637: for(i=1; i<=nlstate; i++){
2638: meandiff[i]=(max[i]-min[i])/(max[i]+min[i])*2.; /* mean difference for each column */
2639: maxmax=FMAX(maxmax,meandiff[i]);
2640: /* 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); */
2641: } /* j loop */
2642: *ncvyear= -( (int)age- (int)agefin);
1.218 brouard 2643: /* printf("Back maxmax=%lf ncvloop=%d, age=%d, agefin=%d ncvyear=%d \n", maxmax, ncvloop, (int)age, (int)agefin, *ncvyear);*/
1.217 brouard 2644: if(maxmax < ftolpl){
1.220 brouard 2645: /* printf("OK Back maxmax=%lf ncvloop=%d, age=%d, agefin=%d ncvyear=%d \n", maxmax, ncvloop, (int)age, (int)agefin, *ncvyear); */
1.217 brouard 2646: free_vector(min,1,nlstate);
2647: free_vector(max,1,nlstate);
2648: free_vector(meandiff,1,nlstate);
2649: return bprlim;
2650: }
2651: } /* age loop */
2652: /* After some age loop it doesn't converge */
2653: 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\
2654: 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);
2655: /* 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); */
2656: free_vector(min,1,nlstate);
2657: free_vector(max,1,nlstate);
2658: free_vector(meandiff,1,nlstate);
2659:
2660: return bprlim; /* should not reach here */
2661: }
2662:
1.126 brouard 2663: /*************** transition probabilities ***************/
2664:
2665: double **pmij(double **ps, double *cov, int ncovmodel, double *x, int nlstate )
2666: {
1.138 brouard 2667: /* According to parameters values stored in x and the covariate's values stored in cov,
2668: computes the probability to be observed in state j being in state i by appying the
2669: model to the ncovmodel covariates (including constant and age).
2670: lnpijopii=ln(pij/pii)= aij+bij*age+cij*v1+dij*v2+... = sum_nc=1^ncovmodel xij(nc)*cov[nc]
2671: and, according on how parameters are entered, the position of the coefficient xij(nc) of the
2672: ncth covariate in the global vector x is given by the formula:
2673: j<i nc+((i-1)*(nlstate+ndeath-1)+j-1)*ncovmodel
2674: j>=i nc + ((i-1)*(nlstate+ndeath-1)+(j-2))*ncovmodel
2675: Computes ln(pij/pii) (lnpijopii), deduces pij/pii by exponentiation,
2676: sums on j different of i to get 1-pii/pii, deduces pii, and then all pij.
2677: Outputs ps[i][j] the probability to be observed in j being in j according to
2678: the values of the covariates cov[nc] and corresponding parameter values x[nc+shiftij]
2679: */
2680: double s1, lnpijopii;
1.126 brouard 2681: /*double t34;*/
1.164 brouard 2682: int i,j, nc, ii, jj;
1.126 brouard 2683:
1.223 brouard 2684: for(i=1; i<= nlstate; i++){
2685: for(j=1; j<i;j++){
2686: for (nc=1, lnpijopii=0.;nc <=ncovmodel; nc++){
2687: /*lnpijopii += param[i][j][nc]*cov[nc];*/
2688: lnpijopii += x[nc+((i-1)*(nlstate+ndeath-1)+j-1)*ncovmodel]*cov[nc];
2689: /* printf("Int j<i s1=%.17e, lnpijopii=%.17e\n",s1,lnpijopii); */
2690: }
2691: ps[i][j]=lnpijopii; /* In fact ln(pij/pii) */
2692: /* printf("s1=%.17e, lnpijopii=%.17e\n",s1,lnpijopii); */
2693: }
2694: for(j=i+1; j<=nlstate+ndeath;j++){
2695: for (nc=1, lnpijopii=0.;nc <=ncovmodel; nc++){
2696: /*lnpijopii += x[(i-1)*nlstate*ncovmodel+(j-2)*ncovmodel+nc+(i-1)*(ndeath-1)*ncovmodel]*cov[nc];*/
2697: lnpijopii += x[nc + ((i-1)*(nlstate+ndeath-1)+(j-2))*ncovmodel]*cov[nc];
2698: /* printf("Int j>i s1=%.17e, lnpijopii=%.17e %lx %lx\n",s1,lnpijopii,s1,lnpijopii); */
2699: }
2700: ps[i][j]=lnpijopii; /* In fact ln(pij/pii) */
2701: }
2702: }
1.218 brouard 2703:
1.223 brouard 2704: for(i=1; i<= nlstate; i++){
2705: s1=0;
2706: for(j=1; j<i; j++){
2707: s1+=exp(ps[i][j]); /* In fact sums pij/pii */
2708: /*printf("debug1 %d %d ps=%lf exp(ps)=%lf s1+=%lf\n",i,j,ps[i][j],exp(ps[i][j]),s1); */
2709: }
2710: for(j=i+1; j<=nlstate+ndeath; j++){
2711: s1+=exp(ps[i][j]); /* In fact sums pij/pii */
2712: /*printf("debug2 %d %d ps=%lf exp(ps)=%lf s1+=%lf\n",i,j,ps[i][j],exp(ps[i][j]),s1); */
2713: }
2714: /* s1= sum_{j<>i} pij/pii=(1-pii)/pii and thus pii is known from s1 */
2715: ps[i][i]=1./(s1+1.);
2716: /* Computing other pijs */
2717: for(j=1; j<i; j++)
2718: ps[i][j]= exp(ps[i][j])*ps[i][i];
2719: for(j=i+1; j<=nlstate+ndeath; j++)
2720: ps[i][j]= exp(ps[i][j])*ps[i][i];
2721: /* ps[i][nlstate+1]=1.-s1- ps[i][i];*/ /* Sum should be 1 */
2722: } /* end i */
1.218 brouard 2723:
1.223 brouard 2724: for(ii=nlstate+1; ii<= nlstate+ndeath; ii++){
2725: for(jj=1; jj<= nlstate+ndeath; jj++){
2726: ps[ii][jj]=0;
2727: ps[ii][ii]=1;
2728: }
2729: }
1.218 brouard 2730:
2731:
1.223 brouard 2732: /* for(ii=1; ii<= nlstate+ndeath; ii++){ */
2733: /* for(jj=1; jj<= nlstate+ndeath; jj++){ */
2734: /* printf(" pmij ps[%d][%d]=%lf ",ii,jj,ps[ii][jj]); */
2735: /* } */
2736: /* printf("\n "); */
2737: /* } */
2738: /* printf("\n ");printf("%lf ",cov[2]);*/
2739: /*
2740: for(i=1; i<= npar; i++) printf("%f ",x[i]);
1.218 brouard 2741: goto end;*/
1.223 brouard 2742: return ps;
1.126 brouard 2743: }
2744:
1.218 brouard 2745: /*************** backward transition probabilities ***************/
2746:
2747: /* 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 ) */
2748: /* double **bmij(double **ps, double *cov, int ncovmodel, double *x, int nlstate, double ***prevacurrent, double ***dnewm, double **doldm, double **dsavm, int ij ) */
2749: double **bmij(double **ps, double *cov, int ncovmodel, double *x, int nlstate, double ***prevacurrent, int ij )
2750: {
1.222 brouard 2751: /* Computes the backward probability at age agefin and covariate ij
2752: * and returns in **ps as well as **bmij.
2753: */
1.218 brouard 2754: int i, ii, j,k;
1.222 brouard 2755:
2756: double **out, **pmij();
2757: double sumnew=0.;
1.218 brouard 2758: double agefin;
1.222 brouard 2759:
2760: double **dnewm, **dsavm, **doldm;
2761: double **bbmij;
2762:
1.218 brouard 2763: doldm=ddoldms; /* global pointers */
1.222 brouard 2764: dnewm=ddnewms;
2765: dsavm=ddsavms;
2766:
2767: agefin=cov[2];
2768: /* bmij *//* age is cov[2], ij is included in cov, but we need for
2769: the observed prevalence (with this covariate ij) */
2770: dsavm=pmij(pmmij,cov,ncovmodel,x,nlstate);
2771: /* We do have the matrix Px in savm and we need pij */
2772: for (j=1;j<=nlstate+ndeath;j++){
2773: sumnew=0.; /* w1 p11 + w2 p21 only on live states */
2774: for (ii=1;ii<=nlstate;ii++){
2775: sumnew+=dsavm[ii][j]*prevacurrent[(int)agefin][ii][ij];
2776: } /* sumnew is (N11+N21)/N..= N.1/N.. = sum on i of w_i pij */
2777: for (ii=1;ii<=nlstate+ndeath;ii++){
2778: if(sumnew >= 1.e-10){
2779: /* if(agefin >= agemaxpar && agefin <= agemaxpar+stepm/YEARM){ */
2780: /* doldm[ii][j]=(ii==j ? 1./sumnew : 0.0); */
2781: /* }else if(agefin >= agemaxpar+stepm/YEARM){ */
2782: /* doldm[ii][j]=(ii==j ? 1./sumnew : 0.0); */
2783: /* }else */
2784: doldm[ii][j]=(ii==j ? 1./sumnew : 0.0);
2785: }else{
2786: 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);
2787: }
2788: } /*End ii */
2789: } /* End j, At the end doldm is diag[1/(w_1p1i+w_2 p2i)] */
2790: /* left Product of this diag matrix by dsavm=Px (newm=dsavm*doldm) */
2791: bbmij=matprod2(dnewm, dsavm,1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, doldm); /* Bug Valgrind */
2792: /* dsavm=doldm; /\* dsavm is now diag [1/(w_1p1i+w_2 p2i)] but can be overwritten*\/ */
2793: /* doldm=dnewm; /\* doldm is now Px * diag [1/(w_1p1i+w_2 p2i)] *\/ */
2794: /* dnewm=dsavm; /\* doldm is now Px * diag [1/(w_1p1i+w_2 p2i)] *\/ */
2795: /* left Product of this matrix by diag matrix of prevalences (savm) */
2796: for (j=1;j<=nlstate+ndeath;j++){
2797: for (ii=1;ii<=nlstate+ndeath;ii++){
2798: dsavm[ii][j]=(ii==j ? prevacurrent[(int)agefin][ii][ij] : 0.0);
2799: }
2800: } /* End j, At the end oldm is diag[1/(w_1p1i+w_2 p2i)] */
2801: ps=matprod2(doldm, dsavm,1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, dnewm); /* Bug Valgrind */
2802: /* newm or out is now diag[w_i] * Px * diag [1/(w_1p1i+w_2 p2i)] */
2803: /* end bmij */
2804: return ps;
1.218 brouard 2805: }
1.217 brouard 2806: /*************** transition probabilities ***************/
2807:
1.218 brouard 2808: double **bpmij(double **ps, double *cov, int ncovmodel, double *x, int nlstate )
1.217 brouard 2809: {
2810: /* According to parameters values stored in x and the covariate's values stored in cov,
2811: computes the probability to be observed in state j being in state i by appying the
2812: model to the ncovmodel covariates (including constant and age).
2813: lnpijopii=ln(pij/pii)= aij+bij*age+cij*v1+dij*v2+... = sum_nc=1^ncovmodel xij(nc)*cov[nc]
2814: and, according on how parameters are entered, the position of the coefficient xij(nc) of the
2815: ncth covariate in the global vector x is given by the formula:
2816: j<i nc+((i-1)*(nlstate+ndeath-1)+j-1)*ncovmodel
2817: j>=i nc + ((i-1)*(nlstate+ndeath-1)+(j-2))*ncovmodel
2818: Computes ln(pij/pii) (lnpijopii), deduces pij/pii by exponentiation,
2819: sums on j different of i to get 1-pii/pii, deduces pii, and then all pij.
2820: Outputs ps[i][j] the probability to be observed in j being in j according to
2821: the values of the covariates cov[nc] and corresponding parameter values x[nc+shiftij]
2822: */
2823: double s1, lnpijopii;
2824: /*double t34;*/
2825: int i,j, nc, ii, jj;
2826:
1.234 brouard 2827: for(i=1; i<= nlstate; i++){
2828: for(j=1; j<i;j++){
2829: for (nc=1, lnpijopii=0.;nc <=ncovmodel; nc++){
2830: /*lnpijopii += param[i][j][nc]*cov[nc];*/
2831: lnpijopii += x[nc+((i-1)*(nlstate+ndeath-1)+j-1)*ncovmodel]*cov[nc];
2832: /* printf("Int j<i s1=%.17e, lnpijopii=%.17e\n",s1,lnpijopii); */
2833: }
2834: ps[i][j]=lnpijopii; /* In fact ln(pij/pii) */
2835: /* printf("s1=%.17e, lnpijopii=%.17e\n",s1,lnpijopii); */
2836: }
2837: for(j=i+1; j<=nlstate+ndeath;j++){
2838: for (nc=1, lnpijopii=0.;nc <=ncovmodel; nc++){
2839: /*lnpijopii += x[(i-1)*nlstate*ncovmodel+(j-2)*ncovmodel+nc+(i-1)*(ndeath-1)*ncovmodel]*cov[nc];*/
2840: lnpijopii += x[nc + ((i-1)*(nlstate+ndeath-1)+(j-2))*ncovmodel]*cov[nc];
2841: /* printf("Int j>i s1=%.17e, lnpijopii=%.17e %lx %lx\n",s1,lnpijopii,s1,lnpijopii); */
2842: }
2843: ps[i][j]=lnpijopii; /* In fact ln(pij/pii) */
2844: }
2845: }
2846:
2847: for(i=1; i<= nlstate; i++){
2848: s1=0;
2849: for(j=1; j<i; j++){
2850: s1+=exp(ps[i][j]); /* In fact sums pij/pii */
2851: /*printf("debug1 %d %d ps=%lf exp(ps)=%lf s1+=%lf\n",i,j,ps[i][j],exp(ps[i][j]),s1); */
2852: }
2853: for(j=i+1; j<=nlstate+ndeath; j++){
2854: s1+=exp(ps[i][j]); /* In fact sums pij/pii */
2855: /*printf("debug2 %d %d ps=%lf exp(ps)=%lf s1+=%lf\n",i,j,ps[i][j],exp(ps[i][j]),s1); */
2856: }
2857: /* s1= sum_{j<>i} pij/pii=(1-pii)/pii and thus pii is known from s1 */
2858: ps[i][i]=1./(s1+1.);
2859: /* Computing other pijs */
2860: for(j=1; j<i; j++)
2861: ps[i][j]= exp(ps[i][j])*ps[i][i];
2862: for(j=i+1; j<=nlstate+ndeath; j++)
2863: ps[i][j]= exp(ps[i][j])*ps[i][i];
2864: /* ps[i][nlstate+1]=1.-s1- ps[i][i];*/ /* Sum should be 1 */
2865: } /* end i */
2866:
2867: for(ii=nlstate+1; ii<= nlstate+ndeath; ii++){
2868: for(jj=1; jj<= nlstate+ndeath; jj++){
2869: ps[ii][jj]=0;
2870: ps[ii][ii]=1;
2871: }
2872: }
2873: /* Added for backcast */ /* Transposed matrix too */
2874: for(jj=1; jj<= nlstate+ndeath; jj++){
2875: s1=0.;
2876: for(ii=1; ii<= nlstate+ndeath; ii++){
2877: s1+=ps[ii][jj];
2878: }
2879: for(ii=1; ii<= nlstate; ii++){
2880: ps[ii][jj]=ps[ii][jj]/s1;
2881: }
2882: }
2883: /* Transposition */
2884: for(jj=1; jj<= nlstate+ndeath; jj++){
2885: for(ii=jj; ii<= nlstate+ndeath; ii++){
2886: s1=ps[ii][jj];
2887: ps[ii][jj]=ps[jj][ii];
2888: ps[jj][ii]=s1;
2889: }
2890: }
2891: /* for(ii=1; ii<= nlstate+ndeath; ii++){ */
2892: /* for(jj=1; jj<= nlstate+ndeath; jj++){ */
2893: /* printf(" pmij ps[%d][%d]=%lf ",ii,jj,ps[ii][jj]); */
2894: /* } */
2895: /* printf("\n "); */
2896: /* } */
2897: /* printf("\n ");printf("%lf ",cov[2]);*/
2898: /*
2899: for(i=1; i<= npar; i++) printf("%f ",x[i]);
2900: goto end;*/
2901: return ps;
1.217 brouard 2902: }
2903:
2904:
1.126 brouard 2905: /**************** Product of 2 matrices ******************/
2906:
1.145 brouard 2907: double **matprod2(double **out, double **in,int nrl, int nrh, int ncl, int nch, int ncolol, int ncoloh, double **b)
1.126 brouard 2908: {
2909: /* Computes the matrix product of in(1,nrh-nrl+1)(1,nch-ncl+1) times
2910: b(1,nch-ncl+1)(1,ncoloh-ncolol+1) into out(...) */
2911: /* in, b, out are matrice of pointers which should have been initialized
2912: before: only the contents of out is modified. The function returns
2913: a pointer to pointers identical to out */
1.145 brouard 2914: int i, j, k;
1.126 brouard 2915: for(i=nrl; i<= nrh; i++)
1.145 brouard 2916: for(k=ncolol; k<=ncoloh; k++){
2917: out[i][k]=0.;
2918: for(j=ncl; j<=nch; j++)
2919: out[i][k] +=in[i][j]*b[j][k];
2920: }
1.126 brouard 2921: return out;
2922: }
2923:
2924:
2925: /************* Higher Matrix Product ***************/
2926:
1.235 brouard 2927: double ***hpxij(double ***po, int nhstepm, double age, int hstepm, double *x, int nlstate, int stepm, double **oldm, double **savm, int ij, int nres )
1.126 brouard 2928: {
1.218 brouard 2929: /* Computes the transition matrix starting at age 'age' and combination of covariate values corresponding to ij over
1.126 brouard 2930: 'nhstepm*hstepm*stepm' months (i.e. until
2931: age (in years) age+nhstepm*hstepm*stepm/12) by multiplying
2932: nhstepm*hstepm matrices.
2933: Output is stored in matrix po[i][j][h] for h every 'hstepm' step
2934: (typically every 2 years instead of every month which is too big
2935: for the memory).
2936: Model is determined by parameters x and covariates have to be
2937: included manually here.
2938:
2939: */
2940:
2941: int i, j, d, h, k;
1.131 brouard 2942: double **out, cov[NCOVMAX+1];
1.126 brouard 2943: double **newm;
1.187 brouard 2944: double agexact;
1.214 brouard 2945: double agebegin, ageend;
1.126 brouard 2946:
2947: /* Hstepm could be zero and should return the unit matrix */
2948: for (i=1;i<=nlstate+ndeath;i++)
2949: for (j=1;j<=nlstate+ndeath;j++){
2950: oldm[i][j]=(i==j ? 1.0 : 0.0);
2951: po[i][j][0]=(i==j ? 1.0 : 0.0);
2952: }
2953: /* Even if hstepm = 1, at least one multiplication by the unit matrix */
2954: for(h=1; h <=nhstepm; h++){
2955: for(d=1; d <=hstepm; d++){
2956: newm=savm;
2957: /* Covariates have to be included here again */
2958: cov[1]=1.;
1.214 brouard 2959: agexact=age+((h-1)*hstepm + (d-1))*stepm/YEARM; /* age just before transition */
1.187 brouard 2960: cov[2]=agexact;
2961: if(nagesqr==1)
1.227 brouard 2962: cov[3]= agexact*agexact;
1.235 brouard 2963: for (k=1; k<=nsd;k++) { /* For single dummy covariates only */
2964: /* Here comes the value of the covariate 'ij' after renumbering k with single dummy covariates */
2965: cov[2+nagesqr+TvarsDind[k]]=nbcode[TvarsD[k]][codtabm(ij,k)];
2966: /* printf("hpxij Dummy combi=%d k=%d TvarsD[%d]=V%d TvarsDind[%d]=%d nbcode=%d cov=%lf codtabm(%d,Tvar[%d])=%d \n",ij,k, k, TvarsD[k],k,TvarsDind[k],nbcode[TvarsD[k]][codtabm(ij,k)],cov[2+nagesqr+TvarsDind[k]], ij, k, codtabm(ij,k)); */
2967: }
2968: for (k=1; k<=nsq;k++) { /* For single varying covariates only */
2969: /* Here comes the value of quantitative after renumbering k with single quantitative covariates */
2970: cov[2+nagesqr+TvarsQind[k]]=Tqresult[nres][k];
2971: /* printf("hPxij Quantitative k=%d TvarsQind[%d]=%d, TvarsQ[%d]=V%d,Tqresult[%d][%d]=%f\n",k,k,TvarsQind[k],k,TvarsQ[k],nres,k,Tqresult[nres][k]); */
2972: }
2973: for (k=1; k<=cptcovage;k++){
2974: if(Dummy[Tvar[Tage[k]]]){
2975: cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,k)]*cov[2];
2976: } else{
2977: cov[2+nagesqr+Tage[k]]=Tqresult[nres][k];
2978: }
2979: /* printf("hPxij Age combi=%d k=%d Tage[%d]=V%d Tqresult[%d][%d]=%f\n",ij,k,k,Tage[k],nres,k,Tqresult[nres][k]); */
2980: }
2981: for (k=1; k<=cptcovprod;k++){ /* */
2982: /* printf("hPxij Prod ij=%d k=%d Tprod[%d]=%d Tvard[%d][1]=V%d, Tvard[%d][2]=V%d\n",ij,k,k,Tprod[k], k,Tvard[k][1], k,Tvard[k][2]); */
2983: cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)] * nbcode[Tvard[k][2]][codtabm(ij,k)];
2984: }
2985: /* for (k=1; k<=cptcovn;k++) */
2986: /* cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,k)]; */
2987: /* for (k=1; k<=cptcovage;k++) /\* Should start at cptcovn+1 *\/ */
2988: /* cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,k)]*cov[2]; */
2989: /* for (k=1; k<=cptcovprod;k++) /\* Useless because included in cptcovn *\/ */
2990: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)]*nbcode[Tvard[k][2]][codtabm(ij,k)]; */
1.227 brouard 2991:
2992:
1.126 brouard 2993: /*printf("hxi cptcov=%d cptcode=%d\n",cptcov,cptcode);*/
2994: /*printf("h=%d d=%d age=%f cov=%f\n",h,d,age,cov[2]);*/
1.218 brouard 2995: /* right multiplication of oldm by the current matrix */
1.126 brouard 2996: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath,
2997: pmij(pmmij,cov,ncovmodel,x,nlstate));
1.217 brouard 2998: /* if((int)age == 70){ */
2999: /* printf(" Forward hpxij age=%d agexact=%f d=%d nhstepm=%d hstepm=%d\n", (int) age, agexact, d, nhstepm, hstepm); */
3000: /* for(i=1; i<=nlstate+ndeath; i++) { */
3001: /* printf("%d pmmij ",i); */
3002: /* for(j=1;j<=nlstate+ndeath;j++) { */
3003: /* printf("%f ",pmmij[i][j]); */
3004: /* } */
3005: /* printf(" oldm "); */
3006: /* for(j=1;j<=nlstate+ndeath;j++) { */
3007: /* printf("%f ",oldm[i][j]); */
3008: /* } */
3009: /* printf("\n"); */
3010: /* } */
3011: /* } */
1.126 brouard 3012: savm=oldm;
3013: oldm=newm;
3014: }
3015: for(i=1; i<=nlstate+ndeath; i++)
3016: for(j=1;j<=nlstate+ndeath;j++) {
1.218 brouard 3017: po[i][j][h]=newm[i][j];
3018: /*if(h==nhstepm) printf("po[%d][%d][%d]=%f ",i,j,h,po[i][j][h]);*/
1.126 brouard 3019: }
1.128 brouard 3020: /*printf("h=%d ",h);*/
1.126 brouard 3021: } /* end h */
1.218 brouard 3022: /* printf("\n H=%d \n",h); */
1.126 brouard 3023: return po;
3024: }
3025:
1.217 brouard 3026: /************* Higher Back Matrix Product ***************/
1.218 brouard 3027: /* double ***hbxij(double ***po, int nhstepm, double age, int hstepm, double *x, double ***prevacurrent, int nlstate, int stepm, double **oldm, double **savm, double **dnewm, double **doldm, double **dsavm, int ij ) */
1.222 brouard 3028: double ***hbxij(double ***po, int nhstepm, double age, int hstepm, double *x, double ***prevacurrent, int nlstate, int stepm, int ij )
1.217 brouard 3029: {
1.218 brouard 3030: /* Computes the transition matrix starting at age 'age' over
1.217 brouard 3031: 'nhstepm*hstepm*stepm' months (i.e. until
1.218 brouard 3032: age (in years) age+nhstepm*hstepm*stepm/12) by multiplying
3033: nhstepm*hstepm matrices.
3034: Output is stored in matrix po[i][j][h] for h every 'hstepm' step
3035: (typically every 2 years instead of every month which is too big
1.217 brouard 3036: for the memory).
1.218 brouard 3037: Model is determined by parameters x and covariates have to be
3038: included manually here.
1.217 brouard 3039:
1.222 brouard 3040: */
1.217 brouard 3041:
3042: int i, j, d, h, k;
3043: double **out, cov[NCOVMAX+1];
3044: double **newm;
3045: double agexact;
3046: double agebegin, ageend;
1.222 brouard 3047: double **oldm, **savm;
1.217 brouard 3048:
1.222 brouard 3049: oldm=oldms;savm=savms;
1.217 brouard 3050: /* Hstepm could be zero and should return the unit matrix */
3051: for (i=1;i<=nlstate+ndeath;i++)
3052: for (j=1;j<=nlstate+ndeath;j++){
3053: oldm[i][j]=(i==j ? 1.0 : 0.0);
3054: po[i][j][0]=(i==j ? 1.0 : 0.0);
3055: }
3056: /* Even if hstepm = 1, at least one multiplication by the unit matrix */
3057: for(h=1; h <=nhstepm; h++){
3058: for(d=1; d <=hstepm; d++){
3059: newm=savm;
3060: /* Covariates have to be included here again */
3061: cov[1]=1.;
3062: agexact=age-((h-1)*hstepm + (d-1))*stepm/YEARM; /* age just before transition */
3063: /* agexact=age+((h-1)*hstepm + (d-1))*stepm/YEARM; /\* age just before transition *\/ */
3064: cov[2]=agexact;
3065: if(nagesqr==1)
1.222 brouard 3066: cov[3]= agexact*agexact;
1.218 brouard 3067: for (k=1; k<=cptcovn;k++)
1.222 brouard 3068: cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,k)];
3069: /* cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,Tvar[k])]; */
1.217 brouard 3070: for (k=1; k<=cptcovage;k++) /* Should start at cptcovn+1 */
1.222 brouard 3071: /* cov[2+Tage[k]]=cov[2+Tage[k]]*cov[2]; */
3072: cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,k)]*cov[2];
3073: /* cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,Tvar[Tage[k]])]*cov[2]; */
1.217 brouard 3074: for (k=1; k<=cptcovprod;k++) /* Useless because included in cptcovn */
1.222 brouard 3075: cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)]*nbcode[Tvard[k][2]][codtabm(ij,k)];
3076: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,Tvard[k][1])]*nbcode[Tvard[k][2]][codtabm(ij,Tvard[k][2])]; */
1.218 brouard 3077:
3078:
1.217 brouard 3079: /*printf("hxi cptcov=%d cptcode=%d\n",cptcov,cptcode);*/
3080: /*printf("h=%d d=%d age=%f cov=%f\n",h,d,age,cov[2]);*/
1.218 brouard 3081: /* Careful transposed matrix */
1.222 brouard 3082: /* age is in cov[2] */
1.218 brouard 3083: /* out=matprod2(newm, bmij(pmmij,cov,ncovmodel,x,nlstate,prevacurrent, dnewm, doldm, dsavm,ij),\ */
1.222 brouard 3084: /* 1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm); */
1.218 brouard 3085: out=matprod2(newm, bmij(pmmij,cov,ncovmodel,x,nlstate,prevacurrent,ij),\
1.222 brouard 3086: 1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm);
1.217 brouard 3087: /* if((int)age == 70){ */
3088: /* printf(" Backward hbxij age=%d agexact=%f d=%d nhstepm=%d hstepm=%d\n", (int) age, agexact, d, nhstepm, hstepm); */
3089: /* for(i=1; i<=nlstate+ndeath; i++) { */
3090: /* printf("%d pmmij ",i); */
3091: /* for(j=1;j<=nlstate+ndeath;j++) { */
3092: /* printf("%f ",pmmij[i][j]); */
3093: /* } */
3094: /* printf(" oldm "); */
3095: /* for(j=1;j<=nlstate+ndeath;j++) { */
3096: /* printf("%f ",oldm[i][j]); */
3097: /* } */
3098: /* printf("\n"); */
3099: /* } */
3100: /* } */
3101: savm=oldm;
3102: oldm=newm;
3103: }
3104: for(i=1; i<=nlstate+ndeath; i++)
3105: for(j=1;j<=nlstate+ndeath;j++) {
1.222 brouard 3106: po[i][j][h]=newm[i][j];
3107: /*if(h==nhstepm) printf("po[%d][%d][%d]=%f ",i,j,h,po[i][j][h]);*/
1.217 brouard 3108: }
3109: /*printf("h=%d ",h);*/
3110: } /* end h */
1.222 brouard 3111: /* printf("\n H=%d \n",h); */
1.217 brouard 3112: return po;
3113: }
3114:
3115:
1.162 brouard 3116: #ifdef NLOPT
3117: double myfunc(unsigned n, const double *p1, double *grad, void *pd){
3118: double fret;
3119: double *xt;
3120: int j;
3121: myfunc_data *d2 = (myfunc_data *) pd;
3122: /* xt = (p1-1); */
3123: xt=vector(1,n);
3124: for (j=1;j<=n;j++) xt[j]=p1[j-1]; /* xt[1]=p1[0] */
3125:
3126: fret=(d2->function)(xt); /* p xt[1]@8 is fine */
3127: /* fret=(*func)(xt); /\* p xt[1]@8 is fine *\/ */
3128: printf("Function = %.12lf ",fret);
3129: for (j=1;j<=n;j++) printf(" %d %.8lf", j, xt[j]);
3130: printf("\n");
3131: free_vector(xt,1,n);
3132: return fret;
3133: }
3134: #endif
1.126 brouard 3135:
3136: /*************** log-likelihood *************/
3137: double func( double *x)
3138: {
1.226 brouard 3139: int i, ii, j, k, mi, d, kk;
3140: int ioffset=0;
3141: double l, ll[NLSTATEMAX+1], cov[NCOVMAX+1];
3142: double **out;
3143: double lli; /* Individual log likelihood */
3144: int s1, s2;
1.228 brouard 3145: int iv=0, iqv=0, itv=0, iqtv=0 ; /* Index of varying covariate, fixed quantitative cov, time varying covariate, quantitative time varying covariate */
1.226 brouard 3146: double bbh, survp;
3147: long ipmx;
3148: double agexact;
3149: /*extern weight */
3150: /* We are differentiating ll according to initial status */
3151: /* for (i=1;i<=npar;i++) printf("%f ", x[i]);*/
3152: /*for(i=1;i<imx;i++)
3153: printf(" %d\n",s[4][i]);
3154: */
1.162 brouard 3155:
1.226 brouard 3156: ++countcallfunc;
1.162 brouard 3157:
1.226 brouard 3158: cov[1]=1.;
1.126 brouard 3159:
1.226 brouard 3160: for(k=1; k<=nlstate; k++) ll[k]=0.;
1.224 brouard 3161: ioffset=0;
1.226 brouard 3162: if(mle==1){
3163: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
3164: /* Computes the values of the ncovmodel covariates of the model
3165: depending if the covariates are fixed or varying (age dependent) and stores them in cov[]
3166: Then computes with function pmij which return a matrix p[i][j] giving the elementary probability
3167: to be observed in j being in i according to the model.
3168: */
3169: ioffset=2+nagesqr+cptcovage;
1.233 brouard 3170: /* Fixed */
1.234 brouard 3171: for (k=1; k<=ncovf;k++){ /* Simple and product fixed covariates without age* products */
3172: cov[ioffset+TvarFind[k]]=covar[Tvar[TvarFind[k]]][i];/* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1, only V1 is fixed (k=6)*/
3173: }
1.226 brouard 3174: /* In model V2+V1*V4+age*V3+V3*V2 Tvar[1] is V2, Tvar[2=V1*V4]
3175: is 6, Tvar[3=age*V3] should not be computed because of age Tvar[4=V3*V2]
3176: has been calculated etc */
3177: /* For an individual i, wav[i] gives the number of effective waves */
3178: /* We compute the contribution to Likelihood of each effective transition
3179: mw[mi][i] is real wave of the mi th effectve wave */
3180: /* Then statuses are computed at each begin and end of an effective wave s1=s[ mw[mi][i] ][i];
3181: s2=s[mw[mi+1][i]][i];
3182: And the iv th varying covariate is the cotvar[mw[mi+1][i]][iv][i]
3183: But if the variable is not in the model TTvar[iv] is the real variable effective in the model:
3184: meaning that decodemodel should be used cotvar[mw[mi+1][i]][TTvar[iv]][i]
3185: */
3186: for(mi=1; mi<= wav[i]-1; mi++){
1.234 brouard 3187: for(k=1; k <= ncovv ; k++){ /* Varying covariates (single and product but no age )*/
3188: cov[ioffset+TvarVind[k]]=cotvar[mw[mi][i]][Tvar[TvarVind[k]]][i];
3189: }
3190: for (ii=1;ii<=nlstate+ndeath;ii++)
3191: for (j=1;j<=nlstate+ndeath;j++){
3192: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
3193: savm[ii][j]=(ii==j ? 1.0 : 0.0);
3194: }
3195: for(d=0; d<dh[mi][i]; d++){
3196: newm=savm;
3197: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM;
3198: cov[2]=agexact;
3199: if(nagesqr==1)
3200: cov[3]= agexact*agexact; /* Should be changed here */
3201: for (kk=1; kk<=cptcovage;kk++) {
3202: cov[Tage[kk]+2+nagesqr]=covar[Tvar[Tage[kk]]][i]*agexact; /* Tage[kk] gives the data-covariate associated with age */
3203: }
3204: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
3205: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
3206: savm=oldm;
3207: oldm=newm;
3208: } /* end mult */
3209:
3210: /*lli=log(out[s[mw[mi][i]][i]][s[mw[mi+1][i]][i]]);*/ /* Original formula */
3211: /* But now since version 0.9 we anticipate for bias at large stepm.
3212: * If stepm is larger than one month (smallest stepm) and if the exact delay
3213: * (in months) between two waves is not a multiple of stepm, we rounded to
3214: * the nearest (and in case of equal distance, to the lowest) interval but now
3215: * we keep into memory the bias bh[mi][i] and also the previous matrix product
3216: * (i.e to dh[mi][i]-1) saved in 'savm'. Then we inter(extra)polate the
3217: * probability in order to take into account the bias as a fraction of the way
1.231 brouard 3218: * from savm to out if bh is negative or even beyond if bh is positive. bh varies
3219: * -stepm/2 to stepm/2 .
3220: * For stepm=1 the results are the same as for previous versions of Imach.
3221: * For stepm > 1 the results are less biased than in previous versions.
3222: */
1.234 brouard 3223: s1=s[mw[mi][i]][i];
3224: s2=s[mw[mi+1][i]][i];
3225: bbh=(double)bh[mi][i]/(double)stepm;
3226: /* bias bh is positive if real duration
3227: * is higher than the multiple of stepm and negative otherwise.
3228: */
3229: /* lli= (savm[s1][s2]>1.e-8 ?(1.+bbh)*log(out[s1][s2])- bbh*log(savm[s1][s2]):log((1.+bbh)*out[s1][s2]));*/
3230: if( s2 > nlstate){
3231: /* i.e. if s2 is a death state and if the date of death is known
3232: then the contribution to the likelihood is the probability to
3233: die between last step unit time and current step unit time,
3234: which is also equal to probability to die before dh
3235: minus probability to die before dh-stepm .
3236: In version up to 0.92 likelihood was computed
3237: as if date of death was unknown. Death was treated as any other
3238: health state: the date of the interview describes the actual state
3239: and not the date of a change in health state. The former idea was
3240: to consider that at each interview the state was recorded
3241: (healthy, disable or death) and IMaCh was corrected; but when we
3242: introduced the exact date of death then we should have modified
3243: the contribution of an exact death to the likelihood. This new
3244: contribution is smaller and very dependent of the step unit
3245: stepm. It is no more the probability to die between last interview
3246: and month of death but the probability to survive from last
3247: interview up to one month before death multiplied by the
3248: probability to die within a month. Thanks to Chris
3249: Jackson for correcting this bug. Former versions increased
3250: mortality artificially. The bad side is that we add another loop
3251: which slows down the processing. The difference can be up to 10%
3252: lower mortality.
3253: */
3254: /* If, at the beginning of the maximization mostly, the
3255: cumulative probability or probability to be dead is
3256: constant (ie = 1) over time d, the difference is equal to
3257: 0. out[s1][3] = savm[s1][3]: probability, being at state
3258: s1 at precedent wave, to be dead a month before current
3259: wave is equal to probability, being at state s1 at
3260: precedent wave, to be dead at mont of the current
3261: wave. Then the observed probability (that this person died)
3262: is null according to current estimated parameter. In fact,
3263: it should be very low but not zero otherwise the log go to
3264: infinity.
3265: */
1.183 brouard 3266: /* #ifdef INFINITYORIGINAL */
3267: /* lli=log(out[s1][s2] - savm[s1][s2]); */
3268: /* #else */
3269: /* if ((out[s1][s2] - savm[s1][s2]) < mytinydouble) */
3270: /* lli=log(mytinydouble); */
3271: /* else */
3272: /* lli=log(out[s1][s2] - savm[s1][s2]); */
3273: /* #endif */
1.226 brouard 3274: lli=log(out[s1][s2] - savm[s1][s2]);
1.216 brouard 3275:
1.226 brouard 3276: } else if ( s2==-1 ) { /* alive */
3277: for (j=1,survp=0. ; j<=nlstate; j++)
3278: survp += (1.+bbh)*out[s1][j]- bbh*savm[s1][j];
3279: /*survp += out[s1][j]; */
3280: lli= log(survp);
3281: }
3282: else if (s2==-4) {
3283: for (j=3,survp=0. ; j<=nlstate; j++)
3284: survp += (1.+bbh)*out[s1][j]- bbh*savm[s1][j];
3285: lli= log(survp);
3286: }
3287: else if (s2==-5) {
3288: for (j=1,survp=0. ; j<=2; j++)
3289: survp += (1.+bbh)*out[s1][j]- bbh*savm[s1][j];
3290: lli= log(survp);
3291: }
3292: else{
3293: lli= log((1.+bbh)*out[s1][s2]- bbh*savm[s1][s2]); /* linear interpolation */
3294: /* 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 */
3295: }
3296: /*lli=(1.+bbh)*log(out[s1][s2])- bbh*log(savm[s1][s2]);*/
3297: /*if(lli ==000.0)*/
3298: /*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); */
3299: ipmx +=1;
3300: sw += weight[i];
3301: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
3302: /* if (lli < log(mytinydouble)){ */
3303: /* 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); */
3304: /* 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]); */
3305: /* } */
3306: } /* end of wave */
3307: } /* end of individual */
3308: } else if(mle==2){
3309: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
3310: for (k=1; k<=cptcovn;k++) cov[2+nagesqr+k]=covar[Tvar[k]][i];
3311: for(mi=1; mi<= wav[i]-1; mi++){
3312: for (ii=1;ii<=nlstate+ndeath;ii++)
3313: for (j=1;j<=nlstate+ndeath;j++){
3314: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
3315: savm[ii][j]=(ii==j ? 1.0 : 0.0);
3316: }
3317: for(d=0; d<=dh[mi][i]; d++){
3318: newm=savm;
3319: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM;
3320: cov[2]=agexact;
3321: if(nagesqr==1)
3322: cov[3]= agexact*agexact;
3323: for (kk=1; kk<=cptcovage;kk++) {
3324: cov[Tage[kk]+2+nagesqr]=covar[Tvar[Tage[kk]]][i]*agexact;
3325: }
3326: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
3327: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
3328: savm=oldm;
3329: oldm=newm;
3330: } /* end mult */
3331:
3332: s1=s[mw[mi][i]][i];
3333: s2=s[mw[mi+1][i]][i];
3334: bbh=(double)bh[mi][i]/(double)stepm;
3335: 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 */
3336: ipmx +=1;
3337: sw += weight[i];
3338: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
3339: } /* end of wave */
3340: } /* end of individual */
3341: } else if(mle==3){ /* exponential inter-extrapolation */
3342: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
3343: for (k=1; k<=cptcovn;k++) cov[2+nagesqr+k]=covar[Tvar[k]][i];
3344: for(mi=1; mi<= wav[i]-1; mi++){
3345: for (ii=1;ii<=nlstate+ndeath;ii++)
3346: for (j=1;j<=nlstate+ndeath;j++){
3347: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
3348: savm[ii][j]=(ii==j ? 1.0 : 0.0);
3349: }
3350: for(d=0; d<dh[mi][i]; d++){
3351: newm=savm;
3352: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM;
3353: cov[2]=agexact;
3354: if(nagesqr==1)
3355: cov[3]= agexact*agexact;
3356: for (kk=1; kk<=cptcovage;kk++) {
3357: cov[Tage[kk]+2+nagesqr]=covar[Tvar[Tage[kk]]][i]*agexact;
3358: }
3359: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
3360: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
3361: savm=oldm;
3362: oldm=newm;
3363: } /* end mult */
3364:
3365: s1=s[mw[mi][i]][i];
3366: s2=s[mw[mi+1][i]][i];
3367: bbh=(double)bh[mi][i]/(double)stepm;
3368: 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 */
3369: ipmx +=1;
3370: sw += weight[i];
3371: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
3372: } /* end of wave */
3373: } /* end of individual */
3374: }else if (mle==4){ /* ml=4 no inter-extrapolation */
3375: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
3376: for (k=1; k<=cptcovn;k++) cov[2+nagesqr+k]=covar[Tvar[k]][i];
3377: for(mi=1; mi<= wav[i]-1; mi++){
3378: for (ii=1;ii<=nlstate+ndeath;ii++)
3379: for (j=1;j<=nlstate+ndeath;j++){
3380: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
3381: savm[ii][j]=(ii==j ? 1.0 : 0.0);
3382: }
3383: for(d=0; d<dh[mi][i]; d++){
3384: newm=savm;
3385: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM;
3386: cov[2]=agexact;
3387: if(nagesqr==1)
3388: cov[3]= agexact*agexact;
3389: for (kk=1; kk<=cptcovage;kk++) {
3390: cov[Tage[kk]+2+nagesqr]=covar[Tvar[Tage[kk]]][i]*agexact;
3391: }
1.126 brouard 3392:
1.226 brouard 3393: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
3394: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
3395: savm=oldm;
3396: oldm=newm;
3397: } /* end mult */
3398:
3399: s1=s[mw[mi][i]][i];
3400: s2=s[mw[mi+1][i]][i];
3401: if( s2 > nlstate){
3402: lli=log(out[s1][s2] - savm[s1][s2]);
3403: } else if ( s2==-1 ) { /* alive */
3404: for (j=1,survp=0. ; j<=nlstate; j++)
3405: survp += out[s1][j];
3406: lli= log(survp);
3407: }else{
3408: lli=log(out[s[mw[mi][i]][i]][s[mw[mi+1][i]][i]]); /* Original formula */
3409: }
3410: ipmx +=1;
3411: sw += weight[i];
3412: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
1.126 brouard 3413: /* printf("i=%6d s1=%1d s2=%1d mi=%1d mw=%1d dh=%3d prob=%10.6f w=%6.4f out=%10.6f sav=%10.6f\n",i,s1,s2,mi,mw[mi][i],dh[mi][i],exp(lli),weight[i],out[s1][s2],savm[s1][s2]); */
1.226 brouard 3414: } /* end of wave */
3415: } /* end of individual */
3416: }else{ /* ml=5 no inter-extrapolation no jackson =0.8a */
3417: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
3418: for (k=1; k<=cptcovn;k++) cov[2+nagesqr+k]=covar[Tvar[k]][i];
3419: for(mi=1; mi<= wav[i]-1; mi++){
3420: for (ii=1;ii<=nlstate+ndeath;ii++)
3421: for (j=1;j<=nlstate+ndeath;j++){
3422: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
3423: savm[ii][j]=(ii==j ? 1.0 : 0.0);
3424: }
3425: for(d=0; d<dh[mi][i]; d++){
3426: newm=savm;
3427: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM;
3428: cov[2]=agexact;
3429: if(nagesqr==1)
3430: cov[3]= agexact*agexact;
3431: for (kk=1; kk<=cptcovage;kk++) {
3432: cov[Tage[kk]+2+nagesqr]=covar[Tvar[Tage[kk]]][i]*agexact;
3433: }
1.126 brouard 3434:
1.226 brouard 3435: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
3436: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
3437: savm=oldm;
3438: oldm=newm;
3439: } /* end mult */
3440:
3441: s1=s[mw[mi][i]][i];
3442: s2=s[mw[mi+1][i]][i];
3443: lli=log(out[s[mw[mi][i]][i]][s[mw[mi+1][i]][i]]); /* Original formula */
3444: ipmx +=1;
3445: sw += weight[i];
3446: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
3447: /*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]);*/
3448: } /* end of wave */
3449: } /* end of individual */
3450: } /* End of if */
3451: for(k=1,l=0.; k<=nlstate; k++) l += ll[k];
3452: /* printf("l1=%f l2=%f ",ll[1],ll[2]); */
3453: l= l*ipmx/sw; /* To get the same order of magnitude as if weight=1 for every body */
3454: return -l;
1.126 brouard 3455: }
3456:
3457: /*************** log-likelihood *************/
3458: double funcone( double *x)
3459: {
1.228 brouard 3460: /* Same as func but slower because of a lot of printf and if */
1.126 brouard 3461: int i, ii, j, k, mi, d, kk;
1.228 brouard 3462: int ioffset=0;
1.131 brouard 3463: double l, ll[NLSTATEMAX+1], cov[NCOVMAX+1];
1.126 brouard 3464: double **out;
3465: double lli; /* Individual log likelihood */
3466: double llt;
3467: int s1, s2;
1.228 brouard 3468: int iv=0, iqv=0, itv=0, iqtv=0 ; /* Index of varying covariate, fixed quantitative cov, time varying covariate, quantitative time varying covariate */
3469:
1.126 brouard 3470: double bbh, survp;
1.187 brouard 3471: double agexact;
1.214 brouard 3472: double agebegin, ageend;
1.126 brouard 3473: /*extern weight */
3474: /* We are differentiating ll according to initial status */
3475: /* for (i=1;i<=npar;i++) printf("%f ", x[i]);*/
3476: /*for(i=1;i<imx;i++)
3477: printf(" %d\n",s[4][i]);
3478: */
3479: cov[1]=1.;
3480:
3481: for(k=1; k<=nlstate; k++) ll[k]=0.;
1.224 brouard 3482: ioffset=0;
3483: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
1.225 brouard 3484: ioffset=2+nagesqr+cptcovage;
1.232 brouard 3485: /* Fixed */
1.224 brouard 3486: /* for (k=1; k<=cptcovn;k++) cov[2+nagesqr+k]=covar[Tvar[k]][i]; */
1.232 brouard 3487: /* for (k=1; k<=ncoveff;k++){ /\* Simple and product fixed Dummy covariates without age* products *\/ */
3488: for (k=1; k<=ncovf;k++){ /* Simple and product fixed covariates without age* products */
3489: cov[ioffset+TvarFind[k]]=covar[Tvar[TvarFind[k]]][i];/* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1, only V1 is fixed (k=6)*/
3490: /* cov[ioffset+TvarFind[1]]=covar[Tvar[TvarFind[1]]][i]; */
3491: /* cov[2+6]=covar[Tvar[6]][i]; */
3492: /* cov[2+6]=covar[2][i]; V2 */
3493: /* cov[TvarFind[2]]=covar[Tvar[TvarFind[2]]][i]; */
3494: /* cov[2+7]=covar[Tvar[7]][i]; */
3495: /* cov[2+7]=covar[7][i]; V7=V1*V2 */
3496: /* cov[TvarFind[3]]=covar[Tvar[TvarFind[3]]][i]; */
3497: /* cov[2+9]=covar[Tvar[9]][i]; */
3498: /* cov[2+9]=covar[1][i]; V1 */
1.225 brouard 3499: }
1.232 brouard 3500: /* for (k=1; k<=nqfveff;k++){ /\* Simple and product fixed Quantitative covariates without age* products *\/ */
3501: /* cov[++ioffset]=coqvar[TvarFQ[k]][i];/\* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1, only V2 and V1*V2 is fixed (k=6 and 7?)*\/ */
3502: /* } */
1.231 brouard 3503: /* for(iqv=1; iqv <= nqfveff; iqv++){ /\* Quantitative fixed covariates *\/ */
3504: /* cov[++ioffset]=coqvar[Tvar[iqv]][i]; /\* Only V2 k=6 and V1*V2 7 *\/ */
3505: /* } */
1.225 brouard 3506:
1.233 brouard 3507:
3508: for(mi=1; mi<= wav[i]-1; mi++){ /* Varying with waves */
1.232 brouard 3509: /* Wave varying (but not age varying) */
3510: for(k=1; k <= ncovv ; k++){ /* Varying covariates (single and product but no age )*/
1.233 brouard 3511: cov[ioffset+TvarVind[k]]=cotvar[mw[mi][i]][Tvar[TvarVind[k]]][i];
1.232 brouard 3512: }
3513: /* for(itv=1; itv <= ntveff; itv++){ /\* Varying dummy covariates (single??)*\/ */
1.231 brouard 3514: /* iv= Tvar[Tmodelind[ioffset-2-nagesqr-cptcovage+itv]]-ncovcol-nqv; /\* Counting the # varying covariate from 1 to ntveff *\/ */
3515: /* cov[ioffset+iv]=cotvar[mw[mi][i]][iv][i]; */
1.232 brouard 3516: /* k=ioffset-2-nagesqr-cptcovage+itv; /\* position in simple model *\/ */
3517: /* cov[ioffset+itv]=cotvar[mw[mi][i]][TmodelInvind[itv]][i]; */
1.231 brouard 3518: /* printf(" i=%d,mi=%d,itv=%d,TmodelInvind[itv]=%d,cotvar[mw[mi][i]][TmodelInvind[itv]][i]=%f\n", i, mi, itv, TmodelInvind[itv],cotvar[mw[mi][i]][TmodelInvind[itv]][i]); */
1.232 brouard 3519: /* for(iqtv=1; iqtv <= nqtveff; iqtv++){ /\* Varying quantitatives covariates *\/ */
3520: /* iv=TmodelInvQind[iqtv]; /\* Counting the # varying covariate from 1 to ntveff *\/ */
3521: /* /\* printf(" i=%d,mi=%d,iqtv=%d,TmodelInvQind[iqtv]=%d,cotqvar[mw[mi][i]][TmodelInvQind[iqtv]][i]=%f\n", i, mi, iqtv, TmodelInvQind[iqtv],cotqvar[mw[mi][i]][TmodelInvQind[iqtv]][i]); *\/ */
3522: /* cov[ioffset+ntveff+iqtv]=cotqvar[mw[mi][i]][TmodelInvQind[iqtv]][i]; */
3523: /* } */
1.126 brouard 3524: for (ii=1;ii<=nlstate+ndeath;ii++)
1.231 brouard 3525: for (j=1;j<=nlstate+ndeath;j++){
3526: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
3527: savm[ii][j]=(ii==j ? 1.0 : 0.0);
3528: }
1.214 brouard 3529:
3530: agebegin=agev[mw[mi][i]][i]; /* Age at beginning of effective wave */
3531: ageend=agev[mw[mi][i]][i] + (dh[mi][i])*stepm/YEARM; /* Age at end of effective wave and at the end of transition */
3532: for(d=0; d<dh[mi][i]; d++){ /* Delay between two effective waves */
1.231 brouard 3533: /*dh[m][i] or dh[mw[mi][i]][i] is the delay between two effective waves m=mw[mi][i]
3534: and mw[mi+1][i]. dh depends on stepm.*/
3535: newm=savm;
3536: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM;
3537: cov[2]=agexact;
3538: if(nagesqr==1)
3539: cov[3]= agexact*agexact;
3540: for (kk=1; kk<=cptcovage;kk++) {
3541: cov[Tage[kk]+2+nagesqr]=covar[Tvar[Tage[kk]]][i]*agexact;
3542: }
3543: /* printf("i=%d,mi=%d,d=%d,mw[mi][i]=%d\n",i, mi,d,mw[mi][i]); */
3544: /* savm=pmij(pmmij,cov,ncovmodel,x,nlstate); */
3545: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
3546: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
3547: /* out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath, */
3548: /* 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate)); */
3549: savm=oldm;
3550: oldm=newm;
1.126 brouard 3551: } /* end mult */
3552:
3553: s1=s[mw[mi][i]][i];
3554: s2=s[mw[mi+1][i]][i];
1.217 brouard 3555: /* if(s2==-1){ */
3556: /* printf(" s1=%d, s2=%d i=%d \n", s1, s2, i); */
3557: /* /\* exit(1); *\/ */
3558: /* } */
1.126 brouard 3559: bbh=(double)bh[mi][i]/(double)stepm;
3560: /* bias is positive if real duration
3561: * is higher than the multiple of stepm and negative otherwise.
3562: */
3563: if( s2 > nlstate && (mle <5) ){ /* Jackson */
1.232 brouard 3564: lli=log(out[s1][s2] - savm[s1][s2]);
1.216 brouard 3565: } else if ( s2==-1 ) { /* alive */
1.232 brouard 3566: for (j=1,survp=0. ; j<=nlstate; j++)
3567: survp += (1.+bbh)*out[s1][j]- bbh*savm[s1][j];
3568: lli= log(survp);
1.126 brouard 3569: }else if (mle==1){
1.232 brouard 3570: lli= log((1.+bbh)*out[s1][s2]- bbh*savm[s1][s2]); /* linear interpolation */
1.126 brouard 3571: } else if(mle==2){
1.232 brouard 3572: lli= (savm[s1][s2]>(double)1.e-8 ?log((1.+bbh)*out[s1][s2]- bbh*savm[s1][s2]):log((1.+bbh)*out[s1][s2])); /* linear interpolation */
1.126 brouard 3573: } else if(mle==3){ /* exponential inter-extrapolation */
1.232 brouard 3574: lli= (savm[s1][s2]>(double)1.e-8 ?(1.+bbh)*log(out[s1][s2])- bbh*log(savm[s1][s2]):log((1.+bbh)*out[s1][s2])); /* exponential inter-extrapolation */
1.126 brouard 3575: } else if (mle==4){ /* mle=4 no inter-extrapolation */
1.232 brouard 3576: lli=log(out[s1][s2]); /* Original formula */
1.136 brouard 3577: } else{ /* mle=0 back to 1 */
1.232 brouard 3578: lli= log((1.+bbh)*out[s1][s2]- bbh*savm[s1][s2]); /* linear interpolation */
3579: /*lli=log(out[s1][s2]); */ /* Original formula */
1.126 brouard 3580: } /* End of if */
3581: ipmx +=1;
3582: sw += weight[i];
3583: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
1.132 brouard 3584: /*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 3585: if(globpr){
1.232 brouard 3586: fprintf(ficresilk,"%9ld %6.1f %6.1f %6d %2d %2d %2d %2d %3d %15.6f %8.4f %8.3f\
1.126 brouard 3587: %11.6f %11.6f %11.6f ", \
1.232 brouard 3588: num[i], agebegin, ageend, i,s1,s2,mi,mw[mi][i],dh[mi][i],exp(lli),weight[i],weight[i]*gipmx/gsw,
3589: 2*weight[i]*lli,out[s1][s2],savm[s1][s2]);
3590: for(k=1,llt=0.,l=0.; k<=nlstate; k++){
3591: llt +=ll[k]*gipmx/gsw;
3592: fprintf(ficresilk," %10.6f",-ll[k]*gipmx/gsw);
3593: }
3594: fprintf(ficresilk," %10.6f\n", -llt);
1.126 brouard 3595: }
1.232 brouard 3596: } /* end of wave */
3597: } /* end of individual */
3598: for(k=1,l=0.; k<=nlstate; k++) l += ll[k];
3599: /* printf("l1=%f l2=%f ",ll[1],ll[2]); */
3600: l= l*ipmx/sw; /* To get the same order of magnitude as if weight=1 for every body */
3601: if(globpr==0){ /* First time we count the contributions and weights */
3602: gipmx=ipmx;
3603: gsw=sw;
3604: }
3605: return -l;
1.126 brouard 3606: }
3607:
3608:
3609: /*************** function likelione ***********/
3610: void likelione(FILE *ficres,double p[], int npar, int nlstate, int *globpri, long *ipmx, double *sw, double *fretone, double (*funcone)(double []))
3611: {
3612: /* This routine should help understanding what is done with
3613: the selection of individuals/waves and
3614: to check the exact contribution to the likelihood.
3615: Plotting could be done.
3616: */
3617: int k;
3618:
3619: if(*globpri !=0){ /* Just counts and sums, no printings */
1.201 brouard 3620: strcpy(fileresilk,"ILK_");
1.202 brouard 3621: strcat(fileresilk,fileresu);
1.126 brouard 3622: if((ficresilk=fopen(fileresilk,"w"))==NULL) {
3623: printf("Problem with resultfile: %s\n", fileresilk);
3624: fprintf(ficlog,"Problem with resultfile: %s\n", fileresilk);
3625: }
1.214 brouard 3626: 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");
3627: fprintf(ficresilk, "#num_i ageb agend i s1 s2 mi mw dh likeli weight %%weight 2wlli out sav ");
1.126 brouard 3628: /* i,s1,s2,mi,mw[mi][i],dh[mi][i],exp(lli),weight[i],2*weight[i]*lli,out[s1][s2],savm[s1][s2]); */
3629: for(k=1; k<=nlstate; k++)
3630: fprintf(ficresilk," -2*gipw/gsw*weight*ll[%d]++",k);
3631: fprintf(ficresilk," -2*gipw/gsw*weight*ll(total)\n");
3632: }
3633:
3634: *fretone=(*funcone)(p);
3635: if(*globpri !=0){
3636: fclose(ficresilk);
1.205 brouard 3637: if (mle ==0)
3638: fprintf(fichtm,"\n<br>File of contributions to the likelihood computed with initial parameters and mle = %d.",mle);
3639: else if(mle >=1)
3640: fprintf(fichtm,"\n<br>File of contributions to the likelihood computed with optimized parameters mle = %d.",mle);
3641: 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 3642:
1.208 brouard 3643:
3644: for (k=1; k<= nlstate ; k++) {
1.211 brouard 3645: 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 3646: <img src=\"%s-p%dj.png\">",k,k,subdirf2(optionfilefiname,"ILK_"),k,subdirf2(optionfilefiname,"ILK_"),k,subdirf2(optionfilefiname,"ILK_"),k);
3647: }
1.207 brouard 3648: 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 3649: <img src=\"%s-ori.png\">",subdirf2(optionfilefiname,"ILK_"),subdirf2(optionfilefiname,"ILK_"),subdirf2(optionfilefiname,"ILK_"));
1.207 brouard 3650: fprintf(fichtm,"<br>- and by state of destination <a href=\"%s-dest.png\">%s-dest.png</a><br> \
1.204 brouard 3651: <img src=\"%s-dest.png\">",subdirf2(optionfilefiname,"ILK_"),subdirf2(optionfilefiname,"ILK_"),subdirf2(optionfilefiname,"ILK_"));
1.207 brouard 3652: fflush(fichtm);
1.205 brouard 3653: }
1.126 brouard 3654: return;
3655: }
3656:
3657:
3658: /*********** Maximum Likelihood Estimation ***************/
3659:
3660: void mlikeli(FILE *ficres,double p[], int npar, int ncovmodel, int nlstate, double ftol, double (*func)(double []))
3661: {
1.165 brouard 3662: int i,j, iter=0;
1.126 brouard 3663: double **xi;
3664: double fret;
3665: double fretone; /* Only one call to likelihood */
3666: /* char filerespow[FILENAMELENGTH];*/
1.162 brouard 3667:
3668: #ifdef NLOPT
3669: int creturn;
3670: nlopt_opt opt;
3671: /* double lb[9] = { -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL }; /\* lower bounds *\/ */
3672: double *lb;
3673: double minf; /* the minimum objective value, upon return */
3674: double * p1; /* Shifted parameters from 0 instead of 1 */
3675: myfunc_data dinst, *d = &dinst;
3676: #endif
3677:
3678:
1.126 brouard 3679: xi=matrix(1,npar,1,npar);
3680: for (i=1;i<=npar;i++)
3681: for (j=1;j<=npar;j++)
3682: xi[i][j]=(i==j ? 1.0 : 0.0);
3683: printf("Powell\n"); fprintf(ficlog,"Powell\n");
1.201 brouard 3684: strcpy(filerespow,"POW_");
1.126 brouard 3685: strcat(filerespow,fileres);
3686: if((ficrespow=fopen(filerespow,"w"))==NULL) {
3687: printf("Problem with resultfile: %s\n", filerespow);
3688: fprintf(ficlog,"Problem with resultfile: %s\n", filerespow);
3689: }
3690: fprintf(ficrespow,"# Powell\n# iter -2*LL");
3691: for (i=1;i<=nlstate;i++)
3692: for(j=1;j<=nlstate+ndeath;j++)
3693: if(j!=i)fprintf(ficrespow," p%1d%1d",i,j);
3694: fprintf(ficrespow,"\n");
1.162 brouard 3695: #ifdef POWELL
1.126 brouard 3696: powell(p,xi,npar,ftol,&iter,&fret,func);
1.162 brouard 3697: #endif
1.126 brouard 3698:
1.162 brouard 3699: #ifdef NLOPT
3700: #ifdef NEWUOA
3701: opt = nlopt_create(NLOPT_LN_NEWUOA,npar);
3702: #else
3703: opt = nlopt_create(NLOPT_LN_BOBYQA,npar);
3704: #endif
3705: lb=vector(0,npar-1);
3706: for (i=0;i<npar;i++) lb[i]= -HUGE_VAL;
3707: nlopt_set_lower_bounds(opt, lb);
3708: nlopt_set_initial_step1(opt, 0.1);
3709:
3710: p1= (p+1); /* p *(p+1)@8 and p *(p1)@8 are equal p1[0]=p[1] */
3711: d->function = func;
3712: printf(" Func %.12lf \n",myfunc(npar,p1,NULL,d));
3713: nlopt_set_min_objective(opt, myfunc, d);
3714: nlopt_set_xtol_rel(opt, ftol);
3715: if ((creturn=nlopt_optimize(opt, p1, &minf)) < 0) {
3716: printf("nlopt failed! %d\n",creturn);
3717: }
3718: else {
3719: printf("found minimum after %d evaluations (NLOPT=%d)\n", countcallfunc ,NLOPT);
3720: printf("found minimum at f(%g,%g) = %0.10g\n", p[0], p[1], minf);
3721: iter=1; /* not equal */
3722: }
3723: nlopt_destroy(opt);
3724: #endif
1.126 brouard 3725: free_matrix(xi,1,npar,1,npar);
3726: fclose(ficrespow);
1.203 brouard 3727: printf("\n#Number of iterations & function calls = %d & %d, -2 Log likelihood = %.12f\n",iter, countcallfunc,func(p));
3728: fprintf(ficlog,"\n#Number of iterations & function calls = %d & %d, -2 Log likelihood = %.12f\n",iter, countcallfunc,func(p));
1.180 brouard 3729: fprintf(ficres,"#Number of iterations & function calls = %d & %d, -2 Log likelihood = %.12f\n",iter, countcallfunc,func(p));
1.126 brouard 3730:
3731: }
3732:
3733: /**** Computes Hessian and covariance matrix ***/
1.203 brouard 3734: void hesscov(double **matcov, double **hess, double p[], int npar, double delti[], double ftolhess, double (*func)(double []))
1.126 brouard 3735: {
3736: double **a,**y,*x,pd;
1.203 brouard 3737: /* double **hess; */
1.164 brouard 3738: int i, j;
1.126 brouard 3739: int *indx;
3740:
3741: double hessii(double p[], double delta, int theta, double delti[],double (*func)(double []),int npar);
1.203 brouard 3742: double hessij(double p[], double **hess, double delti[], int i, int j,double (*func)(double []),int npar);
1.126 brouard 3743: void lubksb(double **a, int npar, int *indx, double b[]) ;
3744: void ludcmp(double **a, int npar, int *indx, double *d) ;
3745: double gompertz(double p[]);
1.203 brouard 3746: /* hess=matrix(1,npar,1,npar); */
1.126 brouard 3747:
3748: printf("\nCalculation of the hessian matrix. Wait...\n");
3749: fprintf(ficlog,"\nCalculation of the hessian matrix. Wait...\n");
3750: for (i=1;i<=npar;i++){
1.203 brouard 3751: printf("%d-",i);fflush(stdout);
3752: fprintf(ficlog,"%d-",i);fflush(ficlog);
1.126 brouard 3753:
3754: hess[i][i]=hessii(p,ftolhess,i,delti,func,npar);
3755:
3756: /* printf(" %f ",p[i]);
3757: printf(" %lf %lf %lf",hess[i][i],ftolhess,delti[i]);*/
3758: }
3759:
3760: for (i=1;i<=npar;i++) {
3761: for (j=1;j<=npar;j++) {
3762: if (j>i) {
1.203 brouard 3763: printf(".%d-%d",i,j);fflush(stdout);
3764: fprintf(ficlog,".%d-%d",i,j);fflush(ficlog);
3765: hess[i][j]=hessij(p,hess, delti,i,j,func,npar);
1.126 brouard 3766:
3767: hess[j][i]=hess[i][j];
3768: /*printf(" %lf ",hess[i][j]);*/
3769: }
3770: }
3771: }
3772: printf("\n");
3773: fprintf(ficlog,"\n");
3774:
3775: printf("\nInverting the hessian to get the covariance matrix. Wait...\n");
3776: fprintf(ficlog,"\nInverting the hessian to get the covariance matrix. Wait...\n");
3777:
3778: a=matrix(1,npar,1,npar);
3779: y=matrix(1,npar,1,npar);
3780: x=vector(1,npar);
3781: indx=ivector(1,npar);
3782: for (i=1;i<=npar;i++)
3783: for (j=1;j<=npar;j++) a[i][j]=hess[i][j];
3784: ludcmp(a,npar,indx,&pd);
3785:
3786: for (j=1;j<=npar;j++) {
3787: for (i=1;i<=npar;i++) x[i]=0;
3788: x[j]=1;
3789: lubksb(a,npar,indx,x);
3790: for (i=1;i<=npar;i++){
3791: matcov[i][j]=x[i];
3792: }
3793: }
3794:
3795: printf("\n#Hessian matrix#\n");
3796: fprintf(ficlog,"\n#Hessian matrix#\n");
3797: for (i=1;i<=npar;i++) {
3798: for (j=1;j<=npar;j++) {
1.203 brouard 3799: printf("%.6e ",hess[i][j]);
3800: fprintf(ficlog,"%.6e ",hess[i][j]);
1.126 brouard 3801: }
3802: printf("\n");
3803: fprintf(ficlog,"\n");
3804: }
3805:
1.203 brouard 3806: /* printf("\n#Covariance matrix#\n"); */
3807: /* fprintf(ficlog,"\n#Covariance matrix#\n"); */
3808: /* for (i=1;i<=npar;i++) { */
3809: /* for (j=1;j<=npar;j++) { */
3810: /* printf("%.6e ",matcov[i][j]); */
3811: /* fprintf(ficlog,"%.6e ",matcov[i][j]); */
3812: /* } */
3813: /* printf("\n"); */
3814: /* fprintf(ficlog,"\n"); */
3815: /* } */
3816:
1.126 brouard 3817: /* Recompute Inverse */
1.203 brouard 3818: /* for (i=1;i<=npar;i++) */
3819: /* for (j=1;j<=npar;j++) a[i][j]=matcov[i][j]; */
3820: /* ludcmp(a,npar,indx,&pd); */
3821:
3822: /* printf("\n#Hessian matrix recomputed#\n"); */
3823:
3824: /* for (j=1;j<=npar;j++) { */
3825: /* for (i=1;i<=npar;i++) x[i]=0; */
3826: /* x[j]=1; */
3827: /* lubksb(a,npar,indx,x); */
3828: /* for (i=1;i<=npar;i++){ */
3829: /* y[i][j]=x[i]; */
3830: /* printf("%.3e ",y[i][j]); */
3831: /* fprintf(ficlog,"%.3e ",y[i][j]); */
3832: /* } */
3833: /* printf("\n"); */
3834: /* fprintf(ficlog,"\n"); */
3835: /* } */
3836:
3837: /* Verifying the inverse matrix */
3838: #ifdef DEBUGHESS
3839: y=matprod2(y,hess,1,npar,1,npar,1,npar,matcov);
1.126 brouard 3840:
1.203 brouard 3841: printf("\n#Verification: multiplying the matrix of covariance by the Hessian matrix, should be unity:#\n");
3842: fprintf(ficlog,"\n#Verification: multiplying the matrix of covariance by the Hessian matrix. Should be unity:#\n");
1.126 brouard 3843:
3844: for (j=1;j<=npar;j++) {
3845: for (i=1;i<=npar;i++){
1.203 brouard 3846: printf("%.2f ",y[i][j]);
3847: fprintf(ficlog,"%.2f ",y[i][j]);
1.126 brouard 3848: }
3849: printf("\n");
3850: fprintf(ficlog,"\n");
3851: }
1.203 brouard 3852: #endif
1.126 brouard 3853:
3854: free_matrix(a,1,npar,1,npar);
3855: free_matrix(y,1,npar,1,npar);
3856: free_vector(x,1,npar);
3857: free_ivector(indx,1,npar);
1.203 brouard 3858: /* free_matrix(hess,1,npar,1,npar); */
1.126 brouard 3859:
3860:
3861: }
3862:
3863: /*************** hessian matrix ****************/
3864: double hessii(double x[], double delta, int theta, double delti[], double (*func)(double []), int npar)
1.203 brouard 3865: { /* Around values of x, computes the function func and returns the scales delti and hessian */
1.126 brouard 3866: int i;
3867: int l=1, lmax=20;
1.203 brouard 3868: double k1,k2, res, fx;
1.132 brouard 3869: double p2[MAXPARM+1]; /* identical to x */
1.126 brouard 3870: double delt=0.0001, delts, nkhi=10.,nkhif=1., khi=1.e-4;
3871: int k=0,kmax=10;
3872: double l1;
3873:
3874: fx=func(x);
3875: for (i=1;i<=npar;i++) p2[i]=x[i];
1.145 brouard 3876: for(l=0 ; l <=lmax; l++){ /* Enlarging the zone around the Maximum */
1.126 brouard 3877: l1=pow(10,l);
3878: delts=delt;
3879: for(k=1 ; k <kmax; k=k+1){
3880: delt = delta*(l1*k);
3881: p2[theta]=x[theta] +delt;
1.145 brouard 3882: k1=func(p2)-fx; /* Might be negative if too close to the theoretical maximum */
1.126 brouard 3883: p2[theta]=x[theta]-delt;
3884: k2=func(p2)-fx;
3885: /*res= (k1-2.0*fx+k2)/delt/delt; */
1.203 brouard 3886: res= (k1+k2)/delt/delt/2.; /* Divided by 2 because L and not 2*L */
1.126 brouard 3887:
1.203 brouard 3888: #ifdef DEBUGHESSII
1.126 brouard 3889: 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);
3890: 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);
3891: #endif
3892: /*if(fabs(k1-2.0*fx+k2) <1.e-13){ */
3893: if((k1 <khi/nkhi/2.) || (k2 <khi/nkhi/2.)){
3894: k=kmax;
3895: }
3896: else if((k1 >khi/nkhif) || (k2 >khi/nkhif)){ /* Keeps lastvalue before 3.84/2 KHI2 5% 1d.f. */
1.164 brouard 3897: k=kmax; l=lmax*10;
1.126 brouard 3898: }
3899: else if((k1 >khi/nkhi) || (k2 >khi/nkhi)){
3900: delts=delt;
3901: }
1.203 brouard 3902: } /* End loop k */
1.126 brouard 3903: }
3904: delti[theta]=delts;
3905: return res;
3906:
3907: }
3908:
1.203 brouard 3909: double hessij( double x[], double **hess, double delti[], int thetai,int thetaj,double (*func)(double []),int npar)
1.126 brouard 3910: {
3911: int i;
1.164 brouard 3912: int l=1, lmax=20;
1.126 brouard 3913: double k1,k2,k3,k4,res,fx;
1.132 brouard 3914: double p2[MAXPARM+1];
1.203 brouard 3915: int k, kmax=1;
3916: double v1, v2, cv12, lc1, lc2;
1.208 brouard 3917:
3918: int firstime=0;
1.203 brouard 3919:
1.126 brouard 3920: fx=func(x);
1.203 brouard 3921: for (k=1; k<=kmax; k=k+10) {
1.126 brouard 3922: for (i=1;i<=npar;i++) p2[i]=x[i];
1.203 brouard 3923: p2[thetai]=x[thetai]+delti[thetai]*k;
3924: p2[thetaj]=x[thetaj]+delti[thetaj]*k;
1.126 brouard 3925: k1=func(p2)-fx;
3926:
1.203 brouard 3927: p2[thetai]=x[thetai]+delti[thetai]*k;
3928: p2[thetaj]=x[thetaj]-delti[thetaj]*k;
1.126 brouard 3929: k2=func(p2)-fx;
3930:
1.203 brouard 3931: p2[thetai]=x[thetai]-delti[thetai]*k;
3932: p2[thetaj]=x[thetaj]+delti[thetaj]*k;
1.126 brouard 3933: k3=func(p2)-fx;
3934:
1.203 brouard 3935: p2[thetai]=x[thetai]-delti[thetai]*k;
3936: p2[thetaj]=x[thetaj]-delti[thetaj]*k;
1.126 brouard 3937: k4=func(p2)-fx;
1.203 brouard 3938: res=(k1-k2-k3+k4)/4.0/delti[thetai]/k/delti[thetaj]/k/2.; /* Because of L not 2*L */
3939: if(k1*k2*k3*k4 <0.){
1.208 brouard 3940: firstime=1;
1.203 brouard 3941: kmax=kmax+10;
1.208 brouard 3942: }
3943: if(kmax >=10 || firstime ==1){
1.218 brouard 3944: 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);
3945: 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 3946: 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);
3947: 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);
3948: }
3949: #ifdef DEBUGHESSIJ
3950: v1=hess[thetai][thetai];
3951: v2=hess[thetaj][thetaj];
3952: cv12=res;
3953: /* Computing eigen value of Hessian matrix */
3954: lc1=((v1+v2)+sqrt((v1+v2)*(v1+v2) - 4*(v1*v2-cv12*cv12)))/2.;
3955: lc2=((v1+v2)-sqrt((v1+v2)*(v1+v2) - 4*(v1*v2-cv12*cv12)))/2.;
3956: if ((lc2 <0) || (lc1 <0) ){
3957: printf("Warning: sub Hessian matrix '%d%d' does not have positive eigen values \n",thetai,thetaj);
3958: fprintf(ficlog, "Warning: sub Hessian matrix '%d%d' does not have positive eigen values \n",thetai,thetaj);
3959: 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);
3960: 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);
3961: }
1.126 brouard 3962: #endif
3963: }
3964: return res;
3965: }
3966:
1.203 brouard 3967: /* Not done yet: Was supposed to fix if not exactly at the maximum */
3968: /* double hessij( double x[], double delti[], int thetai,int thetaj,double (*func)(double []),int npar) */
3969: /* { */
3970: /* int i; */
3971: /* int l=1, lmax=20; */
3972: /* double k1,k2,k3,k4,res,fx; */
3973: /* double p2[MAXPARM+1]; */
3974: /* double delt=0.0001, delts, nkhi=10.,nkhif=1., khi=1.e-4; */
3975: /* int k=0,kmax=10; */
3976: /* double l1; */
3977:
3978: /* fx=func(x); */
3979: /* for(l=0 ; l <=lmax; l++){ /\* Enlarging the zone around the Maximum *\/ */
3980: /* l1=pow(10,l); */
3981: /* delts=delt; */
3982: /* for(k=1 ; k <kmax; k=k+1){ */
3983: /* delt = delti*(l1*k); */
3984: /* for (i=1;i<=npar;i++) p2[i]=x[i]; */
3985: /* p2[thetai]=x[thetai]+delti[thetai]/k; */
3986: /* p2[thetaj]=x[thetaj]+delti[thetaj]/k; */
3987: /* k1=func(p2)-fx; */
3988:
3989: /* p2[thetai]=x[thetai]+delti[thetai]/k; */
3990: /* p2[thetaj]=x[thetaj]-delti[thetaj]/k; */
3991: /* k2=func(p2)-fx; */
3992:
3993: /* p2[thetai]=x[thetai]-delti[thetai]/k; */
3994: /* p2[thetaj]=x[thetaj]+delti[thetaj]/k; */
3995: /* k3=func(p2)-fx; */
3996:
3997: /* p2[thetai]=x[thetai]-delti[thetai]/k; */
3998: /* p2[thetaj]=x[thetaj]-delti[thetaj]/k; */
3999: /* k4=func(p2)-fx; */
4000: /* res=(k1-k2-k3+k4)/4.0/delti[thetai]*k/delti[thetaj]*k/2.; /\* Because of L not 2*L *\/ */
4001: /* #ifdef DEBUGHESSIJ */
4002: /* 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); */
4003: /* 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); */
4004: /* #endif */
4005: /* if((k1 <khi/nkhi/2.) || (k2 <khi/nkhi/2.)|| (k4 <khi/nkhi/2.)|| (k4 <khi/nkhi/2.)){ */
4006: /* k=kmax; */
4007: /* } */
4008: /* else if((k1 >khi/nkhif) || (k2 >khi/nkhif) || (k4 >khi/nkhif) || (k4 >khi/nkhif)){ /\* Keeps lastvalue before 3.84/2 KHI2 5% 1d.f. *\/ */
4009: /* k=kmax; l=lmax*10; */
4010: /* } */
4011: /* else if((k1 >khi/nkhi) || (k2 >khi/nkhi)){ */
4012: /* delts=delt; */
4013: /* } */
4014: /* } /\* End loop k *\/ */
4015: /* } */
4016: /* delti[theta]=delts; */
4017: /* return res; */
4018: /* } */
4019:
4020:
1.126 brouard 4021: /************** Inverse of matrix **************/
4022: void ludcmp(double **a, int n, int *indx, double *d)
4023: {
4024: int i,imax,j,k;
4025: double big,dum,sum,temp;
4026: double *vv;
4027:
4028: vv=vector(1,n);
4029: *d=1.0;
4030: for (i=1;i<=n;i++) {
4031: big=0.0;
4032: for (j=1;j<=n;j++)
4033: if ((temp=fabs(a[i][j])) > big) big=temp;
4034: if (big == 0.0) nrerror("Singular matrix in routine ludcmp");
4035: vv[i]=1.0/big;
4036: }
4037: for (j=1;j<=n;j++) {
4038: for (i=1;i<j;i++) {
4039: sum=a[i][j];
4040: for (k=1;k<i;k++) sum -= a[i][k]*a[k][j];
4041: a[i][j]=sum;
4042: }
4043: big=0.0;
4044: for (i=j;i<=n;i++) {
4045: sum=a[i][j];
4046: for (k=1;k<j;k++)
4047: sum -= a[i][k]*a[k][j];
4048: a[i][j]=sum;
4049: if ( (dum=vv[i]*fabs(sum)) >= big) {
4050: big=dum;
4051: imax=i;
4052: }
4053: }
4054: if (j != imax) {
4055: for (k=1;k<=n;k++) {
4056: dum=a[imax][k];
4057: a[imax][k]=a[j][k];
4058: a[j][k]=dum;
4059: }
4060: *d = -(*d);
4061: vv[imax]=vv[j];
4062: }
4063: indx[j]=imax;
4064: if (a[j][j] == 0.0) a[j][j]=TINY;
4065: if (j != n) {
4066: dum=1.0/(a[j][j]);
4067: for (i=j+1;i<=n;i++) a[i][j] *= dum;
4068: }
4069: }
4070: free_vector(vv,1,n); /* Doesn't work */
4071: ;
4072: }
4073:
4074: void lubksb(double **a, int n, int *indx, double b[])
4075: {
4076: int i,ii=0,ip,j;
4077: double sum;
4078:
4079: for (i=1;i<=n;i++) {
4080: ip=indx[i];
4081: sum=b[ip];
4082: b[ip]=b[i];
4083: if (ii)
4084: for (j=ii;j<=i-1;j++) sum -= a[i][j]*b[j];
4085: else if (sum) ii=i;
4086: b[i]=sum;
4087: }
4088: for (i=n;i>=1;i--) {
4089: sum=b[i];
4090: for (j=i+1;j<=n;j++) sum -= a[i][j]*b[j];
4091: b[i]=sum/a[i][i];
4092: }
4093: }
4094:
4095: void pstamp(FILE *fichier)
4096: {
1.196 brouard 4097: fprintf(fichier,"# %s.%s\n#IMaCh version %s, %s\n#%s\n# %s", optionfilefiname,optionfilext,version,copyright, fullversion, strstart);
1.126 brouard 4098: }
4099:
4100: /************ Frequencies ********************/
1.226 brouard 4101: void freqsummary(char fileres[], int iagemin, int iagemax, int **s, double **agev, int nlstate, int imx, \
4102: int *Tvaraff, int *invalidvarcomb, int **nbcode, int *ncodemax,double **mint,double **anint, char strstart[], \
4103: int firstpass, int lastpass, int stepm, int weightopt, char model[])
4104: { /* Some frequencies */
4105:
1.227 brouard 4106: int i, m, jk, j1, bool, z1,j, k, iv;
1.226 brouard 4107: int iind=0, iage=0;
4108: int mi; /* Effective wave */
4109: int first;
4110: double ***freq; /* Frequencies */
4111: double *meanq;
4112: double **meanqt;
4113: double *pp, **prop, *posprop, *pospropt;
4114: double pos=0., posproptt=0., pospropta=0., k2, dateintsum=0,k2cpt=0;
4115: char fileresp[FILENAMELENGTH], fileresphtm[FILENAMELENGTH], fileresphtmfr[FILENAMELENGTH];
4116: double agebegin, ageend;
4117:
4118: pp=vector(1,nlstate);
4119: prop=matrix(1,nlstate,iagemin-AGEMARGE,iagemax+3+AGEMARGE);
4120: posprop=vector(1,nlstate); /* Counting the number of transition starting from a live state per age */
4121: pospropt=vector(1,nlstate); /* Counting the number of transition starting from a live state */
4122: /* prop=matrix(1,nlstate,iagemin,iagemax+3); */
4123: meanq=vector(1,nqfveff); /* Number of Quantitative Fixed Variables Effective */
4124: meanqt=matrix(1,lastpass,1,nqtveff);
4125: strcpy(fileresp,"P_");
4126: strcat(fileresp,fileresu);
4127: /*strcat(fileresphtm,fileresu);*/
4128: if((ficresp=fopen(fileresp,"w"))==NULL) {
4129: printf("Problem with prevalence resultfile: %s\n", fileresp);
4130: fprintf(ficlog,"Problem with prevalence resultfile: %s\n", fileresp);
4131: exit(0);
4132: }
1.214 brouard 4133:
1.226 brouard 4134: strcpy(fileresphtm,subdirfext(optionfilefiname,"PHTM_",".htm"));
4135: if((ficresphtm=fopen(fileresphtm,"w"))==NULL) {
4136: printf("Problem with prevalence HTM resultfile '%s' with errno='%s'\n",fileresphtm,strerror(errno));
4137: fprintf(ficlog,"Problem with prevalence HTM resultfile '%s' with errno='%s'\n",fileresphtm,strerror(errno));
4138: fflush(ficlog);
4139: exit(70);
4140: }
4141: else{
4142: fprintf(ficresphtm,"<html><head>\n<title>IMaCh PHTM_ %s</title></head>\n <body><font size=\"2\">%s <br> %s</font> \
1.214 brouard 4143: <hr size=\"2\" color=\"#EC5E5E\"> \n\
4144: Title=%s <br>Datafile=%s Firstpass=%d Lastpass=%d Stepm=%d Weight=%d Model=1+age+%s<br>\n",\
1.226 brouard 4145: fileresphtm,version,fullversion,title,datafile,firstpass,lastpass,stepm, weightopt, model);
4146: }
1.237 brouard 4147: fprintf(ficresphtm,"Current page is file <a href=\"%s\">%s</a><br>\n\n<h4>Frequencies and prevalence by age at begin of transition and dummy covariate value at beginning of transition</h4>\n",fileresphtm, fileresphtm);
1.214 brouard 4148:
1.226 brouard 4149: strcpy(fileresphtmfr,subdirfext(optionfilefiname,"PHTMFR_",".htm"));
4150: if((ficresphtmfr=fopen(fileresphtmfr,"w"))==NULL) {
4151: printf("Problem with frequency table HTM resultfile '%s' with errno='%s'\n",fileresphtmfr,strerror(errno));
4152: fprintf(ficlog,"Problem with frequency table HTM resultfile '%s' with errno='%s'\n",fileresphtmfr,strerror(errno));
4153: fflush(ficlog);
4154: exit(70);
4155: }
4156: else{
4157: fprintf(ficresphtmfr,"<html><head>\n<title>IMaCh PHTM_Frequency table %s</title></head>\n <body><font size=\"2\">%s <br> %s</font> \
1.214 brouard 4158: <hr size=\"2\" color=\"#EC5E5E\"> \n\
4159: Title=%s <br>Datafile=%s Firstpass=%d Lastpass=%d Stepm=%d Weight=%d Model=1+age+%s<br>\n",\
1.226 brouard 4160: fileresphtmfr,version,fullversion,title,datafile,firstpass,lastpass,stepm, weightopt, model);
4161: }
4162: fprintf(ficresphtmfr,"Current page is file <a href=\"%s\">%s</a><br>\n\n<h4>Frequencies of all effective transitions by age at begin of transition </h4>Unknown status is -1<br/>\n",fileresphtmfr, fileresphtmfr);
1.214 brouard 4163:
1.226 brouard 4164: freq= ma3x(-5,nlstate+ndeath,-5,nlstate+ndeath,iagemin-AGEMARGE,iagemax+3+AGEMARGE);
4165: j1=0;
1.126 brouard 4166:
1.227 brouard 4167: /* j=ncoveff; /\* Only fixed dummy covariates *\/ */
4168: j=cptcoveff; /* Only dummy covariates of the model */
1.226 brouard 4169: if (cptcovn<1) {j=1;ncodemax[1]=1;}
1.220 brouard 4170:
1.226 brouard 4171: first=1;
1.220 brouard 4172:
1.226 brouard 4173: /* Detects if a combination j1 is empty: for a multinomial variable like 3 education levels:
4174: reference=low_education V1=0,V2=0
4175: med_educ V1=1 V2=0,
4176: high_educ V1=0 V2=1
4177: Then V1=1 and V2=1 is a noisy combination that we want to exclude for the list 2**cptcoveff
4178: */
1.126 brouard 4179:
1.227 brouard 4180: for (j1 = 1; j1 <= (int) pow(2,j); j1++){ /* Loop on covariates combination in order of model, excluding quantitatives V4=0, V3=0 for example, fixed or varying covariates */
1.226 brouard 4181: posproptt=0.;
4182: /*printf("cptcoveff=%d Tvaraff=%d", cptcoveff,Tvaraff[1]);
4183: scanf("%d", i);*/
4184: for (i=-5; i<=nlstate+ndeath; i++)
4185: for (jk=-5; jk<=nlstate+ndeath; jk++)
1.231 brouard 4186: for(m=iagemin; m <= iagemax+3; m++)
4187: freq[i][jk][m]=0;
4188:
1.226 brouard 4189: for (i=1; i<=nlstate; i++) {
4190: for(m=iagemin; m <= iagemax+3; m++)
1.231 brouard 4191: prop[i][m]=0;
1.226 brouard 4192: posprop[i]=0;
4193: pospropt[i]=0;
4194: }
1.227 brouard 4195: /* for (z1=1; z1<= nqfveff; z1++) { */
4196: /* meanq[z1]+=0.; */
4197: /* for(m=1;m<=lastpass;m++){ */
4198: /* meanqt[m][z1]=0.; */
4199: /* } */
4200: /* } */
1.231 brouard 4201:
1.226 brouard 4202: dateintsum=0;
4203: k2cpt=0;
1.227 brouard 4204: /* For that combination of covariate j1, we count and print the frequencies in one pass */
1.226 brouard 4205: for (iind=1; iind<=imx; iind++) { /* For each individual iind */
4206: bool=1;
1.227 brouard 4207: if(anyvaryingduminmodel==0){ /* If All fixed covariates */
1.234 brouard 4208: if (cptcoveff >0) { /* Filter is here: Must be looked at for model=V1+V2+V3+V4 */
1.227 brouard 4209: /* for (z1=1; z1<= nqfveff; z1++) { */
4210: /* meanq[z1]+=coqvar[Tvar[z1]][iind]; /\* Computes mean of quantitative with selected filter *\/ */
4211: /* } */
1.234 brouard 4212: for (z1=1; z1<=cptcoveff; z1++) {
4213: /* if(Tvaraff[z1] ==-20){ */
4214: /* /\* sumnew+=cotvar[mw[mi][iind]][z1][iind]; *\/ */
4215: /* }else if(Tvaraff[z1] ==-10){ */
4216: /* /\* sumnew+=coqvar[z1][iind]; *\/ */
4217: /* }else */
4218: if (covar[Tvaraff[z1]][iind]!= nbcode[Tvaraff[z1]][codtabm(j1,z1)]){
4219: /* Tests if this individual iind responded to j1 (V4=1 V3=0) */
4220: bool=0;
4221: /* 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",
4222: bool,i,z1, z1, Tvaraff[z1],i,covar[Tvaraff[z1]][i],j1,z1,codtabm(j1,z1),
4223: j1,z1,nbcode[Tvaraff[z1]][codtabm(j1,z1)],j1);*/
4224: /* For j1=7 in V1+V2+V3+V4 = 0 1 1 0 and codtabm(7,3)=1 and nbcde[3][?]=1*/
4225: } /* Onlyf fixed */
4226: } /* end z1 */
4227: } /* cptcovn > 0 */
1.227 brouard 4228: } /* end any */
4229: if (bool==1){ /* We selected an individual iind satisfying combination j1 or all fixed */
1.234 brouard 4230: /* for(m=firstpass; m<=lastpass; m++){ */
4231: for(mi=1; mi<wav[iind];mi++){ /* For that wave */
4232: m=mw[mi][iind];
4233: if(anyvaryingduminmodel==1){ /* Some are varying covariates */
4234: for (z1=1; z1<=cptcoveff; z1++) {
4235: if( Fixed[Tmodelind[z1]]==1){
4236: iv= Tvar[Tmodelind[z1]]-ncovcol-nqv;
4237: if (cotvar[m][iv][iind]!= nbcode[Tvaraff[z1]][codtabm(j1,z1)]) /* iv=1 to ntv, right modality */
4238: bool=0;
4239: }else if( Fixed[Tmodelind[z1]]== 0) { /* fixed */
4240: if (covar[Tvaraff[z1]][iind]!= nbcode[Tvaraff[z1]][codtabm(j1,z1)]) {
4241: bool=0;
4242: }
4243: }
4244: }
4245: }/* Some are varying covariates, we tried to speed up if all fixed covariates in the model, avoiding waves loop */
4246: /* bool =0 we keep that guy which corresponds to the combination of dummy values */
4247: if(bool==1){
4248: /* dh[m][iind] or dh[mw[mi][iind]][iind] is the delay between two effective (mi) waves m=mw[mi][iind]
4249: and mw[mi+1][iind]. dh depends on stepm. */
4250: agebegin=agev[m][iind]; /* Age at beginning of wave before transition*/
4251: ageend=agev[m][iind]+(dh[m][iind])*stepm/YEARM; /* Age at end of wave and transition */
4252: if(m >=firstpass && m <=lastpass){
4253: k2=anint[m][iind]+(mint[m][iind]/12.);
4254: /*if ((k2>=dateprev1) && (k2<=dateprev2)) {*/
4255: if(agev[m][iind]==0) agev[m][iind]=iagemax+1; /* All ages equal to 0 are in iagemax+1 */
4256: if(agev[m][iind]==1) agev[m][iind]=iagemax+2; /* All ages equal to 1 are in iagemax+2 */
4257: if (s[m][iind]>0 && s[m][iind]<=nlstate) /* If status at wave m is known and a live state */
4258: prop[s[m][iind]][(int)agev[m][iind]] += weight[iind]; /* At age of beginning of transition, where status is known */
4259: if (m<lastpass) {
4260: /* if(s[m][iind]==4 && s[m+1][iind]==4) */
4261: /* printf(" num=%ld m=%d, iind=%d s1=%d s2=%d agev at m=%d\n", num[iind], m, iind,s[m][iind],s[m+1][iind], (int)agev[m][iind]); */
4262: if(s[m][iind]==-1)
4263: printf(" num=%ld m=%d, iind=%d s1=%d s2=%d agev at m=%d agebegin=%.2f ageend=%.2f, agemed=%d\n", num[iind], m, iind,s[m][iind],s[m+1][iind], (int)agev[m][iind],agebegin, ageend, (int)((agebegin+ageend)/2.));
4264: freq[s[m][iind]][s[m+1][iind]][(int)agev[m][iind]] += weight[iind]; /* At age of beginning of transition, where status is known */
4265: /* freq[s[m][iind]][s[m+1][iind]][(int)((agebegin+ageend)/2.)] += weight[iind]; */
4266: freq[s[m][iind]][s[m+1][iind]][iagemax+3] += weight[iind]; /* Total is in iagemax+3 *//* At age of beginning of transition, where status is known */
4267: }
4268: } /* end if between passes */
4269: if ((agev[m][iind]>1) && (agev[m][iind]< (iagemax+3)) && (anint[m][iind]!=9999) && (mint[m][iind]!=99)) {
4270: dateintsum=dateintsum+k2;
4271: k2cpt++;
4272: /* printf("iind=%ld dateintmean = %lf dateintsum=%lf k2cpt=%lf k2=%lf\n",iind, dateintsum/k2cpt, dateintsum,k2cpt, k2); */
4273: }
4274: } /* end bool 2 */
4275: } /* end m */
1.226 brouard 4276: } /* end bool */
4277: } /* end iind = 1 to imx */
4278: /* prop[s][age] is feeded for any initial and valid live state as well as
4279: freq[s1][s2][age] at single age of beginning the transition, for a combination j1 */
1.231 brouard 4280:
4281:
1.226 brouard 4282: /* fprintf(ficresp, "#Count between %.lf/%.lf/%.lf and %.lf/%.lf/%.lf\n",jprev1, mprev1,anprev1,jprev2, mprev2,anprev2);*/
4283: pstamp(ficresp);
1.227 brouard 4284: /* if (ncoveff>0) { */
4285: if (cptcoveff>0) {
1.226 brouard 4286: fprintf(ficresp, "\n#********** Variable ");
4287: fprintf(ficresphtm, "\n<br/><br/><h3>********** Variable ");
4288: fprintf(ficresphtmfr, "\n<br/><br/><h3>********** Variable ");
1.227 brouard 4289: for (z1=1; z1<=cptcoveff; z1++){
1.234 brouard 4290: fprintf(ficresp, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
4291: fprintf(ficresphtm, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
4292: fprintf(ficresphtmfr, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
1.226 brouard 4293: }
4294: fprintf(ficresp, "**********\n#");
4295: fprintf(ficresphtm, "**********</h3>\n");
4296: fprintf(ficresphtmfr, "**********</h3>\n");
4297: fprintf(ficlog, "\n#********** Variable ");
1.227 brouard 4298: for (z1=1; z1<=cptcoveff; z1++) fprintf(ficlog, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
1.226 brouard 4299: fprintf(ficlog, "**********\n");
4300: }
4301: fprintf(ficresphtm,"<table style=\"text-align:center; border: 1px solid\">");
4302: for(i=1; i<=nlstate;i++) {
4303: fprintf(ficresp, " Age Prev(%d) N(%d) N",i,i);
4304: fprintf(ficresphtm, "<th>Age</th><th>Prev(%d)</th><th>N(%d)</th><th>N</th>",i,i);
4305: }
4306: fprintf(ficresp, "\n");
4307: fprintf(ficresphtm, "\n");
1.231 brouard 4308:
1.226 brouard 4309: /* Header of frequency table by age */
4310: fprintf(ficresphtmfr,"<table style=\"text-align:center; border: 1px solid\">");
4311: fprintf(ficresphtmfr,"<th>Age</th> ");
4312: for(jk=-1; jk <=nlstate+ndeath; jk++){
4313: for(m=-1; m <=nlstate+ndeath; m++){
1.234 brouard 4314: if(jk!=0 && m!=0)
4315: fprintf(ficresphtmfr,"<th>%d%d</th> ",jk,m);
1.226 brouard 4316: }
4317: }
4318: fprintf(ficresphtmfr, "\n");
1.231 brouard 4319:
1.226 brouard 4320: /* For each age */
4321: for(iage=iagemin; iage <= iagemax+3; iage++){
4322: fprintf(ficresphtm,"<tr>");
4323: if(iage==iagemax+1){
1.231 brouard 4324: fprintf(ficlog,"1");
4325: fprintf(ficresphtmfr,"<tr><th>0</th> ");
1.226 brouard 4326: }else if(iage==iagemax+2){
1.231 brouard 4327: fprintf(ficlog,"0");
4328: fprintf(ficresphtmfr,"<tr><th>Unknown</th> ");
1.226 brouard 4329: }else if(iage==iagemax+3){
1.231 brouard 4330: fprintf(ficlog,"Total");
4331: fprintf(ficresphtmfr,"<tr><th>Total</th> ");
1.226 brouard 4332: }else{
1.231 brouard 4333: if(first==1){
4334: first=0;
4335: printf("See log file for details...\n");
4336: }
4337: fprintf(ficresphtmfr,"<tr><th>%d</th> ",iage);
4338: fprintf(ficlog,"Age %d", iage);
1.226 brouard 4339: }
4340: for(jk=1; jk <=nlstate ; jk++){
1.231 brouard 4341: for(m=-1, pp[jk]=0; m <=nlstate+ndeath ; m++)
4342: pp[jk] += freq[jk][m][iage];
1.226 brouard 4343: }
4344: for(jk=1; jk <=nlstate ; jk++){
1.231 brouard 4345: for(m=-1, pos=0; m <=0 ; m++)
4346: pos += freq[jk][m][iage];
4347: if(pp[jk]>=1.e-10){
4348: if(first==1){
4349: printf(" %d.=%.0f loss[%d]=%.1f%%",jk,pp[jk],jk,100*pos/pp[jk]);
4350: }
4351: fprintf(ficlog," %d.=%.0f loss[%d]=%.1f%%",jk,pp[jk],jk,100*pos/pp[jk]);
4352: }else{
4353: if(first==1)
4354: printf(" %d.=%.0f loss[%d]=NaNQ%%",jk,pp[jk],jk);
4355: fprintf(ficlog," %d.=%.0f loss[%d]=NaNQ%%",jk,pp[jk],jk);
4356: }
1.226 brouard 4357: }
1.231 brouard 4358:
1.226 brouard 4359: for(jk=1; jk <=nlstate ; jk++){
1.231 brouard 4360: /* posprop[jk]=0; */
4361: for(m=0, pp[jk]=0; m <=nlstate+ndeath; m++)/* Summing on all ages */
4362: pp[jk] += freq[jk][m][iage];
1.226 brouard 4363: } /* pp[jk] is the total number of transitions starting from state jk and any ending status until this age */
1.231 brouard 4364:
1.226 brouard 4365: for(jk=1,pos=0, pospropta=0.; jk <=nlstate ; jk++){
1.231 brouard 4366: pos += pp[jk]; /* pos is the total number of transitions until this age */
4367: posprop[jk] += prop[jk][iage]; /* prop is the number of transitions from a live state
4368: from jk at age iage prop[s[m][iind]][(int)agev[m][iind]] += weight[iind] */
4369: pospropta += prop[jk][iage]; /* prop is the number of transitions from a live state
4370: from jk at age iage prop[s[m][iind]][(int)agev[m][iind]] += weight[iind] */
1.226 brouard 4371: }
4372: for(jk=1; jk <=nlstate ; jk++){
1.231 brouard 4373: if(pos>=1.e-5){
4374: if(first==1)
4375: printf(" %d.=%.0f prev[%d]=%.1f%%",jk,pp[jk],jk,100*pp[jk]/pos);
4376: fprintf(ficlog," %d.=%.0f prev[%d]=%.1f%%",jk,pp[jk],jk,100*pp[jk]/pos);
4377: }else{
4378: if(first==1)
4379: printf(" %d.=%.0f prev[%d]=NaNQ%%",jk,pp[jk],jk);
4380: fprintf(ficlog," %d.=%.0f prev[%d]=NaNQ%%",jk,pp[jk],jk);
4381: }
4382: if( iage <= iagemax){
4383: if(pos>=1.e-5){
4384: fprintf(ficresp," %d %.5f %.0f %.0f",iage,prop[jk][iage]/pospropta, prop[jk][iage],pospropta);
4385: fprintf(ficresphtm,"<th>%d</th><td>%.5f</td><td>%.0f</td><td>%.0f</td>",iage,prop[jk][iage]/pospropta, prop[jk][iage],pospropta);
4386: /*probs[iage][jk][j1]= pp[jk]/pos;*/
4387: /*printf("\niage=%d jk=%d j1=%d %.5f %.0f %.0f %f",iage,jk,j1,pp[jk]/pos, pp[jk],pos,probs[iage][jk][j1]);*/
4388: }
4389: else{
4390: fprintf(ficresp," %d NaNq %.0f %.0f",iage,prop[jk][iage],pospropta);
4391: fprintf(ficresphtm,"<th>%d</th><td>NaNq</td><td>%.0f</td><td>%.0f</td>",iage, prop[jk][iage],pospropta);
4392: }
4393: }
4394: pospropt[jk] +=posprop[jk];
1.226 brouard 4395: } /* end loop jk */
4396: /* pospropt=0.; */
4397: for(jk=-1; jk <=nlstate+ndeath; jk++){
1.231 brouard 4398: for(m=-1; m <=nlstate+ndeath; m++){
4399: if(freq[jk][m][iage] !=0 ) { /* minimizing output */
4400: if(first==1){
4401: printf(" %d%d=%.0f",jk,m,freq[jk][m][iage]);
4402: }
4403: fprintf(ficlog," %d%d=%.0f",jk,m,freq[jk][m][iage]);
4404: }
4405: if(jk!=0 && m!=0)
4406: fprintf(ficresphtmfr,"<td>%.0f</td> ",freq[jk][m][iage]);
4407: }
1.226 brouard 4408: } /* end loop jk */
4409: posproptt=0.;
4410: for(jk=1; jk <=nlstate; jk++){
1.231 brouard 4411: posproptt += pospropt[jk];
1.226 brouard 4412: }
4413: fprintf(ficresphtmfr,"</tr>\n ");
4414: if(iage <= iagemax){
1.231 brouard 4415: fprintf(ficresp,"\n");
4416: fprintf(ficresphtm,"</tr>\n");
1.226 brouard 4417: }
4418: if(first==1)
1.231 brouard 4419: printf("Others in log...\n");
1.226 brouard 4420: fprintf(ficlog,"\n");
4421: } /* end loop age iage */
4422: fprintf(ficresphtm,"<tr><th>Tot</th>");
4423: for(jk=1; jk <=nlstate ; jk++){
4424: if(posproptt < 1.e-5){
1.231 brouard 4425: fprintf(ficresphtm,"<td>Nanq</td><td>%.0f</td><td>%.0f</td>",pospropt[jk],posproptt);
1.226 brouard 4426: }else{
1.231 brouard 4427: fprintf(ficresphtm,"<td>%.5f</td><td>%.0f</td><td>%.0f</td>",pospropt[jk]/posproptt,pospropt[jk],posproptt);
1.226 brouard 4428: }
4429: }
4430: fprintf(ficresphtm,"</tr>\n");
4431: fprintf(ficresphtm,"</table>\n");
4432: fprintf(ficresphtmfr,"</table>\n");
4433: if(posproptt < 1.e-5){
4434: fprintf(ficresphtm,"\n <p><b> This combination (%d) is not valid and no result will be produced</b></p>",j1);
4435: fprintf(ficresphtmfr,"\n <p><b> This combination (%d) is not valid and no result will be produced</b></p>",j1);
4436: fprintf(ficres,"\n This combination (%d) is not valid and no result will be produced\n\n",j1);
4437: invalidvarcomb[j1]=1;
4438: }else{
4439: fprintf(ficresphtm,"\n <p> This combination (%d) is valid and result will be produced.</p>",j1);
4440: invalidvarcomb[j1]=0;
4441: }
4442: fprintf(ficresphtmfr,"</table>\n");
4443: } /* end selected combination of covariate j1 */
4444: dateintmean=dateintsum/k2cpt;
1.231 brouard 4445:
1.226 brouard 4446: fclose(ficresp);
4447: fclose(ficresphtm);
4448: fclose(ficresphtmfr);
4449: free_vector(meanq,1,nqfveff);
4450: free_matrix(meanqt,1,lastpass,1,nqtveff);
4451: free_ma3x(freq,-5,nlstate+ndeath,-5,nlstate+ndeath, iagemin-AGEMARGE, iagemax+3+AGEMARGE);
4452: free_vector(pospropt,1,nlstate);
4453: free_vector(posprop,1,nlstate);
4454: free_matrix(prop,1,nlstate,iagemin-AGEMARGE, iagemax+3+AGEMARGE);
4455: free_vector(pp,1,nlstate);
4456: /* End of freqsummary */
4457: }
1.126 brouard 4458:
4459: /************ Prevalence ********************/
1.227 brouard 4460: 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)
4461: {
4462: /* Compute observed prevalence between dateprev1 and dateprev2 by counting the number of people
4463: in each health status at the date of interview (if between dateprev1 and dateprev2).
4464: We still use firstpass and lastpass as another selection.
4465: */
1.126 brouard 4466:
1.227 brouard 4467: int i, m, jk, j1, bool, z1,j, iv;
4468: int mi; /* Effective wave */
4469: int iage;
4470: double agebegin, ageend;
4471:
4472: double **prop;
4473: double posprop;
4474: double y2; /* in fractional years */
4475: int iagemin, iagemax;
4476: int first; /** to stop verbosity which is redirected to log file */
4477:
4478: iagemin= (int) agemin;
4479: iagemax= (int) agemax;
4480: /*pp=vector(1,nlstate);*/
4481: prop=matrix(1,nlstate,iagemin-AGEMARGE,iagemax+3+AGEMARGE);
4482: /* freq=ma3x(-1,nlstate+ndeath,-1,nlstate+ndeath,iagemin,iagemax+3);*/
4483: j1=0;
1.222 brouard 4484:
1.227 brouard 4485: /*j=cptcoveff;*/
4486: if (cptcovn<1) {j=1;ncodemax[1]=1;}
1.222 brouard 4487:
1.227 brouard 4488: first=1;
4489: for(j1=1; j1<= (int) pow(2,cptcoveff);j1++){ /* For each combination of covariate */
4490: for (i=1; i<=nlstate; i++)
4491: for(iage=iagemin-AGEMARGE; iage <= iagemax+3+AGEMARGE; iage++)
4492: prop[i][iage]=0.0;
4493: printf("Prevalence combination of varying and fixed dummies %d\n",j1);
4494: /* fprintf(ficlog," V%d=%d ",Tvaraff[j1],nbcode[Tvaraff[j1]][codtabm(k,j1)]); */
4495: fprintf(ficlog,"Prevalence combination of varying and fixed dummies %d\n",j1);
4496:
4497: for (i=1; i<=imx; i++) { /* Each individual */
4498: bool=1;
4499: /* for(m=firstpass; m<=lastpass; m++){/\* Other selection (we can limit to certain interviews*\/ */
4500: for(mi=1; mi<wav[i];mi++){ /* For this wave too look where individual can be counted V4=0 V3=0 */
4501: m=mw[mi][i];
4502: /* Tmodelind[z1]=k is the position of the varying covariate in the model, but which # within 1 to ntv? */
4503: /* Tvar[Tmodelind[z1]] is the n of Vn; n-ncovcol-nqv is the first time varying covariate or iv */
4504: for (z1=1; z1<=cptcoveff; z1++){
4505: if( Fixed[Tmodelind[z1]]==1){
4506: iv= Tvar[Tmodelind[z1]]-ncovcol-nqv;
4507: if (cotvar[m][iv][i]!= nbcode[Tvaraff[z1]][codtabm(j1,z1)]) /* iv=1 to ntv, right modality */
4508: bool=0;
4509: }else if( Fixed[Tmodelind[z1]]== 0) /* fixed */
4510: if (covar[Tvaraff[z1]][i]!= nbcode[Tvaraff[z1]][codtabm(j1,z1)]) {
4511: bool=0;
4512: }
4513: }
4514: if(bool==1){ /* Otherwise we skip that wave/person */
4515: agebegin=agev[m][i]; /* Age at beginning of wave before transition*/
4516: /* ageend=agev[m][i]+(dh[m][i])*stepm/YEARM; /\* Age at end of wave and transition *\/ */
4517: if(m >=firstpass && m <=lastpass){
4518: y2=anint[m][i]+(mint[m][i]/12.); /* Fractional date in year */
4519: if ((y2>=dateprev1) && (y2<=dateprev2)) { /* Here is the main selection (fractional years) */
4520: if(agev[m][i]==0) agev[m][i]=iagemax+1;
4521: if(agev[m][i]==1) agev[m][i]=iagemax+2;
4522: if((int)agev[m][i] <iagemin-AGEMARGE || (int)agev[m][i] >iagemax+3+AGEMARGE){
4523: 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);
4524: exit(1);
4525: }
4526: if (s[m][i]>0 && s[m][i]<=nlstate) {
4527: /*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]]);*/
4528: prop[s[m][i]][(int)agev[m][i]] += weight[i];/* At age of beginning of transition, where status is known */
4529: prop[s[m][i]][iagemax+3] += weight[i];
4530: } /* end valid statuses */
4531: } /* end selection of dates */
4532: } /* end selection of waves */
4533: } /* end bool */
4534: } /* end wave */
4535: } /* end individual */
4536: for(i=iagemin; i <= iagemax+3; i++){
4537: for(jk=1,posprop=0; jk <=nlstate ; jk++) {
4538: posprop += prop[jk][i];
4539: }
4540:
4541: for(jk=1; jk <=nlstate ; jk++){
4542: if( i <= iagemax){
4543: if(posprop>=1.e-5){
4544: probs[i][jk][j1]= prop[jk][i]/posprop;
4545: } else{
4546: if(first==1){
4547: first=0;
4548: printf("Warning Observed prevalence probs[%d][%d][%d]=%lf because of lack of cases\nSee others in log file...\n",jk,i,j1,probs[i][jk][j1]);
4549: }
4550: }
4551: }
4552: }/* end jk */
4553: }/* end i */
1.222 brouard 4554: /*} *//* end i1 */
1.227 brouard 4555: } /* end j1 */
1.222 brouard 4556:
1.227 brouard 4557: /* free_ma3x(freq,-1,nlstate+ndeath,-1,nlstate+ndeath, iagemin, iagemax+3);*/
4558: /*free_vector(pp,1,nlstate);*/
4559: free_matrix(prop,1,nlstate, iagemin-AGEMARGE,iagemax+3+AGEMARGE);
4560: } /* End of prevalence */
1.126 brouard 4561:
4562: /************* Waves Concatenation ***************/
4563:
4564: 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)
4565: {
4566: /* Concatenates waves: wav[i] is the number of effective (useful waves) of individual i.
4567: Death is a valid wave (if date is known).
4568: mw[mi][i] is the mi (mi=1 to wav[i]) effective wave of individual i
4569: dh[m][i] or dh[mw[mi][i]][i] is the delay between two effective waves m=mw[mi][i]
4570: and mw[mi+1][i]. dh depends on stepm.
1.227 brouard 4571: */
1.126 brouard 4572:
1.224 brouard 4573: int i=0, mi=0, m=0, mli=0;
1.126 brouard 4574: /* int j, k=0,jk, ju, jl,jmin=1e+5, jmax=-1;
4575: double sum=0., jmean=0.;*/
1.224 brouard 4576: int first=0, firstwo=0, firsthree=0, firstfour=0, firstfiv=0;
1.126 brouard 4577: int j, k=0,jk, ju, jl;
4578: double sum=0.;
4579: first=0;
1.214 brouard 4580: firstwo=0;
1.217 brouard 4581: firsthree=0;
1.218 brouard 4582: firstfour=0;
1.164 brouard 4583: jmin=100000;
1.126 brouard 4584: jmax=-1;
4585: jmean=0.;
1.224 brouard 4586:
4587: /* Treating live states */
1.214 brouard 4588: for(i=1; i<=imx; i++){ /* For simple cases and if state is death */
1.224 brouard 4589: mi=0; /* First valid wave */
1.227 brouard 4590: mli=0; /* Last valid wave */
1.126 brouard 4591: m=firstpass;
1.214 brouard 4592: while(s[m][i] <= nlstate){ /* a live state */
1.227 brouard 4593: if(m >firstpass && s[m][i]==s[m-1][i] && mint[m][i]==mint[m-1][i] && anint[m][i]==anint[m-1][i]){/* Two succesive identical information on wave m */
4594: mli=m-1;/* mw[++mi][i]=m-1; */
4595: }else if(s[m][i]>=1 || s[m][i]==-4 || s[m][i]==-5){ /* Since 0.98r4 if status=-2 vital status is really unknown, wave should be skipped */
4596: mw[++mi][i]=m;
4597: mli=m;
1.224 brouard 4598: } /* else might be a useless wave -1 and mi is not incremented and mw[mi] not updated */
4599: if(m < lastpass){ /* m < lastpass, standard case */
1.227 brouard 4600: m++; /* mi gives the "effective" current wave, m the current wave, go to next wave by incrementing m */
1.216 brouard 4601: }
1.227 brouard 4602: else{ /* m >= lastpass, eventual special issue with warning */
1.224 brouard 4603: #ifdef UNKNOWNSTATUSNOTCONTRIBUTING
1.227 brouard 4604: break;
1.224 brouard 4605: #else
1.227 brouard 4606: if(s[m][i]==-1 && (int) andc[i] == 9999 && (int)anint[m][i] != 9999){
4607: if(firsthree == 0){
4608: printf("Information! Unknown status for individual %ld line=%d occurred at last wave %d at known date %d/%d. Please, check if your unknown date of death %d/%d means a live state %d at wave %d. This case(%d)/wave(%d) contributes to the likelihood as pi. .\nOthers in log file only\n",num[i],i,lastpass,(int)mint[m][i],(int)anint[m][i], (int) moisdc[i], (int) andc[i], s[m][i], m, i, m);
4609: firsthree=1;
4610: }
4611: fprintf(ficlog,"Information! Unknown status for individual %ld line=%d occurred at last wave %d at known date %d/%d. Please, check if your unknown date of death %d/%d means a live state %d at wave %d. This case(%d)/wave(%d) contributes to the likelihood as pi. .\n",num[i],i,lastpass,(int)mint[m][i],(int)anint[m][i], (int) moisdc[i], (int) andc[i], s[m][i], m, i, m);
4612: mw[++mi][i]=m;
4613: mli=m;
4614: }
4615: if(s[m][i]==-2){ /* Vital status is really unknown */
4616: nbwarn++;
4617: if((int)anint[m][i] == 9999){ /* Has the vital status really been verified? */
4618: 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);
4619: fprintf(ficlog,"Warning! Vital status for individual %ld (line=%d) at last wave %d interviewed at date %d/%d is unknown %d. Please, check if the vital status and the date of death %d/%d are really unknown. This case (%d)/wave (%d) is skipped, no contribution to likelihood.\n",num[i],i,lastpass,(int)mint[m][i],(int)anint[m][i], s[m][i], (int) moisdc[i], (int) andc[i], i, m);
4620: }
4621: break;
4622: }
4623: break;
1.224 brouard 4624: #endif
1.227 brouard 4625: }/* End m >= lastpass */
1.126 brouard 4626: }/* end while */
1.224 brouard 4627:
1.227 brouard 4628: /* mi is the last effective wave, m is lastpass, mw[j][i] gives the # of j-th effective wave for individual i */
1.216 brouard 4629: /* After last pass */
1.224 brouard 4630: /* Treating death states */
1.214 brouard 4631: if (s[m][i] > nlstate){ /* In a death state */
1.227 brouard 4632: /* if( mint[m][i]==mdc[m][i] && anint[m][i]==andc[m][i]){ /\* same date of death and date of interview *\/ */
4633: /* } */
1.126 brouard 4634: mi++; /* Death is another wave */
4635: /* if(mi==0) never been interviewed correctly before death */
1.227 brouard 4636: /* Only death is a correct wave */
1.126 brouard 4637: mw[mi][i]=m;
1.224 brouard 4638: }
4639: #ifndef DISPATCHINGKNOWNDEATHAFTERLASTWAVE
1.227 brouard 4640: else if ((int) andc[i] != 9999) { /* Status is negative. A death occured after lastpass, we can't take it into account because of potential bias */
1.216 brouard 4641: /* m++; */
4642: /* mi++; */
4643: /* s[m][i]=nlstate+1; /\* We are setting the status to the last of non live state *\/ */
4644: /* mw[mi][i]=m; */
1.218 brouard 4645: if ((int)anint[m][i]!= 9999) { /* date of last interview is known */
1.227 brouard 4646: if((andc[i]+moisdc[i]/12.) <=(anint[m][i]+mint[m][i]/12.)){ /* death occured before last wave and status should have been death instead of -1 */
4647: nbwarn++;
4648: if(firstfiv==0){
4649: printf("Warning! Death for individual %ld line=%d occurred at %d/%d before last wave %d interviewed at %d/%d and should have been coded as death instead of '%d'. This case (%d)/wave (%d) is contributing to likelihood.\nOthers in log file only\n",num[i],i,(int) moisdc[i], (int) andc[i], lastpass,(int)mint[m][i],(int)anint[m][i], s[m][i], i,m );
4650: firstfiv=1;
4651: }else{
4652: fprintf(ficlog,"Warning! Death for individual %ld line=%d occurred at %d/%d before last wave %d interviewed at %d/%d and should have been coded as death instead of '%d'. This case (%d)/wave (%d) is contributing to likelihood.\n",num[i],i,(int) moisdc[i], (int) andc[i], lastpass,(int)mint[m][i],(int)anint[m][i], s[m][i], i,m );
4653: }
4654: }else{ /* Death occured afer last wave potential bias */
4655: nberr++;
4656: if(firstwo==0){
4657: printf("Error! Death for individual %ld line=%d occurred at %d/%d after last wave %d interviewed at %d/%d. Potential bias if other individuals are still alive at this date but ignored. This case (%d)/wave (%d) is skipped, no contribution to likelihood.\nOthers in log file only\n",num[i],i,(int) moisdc[i], (int) andc[i], lastpass,(int)mint[m][i],(int)anint[m][i], i,m );
4658: firstwo=1;
4659: }
4660: fprintf(ficlog,"Error! Death for individual %ld line=%d occurred at %d/%d after last wave %d interviewed at %d/%d. Potential bias if other individuals are still alive at this date but ignored. This case (%d)/wave (%d) is skipped, no contribution to likelihood.\n",num[i],i,(int) moisdc[i], (int) andc[i], lastpass,(int)mint[m][i],(int)anint[m][i], i,m );
4661: }
1.218 brouard 4662: }else{ /* end date of interview is known */
1.227 brouard 4663: /* death is known but not confirmed by death status at any wave */
4664: if(firstfour==0){
4665: 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 );
4666: firstfour=1;
4667: }
4668: 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 4669: }
1.224 brouard 4670: } /* end if date of death is known */
4671: #endif
4672: wav[i]=mi; /* mi should be the last effective wave (or mli) */
4673: /* wav[i]=mw[mi][i]; */
1.126 brouard 4674: if(mi==0){
4675: nbwarn++;
4676: if(first==0){
1.227 brouard 4677: printf("Warning! No valid information for individual %ld line=%d (skipped) and may be others, see log file\n",num[i],i);
4678: first=1;
1.126 brouard 4679: }
4680: if(first==1){
1.227 brouard 4681: fprintf(ficlog,"Warning! No valid information for individual %ld line=%d (skipped)\n",num[i],i);
1.126 brouard 4682: }
4683: } /* end mi==0 */
4684: } /* End individuals */
1.214 brouard 4685: /* wav and mw are no more changed */
1.223 brouard 4686:
1.214 brouard 4687:
1.126 brouard 4688: for(i=1; i<=imx; i++){
4689: for(mi=1; mi<wav[i];mi++){
4690: if (stepm <=0)
1.227 brouard 4691: dh[mi][i]=1;
1.126 brouard 4692: else{
1.227 brouard 4693: if (s[mw[mi+1][i]][i] > nlstate) { /* A death */
4694: if (agedc[i] < 2*AGESUP) {
4695: j= rint(agedc[i]*12-agev[mw[mi][i]][i]*12);
4696: if(j==0) j=1; /* Survives at least one month after exam */
4697: else if(j<0){
4698: nberr++;
4699: 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]);
4700: j=1; /* Temporary Dangerous patch */
4701: 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);
4702: 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]);
4703: 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);
4704: }
4705: k=k+1;
4706: if (j >= jmax){
4707: jmax=j;
4708: ijmax=i;
4709: }
4710: if (j <= jmin){
4711: jmin=j;
4712: ijmin=i;
4713: }
4714: sum=sum+j;
4715: /*if (j<0) printf("j=%d num=%d \n",j,i);*/
4716: /* printf("%d %d %d %d\n", s[mw[mi][i]][i] ,s[mw[mi+1][i]][i],j,i);*/
4717: }
4718: }
4719: else{
4720: j= rint( (agev[mw[mi+1][i]][i]*12 - agev[mw[mi][i]][i]*12));
1.126 brouard 4721: /* if (j<0) printf("%d %lf %lf %d %d %d\n", i,agev[mw[mi+1][i]][i], agev[mw[mi][i]][i],j,s[mw[mi][i]][i] ,s[mw[mi+1][i]][i]); */
1.223 brouard 4722:
1.227 brouard 4723: k=k+1;
4724: if (j >= jmax) {
4725: jmax=j;
4726: ijmax=i;
4727: }
4728: else if (j <= jmin){
4729: jmin=j;
4730: ijmin=i;
4731: }
4732: /* if (j<10) printf("j=%d jmin=%d num=%d ",j,jmin,i); */
4733: /*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]);*/
4734: if(j<0){
4735: nberr++;
4736: 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]);
4737: 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]);
4738: }
4739: sum=sum+j;
4740: }
4741: jk= j/stepm;
4742: jl= j -jk*stepm;
4743: ju= j -(jk+1)*stepm;
4744: if(mle <=1){ /* only if we use a the linear-interpoloation pseudo-likelihood */
4745: if(jl==0){
4746: dh[mi][i]=jk;
4747: bh[mi][i]=0;
4748: }else{ /* We want a negative bias in order to only have interpolation ie
4749: * to avoid the price of an extra matrix product in likelihood */
4750: dh[mi][i]=jk+1;
4751: bh[mi][i]=ju;
4752: }
4753: }else{
4754: if(jl <= -ju){
4755: dh[mi][i]=jk;
4756: bh[mi][i]=jl; /* bias is positive if real duration
4757: * is higher than the multiple of stepm and negative otherwise.
4758: */
4759: }
4760: else{
4761: dh[mi][i]=jk+1;
4762: bh[mi][i]=ju;
4763: }
4764: if(dh[mi][i]==0){
4765: dh[mi][i]=1; /* At least one step */
4766: bh[mi][i]=ju; /* At least one step */
4767: /* 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);*/
4768: }
4769: } /* end if mle */
1.126 brouard 4770: }
4771: } /* end wave */
4772: }
4773: jmean=sum/k;
4774: 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 4775: fprintf(ficlog,"Delay (in months) between two waves Min=%d (for indiviudal %d) Max=%d (%d) Mean=%f\n\n ",jmin, ijmin, jmax, ijmax, jmean);
1.227 brouard 4776: }
1.126 brouard 4777:
4778: /*********** Tricode ****************************/
1.220 brouard 4779: void tricode(int *cptcov, int *Tvar, int **nbcode, int imx, int *Ndum)
1.126 brouard 4780: {
1.144 brouard 4781: /**< Uses cptcovn+2*cptcovprod as the number of covariates */
4782: /* 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 4783: * Boring subroutine which should only output nbcode[Tvar[j]][k]
1.224 brouard 4784: * Tvar[5] in V2+V1+V3*age+V2*V4 is 4 (V4) even it is a time varying or quantitative variable
4785: * nbcode[Tvar[5]][1]= nbcode[4][1]=0, nbcode[4][2]=1 (usually);
1.144 brouard 4786: */
1.130 brouard 4787:
1.145 brouard 4788: int ij=1, k=0, j=0, i=0, maxncov=NCOVMAX;
1.136 brouard 4789: int modmaxcovj=0; /* Modality max of covariates j */
1.145 brouard 4790: int cptcode=0; /* Modality max of covariates j */
4791: int modmincovj=0; /* Modality min of covariates j */
4792:
4793:
1.220 brouard 4794: /* cptcoveff=0; */
1.224 brouard 4795: /* *cptcov=0; */
1.126 brouard 4796:
1.144 brouard 4797: for (k=1; k <= maxncov; k++) ncodemax[k]=0; /* Horrible constant again replaced by NCOVMAX */
1.126 brouard 4798:
1.224 brouard 4799: /* Loop on covariates without age and products and no quantitative variable */
4800: /* for (j=1; j<=(cptcovs); j++) { /\* From model V1 + V2*age+ V3 + V3*V4 keeps V1 + V3 = 2 only *\/ */
1.227 brouard 4801: for (k=1; k<=cptcovt; k++) { /* From model V1 + V2*age + V3 + V3*V4 keeps V1 + V3 = 2 only */
4802: for (j=-1; (j < maxncov); j++) Ndum[j]=0;
4803: if(Dummy[k]==0 && Typevar[k] !=1){ /* Dummy covariate and not age product */
4804: switch(Fixed[k]) {
4805: case 0: /* Testing on fixed dummy covariate, simple or product of fixed */
1.231 brouard 4806: for (i=1; i<=imx; i++) { /* Loop on individuals: reads the data file to get the maximum value of the modality of this covariate Vj*/
4807: ij=(int)(covar[Tvar[k]][i]);
4808: /* ij=0 or 1 or -1. Value of the covariate Tvar[j] for individual i
4809: * If product of Vn*Vm, still boolean *:
4810: * If it was coded 1, 2, 3, 4 should be splitted into 3 boolean variables
4811: * 1 => 0 0 0, 2 => 0 0 1, 3 => 0 1 1, 4=1 0 0 */
4812: /* Finds for covariate j, n=Tvar[j] of Vn . ij is the
4813: modality of the nth covariate of individual i. */
4814: if (ij > modmaxcovj)
4815: modmaxcovj=ij;
4816: else if (ij < modmincovj)
4817: modmincovj=ij;
4818: if ((ij < -1) && (ij > NCOVMAX)){
4819: printf( "Error: minimal is less than -1 or maximal is bigger than %d. Exiting. \n", NCOVMAX );
4820: exit(1);
4821: }else
4822: Ndum[ij]++; /*counts and stores the occurence of this modality 0, 1, -1*/
4823: /* If coded 1, 2, 3 , counts the number of 1 Ndum[1], number of 2, Ndum[2], etc */
4824: /*printf("i=%d ij=%d Ndum[ij]=%d imx=%d",i,ij,Ndum[ij],imx);*/
4825: /* getting the maximum value of the modality of the covariate
4826: (should be 0 or 1 now) Tvar[j]. If V=sex and male is coded 0 and
4827: female ies 1, then modmaxcovj=1.
4828: */
4829: } /* end for loop on individuals i */
4830: printf(" Minimal and maximal values of %d th covariate V%d: min=%d max=%d \n", k, Tvar[k], modmincovj, modmaxcovj);
4831: fprintf(ficlog," Minimal and maximal values of %d th covariate V%d: min=%d max=%d \n", k, Tvar[k], modmincovj, modmaxcovj);
4832: cptcode=modmaxcovj;
4833: /* Ndum[0] = frequency of 0 for model-covariate j, Ndum[1] frequency of 1 etc. */
4834: /*for (i=0; i<=cptcode; i++) {*/
4835: for (j=modmincovj; j<=modmaxcovj; j++) { /* j=-1 ? 0 and 1*//* For each value j of the modality of model-cov k */
4836: printf("Frequencies of covariates %d ie V%d with value %d: %d\n", k, Tvar[k], j, Ndum[j]);
4837: fprintf(ficlog, "Frequencies of covariates %d ie V%d with value %d: %d\n", k, Tvar[k], j, Ndum[j]);
4838: if( Ndum[j] != 0 ){ /* Counts if nobody answered modality j ie empty modality, we skip it and reorder */
4839: if( j != -1){
4840: ncodemax[k]++; /* ncodemax[k]= Number of modalities of the k th
4841: covariate for which somebody answered excluding
4842: undefined. Usually 2: 0 and 1. */
4843: }
4844: ncodemaxwundef[k]++; /* ncodemax[j]= Number of modalities of the k th
4845: covariate for which somebody answered including
4846: undefined. Usually 3: -1, 0 and 1. */
4847: } /* In fact ncodemax[k]=2 (dichotom. variables only) but it could be more for
4848: * historical reasons: 3 if coded 1, 2, 3 and 4 and Ndum[2]=0 */
4849: } /* Ndum[-1] number of undefined modalities */
4850:
4851: /* j is a covariate, n=Tvar[j] of Vn; Fills nbcode */
4852: /* For covariate j, modalities could be 1, 2, 3, 4, 5, 6, 7. */
4853: /* If Ndum[1]=0, Ndum[2]=0, Ndum[3]= 635, Ndum[4]=0, Ndum[5]=0, Ndum[6]=27, Ndum[7]=125; */
4854: /* modmincovj=3; modmaxcovj = 7; */
4855: /* There are only 3 modalities non empty 3, 6, 7 (or 2 if 27 is too few) : ncodemax[j]=3; */
4856: /* which will be coded 0, 1, 2 which in binary on 2=3-1 digits are 0=00 1=01, 2=10; */
4857: /* defining two dummy variables: variables V1_1 and V1_2.*/
4858: /* nbcode[Tvar[j]][ij]=k; */
4859: /* nbcode[Tvar[j]][1]=0; */
4860: /* nbcode[Tvar[j]][2]=1; */
4861: /* nbcode[Tvar[j]][3]=2; */
4862: /* To be continued (not working yet). */
4863: ij=0; /* ij is similar to i but can jump over null modalities */
4864: 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*/
4865: if (Ndum[i] == 0) { /* If nobody responded to this modality k */
4866: break;
4867: }
4868: ij++;
4869: nbcode[Tvar[k]][ij]=i; /* stores the original value of modality i in an array nbcode, ij modality from 1 to last non-nul modality. nbcode[1][1]=0 nbcode[1][2]=1*/
4870: cptcode = ij; /* New max modality for covar j */
4871: } /* end of loop on modality i=-1 to 1 or more */
4872: break;
1.227 brouard 4873: case 1: /* Testing on varying covariate, could be simple and
4874: * should look at waves or product of fixed *
4875: * varying. No time to test -1, assuming 0 and 1 only */
1.231 brouard 4876: ij=0;
4877: for(i=0; i<=1;i++){
4878: nbcode[Tvar[k]][++ij]=i;
4879: }
4880: break;
1.227 brouard 4881: default:
1.231 brouard 4882: break;
1.227 brouard 4883: } /* end switch */
4884: } /* end dummy test */
1.225 brouard 4885:
1.192 brouard 4886: /* for (k=0; k<= cptcode; k++) { /\* k=-1 ? k=0 to 1 *\//\* Could be 1 to 4 *\//\* cptcode=modmaxcovj *\/ */
4887: /* /\*recode from 0 *\/ */
4888: /* k is a modality. If we have model=V1+V1*sex */
4889: /* then: nbcode[1][1]=0 ; nbcode[1][2]=1; nbcode[2][1]=0 ; nbcode[2][2]=1; */
4890: /* But if some modality were not used, it is recoded from 0 to a newer modmaxcovj=cptcode *\/ */
4891: /* } */
4892: /* /\* cptcode = ij; *\/ /\* New max modality for covar j *\/ */
4893: /* if (ij > ncodemax[j]) { */
4894: /* printf( " Error ij=%d > ncodemax[%d]=%d\n", ij, j, ncodemax[j]); */
4895: /* fprintf(ficlog, " Error ij=%d > ncodemax[%d]=%d\n", ij, j, ncodemax[j]); */
4896: /* break; */
4897: /* } */
4898: /* } /\* end of loop on modality k *\/ */
1.137 brouard 4899: } /* end of loop on model-covariate j. nbcode[Tvarj][1]=0 and nbcode[Tvarj][2]=1 sets the value of covariate j*/
4900:
1.225 brouard 4901: for (k=-1; k< maxncov; k++) Ndum[k]=0;
1.227 brouard 4902: /* Look at fixed dummy (single or product) covariates to check empty modalities */
1.187 brouard 4903: for (i=1; i<=ncovmodel-2-nagesqr; i++) { /* -2, cste and age and eventually age*age */
1.225 brouard 4904: /* Listing of all covariables in statement model to see if some covariates appear twice. For example, V1 appears twice in V1+V1*V2.*/
1.227 brouard 4905: ij=Tvar[i]; /* Tvar 5,4,3,6,5,7,1,4 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V4*age */
4906: Ndum[ij]++; /* Count the # of 1, 2 etc: {1,1,1,2,2,1,1} because V1 once, V2 once, two V4 and V5 in above */
4907: /* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1, {2, 1, 1, 1, 2, 1, 1, 0, 0} */
1.225 brouard 4908: } /* V4+V3+V5, Ndum[1]@5={0, 0, 1, 1, 1} */
4909:
4910: ij=0;
1.227 brouard 4911: /* for (i=0; i<= maxncov-1; i++) { /\* modmaxcovj is unknown here. Only Ndum[2(V2),3(age*V3), 5(V3*V2) 6(V1*V4) *\/ */
4912: for (k=1; k<= cptcovt; k++) { /* modmaxcovj is unknown here. Only Ndum[2(V2),3(age*V3), 5(V3*V2) 6(V1*V4) */
1.225 brouard 4913: /*printf("Ndum[%d]=%d\n",i, Ndum[i]);*/
1.227 brouard 4914: /* if((Ndum[i]!=0) && (i<=ncovcol)){ /\* Tvar[i] <= ncovmodel ? *\/ */
4915: if(Ndum[Tvar[k]]!=0 && Dummy[k] == 0 && Typevar[k]==0){ /* Only Dummy and non empty in the model */
4916: /* If product not in single variable we don't print results */
1.225 brouard 4917: /*printf("diff Ndum[%d]=%d\n",i, Ndum[i]);*/
1.230 brouard 4918: ++ij;/* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1, */
4919: Tvaraff[ij]=Tvar[k]; /* For printing combination *//* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1, Tvar {5, 4, 3, 6, 5, 2, 7, 1, 1} Tvaraff={4, 3, 1} V4, V3, V1*/
4920: Tmodelind[ij]=k; /* Tmodelind: index in model of dummies Tmodelind[1]=2 V4: pos=2; V3: pos=3, V1=9 {2, 3, 9, ?, ?,} */
1.231 brouard 4921: TmodelInvind[ij]=Tvar[k]- ncovcol-nqv; /* Inverse TmodelInvind[2=V4]=2 second dummy varying cov (V4)4-1-1 {0, 2, 1, } TmodelInvind[3]=1 */
1.227 brouard 4922: if(Fixed[k]!=0)
4923: anyvaryingduminmodel=1;
1.231 brouard 4924: /* }else if((Ndum[i]!=0) && (i<=ncovcol+nqv)){ */
4925: /* Tvaraff[++ij]=-10; /\* Dont'n know how to treat quantitative variables yet *\/ */
4926: /* }else if((Ndum[i]!=0) && (i<=ncovcol+nqv+ntv)){ */
4927: /* Tvaraff[++ij]=i; /\*For printing (unclear) *\/ */
4928: /* }else if((Ndum[i]!=0) && (i<=ncovcol+nqv+ntv+nqtv)){ */
4929: /* Tvaraff[++ij]=-20; /\* Dont'n know how to treat quantitative variables yet *\/ */
1.227 brouard 4930: }
1.225 brouard 4931: } /* Tvaraff[1]@5 {3, 4, -20, 0, 0} Very strange */
4932: /* ij--; */
4933: /* cptcoveff=ij; /\*Number of total covariates*\/ */
4934: *cptcov=ij; /*Number of total real effective covariates: effective
1.231 brouard 4935: * because they can be excluded from the model and real
4936: * if in the model but excluded because missing values, but how to get k from ij?*/
1.227 brouard 4937: for(j=ij+1; j<= cptcovt; j++){
4938: Tvaraff[j]=0;
4939: Tmodelind[j]=0;
4940: }
1.228 brouard 4941: for(j=ntveff+1; j<= cptcovt; j++){
4942: TmodelInvind[j]=0;
4943: }
1.227 brouard 4944: /* To be sorted */
4945: ;
1.126 brouard 4946: }
4947:
1.145 brouard 4948:
1.126 brouard 4949: /*********** Health Expectancies ****************/
4950:
1.235 brouard 4951: void evsij(double ***eij, double x[], int nlstate, int stepm, int bage, int fage, double **oldm, double **savm, int cij, int estepm,char strstart[], int nres )
1.126 brouard 4952:
4953: {
4954: /* Health expectancies, no variances */
1.164 brouard 4955: int i, j, nhstepm, hstepm, h, nstepm;
1.126 brouard 4956: int nhstepma, nstepma; /* Decreasing with age */
4957: double age, agelim, hf;
4958: double ***p3mat;
4959: double eip;
4960:
1.238 brouard 4961: /* pstamp(ficreseij); */
1.126 brouard 4962: fprintf(ficreseij,"# (a) Life expectancies by health status at initial age and (b) health expectancies by health status at initial age\n");
4963: fprintf(ficreseij,"# Age");
4964: for(i=1; i<=nlstate;i++){
4965: for(j=1; j<=nlstate;j++){
4966: fprintf(ficreseij," e%1d%1d ",i,j);
4967: }
4968: fprintf(ficreseij," e%1d. ",i);
4969: }
4970: fprintf(ficreseij,"\n");
4971:
4972:
4973: if(estepm < stepm){
4974: printf ("Problem %d lower than %d\n",estepm, stepm);
4975: }
4976: else hstepm=estepm;
4977: /* We compute the life expectancy from trapezoids spaced every estepm months
4978: * This is mainly to measure the difference between two models: for example
4979: * if stepm=24 months pijx are given only every 2 years and by summing them
4980: * we are calculating an estimate of the Life Expectancy assuming a linear
4981: * progression in between and thus overestimating or underestimating according
4982: * to the curvature of the survival function. If, for the same date, we
4983: * estimate the model with stepm=1 month, we can keep estepm to 24 months
4984: * to compare the new estimate of Life expectancy with the same linear
4985: * hypothesis. A more precise result, taking into account a more precise
4986: * curvature will be obtained if estepm is as small as stepm. */
4987:
4988: /* For example we decided to compute the life expectancy with the smallest unit */
4989: /* hstepm beeing the number of stepms, if hstepm=1 the length of hstepm is stepm.
4990: nhstepm is the number of hstepm from age to agelim
4991: nstepm is the number of stepm from age to agelin.
4992: Look at hpijx to understand the reason of that which relies in memory size
4993: and note for a fixed period like estepm months */
4994: /* We decided (b) to get a life expectancy respecting the most precise curvature of the
4995: survival function given by stepm (the optimization length). Unfortunately it
4996: means that if the survival funtion is printed only each two years of age and if
4997: you sum them up and add 1 year (area under the trapezoids) you won't get the same
4998: results. So we changed our mind and took the option of the best precision.
4999: */
5000: hstepm=hstepm/stepm; /* Typically in stepm units, if stepm=6 & estepm=24 , = 24/6 months = 4 */
5001:
5002: agelim=AGESUP;
5003: /* If stepm=6 months */
5004: /* Computed by stepm unit matrices, product of hstepm matrices, stored
5005: in an array of nhstepm length: nhstepm=10, hstepm=4, stepm=6 months */
5006:
5007: /* nhstepm age range expressed in number of stepm */
5008: nstepm=(int) rint((agelim-bage)*YEARM/stepm); /* Biggest nstepm */
5009: /* Typically if 20 years nstepm = 20*12/6=40 stepm */
5010: /* if (stepm >= YEARM) hstepm=1;*/
5011: nhstepm = nstepm/hstepm;/* Expressed in hstepm, typically nhstepm=40/4=10 */
5012: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
5013:
5014: for (age=bage; age<=fage; age ++){
5015: nstepma=(int) rint((agelim-bage)*YEARM/stepm); /* Biggest nstepm */
5016: /* Typically if 20 years nstepm = 20*12/6=40 stepm */
5017: /* if (stepm >= YEARM) hstepm=1;*/
5018: nhstepma = nstepma/hstepm;/* Expressed in hstepm, typically nhstepma=40/4=10 */
5019:
5020: /* If stepm=6 months */
5021: /* Computed by stepm unit matrices, product of hstepma matrices, stored
5022: in an array of nhstepma length: nhstepma=10, hstepm=4, stepm=6 months */
5023:
1.235 brouard 5024: hpxij(p3mat,nhstepma,age,hstepm,x,nlstate,stepm,oldm, savm, cij, nres);
1.126 brouard 5025:
5026: hf=hstepm*stepm/YEARM; /* Duration of hstepm expressed in year unit. */
5027:
5028: printf("%d|",(int)age);fflush(stdout);
5029: fprintf(ficlog,"%d|",(int)age);fflush(ficlog);
5030:
5031: /* Computing expectancies */
5032: for(i=1; i<=nlstate;i++)
5033: for(j=1; j<=nlstate;j++)
5034: for (h=0, eij[i][j][(int)age]=0; h<=nhstepm-1; h++){
5035: eij[i][j][(int)age] += (p3mat[i][j][h]+p3mat[i][j][h+1])/2.0*hf;
5036:
5037: /* 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]);*/
5038:
5039: }
5040:
5041: fprintf(ficreseij,"%3.0f",age );
5042: for(i=1; i<=nlstate;i++){
5043: eip=0;
5044: for(j=1; j<=nlstate;j++){
5045: eip +=eij[i][j][(int)age];
5046: fprintf(ficreseij,"%9.4f", eij[i][j][(int)age] );
5047: }
5048: fprintf(ficreseij,"%9.4f", eip );
5049: }
5050: fprintf(ficreseij,"\n");
5051:
5052: }
5053: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
5054: printf("\n");
5055: fprintf(ficlog,"\n");
5056:
5057: }
5058:
1.235 brouard 5059: void cvevsij(double ***eij, double x[], int nlstate, int stepm, int bage, int fage, double **oldm, double **savm, int cij, int estepm,double delti[],double **matcov,char strstart[], int nres )
1.126 brouard 5060:
5061: {
5062: /* Covariances of health expectancies eij and of total life expectancies according
1.222 brouard 5063: to initial status i, ei. .
1.126 brouard 5064: */
5065: int i, j, nhstepm, hstepm, h, nstepm, k, cptj, cptj2, i2, j2, ij, ji;
5066: int nhstepma, nstepma; /* Decreasing with age */
5067: double age, agelim, hf;
5068: double ***p3matp, ***p3matm, ***varhe;
5069: double **dnewm,**doldm;
5070: double *xp, *xm;
5071: double **gp, **gm;
5072: double ***gradg, ***trgradg;
5073: int theta;
5074:
5075: double eip, vip;
5076:
5077: varhe=ma3x(1,nlstate*nlstate,1,nlstate*nlstate,(int) bage, (int) fage);
5078: xp=vector(1,npar);
5079: xm=vector(1,npar);
5080: dnewm=matrix(1,nlstate*nlstate,1,npar);
5081: doldm=matrix(1,nlstate*nlstate,1,nlstate*nlstate);
5082:
5083: pstamp(ficresstdeij);
5084: fprintf(ficresstdeij,"# Health expectancies with standard errors\n");
5085: fprintf(ficresstdeij,"# Age");
5086: for(i=1; i<=nlstate;i++){
5087: for(j=1; j<=nlstate;j++)
5088: fprintf(ficresstdeij," e%1d%1d (SE)",i,j);
5089: fprintf(ficresstdeij," e%1d. ",i);
5090: }
5091: fprintf(ficresstdeij,"\n");
5092:
5093: pstamp(ficrescveij);
5094: fprintf(ficrescveij,"# Subdiagonal matrix of covariances of health expectancies by age: cov(eij,ekl)\n");
5095: fprintf(ficrescveij,"# Age");
5096: for(i=1; i<=nlstate;i++)
5097: for(j=1; j<=nlstate;j++){
5098: cptj= (j-1)*nlstate+i;
5099: for(i2=1; i2<=nlstate;i2++)
5100: for(j2=1; j2<=nlstate;j2++){
5101: cptj2= (j2-1)*nlstate+i2;
5102: if(cptj2 <= cptj)
5103: fprintf(ficrescveij," %1d%1d,%1d%1d",i,j,i2,j2);
5104: }
5105: }
5106: fprintf(ficrescveij,"\n");
5107:
5108: if(estepm < stepm){
5109: printf ("Problem %d lower than %d\n",estepm, stepm);
5110: }
5111: else hstepm=estepm;
5112: /* We compute the life expectancy from trapezoids spaced every estepm months
5113: * This is mainly to measure the difference between two models: for example
5114: * if stepm=24 months pijx are given only every 2 years and by summing them
5115: * we are calculating an estimate of the Life Expectancy assuming a linear
5116: * progression in between and thus overestimating or underestimating according
5117: * to the curvature of the survival function. If, for the same date, we
5118: * estimate the model with stepm=1 month, we can keep estepm to 24 months
5119: * to compare the new estimate of Life expectancy with the same linear
5120: * hypothesis. A more precise result, taking into account a more precise
5121: * curvature will be obtained if estepm is as small as stepm. */
5122:
5123: /* For example we decided to compute the life expectancy with the smallest unit */
5124: /* hstepm beeing the number of stepms, if hstepm=1 the length of hstepm is stepm.
5125: nhstepm is the number of hstepm from age to agelim
5126: nstepm is the number of stepm from age to agelin.
5127: Look at hpijx to understand the reason of that which relies in memory size
5128: and note for a fixed period like estepm months */
5129: /* We decided (b) to get a life expectancy respecting the most precise curvature of the
5130: survival function given by stepm (the optimization length). Unfortunately it
5131: means that if the survival funtion is printed only each two years of age and if
5132: you sum them up and add 1 year (area under the trapezoids) you won't get the same
5133: results. So we changed our mind and took the option of the best precision.
5134: */
5135: hstepm=hstepm/stepm; /* Typically in stepm units, if stepm=6 & estepm=24 , = 24/6 months = 4 */
5136:
5137: /* If stepm=6 months */
5138: /* nhstepm age range expressed in number of stepm */
5139: agelim=AGESUP;
5140: nstepm=(int) rint((agelim-bage)*YEARM/stepm);
5141: /* Typically if 20 years nstepm = 20*12/6=40 stepm */
5142: /* if (stepm >= YEARM) hstepm=1;*/
5143: nhstepm = nstepm/hstepm;/* Expressed in hstepm, typically nhstepm=40/4=10 */
5144:
5145: p3matp=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
5146: p3matm=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
5147: gradg=ma3x(0,nhstepm,1,npar,1,nlstate*nlstate);
5148: trgradg =ma3x(0,nhstepm,1,nlstate*nlstate,1,npar);
5149: gp=matrix(0,nhstepm,1,nlstate*nlstate);
5150: gm=matrix(0,nhstepm,1,nlstate*nlstate);
5151:
5152: for (age=bage; age<=fage; age ++){
5153: nstepma=(int) rint((agelim-bage)*YEARM/stepm); /* Biggest nstepm */
5154: /* Typically if 20 years nstepm = 20*12/6=40 stepm */
5155: /* if (stepm >= YEARM) hstepm=1;*/
5156: nhstepma = nstepma/hstepm;/* Expressed in hstepm, typically nhstepma=40/4=10 */
1.218 brouard 5157:
1.126 brouard 5158: /* If stepm=6 months */
5159: /* Computed by stepm unit matrices, product of hstepma matrices, stored
5160: in an array of nhstepma length: nhstepma=10, hstepm=4, stepm=6 months */
5161:
5162: hf=hstepm*stepm/YEARM; /* Duration of hstepm expressed in year unit. */
1.218 brouard 5163:
1.126 brouard 5164: /* Computing Variances of health expectancies */
5165: /* Gradient is computed with plus gp and minus gm. Code is duplicated in order to
5166: decrease memory allocation */
5167: for(theta=1; theta <=npar; theta++){
5168: for(i=1; i<=npar; i++){
1.222 brouard 5169: xp[i] = x[i] + (i==theta ?delti[theta]:0);
5170: xm[i] = x[i] - (i==theta ?delti[theta]:0);
1.126 brouard 5171: }
1.235 brouard 5172: hpxij(p3matp,nhstepm,age,hstepm,xp,nlstate,stepm,oldm,savm, cij, nres);
5173: hpxij(p3matm,nhstepm,age,hstepm,xm,nlstate,stepm,oldm,savm, cij, nres);
1.218 brouard 5174:
1.126 brouard 5175: for(j=1; j<= nlstate; j++){
1.222 brouard 5176: for(i=1; i<=nlstate; i++){
5177: for(h=0; h<=nhstepm-1; h++){
5178: gp[h][(j-1)*nlstate + i] = (p3matp[i][j][h]+p3matp[i][j][h+1])/2.;
5179: gm[h][(j-1)*nlstate + i] = (p3matm[i][j][h]+p3matm[i][j][h+1])/2.;
5180: }
5181: }
1.126 brouard 5182: }
1.218 brouard 5183:
1.126 brouard 5184: for(ij=1; ij<= nlstate*nlstate; ij++)
1.222 brouard 5185: for(h=0; h<=nhstepm-1; h++){
5186: gradg[h][theta][ij]= (gp[h][ij]-gm[h][ij])/2./delti[theta];
5187: }
1.126 brouard 5188: }/* End theta */
5189:
5190:
5191: for(h=0; h<=nhstepm-1; h++)
5192: for(j=1; j<=nlstate*nlstate;j++)
1.222 brouard 5193: for(theta=1; theta <=npar; theta++)
5194: trgradg[h][j][theta]=gradg[h][theta][j];
1.126 brouard 5195:
1.218 brouard 5196:
1.222 brouard 5197: for(ij=1;ij<=nlstate*nlstate;ij++)
1.126 brouard 5198: for(ji=1;ji<=nlstate*nlstate;ji++)
1.222 brouard 5199: varhe[ij][ji][(int)age] =0.;
1.218 brouard 5200:
1.222 brouard 5201: printf("%d|",(int)age);fflush(stdout);
5202: fprintf(ficlog,"%d|",(int)age);fflush(ficlog);
5203: for(h=0;h<=nhstepm-1;h++){
1.126 brouard 5204: for(k=0;k<=nhstepm-1;k++){
1.222 brouard 5205: matprod2(dnewm,trgradg[h],1,nlstate*nlstate,1,npar,1,npar,matcov);
5206: matprod2(doldm,dnewm,1,nlstate*nlstate,1,npar,1,nlstate*nlstate,gradg[k]);
5207: for(ij=1;ij<=nlstate*nlstate;ij++)
5208: for(ji=1;ji<=nlstate*nlstate;ji++)
5209: varhe[ij][ji][(int)age] += doldm[ij][ji]*hf*hf;
1.126 brouard 5210: }
5211: }
1.218 brouard 5212:
1.126 brouard 5213: /* Computing expectancies */
1.235 brouard 5214: hpxij(p3matm,nhstepm,age,hstepm,x,nlstate,stepm,oldm, savm, cij,nres);
1.126 brouard 5215: for(i=1; i<=nlstate;i++)
5216: for(j=1; j<=nlstate;j++)
1.222 brouard 5217: for (h=0, eij[i][j][(int)age]=0; h<=nhstepm-1; h++){
5218: eij[i][j][(int)age] += (p3matm[i][j][h]+p3matm[i][j][h+1])/2.0*hf;
1.218 brouard 5219:
1.222 brouard 5220: /* if((int)age==70)printf("i=%2d,j=%2d,h=%2d,age=%3d,%9.4f,%9.4f,%9.4f\n",i,j,h,(int)age,p3mat[i][j][h],hf,eij[i][j][(int)age]);*/
1.218 brouard 5221:
1.222 brouard 5222: }
1.218 brouard 5223:
1.126 brouard 5224: fprintf(ficresstdeij,"%3.0f",age );
5225: for(i=1; i<=nlstate;i++){
5226: eip=0.;
5227: vip=0.;
5228: for(j=1; j<=nlstate;j++){
1.222 brouard 5229: eip += eij[i][j][(int)age];
5230: for(k=1; k<=nlstate;k++) /* Sum on j and k of cov(eij,eik) */
5231: vip += varhe[(j-1)*nlstate+i][(k-1)*nlstate+i][(int)age];
5232: 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 5233: }
5234: fprintf(ficresstdeij," %9.4f (%.4f)", eip, sqrt(vip));
5235: }
5236: fprintf(ficresstdeij,"\n");
1.218 brouard 5237:
1.126 brouard 5238: fprintf(ficrescveij,"%3.0f",age );
5239: for(i=1; i<=nlstate;i++)
5240: for(j=1; j<=nlstate;j++){
1.222 brouard 5241: cptj= (j-1)*nlstate+i;
5242: for(i2=1; i2<=nlstate;i2++)
5243: for(j2=1; j2<=nlstate;j2++){
5244: cptj2= (j2-1)*nlstate+i2;
5245: if(cptj2 <= cptj)
5246: fprintf(ficrescveij," %.4f", varhe[cptj][cptj2][(int)age]);
5247: }
1.126 brouard 5248: }
5249: fprintf(ficrescveij,"\n");
1.218 brouard 5250:
1.126 brouard 5251: }
5252: free_matrix(gm,0,nhstepm,1,nlstate*nlstate);
5253: free_matrix(gp,0,nhstepm,1,nlstate*nlstate);
5254: free_ma3x(gradg,0,nhstepm,1,npar,1,nlstate*nlstate);
5255: free_ma3x(trgradg,0,nhstepm,1,nlstate*nlstate,1,npar);
5256: free_ma3x(p3matm,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
5257: free_ma3x(p3matp,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
5258: printf("\n");
5259: fprintf(ficlog,"\n");
1.218 brouard 5260:
1.126 brouard 5261: free_vector(xm,1,npar);
5262: free_vector(xp,1,npar);
5263: free_matrix(dnewm,1,nlstate*nlstate,1,npar);
5264: free_matrix(doldm,1,nlstate*nlstate,1,nlstate*nlstate);
5265: free_ma3x(varhe,1,nlstate*nlstate,1,nlstate*nlstate,(int) bage, (int)fage);
5266: }
1.218 brouard 5267:
1.126 brouard 5268: /************ Variance ******************/
1.235 brouard 5269: void varevsij(char optionfilefiname[], double ***vareij, double **matcov, double x[], double delti[], int nlstate, int stepm, double bage, double fage, double **oldm, double **savm, double **prlim, double ftolpl, int *ncvyearp, int ij, int estepm, int cptcov, int cptcod, int popbased, int mobilav, char strstart[], int nres)
1.218 brouard 5270: {
5271: /* Variance of health expectancies */
5272: /* double **prevalim(double **prlim, int nlstate, double *xp, double age, double **oldm, double ** savm,double ftolpl);*/
5273: /* double **newm;*/
5274: /* int movingaverage(double ***probs, double bage,double fage, double ***mobaverage, int mobilav)*/
5275:
5276: /* int movingaverage(); */
5277: double **dnewm,**doldm;
5278: double **dnewmp,**doldmp;
5279: int i, j, nhstepm, hstepm, h, nstepm ;
5280: int k;
5281: double *xp;
5282: double **gp, **gm; /* for var eij */
5283: double ***gradg, ***trgradg; /*for var eij */
5284: double **gradgp, **trgradgp; /* for var p point j */
5285: double *gpp, *gmp; /* for var p point j */
5286: double **varppt; /* for var p point j nlstate to nlstate+ndeath */
5287: double ***p3mat;
5288: double age,agelim, hf;
5289: /* double ***mobaverage; */
5290: int theta;
5291: char digit[4];
5292: char digitp[25];
5293:
5294: char fileresprobmorprev[FILENAMELENGTH];
5295:
5296: if(popbased==1){
5297: if(mobilav!=0)
5298: strcpy(digitp,"-POPULBASED-MOBILAV_");
5299: else strcpy(digitp,"-POPULBASED-NOMOBIL_");
5300: }
5301: else
5302: strcpy(digitp,"-STABLBASED_");
1.126 brouard 5303:
1.218 brouard 5304: /* if (mobilav!=0) { */
5305: /* mobaverage= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
5306: /* if (movingaverage(probs, bage, fage, mobaverage,mobilav)!=0){ */
5307: /* fprintf(ficlog," Error in movingaverage mobilav=%d\n",mobilav); */
5308: /* printf(" Error in movingaverage mobilav=%d\n",mobilav); */
5309: /* } */
5310: /* } */
5311:
5312: strcpy(fileresprobmorprev,"PRMORPREV-");
5313: sprintf(digit,"%-d",ij);
5314: /*printf("DIGIT=%s, ij=%d ijr=%-d|\n",digit, ij,ij);*/
5315: strcat(fileresprobmorprev,digit); /* Tvar to be done */
5316: strcat(fileresprobmorprev,digitp); /* Popbased or not, mobilav or not */
5317: strcat(fileresprobmorprev,fileresu);
5318: if((ficresprobmorprev=fopen(fileresprobmorprev,"w"))==NULL) {
5319: printf("Problem with resultfile: %s\n", fileresprobmorprev);
5320: fprintf(ficlog,"Problem with resultfile: %s\n", fileresprobmorprev);
5321: }
5322: printf("Computing total mortality p.j=w1*p1j+w2*p2j+..: result on file '%s' \n",fileresprobmorprev);
5323: fprintf(ficlog,"Computing total mortality p.j=w1*p1j+w2*p2j+..: result on file '%s' \n",fileresprobmorprev);
5324: pstamp(ficresprobmorprev);
5325: fprintf(ficresprobmorprev,"# probabilities of dying before estepm=%d months for people of exact age and weighted probabilities w1*p1j+w2*p2j+... stand dev in()\n",estepm);
1.238 brouard 5326: fprintf(ficresprobmorprev,"# Selected quantitative variables and dummies");
5327: for (j=1; j<= nsq; j++){ /* For each selected (single) quantitative value */
5328: fprintf(ficresprobmorprev," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
5329: }
5330: for(j=1;j<=cptcoveff;j++)
5331: fprintf(ficresprobmorprev,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(ij,j)]);
5332: fprintf(ficresprobmorprev,"\n");
5333:
1.218 brouard 5334: fprintf(ficresprobmorprev,"# Age cov=%-d",ij);
5335: for(j=nlstate+1; j<=(nlstate+ndeath);j++){
5336: fprintf(ficresprobmorprev," p.%-d SE",j);
5337: for(i=1; i<=nlstate;i++)
5338: fprintf(ficresprobmorprev," w%1d p%-d%-d",i,i,j);
5339: }
5340: fprintf(ficresprobmorprev,"\n");
5341:
5342: fprintf(ficgp,"\n# Routine varevsij");
5343: fprintf(ficgp,"\nunset title \n");
5344: /* fprintf(fichtm, "#Local time at start: %s", strstart);*/
5345: 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");
5346: fprintf(fichtm,"\n<br>%s <br>\n",digitp);
5347: /* } */
5348: varppt = matrix(nlstate+1,nlstate+ndeath,nlstate+1,nlstate+ndeath);
5349: pstamp(ficresvij);
5350: fprintf(ficresvij,"# Variance and covariance of health expectancies e.j \n# (weighted average of eij where weights are ");
5351: if(popbased==1)
5352: 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);
5353: else
5354: fprintf(ficresvij,"the age specific period (stable) prevalences in each health state \n");
5355: fprintf(ficresvij,"# Age");
5356: for(i=1; i<=nlstate;i++)
5357: for(j=1; j<=nlstate;j++)
5358: fprintf(ficresvij," Cov(e.%1d, e.%1d)",i,j);
5359: fprintf(ficresvij,"\n");
5360:
5361: xp=vector(1,npar);
5362: dnewm=matrix(1,nlstate,1,npar);
5363: doldm=matrix(1,nlstate,1,nlstate);
5364: dnewmp= matrix(nlstate+1,nlstate+ndeath,1,npar);
5365: doldmp= matrix(nlstate+1,nlstate+ndeath,nlstate+1,nlstate+ndeath);
5366:
5367: gradgp=matrix(1,npar,nlstate+1,nlstate+ndeath);
5368: gpp=vector(nlstate+1,nlstate+ndeath);
5369: gmp=vector(nlstate+1,nlstate+ndeath);
5370: trgradgp =matrix(nlstate+1,nlstate+ndeath,1,npar); /* mu or p point j*/
1.126 brouard 5371:
1.218 brouard 5372: if(estepm < stepm){
5373: printf ("Problem %d lower than %d\n",estepm, stepm);
5374: }
5375: else hstepm=estepm;
5376: /* For example we decided to compute the life expectancy with the smallest unit */
5377: /* hstepm beeing the number of stepms, if hstepm=1 the length of hstepm is stepm.
5378: nhstepm is the number of hstepm from age to agelim
5379: nstepm is the number of stepm from age to agelim.
5380: Look at function hpijx to understand why because of memory size limitations,
5381: we decided (b) to get a life expectancy respecting the most precise curvature of the
5382: survival function given by stepm (the optimization length). Unfortunately it
5383: means that if the survival funtion is printed every two years of age and if
5384: you sum them up and add 1 year (area under the trapezoids) you won't get the same
5385: results. So we changed our mind and took the option of the best precision.
5386: */
5387: hstepm=hstepm/stepm; /* Typically in stepm units, if stepm=6 & estepm=24 , = 24/6 months = 4 */
5388: agelim = AGESUP;
5389: for (age=bage; age<=fage; age ++){ /* If stepm=6 months */
5390: nstepm=(int) rint((agelim-age)*YEARM/stepm); /* Typically 20 years = 20*12/6=40 */
5391: nhstepm = nstepm/hstepm;/* Expressed in hstepm, typically nhstepm=40/4=10 */
5392: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
5393: gradg=ma3x(0,nhstepm,1,npar,1,nlstate);
5394: gp=matrix(0,nhstepm,1,nlstate);
5395: gm=matrix(0,nhstepm,1,nlstate);
5396:
5397:
5398: for(theta=1; theta <=npar; theta++){
5399: for(i=1; i<=npar; i++){ /* Computes gradient x + delta*/
5400: xp[i] = x[i] + (i==theta ?delti[theta]:0);
5401: }
5402:
1.235 brouard 5403: prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp,ij, nresult);
1.218 brouard 5404:
5405: if (popbased==1) {
5406: if(mobilav ==0){
5407: for(i=1; i<=nlstate;i++)
5408: prlim[i][i]=probs[(int)age][i][ij];
5409: }else{ /* mobilav */
5410: for(i=1; i<=nlstate;i++)
5411: prlim[i][i]=mobaverage[(int)age][i][ij];
5412: }
5413: }
5414:
1.235 brouard 5415: hpxij(p3mat,nhstepm,age,hstepm,xp,nlstate,stepm,oldm,savm, ij,nres); /* Returns p3mat[i][j][h] for h=1 to nhstepm */
1.218 brouard 5416: for(j=1; j<= nlstate; j++){
5417: for(h=0; h<=nhstepm; h++){
5418: for(i=1, gp[h][j]=0.;i<=nlstate;i++)
5419: gp[h][j] += prlim[i][i]*p3mat[i][j][h];
5420: }
5421: }
5422: /* Next for computing probability of death (h=1 means
5423: computed over hstepm matrices product = hstepm*stepm months)
5424: as a weighted average of prlim.
5425: */
5426: for(j=nlstate+1;j<=nlstate+ndeath;j++){
5427: for(i=1,gpp[j]=0.; i<= nlstate; i++)
5428: gpp[j] += prlim[i][i]*p3mat[i][j][1];
5429: }
5430: /* end probability of death */
5431:
5432: for(i=1; i<=npar; i++) /* Computes gradient x - delta */
5433: xp[i] = x[i] - (i==theta ?delti[theta]:0);
5434:
1.235 brouard 5435: prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp, ij, nresult);
1.218 brouard 5436:
5437: if (popbased==1) {
5438: if(mobilav ==0){
5439: for(i=1; i<=nlstate;i++)
5440: prlim[i][i]=probs[(int)age][i][ij];
5441: }else{ /* mobilav */
5442: for(i=1; i<=nlstate;i++)
5443: prlim[i][i]=mobaverage[(int)age][i][ij];
5444: }
5445: }
5446:
1.235 brouard 5447: hpxij(p3mat,nhstepm,age,hstepm,xp,nlstate,stepm,oldm,savm, ij,nres);
1.218 brouard 5448:
5449: for(j=1; j<= nlstate; j++){ /* Sum of wi * eij = e.j */
5450: for(h=0; h<=nhstepm; h++){
5451: for(i=1, gm[h][j]=0.;i<=nlstate;i++)
5452: gm[h][j] += prlim[i][i]*p3mat[i][j][h];
5453: }
5454: }
5455: /* This for computing probability of death (h=1 means
5456: computed over hstepm matrices product = hstepm*stepm months)
5457: as a weighted average of prlim.
5458: */
5459: for(j=nlstate+1;j<=nlstate+ndeath;j++){
5460: for(i=1,gmp[j]=0.; i<= nlstate; i++)
5461: gmp[j] += prlim[i][i]*p3mat[i][j][1];
5462: }
5463: /* end probability of death */
5464:
5465: for(j=1; j<= nlstate; j++) /* vareij */
5466: for(h=0; h<=nhstepm; h++){
5467: gradg[h][theta][j]= (gp[h][j]-gm[h][j])/2./delti[theta];
5468: }
5469:
5470: for(j=nlstate+1; j<= nlstate+ndeath; j++){ /* var mu */
5471: gradgp[theta][j]= (gpp[j]-gmp[j])/2./delti[theta];
5472: }
5473:
5474: } /* End theta */
5475:
5476: trgradg =ma3x(0,nhstepm,1,nlstate,1,npar); /* veij */
5477:
5478: for(h=0; h<=nhstepm; h++) /* veij */
5479: for(j=1; j<=nlstate;j++)
5480: for(theta=1; theta <=npar; theta++)
5481: trgradg[h][j][theta]=gradg[h][theta][j];
5482:
5483: for(j=nlstate+1; j<=nlstate+ndeath;j++) /* mu */
5484: for(theta=1; theta <=npar; theta++)
5485: trgradgp[j][theta]=gradgp[theta][j];
5486:
5487:
5488: hf=hstepm*stepm/YEARM; /* Duration of hstepm expressed in year unit. */
5489: for(i=1;i<=nlstate;i++)
5490: for(j=1;j<=nlstate;j++)
5491: vareij[i][j][(int)age] =0.;
5492:
5493: for(h=0;h<=nhstepm;h++){
5494: for(k=0;k<=nhstepm;k++){
5495: matprod2(dnewm,trgradg[h],1,nlstate,1,npar,1,npar,matcov);
5496: matprod2(doldm,dnewm,1,nlstate,1,npar,1,nlstate,gradg[k]);
5497: for(i=1;i<=nlstate;i++)
5498: for(j=1;j<=nlstate;j++)
5499: vareij[i][j][(int)age] += doldm[i][j]*hf*hf;
5500: }
5501: }
5502:
5503: /* pptj */
5504: matprod2(dnewmp,trgradgp,nlstate+1,nlstate+ndeath,1,npar,1,npar,matcov);
5505: matprod2(doldmp,dnewmp,nlstate+1,nlstate+ndeath,1,npar,nlstate+1,nlstate+ndeath,gradgp);
5506: for(j=nlstate+1;j<=nlstate+ndeath;j++)
5507: for(i=nlstate+1;i<=nlstate+ndeath;i++)
5508: varppt[j][i]=doldmp[j][i];
5509: /* end ppptj */
5510: /* x centered again */
5511:
1.235 brouard 5512: prevalim(prlim,nlstate,x,age,oldm,savm,ftolpl,ncvyearp,ij, nresult);
1.218 brouard 5513:
5514: if (popbased==1) {
5515: if(mobilav ==0){
5516: for(i=1; i<=nlstate;i++)
5517: prlim[i][i]=probs[(int)age][i][ij];
5518: }else{ /* mobilav */
5519: for(i=1; i<=nlstate;i++)
5520: prlim[i][i]=mobaverage[(int)age][i][ij];
5521: }
5522: }
5523:
5524: /* This for computing probability of death (h=1 means
5525: computed over hstepm (estepm) matrices product = hstepm*stepm months)
5526: as a weighted average of prlim.
5527: */
1.235 brouard 5528: hpxij(p3mat,nhstepm,age,hstepm,x,nlstate,stepm,oldm,savm, ij, nres);
1.218 brouard 5529: for(j=nlstate+1;j<=nlstate+ndeath;j++){
5530: for(i=1,gmp[j]=0.;i<= nlstate; i++)
5531: gmp[j] += prlim[i][i]*p3mat[i][j][1];
5532: }
5533: /* end probability of death */
5534:
5535: fprintf(ficresprobmorprev,"%3d %d ",(int) age, ij);
5536: for(j=nlstate+1; j<=(nlstate+ndeath);j++){
5537: fprintf(ficresprobmorprev," %11.3e %11.3e",gmp[j], sqrt(varppt[j][j]));
5538: for(i=1; i<=nlstate;i++){
5539: fprintf(ficresprobmorprev," %11.3e %11.3e ",prlim[i][i],p3mat[i][j][1]);
5540: }
5541: }
5542: fprintf(ficresprobmorprev,"\n");
5543:
5544: fprintf(ficresvij,"%.0f ",age );
5545: for(i=1; i<=nlstate;i++)
5546: for(j=1; j<=nlstate;j++){
5547: fprintf(ficresvij," %.4f", vareij[i][j][(int)age]);
5548: }
5549: fprintf(ficresvij,"\n");
5550: free_matrix(gp,0,nhstepm,1,nlstate);
5551: free_matrix(gm,0,nhstepm,1,nlstate);
5552: free_ma3x(gradg,0,nhstepm,1,npar,1,nlstate);
5553: free_ma3x(trgradg,0,nhstepm,1,nlstate,1,npar);
5554: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
5555: } /* End age */
5556: free_vector(gpp,nlstate+1,nlstate+ndeath);
5557: free_vector(gmp,nlstate+1,nlstate+ndeath);
5558: free_matrix(gradgp,1,npar,nlstate+1,nlstate+ndeath);
5559: free_matrix(trgradgp,nlstate+1,nlstate+ndeath,1,npar); /* mu or p point j*/
5560: /* fprintf(ficgp,"\nunset parametric;unset label; set ter png small size 320, 240"); */
5561: fprintf(ficgp,"\nunset parametric;unset label; set ter svg size 640, 480");
5562: /* for(j=nlstate+1; j<= nlstate+ndeath; j++){ *//* Only the first actually */
5563: fprintf(ficgp,"\n set log y; unset log x;set xlabel \"Age\"; set ylabel \"Force of mortality (year-1)\";");
5564: fprintf(ficgp,"\nset out \"%s%s.svg\";",subdirf3(optionfilefiname,"VARMUPTJGR-",digitp),digit);
5565: /* fprintf(ficgp,"\n plot \"%s\" u 1:($3*%6.3f) not w l 1 ",fileresprobmorprev,YEARM/estepm); */
5566: /* fprintf(ficgp,"\n replot \"%s\" u 1:(($3+1.96*$4)*%6.3f) t \"95\%% interval\" w l 2 ",fileresprobmorprev,YEARM/estepm); */
5567: /* fprintf(ficgp,"\n replot \"%s\" u 1:(($3-1.96*$4)*%6.3f) not w l 2 ",fileresprobmorprev,YEARM/estepm); */
5568: fprintf(ficgp,"\n plot \"%s\" u 1:($3) not w l lt 1 ",subdirf(fileresprobmorprev));
5569: fprintf(ficgp,"\n replot \"%s\" u 1:(($3+1.96*$4)) t \"95%% interval\" w l lt 2 ",subdirf(fileresprobmorprev));
5570: fprintf(ficgp,"\n replot \"%s\" u 1:(($3-1.96*$4)) not w l lt 2 ",subdirf(fileresprobmorprev));
5571: fprintf(fichtm,"\n<br> File (multiple files are possible if covariates are present): <A href=\"%s\">%s</a>\n",subdirf(fileresprobmorprev),subdirf(fileresprobmorprev));
5572: 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);
5573: /* 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 5574: */
1.218 brouard 5575: /* fprintf(ficgp,"\nset out \"varmuptjgr%s%s%s.svg\";replot;",digitp,optionfilefiname,digit); */
5576: fprintf(ficgp,"\nset out;\nset out \"%s%s.svg\";replot;set out;\n",subdirf3(optionfilefiname,"VARMUPTJGR-",digitp),digit);
1.126 brouard 5577:
1.218 brouard 5578: free_vector(xp,1,npar);
5579: free_matrix(doldm,1,nlstate,1,nlstate);
5580: free_matrix(dnewm,1,nlstate,1,npar);
5581: free_matrix(doldmp,nlstate+1,nlstate+ndeath,nlstate+1,nlstate+ndeath);
5582: free_matrix(dnewmp,nlstate+1,nlstate+ndeath,1,npar);
5583: free_matrix(varppt,nlstate+1,nlstate+ndeath,nlstate+1,nlstate+ndeath);
5584: /* if (mobilav!=0) free_ma3x(mobaverage,1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
5585: fclose(ficresprobmorprev);
5586: fflush(ficgp);
5587: fflush(fichtm);
5588: } /* end varevsij */
1.126 brouard 5589:
5590: /************ Variance of prevlim ******************/
1.235 brouard 5591: 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[], int nres)
1.126 brouard 5592: {
1.205 brouard 5593: /* Variance of prevalence limit for each state ij using current parameters x[] and estimates of neighbourhood give by delti*/
1.126 brouard 5594: /* double **prevalim(double **prlim, int nlstate, double *xp, double age, double **oldm, double **savm,double ftolpl);*/
1.164 brouard 5595:
1.126 brouard 5596: double **dnewm,**doldm;
5597: int i, j, nhstepm, hstepm;
5598: double *xp;
5599: double *gp, *gm;
5600: double **gradg, **trgradg;
1.208 brouard 5601: double **mgm, **mgp;
1.126 brouard 5602: double age,agelim;
5603: int theta;
5604:
5605: pstamp(ficresvpl);
5606: fprintf(ficresvpl,"# Standard deviation of period (stable) prevalences \n");
5607: fprintf(ficresvpl,"# Age");
5608: for(i=1; i<=nlstate;i++)
5609: fprintf(ficresvpl," %1d-%1d",i,i);
5610: fprintf(ficresvpl,"\n");
5611:
5612: xp=vector(1,npar);
5613: dnewm=matrix(1,nlstate,1,npar);
5614: doldm=matrix(1,nlstate,1,nlstate);
5615:
5616: hstepm=1*YEARM; /* Every year of age */
5617: hstepm=hstepm/stepm; /* Typically in stepm units, if j= 2 years, = 2/6 months = 4 */
5618: agelim = AGESUP;
5619: for (age=bage; age<=fage; age ++){ /* If stepm=6 months */
5620: nhstepm=(int) rint((agelim-age)*YEARM/stepm); /* Typically 20 years = 20*12/6=40 */
5621: if (stepm >= YEARM) hstepm=1;
5622: nhstepm = nhstepm/hstepm; /* Typically 40/4=10 */
5623: gradg=matrix(1,npar,1,nlstate);
1.208 brouard 5624: mgp=matrix(1,npar,1,nlstate);
5625: mgm=matrix(1,npar,1,nlstate);
1.126 brouard 5626: gp=vector(1,nlstate);
5627: gm=vector(1,nlstate);
5628:
5629: for(theta=1; theta <=npar; theta++){
5630: for(i=1; i<=npar; i++){ /* Computes gradient */
5631: xp[i] = x[i] + (i==theta ?delti[theta]:0);
5632: }
1.209 brouard 5633: if((int)age==79 ||(int)age== 80 ||(int)age== 81 )
1.235 brouard 5634: prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp,ij,nres);
1.209 brouard 5635: else
1.235 brouard 5636: prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp,ij,nres);
1.208 brouard 5637: for(i=1;i<=nlstate;i++){
1.126 brouard 5638: gp[i] = prlim[i][i];
1.208 brouard 5639: mgp[theta][i] = prlim[i][i];
5640: }
1.126 brouard 5641: for(i=1; i<=npar; i++) /* Computes gradient */
5642: xp[i] = x[i] - (i==theta ?delti[theta]:0);
1.209 brouard 5643: if((int)age==79 ||(int)age== 80 ||(int)age== 81 )
1.235 brouard 5644: prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp,ij,nres);
1.209 brouard 5645: else
1.235 brouard 5646: prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp,ij,nres);
1.208 brouard 5647: for(i=1;i<=nlstate;i++){
1.126 brouard 5648: gm[i] = prlim[i][i];
1.208 brouard 5649: mgm[theta][i] = prlim[i][i];
5650: }
1.126 brouard 5651: for(i=1;i<=nlstate;i++)
5652: gradg[theta][i]= (gp[i]-gm[i])/2./delti[theta];
1.209 brouard 5653: /* gradg[theta][2]= -gradg[theta][1]; */ /* For testing if nlstate=2 */
1.126 brouard 5654: } /* End theta */
5655:
5656: trgradg =matrix(1,nlstate,1,npar);
5657:
5658: for(j=1; j<=nlstate;j++)
5659: for(theta=1; theta <=npar; theta++)
5660: trgradg[j][theta]=gradg[theta][j];
1.209 brouard 5661: /* if((int)age==79 ||(int)age== 80 ||(int)age== 81 ){ */
5662: /* printf("\nmgm mgp %d ",(int)age); */
5663: /* for(j=1; j<=nlstate;j++){ */
5664: /* printf(" %d ",j); */
5665: /* for(theta=1; theta <=npar; theta++) */
5666: /* printf(" %d %lf %lf",theta,mgm[theta][j],mgp[theta][j]); */
5667: /* printf("\n "); */
5668: /* } */
5669: /* } */
5670: /* if((int)age==79 ||(int)age== 80 ||(int)age== 81 ){ */
5671: /* printf("\n gradg %d ",(int)age); */
5672: /* for(j=1; j<=nlstate;j++){ */
5673: /* printf("%d ",j); */
5674: /* for(theta=1; theta <=npar; theta++) */
5675: /* printf("%d %lf ",theta,gradg[theta][j]); */
5676: /* printf("\n "); */
5677: /* } */
5678: /* } */
1.126 brouard 5679:
5680: for(i=1;i<=nlstate;i++)
5681: varpl[i][(int)age] =0.;
1.209 brouard 5682: if((int)age==79 ||(int)age== 80 ||(int)age== 81){
1.205 brouard 5683: matprod2(dnewm,trgradg,1,nlstate,1,npar,1,npar,matcov);
5684: matprod2(doldm,dnewm,1,nlstate,1,npar,1,nlstate,gradg);
5685: }else{
1.126 brouard 5686: matprod2(dnewm,trgradg,1,nlstate,1,npar,1,npar,matcov);
5687: matprod2(doldm,dnewm,1,nlstate,1,npar,1,nlstate,gradg);
1.205 brouard 5688: }
1.126 brouard 5689: for(i=1;i<=nlstate;i++)
5690: varpl[i][(int)age] = doldm[i][i]; /* Covariances are useless */
5691:
5692: fprintf(ficresvpl,"%.0f ",age );
5693: for(i=1; i<=nlstate;i++)
5694: fprintf(ficresvpl," %.5f (%.5f)",prlim[i][i],sqrt(varpl[i][(int)age]));
5695: fprintf(ficresvpl,"\n");
5696: free_vector(gp,1,nlstate);
5697: free_vector(gm,1,nlstate);
1.208 brouard 5698: free_matrix(mgm,1,npar,1,nlstate);
5699: free_matrix(mgp,1,npar,1,nlstate);
1.126 brouard 5700: free_matrix(gradg,1,npar,1,nlstate);
5701: free_matrix(trgradg,1,nlstate,1,npar);
5702: } /* End age */
5703:
5704: free_vector(xp,1,npar);
5705: free_matrix(doldm,1,nlstate,1,npar);
5706: free_matrix(dnewm,1,nlstate,1,nlstate);
5707:
5708: }
5709:
5710: /************ Variance of one-step probabilities ******************/
5711: void varprob(char optionfilefiname[], double **matcov, double x[], double delti[], int nlstate, double bage, double fage, int ij, int *Tvar, int **nbcode, int *ncodemax, char strstart[])
1.222 brouard 5712: {
5713: int i, j=0, k1, l1, tj;
5714: int k2, l2, j1, z1;
5715: int k=0, l;
5716: int first=1, first1, first2;
5717: double cv12, mu1, mu2, lc1, lc2, v12, v21, v11, v22,v1,v2, c12, tnalp;
5718: double **dnewm,**doldm;
5719: double *xp;
5720: double *gp, *gm;
5721: double **gradg, **trgradg;
5722: double **mu;
5723: double age, cov[NCOVMAX+1];
5724: double std=2.0; /* Number of standard deviation wide of confidence ellipsoids */
5725: int theta;
5726: char fileresprob[FILENAMELENGTH];
5727: char fileresprobcov[FILENAMELENGTH];
5728: char fileresprobcor[FILENAMELENGTH];
5729: double ***varpij;
5730:
5731: strcpy(fileresprob,"PROB_");
5732: strcat(fileresprob,fileres);
5733: if((ficresprob=fopen(fileresprob,"w"))==NULL) {
5734: printf("Problem with resultfile: %s\n", fileresprob);
5735: fprintf(ficlog,"Problem with resultfile: %s\n", fileresprob);
5736: }
5737: strcpy(fileresprobcov,"PROBCOV_");
5738: strcat(fileresprobcov,fileresu);
5739: if((ficresprobcov=fopen(fileresprobcov,"w"))==NULL) {
5740: printf("Problem with resultfile: %s\n", fileresprobcov);
5741: fprintf(ficlog,"Problem with resultfile: %s\n", fileresprobcov);
5742: }
5743: strcpy(fileresprobcor,"PROBCOR_");
5744: strcat(fileresprobcor,fileresu);
5745: if((ficresprobcor=fopen(fileresprobcor,"w"))==NULL) {
5746: printf("Problem with resultfile: %s\n", fileresprobcor);
5747: fprintf(ficlog,"Problem with resultfile: %s\n", fileresprobcor);
5748: }
5749: printf("Computing standard deviation of one-step probabilities: result on file '%s' \n",fileresprob);
5750: fprintf(ficlog,"Computing standard deviation of one-step probabilities: result on file '%s' \n",fileresprob);
5751: printf("Computing matrix of variance covariance of one-step probabilities: result on file '%s' \n",fileresprobcov);
5752: fprintf(ficlog,"Computing matrix of variance covariance of one-step probabilities: result on file '%s' \n",fileresprobcov);
5753: printf("and correlation matrix of one-step probabilities: result on file '%s' \n",fileresprobcor);
5754: fprintf(ficlog,"and correlation matrix of one-step probabilities: result on file '%s' \n",fileresprobcor);
5755: pstamp(ficresprob);
5756: fprintf(ficresprob,"#One-step probabilities and stand. devi in ()\n");
5757: fprintf(ficresprob,"# Age");
5758: pstamp(ficresprobcov);
5759: fprintf(ficresprobcov,"#One-step probabilities and covariance matrix\n");
5760: fprintf(ficresprobcov,"# Age");
5761: pstamp(ficresprobcor);
5762: fprintf(ficresprobcor,"#One-step probabilities and correlation matrix\n");
5763: fprintf(ficresprobcor,"# Age");
1.126 brouard 5764:
5765:
1.222 brouard 5766: for(i=1; i<=nlstate;i++)
5767: for(j=1; j<=(nlstate+ndeath);j++){
5768: fprintf(ficresprob," p%1d-%1d (SE)",i,j);
5769: fprintf(ficresprobcov," p%1d-%1d ",i,j);
5770: fprintf(ficresprobcor," p%1d-%1d ",i,j);
5771: }
5772: /* fprintf(ficresprob,"\n");
5773: fprintf(ficresprobcov,"\n");
5774: fprintf(ficresprobcor,"\n");
5775: */
5776: xp=vector(1,npar);
5777: dnewm=matrix(1,(nlstate)*(nlstate+ndeath),1,npar);
5778: doldm=matrix(1,(nlstate)*(nlstate+ndeath),1,(nlstate)*(nlstate+ndeath));
5779: mu=matrix(1,(nlstate)*(nlstate+ndeath), (int) bage, (int)fage);
5780: varpij=ma3x(1,nlstate*(nlstate+ndeath),1,nlstate*(nlstate+ndeath),(int) bage, (int) fage);
5781: first=1;
5782: fprintf(ficgp,"\n# Routine varprob");
5783: fprintf(fichtm,"\n<li><h4> Computing and drawing one step probabilities with their confidence intervals</h4></li>\n");
5784: fprintf(fichtm,"\n");
5785:
5786: 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);
5787: 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);
5788: fprintf(fichtmcov,"\nEllipsoids of confidence centered on point (p<inf>ij</inf>, p<inf>kl</inf>) are estimated \
1.126 brouard 5789: and drawn. It helps understanding how is the covariance between two incidences.\
5790: They are expressed in year<sup>-1</sup> in order to be less dependent of stepm.<br>\n");
1.222 brouard 5791: fprintf(fichtmcov,"\n<br> Contour plot corresponding to x'cov<sup>-1</sup>x = 4 (where x is the column vector (pij,pkl)) are drawn. \
1.126 brouard 5792: It can be understood this way: if pij and pkl where uncorrelated the (2x2) matrix of covariance \
5793: would have been (1/(var pij), 0 , 0, 1/(var pkl)), and the confidence interval would be 2 \
5794: standard deviations wide on each axis. <br>\
5795: Now, if both incidences are correlated (usual case) we diagonalised the inverse of the covariance matrix\
5796: and made the appropriate rotation to look at the uncorrelated principal directions.<br>\
5797: To be simple, these graphs help to understand the significativity of each parameter in relation to a second other one.<br> \n");
5798:
1.222 brouard 5799: cov[1]=1;
5800: /* tj=cptcoveff; */
1.225 brouard 5801: tj = (int) pow(2,cptcoveff);
1.222 brouard 5802: if (cptcovn<1) {tj=1;ncodemax[1]=1;}
5803: j1=0;
1.224 brouard 5804: for(j1=1; j1<=tj;j1++){ /* For each valid combination of covariates or only once*/
1.222 brouard 5805: if (cptcovn>0) {
5806: fprintf(ficresprob, "\n#********** Variable ");
1.225 brouard 5807: for (z1=1; z1<=cptcoveff; z1++) fprintf(ficresprob, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
1.222 brouard 5808: fprintf(ficresprob, "**********\n#\n");
5809: fprintf(ficresprobcov, "\n#********** Variable ");
1.225 brouard 5810: for (z1=1; z1<=cptcoveff; z1++) fprintf(ficresprobcov, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
1.222 brouard 5811: fprintf(ficresprobcov, "**********\n#\n");
1.220 brouard 5812:
1.222 brouard 5813: fprintf(ficgp, "\n#********** Variable ");
1.225 brouard 5814: for (z1=1; z1<=cptcoveff; z1++) fprintf(ficgp, " V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
1.222 brouard 5815: fprintf(ficgp, "**********\n#\n");
1.220 brouard 5816:
5817:
1.222 brouard 5818: fprintf(fichtmcov, "\n<hr size=\"2\" color=\"#EC5E5E\">********** Variable ");
1.225 brouard 5819: for (z1=1; z1<=cptcoveff; z1++) fprintf(fichtm, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
1.222 brouard 5820: fprintf(fichtmcov, "**********\n<hr size=\"2\" color=\"#EC5E5E\">");
1.220 brouard 5821:
1.222 brouard 5822: fprintf(ficresprobcor, "\n#********** Variable ");
1.225 brouard 5823: for (z1=1; z1<=cptcoveff; z1++) fprintf(ficresprobcor, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
1.222 brouard 5824: fprintf(ficresprobcor, "**********\n#");
5825: if(invalidvarcomb[j1]){
5826: fprintf(ficgp,"\n#Combination (%d) ignored because no cases \n",j1);
5827: fprintf(fichtmcov,"\n<h3>Combination (%d) ignored because no cases </h3>\n",j1);
5828: continue;
5829: }
5830: }
5831: gradg=matrix(1,npar,1,(nlstate)*(nlstate+ndeath));
5832: trgradg=matrix(1,(nlstate)*(nlstate+ndeath),1,npar);
5833: gp=vector(1,(nlstate)*(nlstate+ndeath));
5834: gm=vector(1,(nlstate)*(nlstate+ndeath));
5835: for (age=bage; age<=fage; age ++){
5836: cov[2]=age;
5837: if(nagesqr==1)
5838: cov[3]= age*age;
5839: for (k=1; k<=cptcovn;k++) {
5840: cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(j1,k)];
5841: /*cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(j1,Tvar[k])];*//* j1 1 2 3 4
5842: * 1 1 1 1 1
5843: * 2 2 1 1 1
5844: * 3 1 2 1 1
5845: */
5846: /* nbcode[1][1]=0 nbcode[1][2]=1;*/
5847: }
5848: /* for (k=1; k<=cptcovage;k++) cov[2+Tage[k]]=cov[2+Tage[k]]*cov[2]; */
5849: for (k=1; k<=cptcovage;k++) cov[2+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,k)]*cov[2];
5850: for (k=1; k<=cptcovprod;k++)
5851: cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)]*nbcode[Tvard[k][2]][codtabm(ij,k)];
1.220 brouard 5852:
5853:
1.222 brouard 5854: for(theta=1; theta <=npar; theta++){
5855: for(i=1; i<=npar; i++)
5856: xp[i] = x[i] + (i==theta ?delti[theta]:(double)0);
1.220 brouard 5857:
1.222 brouard 5858: pmij(pmmij,cov,ncovmodel,xp,nlstate);
1.220 brouard 5859:
1.222 brouard 5860: k=0;
5861: for(i=1; i<= (nlstate); i++){
5862: for(j=1; j<=(nlstate+ndeath);j++){
5863: k=k+1;
5864: gp[k]=pmmij[i][j];
5865: }
5866: }
1.220 brouard 5867:
1.222 brouard 5868: for(i=1; i<=npar; i++)
5869: xp[i] = x[i] - (i==theta ?delti[theta]:(double)0);
1.220 brouard 5870:
1.222 brouard 5871: pmij(pmmij,cov,ncovmodel,xp,nlstate);
5872: k=0;
5873: for(i=1; i<=(nlstate); i++){
5874: for(j=1; j<=(nlstate+ndeath);j++){
5875: k=k+1;
5876: gm[k]=pmmij[i][j];
5877: }
5878: }
1.220 brouard 5879:
1.222 brouard 5880: for(i=1; i<= (nlstate)*(nlstate+ndeath); i++)
5881: gradg[theta][i]=(gp[i]-gm[i])/(double)2./delti[theta];
5882: }
1.126 brouard 5883:
1.222 brouard 5884: for(j=1; j<=(nlstate)*(nlstate+ndeath);j++)
5885: for(theta=1; theta <=npar; theta++)
5886: trgradg[j][theta]=gradg[theta][j];
1.220 brouard 5887:
1.222 brouard 5888: matprod2(dnewm,trgradg,1,(nlstate)*(nlstate+ndeath),1,npar,1,npar,matcov);
5889: matprod2(doldm,dnewm,1,(nlstate)*(nlstate+ndeath),1,npar,1,(nlstate)*(nlstate+ndeath),gradg);
1.220 brouard 5890:
1.222 brouard 5891: pmij(pmmij,cov,ncovmodel,x,nlstate);
1.220 brouard 5892:
1.222 brouard 5893: k=0;
5894: for(i=1; i<=(nlstate); i++){
5895: for(j=1; j<=(nlstate+ndeath);j++){
5896: k=k+1;
5897: mu[k][(int) age]=pmmij[i][j];
5898: }
5899: }
5900: for(i=1;i<=(nlstate)*(nlstate+ndeath);i++)
5901: for(j=1;j<=(nlstate)*(nlstate+ndeath);j++)
5902: varpij[i][j][(int)age] = doldm[i][j];
1.220 brouard 5903:
1.222 brouard 5904: /*printf("\n%d ",(int)age);
5905: for (i=1; i<=(nlstate)*(nlstate+ndeath);i++){
5906: printf("%e [%e ;%e] ",gm[i],gm[i]-2*sqrt(doldm[i][i]),gm[i]+2*sqrt(doldm[i][i]));
5907: fprintf(ficlog,"%e [%e ;%e] ",gm[i],gm[i]-2*sqrt(doldm[i][i]),gm[i]+2*sqrt(doldm[i][i]));
5908: }*/
1.220 brouard 5909:
1.222 brouard 5910: fprintf(ficresprob,"\n%d ",(int)age);
5911: fprintf(ficresprobcov,"\n%d ",(int)age);
5912: fprintf(ficresprobcor,"\n%d ",(int)age);
1.220 brouard 5913:
1.222 brouard 5914: for (i=1; i<=(nlstate)*(nlstate+ndeath);i++)
5915: fprintf(ficresprob,"%11.3e (%11.3e) ",mu[i][(int) age],sqrt(varpij[i][i][(int)age]));
5916: for (i=1; i<=(nlstate)*(nlstate+ndeath);i++){
5917: fprintf(ficresprobcov,"%11.3e ",mu[i][(int) age]);
5918: fprintf(ficresprobcor,"%11.3e ",mu[i][(int) age]);
5919: }
5920: i=0;
5921: for (k=1; k<=(nlstate);k++){
5922: for (l=1; l<=(nlstate+ndeath);l++){
5923: i++;
5924: fprintf(ficresprobcov,"\n%d %d-%d",(int)age,k,l);
5925: fprintf(ficresprobcor,"\n%d %d-%d",(int)age,k,l);
5926: for (j=1; j<=i;j++){
5927: /* printf(" k=%d l=%d i=%d j=%d\n",k,l,i,j);fflush(stdout); */
5928: fprintf(ficresprobcov," %11.3e",varpij[i][j][(int)age]);
5929: fprintf(ficresprobcor," %11.3e",varpij[i][j][(int) age]/sqrt(varpij[i][i][(int) age])/sqrt(varpij[j][j][(int)age]));
5930: }
5931: }
5932: }/* end of loop for state */
5933: } /* end of loop for age */
5934: free_vector(gp,1,(nlstate+ndeath)*(nlstate+ndeath));
5935: free_vector(gm,1,(nlstate+ndeath)*(nlstate+ndeath));
5936: free_matrix(trgradg,1,(nlstate+ndeath)*(nlstate+ndeath),1,npar);
5937: free_matrix(gradg,1,(nlstate+ndeath)*(nlstate+ndeath),1,npar);
5938:
5939: /* Confidence intervalle of pij */
5940: /*
5941: fprintf(ficgp,"\nunset parametric;unset label");
5942: fprintf(ficgp,"\nset log y;unset log x; set xlabel \"Age\";set ylabel \"probability (year-1)\"");
5943: fprintf(ficgp,"\nset ter png small\nset size 0.65,0.65");
5944: 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);
5945: fprintf(fichtm,"\n<br><img src=\"pijgr%s.png\"> ",optionfilefiname);
5946: fprintf(ficgp,"\nset out \"pijgr%s.png\"",optionfilefiname);
5947: fprintf(ficgp,"\nplot \"%s\" every :::%d::%d u 1:2 \"\%%lf",k1,k2,xfilevarprob);
5948: */
5949:
5950: /* Drawing ellipsoids of confidence of two variables p(k1-l1,k2-l2)*/
5951: first1=1;first2=2;
5952: for (k2=1; k2<=(nlstate);k2++){
5953: for (l2=1; l2<=(nlstate+ndeath);l2++){
5954: if(l2==k2) continue;
5955: j=(k2-1)*(nlstate+ndeath)+l2;
5956: for (k1=1; k1<=(nlstate);k1++){
5957: for (l1=1; l1<=(nlstate+ndeath);l1++){
5958: if(l1==k1) continue;
5959: i=(k1-1)*(nlstate+ndeath)+l1;
5960: if(i<=j) continue;
5961: for (age=bage; age<=fage; age ++){
5962: if ((int)age %5==0){
5963: v1=varpij[i][i][(int)age]/stepm*YEARM/stepm*YEARM;
5964: v2=varpij[j][j][(int)age]/stepm*YEARM/stepm*YEARM;
5965: cv12=varpij[i][j][(int)age]/stepm*YEARM/stepm*YEARM;
5966: mu1=mu[i][(int) age]/stepm*YEARM ;
5967: mu2=mu[j][(int) age]/stepm*YEARM;
5968: c12=cv12/sqrt(v1*v2);
5969: /* Computing eigen value of matrix of covariance */
5970: lc1=((v1+v2)+sqrt((v1+v2)*(v1+v2) - 4*(v1*v2-cv12*cv12)))/2.;
5971: lc2=((v1+v2)-sqrt((v1+v2)*(v1+v2) - 4*(v1*v2-cv12*cv12)))/2.;
5972: if ((lc2 <0) || (lc1 <0) ){
5973: if(first2==1){
5974: first1=0;
5975: 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);
5976: }
5977: 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);
5978: /* lc1=fabs(lc1); */ /* If we want to have them positive */
5979: /* lc2=fabs(lc2); */
5980: }
1.220 brouard 5981:
1.222 brouard 5982: /* Eigen vectors */
5983: v11=(1./sqrt(1+(v1-lc1)*(v1-lc1)/cv12/cv12));
5984: /*v21=sqrt(1.-v11*v11); *//* error */
5985: v21=(lc1-v1)/cv12*v11;
5986: v12=-v21;
5987: v22=v11;
5988: tnalp=v21/v11;
5989: if(first1==1){
5990: first1=0;
5991: 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);
5992: }
5993: 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);
5994: /*printf(fignu*/
5995: /* mu1+ v11*lc1*cost + v12*lc2*sin(t) */
5996: /* mu2+ v21*lc1*cost + v22*lc2*sin(t) */
5997: if(first==1){
5998: first=0;
5999: fprintf(ficgp,"\n# Ellipsoids of confidence\n#\n");
6000: fprintf(ficgp,"\nset parametric;unset label");
6001: 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);
6002: fprintf(ficgp,"\nset ter svg size 640, 480");
6003: fprintf(fichtmcov,"\n<br>Ellipsoids of confidence cov(p%1d%1d,p%1d%1d) expressed in year<sup>-1</sup>\
1.220 brouard 6004: :<a href=\"%s_%d%1d%1d-%1d%1d.svg\"> \
1.201 brouard 6005: %s_%d%1d%1d-%1d%1d.svg</A>, ",k1,l1,k2,l2,\
1.222 brouard 6006: subdirf2(optionfilefiname,"VARPIJGR_"), j1,k1,l1,k2,l2, \
6007: subdirf2(optionfilefiname,"VARPIJGR_"), j1,k1,l1,k2,l2);
6008: fprintf(fichtmcov,"\n<br><img src=\"%s_%d%1d%1d-%1d%1d.svg\"> ",subdirf2(optionfilefiname,"VARPIJGR_"), j1,k1,l1,k2,l2);
6009: fprintf(fichtmcov,"\n<br> Correlation at age %d (%.3f),",(int) age, c12);
6010: fprintf(ficgp,"\nset out \"%s_%d%1d%1d-%1d%1d.svg\"",subdirf2(optionfilefiname,"VARPIJGR_"), j1,k1,l1,k2,l2);
6011: fprintf(ficgp,"\nset label \"%d\" at %11.3e,%11.3e center",(int) age, mu1,mu2);
6012: fprintf(ficgp,"\n# Age %d, p%1d%1d - p%1d%1d",(int) age, k1,l1,k2,l2);
6013: 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", \
6014: mu1,std,v11,sqrt(lc1),v12,sqrt(lc2), \
6015: mu2,std,v21,sqrt(lc1),v22,sqrt(lc2));
6016: }else{
6017: first=0;
6018: fprintf(fichtmcov," %d (%.3f),",(int) age, c12);
6019: fprintf(ficgp,"\n# Age %d, p%1d%1d - p%1d%1d",(int) age, k1,l1,k2,l2);
6020: fprintf(ficgp,"\nset label \"%d\" at %11.3e,%11.3e center",(int) age, mu1,mu2);
6021: 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", \
6022: mu1,std,v11,sqrt(lc1),v12,sqrt(lc2), \
6023: mu2,std,v21,sqrt(lc1),v22,sqrt(lc2));
6024: }/* if first */
6025: } /* age mod 5 */
6026: } /* end loop age */
6027: fprintf(ficgp,"\nset out;\nset out \"%s_%d%1d%1d-%1d%1d.svg\";replot;set out;",subdirf2(optionfilefiname,"VARPIJGR_"), j1,k1,l1,k2,l2);
6028: first=1;
6029: } /*l12 */
6030: } /* k12 */
6031: } /*l1 */
6032: }/* k1 */
6033: } /* loop on combination of covariates j1 */
6034: free_ma3x(varpij,1,nlstate,1,nlstate+ndeath,(int) bage, (int)fage);
6035: free_matrix(mu,1,(nlstate+ndeath)*(nlstate+ndeath),(int) bage, (int)fage);
6036: free_matrix(doldm,1,(nlstate)*(nlstate+ndeath),1,(nlstate)*(nlstate+ndeath));
6037: free_matrix(dnewm,1,(nlstate)*(nlstate+ndeath),1,npar);
6038: free_vector(xp,1,npar);
6039: fclose(ficresprob);
6040: fclose(ficresprobcov);
6041: fclose(ficresprobcor);
6042: fflush(ficgp);
6043: fflush(fichtmcov);
6044: }
1.126 brouard 6045:
6046:
6047: /******************* Printing html file ***********/
1.201 brouard 6048: void printinghtml(char fileresu[], char title[], char datafile[], int firstpass, \
1.126 brouard 6049: int lastpass, int stepm, int weightopt, char model[],\
6050: int imx,int jmin, int jmax, double jmeanint,char rfileres[],\
1.217 brouard 6051: int popforecast, int prevfcast, int backcast, int estepm , \
1.213 brouard 6052: double jprev1, double mprev1,double anprev1, double dateprev1, \
6053: double jprev2, double mprev2,double anprev2, double dateprev2){
1.237 brouard 6054: int jj1, k1, i1, cpt, k4, nres;
1.126 brouard 6055:
6056: fprintf(fichtm,"<ul><li><a href='#firstorder'>Result files (first order: no variance)</a>\n \
6057: <li><a href='#secondorder'>Result files (second order (variance)</a>\n \
6058: </ul>");
1.237 brouard 6059: fprintf(fichtm,"<ul><li> model=1+age+%s\n \
6060: </ul>", model);
1.214 brouard 6061: fprintf(fichtm,"<ul><li><h4><a name='firstorder'>Result files (first order: no variance)</a></h4>\n");
6062: 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",
6063: jprev1, mprev1,anprev1,jprev2, mprev2,anprev2,subdirfext3(optionfilefiname,"PHTMFR_",".htm"),subdirfext3(optionfilefiname,"PHTMFR_",".htm"));
6064: 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 6065: jprev1, mprev1,anprev1,jprev2, mprev2,anprev2,subdirfext3(optionfilefiname,"PHTM_",".htm"),subdirfext3(optionfilefiname,"PHTM_",".htm"));
6066: fprintf(fichtm,", <a href=\"%s\">%s</a> (text file) <br>\n",subdirf2(fileresu,"P_"),subdirf2(fileresu,"P_"));
1.126 brouard 6067: fprintf(fichtm,"\
6068: - Estimated transition probabilities over %d (stepm) months: <a href=\"%s\">%s</a><br>\n ",
1.201 brouard 6069: stepm,subdirf2(fileresu,"PIJ_"),subdirf2(fileresu,"PIJ_"));
1.126 brouard 6070: fprintf(fichtm,"\
1.217 brouard 6071: - Estimated back transition probabilities over %d (stepm) months: <a href=\"%s\">%s</a><br>\n ",
6072: stepm,subdirf2(fileresu,"PIJB_"),subdirf2(fileresu,"PIJB_"));
6073: fprintf(fichtm,"\
1.126 brouard 6074: - Period (stable) prevalence in each health state: <a href=\"%s\">%s</a> <br>\n",
1.201 brouard 6075: subdirf2(fileresu,"PL_"),subdirf2(fileresu,"PL_"));
1.126 brouard 6076: fprintf(fichtm,"\
1.217 brouard 6077: - Period (stable) back prevalence in each health state: <a href=\"%s\">%s</a> <br>\n",
6078: subdirf2(fileresu,"PLB_"),subdirf2(fileresu,"PLB_"));
6079: fprintf(fichtm,"\
1.211 brouard 6080: - (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 6081: <a href=\"%s\">%s</a> <br>\n",
1.201 brouard 6082: estepm,subdirf2(fileresu,"E_"),subdirf2(fileresu,"E_"));
1.211 brouard 6083: if(prevfcast==1){
6084: fprintf(fichtm,"\
6085: - Prevalence projections by age and states: \
1.201 brouard 6086: <a href=\"%s\">%s</a> <br>\n</li>", subdirf2(fileresu,"F_"),subdirf2(fileresu,"F_"));
1.211 brouard 6087: }
1.126 brouard 6088:
1.222 brouard 6089: fprintf(fichtm," \n<ul><li><b>Graphs</b></li><p>");
1.126 brouard 6090:
1.225 brouard 6091: m=pow(2,cptcoveff);
1.222 brouard 6092: if (cptcovn < 1) {m=1;ncodemax[1]=1;}
1.126 brouard 6093:
1.222 brouard 6094: jj1=0;
1.237 brouard 6095:
6096: for(nres=1; nres <= nresult; nres++) /* For each resultline */
1.222 brouard 6097: for(k1=1; k1<=m;k1++){
1.237 brouard 6098: if(TKresult[nres]!= k1)
6099: continue;
1.220 brouard 6100:
1.222 brouard 6101: /* for(i1=1; i1<=ncodemax[k1];i1++){ */
6102: jj1++;
6103: if (cptcovn > 0) {
6104: fprintf(fichtm,"<hr size=\"2\" color=\"#EC5E5E\">************ Results for covariates");
1.225 brouard 6105: for (cpt=1; cpt<=cptcoveff;cpt++){
1.237 brouard 6106: fprintf(fichtm," V%d=%d ",Tvresult[nres][cpt],(int)Tresult[nres][cpt]);
6107: printf(" V%d=%d ",Tvresult[nres][cpt],Tresult[nres][cpt]);fflush(stdout);
6108: /* fprintf(fichtm," V%d=%d ",Tvaraff[cpt],nbcode[Tvaraff[cpt]][codtabm(jj1,cpt)]); */
6109: /* printf(" V%d=%d ",Tvaraff[cpt],nbcode[Tvaraff[cpt]][codtabm(jj1,cpt)]);fflush(stdout); */
1.222 brouard 6110: }
1.237 brouard 6111: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
6112: fprintf(fichtm," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
6113: printf(" V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);fflush(stdout);
6114: }
6115:
1.230 brouard 6116: /* if(nqfveff+nqtveff 0) */ /* Test to be done */
1.222 brouard 6117: fprintf(fichtm," ************\n<hr size=\"2\" color=\"#EC5E5E\">");
6118: if(invalidvarcomb[k1]){
6119: fprintf(fichtm,"\n<h3>Combination (%d) ignored because no cases </h3>\n",k1);
6120: printf("\nCombination (%d) ignored because no cases \n",k1);
6121: continue;
6122: }
6123: }
6124: /* aij, bij */
6125: fprintf(fichtm,"<br>- Logit model (yours is: 1+age+%s), for example: logit(pij)=log(pij/pii)= aij+ bij age + V1 age + etc. as a function of age: <a href=\"%s_%d-1.svg\">%s_%d-1.svg</a><br> \
1.211 brouard 6126: <img src=\"%s_%d-1.svg\">",model,subdirf2(optionfilefiname,"PE_"),jj1,subdirf2(optionfilefiname,"PE_"),jj1,subdirf2(optionfilefiname,"PE_"),jj1);
1.222 brouard 6127: /* Pij */
6128: 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 6129: <img src=\"%s_%d-2.svg\">",stepm,subdirf2(optionfilefiname,"PE_"),jj1,subdirf2(optionfilefiname,"PE_"),jj1,subdirf2(optionfilefiname,"PE_"),jj1);
1.222 brouard 6130: /* Quasi-incidences */
6131: fprintf(fichtm,"<br>\n- I<sub>ij</sub> or Conditional probabilities to be observed in state j being in state i %d (stepm) months\
1.220 brouard 6132: before but expressed in per year i.e. quasi incidences if stepm is small and probabilities too, \
1.211 brouard 6133: incidence (rates) are the limit when h tends to zero of the ratio of the probability <sub>h</sub>P<sub>ij</sub> \
6134: 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 6135: <img src=\"%s_%d-3.svg\">",stepm,subdirf2(optionfilefiname,"PE_"),jj1,subdirf2(optionfilefiname,"PE_"),jj1,subdirf2(optionfilefiname,"PE_"),jj1);
1.222 brouard 6136: /* Survival functions (period) in state j */
6137: for(cpt=1; cpt<=nlstate;cpt++){
6138: fprintf(fichtm,"<br>\n- Survival functions in state %d. Or probability to survive in state %d being in state (1 to %d) at different ages. <a href=\"%s%d_%d.svg\">%s%d_%d.svg</a><br> \
1.201 brouard 6139: <img src=\"%s_%d-%d.svg\">", cpt, cpt, nlstate, subdirf2(optionfilefiname,"LIJ_"),cpt,jj1,subdirf2(optionfilefiname,"LIJ_"),cpt,jj1,subdirf2(optionfilefiname,"LIJ_"),cpt,jj1);
1.222 brouard 6140: }
6141: /* State specific survival functions (period) */
6142: for(cpt=1; cpt<=nlstate;cpt++){
6143: fprintf(fichtm,"<br>\n- Survival functions from state %d in each live state and total.\
1.220 brouard 6144: Or probability to survive in various states (1 to %d) being in state %d at different ages. \
1.201 brouard 6145: <a href=\"%s%d_%d.svg\">%s%d_%d.svg</a><br> <img src=\"%s_%d-%d.svg\">", cpt, nlstate, cpt, subdirf2(optionfilefiname,"LIJT_"),cpt,jj1,subdirf2(optionfilefiname,"LIJT_"),cpt,jj1,subdirf2(optionfilefiname,"LIJT_"),cpt,jj1);
1.222 brouard 6146: }
6147: /* Period (stable) prevalence in each health state */
6148: for(cpt=1; cpt<=nlstate;cpt++){
6149: 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 6150: <img src=\"%s_%d-%d.svg\">", cpt, cpt, nlstate, subdirf2(optionfilefiname,"P_"),cpt,jj1,subdirf2(optionfilefiname,"P_"),cpt,jj1,subdirf2(optionfilefiname,"P_"),cpt,jj1);
1.222 brouard 6151: }
6152: if(backcast==1){
6153: /* Period (stable) back prevalence in each health state */
6154: for(cpt=1; cpt<=nlstate;cpt++){
6155: fprintf(fichtm,"<br>\n- Convergence to period (stable) back prevalence in state %d. Or probability to be in state %d being in state (1 to %d) at different ages. <a href=\"%s_%d-%d.svg\">%s_%d-%d.svg</a><br> \
1.217 brouard 6156: <img src=\"%s_%d-%d.svg\">", cpt, cpt, nlstate, subdirf2(optionfilefiname,"PB_"),cpt,jj1,subdirf2(optionfilefiname,"PB_"),cpt,jj1,subdirf2(optionfilefiname,"PB_"),cpt,jj1);
1.222 brouard 6157: }
1.217 brouard 6158: }
1.222 brouard 6159: if(prevfcast==1){
6160: /* Projection of prevalence up to period (stable) prevalence in each health state */
6161: for(cpt=1; cpt<=nlstate;cpt++){
6162: 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 6163: <img src=\"%s_%d-%d.svg\">", dateprev1, dateprev2, cpt, cpt, nlstate, subdirf2(optionfilefiname,"PROJ_"),cpt,jj1,subdirf2(optionfilefiname,"PROJ_"),cpt,jj1,subdirf2(optionfilefiname,"PROJ_"),cpt,jj1);
1.222 brouard 6164: }
6165: }
1.220 brouard 6166:
1.222 brouard 6167: for(cpt=1; cpt<=nlstate;cpt++) {
6168: 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 6169: <img src=\"%s_%d%d.svg\">",cpt,nlstate,subdirf2(optionfilefiname,"EXP_"),cpt,jj1,subdirf2(optionfilefiname,"EXP_"),cpt,jj1,subdirf2(optionfilefiname,"EXP_"),cpt,jj1);
1.222 brouard 6170: }
6171: /* } /\* end i1 *\/ */
6172: }/* End k1 */
6173: fprintf(fichtm,"</ul>");
1.126 brouard 6174:
1.222 brouard 6175: fprintf(fichtm,"\
1.126 brouard 6176: \n<br><li><h4> <a name='secondorder'>Result files (second order: variances)</a></h4>\n\
1.193 brouard 6177: - Parameter file with estimated parameters and covariance matrix: <a href=\"%s\">%s</a> <br> \
1.203 brouard 6178: - 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 6179: But because parameters are usually highly correlated (a higher incidence of disability \
6180: and a higher incidence of recovery can give very close observed transition) it might \
6181: be very useful to look not only at linear confidence intervals estimated from the \
6182: variances but at the covariance matrix. And instead of looking at the estimated coefficients \
6183: (parameters) of the logistic regression, it might be more meaningful to visualize the \
6184: covariance matrix of the one-step probabilities. \
6185: See page 'Matrix of variance-covariance of one-step probabilities' below. \n", rfileres,rfileres);
1.126 brouard 6186:
1.222 brouard 6187: fprintf(fichtm," - Standard deviation of one-step probabilities: <a href=\"%s\">%s</a> <br>\n",
6188: subdirf2(fileresu,"PROB_"),subdirf2(fileresu,"PROB_"));
6189: fprintf(fichtm,"\
1.126 brouard 6190: - Variance-covariance of one-step probabilities: <a href=\"%s\">%s</a> <br>\n",
1.222 brouard 6191: subdirf2(fileresu,"PROBCOV_"),subdirf2(fileresu,"PROBCOV_"));
1.126 brouard 6192:
1.222 brouard 6193: fprintf(fichtm,"\
1.126 brouard 6194: - Correlation matrix of one-step probabilities: <a href=\"%s\">%s</a> <br>\n",
1.222 brouard 6195: subdirf2(fileresu,"PROBCOR_"),subdirf2(fileresu,"PROBCOR_"));
6196: fprintf(fichtm,"\
1.126 brouard 6197: - 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): \
6198: <a href=\"%s\">%s</a> <br>\n</li>",
1.201 brouard 6199: estepm,subdirf2(fileresu,"CVE_"),subdirf2(fileresu,"CVE_"));
1.222 brouard 6200: fprintf(fichtm,"\
1.126 brouard 6201: - (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): \
6202: <a href=\"%s\">%s</a> <br>\n</li>",
1.201 brouard 6203: estepm,subdirf2(fileresu,"STDE_"),subdirf2(fileresu,"STDE_"));
1.222 brouard 6204: fprintf(fichtm,"\
1.128 brouard 6205: - Variances and covariances of health expectancies by age. Status (i) based health expectancies (in state j), e<sup>ij</sup> are weighted by the period prevalences in each state i (if popbased=1, an additional computation is done using the cross-sectional prevalences, i.e population based) (estepm=%d months): <a href=\"%s\">%s</a><br>\n",
1.222 brouard 6206: estepm, subdirf2(fileresu,"V_"),subdirf2(fileresu,"V_"));
6207: fprintf(fichtm,"\
1.128 brouard 6208: - Total life expectancy and total health expectancies to be spent in each health state e<sup>.j</sup> with their standard errors (if popbased=1, an additional computation is done using the cross-sectional prevalences, i.e population based) (estepm=%d months): <a href=\"%s\">%s</a> <br>\n",
1.222 brouard 6209: estepm, subdirf2(fileresu,"T_"),subdirf2(fileresu,"T_"));
6210: fprintf(fichtm,"\
1.126 brouard 6211: - Standard deviation of period (stable) prevalences: <a href=\"%s\">%s</a> <br>\n",\
1.222 brouard 6212: subdirf2(fileresu,"VPL_"),subdirf2(fileresu,"VPL_"));
1.126 brouard 6213:
6214: /* if(popforecast==1) fprintf(fichtm,"\n */
6215: /* - Prevalences forecasting: <a href=\"f%s\">f%s</a> <br>\n */
6216: /* - Population forecasting (if popforecast=1): <a href=\"pop%s\">pop%s</a> <br>\n */
6217: /* <br>",fileres,fileres,fileres,fileres); */
6218: /* else */
6219: /* fprintf(fichtm,"\n No population forecast: popforecast = %d (instead of 1) or stepm = %d (instead of 1) or model=%s (instead of .)<br><br></li>\n",popforecast, stepm, model); */
1.222 brouard 6220: fflush(fichtm);
6221: fprintf(fichtm," <ul><li><b>Graphs</b></li><p>");
1.126 brouard 6222:
1.225 brouard 6223: m=pow(2,cptcoveff);
1.222 brouard 6224: if (cptcovn < 1) {m=1;ncodemax[1]=1;}
1.126 brouard 6225:
1.222 brouard 6226: jj1=0;
1.237 brouard 6227:
6228: for(nres=1; nres <= nresult; nres++) /* For each resultline */
1.222 brouard 6229: for(k1=1; k1<=m;k1++){
1.237 brouard 6230: if(TKresult[nres]!= k1)
6231: continue;
1.222 brouard 6232: /* for(i1=1; i1<=ncodemax[k1];i1++){ */
6233: jj1++;
1.126 brouard 6234: if (cptcovn > 0) {
6235: fprintf(fichtm,"<hr size=\"2\" color=\"#EC5E5E\">************ Results for covariates");
1.225 brouard 6236: for (cpt=1; cpt<=cptcoveff;cpt++) /**< cptcoveff number of variables */
1.237 brouard 6237: fprintf(fichtm," V%d=%d ",Tvresult[nres][cpt],Tresult[nres][cpt]);
6238: /* fprintf(fichtm," V%d=%d ",Tvaraff[cpt],nbcode[Tvaraff[cpt]][codtabm(jj1,cpt)]); */
6239: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
6240: fprintf(fichtm," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
6241: }
6242:
1.126 brouard 6243: fprintf(fichtm," ************\n<hr size=\"2\" color=\"#EC5E5E\">");
1.220 brouard 6244:
1.222 brouard 6245: if(invalidvarcomb[k1]){
6246: fprintf(fichtm,"\n<h4>Combination (%d) ignored because no cases </h4>\n",k1);
6247: continue;
6248: }
1.126 brouard 6249: }
6250: for(cpt=1; cpt<=nlstate;cpt++) {
1.218 brouard 6251: fprintf(fichtm,"\n<br>- Observed (cross-sectional) and period (incidence based) \
6252: prevalence (with 95%% confidence interval) in state (%d): <a href=\"%s_%d-%d.svg\"> %s_%d-%d.svg</a>\n <br>\
1.205 brouard 6253: <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 6254: }
6255: fprintf(fichtm,"\n<br>- Total life expectancy by age and \
1.128 brouard 6256: health expectancies in states (1) and (2). If popbased=1 the smooth (due to the model) \
6257: true period expectancies (those weighted with period prevalences are also\
6258: drawn in addition to the population based expectancies computed using\
1.218 brouard 6259: observed and cahotic prevalences: <a href=\"%s_%d.svg\">%s_%d.svg</a>\n<br>\
1.205 brouard 6260: <img src=\"%s_%d.svg\">",subdirf2(optionfilefiname,"E_"),jj1,subdirf2(optionfilefiname,"E_"),jj1,subdirf2(optionfilefiname,"E_"),jj1);
1.222 brouard 6261: /* } /\* end i1 *\/ */
6262: }/* End k1 */
6263: fprintf(fichtm,"</ul>");
6264: fflush(fichtm);
1.126 brouard 6265: }
6266:
6267: /******************* Gnuplot file **************/
1.223 brouard 6268: void printinggnuplot(char fileresu[], char optionfilefiname[], double ageminpar, double agemaxpar, double fage , int prevfcast, int backcast, char pathc[], double p[]){
1.126 brouard 6269:
6270: char dirfileres[132],optfileres[132];
1.223 brouard 6271: char gplotcondition[132];
1.237 brouard 6272: int cpt=0,k1=0,i=0,k=0,j=0,jk=0,k2=0,k3=0,k4=0,ij=0, ijp=0, l=0;
1.211 brouard 6273: int lv=0, vlv=0, kl=0;
1.130 brouard 6274: int ng=0;
1.201 brouard 6275: int vpopbased;
1.223 brouard 6276: int ioffset; /* variable offset for columns */
1.235 brouard 6277: int nres=0; /* Index of resultline */
1.219 brouard 6278:
1.126 brouard 6279: /* if((ficgp=fopen(optionfilegnuplot,"a"))==NULL) { */
6280: /* printf("Problem with file %s",optionfilegnuplot); */
6281: /* fprintf(ficlog,"Problem with file %s",optionfilegnuplot); */
6282: /* } */
6283:
6284: /*#ifdef windows */
6285: fprintf(ficgp,"cd \"%s\" \n",pathc);
1.223 brouard 6286: /*#endif */
1.225 brouard 6287: m=pow(2,cptcoveff);
1.126 brouard 6288:
1.202 brouard 6289: /* Contribution to likelihood */
6290: /* Plot the probability implied in the likelihood */
1.223 brouard 6291: fprintf(ficgp,"\n# Contributions to the Likelihood, mle >=1. For mle=4 no interpolation, pure matrix products.\n#\n");
6292: fprintf(ficgp,"\n set log y; unset log x;set xlabel \"Age\"; set ylabel \"Likelihood (-2Log(L))\";");
6293: /* fprintf(ficgp,"\nset ter svg size 640, 480"); */ /* Too big for svg */
6294: fprintf(ficgp,"\nset ter pngcairo size 640, 480");
1.204 brouard 6295: /* nice for mle=4 plot by number of matrix products.
1.202 brouard 6296: replot "rrtest1/toto.txt" u 2:($4 == 1 && $5==2 ? $9 : 1/0):5 t "p12" with point lc 1 */
6297: /* replot exp(p1+p2*x)/(1+exp(p1+p2*x)+exp(p3+p4*x)+exp(p5+p6*x)) t "p12(x)" */
1.223 brouard 6298: /* fprintf(ficgp,"\nset out \"%s.svg\";",subdirf2(optionfilefiname,"ILK_")); */
6299: fprintf(ficgp,"\nset out \"%s-dest.png\";",subdirf2(optionfilefiname,"ILK_"));
6300: 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));
6301: fprintf(ficgp,"\nset out \"%s-ori.png\";",subdirf2(optionfilefiname,"ILK_"));
6302: 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));
6303: for (i=1; i<= nlstate ; i ++) {
6304: fprintf(ficgp,"\nset out \"%s-p%dj.png\";set ylabel \"Probability for each individual/wave\";",subdirf2(optionfilefiname,"ILK_"),i);
6305: fprintf(ficgp,"unset log;\n# plot weighted, mean weight should have point size of 0.5\n plot \"%s\"",subdirf(fileresilk));
6306: 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);
6307: for (j=2; j<= nlstate+ndeath ; j ++) {
6308: 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);
6309: }
6310: fprintf(ficgp,";\nset out; unset ylabel;\n");
6311: }
6312: /* 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 */
6313: /* fprintf(ficgp,"\nset log y;plot \"%s\" u 2:(-$11):3 t \"All sample, all transitions\" with dots lc variable",subdirf(fileresilk)); */
6314: /* fprintf(ficgp,"\nreplot \"%s\" u 2:($3 <= 3 ? -$11 : 1/0):3 t \"First 3 individuals\" with line lc variable", subdirf(fileresilk)); */
6315: fprintf(ficgp,"\nset out;unset log\n");
6316: /* fprintf(ficgp,"\nset out \"%s.svg\"; replot; set out; # bug gnuplot",subdirf2(optionfilefiname,"ILK_")); */
1.202 brouard 6317:
1.126 brouard 6318: strcpy(dirfileres,optionfilefiname);
6319: strcpy(optfileres,"vpl");
1.223 brouard 6320: /* 1eme*/
1.238 brouard 6321: for (cpt=1; cpt<= nlstate ; cpt ++){ /* For each live state */
6322: for (k1=1; k1<= m ; k1 ++){ /* For each valid combination of covariate */
1.236 brouard 6323: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
1.238 brouard 6324: /* plot [100000000000000000000:-100000000000000000000] "mysbiaspar/vplrmysbiaspar.txt to check */
6325: if(TKresult[nres]!= k1)
6326: continue;
6327: /* We are interested in selected combination by the resultline */
6328: printf("\n# 1st: Period (stable) prevalence with CI: 'VPL_' files and live state =%d ", cpt);
6329: fprintf(ficgp,"\n# 1st: Period (stable) prevalence with CI: 'VPL_' files and live state =%d ", cpt);
6330: for (k=1; k<=cptcoveff; k++){ /* For each covariate k get corresponding value lv for combination k1 */
6331: lv= decodtabm(k1,k,cptcoveff); /* Should be the value of the covariate corresponding to k1 combination */
6332: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
6333: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
6334: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
6335: vlv= nbcode[Tvaraff[k]][lv]; /* vlv is the value of the covariate lv, 0 or 1 */
6336: /* For each combination of covariate k1 (V1=1, V3=0), we printed the current covariate k and its value vlv */
6337: printf(" V%d=%d ",Tvaraff[k],vlv);
6338: fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv);
6339: }
6340: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
6341: printf(" V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
6342: fprintf(ficgp," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
6343: }
6344: printf("\n#\n");
6345: fprintf(ficgp,"\n#\n");
6346: if(invalidvarcomb[k1]){
6347: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
6348: continue;
6349: }
1.235 brouard 6350:
1.238 brouard 6351: fprintf(ficgp,"\nset out \"%s_%d-%d.svg\" \n",subdirf2(optionfilefiname,"V_"),cpt,k1);
6352: fprintf(ficgp,"\n#set out \"V_%s_%d-%d.svg\" \n",optionfilefiname,cpt,k1);
6353: fprintf(ficgp,"set xlabel \"Age\" \n\
1.235 brouard 6354: set ylabel \"Probability\" \n \
6355: set ter svg size 640, 480\n \
1.201 brouard 6356: plot [%.f:%.f] \"%s\" every :::%d::%d u 1:2 \"%%lf",ageminpar,fage,subdirf2(fileresu,"VPL_"),k1-1,k1-1);
1.235 brouard 6357:
1.238 brouard 6358: for (i=1; i<= nlstate ; i ++) {
6359: if (i==cpt) fprintf(ficgp," %%lf (%%lf)");
6360: else fprintf(ficgp," %%*lf (%%*lf)");
6361: }
6362: 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);
6363: for (i=1; i<= nlstate ; i ++) {
6364: if (i==cpt) fprintf(ficgp," %%lf (%%lf)");
6365: else fprintf(ficgp," %%*lf (%%*lf)");
6366: }
6367: 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);
6368: for (i=1; i<= nlstate ; i ++) {
6369: if (i==cpt) fprintf(ficgp," %%lf (%%lf)");
6370: else fprintf(ficgp," %%*lf (%%*lf)");
6371: }
6372: 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));
6373: if(backcast==1){ /* We need to get the corresponding values of the covariates involved in this combination k1 */
6374: /* 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); */
6375: fprintf(ficgp,",\"%s\" u 1:((",subdirf2(fileresu,"PLB_")); /* Age is in 1 */
6376: if(cptcoveff ==0){
6377: fprintf(ficgp,"$%d)) t 'Backward prevalence in state %d' with line ", 2+(cpt-1), cpt );
6378: }else{
6379: kl=0;
6380: for (k=1; k<=cptcoveff; k++){ /* For each combination of covariate */
6381: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate value corresponding to k1 combination and kth covariate */
6382: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
6383: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
6384: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
6385: vlv= nbcode[Tvaraff[k]][lv];
1.223 brouard 6386: kl++;
1.238 brouard 6387: /* 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 *\/ */
6388: /*6+(cpt-1)*(nlstate+1)+1+(i-1)+(nlstate+1)*nlstate; 6+(1-1)*(2+1)+1+(1-1) +(2+1)*2=13 */
6389: /*6+1+(i-1)+(nlstate+1)*nlstate; 6+1+(1-1) +(2+1)*2=13 */
6390: /* '' 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*/
6391: if(k==cptcoveff){
6392: fprintf(ficgp,"$%d==%d && $%d==%d)? $%d : 1/0) t 'Backward prevalence in state %d' ",kl+1, Tvaraff[k],kl+1+1,nbcode[Tvaraff[k]][lv], \
6393: 4+(cpt-1), cpt ); /* 4 or 6 ?*/
6394: }else{
6395: fprintf(ficgp,"$%d==%d && $%d==%d && ",kl+1, Tvaraff[k],kl+1+1,nbcode[Tvaraff[k]][lv]);
6396: kl++;
6397: }
6398: } /* end covariate */
6399: } /* end if no covariate */
6400: } /* end if backcast */
6401: fprintf(ficgp,"\nset out \n");
6402: } /* nres */
1.201 brouard 6403: } /* k1 */
6404: } /* cpt */
1.235 brouard 6405:
6406:
1.126 brouard 6407: /*2 eme*/
1.238 brouard 6408: for (k1=1; k1<= m ; k1 ++){
6409: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
6410: if(TKresult[nres]!= k1)
6411: continue;
6412: fprintf(ficgp,"\n# 2nd: Total life expectancy with CI: 't' files ");
6413: for (k=1; k<=cptcoveff; k++){ /* For each covariate and each value */
1.225 brouard 6414: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate number corresponding to k1 combination */
1.223 brouard 6415: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
6416: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
6417: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
6418: vlv= nbcode[Tvaraff[k]][lv];
6419: fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv);
1.211 brouard 6420: }
1.237 brouard 6421: /* for(k=1; k <= ncovds; k++){ */
1.236 brouard 6422: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
1.238 brouard 6423: printf(" V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
1.236 brouard 6424: fprintf(ficgp," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
1.238 brouard 6425: }
1.211 brouard 6426: fprintf(ficgp,"\n#\n");
1.223 brouard 6427: if(invalidvarcomb[k1]){
6428: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
6429: continue;
6430: }
1.219 brouard 6431:
1.238 brouard 6432: fprintf(ficgp,"\nset out \"%s_%d.svg\" \n",subdirf2(optionfilefiname,"E_"),k1);
6433: for(vpopbased=0; vpopbased <= popbased; vpopbased++){ /* Done for vpopbased=0 and vpopbased=1 if popbased==1*/
6434: if(vpopbased==0)
6435: fprintf(ficgp,"set ylabel \"Years\" \nset ter svg size 640, 480\nplot [%.f:%.f] ",ageminpar,fage);
6436: else
6437: fprintf(ficgp,"\nreplot ");
6438: for (i=1; i<= nlstate+1 ; i ++) {
6439: k=2*i;
6440: 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);
6441: for (j=1; j<= nlstate+1 ; j ++) {
6442: if (j==i) fprintf(ficgp," %%lf (%%lf)");
6443: else fprintf(ficgp," %%*lf (%%*lf)");
6444: }
6445: if (i== 1) fprintf(ficgp,"\" t\"TLE\" w l lt %d, \\\n",i);
6446: else fprintf(ficgp,"\" t\"LE in state (%d)\" w l lt %d, \\\n",i-1,i+1);
6447: 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);
6448: for (j=1; j<= nlstate+1 ; j ++) {
6449: if (j==i) fprintf(ficgp," %%lf (%%lf)");
6450: else fprintf(ficgp," %%*lf (%%*lf)");
6451: }
6452: fprintf(ficgp,"\" t\"\" w l lt 0,");
6453: 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);
6454: for (j=1; j<= nlstate+1 ; j ++) {
6455: if (j==i) fprintf(ficgp," %%lf (%%lf)");
6456: else fprintf(ficgp," %%*lf (%%*lf)");
6457: }
6458: if (i== (nlstate+1)) fprintf(ficgp,"\" t\"\" w l lt 0");
6459: else fprintf(ficgp,"\" t\"\" w l lt 0,\\\n");
6460: } /* state */
6461: } /* vpopbased */
6462: fprintf(ficgp,"\nset out;set out \"%s_%d.svg\"; replot; set out; \n",subdirf2(optionfilefiname,"E_"),k1); /* Buggy gnuplot */
6463: } /* end nres */
6464: } /* k1 end 2 eme*/
6465:
6466:
6467: /*3eme*/
6468: for (k1=1; k1<= m ; k1 ++){
6469: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
6470: if(TKresult[nres]!= k)
6471: continue;
6472:
6473: for (cpt=1; cpt<= nlstate ; cpt ++) {
6474: fprintf(ficgp,"\n# 3d: Life expectancy with EXP_ files: combination=%d state=%d",k1, cpt);
6475: for (k=1; k<=cptcoveff; k++){ /* For each covariate and each value */
6476: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate number corresponding to k1 combination */
6477: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
6478: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
6479: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
6480: vlv= nbcode[Tvaraff[k]][lv];
6481: fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv);
6482: }
6483: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
6484: fprintf(ficgp," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
6485: }
6486: fprintf(ficgp,"\n#\n");
6487: if(invalidvarcomb[k1]){
6488: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
6489: continue;
6490: }
6491:
6492: /* k=2+nlstate*(2*cpt-2); */
6493: k=2+(nlstate+1)*(cpt-1);
6494: fprintf(ficgp,"\nset out \"%s_%d%d.svg\" \n",subdirf2(optionfilefiname,"EXP_"),cpt,k1);
6495: fprintf(ficgp,"set ter svg size 640, 480\n\
1.201 brouard 6496: 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.238 brouard 6497: /*fprintf(ficgp,",\"e%s\" every :::%d::%d u 1:($%d-2*$%d) \"\%%lf ",fileres,k1-1,k1-1,k,k+1);
6498: for (i=1; i<= nlstate*2 ; i ++) fprintf(ficgp,"\%%lf (\%%lf) ");
6499: fprintf(ficgp,"\" t \"e%d1\" w l",cpt);
6500: fprintf(ficgp,",\"e%s\" every :::%d::%d u 1:($%d+2*$%d) \"\%%lf ",fileres,k1-1,k1-1,k,k+1);
6501: for (i=1; i<= nlstate*2 ; i ++) fprintf(ficgp,"\%%lf (\%%lf) ");
6502: fprintf(ficgp,"\" t \"e%d1\" w l",cpt);
1.219 brouard 6503:
1.238 brouard 6504: */
6505: for (i=1; i< nlstate ; i ++) {
6506: 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);
6507: /* fprintf(ficgp," ,\"%s\" every :::%d::%d u 1:%d t \"e%d%d\" w l",subdirf2(fileres,"e"),k1-1,k1-1,k+2*i,cpt,i+1);*/
1.219 brouard 6508:
1.238 brouard 6509: }
6510: fprintf(ficgp," ,\"%s\" every :::%d::%d u 1:%d t \"e%d.\" w l",subdirf2(fileresu,"E_"),k1-1,k1-1,k+nlstate,cpt);
6511: }
6512: } /* end nres */
6513: } /* end kl 3eme */
1.126 brouard 6514:
1.223 brouard 6515: /* 4eme */
1.201 brouard 6516: /* Survival functions (period) from state i in state j by initial state i */
1.238 brouard 6517: for (k1=1; k1<=m; k1++){ /* For each covariate and each value */
6518: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
6519: if(TKresult[nres]!= k1)
1.223 brouard 6520: continue;
1.238 brouard 6521: for (cpt=1; cpt<=nlstate ; cpt ++) { /* For each life state cpt*/
6522: fprintf(ficgp,"\n#\n#\n# Survival functions in state j : 'LIJ_' files, cov=%d state=%d",k1, cpt);
6523: for (k=1; k<=cptcoveff; k++){ /* For each covariate and each value */
6524: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate number corresponding to k1 combination */
6525: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
6526: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
6527: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
6528: vlv= nbcode[Tvaraff[k]][lv];
6529: fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv);
6530: }
6531: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
6532: fprintf(ficgp," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
6533: }
6534: fprintf(ficgp,"\n#\n");
6535: if(invalidvarcomb[k1]){
6536: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
6537: continue;
1.223 brouard 6538: }
1.238 brouard 6539:
6540: fprintf(ficgp,"\nset out \"%s_%d-%d.svg\" \n",subdirf2(optionfilefiname,"LIJ_"),cpt,k1);
6541: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability to be alive\" \n\
6542: set ter svg size 640, 480\nunset log y\nplot [%.f:%.f] ", ageminpar, agemaxpar);
6543: k=3;
6544: for (i=1; i<= nlstate ; i ++){
6545: if(i==1){
6546: fprintf(ficgp,"\"%s\"",subdirf2(fileresu,"PIJ_"));
6547: }else{
6548: fprintf(ficgp,", '' ");
6549: }
6550: l=(nlstate+ndeath)*(i-1)+1;
6551: fprintf(ficgp," u ($1==%d ? ($3):1/0):($%d/($%d",k1,k+l+(cpt-1),k+l);
6552: for (j=2; j<= nlstate+ndeath ; j ++)
6553: fprintf(ficgp,"+$%d",k+l+j-1);
6554: fprintf(ficgp,")) t \"l(%d,%d)\" w l",i,cpt);
6555: } /* nlstate */
6556: fprintf(ficgp,"\nset out\n");
6557: } /* end cpt state*/
6558: } /* end nres */
6559: } /* end covariate k1 */
6560:
1.220 brouard 6561: /* 5eme */
1.201 brouard 6562: /* Survival functions (period) from state i in state j by final state j */
1.238 brouard 6563: for (k1=1; k1<= m ; k1++){ /* For each covariate combination if any */
6564: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
6565: if(TKresult[nres]!= k1)
1.227 brouard 6566: continue;
1.238 brouard 6567: for (cpt=1; cpt<=nlstate ; cpt ++) { /* For each inital state */
6568: 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);
6569: for (k=1; k<=cptcoveff; k++){ /* For each covariate and each value */
6570: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate number corresponding to k1 combination */
6571: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
6572: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
6573: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
6574: vlv= nbcode[Tvaraff[k]][lv];
6575: fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv);
6576: }
6577: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
6578: fprintf(ficgp," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
6579: }
6580: fprintf(ficgp,"\n#\n");
6581: if(invalidvarcomb[k1]){
6582: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
6583: continue;
6584: }
1.227 brouard 6585:
1.238 brouard 6586: fprintf(ficgp,"\nset out \"%s_%d-%d.svg\" \n",subdirf2(optionfilefiname,"LIJT_"),cpt,k1);
6587: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability to be alive\" \n\
6588: set ter svg size 640, 480\nunset log y\nplot [%.f:%.f] ", ageminpar, agemaxpar);
6589: k=3;
6590: for (j=1; j<= nlstate ; j ++){ /* Lived in state j */
6591: if(j==1)
6592: fprintf(ficgp,"\"%s\"",subdirf2(fileresu,"PIJ_"));
6593: else
6594: fprintf(ficgp,", '' ");
6595: l=(nlstate+ndeath)*(cpt-1) +j;
6596: fprintf(ficgp," u (($1==%d && (floor($2)%%5 == 0)) ? ($3):1/0):($%d",k1,k+l);
6597: /* for (i=2; i<= nlstate+ndeath ; i ++) */
6598: /* fprintf(ficgp,"+$%d",k+l+i-1); */
6599: fprintf(ficgp,") t \"l(%d,%d)\" w l",cpt,j);
6600: } /* nlstate */
6601: fprintf(ficgp,", '' ");
6602: fprintf(ficgp," u (($1==%d && (floor($2)%%5 == 0)) ? ($3):1/0):(",k1);
6603: for (j=1; j<= nlstate ; j ++){ /* Lived in state j */
6604: l=(nlstate+ndeath)*(cpt-1) +j;
6605: if(j < nlstate)
6606: fprintf(ficgp,"$%d +",k+l);
6607: else
6608: fprintf(ficgp,"$%d) t\"l(%d,.)\" w l",k+l,cpt);
6609: }
6610: fprintf(ficgp,"\nset out\n");
6611: } /* end cpt state*/
6612: } /* end covariate */
6613: } /* end nres */
1.227 brouard 6614:
1.220 brouard 6615: /* 6eme */
1.202 brouard 6616: /* CV preval stable (period) for each covariate */
1.237 brouard 6617: for (k1=1; k1<= m ; k1 ++) /* For each covariate combination if any */
6618: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
6619: if(TKresult[nres]!= k1)
6620: continue;
1.153 brouard 6621: for (cpt=1; cpt<=nlstate ; cpt ++) { /* For each life state */
1.227 brouard 6622:
1.211 brouard 6623: fprintf(ficgp,"\n#\n#\n#CV preval stable (period): 'pij' files, covariatecombination#=%d state=%d",k1, cpt);
1.225 brouard 6624: for (k=1; k<=cptcoveff; k++){ /* For each covariate and each value */
1.227 brouard 6625: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate number corresponding to k1 combination */
6626: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
6627: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
6628: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
6629: vlv= nbcode[Tvaraff[k]][lv];
6630: fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv);
1.211 brouard 6631: }
1.237 brouard 6632: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
6633: fprintf(ficgp," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
6634: }
1.211 brouard 6635: fprintf(ficgp,"\n#\n");
1.223 brouard 6636: if(invalidvarcomb[k1]){
1.227 brouard 6637: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
6638: continue;
1.223 brouard 6639: }
1.227 brouard 6640:
1.201 brouard 6641: fprintf(ficgp,"\nset out \"%s_%d-%d.svg\" \n",subdirf2(optionfilefiname,"P_"),cpt,k1);
1.126 brouard 6642: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability\" \n\
1.238 brouard 6643: set ter svg size 640, 480\nunset log y\nplot [%.f:%.f] ", ageminpar, agemaxpar);
1.211 brouard 6644: k=3; /* Offset */
1.153 brouard 6645: for (i=1; i<= nlstate ; i ++){
1.227 brouard 6646: if(i==1)
6647: fprintf(ficgp,"\"%s\"",subdirf2(fileresu,"PIJ_"));
6648: else
6649: fprintf(ficgp,", '' ");
6650: l=(nlstate+ndeath)*(i-1)+1;
6651: fprintf(ficgp," u ($1==%d ? ($3):1/0):($%d/($%d",k1,k+l+(cpt-1),k+l);
6652: for (j=2; j<= nlstate ; j ++)
6653: fprintf(ficgp,"+$%d",k+l+j-1);
6654: fprintf(ficgp,")) t \"prev(%d,%d)\" w l",i,cpt);
1.153 brouard 6655: } /* nlstate */
1.201 brouard 6656: fprintf(ficgp,"\nset out\n");
1.153 brouard 6657: } /* end cpt state*/
6658: } /* end covariate */
1.227 brouard 6659:
6660:
1.220 brouard 6661: /* 7eme */
1.218 brouard 6662: if(backcast == 1){
1.217 brouard 6663: /* CV back preval stable (period) for each covariate */
1.237 brouard 6664: for (k1=1; k1<= m ; k1 ++) /* For each covariate combination if any */
6665: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
6666: if(TKresult[nres]!= k1)
6667: continue;
1.218 brouard 6668: for (cpt=1; cpt<=nlstate ; cpt ++) { /* For each life state */
1.227 brouard 6669: fprintf(ficgp,"\n#\n#\n#CV Back preval stable (period): 'pij' files, covariatecombination#=%d state=%d",k1, cpt);
6670: for (k=1; k<=cptcoveff; k++){ /* For each covariate and each value */
6671: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate number corresponding to k1 combination */
6672: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
6673: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
1.223 brouard 6674: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
1.227 brouard 6675: vlv= nbcode[Tvaraff[k]][lv];
6676: fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv);
6677: }
1.237 brouard 6678: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
6679: fprintf(ficgp," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
6680: }
1.227 brouard 6681: fprintf(ficgp,"\n#\n");
6682: if(invalidvarcomb[k1]){
6683: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
6684: continue;
6685: }
6686:
6687: fprintf(ficgp,"\nset out \"%s_%d-%d.svg\" \n",subdirf2(optionfilefiname,"PB_"),cpt,k1);
6688: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability\" \n\
1.238 brouard 6689: set ter svg size 640, 480\nunset log y\nplot [%.f:%.f] ", ageminpar, agemaxpar);
1.227 brouard 6690: k=3; /* Offset */
6691: for (i=1; i<= nlstate ; i ++){
6692: if(i==1)
6693: fprintf(ficgp,"\"%s\"",subdirf2(fileresu,"PIJB_"));
6694: else
6695: fprintf(ficgp,", '' ");
6696: /* l=(nlstate+ndeath)*(i-1)+1; */
6697: l=(nlstate+ndeath)*(cpt-1)+1;
6698: /* fprintf(ficgp," u ($1==%d ? ($3):1/0):($%d/($%d",k1,k+l+(cpt-1),k+l); /\* a vérifier *\/ */
6699: /* fprintf(ficgp," u ($1==%d ? ($3):1/0):($%d/($%d",k1,k+l+(cpt-1),k+l+(cpt-1)+i-1); /\* a vérifier *\/ */
6700: fprintf(ficgp," u ($1==%d ? ($3):1/0):($%d",k1,k+l+(cpt-1)+i-1); /* a vérifier */
6701: /* for (j=2; j<= nlstate ; j ++) */
6702: /* fprintf(ficgp,"+$%d",k+l+j-1); */
6703: /* /\* fprintf(ficgp,"+$%d",k+l+j-1); *\/ */
6704: fprintf(ficgp,") t \"bprev(%d,%d)\" w l",i,cpt);
6705: } /* nlstate */
6706: fprintf(ficgp,"\nset out\n");
1.218 brouard 6707: } /* end cpt state*/
6708: } /* end covariate */
6709: } /* End if backcast */
6710:
1.223 brouard 6711: /* 8eme */
1.218 brouard 6712: if(prevfcast==1){
6713: /* Projection from cross-sectional to stable (period) for each covariate */
6714:
1.237 brouard 6715: for (k1=1; k1<= m ; k1 ++) /* For each covariate combination if any */
6716: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
6717: if(TKresult[nres]!= k1)
6718: continue;
1.211 brouard 6719: for (cpt=1; cpt<=nlstate ; cpt ++) { /* For each life state */
1.227 brouard 6720: fprintf(ficgp,"\n#\n#\n#Projection of prevalence to stable (period): 'PROJ_' files, covariatecombination#=%d state=%d",k1, cpt);
6721: for (k=1; k<=cptcoveff; k++){ /* For each correspondig covariate value */
6722: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate value corresponding to k1 combination and kth covariate */
6723: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
6724: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
6725: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
6726: vlv= nbcode[Tvaraff[k]][lv];
6727: fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv);
6728: }
1.237 brouard 6729: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
6730: fprintf(ficgp," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
6731: }
1.227 brouard 6732: fprintf(ficgp,"\n#\n");
6733: if(invalidvarcomb[k1]){
6734: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
6735: continue;
6736: }
6737:
6738: fprintf(ficgp,"# hpijx=probability over h years, hp.jx is weighted by observed prev\n ");
6739: fprintf(ficgp,"\nset out \"%s_%d-%d.svg\" \n",subdirf2(optionfilefiname,"PROJ_"),cpt,k1);
6740: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Prevalence\" \n\
1.238 brouard 6741: set ter svg size 640, 480\nunset log y\nplot [%.f:%.f] ", ageminpar, agemaxpar);
1.227 brouard 6742: for (i=1; i<= nlstate+1 ; i ++){ /* nlstate +1 p11 p21 p.1 */
6743: /*# V1 = 1 V2 = 0 yearproj age p11 p21 p.1 p12 p22 p.2 p13 p23 p.3*/
6744: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
6745: /*# yearproj age p11 p21 p.1 p12 p22 p.2 p13 p23 p.3*/
6746: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
6747: if(i==1){
6748: fprintf(ficgp,"\"%s\"",subdirf2(fileresu,"F_"));
6749: }else{
6750: fprintf(ficgp,",\\\n '' ");
6751: }
6752: if(cptcoveff ==0){ /* No covariate */
6753: ioffset=2; /* Age is in 2 */
6754: /*# yearproj age p11 p21 p31 p.1 p12 p22 p32 p.2 p13 p23 p33 p.3 p14 p24 p34 p.4*/
6755: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 */
6756: /*# V1 = 1 yearproj age p11 p21 p31 p.1 p12 p22 p32 p.2 p13 p23 p33 p.3 p14 p24 p34 p.4*/
6757: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 */
6758: fprintf(ficgp," u %d:(", ioffset);
6759: if(i==nlstate+1)
6760: fprintf(ficgp," $%d/(1.-$%d)) t 'pw.%d' with line ", \
6761: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset+1+(i-1)+(nlstate+1)*nlstate,cpt );
6762: else
6763: fprintf(ficgp," $%d/(1.-$%d)) t 'p%d%d' with line ", \
6764: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset+1+(i-1)+(nlstate+1)*nlstate,i,cpt );
6765: }else{ /* more than 2 covariates */
6766: if(cptcoveff ==1){
6767: ioffset=4; /* Age is in 4 */
6768: }else{
6769: ioffset=6; /* Age is in 6 */
6770: /*# V1 = 1 V2 = 0 yearproj age p11 p21 p.1 p12 p22 p.2 p13 p23 p.3*/
6771: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
6772: }
6773: fprintf(ficgp," u %d:(",ioffset);
6774: kl=0;
6775: strcpy(gplotcondition,"(");
6776: for (k=1; k<=cptcoveff; k++){ /* For each covariate writing the chain of conditions */
6777: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate value corresponding to combination k1 and covariate k */
6778: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
6779: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
6780: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
6781: vlv= nbcode[Tvaraff[k]][lv]; /* Value of the modality of Tvaraff[k] */
6782: kl++;
6783: sprintf(gplotcondition+strlen(gplotcondition),"$%d==%d && $%d==%d " ,kl,Tvaraff[k], kl+1, nbcode[Tvaraff[k]][lv]);
6784: kl++;
6785: if(k <cptcoveff && cptcoveff>1)
6786: sprintf(gplotcondition+strlen(gplotcondition)," && ");
6787: }
6788: strcpy(gplotcondition+strlen(gplotcondition),")");
6789: /* 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 *\/ */
6790: /*6+(cpt-1)*(nlstate+1)+1+(i-1)+(nlstate+1)*nlstate; 6+(1-1)*(2+1)+1+(1-1) +(2+1)*2=13 */
6791: /*6+1+(i-1)+(nlstate+1)*nlstate; 6+1+(1-1) +(2+1)*2=13 */
6792: /* '' 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*/
6793: if(i==nlstate+1){
6794: fprintf(ficgp,"%s ? $%d/(1.-$%d) : 1/0) t 'p.%d' with line ", gplotcondition, \
6795: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset+1+(i-1)+(nlstate+1)*nlstate,cpt );
6796: }else{
6797: fprintf(ficgp,"%s ? $%d/(1.-$%d) : 1/0) t 'p%d%d' with line ", gplotcondition, \
6798: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset +1+(i-1)+(nlstate+1)*nlstate,i,cpt );
6799: }
6800: } /* end if covariate */
6801: } /* nlstate */
6802: fprintf(ficgp,"\nset out\n");
1.223 brouard 6803: } /* end cpt state*/
6804: } /* end covariate */
6805: } /* End if prevfcast */
1.227 brouard 6806:
6807:
1.238 brouard 6808: /* 9eme writing MLE parameters */
6809: fprintf(ficgp,"\n##############\n#9eme MLE estimated parameters\n#############\n");
1.126 brouard 6810: for(i=1,jk=1; i <=nlstate; i++){
1.187 brouard 6811: fprintf(ficgp,"# initial state %d\n",i);
1.126 brouard 6812: for(k=1; k <=(nlstate+ndeath); k++){
6813: if (k != i) {
1.227 brouard 6814: fprintf(ficgp,"# current state %d\n",k);
6815: for(j=1; j <=ncovmodel; j++){
6816: fprintf(ficgp,"p%d=%f; ",jk,p[jk]);
6817: jk++;
6818: }
6819: fprintf(ficgp,"\n");
1.126 brouard 6820: }
6821: }
1.223 brouard 6822: }
1.187 brouard 6823: fprintf(ficgp,"##############\n#\n");
1.227 brouard 6824:
1.145 brouard 6825: /*goto avoid;*/
1.238 brouard 6826: /* 10eme Graphics of probabilities or incidences using written MLE parameters */
6827: fprintf(ficgp,"\n##############\n#10eme Graphics of probabilities or incidences\n#############\n");
1.187 brouard 6828: fprintf(ficgp,"# logi(p12/p11)=a12+b12*age+c12age*age+d12*V1+e12*V1*age\n");
6829: fprintf(ficgp,"# logi(p12/p11)=p1 +p2*age +p3*age*age+ p4*V1+ p5*V1*age\n");
6830: fprintf(ficgp,"# logi(p13/p11)=a13+b13*age+c13age*age+d13*V1+e13*V1*age\n");
6831: fprintf(ficgp,"# logi(p13/p11)=p6 +p7*age +p8*age*age+ p9*V1+ p10*V1*age\n");
6832: fprintf(ficgp,"# p12+p13+p14+p11=1=p11(1+exp(a12+b12*age+c12age*age+d12*V1+e12*V1*age)\n");
6833: fprintf(ficgp,"# +exp(a13+b13*age+c13age*age+d13*V1+e13*V1*age)+...)\n");
6834: fprintf(ficgp,"# p11=1/(1+exp(a12+b12*age+c12age*age+d12*V1+e12*V1*age)\n");
6835: fprintf(ficgp,"# +exp(a13+b13*age+c13age*age+d13*V1+e13*V1*age)+...)\n");
6836: fprintf(ficgp,"# p12=exp(a12+b12*age+c12age*age+d12*V1+e12*V1*age)/\n");
6837: fprintf(ficgp,"# (1+exp(a12+b12*age+c12age*age+d12*V1+e12*V1*age)\n");
6838: fprintf(ficgp,"# +exp(a13+b13*age+c13age*age+d13*V1+e13*V1*age))\n");
6839: fprintf(ficgp,"# +exp(a14+b14*age+c14age*age+d14*V1+e14*V1*age)+...)\n");
6840: fprintf(ficgp,"#\n");
1.223 brouard 6841: for(ng=1; ng<=3;ng++){ /* Number of graphics: first is logit, 2nd is probabilities, third is incidences per year*/
1.238 brouard 6842: fprintf(ficgp,"#Number of graphics: first is logit, 2nd is probabilities, third is incidences per year\n");
1.237 brouard 6843: fprintf(ficgp,"#model=%s \n",model);
1.238 brouard 6844: fprintf(ficgp,"# Type of graphic ng=%d\n",ng);
1.237 brouard 6845: fprintf(ficgp,"# jk=1 to 2^%d=%d\n",cptcoveff,m);/* to be checked */
6846: for(jk=1; jk <=m; jk++) /* For each combination of covariate */
6847: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
6848: if(TKresult[nres]!= jk)
6849: continue;
6850: fprintf(ficgp,"# Combination of dummy jk=%d and ",jk);
6851: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
6852: fprintf(ficgp," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
6853: }
6854: fprintf(ficgp,"\n#\n");
1.223 brouard 6855: fprintf(ficgp,"\nset out \"%s_%d-%d.svg\" ",subdirf2(optionfilefiname,"PE_"),jk,ng);
6856: fprintf(ficgp,"\nset ter svg size 640, 480 ");
6857: if (ng==1){
6858: fprintf(ficgp,"\nset ylabel \"Value of the logit of the model\"\n"); /* exp(a12+b12*x) could be nice */
6859: fprintf(ficgp,"\nunset log y");
6860: }else if (ng==2){
6861: fprintf(ficgp,"\nset ylabel \"Probability\"\n");
6862: fprintf(ficgp,"\nset log y");
6863: }else if (ng==3){
6864: fprintf(ficgp,"\nset ylabel \"Quasi-incidence per year\"\n");
6865: fprintf(ficgp,"\nset log y");
6866: }else
6867: fprintf(ficgp,"\nunset title ");
6868: fprintf(ficgp,"\nplot [%.f:%.f] ",ageminpar,agemaxpar);
6869: i=1;
6870: for(k2=1; k2<=nlstate; k2++) {
6871: k3=i;
6872: for(k=1; k<=(nlstate+ndeath); k++) {
6873: if (k != k2){
6874: switch( ng) {
6875: case 1:
6876: if(nagesqr==0)
6877: fprintf(ficgp," p%d+p%d*x",i,i+1);
6878: else /* nagesqr =1 */
6879: fprintf(ficgp," p%d+p%d*x+p%d*x*x",i,i+1,i+1+nagesqr);
6880: break;
6881: case 2: /* ng=2 */
6882: if(nagesqr==0)
6883: fprintf(ficgp," exp(p%d+p%d*x",i,i+1);
6884: else /* nagesqr =1 */
6885: fprintf(ficgp," exp(p%d+p%d*x+p%d*x*x",i,i+1,i+1+nagesqr);
6886: break;
6887: case 3:
6888: if(nagesqr==0)
6889: fprintf(ficgp," %f*exp(p%d+p%d*x",YEARM/stepm,i,i+1);
6890: else /* nagesqr =1 */
6891: fprintf(ficgp," %f*exp(p%d+p%d*x+p%d*x*x",YEARM/stepm,i,i+1,i+1+nagesqr);
6892: break;
6893: }
6894: ij=1;/* To be checked else nbcode[0][0] wrong */
1.237 brouard 6895: ijp=1; /* product no age */
6896: /* for(j=3; j <=ncovmodel-nagesqr; j++) { */
6897: for(j=1; j <=cptcovt; j++) { /* For each covariate of the simplified model */
1.223 brouard 6898: /* printf("Tage[%d]=%d, j=%d\n", ij, Tage[ij], j); */
1.237 brouard 6899: if(j==Tage[ij]) { /* Product by age */
6900: if(ij <=cptcovage) { /* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1, 2 V5 and V1 */
1.238 brouard 6901: if(DummyV[j]==0){
1.237 brouard 6902: fprintf(ficgp,"+p%d*%d*x",i+j+2+nagesqr-1,Tinvresult[nres][Tvar[j]]);;
6903: }else{ /* quantitative */
6904: fprintf(ficgp,"+p%d*%f*x",i+j+2+nagesqr-1,Tqinvresult[nres][Tvar[j]]); /* Tqinvresult in decoderesult */
6905: /* fprintf(ficgp,"+p%d*%d*x",i+j+nagesqr-1,nbcode[Tvar[j-2]][codtabm(jk,Tvar[j-2])]); */
6906: }
6907: ij++;
6908: }
6909: }else if(j==Tprod[ijp]) { /* */
6910: /* printf("Tprod[%d]=%d, j=%d\n", ij, Tprod[ijp], j); */
6911: if(ijp <=cptcovprod) { /* Product */
1.238 brouard 6912: if(DummyV[Tvard[ijp][1]]==0){/* Vn is dummy */
6913: if(DummyV[Tvard[ijp][2]]==0){/* Vn and Vm are dummy */
1.237 brouard 6914: /* fprintf(ficgp,"+p%d*%d*%d",i+j+2+nagesqr-1,nbcode[Tvard[ijp][1]][codtabm(jk,j)],nbcode[Tvard[ijp][2]][codtabm(jk,j)]); */
6915: fprintf(ficgp,"+p%d*%d*%d",i+j+2+nagesqr-1,Tinvresult[nres][Tvard[ijp][1]],Tinvresult[nres][Tvard[ijp][2]]);
6916: }else{ /* Vn is dummy and Vm is quanti */
6917: /* fprintf(ficgp,"+p%d*%d*%f",i+j+2+nagesqr-1,nbcode[Tvard[ijp][1]][codtabm(jk,j)],Tqinvresult[nres][Tvard[ijp][2]]); */
6918: fprintf(ficgp,"+p%d*%d*%f",i+j+2+nagesqr-1,Tinvresult[nres][Tvard[ijp][1]],Tqinvresult[nres][Tvard[ijp][2]]);
6919: }
6920: }else{ /* Vn*Vm Vn is quanti */
1.238 brouard 6921: if(DummyV[Tvard[ijp][2]]==0){
1.237 brouard 6922: fprintf(ficgp,"+p%d*%d*%f",i+j+2+nagesqr-1,Tinvresult[nres][Tvard[ijp][2]],Tqinvresult[nres][Tvard[ijp][1]]);
6923: }else{ /* Both quanti */
6924: fprintf(ficgp,"+p%d*%f*%f",i+j+2+nagesqr-1,Tqinvresult[nres][Tvard[ijp][1]],Tqinvresult[nres][Tvard[ijp][2]]);
6925: }
6926: }
1.238 brouard 6927: ijp++;
1.237 brouard 6928: }
6929: } else{ /* simple covariate */
6930: /* fprintf(ficgp,"+p%d*%d",i+j+2+nagesqr-1,nbcode[Tvar[j]][codtabm(jk,j)]); /\* Valgrind bug nbcode *\/ */
6931: if(Dummy[j]==0){
6932: fprintf(ficgp,"+p%d*%d",i+j+2+nagesqr-1,Tinvresult[nres][Tvar[j]]); /* */
6933: }else{ /* quantitative */
6934: fprintf(ficgp,"+p%d*%f",i+j+2+nagesqr-1,Tqinvresult[nres][Tvar[j]]); /* */
1.223 brouard 6935: /* fprintf(ficgp,"+p%d*%d*x",i+j+nagesqr-1,nbcode[Tvar[j-2]][codtabm(jk,Tvar[j-2])]); */
6936: }
1.237 brouard 6937: } /* end simple */
6938: } /* end j */
1.223 brouard 6939: }else{
6940: i=i-ncovmodel;
6941: if(ng !=1 ) /* For logit formula of log p11 is more difficult to get */
6942: fprintf(ficgp," (1.");
6943: }
1.227 brouard 6944:
1.223 brouard 6945: if(ng != 1){
6946: fprintf(ficgp,")/(1");
1.227 brouard 6947:
1.223 brouard 6948: for(k1=1; k1 <=nlstate; k1++){
6949: if(nagesqr==0)
6950: fprintf(ficgp,"+exp(p%d+p%d*x",k3+(k1-1)*ncovmodel,k3+(k1-1)*ncovmodel+1);
6951: else /* nagesqr =1 */
6952: fprintf(ficgp,"+exp(p%d+p%d*x+p%d*x*x",k3+(k1-1)*ncovmodel,k3+(k1-1)*ncovmodel+1,k3+(k1-1)*ncovmodel+1+nagesqr);
1.217 brouard 6953:
1.223 brouard 6954: ij=1;
6955: for(j=3; j <=ncovmodel-nagesqr; j++){
1.237 brouard 6956: if((j-2)==Tage[ij]) { /* Bug valgrind */
6957: if(ij <=cptcovage) { /* Bug valgrind */
1.223 brouard 6958: fprintf(ficgp,"+p%d*%d*x",k3+(k1-1)*ncovmodel+1+j-2+nagesqr,nbcode[Tvar[j-2]][codtabm(jk,j-2)]);
6959: /* fprintf(ficgp,"+p%d*%d*x",k3+(k1-1)*ncovmodel+1+j-2+nagesqr,nbcode[Tvar[j-2]][codtabm(jk,Tvar[j-2])]); */
6960: ij++;
6961: }
6962: }
6963: else
1.225 brouard 6964: fprintf(ficgp,"+p%d*%d",k3+(k1-1)*ncovmodel+1+j-2+nagesqr,nbcode[Tvar[j-2]][codtabm(jk,j-2)]);/* Valgrind bug nbcode */
1.223 brouard 6965: }
6966: fprintf(ficgp,")");
6967: }
6968: fprintf(ficgp,")");
6969: if(ng ==2)
6970: fprintf(ficgp," t \"p%d%d\" ", k2,k);
6971: else /* ng= 3 */
6972: fprintf(ficgp," t \"i%d%d\" ", k2,k);
6973: }else{ /* end ng <> 1 */
6974: if( k !=k2) /* logit p11 is hard to draw */
6975: fprintf(ficgp," t \"logit(p%d%d)\" ", k2,k);
6976: }
6977: if ((k+k2)!= (nlstate*2+ndeath) && ng != 1)
6978: fprintf(ficgp,",");
6979: if (ng == 1 && k!=k2 && (k+k2)!= (nlstate*2+ndeath))
6980: fprintf(ficgp,",");
6981: i=i+ncovmodel;
6982: } /* end k */
6983: } /* end k2 */
6984: fprintf(ficgp,"\n set out\n");
6985: } /* end jk */
6986: } /* end ng */
6987: /* avoid: */
6988: fflush(ficgp);
1.126 brouard 6989: } /* end gnuplot */
6990:
6991:
6992: /*************** Moving average **************/
1.219 brouard 6993: /* int movingaverage(double ***probs, double bage, double fage, double ***mobaverage, int mobilav, double bageout, double fageout){ */
1.222 brouard 6994: int movingaverage(double ***probs, double bage, double fage, double ***mobaverage, int mobilav){
1.218 brouard 6995:
1.222 brouard 6996: int i, cpt, cptcod;
6997: int modcovmax =1;
6998: int mobilavrange, mob;
6999: int iage=0;
7000:
7001: double sum=0.;
7002: double age;
7003: double *sumnewp, *sumnewm;
7004: double *agemingood, *agemaxgood; /* Currently identical for all covariates */
7005:
7006:
1.225 brouard 7007: /* modcovmax=2*cptcoveff;/\* Max number of modalities. We suppose */
1.222 brouard 7008: /* a covariate has 2 modalities, should be equal to ncovcombmax *\/ */
7009:
7010: sumnewp = vector(1,ncovcombmax);
7011: sumnewm = vector(1,ncovcombmax);
7012: agemingood = vector(1,ncovcombmax);
7013: agemaxgood = vector(1,ncovcombmax);
7014:
7015: for (cptcod=1;cptcod<=ncovcombmax;cptcod++){
7016: sumnewm[cptcod]=0.;
7017: sumnewp[cptcod]=0.;
7018: agemingood[cptcod]=0;
7019: agemaxgood[cptcod]=0;
7020: }
7021: if (cptcovn<1) ncovcombmax=1; /* At least 1 pass */
7022:
7023: if(mobilav==1||mobilav ==3 ||mobilav==5 ||mobilav== 7){
7024: if(mobilav==1) mobilavrange=5; /* default */
7025: else mobilavrange=mobilav;
7026: for (age=bage; age<=fage; age++)
7027: for (i=1; i<=nlstate;i++)
7028: for (cptcod=1;cptcod<=ncovcombmax;cptcod++)
7029: mobaverage[(int)age][i][cptcod]=probs[(int)age][i][cptcod];
7030: /* We keep the original values on the extreme ages bage, fage and for
7031: fage+1 and bage-1 we use a 3 terms moving average; for fage+2 bage+2
7032: we use a 5 terms etc. until the borders are no more concerned.
7033: */
7034: for (mob=3;mob <=mobilavrange;mob=mob+2){
7035: for (age=bage+(mob-1)/2; age<=fage-(mob-1)/2; age++){
7036: for (i=1; i<=nlstate;i++){
7037: for (cptcod=1;cptcod<=ncovcombmax;cptcod++){
7038: mobaverage[(int)age][i][cptcod] =probs[(int)age][i][cptcod];
7039: for (cpt=1;cpt<=(mob-1)/2;cpt++){
7040: mobaverage[(int)age][i][cptcod] +=probs[(int)age-cpt][i][cptcod];
7041: mobaverage[(int)age][i][cptcod] +=probs[(int)age+cpt][i][cptcod];
7042: }
7043: mobaverage[(int)age][i][cptcod]=mobaverage[(int)age][i][cptcod]/mob;
7044: }
7045: }
7046: }/* end age */
7047: }/* end mob */
7048: }else
7049: return -1;
7050: for (cptcod=1;cptcod<=ncovcombmax;cptcod++){
7051: /* for (age=bage+(mob-1)/2; age<=fage-(mob-1)/2; age++){ */
7052: if(invalidvarcomb[cptcod]){
7053: printf("\nCombination (%d) ignored because no cases \n",cptcod);
7054: continue;
7055: }
1.219 brouard 7056:
1.222 brouard 7057: agemingood[cptcod]=fage-(mob-1)/2;
7058: for (age=fage-(mob-1)/2; age>=bage; age--){/* From oldest to youngest, finding the youngest wrong */
7059: sumnewm[cptcod]=0.;
7060: for (i=1; i<=nlstate;i++){
7061: sumnewm[cptcod]+=mobaverage[(int)age][i][cptcod];
7062: }
7063: if(fabs(sumnewm[cptcod] - 1.) <= 1.e-3) { /* good */
7064: agemingood[cptcod]=age;
7065: }else{ /* bad */
7066: for (i=1; i<=nlstate;i++){
7067: mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemingood[cptcod]][i][cptcod];
7068: } /* i */
7069: } /* end bad */
7070: }/* age */
7071: sum=0.;
7072: for (i=1; i<=nlstate;i++){
7073: sum+=mobaverage[(int)agemingood[cptcod]][i][cptcod];
7074: }
7075: if(fabs(sum - 1.) > 1.e-3) { /* bad */
7076: printf("For this combination of covariate cptcod=%d, we can't get a smoothed prevalence which sums to one at any descending age!\n",cptcod);
7077: /* for (i=1; i<=nlstate;i++){ */
7078: /* mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemingood[cptcod]][i][cptcod]; */
7079: /* } /\* i *\/ */
7080: } /* end bad */
7081: /* else{ /\* We found some ages summing to one, we will smooth the oldest *\/ */
7082: /* From youngest, finding the oldest wrong */
7083: agemaxgood[cptcod]=bage+(mob-1)/2;
7084: for (age=bage+(mob-1)/2; age<=fage; age++){
7085: sumnewm[cptcod]=0.;
7086: for (i=1; i<=nlstate;i++){
7087: sumnewm[cptcod]+=mobaverage[(int)age][i][cptcod];
7088: }
7089: if(fabs(sumnewm[cptcod] - 1.) <= 1.e-3) { /* good */
7090: agemaxgood[cptcod]=age;
7091: }else{ /* bad */
7092: for (i=1; i<=nlstate;i++){
7093: mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemaxgood[cptcod]][i][cptcod];
7094: } /* i */
7095: } /* end bad */
7096: }/* age */
7097: sum=0.;
7098: for (i=1; i<=nlstate;i++){
7099: sum+=mobaverage[(int)agemaxgood[cptcod]][i][cptcod];
7100: }
7101: if(fabs(sum - 1.) > 1.e-3) { /* bad */
7102: printf("For this combination of covariate cptcod=%d, we can't get a smoothed prevalence which sums to one at any ascending age!\n",cptcod);
7103: /* for (i=1; i<=nlstate;i++){ */
7104: /* mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemingood[cptcod]][i][cptcod]; */
7105: /* } /\* i *\/ */
7106: } /* end bad */
7107:
7108: for (age=bage; age<=fage; age++){
1.235 brouard 7109: /* printf("%d %d ", cptcod, (int)age); */
1.222 brouard 7110: sumnewp[cptcod]=0.;
7111: sumnewm[cptcod]=0.;
7112: for (i=1; i<=nlstate;i++){
7113: sumnewp[cptcod]+=probs[(int)age][i][cptcod];
7114: sumnewm[cptcod]+=mobaverage[(int)age][i][cptcod];
7115: /* printf("%.4f %.4f ",probs[(int)age][i][cptcod], mobaverage[(int)age][i][cptcod]); */
7116: }
7117: /* printf("%.4f %.4f \n",sumnewp[cptcod], sumnewm[cptcod]); */
7118: }
7119: /* printf("\n"); */
7120: /* } */
7121: /* brutal averaging */
7122: for (i=1; i<=nlstate;i++){
7123: for (age=1; age<=bage; age++){
7124: mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemingood[cptcod]][i][cptcod];
7125: /* printf("age=%d i=%d cptcod=%d mobaverage=%.4f \n",(int)age,i, cptcod, mobaverage[(int)age][i][cptcod]); */
7126: }
7127: for (age=fage; age<=AGESUP; age++){
7128: mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemaxgood[cptcod]][i][cptcod];
7129: /* printf("age=%d i=%d cptcod=%d mobaverage=%.4f \n",(int)age,i, cptcod, mobaverage[(int)age][i][cptcod]); */
7130: }
7131: } /* end i status */
7132: for (i=nlstate+1; i<=nlstate+ndeath;i++){
7133: for (age=1; age<=AGESUP; age++){
7134: /*printf("i=%d, age=%d, cptcod=%d\n",i, (int)age, cptcod);*/
7135: mobaverage[(int)age][i][cptcod]=0.;
7136: }
7137: }
7138: }/* end cptcod */
7139: free_vector(sumnewm,1, ncovcombmax);
7140: free_vector(sumnewp,1, ncovcombmax);
7141: free_vector(agemaxgood,1, ncovcombmax);
7142: free_vector(agemingood,1, ncovcombmax);
7143: return 0;
7144: }/* End movingaverage */
1.218 brouard 7145:
1.126 brouard 7146:
7147: /************** Forecasting ******************/
1.235 brouard 7148: 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 7149: /* proj1, year, month, day of starting projection
7150: agemin, agemax range of age
7151: dateprev1 dateprev2 range of dates during which prevalence is computed
7152: anproj2 year of en of projection (same day and month as proj1).
7153: */
1.235 brouard 7154: int yearp, stepsize, hstepm, nhstepm, j, k, cptcod, i, h, i1, k4, nres=0;
1.126 brouard 7155: double agec; /* generic age */
7156: double agelim, ppij, yp,yp1,yp2,jprojmean,mprojmean,anprojmean;
7157: double *popeffectif,*popcount;
7158: double ***p3mat;
1.218 brouard 7159: /* double ***mobaverage; */
1.126 brouard 7160: char fileresf[FILENAMELENGTH];
7161:
7162: agelim=AGESUP;
1.211 brouard 7163: /* Compute observed prevalence between dateprev1 and dateprev2 by counting the number of people
7164: in each health status at the date of interview (if between dateprev1 and dateprev2).
7165: We still use firstpass and lastpass as another selection.
7166: */
1.214 brouard 7167: /* freqsummary(fileres, agemin, agemax, s, agev, nlstate, imx,Tvaraff,nbcode, ncodemax,mint,anint,strstart,\ */
7168: /* firstpass, lastpass, stepm, weightopt, model); */
1.126 brouard 7169:
1.201 brouard 7170: strcpy(fileresf,"F_");
7171: strcat(fileresf,fileresu);
1.126 brouard 7172: if((ficresf=fopen(fileresf,"w"))==NULL) {
7173: printf("Problem with forecast resultfile: %s\n", fileresf);
7174: fprintf(ficlog,"Problem with forecast resultfile: %s\n", fileresf);
7175: }
1.235 brouard 7176: printf("\nComputing forecasting: result on file '%s', please wait... \n", fileresf);
7177: fprintf(ficlog,"\nComputing forecasting: result on file '%s', please wait... \n", fileresf);
1.126 brouard 7178:
1.225 brouard 7179: if (cptcoveff==0) ncodemax[cptcoveff]=1;
1.126 brouard 7180:
7181:
7182: stepsize=(int) (stepm+YEARM-1)/YEARM;
7183: if (stepm<=12) stepsize=1;
7184: if(estepm < stepm){
7185: printf ("Problem %d lower than %d\n",estepm, stepm);
7186: }
7187: else hstepm=estepm;
7188:
7189: hstepm=hstepm/stepm;
7190: yp1=modf(dateintmean,&yp);/* extracts integral of datemean in yp and
7191: fractional in yp1 */
7192: anprojmean=yp;
7193: yp2=modf((yp1*12),&yp);
7194: mprojmean=yp;
7195: yp1=modf((yp2*30.5),&yp);
7196: jprojmean=yp;
7197: if(jprojmean==0) jprojmean=1;
7198: if(mprojmean==0) jprojmean=1;
7199:
1.227 brouard 7200: i1=pow(2,cptcoveff);
1.126 brouard 7201: if (cptcovn < 1){i1=1;}
7202:
7203: fprintf(ficresf,"# Mean day of interviews %.lf/%.lf/%.lf (%.2f) between %.2f and %.2f \n",jprojmean,mprojmean,anprojmean,dateintmean,dateprev1,dateprev2);
7204:
7205: fprintf(ficresf,"#****** Routine prevforecast **\n");
1.227 brouard 7206:
1.126 brouard 7207: /* if (h==(int)(YEARM*yearp)){ */
1.235 brouard 7208: for(nres=1; nres <= nresult; nres++) /* For each resultline */
7209: for(k=1; k<=i1;k++){
7210: if(TKresult[nres]!= k)
7211: continue;
1.227 brouard 7212: if(invalidvarcomb[k]){
7213: printf("\nCombination (%d) projection ignored because no cases \n",k);
7214: continue;
7215: }
7216: fprintf(ficresf,"\n#****** hpijx=probability over h years, hp.jx is weighted by observed prev \n#");
7217: for(j=1;j<=cptcoveff;j++) {
7218: fprintf(ficresf," V%d (=) %d",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
7219: }
1.235 brouard 7220: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
1.238 brouard 7221: fprintf(ficresf," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
1.235 brouard 7222: }
1.227 brouard 7223: fprintf(ficresf," yearproj age");
7224: for(j=1; j<=nlstate+ndeath;j++){
7225: for(i=1; i<=nlstate;i++)
7226: fprintf(ficresf," p%d%d",i,j);
7227: fprintf(ficresf," wp.%d",j);
7228: }
7229: for (yearp=0; yearp<=(anproj2-anproj1);yearp +=stepsize) {
7230: fprintf(ficresf,"\n");
7231: fprintf(ficresf,"\n# Forecasting at date %.lf/%.lf/%.lf ",jproj1,mproj1,anproj1+yearp);
7232: for (agec=fage; agec>=(ageminpar-1); agec--){
7233: nhstepm=(int) rint((agelim-agec)*YEARM/stepm);
7234: nhstepm = nhstepm/hstepm;
7235: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
7236: oldm=oldms;savm=savms;
1.235 brouard 7237: hpxij(p3mat,nhstepm,agec,hstepm,p,nlstate,stepm,oldm,savm, k,nres);
1.227 brouard 7238:
7239: for (h=0; h<=nhstepm; h++){
7240: if (h*hstepm/YEARM*stepm ==yearp) {
7241: fprintf(ficresf,"\n");
7242: for(j=1;j<=cptcoveff;j++)
7243: fprintf(ficresf,"%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
7244: fprintf(ficresf,"%.f %.f ",anproj1+yearp,agec+h*hstepm/YEARM*stepm);
7245: }
7246: for(j=1; j<=nlstate+ndeath;j++) {
7247: ppij=0.;
7248: for(i=1; i<=nlstate;i++) {
7249: if (mobilav==1)
7250: ppij=ppij+p3mat[i][j][h]*mobaverage[(int)agec][i][k];
7251: else {
7252: ppij=ppij+p3mat[i][j][h]*probs[(int)(agec)][i][k];
7253: }
7254: if (h*hstepm/YEARM*stepm== yearp) {
7255: fprintf(ficresf," %.3f", p3mat[i][j][h]);
7256: }
7257: } /* end i */
7258: if (h*hstepm/YEARM*stepm==yearp) {
7259: fprintf(ficresf," %.3f", ppij);
7260: }
7261: }/* end j */
7262: } /* end h */
7263: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
7264: } /* end agec */
7265: } /* end yearp */
7266: } /* end k */
1.219 brouard 7267:
1.126 brouard 7268: fclose(ficresf);
1.215 brouard 7269: printf("End of Computing forecasting \n");
7270: fprintf(ficlog,"End of Computing forecasting\n");
7271:
1.126 brouard 7272: }
7273:
1.218 brouard 7274: /* /\************** Back Forecasting ******************\/ */
1.225 brouard 7275: /* void prevbackforecast(char fileres[], double anback1, double mback1, double jback1, double ageminpar, double agemax, double dateprev1, double dateprev2, int mobilav, double bage, double fage, int firstpass, int lastpass, double anback2, double p[], int cptcoveff){ */
1.218 brouard 7276: /* /\* back1, year, month, day of starting backection */
7277: /* agemin, agemax range of age */
7278: /* dateprev1 dateprev2 range of dates during which prevalence is computed */
7279: /* anback2 year of en of backection (same day and month as back1). */
7280: /* *\/ */
7281: /* int yearp, stepsize, hstepm, nhstepm, j, k, cptcod, i, h, i1; */
7282: /* double agec; /\* generic age *\/ */
7283: /* double agelim, ppij, yp,yp1,yp2,jprojmean,mprojmean,anprojmean; */
7284: /* double *popeffectif,*popcount; */
7285: /* double ***p3mat; */
7286: /* /\* double ***mobaverage; *\/ */
7287: /* char fileresfb[FILENAMELENGTH]; */
7288:
7289: /* agelim=AGESUP; */
7290: /* /\* Compute observed prevalence between dateprev1 and dateprev2 by counting the number of people */
7291: /* in each health status at the date of interview (if between dateprev1 and dateprev2). */
7292: /* We still use firstpass and lastpass as another selection. */
7293: /* *\/ */
7294: /* /\* freqsummary(fileres, agemin, agemax, s, agev, nlstate, imx,Tvaraff,nbcode, ncodemax,mint,anint,strstart,\ *\/ */
7295: /* /\* firstpass, lastpass, stepm, weightopt, model); *\/ */
7296: /* prevalence(probs, ageminpar, agemax, s, agev, nlstate, imx, Tvar, nbcode, ncodemax, mint, anint, dateprev1, dateprev2, firstpass, lastpass); */
7297:
7298: /* strcpy(fileresfb,"FB_"); */
7299: /* strcat(fileresfb,fileresu); */
7300: /* if((ficresfb=fopen(fileresfb,"w"))==NULL) { */
7301: /* printf("Problem with back forecast resultfile: %s\n", fileresfb); */
7302: /* fprintf(ficlog,"Problem with back forecast resultfile: %s\n", fileresfb); */
7303: /* } */
7304: /* printf("Computing back forecasting: result on file '%s', please wait... \n", fileresfb); */
7305: /* fprintf(ficlog,"Computing back forecasting: result on file '%s', please wait... \n", fileresfb); */
7306:
1.225 brouard 7307: /* if (cptcoveff==0) ncodemax[cptcoveff]=1; */
1.218 brouard 7308:
7309: /* /\* if (mobilav!=0) { *\/ */
7310: /* /\* mobaverage= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX); *\/ */
7311: /* /\* if (movingaverage(probs, ageminpar, fage, mobaverage,mobilav)!=0){ *\/ */
7312: /* /\* fprintf(ficlog," Error in movingaverage mobilav=%d\n",mobilav); *\/ */
7313: /* /\* printf(" Error in movingaverage mobilav=%d\n",mobilav); *\/ */
7314: /* /\* } *\/ */
7315: /* /\* } *\/ */
7316:
7317: /* stepsize=(int) (stepm+YEARM-1)/YEARM; */
7318: /* if (stepm<=12) stepsize=1; */
7319: /* if(estepm < stepm){ */
7320: /* printf ("Problem %d lower than %d\n",estepm, stepm); */
7321: /* } */
7322: /* else hstepm=estepm; */
7323:
7324: /* hstepm=hstepm/stepm; */
7325: /* yp1=modf(dateintmean,&yp);/\* extracts integral of datemean in yp and */
7326: /* fractional in yp1 *\/ */
7327: /* anprojmean=yp; */
7328: /* yp2=modf((yp1*12),&yp); */
7329: /* mprojmean=yp; */
7330: /* yp1=modf((yp2*30.5),&yp); */
7331: /* jprojmean=yp; */
7332: /* if(jprojmean==0) jprojmean=1; */
7333: /* if(mprojmean==0) jprojmean=1; */
7334:
1.225 brouard 7335: /* i1=cptcoveff; */
1.218 brouard 7336: /* if (cptcovn < 1){i1=1;} */
1.217 brouard 7337:
1.218 brouard 7338: /* fprintf(ficresfb,"# Mean day of interviews %.lf/%.lf/%.lf (%.2f) between %.2f and %.2f \n",jprojmean,mprojmean,anprojmean,dateintmean,dateprev1,dateprev2); */
1.217 brouard 7339:
1.218 brouard 7340: /* fprintf(ficresfb,"#****** Routine prevbackforecast **\n"); */
7341:
7342: /* /\* if (h==(int)(YEARM*yearp)){ *\/ */
7343: /* for(cptcov=1, k=0;cptcov<=i1;cptcov++){ */
1.225 brouard 7344: /* for(cptcod=1;cptcod<=ncodemax[cptcoveff];cptcod++){ */
1.218 brouard 7345: /* k=k+1; */
7346: /* fprintf(ficresfb,"\n#****** hbijx=probability over h years, hp.jx is weighted by observed prev \n#"); */
1.225 brouard 7347: /* for(j=1;j<=cptcoveff;j++) { */
1.218 brouard 7348: /* fprintf(ficresfb," V%d (=) %d",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]); */
7349: /* } */
7350: /* fprintf(ficresfb," yearbproj age"); */
7351: /* for(j=1; j<=nlstate+ndeath;j++){ */
7352: /* for(i=1; i<=nlstate;i++) */
7353: /* fprintf(ficresfb," p%d%d",i,j); */
7354: /* fprintf(ficresfb," p.%d",j); */
7355: /* } */
7356: /* for (yearp=0; yearp>=(anback2-anback1);yearp -=stepsize) { */
7357: /* /\* for (yearp=0; yearp<=(anproj2-anproj1);yearp +=stepsize) { *\/ */
7358: /* fprintf(ficresfb,"\n"); */
7359: /* fprintf(ficresfb,"\n# Back Forecasting at date %.lf/%.lf/%.lf ",jback1,mback1,anback1+yearp); */
7360: /* for (agec=fage; agec>=(ageminpar-1); agec--){ */
7361: /* nhstepm=(int) rint((agelim-agec)*YEARM/stepm); */
7362: /* nhstepm = nhstepm/hstepm; */
7363: /* p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); */
7364: /* oldm=oldms;savm=savms; */
7365: /* hbxij(p3mat,nhstepm,agec,hstepm,p,prevacurrent,nlstate,stepm,oldm,savm,oldm,savm, dnewm, doldm, dsavm, k); */
7366: /* for (h=0; h<=nhstepm; h++){ */
7367: /* if (h*hstepm/YEARM*stepm ==yearp) { */
7368: /* fprintf(ficresfb,"\n"); */
1.225 brouard 7369: /* for(j=1;j<=cptcoveff;j++) */
1.218 brouard 7370: /* fprintf(ficresfb,"%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]); */
7371: /* fprintf(ficresfb,"%.f %.f ",anback1+yearp,agec+h*hstepm/YEARM*stepm); */
7372: /* } */
7373: /* for(j=1; j<=nlstate+ndeath;j++) { */
7374: /* ppij=0.; */
7375: /* for(i=1; i<=nlstate;i++) { */
7376: /* if (mobilav==1) */
7377: /* ppij=ppij+p3mat[i][j][h]*mobaverage[(int)agec][i][cptcod]; */
7378: /* else { */
7379: /* ppij=ppij+p3mat[i][j][h]*probs[(int)(agec)][i][cptcod]; */
7380: /* } */
7381: /* if (h*hstepm/YEARM*stepm== yearp) { */
7382: /* fprintf(ficresfb," %.3f", p3mat[i][j][h]); */
7383: /* } */
7384: /* } /\* end i *\/ */
7385: /* if (h*hstepm/YEARM*stepm==yearp) { */
7386: /* fprintf(ficresfb," %.3f", ppij); */
7387: /* } */
7388: /* }/\* end j *\/ */
7389: /* } /\* end h *\/ */
7390: /* free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); */
7391: /* } /\* end agec *\/ */
7392: /* } /\* end yearp *\/ */
7393: /* } /\* end cptcod *\/ */
7394: /* } /\* end cptcov *\/ */
7395:
7396: /* /\* if (mobilav!=0) free_ma3x(mobaverage,1, AGESUP,1,NCOVMAX, 1,NCOVMAX); *\/ */
7397:
7398: /* fclose(ficresfb); */
7399: /* printf("End of Computing Back forecasting \n"); */
7400: /* fprintf(ficlog,"End of Computing Back forecasting\n"); */
1.217 brouard 7401:
1.218 brouard 7402: /* } */
1.217 brouard 7403:
1.126 brouard 7404: /************** Forecasting *****not tested NB*************/
1.227 brouard 7405: /* void populforecast(char fileres[], double anpyram,double mpyram,double jpyram,double ageminpar, double agemax,double dateprev1, double dateprev2s, int mobilav, double agedeb, double fage, int popforecast, char popfile[], double anpyram1,double p[], int i2){ */
1.126 brouard 7406:
1.227 brouard 7407: /* int cpt, stepsize, hstepm, nhstepm, j,k,c, cptcod, i,h; */
7408: /* int *popage; */
7409: /* double calagedatem, agelim, kk1, kk2; */
7410: /* double *popeffectif,*popcount; */
7411: /* double ***p3mat,***tabpop,***tabpopprev; */
7412: /* /\* double ***mobaverage; *\/ */
7413: /* char filerespop[FILENAMELENGTH]; */
1.126 brouard 7414:
1.227 brouard 7415: /* tabpop= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
7416: /* tabpopprev= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
7417: /* agelim=AGESUP; */
7418: /* calagedatem=(anpyram+mpyram/12.+jpyram/365.-dateintmean)*YEARM; */
1.126 brouard 7419:
1.227 brouard 7420: /* prevalence(probs, ageminpar, agemax, s, agev, nlstate, imx, Tvar, nbcode, ncodemax, mint, anint, dateprev1, dateprev2, firstpass, lastpass); */
1.126 brouard 7421:
7422:
1.227 brouard 7423: /* strcpy(filerespop,"POP_"); */
7424: /* strcat(filerespop,fileresu); */
7425: /* if((ficrespop=fopen(filerespop,"w"))==NULL) { */
7426: /* printf("Problem with forecast resultfile: %s\n", filerespop); */
7427: /* fprintf(ficlog,"Problem with forecast resultfile: %s\n", filerespop); */
7428: /* } */
7429: /* printf("Computing forecasting: result on file '%s' \n", filerespop); */
7430: /* fprintf(ficlog,"Computing forecasting: result on file '%s' \n", filerespop); */
1.126 brouard 7431:
1.227 brouard 7432: /* if (cptcoveff==0) ncodemax[cptcoveff]=1; */
1.126 brouard 7433:
1.227 brouard 7434: /* /\* if (mobilav!=0) { *\/ */
7435: /* /\* mobaverage= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX); *\/ */
7436: /* /\* if (movingaverage(probs, ageminpar, fage, mobaverage,mobilav)!=0){ *\/ */
7437: /* /\* fprintf(ficlog," Error in movingaverage mobilav=%d\n",mobilav); *\/ */
7438: /* /\* printf(" Error in movingaverage mobilav=%d\n",mobilav); *\/ */
7439: /* /\* } *\/ */
7440: /* /\* } *\/ */
1.126 brouard 7441:
1.227 brouard 7442: /* stepsize=(int) (stepm+YEARM-1)/YEARM; */
7443: /* if (stepm<=12) stepsize=1; */
1.126 brouard 7444:
1.227 brouard 7445: /* agelim=AGESUP; */
1.126 brouard 7446:
1.227 brouard 7447: /* hstepm=1; */
7448: /* hstepm=hstepm/stepm; */
1.218 brouard 7449:
1.227 brouard 7450: /* if (popforecast==1) { */
7451: /* if((ficpop=fopen(popfile,"r"))==NULL) { */
7452: /* printf("Problem with population file : %s\n",popfile);exit(0); */
7453: /* fprintf(ficlog,"Problem with population file : %s\n",popfile);exit(0); */
7454: /* } */
7455: /* popage=ivector(0,AGESUP); */
7456: /* popeffectif=vector(0,AGESUP); */
7457: /* popcount=vector(0,AGESUP); */
1.126 brouard 7458:
1.227 brouard 7459: /* i=1; */
7460: /* while ((c=fscanf(ficpop,"%d %lf\n",&popage[i],&popcount[i])) != EOF) i=i+1; */
1.218 brouard 7461:
1.227 brouard 7462: /* imx=i; */
7463: /* for (i=1; i<imx;i++) popeffectif[popage[i]]=popcount[i]; */
7464: /* } */
1.218 brouard 7465:
1.227 brouard 7466: /* for(cptcov=1,k=0;cptcov<=i2;cptcov++){ */
7467: /* for(cptcod=1;cptcod<=ncodemax[cptcoveff];cptcod++){ */
7468: /* k=k+1; */
7469: /* fprintf(ficrespop,"\n#******"); */
7470: /* for(j=1;j<=cptcoveff;j++) { */
7471: /* fprintf(ficrespop," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]); */
7472: /* } */
7473: /* fprintf(ficrespop,"******\n"); */
7474: /* fprintf(ficrespop,"# Age"); */
7475: /* for(j=1; j<=nlstate+ndeath;j++) fprintf(ficrespop," P.%d",j); */
7476: /* if (popforecast==1) fprintf(ficrespop," [Population]"); */
1.126 brouard 7477:
1.227 brouard 7478: /* for (cpt=0; cpt<=0;cpt++) { */
7479: /* fprintf(ficrespop,"\n\n# Forecasting at date %.lf/%.lf/%.lf ",jpyram,mpyram,anpyram+cpt); */
1.126 brouard 7480:
1.227 brouard 7481: /* for (agedeb=(fage-((int)calagedatem %12/12.)); agedeb>=(ageminpar-((int)calagedatem %12)/12.); agedeb--){ */
7482: /* nhstepm=(int) rint((agelim-agedeb)*YEARM/stepm); */
7483: /* nhstepm = nhstepm/hstepm; */
1.126 brouard 7484:
1.227 brouard 7485: /* p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); */
7486: /* oldm=oldms;savm=savms; */
7487: /* hpxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k); */
1.218 brouard 7488:
1.227 brouard 7489: /* for (h=0; h<=nhstepm; h++){ */
7490: /* if (h==(int) (calagedatem+YEARM*cpt)) { */
7491: /* fprintf(ficrespop,"\n %3.f ",agedeb+h*hstepm/YEARM*stepm); */
7492: /* } */
7493: /* for(j=1; j<=nlstate+ndeath;j++) { */
7494: /* kk1=0.;kk2=0; */
7495: /* for(i=1; i<=nlstate;i++) { */
7496: /* if (mobilav==1) */
7497: /* kk1=kk1+p3mat[i][j][h]*mobaverage[(int)agedeb+1][i][cptcod]; */
7498: /* else { */
7499: /* kk1=kk1+p3mat[i][j][h]*probs[(int)(agedeb+1)][i][cptcod]; */
7500: /* } */
7501: /* } */
7502: /* if (h==(int)(calagedatem+12*cpt)){ */
7503: /* tabpop[(int)(agedeb)][j][cptcod]=kk1; */
7504: /* /\*fprintf(ficrespop," %.3f", kk1); */
7505: /* if (popforecast==1) fprintf(ficrespop," [%.f]", kk1*popeffectif[(int)agedeb+1]);*\/ */
7506: /* } */
7507: /* } */
7508: /* for(i=1; i<=nlstate;i++){ */
7509: /* kk1=0.; */
7510: /* for(j=1; j<=nlstate;j++){ */
7511: /* kk1= kk1+tabpop[(int)(agedeb)][j][cptcod]; */
7512: /* } */
7513: /* tabpopprev[(int)(agedeb)][i][cptcod]=tabpop[(int)(agedeb)][i][cptcod]/kk1*popeffectif[(int)(agedeb+(calagedatem+12*cpt)*hstepm/YEARM*stepm-1)]; */
7514: /* } */
1.218 brouard 7515:
1.227 brouard 7516: /* if (h==(int)(calagedatem+12*cpt)) */
7517: /* for(j=1; j<=nlstate;j++) */
7518: /* fprintf(ficrespop," %15.2f",tabpopprev[(int)(agedeb+1)][j][cptcod]); */
7519: /* } */
7520: /* free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); */
7521: /* } */
7522: /* } */
1.218 brouard 7523:
1.227 brouard 7524: /* /\******\/ */
1.218 brouard 7525:
1.227 brouard 7526: /* for (cpt=1; cpt<=(anpyram1-anpyram);cpt++) { */
7527: /* fprintf(ficrespop,"\n\n# Forecasting at date %.lf/%.lf/%.lf ",jpyram,mpyram,anpyram+cpt); */
7528: /* for (agedeb=(fage-((int)calagedatem %12/12.)); agedeb>=(ageminpar-((int)calagedatem %12)/12.); agedeb--){ */
7529: /* nhstepm=(int) rint((agelim-agedeb)*YEARM/stepm); */
7530: /* nhstepm = nhstepm/hstepm; */
1.126 brouard 7531:
1.227 brouard 7532: /* p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); */
7533: /* oldm=oldms;savm=savms; */
7534: /* hpxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k); */
7535: /* for (h=0; h<=nhstepm; h++){ */
7536: /* if (h==(int) (calagedatem+YEARM*cpt)) { */
7537: /* fprintf(ficresf,"\n %3.f ",agedeb+h*hstepm/YEARM*stepm); */
7538: /* } */
7539: /* for(j=1; j<=nlstate+ndeath;j++) { */
7540: /* kk1=0.;kk2=0; */
7541: /* for(i=1; i<=nlstate;i++) { */
7542: /* kk1=kk1+p3mat[i][j][h]*tabpopprev[(int)agedeb+1][i][cptcod]; */
7543: /* } */
7544: /* if (h==(int)(calagedatem+12*cpt)) fprintf(ficresf," %15.2f", kk1); */
7545: /* } */
7546: /* } */
7547: /* free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); */
7548: /* } */
7549: /* } */
7550: /* } */
7551: /* } */
1.218 brouard 7552:
1.227 brouard 7553: /* /\* if (mobilav!=0) free_ma3x(mobaverage,1, AGESUP,1,NCOVMAX, 1,NCOVMAX); *\/ */
1.218 brouard 7554:
1.227 brouard 7555: /* if (popforecast==1) { */
7556: /* free_ivector(popage,0,AGESUP); */
7557: /* free_vector(popeffectif,0,AGESUP); */
7558: /* free_vector(popcount,0,AGESUP); */
7559: /* } */
7560: /* free_ma3x(tabpop,1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
7561: /* free_ma3x(tabpopprev,1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
7562: /* fclose(ficrespop); */
7563: /* } /\* End of popforecast *\/ */
1.218 brouard 7564:
1.126 brouard 7565: int fileappend(FILE *fichier, char *optionfich)
7566: {
7567: if((fichier=fopen(optionfich,"a"))==NULL) {
7568: printf("Problem with file: %s\n", optionfich);
7569: fprintf(ficlog,"Problem with file: %s\n", optionfich);
7570: return (0);
7571: }
7572: fflush(fichier);
7573: return (1);
7574: }
7575:
7576:
7577: /**************** function prwizard **********************/
7578: void prwizard(int ncovmodel, int nlstate, int ndeath, char model[], FILE *ficparo)
7579: {
7580:
7581: /* Wizard to print covariance matrix template */
7582:
1.164 brouard 7583: char ca[32], cb[32];
7584: int i,j, k, li, lj, lk, ll, jj, npar, itimes;
1.126 brouard 7585: int numlinepar;
7586:
7587: printf("# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n");
7588: fprintf(ficparo,"# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n");
7589: for(i=1; i <=nlstate; i++){
7590: jj=0;
7591: for(j=1; j <=nlstate+ndeath; j++){
7592: if(j==i) continue;
7593: jj++;
7594: /*ca[0]= k+'a'-1;ca[1]='\0';*/
7595: printf("%1d%1d",i,j);
7596: fprintf(ficparo,"%1d%1d",i,j);
7597: for(k=1; k<=ncovmodel;k++){
7598: /* printf(" %lf",param[i][j][k]); */
7599: /* fprintf(ficparo," %lf",param[i][j][k]); */
7600: printf(" 0.");
7601: fprintf(ficparo," 0.");
7602: }
7603: printf("\n");
7604: fprintf(ficparo,"\n");
7605: }
7606: }
7607: printf("# Scales (for hessian or gradient estimation)\n");
7608: fprintf(ficparo,"# Scales (for hessian or gradient estimation)\n");
7609: npar= (nlstate+ndeath-1)*nlstate*ncovmodel; /* Number of parameters*/
7610: for(i=1; i <=nlstate; i++){
7611: jj=0;
7612: for(j=1; j <=nlstate+ndeath; j++){
7613: if(j==i) continue;
7614: jj++;
7615: fprintf(ficparo,"%1d%1d",i,j);
7616: printf("%1d%1d",i,j);
7617: fflush(stdout);
7618: for(k=1; k<=ncovmodel;k++){
7619: /* printf(" %le",delti3[i][j][k]); */
7620: /* fprintf(ficparo," %le",delti3[i][j][k]); */
7621: printf(" 0.");
7622: fprintf(ficparo," 0.");
7623: }
7624: numlinepar++;
7625: printf("\n");
7626: fprintf(ficparo,"\n");
7627: }
7628: }
7629: printf("# Covariance matrix\n");
7630: /* # 121 Var(a12)\n\ */
7631: /* # 122 Cov(b12,a12) Var(b12)\n\ */
7632: /* # 131 Cov(a13,a12) Cov(a13,b12, Var(a13)\n\ */
7633: /* # 132 Cov(b13,a12) Cov(b13,b12, Cov(b13,a13) Var(b13)\n\ */
7634: /* # 212 Cov(a21,a12) Cov(a21,b12, Cov(a21,a13) Cov(a21,b13) Var(a21)\n\ */
7635: /* # 212 Cov(b21,a12) Cov(b21,b12, Cov(b21,a13) Cov(b21,b13) Cov(b21,a21) Var(b21)\n\ */
7636: /* # 232 Cov(a23,a12) Cov(a23,b12, Cov(a23,a13) Cov(a23,b13) Cov(a23,a21) Cov(a23,b21) Var(a23)\n\ */
7637: /* # 232 Cov(b23,a12) Cov(b23,b12) ... Var (b23)\n" */
7638: fflush(stdout);
7639: fprintf(ficparo,"# Covariance matrix\n");
7640: /* # 121 Var(a12)\n\ */
7641: /* # 122 Cov(b12,a12) Var(b12)\n\ */
7642: /* # ...\n\ */
7643: /* # 232 Cov(b23,a12) Cov(b23,b12) ... Var (b23)\n" */
7644:
7645: for(itimes=1;itimes<=2;itimes++){
7646: jj=0;
7647: for(i=1; i <=nlstate; i++){
7648: for(j=1; j <=nlstate+ndeath; j++){
7649: if(j==i) continue;
7650: for(k=1; k<=ncovmodel;k++){
7651: jj++;
7652: ca[0]= k+'a'-1;ca[1]='\0';
7653: if(itimes==1){
7654: printf("#%1d%1d%d",i,j,k);
7655: fprintf(ficparo,"#%1d%1d%d",i,j,k);
7656: }else{
7657: printf("%1d%1d%d",i,j,k);
7658: fprintf(ficparo,"%1d%1d%d",i,j,k);
7659: /* printf(" %.5le",matcov[i][j]); */
7660: }
7661: ll=0;
7662: for(li=1;li <=nlstate; li++){
7663: for(lj=1;lj <=nlstate+ndeath; lj++){
7664: if(lj==li) continue;
7665: for(lk=1;lk<=ncovmodel;lk++){
7666: ll++;
7667: if(ll<=jj){
7668: cb[0]= lk +'a'-1;cb[1]='\0';
7669: if(ll<jj){
7670: if(itimes==1){
7671: printf(" Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj);
7672: fprintf(ficparo," Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj);
7673: }else{
7674: printf(" 0.");
7675: fprintf(ficparo," 0.");
7676: }
7677: }else{
7678: if(itimes==1){
7679: printf(" Var(%s%1d%1d)",ca,i,j);
7680: fprintf(ficparo," Var(%s%1d%1d)",ca,i,j);
7681: }else{
7682: printf(" 0.");
7683: fprintf(ficparo," 0.");
7684: }
7685: }
7686: }
7687: } /* end lk */
7688: } /* end lj */
7689: } /* end li */
7690: printf("\n");
7691: fprintf(ficparo,"\n");
7692: numlinepar++;
7693: } /* end k*/
7694: } /*end j */
7695: } /* end i */
7696: } /* end itimes */
7697:
7698: } /* end of prwizard */
7699: /******************* Gompertz Likelihood ******************************/
7700: double gompertz(double x[])
7701: {
7702: double A,B,L=0.0,sump=0.,num=0.;
7703: int i,n=0; /* n is the size of the sample */
7704:
1.220 brouard 7705: for (i=1;i<=imx ; i++) {
1.126 brouard 7706: sump=sump+weight[i];
7707: /* sump=sump+1;*/
7708: num=num+1;
7709: }
7710:
7711:
7712: /* for (i=0; i<=imx; i++)
7713: 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]);*/
7714:
7715: for (i=1;i<=imx ; i++)
7716: {
7717: if (cens[i] == 1 && wav[i]>1)
7718: A=-x[1]/(x[2])*(exp(x[2]*(agecens[i]-agegomp))-exp(x[2]*(ageexmed[i]-agegomp)));
7719:
7720: if (cens[i] == 0 && wav[i]>1)
7721: A=-x[1]/(x[2])*(exp(x[2]*(agedc[i]-agegomp))-exp(x[2]*(ageexmed[i]-agegomp)))
7722: +log(x[1]/YEARM)+x[2]*(agedc[i]-agegomp)+log(YEARM);
7723:
7724: /*if (wav[i] > 1 && agecens[i] > 15) {*/ /* ??? */
7725: if (wav[i] > 1 ) { /* ??? */
7726: L=L+A*weight[i];
7727: /* 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]);*/
7728: }
7729: }
7730:
7731: /*printf("x1=%2.9f x2=%2.9f x3=%2.9f L=%f\n",x[1],x[2],x[3],L);*/
7732:
7733: return -2*L*num/sump;
7734: }
7735:
1.136 brouard 7736: #ifdef GSL
7737: /******************* Gompertz_f Likelihood ******************************/
7738: double gompertz_f(const gsl_vector *v, void *params)
7739: {
7740: double A,B,LL=0.0,sump=0.,num=0.;
7741: double *x= (double *) v->data;
7742: int i,n=0; /* n is the size of the sample */
7743:
7744: for (i=0;i<=imx-1 ; i++) {
7745: sump=sump+weight[i];
7746: /* sump=sump+1;*/
7747: num=num+1;
7748: }
7749:
7750:
7751: /* for (i=0; i<=imx; i++)
7752: 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]);*/
7753: printf("x[0]=%lf x[1]=%lf\n",x[0],x[1]);
7754: for (i=1;i<=imx ; i++)
7755: {
7756: if (cens[i] == 1 && wav[i]>1)
7757: A=-x[0]/(x[1])*(exp(x[1]*(agecens[i]-agegomp))-exp(x[1]*(ageexmed[i]-agegomp)));
7758:
7759: if (cens[i] == 0 && wav[i]>1)
7760: A=-x[0]/(x[1])*(exp(x[1]*(agedc[i]-agegomp))-exp(x[1]*(ageexmed[i]-agegomp)))
7761: +log(x[0]/YEARM)+x[1]*(agedc[i]-agegomp)+log(YEARM);
7762:
7763: /*if (wav[i] > 1 && agecens[i] > 15) {*/ /* ??? */
7764: if (wav[i] > 1 ) { /* ??? */
7765: LL=LL+A*weight[i];
7766: /* 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]);*/
7767: }
7768: }
7769:
7770: /*printf("x1=%2.9f x2=%2.9f x3=%2.9f L=%f\n",x[1],x[2],x[3],L);*/
7771: printf("x[0]=%lf x[1]=%lf -2*LL*num/sump=%lf\n",x[0],x[1],-2*LL*num/sump);
7772:
7773: return -2*LL*num/sump;
7774: }
7775: #endif
7776:
1.126 brouard 7777: /******************* Printing html file ***********/
1.201 brouard 7778: void printinghtmlmort(char fileresu[], char title[], char datafile[], int firstpass, \
1.126 brouard 7779: int lastpass, int stepm, int weightopt, char model[],\
7780: int imx, double p[],double **matcov,double agemortsup){
7781: int i,k;
7782:
7783: fprintf(fichtm,"<ul><li><h4>Result files </h4>\n Force of mortality. Parameters of the Gompertz fit (with confidence interval in brackets):<br>");
7784: fprintf(fichtm," mu(age) =%lf*exp(%lf*(age-%d)) per year<br><br>",p[1],p[2],agegomp);
7785: for (i=1;i<=2;i++)
7786: 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 7787: fprintf(fichtm,"<br><br><img src=\"graphmort.svg\">");
1.126 brouard 7788: fprintf(fichtm,"</ul>");
7789:
7790: fprintf(fichtm,"<ul><li><h4>Life table</h4>\n <br>");
7791:
7792: 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>");
7793:
7794: for (k=agegomp;k<(agemortsup-2);k++)
7795: 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]);
7796:
7797:
7798: fflush(fichtm);
7799: }
7800:
7801: /******************* Gnuplot file **************/
1.201 brouard 7802: void printinggnuplotmort(char fileresu[], char optionfilefiname[], double ageminpar, double agemaxpar, double fage , char pathc[], double p[]){
1.126 brouard 7803:
7804: char dirfileres[132],optfileres[132];
1.164 brouard 7805:
1.126 brouard 7806: int ng;
7807:
7808:
7809: /*#ifdef windows */
7810: fprintf(ficgp,"cd \"%s\" \n",pathc);
7811: /*#endif */
7812:
7813:
7814: strcpy(dirfileres,optionfilefiname);
7815: strcpy(optfileres,"vpl");
1.199 brouard 7816: fprintf(ficgp,"set out \"graphmort.svg\"\n ");
1.126 brouard 7817: fprintf(ficgp,"set xlabel \"Age\"\n set ylabel \"Force of mortality (per year)\" \n ");
1.199 brouard 7818: fprintf(ficgp, "set ter svg size 640, 480\n set log y\n");
1.145 brouard 7819: /* fprintf(ficgp, "set size 0.65,0.65\n"); */
1.126 brouard 7820: fprintf(ficgp,"plot [%d:100] %lf*exp(%lf*(x-%d))",agegomp,p[1],p[2],agegomp);
7821:
7822: }
7823:
1.136 brouard 7824: int readdata(char datafile[], int firstobs, int lastobs, int *imax)
7825: {
1.126 brouard 7826:
1.136 brouard 7827: /*-------- data file ----------*/
7828: FILE *fic;
7829: char dummy[]=" ";
1.223 brouard 7830: int i=0, j=0, n=0, iv=0;
7831: int lstra;
1.136 brouard 7832: int linei, month, year,iout;
7833: char line[MAXLINE], linetmp[MAXLINE];
1.164 brouard 7834: char stra[MAXLINE], strb[MAXLINE];
1.136 brouard 7835: char *stratrunc;
1.223 brouard 7836:
1.126 brouard 7837:
7838:
1.136 brouard 7839: if((fic=fopen(datafile,"r"))==NULL) {
1.218 brouard 7840: printf("Problem while opening datafile: %s with errno='%s'\n", datafile,strerror(errno));fflush(stdout);
7841: fprintf(ficlog,"Problem while opening datafile: %s with errno='%s'\n", datafile,strerror(errno));fflush(ficlog);return 1;
1.136 brouard 7842: }
1.126 brouard 7843:
1.136 brouard 7844: i=1;
7845: linei=0;
7846: while ((fgets(line, MAXLINE, fic) != NULL) &&((i >= firstobs) && (i <=lastobs))) {
7847: linei=linei+1;
7848: for(j=strlen(line); j>=0;j--){ /* Untabifies line */
7849: if(line[j] == '\t')
7850: line[j] = ' ';
7851: }
7852: for(j=strlen(line)-1; (line[j]==' ')||(line[j]==10)||(line[j]==13);j--){
7853: ;
7854: };
7855: line[j+1]=0; /* Trims blanks at end of line */
7856: if(line[0]=='#'){
7857: fprintf(ficlog,"Comment line\n%s\n",line);
7858: printf("Comment line\n%s\n",line);
7859: continue;
7860: }
7861: trimbb(linetmp,line); /* Trims multiple blanks in line */
1.164 brouard 7862: strcpy(line, linetmp);
1.223 brouard 7863:
7864: /* Loops on waves */
7865: for (j=maxwav;j>=1;j--){
7866: for (iv=nqtv;iv>=1;iv--){ /* Loop on time varying quantitative variables */
1.238 brouard 7867: cutv(stra, strb, line, ' ');
7868: if(strb[0]=='.') { /* Missing value */
7869: lval=-1;
7870: cotqvar[j][iv][i]=-1; /* 0.0/0.0 */
7871: cotvar[j][ntv+iv][i]=-1; /* For performance reasons */
7872: if(isalpha(strb[1])) { /* .m or .d Really Missing value */
7873: printf("Error reading data around '%s' at line number %d for individual %d, '%s'\nShould be the %d th quantitative value out of %d measured at wave %d. If missing, you should remove this individual or impute a value. Exiting.\n", strb, linei,i,line,iv, nqtv, j);
7874: fprintf(ficlog,"Error reading data around '%s' at line number %d for individual %d, '%s'\nShould be the %d th quantitative value out of %d measured at wave %d. If missing, you should remove this individual or impute a value. Exiting.\n", strb, linei,i,line,iv, nqtv, j);fflush(ficlog);
7875: return 1;
7876: }
7877: }else{
7878: errno=0;
7879: /* what_kind_of_number(strb); */
7880: dval=strtod(strb,&endptr);
7881: /* if( strb[0]=='\0' || (*endptr != '\0')){ */
7882: /* if(strb != endptr && *endptr == '\0') */
7883: /* dval=dlval; */
7884: /* if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN)) */
7885: if( strb[0]=='\0' || (*endptr != '\0')){
7886: printf("Error reading data around '%s' at line number %d for individual %d, '%s'\nShould be the %d th quantitative value out of %d measured at wave %d. Setting maxwav=%d might be wrong. Exiting.\n", strb, linei,i,line,iv, nqtv, j,maxwav);
7887: fprintf(ficlog,"Error reading data around '%s' at line number %d for individual %d, '%s'\nShould be the %d th quantitative value out of %d measured at wave %d. Setting maxwav=%d might be wrong. Exiting.\n", strb, linei,i,line, iv, nqtv, j,maxwav);fflush(ficlog);
7888: return 1;
7889: }
7890: cotqvar[j][iv][i]=dval;
7891: cotvar[j][ntv+iv][i]=dval;
7892: }
7893: strcpy(line,stra);
1.223 brouard 7894: }/* end loop ntqv */
1.225 brouard 7895:
1.223 brouard 7896: for (iv=ntv;iv>=1;iv--){ /* Loop on time varying dummies */
1.238 brouard 7897: cutv(stra, strb, line, ' ');
7898: if(strb[0]=='.') { /* Missing value */
7899: lval=-1;
7900: }else{
7901: errno=0;
7902: lval=strtol(strb,&endptr,10);
7903: /* if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN))*/
7904: if( strb[0]=='\0' || (*endptr != '\0')){
7905: printf("Error reading data around '%s' at line number %d for individual %d, '%s'\nShould be the %d th dummy covariate out of %d measured at wave %d. Setting maxwav=%d might be wrong. Exiting.\n", strb, linei,i,line,iv, ntv, j,maxwav);
7906: fprintf(ficlog,"Error reading data around '%s' at line number %d for individual %d, '%s'\nShould be the %d dummy covariate out of %d measured wave %d. Setting maxwav=%d might be wrong. Exiting.\n", strb, linei,i,line,iv, ntv,j,maxwav);fflush(ficlog);
7907: return 1;
7908: }
7909: }
7910: if(lval <-1 || lval >1){
7911: printf("Error reading data around '%ld' at line number %d for individual %d, '%s'\n \
1.223 brouard 7912: Should be a value of %d(nth) covariate (0 should be the value for the reference and 1\n \
7913: for the alternative. IMaCh does not build design variables automatically, do it yourself.\n \
1.238 brouard 7914: For example, for multinomial values like 1, 2 and 3,\n \
7915: build V1=0 V2=0 for the reference value (1),\n \
7916: V1=1 V2=0 for (2) \n \
1.223 brouard 7917: and V1=0 V2=1 for (3). V1=1 V2=1 should not exist and the corresponding\n \
1.238 brouard 7918: output of IMaCh is often meaningless.\n \
1.223 brouard 7919: Exiting.\n",lval,linei, i,line,j);
1.238 brouard 7920: fprintf(ficlog,"Error reading data around '%ld' at line number %d for individual %d, '%s'\n \
1.223 brouard 7921: Should be a value of %d(nth) covariate (0 should be the value for the reference and 1\n \
7922: for the alternative. IMaCh does not build design variables automatically, do it yourself.\n \
1.238 brouard 7923: For example, for multinomial values like 1, 2 and 3,\n \
7924: build V1=0 V2=0 for the reference value (1),\n \
7925: V1=1 V2=0 for (2) \n \
1.223 brouard 7926: and V1=0 V2=1 for (3). V1=1 V2=1 should not exist and the corresponding\n \
1.238 brouard 7927: output of IMaCh is often meaningless.\n \
1.223 brouard 7928: Exiting.\n",lval,linei, i,line,j);fflush(ficlog);
1.238 brouard 7929: return 1;
7930: }
7931: cotvar[j][iv][i]=(double)(lval);
7932: strcpy(line,stra);
1.223 brouard 7933: }/* end loop ntv */
1.225 brouard 7934:
1.223 brouard 7935: /* Statuses at wave */
1.137 brouard 7936: cutv(stra, strb, line, ' ');
1.223 brouard 7937: if(strb[0]=='.') { /* Missing value */
1.238 brouard 7938: lval=-1;
1.136 brouard 7939: }else{
1.238 brouard 7940: errno=0;
7941: lval=strtol(strb,&endptr,10);
7942: /* if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN))*/
7943: if( strb[0]=='\0' || (*endptr != '\0')){
7944: 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);
7945: 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);
7946: return 1;
7947: }
1.136 brouard 7948: }
1.225 brouard 7949:
1.136 brouard 7950: s[j][i]=lval;
1.225 brouard 7951:
1.223 brouard 7952: /* Date of Interview */
1.136 brouard 7953: strcpy(line,stra);
7954: cutv(stra, strb,line,' ');
1.169 brouard 7955: if( (iout=sscanf(strb,"%d/%d",&month, &year)) != 0){
1.136 brouard 7956: }
1.169 brouard 7957: else if( (iout=sscanf(strb,"%s.",dummy)) != 0){
1.225 brouard 7958: month=99;
7959: year=9999;
1.136 brouard 7960: }else{
1.225 brouard 7961: 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);
7962: 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);
7963: return 1;
1.136 brouard 7964: }
7965: anint[j][i]= (double) year;
7966: mint[j][i]= (double)month;
7967: strcpy(line,stra);
1.223 brouard 7968: } /* End loop on waves */
1.225 brouard 7969:
1.223 brouard 7970: /* Date of death */
1.136 brouard 7971: cutv(stra, strb,line,' ');
1.169 brouard 7972: if( (iout=sscanf(strb,"%d/%d",&month, &year)) != 0){
1.136 brouard 7973: }
1.169 brouard 7974: else if( (iout=sscanf(strb,"%s.",dummy)) != 0){
1.136 brouard 7975: month=99;
7976: year=9999;
7977: }else{
1.141 brouard 7978: printf("Error reading data around '%s' at line number %d for individual %d, '%s'\nShould be a date of death (mm/yyyy or .). Exiting.\n",strb, linei,i,line);
1.225 brouard 7979: 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);
7980: return 1;
1.136 brouard 7981: }
7982: andc[i]=(double) year;
7983: moisdc[i]=(double) month;
7984: strcpy(line,stra);
7985:
1.223 brouard 7986: /* Date of birth */
1.136 brouard 7987: cutv(stra, strb,line,' ');
1.169 brouard 7988: if( (iout=sscanf(strb,"%d/%d",&month, &year)) != 0){
1.136 brouard 7989: }
1.169 brouard 7990: else if( (iout=sscanf(strb,"%s.", dummy)) != 0){
1.136 brouard 7991: month=99;
7992: year=9999;
7993: }else{
1.141 brouard 7994: 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);
7995: fprintf(ficlog,"Error reading data around '%s' at line number %d for individual %d, '%s'\nShould be a date of birth (mm/yyyy or .). Exiting.\n",strb, linei,i,line);fflush(ficlog);
1.225 brouard 7996: return 1;
1.136 brouard 7997: }
7998: if (year==9999) {
1.141 brouard 7999: 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);
8000: fprintf(ficlog,"Error reading data around '%s' at line number %d for individual %d, '%s'\nShould be a date of birth (mm/yyyy) but at least the year of birth should be given. Exiting.\n",strb, linei,i,line);fflush(ficlog);
1.225 brouard 8001: return 1;
8002:
1.136 brouard 8003: }
8004: annais[i]=(double)(year);
8005: moisnais[i]=(double)(month);
8006: strcpy(line,stra);
1.225 brouard 8007:
1.223 brouard 8008: /* Sample weight */
1.136 brouard 8009: cutv(stra, strb,line,' ');
8010: errno=0;
8011: dval=strtod(strb,&endptr);
8012: if( strb[0]=='\0' || (*endptr != '\0')){
1.141 brouard 8013: printf("Error reading data around '%f' at line number %d, \"%s\" for individual %d\nShould be a weight. Exiting.\n",dval, i,line,linei);
8014: 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 8015: fflush(ficlog);
8016: return 1;
8017: }
8018: weight[i]=dval;
8019: strcpy(line,stra);
1.225 brouard 8020:
1.223 brouard 8021: for (iv=nqv;iv>=1;iv--){ /* Loop on fixed quantitative variables */
8022: cutv(stra, strb, line, ' ');
8023: if(strb[0]=='.') { /* Missing value */
1.225 brouard 8024: lval=-1;
1.223 brouard 8025: }else{
1.225 brouard 8026: errno=0;
8027: /* what_kind_of_number(strb); */
8028: dval=strtod(strb,&endptr);
8029: /* if(strb != endptr && *endptr == '\0') */
8030: /* dval=dlval; */
8031: /* if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN)) */
8032: if( strb[0]=='\0' || (*endptr != '\0')){
8033: printf("Error reading data around '%s' at line number %d for individual %d, '%s'\nShould be the %d th quantitative value (out of %d) constant for all waves. Setting maxwav=%d might be wrong. Exiting.\n", strb, linei,i,line, iv, nqv, maxwav);
8034: fprintf(ficlog,"Error reading data around '%s' at line number %d for individual %d, '%s'\nShould be the %d th quantitative value (out of %d) constant for all waves. Setting maxwav=%d might be wrong. Exiting.\n", strb, linei,i,line, iv, nqv, maxwav);fflush(ficlog);
8035: return 1;
8036: }
8037: coqvar[iv][i]=dval;
1.226 brouard 8038: covar[ncovcol+iv][i]=dval; /* including qvar in standard covar for performance reasons */
1.223 brouard 8039: }
8040: strcpy(line,stra);
8041: }/* end loop nqv */
1.136 brouard 8042:
1.223 brouard 8043: /* Covariate values */
1.136 brouard 8044: for (j=ncovcol;j>=1;j--){
8045: cutv(stra, strb,line,' ');
1.223 brouard 8046: if(strb[0]=='.') { /* Missing covariate value */
1.225 brouard 8047: lval=-1;
1.136 brouard 8048: }else{
1.225 brouard 8049: errno=0;
8050: lval=strtol(strb,&endptr,10);
8051: if( strb[0]=='\0' || (*endptr != '\0')){
8052: 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);
8053: 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);
8054: return 1;
8055: }
1.136 brouard 8056: }
8057: if(lval <-1 || lval >1){
1.225 brouard 8058: printf("Error reading data around '%ld' at line number %d for individual %d, '%s'\n \
1.136 brouard 8059: Should be a value of %d(nth) covariate (0 should be the value for the reference and 1\n \
8060: for the alternative. IMaCh does not build design variables automatically, do it yourself.\n \
1.225 brouard 8061: For example, for multinomial values like 1, 2 and 3,\n \
8062: build V1=0 V2=0 for the reference value (1),\n \
8063: V1=1 V2=0 for (2) \n \
1.136 brouard 8064: and V1=0 V2=1 for (3). V1=1 V2=1 should not exist and the corresponding\n \
1.225 brouard 8065: output of IMaCh is often meaningless.\n \
1.136 brouard 8066: Exiting.\n",lval,linei, i,line,j);
1.225 brouard 8067: fprintf(ficlog,"Error reading data around '%ld' at line number %d for individual %d, '%s'\n \
1.136 brouard 8068: Should be a value of %d(nth) covariate (0 should be the value for the reference and 1\n \
8069: for the alternative. IMaCh does not build design variables automatically, do it yourself.\n \
1.225 brouard 8070: For example, for multinomial values like 1, 2 and 3,\n \
8071: build V1=0 V2=0 for the reference value (1),\n \
8072: V1=1 V2=0 for (2) \n \
1.136 brouard 8073: and V1=0 V2=1 for (3). V1=1 V2=1 should not exist and the corresponding\n \
1.225 brouard 8074: output of IMaCh is often meaningless.\n \
1.136 brouard 8075: Exiting.\n",lval,linei, i,line,j);fflush(ficlog);
1.225 brouard 8076: return 1;
1.136 brouard 8077: }
8078: covar[j][i]=(double)(lval);
8079: strcpy(line,stra);
8080: }
8081: lstra=strlen(stra);
1.225 brouard 8082:
1.136 brouard 8083: if(lstra > 9){ /* More than 2**32 or max of what printf can write with %ld */
8084: stratrunc = &(stra[lstra-9]);
8085: num[i]=atol(stratrunc);
8086: }
8087: else
8088: num[i]=atol(stra);
8089: /*if((s[2][i]==2) && (s[3][i]==-1)&&(s[4][i]==9)){
8090: 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;}*/
8091:
8092: i=i+1;
8093: } /* End loop reading data */
1.225 brouard 8094:
1.136 brouard 8095: *imax=i-1; /* Number of individuals */
8096: fclose(fic);
1.225 brouard 8097:
1.136 brouard 8098: return (0);
1.164 brouard 8099: /* endread: */
1.225 brouard 8100: printf("Exiting readdata: ");
8101: fclose(fic);
8102: return (1);
1.223 brouard 8103: }
1.126 brouard 8104:
1.234 brouard 8105: void removefirstspace(char **stri){/*, char stro[]) {*/
1.230 brouard 8106: char *p1 = *stri, *p2 = *stri;
1.235 brouard 8107: while (*p2 == ' ')
1.234 brouard 8108: p2++;
8109: /* while ((*p1++ = *p2++) !=0) */
8110: /* ; */
8111: /* do */
8112: /* while (*p2 == ' ') */
8113: /* p2++; */
8114: /* while (*p1++ == *p2++); */
8115: *stri=p2;
1.145 brouard 8116: }
8117:
1.235 brouard 8118: int decoderesult ( char resultline[], int nres)
1.230 brouard 8119: /**< This routine decode one result line and returns the combination # of dummy covariates only **/
8120: {
1.235 brouard 8121: int j=0, k=0, k1=0, k2=0, k3=0, k4=0, match=0, k2q=0, k3q=0, k4q=0;
1.230 brouard 8122: char resultsav[MAXLINE];
1.234 brouard 8123: int resultmodel[MAXLINE];
8124: int modelresult[MAXLINE];
1.230 brouard 8125: char stra[80], strb[80], strc[80], strd[80],stre[80];
8126:
1.234 brouard 8127: removefirstspace(&resultline);
1.233 brouard 8128: printf("decoderesult:%s\n",resultline);
1.230 brouard 8129:
8130: if (strstr(resultline,"v") !=0){
8131: printf("Error. 'v' must be in upper case 'V' result: %s ",resultline);
8132: fprintf(ficlog,"Error. 'v' must be in upper case result: %s ",resultline);fflush(ficlog);
8133: return 1;
8134: }
8135: trimbb(resultsav, resultline);
8136: if (strlen(resultsav) >1){
8137: j=nbocc(resultsav,'='); /**< j=Number of covariate values'=' */
8138: }
1.234 brouard 8139: if( j != cptcovs ){ /* Be careful if a variable is in a product but not single */
8140: printf("ERROR: the number of variable in the resultline, %d, differs from the number of variable used in the model line, %d.\n",j, cptcovs);
8141: fprintf(ficlog,"ERROR: the number of variable in the resultline, %d, differs from the number of variable used in the model line, %d.\n",j, cptcovs);
8142: }
8143: for(k=1; k<=j;k++){ /* Loop on any covariate of the result line */
8144: if(nbocc(resultsav,'=') >1){
8145: cutl(stra,strb,resultsav,' '); /* keeps in strb after the first ' '
8146: resultsav= V4=1 V5=25.1 V3=0 strb=V3=0 stra= V4=1 V5=25.1 */
8147: cutl(strc,strd,strb,'='); /* strb:V4=1 strc=1 strd=V4 */
8148: }else
8149: cutl(strc,strd,resultsav,'=');
1.230 brouard 8150: Tvalsel[k]=atof(strc); /* 1 */
1.234 brouard 8151:
1.230 brouard 8152: cutl(strc,stre,strd,'V'); /* strd='V4' strc=4 stre='V' */;
8153: Tvarsel[k]=atoi(strc);
8154: /* Typevarsel[k]=1; /\* 1 for age product *\/ */
8155: /* cptcovsel++; */
8156: if (nbocc(stra,'=') >0)
8157: strcpy(resultsav,stra); /* and analyzes it */
8158: }
1.235 brouard 8159: /* Checking for missing or useless values in comparison of current model needs */
1.236 brouard 8160: for(k1=1; k1<= cptcovt ;k1++){ /* model line V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
8161: if(Typevar[k1]==0){ /* Single covariate in model */
1.234 brouard 8162: match=0;
1.236 brouard 8163: for(k2=1; k2 <=j;k2++){/* result line V4=1 V5=24.1 V3=1 V2=8 V1=0 */
1.237 brouard 8164: if(Tvar[k1]==Tvarsel[k2]) {/* Tvar[1]=5 == Tvarsel[2]=5 */
1.236 brouard 8165: modelresult[k2]=k1;/* modelresult[2]=1 modelresult[1]=2 modelresult[3]=3 modelresult[6]=4 modelresult[9]=5 */
1.234 brouard 8166: match=1;
8167: break;
8168: }
8169: }
8170: if(match == 0){
8171: printf("Error in result line: %d value missing; result: %s, model=%s\n",k1, resultline, model);
8172: }
8173: }
8174: }
1.235 brouard 8175: /* Checking for missing or useless values in comparison of current model needs */
8176: for(k2=1; k2 <=j;k2++){ /* result line V4=1 V5=24.1 V3=1 V2=8 V1=0 */
1.234 brouard 8177: match=0;
1.235 brouard 8178: for(k1=1; k1<= cptcovt ;k1++){ /* model line V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
8179: if(Typevar[k1]==0){ /* Single */
1.237 brouard 8180: if(Tvar[k1]==Tvarsel[k2]) { /* Tvar[2]=4 == Tvarsel[1]=4 */
1.235 brouard 8181: resultmodel[k1]=k2; /* resultmodel[2]=1 resultmodel[1]=2 resultmodel[3]=3 resultmodel[6]=4 resultmodel[9]=5 */
1.234 brouard 8182: ++match;
8183: }
8184: }
8185: }
8186: if(match == 0){
8187: printf("Error in result line: %d value missing; result: %s, model=%s\n",k1, resultline, model);
8188: }else if(match > 1){
8189: printf("Error in result line: %d doubled; result: %s, model=%s\n",k2, resultline, model);
8190: }
8191: }
1.235 brouard 8192:
1.234 brouard 8193: /* We need to deduce which combination number is chosen and save quantitative values */
1.235 brouard 8194: /* model line V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
8195: /* result line V4=1 V5=25.1 V3=0 V2=8 V1=1 */
8196: /* should give a combination of dummy V4=1, V3=0, V1=1 => V4*2**(0) + V3*2**(1) + V1*2**(2) = 5 + (1offset) = 6*/
8197: /* result line V4=1 V5=24.1 V3=1 V2=8 V1=0 */
8198: /* should give a combination of dummy V4=1, V3=1, V1=0 => V4*2**(0) + V3*2**(1) + V1*2**(2) = 3 + (1offset) = 4*/
8199: /* 1 0 0 0 */
8200: /* 2 1 0 0 */
8201: /* 3 0 1 0 */
8202: /* 4 1 1 0 */ /* V4=1, V3=1, V1=0 */
8203: /* 5 0 0 1 */
8204: /* 6 1 0 1 */ /* V4=1, V3=0, V1=1 */
8205: /* 7 0 1 1 */
8206: /* 8 1 1 1 */
1.237 brouard 8207: /* V(Tvresult)=Tresult V4=1 V3=0 V1=1 Tresult[nres=1][2]=0 */
8208: /* V(Tvqresult)=Tqresult V5=25.1 V2=8 Tqresult[nres=1][1]=25.1 */
8209: /* V5*age V5 known which value for nres? */
8210: /* Tqinvresult[2]=8 Tqinvresult[1]=25.1 */
1.235 brouard 8211: for(k1=1, k=0, k4=0, k4q=0; k1 <=cptcovt;k1++){ /* model line */
8212: if( Dummy[k1]==0 && Typevar[k1]==0 ){ /* Single dummy */
1.237 brouard 8213: k3= resultmodel[k1]; /* resultmodel[2(V4)] = 1=k3 */
1.235 brouard 8214: k2=(int)Tvarsel[k3]; /* Tvarsel[resultmodel[2]]= Tvarsel[1] = 4=k2 */
8215: k+=Tvalsel[k3]*pow(2,k4); /* Tvalsel[1]=1 */
1.237 brouard 8216: Tresult[nres][k4+1]=Tvalsel[k3];/* Tresult[nres][1]=1(V4=1) Tresult[nres][2]=0(V3=0) */
8217: Tvresult[nres][k4+1]=(int)Tvarsel[k3];/* Tvresult[nres][1]=4 Tvresult[nres][3]=1 */
8218: Tinvresult[nres][(int)Tvarsel[k3]]=Tvalsel[k3]; /* Tinvresult[nres][4]=1 */
1.235 brouard 8219: printf("Decoderesult Dummy k=%d, V(k2=V%d)= Tvalsel[%d]=%d, 2**(%d)\n",k, k2, k3, (int)Tvalsel[k3], k4);
8220: k4++;;
8221: } else if( Dummy[k1]==1 && Typevar[k1]==0 ){ /* Single quantitative */
8222: k3q= resultmodel[k1]; /* resultmodel[2] = 1=k3 */
8223: k2q=(int)Tvarsel[k3q]; /* Tvarsel[resultmodel[2]]= Tvarsel[1] = 4=k2 */
1.237 brouard 8224: Tqresult[nres][k4q+1]=Tvalsel[k3q]; /* Tqresult[nres][1]=25.1 */
8225: Tvqresult[nres][k4q+1]=(int)Tvarsel[k3q]; /* Tvqresult[nres][1]=5 */
8226: Tqinvresult[nres][(int)Tvarsel[k3q]]=Tvalsel[k3q]; /* Tqinvresult[nres][5]=25.1 */
1.235 brouard 8227: printf("Decoderesult Quantitative nres=%d, V(k2q=V%d)= Tvalsel[%d]=%d, Tvarsel[%d]=%f\n",nres, k2q, k3q, Tvarsel[k3q], k3q, Tvalsel[k3q]);
8228: k4q++;;
8229: }
8230: }
1.234 brouard 8231:
1.235 brouard 8232: TKresult[nres]=++k; /* Combination for the nresult and the model */
1.230 brouard 8233: return (0);
8234: }
1.235 brouard 8235:
1.230 brouard 8236: int decodemodel( char model[], int lastobs)
8237: /**< This routine decodes the model and returns:
1.224 brouard 8238: * Model V1+V2+V3+V8+V7*V8+V5*V6+V8*age+V3*age+age*age
8239: * - nagesqr = 1 if age*age in the model, otherwise 0.
8240: * - cptcovt total number of covariates of the model nbocc(+)+1 = 8 excepting constant and age and age*age
8241: * - cptcovn or number of covariates k of the models excluding age*products =6 and age*age
8242: * - cptcovage number of covariates with age*products =2
8243: * - cptcovs number of simple covariates
8244: * - 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
8245: * which is a new column after the 9 (ncovcol) variables.
8246: * - if k is a product Vn*Vm covar[k][i] is filled with correct values for each individual
8247: * - Tprod[l] gives the kth covariates of the product Vn*Vm l=1 to cptcovprod-cptcovage
8248: * Tprod[1]@2 {5, 6}: position of first product V7*V8 is 5, and second V5*V6 is 6.
8249: * - Tvard[k] p Tvard[1][1]@4 {7, 8, 5, 6} for V7*V8 and V5*V6 .
8250: */
1.136 brouard 8251: {
1.238 brouard 8252: int i, j, k, ks, v;
1.227 brouard 8253: int j1, k1, k2, k3, k4;
1.136 brouard 8254: char modelsav[80];
1.145 brouard 8255: char stra[80], strb[80], strc[80], strd[80],stre[80];
1.187 brouard 8256: char *strpt;
1.136 brouard 8257:
1.145 brouard 8258: /*removespace(model);*/
1.136 brouard 8259: if (strlen(model) >1){ /* If there is at least 1 covariate */
1.145 brouard 8260: j=0, j1=0, k1=0, k2=-1, ks=0, cptcovn=0;
1.137 brouard 8261: if (strstr(model,"AGE") !=0){
1.192 brouard 8262: printf("Error. AGE must be in lower case 'age' model=1+age+%s. ",model);
8263: fprintf(ficlog,"Error. AGE must be in lower case model=1+age+%s. ",model);fflush(ficlog);
1.136 brouard 8264: return 1;
8265: }
1.141 brouard 8266: if (strstr(model,"v") !=0){
8267: printf("Error. 'v' must be in upper case 'V' model=%s ",model);
8268: fprintf(ficlog,"Error. 'v' must be in upper case model=%s ",model);fflush(ficlog);
8269: return 1;
8270: }
1.187 brouard 8271: strcpy(modelsav,model);
8272: if ((strpt=strstr(model,"age*age")) !=0){
8273: printf(" strpt=%s, model=%s\n",strpt, model);
8274: if(strpt != model){
1.234 brouard 8275: printf("Error in model: 'model=%s'; 'age*age' should in first place before other covariates\n \
1.192 brouard 8276: 'model=1+age+age*age+V1.' or 'model=1+age+age*age+V1+V1*age.', please swap as well as \n \
1.187 brouard 8277: corresponding column of parameters.\n",model);
1.234 brouard 8278: fprintf(ficlog,"Error in model: 'model=%s'; 'age*age' should in first place before other covariates\n \
1.192 brouard 8279: 'model=1+age+age*age+V1.' or 'model=1+age+age*age+V1+V1*age.', please swap as well as \n \
1.187 brouard 8280: corresponding column of parameters.\n",model); fflush(ficlog);
1.234 brouard 8281: return 1;
1.225 brouard 8282: }
1.187 brouard 8283: nagesqr=1;
8284: if (strstr(model,"+age*age") !=0)
1.234 brouard 8285: substrchaine(modelsav, model, "+age*age");
1.187 brouard 8286: else if (strstr(model,"age*age+") !=0)
1.234 brouard 8287: substrchaine(modelsav, model, "age*age+");
1.187 brouard 8288: else
1.234 brouard 8289: substrchaine(modelsav, model, "age*age");
1.187 brouard 8290: }else
8291: nagesqr=0;
8292: if (strlen(modelsav) >1){
8293: j=nbocc(modelsav,'+'); /**< j=Number of '+' */
8294: j1=nbocc(modelsav,'*'); /**< j1=Number of '*' */
1.224 brouard 8295: cptcovs=j+1-j1; /**< Number of simple covariates V1+V1*age+V3 +V3*V4+age*age=> V1 + V3 =5-3=2 */
1.187 brouard 8296: cptcovt= j+1; /* Number of total covariates in the model, not including
1.225 brouard 8297: * cst, age and age*age
8298: * V1+V1*age+ V3 + V3*V4+age*age=> 3+1=4*/
8299: /* including age products which are counted in cptcovage.
8300: * but the covariates which are products must be treated
8301: * separately: ncovn=4- 2=2 (V1+V3). */
1.187 brouard 8302: cptcovprod=j1; /**< Number of products V1*V2 +v3*age = 2 */
8303: cptcovprodnoage=0; /**< Number of covariate products without age: V3*V4 =1 */
1.225 brouard 8304:
8305:
1.187 brouard 8306: /* Design
8307: * V1 V2 V3 V4 V5 V6 V7 V8 V9 Weight
8308: * < ncovcol=8 >
8309: * Model V2 + V1 + V3*age + V3 + V5*V6 + V7*V8 + V8*age + V8
8310: * k= 1 2 3 4 5 6 7 8
8311: * cptcovn number of covariates (not including constant and age ) = # of + plus 1 = 7+1=8
8312: * covar[k,i], value of kth covariate if not including age for individual i:
1.224 brouard 8313: * covar[1][i]= (V1), covar[4][i]=(V4), covar[8][i]=(V8)
8314: * Tvar[k] # of the kth covariate: Tvar[1]=2 Tvar[2]=1 Tvar[4]=3 Tvar[8]=8
1.187 brouard 8315: * if multiplied by age: V3*age Tvar[3=V3*age]=3 (V3) Tvar[7]=8 and
8316: * Tage[++cptcovage]=k
8317: * if products, new covar are created after ncovcol with k1
8318: * Tvar[k]=ncovcol+k1; # of the kth covariate product: Tvar[5]=ncovcol+1=10 Tvar[6]=ncovcol+1=11
8319: * Tprod[k1]=k; Tprod[1]=5 Tprod[2]= 6; gives the position of the k1th product
8320: * 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
8321: * Tvar[cptcovn+k2]=Tvard[k1][1];Tvar[cptcovn+k2+1]=Tvard[k1][2];
8322: * Tvar[8+1]=5;Tvar[8+2]=6;Tvar[8+3]=7;Tvar[8+4]=8 inverted
8323: * V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11
8324: * < ncovcol=8 >
8325: * Model V2 + V1 + V3*age + V3 + V5*V6 + V7*V8 + V8*age + V8 d1 d1 d2 d2
8326: * k= 1 2 3 4 5 6 7 8 9 10 11 12
8327: * Tvar[k]= 2 1 3 3 10 11 8 8 5 6 7 8
8328: * p Tvar[1]@12={2, 1, 3, 3, 11, 10, 8, 8, 7, 8, 5, 6}
8329: * p Tprod[1]@2={ 6, 5}
8330: *p Tvard[1][1]@4= {7, 8, 5, 6}
8331: * covar[k][i]= V2 V1 ? V3 V5*V6? V7*V8? ? V8
8332: * cov[Tage[kk]+2]=covar[Tvar[Tage[kk]]][i]*cov[2];
8333: *How to reorganize?
8334: * Model V1 + V2 + V3 + V8 + V5*V6 + V7*V8 + V3*age + V8*age
8335: * Tvars {2, 1, 3, 3, 11, 10, 8, 8, 7, 8, 5, 6}
8336: * {2, 1, 4, 8, 5, 6, 3, 7}
8337: * Struct []
8338: */
1.225 brouard 8339:
1.187 brouard 8340: /* This loop fills the array Tvar from the string 'model'.*/
8341: /* j is the number of + signs in the model V1+V2+V3 j=2 i=3 to 1 */
8342: /* modelsav=V2+V1+V4+age*V3 strb=age*V3 stra=V2+V1+V4 */
8343: /* k=4 (age*V3) Tvar[k=4]= 3 (from V3) Tage[cptcovage=1]=4 */
8344: /* k=3 V4 Tvar[k=3]= 4 (from V4) */
8345: /* k=2 V1 Tvar[k=2]= 1 (from V1) */
8346: /* k=1 Tvar[1]=2 (from V2) */
8347: /* k=5 Tvar[5] */
8348: /* for (k=1; k<=cptcovn;k++) { */
1.198 brouard 8349: /* cov[2+k]=nbcode[Tvar[k]][codtabm(ij,Tvar[k])]; */
1.187 brouard 8350: /* } */
1.198 brouard 8351: /* for (k=1; k<=cptcovage;k++) cov[2+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,Tvar[Tage[k])]]*cov[2]; */
1.187 brouard 8352: /*
8353: * Treating invertedly V2+V1+V3*age+V2*V4 is as if written V2*V4 +V3*age + V1 + V2 */
1.227 brouard 8354: for(k=cptcovt; k>=1;k--){ /**< Number of covariates not including constant and age, neither age*age*/
8355: Tvar[k]=0; Tprod[k]=0; Tposprod[k]=0;
8356: }
1.187 brouard 8357: cptcovage=0;
8358: for(k=1; k<=cptcovt;k++){ /* Loop on total covariates of the model */
1.234 brouard 8359: cutl(stra,strb,modelsav,'+'); /* keeps in strb after the first '+'
1.225 brouard 8360: modelsav==V2+V1+V4+V3*age strb=V3*age stra=V2+V1+V4 */
1.234 brouard 8361: if (nbocc(modelsav,'+')==0) strcpy(strb,modelsav); /* and analyzes it */
8362: /* printf("i=%d a=%s b=%s sav=%s\n",i, stra,strb,modelsav);*/
8363: /*scanf("%d",i);*/
8364: if (strchr(strb,'*')) { /**< Model includes a product V2+V1+V4+V3*age strb=V3*age */
8365: cutl(strc,strd,strb,'*'); /**< strd*strc Vm*Vn: strb=V3*age(input) strc=age strd=V3 ; V3*V2 strc=V2, strd=V3 */
8366: if (strcmp(strc,"age")==0) { /**< Model includes age: Vn*age */
8367: /* covar is not filled and then is empty */
8368: cptcovprod--;
8369: cutl(stre,strb,strd,'V'); /* strd=V3(input): stre="3" */
8370: Tvar[k]=atoi(stre); /* V2+V1+V4+V3*age Tvar[4]=3 ; V1+V2*age Tvar[2]=2; V1+V1*age Tvar[2]=1 */
8371: Typevar[k]=1; /* 1 for age product */
8372: cptcovage++; /* Sums the number of covariates which include age as a product */
8373: Tage[cptcovage]=k; /* Tvar[4]=3, Tage[1] = 4 or V1+V1*age Tvar[2]=1, Tage[1]=2 */
8374: /*printf("stre=%s ", stre);*/
8375: } else if (strcmp(strd,"age")==0) { /* or age*Vn */
8376: cptcovprod--;
8377: cutl(stre,strb,strc,'V');
8378: Tvar[k]=atoi(stre);
8379: Typevar[k]=1; /* 1 for age product */
8380: cptcovage++;
8381: Tage[cptcovage]=k;
8382: } else { /* Age is not in the model product V2+V1+V1*V4+V3*age+V3*V2 strb=V3*V2*/
8383: /* loops on k1=1 (V3*V2) and k1=2 V4*V3 */
8384: cptcovn++;
8385: cptcovprodnoage++;k1++;
8386: cutl(stre,strb,strc,'V'); /* strc= Vn, stre is n; strb=V3*V2 stre=3 strc=*/
8387: Tvar[k]=ncovcol+nqv+ntv+nqtv+k1; /* For model-covariate k tells which data-covariate to use but
8388: because this model-covariate is a construction we invent a new column
8389: which is after existing variables ncovcol+nqv+ntv+nqtv + k1
8390: If already ncovcol=4 and model=V2+V1+V1*V4+age*V3+V3*V2
8391: Tvar[3=V1*V4]=4+1 Tvar[5=V3*V2]=4 + 2= 6, etc */
8392: Typevar[k]=2; /* 2 for double fixed dummy covariates */
8393: cutl(strc,strb,strd,'V'); /* strd was Vm, strc is m */
8394: Tprod[k1]=k; /* Tprod[1]=3(=V1*V4) for V2+V1+V1*V4+age*V3+V3*V2 */
8395: Tposprod[k]=k1; /* Tpsprod[3]=1, Tposprod[2]=5 */
8396: Tvard[k1][1] =atoi(strc); /* m 1 for V1*/
8397: Tvard[k1][2] =atoi(stre); /* n 4 for V4*/
8398: k2=k2+2; /* k2 is initialize to -1, We want to store the n and m in Vn*Vm at the end of Tvar */
8399: /* Tvar[cptcovt+k2]=Tvard[k1][1]; /\* Tvar[(cptcovt=4+k2=1)=5]= 1 (V1) *\/ */
8400: /* Tvar[cptcovt+k2+1]=Tvard[k1][2]; /\* Tvar[(cptcovt=4+(k2=1)+1)=6]= 4 (V4) *\/ */
1.225 brouard 8401: /*ncovcol=4 and model=V2+V1+V1*V4+age*V3+V3*V2, Tvar[3]=5, Tvar[4]=6, cptcovt=5 */
1.234 brouard 8402: /* 1 2 3 4 5 | Tvar[5+1)=1, Tvar[7]=2 */
8403: for (i=1; i<=lastobs;i++){
8404: /* Computes the new covariate which is a product of
8405: covar[n][i]* covar[m][i] and stores it at ncovol+k1 May not be defined */
8406: covar[ncovcol+k1][i]=covar[atoi(stre)][i]*covar[atoi(strc)][i];
8407: }
8408: } /* End age is not in the model */
8409: } /* End if model includes a product */
8410: else { /* no more sum */
8411: /*printf("d=%s c=%s b=%s\n", strd,strc,strb);*/
8412: /* scanf("%d",i);*/
8413: cutl(strd,strc,strb,'V');
8414: ks++; /**< Number of simple covariates dummy or quantitative, fixe or varying */
8415: cptcovn++; /** V4+V3+V5: V4 and V3 timevarying dummy covariates, V5 timevarying quantitative */
8416: Tvar[k]=atoi(strd);
8417: Typevar[k]=0; /* 0 for simple covariates */
8418: }
8419: strcpy(modelsav,stra); /* modelsav=V2+V1+V4 stra=V2+V1+V4 */
1.223 brouard 8420: /*printf("a=%s b=%s sav=%s\n", stra,strb,modelsav);
1.225 brouard 8421: scanf("%d",i);*/
1.187 brouard 8422: } /* end of loop + on total covariates */
8423: } /* end if strlen(modelsave == 0) age*age might exist */
8424: } /* end if strlen(model == 0) */
1.136 brouard 8425:
8426: /*The number n of Vn is stored in Tvar. cptcovage =number of age covariate. Tage gives the position of age. cptcovprod= number of products.
8427: If model=V1+V1*age then Tvar[1]=1 Tvar[2]=1 cptcovage=1 Tage[1]=2 cptcovprod=0*/
1.225 brouard 8428:
1.136 brouard 8429: /* printf("tvar1=%d tvar2=%d tvar3=%d cptcovage=%d Tage=%d",Tvar[1],Tvar[2],Tvar[3],cptcovage,Tage[1]);
1.225 brouard 8430: printf("cptcovprod=%d ", cptcovprod);
8431: fprintf(ficlog,"cptcovprod=%d ", cptcovprod);
8432: scanf("%d ",i);*/
8433:
8434:
1.230 brouard 8435: /* Until here, decodemodel knows only the grammar (simple, product, age*) of the model but not what kind
8436: of variable (dummy vs quantitative, fixed vs time varying) is behind. But we know the # of each. */
1.226 brouard 8437: /* ncovcol= 1, nqv=1 | ntv=2, nqtv= 1 = 5 possible variables data: 2 fixed 3, varying
8438: model= V5 + V4 +V3 + V4*V3 + V5*age + V2 + V1*V2 + V1*age + V5*age, V1 is not used saving its place
8439: k = 1 2 3 4 5 6 7 8 9
8440: Tvar[k]= 5 4 3 1+1+2+1+1=6 5 2 7 1 5
8441: Typevar[k]= 0 0 0 2 1 0 2 1 1
1.227 brouard 8442: Fixed[k] 1 1 1 1 3 0 0 or 2 2 3
8443: Dummy[k] 1 0 0 0 3 1 1 2 3
8444: Tmodelind[combination of covar]=k;
1.225 brouard 8445: */
8446: /* Dispatching between quantitative and time varying covariates */
1.226 brouard 8447: /* If Tvar[k] >ncovcol it is a product */
1.225 brouard 8448: /* Tvar[k] is the value n of Vn with n varying for 1 to nvcol, or p Vp=Vn*Vm for product */
1.226 brouard 8449: /* Computing effective variables, ie used by the model, that is from the cptcovt variables */
1.227 brouard 8450: printf("Model=%s\n\
8451: Typevar: 0 for simple covariate (dummy, quantitative, fixed or varying), 1 for age product, 2 for product \n\
8452: Fixed[k] 0=fixed (product or simple), 1 varying, 2 fixed with age product, 3 varying with age product \n\
8453: Dummy[k] 0=dummy (0 1), 1 quantitative (single or product without age), 2 dummy with age product, 3 quant with age product\n",model);
8454: fprintf(ficlog,"Model=%s\n\
8455: Typevar: 0 for simple covariate (dummy, quantitative, fixed or varying), 1 for age product, 2 for product \n\
8456: Fixed[k] 0=fixed (product or simple), 1 varying, 2 fixed with age product, 3 varying with age product \n\
8457: Dummy[k] 0=dummy (0 1), 1 quantitative (single or product without age), 2 dummy with age product, 3 quant with age product\n",model);
8458:
1.238 brouard 8459: for(v=1; v <=ncovcol;v++){
8460: DummyV[v]=0;
8461: FixedV[v]=0;
8462: }
8463: for(v=ncovcol+1; v <=ncovcol+nqv;v++){
8464: DummyV[v]=1;
8465: FixedV[v]=0;
8466: }
8467: for(v=ncovcol+nqv+1; v <=ncovcol+nqv+ntv;v++){
8468: DummyV[v]=0;
8469: FixedV[v]=1;
8470: }
8471: for(v=ncovcol+nqv+ntv+1; v <=ncovcol+nqv+ntv+nqtv;v++){
8472: DummyV[v]=1;
8473: FixedV[v]=1;
8474: }
8475: for(v=1; v <=ncovcol+nqv+ntv+nqtv;v++){
8476: printf("Decodemodel: V%d, Dummy(V%d)=%d, FixedV(V%d)=%d\n",v,v,DummyV[v],v,FixedV[v]);
8477: fprintf(ficlog,"Decodemodel: V%d, Dummy(V%d)=%d, FixedV(V%d)=%d\n",v,v,DummyV[v],v,FixedV[v]);
8478: }
1.234 brouard 8479: for(k=1, ncovf=0, nsd=0, nsq=0, ncovv=0, ncova=0, ncoveff=0, nqfveff=0, ntveff=0, nqtveff=0;k<=cptcovt; k++){ /* or cptocvt */
8480: if (Tvar[k] <=ncovcol && Typevar[k]==0 ){ /* Simple fixed dummy (<=ncovcol) covariates */
1.227 brouard 8481: Fixed[k]= 0;
8482: Dummy[k]= 0;
1.225 brouard 8483: ncoveff++;
1.232 brouard 8484: ncovf++;
1.234 brouard 8485: nsd++;
8486: modell[k].maintype= FTYPE;
8487: TvarsD[nsd]=Tvar[k];
8488: TvarsDind[nsd]=k;
8489: TvarF[ncovf]=Tvar[k];
8490: TvarFind[ncovf]=k;
8491: TvarFD[ncoveff]=Tvar[k]; /* TvarFD[1]=V1 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
8492: TvarFDind[ncoveff]=k; /* TvarFDind[1]=9 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
8493: }else if( Tvar[k] <=ncovcol && Typevar[k]==2){ /* Product of fixed dummy (<=ncovcol) covariates */
8494: Fixed[k]= 0;
8495: Dummy[k]= 0;
8496: ncoveff++;
8497: ncovf++;
8498: modell[k].maintype= FTYPE;
8499: TvarF[ncovf]=Tvar[k];
8500: TvarFind[ncovf]=k;
1.230 brouard 8501: TvarFD[ncoveff]=Tvar[k]; /* TvarFD[1]=V1 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
1.231 brouard 8502: TvarFDind[ncoveff]=k; /* TvarFDind[1]=9 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
8503: }else if( Tvar[k] <=ncovcol+nqv && Typevar[k]==0){ /* Remind that product Vn*Vm are added in k*/ /* Only simple fixed quantitative variable */
1.227 brouard 8504: Fixed[k]= 0;
8505: Dummy[k]= 1;
1.230 brouard 8506: nqfveff++;
1.234 brouard 8507: modell[k].maintype= FTYPE;
8508: modell[k].subtype= FQ;
8509: nsq++;
8510: TvarsQ[nsq]=Tvar[k];
8511: TvarsQind[nsq]=k;
1.232 brouard 8512: ncovf++;
1.234 brouard 8513: TvarF[ncovf]=Tvar[k];
8514: TvarFind[ncovf]=k;
1.231 brouard 8515: TvarFQ[nqfveff]=Tvar[k]-ncovcol; /* TvarFQ[1]=V2-1=1st in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ /* Only simple fixed quantitative variable */
1.230 brouard 8516: TvarFQind[nqfveff]=k; /* TvarFQind[1]=6 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ /* Only simple fixed quantitative variable */
1.234 brouard 8517: }else if( Tvar[k] <=ncovcol+nqv+ntv && Typevar[k]==0){/* Only simple time varying variables */
1.227 brouard 8518: Fixed[k]= 1;
8519: Dummy[k]= 0;
1.225 brouard 8520: ntveff++; /* Only simple time varying dummy variable */
1.234 brouard 8521: modell[k].maintype= VTYPE;
8522: modell[k].subtype= VD;
8523: nsd++;
8524: TvarsD[nsd]=Tvar[k];
8525: TvarsDind[nsd]=k;
8526: ncovv++; /* Only simple time varying variables */
8527: TvarV[ncovv]=Tvar[k];
8528: TvarVind[ncovv]=k;
1.231 brouard 8529: TvarVD[ntveff]=Tvar[k]; /* TvarVD[1]=V4 TvarVD[2]=V3 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ /* Only simple time varying dummy variable */
8530: TvarVDind[ntveff]=k; /* TvarVDind[1]=2 TvarVDind[2]=3 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ /* Only simple time varying dummy variable */
1.228 brouard 8531: printf("Quasi Tmodelind[%d]=%d,Tvar[Tmodelind[%d]]=V%d, ncovcol=%d, nqv=%d,Tvar[k]- ncovcol-nqv=%d\n",ntveff,k,ntveff,Tvar[k], ncovcol, nqv,Tvar[k]- ncovcol-nqv);
8532: printf("Quasi TmodelInvind[%d]=%d\n",k,Tvar[k]- ncovcol-nqv);
1.231 brouard 8533: }else if( Tvar[k] <=ncovcol+nqv+ntv+nqtv && Typevar[k]==0){ /* Only simple time varying quantitative variable V5*/
1.234 brouard 8534: Fixed[k]= 1;
8535: Dummy[k]= 1;
8536: nqtveff++;
8537: modell[k].maintype= VTYPE;
8538: modell[k].subtype= VQ;
8539: ncovv++; /* Only simple time varying variables */
8540: nsq++;
8541: TvarsQ[nsq]=Tvar[k];
8542: TvarsQind[nsq]=k;
8543: TvarV[ncovv]=Tvar[k];
8544: TvarVind[ncovv]=k;
1.231 brouard 8545: TvarVQ[nqtveff]=Tvar[k]; /* TvarVQ[1]=V5 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ /* Only simple time varying quantitative variable */
8546: TvarVQind[nqtveff]=k; /* TvarVQind[1]=1 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ /* Only simple time varying quantitative variable */
1.234 brouard 8547: TmodelInvQind[nqtveff]=Tvar[k]- ncovcol-nqv-ntv;/* Only simple time varying quantitative variable */
8548: /* Tmodeliqind[k]=nqtveff;/\* Only simple time varying quantitative variable *\/ */
8549: printf("Quasi TmodelQind[%d]=%d,Tvar[TmodelQind[%d]]=V%d, ncovcol=%d, nqv=%d, ntv=%d,Tvar[k]- ncovcol-nqv-ntv=%d\n",nqtveff,k,nqtveff,Tvar[k], ncovcol, nqv, ntv, Tvar[k]- ncovcol-nqv-ntv);
1.228 brouard 8550: printf("Quasi TmodelInvQind[%d]=%d\n",k,Tvar[k]- ncovcol-nqv-ntv);
1.227 brouard 8551: }else if (Typevar[k] == 1) { /* product with age */
1.234 brouard 8552: ncova++;
8553: TvarA[ncova]=Tvar[k];
8554: TvarAind[ncova]=k;
1.231 brouard 8555: if (Tvar[k] <=ncovcol ){ /* Product age with fixed dummy covariatee */
1.234 brouard 8556: Fixed[k]= 2;
8557: Dummy[k]= 2;
8558: modell[k].maintype= ATYPE;
8559: modell[k].subtype= APFD;
8560: /* ncoveff++; */
1.227 brouard 8561: }else if( Tvar[k] <=ncovcol+nqv) { /* Remind that product Vn*Vm are added in k*/
1.234 brouard 8562: Fixed[k]= 2;
8563: Dummy[k]= 3;
8564: modell[k].maintype= ATYPE;
8565: modell[k].subtype= APFQ; /* Product age * fixed quantitative */
8566: /* nqfveff++; /\* Only simple fixed quantitative variable *\/ */
1.227 brouard 8567: }else if( Tvar[k] <=ncovcol+nqv+ntv ){
1.234 brouard 8568: Fixed[k]= 3;
8569: Dummy[k]= 2;
8570: modell[k].maintype= ATYPE;
8571: modell[k].subtype= APVD; /* Product age * varying dummy */
8572: /* ntveff++; /\* Only simple time varying dummy variable *\/ */
1.227 brouard 8573: }else if( Tvar[k] <=ncovcol+nqv+ntv+nqtv){
1.234 brouard 8574: Fixed[k]= 3;
8575: Dummy[k]= 3;
8576: modell[k].maintype= ATYPE;
8577: modell[k].subtype= APVQ; /* Product age * varying quantitative */
8578: /* nqtveff++;/\* Only simple time varying quantitative variable *\/ */
1.227 brouard 8579: }
8580: }else if (Typevar[k] == 2) { /* product without age */
8581: k1=Tposprod[k];
8582: if(Tvard[k1][1] <=ncovcol){
1.234 brouard 8583: if(Tvard[k1][2] <=ncovcol){
8584: Fixed[k]= 1;
8585: Dummy[k]= 0;
8586: modell[k].maintype= FTYPE;
8587: modell[k].subtype= FPDD; /* Product fixed dummy * fixed dummy */
8588: ncovf++; /* Fixed variables without age */
8589: TvarF[ncovf]=Tvar[k];
8590: TvarFind[ncovf]=k;
8591: }else if(Tvard[k1][2] <=ncovcol+nqv){
8592: Fixed[k]= 0; /* or 2 ?*/
8593: Dummy[k]= 1;
8594: modell[k].maintype= FTYPE;
8595: modell[k].subtype= FPDQ; /* Product fixed dummy * fixed quantitative */
8596: ncovf++; /* Varying variables without age */
8597: TvarF[ncovf]=Tvar[k];
8598: TvarFind[ncovf]=k;
8599: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv){
8600: Fixed[k]= 1;
8601: Dummy[k]= 0;
8602: modell[k].maintype= VTYPE;
8603: modell[k].subtype= VPDD; /* Product fixed dummy * varying dummy */
8604: ncovv++; /* Varying variables without age */
8605: TvarV[ncovv]=Tvar[k];
8606: TvarVind[ncovv]=k;
8607: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv+nqtv){
8608: Fixed[k]= 1;
8609: Dummy[k]= 1;
8610: modell[k].maintype= VTYPE;
8611: modell[k].subtype= VPDQ; /* Product fixed dummy * varying quantitative */
8612: ncovv++; /* Varying variables without age */
8613: TvarV[ncovv]=Tvar[k];
8614: TvarVind[ncovv]=k;
8615: }
1.227 brouard 8616: }else if(Tvard[k1][1] <=ncovcol+nqv){
1.234 brouard 8617: if(Tvard[k1][2] <=ncovcol){
8618: Fixed[k]= 0; /* or 2 ?*/
8619: Dummy[k]= 1;
8620: modell[k].maintype= FTYPE;
8621: modell[k].subtype= FPDQ; /* Product fixed quantitative * fixed dummy */
8622: ncovf++; /* Fixed variables without age */
8623: TvarF[ncovf]=Tvar[k];
8624: TvarFind[ncovf]=k;
8625: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv){
8626: Fixed[k]= 1;
8627: Dummy[k]= 1;
8628: modell[k].maintype= VTYPE;
8629: modell[k].subtype= VPDQ; /* Product fixed quantitative * varying dummy */
8630: ncovv++; /* Varying variables without age */
8631: TvarV[ncovv]=Tvar[k];
8632: TvarVind[ncovv]=k;
8633: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv+nqtv){
8634: Fixed[k]= 1;
8635: Dummy[k]= 1;
8636: modell[k].maintype= VTYPE;
8637: modell[k].subtype= VPQQ; /* Product fixed quantitative * varying quantitative */
8638: ncovv++; /* Varying variables without age */
8639: TvarV[ncovv]=Tvar[k];
8640: TvarVind[ncovv]=k;
8641: ncovv++; /* Varying variables without age */
8642: TvarV[ncovv]=Tvar[k];
8643: TvarVind[ncovv]=k;
8644: }
1.227 brouard 8645: }else if(Tvard[k1][1] <=ncovcol+nqv+ntv){
1.234 brouard 8646: if(Tvard[k1][2] <=ncovcol){
8647: Fixed[k]= 1;
8648: Dummy[k]= 1;
8649: modell[k].maintype= VTYPE;
8650: modell[k].subtype= VPDD; /* Product time varying dummy * fixed dummy */
8651: ncovv++; /* Varying variables without age */
8652: TvarV[ncovv]=Tvar[k];
8653: TvarVind[ncovv]=k;
8654: }else if(Tvard[k1][2] <=ncovcol+nqv){
8655: Fixed[k]= 1;
8656: Dummy[k]= 1;
8657: modell[k].maintype= VTYPE;
8658: modell[k].subtype= VPDQ; /* Product time varying dummy * fixed quantitative */
8659: ncovv++; /* Varying variables without age */
8660: TvarV[ncovv]=Tvar[k];
8661: TvarVind[ncovv]=k;
8662: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv){
8663: Fixed[k]= 1;
8664: Dummy[k]= 0;
8665: modell[k].maintype= VTYPE;
8666: modell[k].subtype= VPDD; /* Product time varying dummy * time varying dummy */
8667: ncovv++; /* Varying variables without age */
8668: TvarV[ncovv]=Tvar[k];
8669: TvarVind[ncovv]=k;
8670: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv+nqtv){
8671: Fixed[k]= 1;
8672: Dummy[k]= 1;
8673: modell[k].maintype= VTYPE;
8674: modell[k].subtype= VPDQ; /* Product time varying dummy * time varying quantitative */
8675: ncovv++; /* Varying variables without age */
8676: TvarV[ncovv]=Tvar[k];
8677: TvarVind[ncovv]=k;
8678: }
1.227 brouard 8679: }else if(Tvard[k1][1] <=ncovcol+nqv+ntv+nqtv){
1.234 brouard 8680: if(Tvard[k1][2] <=ncovcol){
8681: Fixed[k]= 1;
8682: Dummy[k]= 1;
8683: modell[k].maintype= VTYPE;
8684: modell[k].subtype= VPDQ; /* Product time varying quantitative * fixed dummy */
8685: ncovv++; /* Varying variables without age */
8686: TvarV[ncovv]=Tvar[k];
8687: TvarVind[ncovv]=k;
8688: }else if(Tvard[k1][2] <=ncovcol+nqv){
8689: Fixed[k]= 1;
8690: Dummy[k]= 1;
8691: modell[k].maintype= VTYPE;
8692: modell[k].subtype= VPQQ; /* Product time varying quantitative * fixed quantitative */
8693: ncovv++; /* Varying variables without age */
8694: TvarV[ncovv]=Tvar[k];
8695: TvarVind[ncovv]=k;
8696: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv){
8697: Fixed[k]= 1;
8698: Dummy[k]= 1;
8699: modell[k].maintype= VTYPE;
8700: modell[k].subtype= VPDQ; /* Product time varying quantitative * time varying dummy */
8701: ncovv++; /* Varying variables without age */
8702: TvarV[ncovv]=Tvar[k];
8703: TvarVind[ncovv]=k;
8704: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv+nqtv){
8705: Fixed[k]= 1;
8706: Dummy[k]= 1;
8707: modell[k].maintype= VTYPE;
8708: modell[k].subtype= VPQQ; /* Product time varying quantitative * time varying quantitative */
8709: ncovv++; /* Varying variables without age */
8710: TvarV[ncovv]=Tvar[k];
8711: TvarVind[ncovv]=k;
8712: }
1.227 brouard 8713: }else{
1.234 brouard 8714: printf("Error unknown type of covariate: Tvard[%d][1]=%d,Tvard[%d][2]=%d\n",k1,Tvard[k1][1],k1,Tvard[k1][2]);
8715: fprintf(ficlog,"Error unknown type of covariate: Tvard[%d][1]=%d,Tvard[%d][2]=%d\n",k1,Tvard[k1][1],k1,Tvard[k1][2]);
1.226 brouard 8716: } /* end k1 */
1.225 brouard 8717: }else{
1.226 brouard 8718: printf("Error, current version can't treat for performance reasons, Tvar[%d]=%d, Typevar[%d]=%d\n", k, Tvar[k], k, Typevar[k]);
8719: fprintf(ficlog,"Error, current version can't treat for performance reasons, Tvar[%d]=%d, Typevar[%d]=%d\n", k, Tvar[k], k, Typevar[k]);
1.225 brouard 8720: }
1.227 brouard 8721: printf("Decodemodel, k=%d, Tvar[%d]=V%d,Typevar=%d, Fixed=%d, Dummy=%d\n",k, k,Tvar[k],Typevar[k],Fixed[k],Dummy[k]);
1.231 brouard 8722: printf(" modell[%d].maintype=%d, modell[%d].subtype=%d\n",k,modell[k].maintype,k,modell[k].subtype);
1.227 brouard 8723: fprintf(ficlog,"Decodemodel, k=%d, Tvar[%d]=V%d,Typevar=%d, Fixed=%d, Dummy=%d\n",k, k,Tvar[k],Typevar[k],Fixed[k],Dummy[k]);
8724: }
8725: /* Searching for doublons in the model */
8726: for(k1=1; k1<= cptcovt;k1++){
8727: for(k2=1; k2 <k1;k2++){
8728: if((Typevar[k1]==Typevar[k2]) && (Fixed[Tvar[k1]]==Fixed[Tvar[k2]]) && (Dummy[Tvar[k1]]==Dummy[Tvar[k2]] )){
1.234 brouard 8729: if((Typevar[k1] == 0 || Typevar[k1] == 1)){ /* Simple or age product */
8730: if(Tvar[k1]==Tvar[k2]){
8731: printf("Error duplication in the model=%s at positions (+) %d and %d, Tvar[%d]=V%d, Tvar[%d]=V%d, Typevar=%d, Fixed=%d, Dummy=%d\n", model, k1,k2, k1, Tvar[k1], k2, Tvar[k2],Typevar[k1],Fixed[Tvar[k1]],Dummy[Tvar[k1]]);
8732: fprintf(ficlog,"Error duplication in the model=%s at positions (+) %d and %d, Tvar[%d]=V%d, Tvar[%d]=V%d, Typevar=%d, Fixed=%d, Dummy=%d\n", model, k1,k2, k1, Tvar[k1], k2, Tvar[k2],Typevar[k1],Fixed[Tvar[k1]],Dummy[Tvar[k1]]); fflush(ficlog);
8733: return(1);
8734: }
8735: }else if (Typevar[k1] ==2){
8736: k3=Tposprod[k1];
8737: k4=Tposprod[k2];
8738: if( ((Tvard[k3][1]== Tvard[k4][1])&&(Tvard[k3][2]== Tvard[k4][2])) || ((Tvard[k3][1]== Tvard[k4][2])&&(Tvard[k3][2]== Tvard[k4][1])) ){
8739: printf("Error duplication in the model=%s at positions (+) %d and %d, V%d*V%d, Typevar=%d, Fixed=%d, Dummy=%d\n",model, k1,k2, Tvard[k3][1], Tvard[k3][2],Typevar[k1],Fixed[Tvar[k1]],Dummy[Tvar[k1]]);
8740: fprintf(ficlog,"Error duplication in the model=%s at positions (+) %d and %d, V%d*V%d, Typevar=%d, Fixed=%d, Dummy=%d\n",model, k1,k2, Tvard[k3][1], Tvard[k3][2],Typevar[k1],Fixed[Tvar[k1]],Dummy[Tvar[k1]]); fflush(ficlog);
8741: return(1);
8742: }
8743: }
1.227 brouard 8744: }
8745: }
1.225 brouard 8746: }
8747: printf("ncoveff=%d, nqfveff=%d, ntveff=%d, nqtveff=%d, cptcovn=%d\n",ncoveff,nqfveff,ntveff,nqtveff,cptcovn);
8748: fprintf(ficlog,"ncoveff=%d, nqfveff=%d, ntveff=%d, nqtveff=%d, cptcovn=%d\n",ncoveff,nqfveff,ntveff,nqtveff,cptcovn);
1.234 brouard 8749: printf("ncovf=%d, ncovv=%d, ncova=%d, nsd=%d, nsq=%d\n",ncovf,ncovv,ncova,nsd,nsq);
8750: fprintf(ficlog,"ncovf=%d, ncovv=%d, ncova=%d, nsd=%d, nsq=%d\n",ncovf,ncovv,ncova,nsd, nsq);
1.137 brouard 8751: return (0); /* with covar[new additional covariate if product] and Tage if age */
1.164 brouard 8752: /*endread:*/
1.225 brouard 8753: printf("Exiting decodemodel: ");
8754: return (1);
1.136 brouard 8755: }
8756:
1.169 brouard 8757: int calandcheckages(int imx, int maxwav, double *agemin, double *agemax, int *nberr, int *nbwarn )
1.136 brouard 8758: {
8759: int i, m;
1.218 brouard 8760: int firstone=0;
8761:
1.136 brouard 8762: for (i=1; i<=imx; i++) {
8763: for(m=2; (m<= maxwav); m++) {
8764: if (((int)mint[m][i]== 99) && (s[m][i] <= nlstate)){
8765: anint[m][i]=9999;
1.216 brouard 8766: if (s[m][i] != -2) /* Keeping initial status of unknown vital status */
8767: s[m][i]=-1;
1.136 brouard 8768: }
8769: if((int)moisdc[i]==99 && (int)andc[i]==9999 && s[m][i]>nlstate){
1.169 brouard 8770: *nberr = *nberr + 1;
1.218 brouard 8771: if(firstone == 0){
8772: firstone=1;
8773: 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);
8774: }
8775: 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 8776: s[m][i]=-1;
8777: }
8778: if((int)moisdc[i]==99 && (int)andc[i]!=9999 && s[m][i]>nlstate){
1.169 brouard 8779: (*nberr)++;
1.136 brouard 8780: 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]);
8781: 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]);
8782: s[m][i]=-1; /* We prefer to skip it (and to skip it in version 0.8a1 too */
8783: }
8784: }
8785: }
8786:
8787: for (i=1; i<=imx; i++) {
8788: agedc[i]=(moisdc[i]/12.+andc[i])-(moisnais[i]/12.+annais[i]);
8789: for(m=firstpass; (m<= lastpass); m++){
1.214 brouard 8790: 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 8791: if (s[m][i] >= nlstate+1) {
1.169 brouard 8792: if(agedc[i]>0){
8793: if((int)moisdc[i]!=99 && (int)andc[i]!=9999){
1.136 brouard 8794: agev[m][i]=agedc[i];
1.214 brouard 8795: /*if(moisdc[i]==99 && andc[i]==9999) s[m][i]=-1;*/
1.169 brouard 8796: }else {
1.136 brouard 8797: if ((int)andc[i]!=9999){
8798: nbwarn++;
8799: printf("Warning negative age at death: %ld line:%d\n",num[i],i);
8800: fprintf(ficlog,"Warning negative age at death: %ld line:%d\n",num[i],i);
8801: agev[m][i]=-1;
8802: }
8803: }
1.169 brouard 8804: } /* agedc > 0 */
1.214 brouard 8805: } /* end if */
1.136 brouard 8806: else if(s[m][i] !=9){ /* Standard case, age in fractional
8807: years but with the precision of a month */
8808: agev[m][i]=(mint[m][i]/12.+1./24.+anint[m][i])-(moisnais[i]/12.+1./24.+annais[i]);
8809: if((int)mint[m][i]==99 || (int)anint[m][i]==9999)
8810: agev[m][i]=1;
8811: else if(agev[m][i] < *agemin){
8812: *agemin=agev[m][i];
8813: printf(" Min anint[%d][%d]=%.2f annais[%d]=%.2f, agemin=%.2f\n",m,i,anint[m][i], i,annais[i], *agemin);
8814: }
8815: else if(agev[m][i] >*agemax){
8816: *agemax=agev[m][i];
1.156 brouard 8817: /* printf(" Max anint[%d][%d]=%.0f annais[%d]=%.0f, agemax=%.2f\n",m,i,anint[m][i], i,annais[i], *agemax);*/
1.136 brouard 8818: }
8819: /*agev[m][i]=anint[m][i]-annais[i];*/
8820: /* agev[m][i] = age[i]+2*m;*/
1.214 brouard 8821: } /* en if 9*/
1.136 brouard 8822: else { /* =9 */
1.214 brouard 8823: /* printf("Debug num[%d]=%ld s[%d][%d]=%d\n",i,num[i], m,i, s[m][i]); */
1.136 brouard 8824: agev[m][i]=1;
8825: s[m][i]=-1;
8826: }
8827: }
1.214 brouard 8828: else if(s[m][i]==0) /*= 0 Unknown */
1.136 brouard 8829: agev[m][i]=1;
1.214 brouard 8830: else{
8831: printf("Warning, num[%d]=%ld, s[%d][%d]=%d\n", i, num[i], m, i,s[m][i]);
8832: fprintf(ficlog, "Warning, num[%d]=%ld, s[%d][%d]=%d\n", i, num[i], m, i,s[m][i]);
8833: agev[m][i]=0;
8834: }
8835: } /* End for lastpass */
8836: }
1.136 brouard 8837:
8838: for (i=1; i<=imx; i++) {
8839: for(m=firstpass; (m<=lastpass); m++){
8840: if (s[m][i] > (nlstate+ndeath)) {
1.169 brouard 8841: (*nberr)++;
1.136 brouard 8842: 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);
8843: 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);
8844: return 1;
8845: }
8846: }
8847: }
8848:
8849: /*for (i=1; i<=imx; i++){
8850: for (m=firstpass; (m<lastpass); m++){
8851: printf("%ld %d %.lf %d %d\n", num[i],(covar[1][i]),agev[m][i],s[m][i],s[m+1][i]);
8852: }
8853:
8854: }*/
8855:
8856:
1.139 brouard 8857: printf("Total number of individuals= %d, Agemin = %.2f, Agemax= %.2f\n\n", imx, *agemin, *agemax);
8858: fprintf(ficlog,"Total number of individuals= %d, Agemin = %.2f, Agemax= %.2f\n\n", imx, *agemin, *agemax);
1.136 brouard 8859:
8860: return (0);
1.164 brouard 8861: /* endread:*/
1.136 brouard 8862: printf("Exiting calandcheckages: ");
8863: return (1);
8864: }
8865:
1.172 brouard 8866: #if defined(_MSC_VER)
8867: /*printf("Visual C++ compiler: %s \n;", _MSC_FULL_VER);*/
8868: /*fprintf(ficlog, "Visual C++ compiler: %s \n;", _MSC_FULL_VER);*/
8869: //#include "stdafx.h"
8870: //#include <stdio.h>
8871: //#include <tchar.h>
8872: //#include <windows.h>
8873: //#include <iostream>
8874: typedef BOOL(WINAPI *LPFN_ISWOW64PROCESS) (HANDLE, PBOOL);
8875:
8876: LPFN_ISWOW64PROCESS fnIsWow64Process;
8877:
8878: BOOL IsWow64()
8879: {
8880: BOOL bIsWow64 = FALSE;
8881:
8882: //typedef BOOL (APIENTRY *LPFN_ISWOW64PROCESS)
8883: // (HANDLE, PBOOL);
8884:
8885: //LPFN_ISWOW64PROCESS fnIsWow64Process;
8886:
8887: HMODULE module = GetModuleHandle(_T("kernel32"));
8888: const char funcName[] = "IsWow64Process";
8889: fnIsWow64Process = (LPFN_ISWOW64PROCESS)
8890: GetProcAddress(module, funcName);
8891:
8892: if (NULL != fnIsWow64Process)
8893: {
8894: if (!fnIsWow64Process(GetCurrentProcess(),
8895: &bIsWow64))
8896: //throw std::exception("Unknown error");
8897: printf("Unknown error\n");
8898: }
8899: return bIsWow64 != FALSE;
8900: }
8901: #endif
1.177 brouard 8902:
1.191 brouard 8903: void syscompilerinfo(int logged)
1.167 brouard 8904: {
8905: /* #include "syscompilerinfo.h"*/
1.185 brouard 8906: /* command line Intel compiler 32bit windows, XP compatible:*/
8907: /* /GS /W3 /Gy
8908: /Zc:wchar_t /Zi /O2 /Fd"Release\vc120.pdb" /D "WIN32" /D "NDEBUG" /D
8909: "_CONSOLE" /D "_LIB" /D "_USING_V110_SDK71_" /D "_UNICODE" /D
8910: "UNICODE" /Qipo /Zc:forScope /Gd /Oi /MT /Fa"Release\" /EHsc /nologo
1.186 brouard 8911: /Fo"Release\" /Qprof-dir "Release\" /Fp"Release\IMaCh.pch"
8912: */
8913: /* 64 bits */
1.185 brouard 8914: /*
8915: /GS /W3 /Gy
8916: /Zc:wchar_t /Zi /O2 /Fd"x64\Release\vc120.pdb" /D "WIN32" /D "NDEBUG"
8917: /D "_CONSOLE" /D "_LIB" /D "_UNICODE" /D "UNICODE" /Qipo /Zc:forScope
8918: /Oi /MD /Fa"x64\Release\" /EHsc /nologo /Fo"x64\Release\" /Qprof-dir
8919: "x64\Release\" /Fp"x64\Release\IMaCh.pch" */
8920: /* Optimization are useless and O3 is slower than O2 */
8921: /*
8922: /GS /W3 /Gy /Zc:wchar_t /Zi /O3 /Fd"x64\Release\vc120.pdb" /D "WIN32"
8923: /D "NDEBUG" /D "_CONSOLE" /D "_LIB" /D "_UNICODE" /D "UNICODE" /Qipo
8924: /Zc:forScope /Oi /MD /Fa"x64\Release\" /EHsc /nologo /Qparallel
8925: /Fo"x64\Release\" /Qprof-dir "x64\Release\" /Fp"x64\Release\IMaCh.pch"
8926: */
1.186 brouard 8927: /* Link is */ /* /OUT:"visual studio
1.185 brouard 8928: 2013\Projects\IMaCh\Release\IMaCh.exe" /MANIFEST /NXCOMPAT
8929: /PDB:"visual studio
8930: 2013\Projects\IMaCh\Release\IMaCh.pdb" /DYNAMICBASE
8931: "kernel32.lib" "user32.lib" "gdi32.lib" "winspool.lib"
8932: "comdlg32.lib" "advapi32.lib" "shell32.lib" "ole32.lib"
8933: "oleaut32.lib" "uuid.lib" "odbc32.lib" "odbccp32.lib"
8934: /MACHINE:X86 /OPT:REF /SAFESEH /INCREMENTAL:NO
8935: /SUBSYSTEM:CONSOLE",5.01" /MANIFESTUAC:"level='asInvoker'
8936: uiAccess='false'"
8937: /ManifestFile:"Release\IMaCh.exe.intermediate.manifest" /OPT:ICF
8938: /NOLOGO /TLBID:1
8939: */
1.177 brouard 8940: #if defined __INTEL_COMPILER
1.178 brouard 8941: #if defined(__GNUC__)
8942: struct utsname sysInfo; /* For Intel on Linux and OS/X */
8943: #endif
1.177 brouard 8944: #elif defined(__GNUC__)
1.179 brouard 8945: #ifndef __APPLE__
1.174 brouard 8946: #include <gnu/libc-version.h> /* Only on gnu */
1.179 brouard 8947: #endif
1.177 brouard 8948: struct utsname sysInfo;
1.178 brouard 8949: int cross = CROSS;
8950: if (cross){
8951: printf("Cross-");
1.191 brouard 8952: if(logged) fprintf(ficlog, "Cross-");
1.178 brouard 8953: }
1.174 brouard 8954: #endif
8955:
1.171 brouard 8956: #include <stdint.h>
1.178 brouard 8957:
1.191 brouard 8958: printf("Compiled with:");if(logged)fprintf(ficlog,"Compiled with:");
1.169 brouard 8959: #if defined(__clang__)
1.191 brouard 8960: printf(" Clang/LLVM");if(logged)fprintf(ficlog," Clang/LLVM"); /* Clang/LLVM. ---------------------------------------------- */
1.169 brouard 8961: #endif
8962: #if defined(__ICC) || defined(__INTEL_COMPILER)
1.191 brouard 8963: printf(" Intel ICC/ICPC");if(logged)fprintf(ficlog," Intel ICC/ICPC");/* Intel ICC/ICPC. ------------------------------------------ */
1.169 brouard 8964: #endif
8965: #if defined(__GNUC__) || defined(__GNUG__)
1.191 brouard 8966: printf(" GNU GCC/G++");if(logged)fprintf(ficlog," GNU GCC/G++");/* GNU GCC/G++. --------------------------------------------- */
1.169 brouard 8967: #endif
8968: #if defined(__HP_cc) || defined(__HP_aCC)
1.191 brouard 8969: printf(" Hewlett-Packard C/aC++");if(logged)fprintf(fcilog," Hewlett-Packard C/aC++"); /* Hewlett-Packard C/aC++. ---------------------------------- */
1.169 brouard 8970: #endif
8971: #if defined(__IBMC__) || defined(__IBMCPP__)
1.191 brouard 8972: printf(" IBM XL C/C++"); if(logged) fprintf(ficlog," IBM XL C/C++");/* IBM XL C/C++. -------------------------------------------- */
1.169 brouard 8973: #endif
8974: #if defined(_MSC_VER)
1.191 brouard 8975: printf(" Microsoft Visual Studio");if(logged)fprintf(ficlog," Microsoft Visual Studio");/* Microsoft Visual Studio. --------------------------------- */
1.169 brouard 8976: #endif
8977: #if defined(__PGI)
1.191 brouard 8978: printf(" Portland Group PGCC/PGCPP");if(logged) fprintf(ficlog," Portland Group PGCC/PGCPP");/* Portland Group PGCC/PGCPP. ------------------------------- */
1.169 brouard 8979: #endif
8980: #if defined(__SUNPRO_C) || defined(__SUNPRO_CC)
1.191 brouard 8981: printf(" Oracle Solaris Studio");if(logged)fprintf(ficlog," Oracle Solaris Studio\n");/* Oracle Solaris Studio. ----------------------------------- */
1.167 brouard 8982: #endif
1.191 brouard 8983: printf(" for "); if (logged) fprintf(ficlog, " for ");
1.169 brouard 8984:
1.167 brouard 8985: // http://stackoverflow.com/questions/4605842/how-to-identify-platform-compiler-from-preprocessor-macros
8986: #ifdef _WIN32 // note the underscore: without it, it's not msdn official!
8987: // Windows (x64 and x86)
1.191 brouard 8988: printf("Windows (x64 and x86) ");if(logged) fprintf(ficlog,"Windows (x64 and x86) ");
1.167 brouard 8989: #elif __unix__ // all unices, not all compilers
8990: // Unix
1.191 brouard 8991: printf("Unix ");if(logged) fprintf(ficlog,"Unix ");
1.167 brouard 8992: #elif __linux__
8993: // linux
1.191 brouard 8994: printf("linux ");if(logged) fprintf(ficlog,"linux ");
1.167 brouard 8995: #elif __APPLE__
1.174 brouard 8996: // Mac OS, not sure if this is covered by __posix__ and/or __unix__ though..
1.191 brouard 8997: printf("Mac OS ");if(logged) fprintf(ficlog,"Mac OS ");
1.167 brouard 8998: #endif
8999:
9000: /* __MINGW32__ */
9001: /* __CYGWIN__ */
9002: /* __MINGW64__ */
9003: // http://msdn.microsoft.com/en-us/library/b0084kay.aspx
9004: /* _MSC_VER //the Visual C++ compiler is 17.00.51106.1, the _MSC_VER macro evaluates to 1700. Type cl /? */
9005: /* _MSC_FULL_VER //the Visual C++ compiler is 15.00.20706.01, the _MSC_FULL_VER macro evaluates to 150020706 */
9006: /* _WIN64 // Defined for applications for Win64. */
9007: /* _M_X64 // Defined for compilations that target x64 processors. */
9008: /* _DEBUG // Defined when you compile with /LDd, /MDd, and /MTd. */
1.171 brouard 9009:
1.167 brouard 9010: #if UINTPTR_MAX == 0xffffffff
1.191 brouard 9011: printf(" 32-bit"); if(logged) fprintf(ficlog," 32-bit");/* 32-bit */
1.167 brouard 9012: #elif UINTPTR_MAX == 0xffffffffffffffff
1.191 brouard 9013: printf(" 64-bit"); if(logged) fprintf(ficlog," 64-bit");/* 64-bit */
1.167 brouard 9014: #else
1.191 brouard 9015: printf(" wtf-bit"); if(logged) fprintf(ficlog," wtf-bit");/* wtf */
1.167 brouard 9016: #endif
9017:
1.169 brouard 9018: #if defined(__GNUC__)
9019: # if defined(__GNUC_PATCHLEVEL__)
9020: # define __GNUC_VERSION__ (__GNUC__ * 10000 \
9021: + __GNUC_MINOR__ * 100 \
9022: + __GNUC_PATCHLEVEL__)
9023: # else
9024: # define __GNUC_VERSION__ (__GNUC__ * 10000 \
9025: + __GNUC_MINOR__ * 100)
9026: # endif
1.174 brouard 9027: printf(" using GNU C version %d.\n", __GNUC_VERSION__);
1.191 brouard 9028: if(logged) fprintf(ficlog, " using GNU C version %d.\n", __GNUC_VERSION__);
1.176 brouard 9029:
9030: if (uname(&sysInfo) != -1) {
9031: printf("Running on: %s %s %s %s %s\n",sysInfo.sysname, sysInfo.nodename, sysInfo.release, sysInfo.version, sysInfo.machine);
1.191 brouard 9032: 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 9033: }
9034: else
9035: perror("uname() error");
1.179 brouard 9036: //#ifndef __INTEL_COMPILER
9037: #if !defined (__INTEL_COMPILER) && !defined(__APPLE__)
1.174 brouard 9038: printf("GNU libc version: %s\n", gnu_get_libc_version());
1.191 brouard 9039: if(logged) fprintf(ficlog,"GNU libc version: %s\n", gnu_get_libc_version());
1.177 brouard 9040: #endif
1.169 brouard 9041: #endif
1.172 brouard 9042:
9043: // void main()
9044: // {
1.169 brouard 9045: #if defined(_MSC_VER)
1.174 brouard 9046: if (IsWow64()){
1.191 brouard 9047: printf("\nThe program (probably compiled for 32bit) is running under WOW64 (64bit) emulation.\n");
9048: if (logged) fprintf(ficlog, "\nThe program (probably compiled for 32bit) is running under WOW64 (64bit) emulation.\n");
1.174 brouard 9049: }
9050: else{
1.191 brouard 9051: printf("\nThe program is not running under WOW64 (i.e probably on a 64bit Windows).\n");
9052: if (logged) fprintf(ficlog, "\nThe programm is not running under WOW64 (i.e probably on a 64bit Windows).\n");
1.174 brouard 9053: }
1.172 brouard 9054: // printf("\nPress Enter to continue...");
9055: // getchar();
9056: // }
9057:
1.169 brouard 9058: #endif
9059:
1.167 brouard 9060:
1.219 brouard 9061: }
1.136 brouard 9062:
1.219 brouard 9063: int prevalence_limit(double *p, double **prlim, double ageminpar, double agemaxpar, double ftolpl, int *ncvyearp){
1.180 brouard 9064: /*--------------- Prevalence limit (period or stable prevalence) --------------*/
1.235 brouard 9065: int i, j, k, i1, k4=0, nres=0 ;
1.202 brouard 9066: /* double ftolpl = 1.e-10; */
1.180 brouard 9067: double age, agebase, agelim;
1.203 brouard 9068: double tot;
1.180 brouard 9069:
1.202 brouard 9070: strcpy(filerespl,"PL_");
9071: strcat(filerespl,fileresu);
9072: if((ficrespl=fopen(filerespl,"w"))==NULL) {
9073: printf("Problem with period (stable) prevalence resultfile: %s\n", filerespl);return 1;
9074: fprintf(ficlog,"Problem with period (stable) prevalence resultfile: %s\n", filerespl);return 1;
9075: }
1.227 brouard 9076: printf("\nComputing period (stable) prevalence: result on file '%s' \n", filerespl);
9077: fprintf(ficlog,"\nComputing period (stable) prevalence: result on file '%s' \n", filerespl);
1.202 brouard 9078: pstamp(ficrespl);
1.203 brouard 9079: fprintf(ficrespl,"# Period (stable) prevalence. Precision given by ftolpl=%g \n", ftolpl);
1.202 brouard 9080: fprintf(ficrespl,"#Age ");
9081: for(i=1; i<=nlstate;i++) fprintf(ficrespl,"%d-%d ",i,i);
9082: fprintf(ficrespl,"\n");
1.180 brouard 9083:
1.219 brouard 9084: /* prlim=matrix(1,nlstate,1,nlstate);*/ /* back in main */
1.180 brouard 9085:
1.219 brouard 9086: agebase=ageminpar;
9087: agelim=agemaxpar;
1.180 brouard 9088:
1.227 brouard 9089: /* i1=pow(2,ncoveff); */
1.234 brouard 9090: i1=pow(2,cptcoveff); /* Number of combination of dummy covariates */
1.219 brouard 9091: if (cptcovn < 1){i1=1;}
1.180 brouard 9092:
1.238 brouard 9093: for(k=1; k<=i1;k++){ /* For each combination k of dummy covariates in the model */
9094: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
9095: if(TKresult[nres]!= k)
9096: continue;
1.235 brouard 9097:
1.238 brouard 9098: /* for(cptcov=1,k=0;cptcov<=i1;cptcov++){ */
9099: /* for(cptcov=1,k=0;cptcov<=1;cptcov++){ */
9100: //for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){
9101: /* k=k+1; */
9102: /* to clean */
9103: //printf("cptcov=%d cptcod=%d codtab=%d\n",cptcov, cptcod,codtabm(cptcod,cptcov));
9104: fprintf(ficrespl,"#******");
9105: printf("#******");
9106: fprintf(ficlog,"#******");
9107: for(j=1;j<=cptcoveff ;j++) {/* all covariates */
9108: fprintf(ficrespl," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]); /* Here problem for varying dummy*/
9109: printf(" V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
9110: fprintf(ficlog," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
9111: }
9112: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
9113: printf(" V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
9114: fprintf(ficrespl," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
9115: fprintf(ficlog," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
9116: }
9117: fprintf(ficrespl,"******\n");
9118: printf("******\n");
9119: fprintf(ficlog,"******\n");
9120: if(invalidvarcomb[k]){
9121: printf("\nCombination (%d) ignored because no case \n",k);
9122: fprintf(ficrespl,"#Combination (%d) ignored because no case \n",k);
9123: fprintf(ficlog,"\nCombination (%d) ignored because no case \n",k);
9124: continue;
9125: }
1.219 brouard 9126:
1.238 brouard 9127: fprintf(ficrespl,"#Age ");
9128: for(j=1;j<=cptcoveff;j++) {
9129: fprintf(ficrespl,"V%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
9130: }
9131: for(i=1; i<=nlstate;i++) fprintf(ficrespl," %d-%d ",i,i);
9132: fprintf(ficrespl,"Total Years_to_converge\n");
1.227 brouard 9133:
1.238 brouard 9134: for (age=agebase; age<=agelim; age++){
9135: /* for (age=agebase; age<=agebase; age++){ */
9136: prevalim(prlim, nlstate, p, age, oldm, savm, ftolpl, ncvyearp, k, nres);
9137: fprintf(ficrespl,"%.0f ",age );
9138: for(j=1;j<=cptcoveff;j++)
9139: fprintf(ficrespl,"%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
9140: tot=0.;
9141: for(i=1; i<=nlstate;i++){
9142: tot += prlim[i][i];
9143: fprintf(ficrespl," %.5f", prlim[i][i]);
9144: }
9145: fprintf(ficrespl," %.3f %d\n", tot, *ncvyearp);
9146: } /* Age */
9147: /* was end of cptcod */
9148: } /* cptcov */
9149: } /* nres */
1.219 brouard 9150: return 0;
1.180 brouard 9151: }
9152:
1.218 brouard 9153: 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){
9154: /*--------------- Back Prevalence limit (period or stable prevalence) --------------*/
9155:
9156: /* Computes the back prevalence limit for any combination of covariate values
9157: * at any age between ageminpar and agemaxpar
9158: */
1.235 brouard 9159: int i, j, k, i1, nres=0 ;
1.217 brouard 9160: /* double ftolpl = 1.e-10; */
9161: double age, agebase, agelim;
9162: double tot;
1.218 brouard 9163: /* double ***mobaverage; */
9164: /* double **dnewm, **doldm, **dsavm; /\* for use *\/ */
1.217 brouard 9165:
9166: strcpy(fileresplb,"PLB_");
9167: strcat(fileresplb,fileresu);
9168: if((ficresplb=fopen(fileresplb,"w"))==NULL) {
9169: printf("Problem with period (stable) back prevalence resultfile: %s\n", fileresplb);return 1;
9170: fprintf(ficlog,"Problem with period (stable) back prevalence resultfile: %s\n", fileresplb);return 1;
9171: }
9172: printf("Computing period (stable) back prevalence: result on file '%s' \n", fileresplb);
9173: fprintf(ficlog,"Computing period (stable) back prevalence: result on file '%s' \n", fileresplb);
9174: pstamp(ficresplb);
9175: fprintf(ficresplb,"# Period (stable) back prevalence. Precision given by ftolpl=%g \n", ftolpl);
9176: fprintf(ficresplb,"#Age ");
9177: for(i=1; i<=nlstate;i++) fprintf(ficresplb,"%d-%d ",i,i);
9178: fprintf(ficresplb,"\n");
9179:
1.218 brouard 9180:
9181: /* prlim=matrix(1,nlstate,1,nlstate);*/ /* back in main */
9182:
9183: agebase=ageminpar;
9184: agelim=agemaxpar;
9185:
9186:
1.227 brouard 9187: i1=pow(2,cptcoveff);
1.218 brouard 9188: if (cptcovn < 1){i1=1;}
1.227 brouard 9189:
1.238 brouard 9190: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
9191: for(k=1; k<=i1;k++){ /* For any combination of dummy covariates, fixed and varying */
9192: if(TKresult[nres]!= k)
9193: continue;
9194: //printf("cptcov=%d cptcod=%d codtab=%d\n",cptcov, cptcod,codtabm(cptcod,cptcov));
9195: fprintf(ficresplb,"#******");
9196: printf("#******");
9197: fprintf(ficlog,"#******");
9198: for(j=1;j<=cptcoveff ;j++) {/* all covariates */
9199: fprintf(ficresplb," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
9200: printf(" V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
9201: fprintf(ficlog," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
9202: }
9203: for (j=1; j<= nsq; j++){ /* For each selected (single) quantitative value */
9204: printf(" V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
9205: fprintf(ficresplb," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
9206: fprintf(ficlog," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
9207: }
9208: fprintf(ficresplb,"******\n");
9209: printf("******\n");
9210: fprintf(ficlog,"******\n");
9211: if(invalidvarcomb[k]){
9212: printf("\nCombination (%d) ignored because no cases \n",k);
9213: fprintf(ficresplb,"#Combination (%d) ignored because no cases \n",k);
9214: fprintf(ficlog,"\nCombination (%d) ignored because no cases \n",k);
9215: continue;
9216: }
1.218 brouard 9217:
1.238 brouard 9218: fprintf(ficresplb,"#Age ");
9219: for(j=1;j<=cptcoveff;j++) {
9220: fprintf(ficresplb,"V%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
9221: }
9222: for(i=1; i<=nlstate;i++) fprintf(ficresplb," %d-%d ",i,i);
9223: fprintf(ficresplb,"Total Years_to_converge\n");
1.218 brouard 9224:
9225:
1.238 brouard 9226: for (age=agebase; age<=agelim; age++){
9227: /* for (age=agebase; age<=agebase; age++){ */
9228: if(mobilavproj > 0){
9229: /* bprevalim(bprlim, mobaverage, nlstate, p, age, ageminpar, agemaxpar, oldm, savm, doldm, dsavm, ftolpl, ncvyearp, k); */
9230: /* bprevalim(bprlim, mobaverage, nlstate, p, age, oldm, savm, dnewm, doldm, dsavm, ftolpl, ncvyearp, k); */
9231: bprevalim(bprlim, mobaverage, nlstate, p, age, ftolpl, ncvyearp, k);
9232: }else if (mobilavproj == 0){
9233: 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);
9234: 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);
9235: exit(1);
9236: }else{
9237: /* bprevalim(bprlim, probs, nlstate, p, age, oldm, savm, dnewm, doldm, dsavm, ftolpl, ncvyearp, k); */
9238: bprevalim(bprlim, probs, nlstate, p, age, ftolpl, ncvyearp, k);
9239: }
9240: fprintf(ficresplb,"%.0f ",age );
9241: for(j=1;j<=cptcoveff;j++)
9242: fprintf(ficresplb,"%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
9243: tot=0.;
9244: for(i=1; i<=nlstate;i++){
9245: tot += bprlim[i][i];
9246: fprintf(ficresplb," %.5f", bprlim[i][i]);
9247: }
9248: fprintf(ficresplb," %.3f %d\n", tot, *ncvyearp);
9249: } /* Age */
9250: /* was end of cptcod */
9251: } /* end of any combination */
9252: } /* end of nres */
1.218 brouard 9253: /* hBijx(p, bage, fage); */
9254: /* fclose(ficrespijb); */
9255:
9256: return 0;
1.217 brouard 9257: }
1.218 brouard 9258:
1.180 brouard 9259: int hPijx(double *p, int bage, int fage){
9260: /*------------- h Pij x at various ages ------------*/
9261:
9262: int stepsize;
9263: int agelim;
9264: int hstepm;
9265: int nhstepm;
1.235 brouard 9266: int h, i, i1, j, k, k4, nres=0;
1.180 brouard 9267:
9268: double agedeb;
9269: double ***p3mat;
9270:
1.201 brouard 9271: strcpy(filerespij,"PIJ_"); strcat(filerespij,fileresu);
1.180 brouard 9272: if((ficrespij=fopen(filerespij,"w"))==NULL) {
9273: printf("Problem with Pij resultfile: %s\n", filerespij); return 1;
9274: fprintf(ficlog,"Problem with Pij resultfile: %s\n", filerespij); return 1;
9275: }
9276: printf("Computing pij: result on file '%s' \n", filerespij);
9277: fprintf(ficlog,"Computing pij: result on file '%s' \n", filerespij);
9278:
9279: stepsize=(int) (stepm+YEARM-1)/YEARM;
9280: /*if (stepm<=24) stepsize=2;*/
9281:
9282: agelim=AGESUP;
9283: hstepm=stepsize*YEARM; /* Every year of age */
9284: hstepm=hstepm/stepm; /* Typically 2 years, = 2/6 months = 4 */
1.218 brouard 9285:
1.180 brouard 9286: /* hstepm=1; aff par mois*/
9287: pstamp(ficrespij);
9288: fprintf(ficrespij,"#****** h Pij x Probability to be in state j at age x+h being in i at x ");
1.227 brouard 9289: i1= pow(2,cptcoveff);
1.218 brouard 9290: /* for(cptcov=1,k=0;cptcov<=i1;cptcov++){ */
9291: /* /\*for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){*\/ */
9292: /* k=k+1; */
1.235 brouard 9293: for(nres=1; nres <= nresult; nres++) /* For each resultline */
9294: for(k=1; k<=i1;k++){
9295: if(TKresult[nres]!= k)
9296: continue;
1.183 brouard 9297: fprintf(ficrespij,"\n#****** ");
1.227 brouard 9298: for(j=1;j<=cptcoveff;j++)
1.198 brouard 9299: fprintf(ficrespij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
1.235 brouard 9300: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
9301: printf(" V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
9302: fprintf(ficrespij," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
9303: }
1.183 brouard 9304: fprintf(ficrespij,"******\n");
9305:
9306: for (agedeb=fage; agedeb>=bage; agedeb--){ /* If stepm=6 months */
9307: nhstepm=(int) rint((agelim-agedeb)*YEARM/stepm); /* Typically 20 years = 20*12/6=40 */
9308: nhstepm = nhstepm/hstepm; /* Typically 40/4=10 */
9309:
9310: /* nhstepm=nhstepm*YEARM; aff par mois*/
1.180 brouard 9311:
1.183 brouard 9312: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
9313: oldm=oldms;savm=savms;
1.235 brouard 9314: hpxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k, nres);
1.183 brouard 9315: fprintf(ficrespij,"# Cov Agex agex+h hpijx with i,j=");
9316: for(i=1; i<=nlstate;i++)
9317: for(j=1; j<=nlstate+ndeath;j++)
9318: fprintf(ficrespij," %1d-%1d",i,j);
9319: fprintf(ficrespij,"\n");
9320: for (h=0; h<=nhstepm; h++){
9321: /*agedebphstep = agedeb + h*hstepm/YEARM*stepm;*/
9322: fprintf(ficrespij,"%d %3.f %3.f",k, agedeb, agedeb + h*hstepm/YEARM*stepm );
1.180 brouard 9323: for(i=1; i<=nlstate;i++)
9324: for(j=1; j<=nlstate+ndeath;j++)
1.183 brouard 9325: fprintf(ficrespij," %.5f", p3mat[i][j][h]);
1.180 brouard 9326: fprintf(ficrespij,"\n");
9327: }
1.183 brouard 9328: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
9329: fprintf(ficrespij,"\n");
9330: }
1.180 brouard 9331: /*}*/
9332: }
1.218 brouard 9333: return 0;
1.180 brouard 9334: }
1.218 brouard 9335:
9336: int hBijx(double *p, int bage, int fage, double ***prevacurrent){
1.217 brouard 9337: /*------------- h Bij x at various ages ------------*/
9338:
9339: int stepsize;
1.218 brouard 9340: /* int agelim; */
9341: int ageminl;
1.217 brouard 9342: int hstepm;
9343: int nhstepm;
1.238 brouard 9344: int h, i, i1, j, k, nres;
1.218 brouard 9345:
1.217 brouard 9346: double agedeb;
9347: double ***p3mat;
1.218 brouard 9348:
9349: strcpy(filerespijb,"PIJB_"); strcat(filerespijb,fileresu);
9350: if((ficrespijb=fopen(filerespijb,"w"))==NULL) {
9351: printf("Problem with Pij back resultfile: %s\n", filerespijb); return 1;
9352: fprintf(ficlog,"Problem with Pij back resultfile: %s\n", filerespijb); return 1;
9353: }
9354: printf("Computing pij back: result on file '%s' \n", filerespijb);
9355: fprintf(ficlog,"Computing pij back: result on file '%s' \n", filerespijb);
9356:
9357: stepsize=(int) (stepm+YEARM-1)/YEARM;
9358: /*if (stepm<=24) stepsize=2;*/
1.217 brouard 9359:
1.218 brouard 9360: /* agelim=AGESUP; */
9361: ageminl=30;
9362: hstepm=stepsize*YEARM; /* Every year of age */
9363: hstepm=hstepm/stepm; /* Typically 2 years, = 2/6 months = 4 */
9364:
9365: /* hstepm=1; aff par mois*/
9366: pstamp(ficrespijb);
9367: fprintf(ficrespijb,"#****** h Pij x Back Probability to be in state i at age x-h being in j at x ");
1.227 brouard 9368: i1= pow(2,cptcoveff);
1.218 brouard 9369: /* for(cptcov=1,k=0;cptcov<=i1;cptcov++){ */
9370: /* /\*for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){*\/ */
9371: /* k=k+1; */
1.238 brouard 9372: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
9373: for(k=1; k<=i1;k++){ /* For any combination of dummy covariates, fixed and varying */
9374: if(TKresult[nres]!= k)
9375: continue;
9376: fprintf(ficrespijb,"\n#****** ");
9377: for(j=1;j<=cptcoveff;j++)
9378: fprintf(ficrespijb,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
9379: for (j=1; j<= nsq; j++){ /* For each selected (single) quantitative value */
9380: fprintf(ficrespijb," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
9381: }
9382: fprintf(ficrespijb,"******\n");
9383: if(invalidvarcomb[k]){
9384: fprintf(ficrespijb,"\n#Combination (%d) ignored because no cases \n",k);
9385: continue;
9386: }
9387:
9388: /* for (agedeb=fage; agedeb>=bage; agedeb--){ /\* If stepm=6 months *\/ */
9389: for (agedeb=bage; agedeb<=fage; agedeb++){ /* If stepm=6 months and estepm=24 (2 years) */
9390: /* nhstepm=(int) rint((agelim-agedeb)*YEARM/stepm); /\* Typically 20 years = 20*12/6=40 *\/ */
9391: nhstepm=(int) rint((agedeb-ageminl)*YEARM/stepm); /* Typically 20 years = 20*12/6=40 */
9392: nhstepm = nhstepm/hstepm; /* Typically 40/4=10, because estepm=24 stepm=6 => hstepm=24/6=4 */
9393:
9394: /* nhstepm=nhstepm*YEARM; aff par mois*/
9395:
9396: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
9397: /* oldm=oldms;savm=savms; */
9398: /* hbxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k); */
9399: hbxij(p3mat,nhstepm,agedeb,hstepm,p,prevacurrent,nlstate,stepm, k);
9400: /* hbxij(p3mat,nhstepm,agedeb,hstepm,p,prevacurrent,nlstate,stepm,oldm,savm, dnewm, doldm, dsavm, k); */
9401: fprintf(ficrespijb,"# Cov Agex agex-h hpijx with i,j=");
1.217 brouard 9402: for(i=1; i<=nlstate;i++)
9403: for(j=1; j<=nlstate+ndeath;j++)
1.238 brouard 9404: fprintf(ficrespijb," %1d-%1d",i,j);
1.217 brouard 9405: fprintf(ficrespijb,"\n");
1.238 brouard 9406: for (h=0; h<=nhstepm; h++){
9407: /*agedebphstep = agedeb + h*hstepm/YEARM*stepm;*/
9408: fprintf(ficrespijb,"%d %3.f %3.f",k, agedeb, agedeb - h*hstepm/YEARM*stepm );
9409: /* fprintf(ficrespijb,"%d %3.f %3.f",k, agedeb, agedeb + h*hstepm/YEARM*stepm ); */
9410: for(i=1; i<=nlstate;i++)
9411: for(j=1; j<=nlstate+ndeath;j++)
9412: fprintf(ficrespijb," %.5f", p3mat[i][j][h]);
9413: fprintf(ficrespijb,"\n");
9414: }
9415: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
9416: fprintf(ficrespijb,"\n");
9417: } /* end age deb */
9418: } /* end combination */
9419: } /* end nres */
1.218 brouard 9420: return 0;
9421: } /* hBijx */
1.217 brouard 9422:
1.180 brouard 9423:
1.136 brouard 9424: /***********************************************/
9425: /**************** Main Program *****************/
9426: /***********************************************/
9427:
9428: int main(int argc, char *argv[])
9429: {
9430: #ifdef GSL
9431: const gsl_multimin_fminimizer_type *T;
9432: size_t iteri = 0, it;
9433: int rval = GSL_CONTINUE;
9434: int status = GSL_SUCCESS;
9435: double ssval;
9436: #endif
9437: int movingaverage(double ***probs, double bage,double fage, double ***mobaverage, int mobilav);
1.164 brouard 9438: int i,j, k, n=MAXN,iter=0,m,size=100, cptcod;
1.209 brouard 9439: int ncvyear=0; /* Number of years needed for the period prevalence to converge */
1.164 brouard 9440: int jj, ll, li, lj, lk;
1.136 brouard 9441: int numlinepar=0; /* Current linenumber of parameter file */
1.197 brouard 9442: int num_filled;
1.136 brouard 9443: int itimes;
9444: int NDIM=2;
9445: int vpopbased=0;
1.235 brouard 9446: int nres=0;
1.136 brouard 9447:
1.164 brouard 9448: char ca[32], cb[32];
1.136 brouard 9449: /* FILE *fichtm; *//* Html File */
9450: /* FILE *ficgp;*/ /*Gnuplot File */
9451: struct stat info;
1.191 brouard 9452: double agedeb=0.;
1.194 brouard 9453:
9454: double ageminpar=AGEOVERFLOW,agemin=AGEOVERFLOW, agemaxpar=-AGEOVERFLOW, agemax=-AGEOVERFLOW;
1.219 brouard 9455: double ageminout=-AGEOVERFLOW,agemaxout=AGEOVERFLOW; /* Smaller Age range redefined after movingaverage */
1.136 brouard 9456:
1.165 brouard 9457: double fret;
1.191 brouard 9458: double dum=0.; /* Dummy variable */
1.136 brouard 9459: double ***p3mat;
1.218 brouard 9460: /* double ***mobaverage; */
1.164 brouard 9461:
9462: char line[MAXLINE];
1.197 brouard 9463: char path[MAXLINE],pathc[MAXLINE],pathcd[MAXLINE],pathtot[MAXLINE];
9464:
1.234 brouard 9465: char modeltemp[MAXLINE];
1.230 brouard 9466: char resultline[MAXLINE];
9467:
1.136 brouard 9468: char pathr[MAXLINE], pathimach[MAXLINE];
1.164 brouard 9469: char *tok, *val; /* pathtot */
1.136 brouard 9470: int firstobs=1, lastobs=10;
1.195 brouard 9471: int c, h , cpt, c2;
1.191 brouard 9472: int jl=0;
9473: int i1, j1, jk, stepsize=0;
1.194 brouard 9474: int count=0;
9475:
1.164 brouard 9476: int *tab;
1.136 brouard 9477: int mobilavproj=0 , prevfcast=0 ; /* moving average of prev, If prevfcast=1 prevalence projection */
1.217 brouard 9478: int backcast=0;
1.136 brouard 9479: int mobilav=0,popforecast=0;
1.191 brouard 9480: int hstepm=0, nhstepm=0;
1.136 brouard 9481: int agemortsup;
9482: float sumlpop=0.;
9483: double jprev1=1, mprev1=1,anprev1=2000,jprev2=1, mprev2=1,anprev2=2000;
9484: double jpyram=1, mpyram=1,anpyram=2000,jpyram1=1, mpyram1=1,anpyram1=2000;
9485:
1.191 brouard 9486: double bage=0, fage=110., age, agelim=0., agebase=0.;
1.136 brouard 9487: double ftolpl=FTOL;
9488: double **prlim;
1.217 brouard 9489: double **bprlim;
1.136 brouard 9490: double ***param; /* Matrix of parameters */
9491: double *p;
9492: double **matcov; /* Matrix of covariance */
1.203 brouard 9493: double **hess; /* Hessian matrix */
1.136 brouard 9494: double ***delti3; /* Scale */
9495: double *delti; /* Scale */
9496: double ***eij, ***vareij;
9497: double **varpl; /* Variances of prevalence limits by age */
9498: double *epj, vepp;
1.164 brouard 9499:
1.136 brouard 9500: double dateprev1, dateprev2,jproj1=1,mproj1=1,anproj1=2000,jproj2=1,mproj2=1,anproj2=2000;
1.217 brouard 9501: double jback1=1,mback1=1,anback1=2000,jback2=1,mback2=1,anback2=2000;
9502:
1.136 brouard 9503: double **ximort;
1.145 brouard 9504: char *alph[]={"a","a","b","c","d","e"}, str[4]="1234";
1.136 brouard 9505: int *dcwave;
9506:
1.164 brouard 9507: char z[1]="c";
1.136 brouard 9508:
9509: /*char *strt;*/
9510: char strtend[80];
1.126 brouard 9511:
1.164 brouard 9512:
1.126 brouard 9513: /* setlocale (LC_ALL, ""); */
9514: /* bindtextdomain (PACKAGE, LOCALEDIR); */
9515: /* textdomain (PACKAGE); */
9516: /* setlocale (LC_CTYPE, ""); */
9517: /* setlocale (LC_MESSAGES, ""); */
9518:
9519: /* gettimeofday(&start_time, (struct timezone*)0); */ /* at first time */
1.157 brouard 9520: rstart_time = time(NULL);
9521: /* (void) gettimeofday(&start_time,&tzp);*/
9522: start_time = *localtime(&rstart_time);
1.126 brouard 9523: curr_time=start_time;
1.157 brouard 9524: /*tml = *localtime(&start_time.tm_sec);*/
9525: /* strcpy(strstart,asctime(&tml)); */
9526: strcpy(strstart,asctime(&start_time));
1.126 brouard 9527:
9528: /* printf("Localtime (at start)=%s",strstart); */
1.157 brouard 9529: /* tp.tm_sec = tp.tm_sec +86400; */
9530: /* tm = *localtime(&start_time.tm_sec); */
1.126 brouard 9531: /* tmg.tm_year=tmg.tm_year +dsign*dyear; */
9532: /* tmg.tm_mon=tmg.tm_mon +dsign*dmonth; */
9533: /* tmg.tm_hour=tmg.tm_hour + 1; */
1.157 brouard 9534: /* tp.tm_sec = mktime(&tmg); */
1.126 brouard 9535: /* strt=asctime(&tmg); */
9536: /* printf("Time(after) =%s",strstart); */
9537: /* (void) time (&time_value);
9538: * printf("time=%d,t-=%d\n",time_value,time_value-86400);
9539: * tm = *localtime(&time_value);
9540: * strstart=asctime(&tm);
9541: * printf("tim_value=%d,asctime=%s\n",time_value,strstart);
9542: */
9543:
9544: nberr=0; /* Number of errors and warnings */
9545: nbwarn=0;
1.184 brouard 9546: #ifdef WIN32
9547: _getcwd(pathcd, size);
9548: #else
1.126 brouard 9549: getcwd(pathcd, size);
1.184 brouard 9550: #endif
1.191 brouard 9551: syscompilerinfo(0);
1.196 brouard 9552: printf("\nIMaCh version %s, %s\n%s",version, copyright, fullversion);
1.126 brouard 9553: if(argc <=1){
9554: printf("\nEnter the parameter file name: ");
1.205 brouard 9555: if(!fgets(pathr,FILENAMELENGTH,stdin)){
9556: printf("ERROR Empty parameter file name\n");
9557: goto end;
9558: }
1.126 brouard 9559: i=strlen(pathr);
9560: if(pathr[i-1]=='\n')
9561: pathr[i-1]='\0';
1.156 brouard 9562: i=strlen(pathr);
1.205 brouard 9563: if(i >= 1 && pathr[i-1]==' ') {/* This may happen when dragging on oS/X! */
1.156 brouard 9564: pathr[i-1]='\0';
1.205 brouard 9565: }
9566: i=strlen(pathr);
9567: if( i==0 ){
9568: printf("ERROR Empty parameter file name\n");
9569: goto end;
9570: }
9571: for (tok = pathr; tok != NULL; ){
1.126 brouard 9572: printf("Pathr |%s|\n",pathr);
9573: while ((val = strsep(&tok, "\"" )) != NULL && *val == '\0');
9574: printf("val= |%s| pathr=%s\n",val,pathr);
9575: strcpy (pathtot, val);
9576: if(pathr[0] == '\0') break; /* Dirty */
9577: }
9578: }
9579: else{
9580: strcpy(pathtot,argv[1]);
9581: }
9582: /*if(getcwd(pathcd, MAXLINE)!= NULL)printf ("Error pathcd\n");*/
9583: /*cygwin_split_path(pathtot,path,optionfile);
9584: printf("pathtot=%s, path=%s, optionfile=%s\n",pathtot,path,optionfile);*/
9585: /* cutv(path,optionfile,pathtot,'\\');*/
9586:
9587: /* Split argv[0], imach program to get pathimach */
9588: printf("\nargv[0]=%s argv[1]=%s, \n",argv[0],argv[1]);
9589: split(argv[0],pathimach,optionfile,optionfilext,optionfilefiname);
9590: printf("\nargv[0]=%s pathimach=%s, \noptionfile=%s \noptionfilext=%s \noptionfilefiname=%s\n",argv[0],pathimach,optionfile,optionfilext,optionfilefiname);
9591: /* strcpy(pathimach,argv[0]); */
9592: /* Split argv[1]=pathtot, parameter file name to get path, optionfile, extension and name */
9593: split(pathtot,path,optionfile,optionfilext,optionfilefiname);
9594: printf("\npathtot=%s,\npath=%s,\noptionfile=%s \noptionfilext=%s \noptionfilefiname=%s\n",pathtot,path,optionfile,optionfilext,optionfilefiname);
1.184 brouard 9595: #ifdef WIN32
9596: _chdir(path); /* Can be a relative path */
9597: if(_getcwd(pathcd,MAXLINE) > 0) /* So pathcd is the full path */
9598: #else
1.126 brouard 9599: chdir(path); /* Can be a relative path */
1.184 brouard 9600: if (getcwd(pathcd, MAXLINE) > 0) /* So pathcd is the full path */
9601: #endif
9602: printf("Current directory %s!\n",pathcd);
1.126 brouard 9603: strcpy(command,"mkdir ");
9604: strcat(command,optionfilefiname);
9605: if((outcmd=system(command)) != 0){
1.169 brouard 9606: printf("Directory already exists (or can't create it) %s%s, err=%d\n",path,optionfilefiname,outcmd);
1.126 brouard 9607: /* fprintf(ficlog,"Problem creating directory %s%s\n",path,optionfilefiname); */
9608: /* fclose(ficlog); */
9609: /* exit(1); */
9610: }
9611: /* if((imk=mkdir(optionfilefiname))<0){ */
9612: /* perror("mkdir"); */
9613: /* } */
9614:
9615: /*-------- arguments in the command line --------*/
9616:
1.186 brouard 9617: /* Main Log file */
1.126 brouard 9618: strcat(filelog, optionfilefiname);
9619: strcat(filelog,".log"); /* */
9620: if((ficlog=fopen(filelog,"w"))==NULL) {
9621: printf("Problem with logfile %s\n",filelog);
9622: goto end;
9623: }
9624: fprintf(ficlog,"Log filename:%s\n",filelog);
1.197 brouard 9625: fprintf(ficlog,"Version %s %s",version,fullversion);
1.126 brouard 9626: fprintf(ficlog,"\nEnter the parameter file name: \n");
9627: fprintf(ficlog,"pathimach=%s\npathtot=%s\n\
9628: path=%s \n\
9629: optionfile=%s\n\
9630: optionfilext=%s\n\
1.156 brouard 9631: optionfilefiname='%s'\n",pathimach,pathtot,path,optionfile,optionfilext,optionfilefiname);
1.126 brouard 9632:
1.197 brouard 9633: syscompilerinfo(1);
1.167 brouard 9634:
1.126 brouard 9635: printf("Local time (at start):%s",strstart);
9636: fprintf(ficlog,"Local time (at start): %s",strstart);
9637: fflush(ficlog);
9638: /* (void) gettimeofday(&curr_time,&tzp); */
1.157 brouard 9639: /* printf("Elapsed time %d\n", asc_diff_time(curr_time.tm_sec-start_time.tm_sec,tmpout)); */
1.126 brouard 9640:
9641: /* */
9642: strcpy(fileres,"r");
9643: strcat(fileres, optionfilefiname);
1.201 brouard 9644: strcat(fileresu, optionfilefiname); /* Without r in front */
1.126 brouard 9645: strcat(fileres,".txt"); /* Other files have txt extension */
1.201 brouard 9646: strcat(fileresu,".txt"); /* Other files have txt extension */
1.126 brouard 9647:
1.186 brouard 9648: /* Main ---------arguments file --------*/
1.126 brouard 9649:
9650: if((ficpar=fopen(optionfile,"r"))==NULL) {
1.155 brouard 9651: printf("Problem with optionfile '%s' with errno='%s'\n",optionfile,strerror(errno));
9652: fprintf(ficlog,"Problem with optionfile '%s' with errno='%s'\n",optionfile,strerror(errno));
1.126 brouard 9653: fflush(ficlog);
1.149 brouard 9654: /* goto end; */
9655: exit(70);
1.126 brouard 9656: }
9657:
9658:
9659:
9660: strcpy(filereso,"o");
1.201 brouard 9661: strcat(filereso,fileresu);
1.126 brouard 9662: if((ficparo=fopen(filereso,"w"))==NULL) { /* opened on subdirectory */
9663: printf("Problem with Output resultfile: %s\n", filereso);
9664: fprintf(ficlog,"Problem with Output resultfile: %s\n", filereso);
9665: fflush(ficlog);
9666: goto end;
9667: }
9668:
9669: /* Reads comments: lines beginning with '#' */
9670: numlinepar=0;
1.197 brouard 9671:
9672: /* First parameter line */
9673: while(fgets(line, MAXLINE, ficpar)) {
9674: /* If line starts with a # it is a comment */
9675: if (line[0] == '#') {
9676: numlinepar++;
9677: fputs(line,stdout);
9678: fputs(line,ficparo);
9679: fputs(line,ficlog);
9680: continue;
9681: }else
9682: break;
9683: }
9684: if((num_filled=sscanf(line,"title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\n", \
9685: title, datafile, &lastobs, &firstpass,&lastpass)) !=EOF){
9686: if (num_filled != 5) {
9687: printf("Should be 5 parameters\n");
9688: }
1.126 brouard 9689: numlinepar++;
1.197 brouard 9690: printf("title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\n", title, datafile, lastobs, firstpass,lastpass);
9691: }
9692: /* Second parameter line */
9693: while(fgets(line, MAXLINE, ficpar)) {
9694: /* If line starts with a # it is a comment */
9695: if (line[0] == '#') {
9696: numlinepar++;
9697: fputs(line,stdout);
9698: fputs(line,ficparo);
9699: fputs(line,ficlog);
9700: continue;
9701: }else
9702: break;
9703: }
1.223 brouard 9704: if((num_filled=sscanf(line,"ftol=%lf stepm=%d ncovcol=%d nqv=%d ntv=%d nqtv=%d nlstate=%d ndeath=%d maxwav=%d mle=%d weight=%d\n", \
9705: &ftol, &stepm, &ncovcol, &nqv, &ntv, &nqtv, &nlstate, &ndeath, &maxwav, &mle, &weightopt)) !=EOF){
9706: if (num_filled != 11) {
9707: printf("Not 11 parameters, for example:ftol=1.e-8 stepm=12 ncovcol=2 nqv=1 ntv=2 nqtv=1 nlstate=2 ndeath=1 maxwav=3 mle=1 weight=1\n");
1.209 brouard 9708: printf("but line=%s\n",line);
1.197 brouard 9709: }
1.223 brouard 9710: printf("ftol=%e stepm=%d ncovcol=%d nqv=%d ntv=%d nqtv=%d nlstate=%d ndeath=%d maxwav=%d mle=%d weight=%d\n",ftol, stepm, ncovcol, nqv, ntv, nqtv, nlstate, ndeath, maxwav, mle, weightopt);
1.126 brouard 9711: }
1.203 brouard 9712: /* ftolpl=6*ftol*1.e5; /\* 6.e-3 make convergences in less than 80 loops for the prevalence limit *\/ */
1.209 brouard 9713: /*ftolpl=6.e-4; *//* 6.e-3 make convergences in less than 80 loops for the prevalence limit */
1.197 brouard 9714: /* Third parameter line */
9715: while(fgets(line, MAXLINE, ficpar)) {
9716: /* If line starts with a # it is a comment */
9717: if (line[0] == '#') {
9718: numlinepar++;
9719: fputs(line,stdout);
9720: fputs(line,ficparo);
9721: fputs(line,ficlog);
9722: continue;
9723: }else
9724: break;
9725: }
1.201 brouard 9726: if((num_filled=sscanf(line,"model=1+age%[^.\n]", model)) !=EOF){
9727: if (num_filled == 0)
9728: model[0]='\0';
9729: else if (num_filled != 1){
1.197 brouard 9730: printf("ERROR %d: Model should be at minimum 'model=1+age.' %s\n",num_filled, line);
9731: fprintf(ficlog,"ERROR %d: Model should be at minimum 'model=1+age.' %s\n",num_filled, line);
9732: model[0]='\0';
9733: goto end;
9734: }
9735: else{
9736: if (model[0]=='+'){
9737: for(i=1; i<=strlen(model);i++)
9738: modeltemp[i-1]=model[i];
1.201 brouard 9739: strcpy(model,modeltemp);
1.197 brouard 9740: }
9741: }
1.199 brouard 9742: /* printf(" model=1+age%s modeltemp= %s, model=%s\n",model, modeltemp, model);fflush(stdout); */
1.203 brouard 9743: printf("model=1+age+%s\n",model);fflush(stdout);
1.197 brouard 9744: }
9745: /* 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); */
9746: /* numlinepar=numlinepar+3; /\* In general *\/ */
9747: /* printf("title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\nftol=%e stepm=%d ncovcol=%d nlstate=%d ndeath=%d maxwav=%d mle=%d weight=%d\nmodel=1+age+%s\n", title, datafile, lastobs, firstpass,lastpass,ftol, stepm, ncovcol, nlstate,ndeath, maxwav, mle, weightopt,model); */
1.223 brouard 9748: fprintf(ficparo,"title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\nftol=%e stepm=%d ncovcol=%d nqv=%d ntv=%d nqtv=%d nlstate=%d ndeath=%d maxwav=%d mle=%d weight=%d\nmodel=1+age+%s.\n", title, datafile, lastobs, firstpass,lastpass,ftol,stepm,ncovcol, nqv, ntv, nqtv, nlstate,ndeath,maxwav, mle, weightopt,model);
9749: fprintf(ficlog,"title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\nftol=%e stepm=%d ncovcol=%d nqv=%d ntv=%d nqtv=%d nlstate=%d ndeath=%d maxwav=%d mle=%d weight=%d\nmodel=1+age+%s.\n", title, datafile, lastobs, firstpass,lastpass,ftol,stepm,ncovcol, nqv, ntv, nqtv, nlstate,ndeath,maxwav, mle, weightopt,model);
1.126 brouard 9750: fflush(ficlog);
1.190 brouard 9751: /* if(model[0]=='#'|| model[0]== '\0'){ */
9752: if(model[0]=='#'){
1.187 brouard 9753: printf("Error in 'model' line: model should start with 'model=1+age+' and end with '.' \n \
9754: 'model=1+age+.' or 'model=1+age+V1.' or 'model=1+age+age*age+V1+V1*age.' or \n \
9755: 'model=1+age+V1+V2.' or 'model=1+age+V1+V2+V1*V2.' etc. \n"); \
9756: if(mle != -1){
9757: printf("Fix the model line and run imach with mle=-1 to get a correct template of the parameter file.\n");
9758: exit(1);
9759: }
9760: }
1.126 brouard 9761: while((c=getc(ficpar))=='#' && c!= EOF){
9762: ungetc(c,ficpar);
9763: fgets(line, MAXLINE, ficpar);
9764: numlinepar++;
1.195 brouard 9765: if(line[1]=='q'){ /* This #q will quit imach (the answer is q) */
9766: z[0]=line[1];
9767: }
9768: /* printf("****line [1] = %c \n",line[1]); */
1.141 brouard 9769: fputs(line, stdout);
9770: //puts(line);
1.126 brouard 9771: fputs(line,ficparo);
9772: fputs(line,ficlog);
9773: }
9774: ungetc(c,ficpar);
9775:
9776:
1.145 brouard 9777: covar=matrix(0,NCOVMAX,1,n); /**< used in readdata */
1.225 brouard 9778: coqvar=matrix(1,nqv,1,n); /**< Fixed quantitative covariate */
1.233 brouard 9779: cotvar=ma3x(1,maxwav,1,ntv+nqtv,1,n); /**< Time varying covariate (dummy and quantitative)*/
1.225 brouard 9780: cotqvar=ma3x(1,maxwav,1,nqtv,1,n); /**< Time varying quantitative covariate */
1.136 brouard 9781: cptcovn=0; /*Number of covariates, i.e. number of '+' in model statement plus one, indepently of n in Vn*/
9782: /* v1+v2+v3+v2*v4+v5*age makes cptcovn = 5
9783: v1+v2*age+v2*v3 makes cptcovn = 3
9784: */
9785: if (strlen(model)>1)
1.187 brouard 9786: 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 9787: else
1.187 brouard 9788: ncovmodel=2; /* Constant and age */
1.133 brouard 9789: nforce= (nlstate+ndeath-1)*nlstate; /* Number of forces ij from state i to j */
9790: npar= nforce*ncovmodel; /* Number of parameters like aij*/
1.131 brouard 9791: if(npar >MAXPARM || nlstate >NLSTATEMAX || ndeath >NDEATHMAX || ncovmodel>NCOVMAX){
9792: 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);
9793: 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);
9794: fflush(stdout);
9795: fclose (ficlog);
9796: goto end;
9797: }
1.126 brouard 9798: delti3= ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel);
9799: delti=delti3[1][1];
9800: /*delti=vector(1,npar); *//* Scale of each paramater (output from hesscov)*/
9801: if(mle==-1){ /* Print a wizard for help writing covariance matrix */
9802: prwizard(ncovmodel, nlstate, ndeath, model, ficparo);
1.191 brouard 9803: printf(" You chose mle=-1, look at file %s for a template of covariance matrix \n",filereso);
9804: fprintf(ficlog," You chose mle=-1, look at file %s for a template of covariance matrix \n",filereso);
1.126 brouard 9805: free_ma3x(delti3,1,nlstate,1, nlstate+ndeath-1,1,ncovmodel);
9806: fclose (ficparo);
9807: fclose (ficlog);
9808: goto end;
9809: exit(0);
1.220 brouard 9810: } else if(mle==-5) { /* Main Wizard */
1.126 brouard 9811: prwizard(ncovmodel, nlstate, ndeath, model, ficparo);
1.192 brouard 9812: printf(" You chose mle=-3, look at file %s for a template of covariance matrix \n",filereso);
9813: fprintf(ficlog," You chose mle=-3, look at file %s for a template of covariance matrix \n",filereso);
1.126 brouard 9814: param= ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel);
9815: matcov=matrix(1,npar,1,npar);
1.203 brouard 9816: hess=matrix(1,npar,1,npar);
1.220 brouard 9817: } else{ /* Begin of mle != -1 or -5 */
1.145 brouard 9818: /* Read guessed parameters */
1.126 brouard 9819: /* Reads comments: lines beginning with '#' */
9820: while((c=getc(ficpar))=='#' && c!= EOF){
9821: ungetc(c,ficpar);
9822: fgets(line, MAXLINE, ficpar);
9823: numlinepar++;
1.141 brouard 9824: fputs(line,stdout);
1.126 brouard 9825: fputs(line,ficparo);
9826: fputs(line,ficlog);
9827: }
9828: ungetc(c,ficpar);
9829:
9830: param= ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel);
9831: for(i=1; i <=nlstate; i++){
1.234 brouard 9832: j=0;
1.126 brouard 9833: for(jj=1; jj <=nlstate+ndeath; jj++){
1.234 brouard 9834: if(jj==i) continue;
9835: j++;
9836: fscanf(ficpar,"%1d%1d",&i1,&j1);
9837: if ((i1 != i) || (j1 != jj)){
9838: printf("Error in line parameters number %d, %1d%1d instead of %1d%1d \n \
1.126 brouard 9839: It might be a problem of design; if ncovcol and the model are correct\n \
9840: run imach with mle=-1 to get a correct template of the parameter file.\n",numlinepar, i,j, i1, j1);
1.234 brouard 9841: exit(1);
9842: }
9843: fprintf(ficparo,"%1d%1d",i1,j1);
9844: if(mle==1)
9845: printf("%1d%1d",i,jj);
9846: fprintf(ficlog,"%1d%1d",i,jj);
9847: for(k=1; k<=ncovmodel;k++){
9848: fscanf(ficpar," %lf",¶m[i][j][k]);
9849: if(mle==1){
9850: printf(" %lf",param[i][j][k]);
9851: fprintf(ficlog," %lf",param[i][j][k]);
9852: }
9853: else
9854: fprintf(ficlog," %lf",param[i][j][k]);
9855: fprintf(ficparo," %lf",param[i][j][k]);
9856: }
9857: fscanf(ficpar,"\n");
9858: numlinepar++;
9859: if(mle==1)
9860: printf("\n");
9861: fprintf(ficlog,"\n");
9862: fprintf(ficparo,"\n");
1.126 brouard 9863: }
9864: }
9865: fflush(ficlog);
1.234 brouard 9866:
1.145 brouard 9867: /* Reads scales values */
1.126 brouard 9868: p=param[1][1];
9869:
9870: /* Reads comments: lines beginning with '#' */
9871: while((c=getc(ficpar))=='#' && c!= EOF){
9872: ungetc(c,ficpar);
9873: fgets(line, MAXLINE, ficpar);
9874: numlinepar++;
1.141 brouard 9875: fputs(line,stdout);
1.126 brouard 9876: fputs(line,ficparo);
9877: fputs(line,ficlog);
9878: }
9879: ungetc(c,ficpar);
9880:
9881: for(i=1; i <=nlstate; i++){
9882: for(j=1; j <=nlstate+ndeath-1; j++){
1.234 brouard 9883: fscanf(ficpar,"%1d%1d",&i1,&j1);
9884: if ( (i1-i) * (j1-j) != 0){
9885: printf("Error in line parameters number %d, %1d%1d instead of %1d%1d \n",numlinepar, i,j, i1, j1);
9886: exit(1);
9887: }
9888: printf("%1d%1d",i,j);
9889: fprintf(ficparo,"%1d%1d",i1,j1);
9890: fprintf(ficlog,"%1d%1d",i1,j1);
9891: for(k=1; k<=ncovmodel;k++){
9892: fscanf(ficpar,"%le",&delti3[i][j][k]);
9893: printf(" %le",delti3[i][j][k]);
9894: fprintf(ficparo," %le",delti3[i][j][k]);
9895: fprintf(ficlog," %le",delti3[i][j][k]);
9896: }
9897: fscanf(ficpar,"\n");
9898: numlinepar++;
9899: printf("\n");
9900: fprintf(ficparo,"\n");
9901: fprintf(ficlog,"\n");
1.126 brouard 9902: }
9903: }
9904: fflush(ficlog);
1.234 brouard 9905:
1.145 brouard 9906: /* Reads covariance matrix */
1.126 brouard 9907: delti=delti3[1][1];
1.220 brouard 9908:
9909:
1.126 brouard 9910: /* free_ma3x(delti3,1,nlstate,1,nlstate+ndeath-1,1,ncovmodel); */ /* Hasn't to to freed here otherwise delti is no more allocated */
1.220 brouard 9911:
1.126 brouard 9912: /* Reads comments: lines beginning with '#' */
9913: while((c=getc(ficpar))=='#' && c!= EOF){
9914: ungetc(c,ficpar);
9915: fgets(line, MAXLINE, ficpar);
9916: numlinepar++;
1.141 brouard 9917: fputs(line,stdout);
1.126 brouard 9918: fputs(line,ficparo);
9919: fputs(line,ficlog);
9920: }
9921: ungetc(c,ficpar);
1.220 brouard 9922:
1.126 brouard 9923: matcov=matrix(1,npar,1,npar);
1.203 brouard 9924: hess=matrix(1,npar,1,npar);
1.131 brouard 9925: for(i=1; i <=npar; i++)
9926: for(j=1; j <=npar; j++) matcov[i][j]=0.;
1.220 brouard 9927:
1.194 brouard 9928: /* Scans npar lines */
1.126 brouard 9929: for(i=1; i <=npar; i++){
1.226 brouard 9930: count=fscanf(ficpar,"%1d%1d%d",&i1,&j1,&jk);
1.194 brouard 9931: if(count != 3){
1.226 brouard 9932: printf("Error! Error in parameter file %s at line %d after line starting with %1d%1d%1d\n\
1.194 brouard 9933: This is probably because your covariance matrix doesn't \n contain exactly %d lines corresponding to your model line '1+age+%s'.\n\
9934: Please run with mle=-1 to get a correct covariance matrix.\n",optionfile,numlinepar, i1,j1,jk, npar, model);
1.226 brouard 9935: fprintf(ficlog,"Error! Error in parameter file %s at line %d after line starting with %1d%1d%1d\n\
1.194 brouard 9936: This is probably because your covariance matrix doesn't \n contain exactly %d lines corresponding to your model line '1+age+%s'.\n\
9937: Please run with mle=-1 to get a correct covariance matrix.\n",optionfile,numlinepar, i1,j1,jk, npar, model);
1.226 brouard 9938: exit(1);
1.220 brouard 9939: }else{
1.226 brouard 9940: if(mle==1)
9941: printf("%1d%1d%d",i1,j1,jk);
9942: }
9943: fprintf(ficlog,"%1d%1d%d",i1,j1,jk);
9944: fprintf(ficparo,"%1d%1d%d",i1,j1,jk);
1.126 brouard 9945: for(j=1; j <=i; j++){
1.226 brouard 9946: fscanf(ficpar," %le",&matcov[i][j]);
9947: if(mle==1){
9948: printf(" %.5le",matcov[i][j]);
9949: }
9950: fprintf(ficlog," %.5le",matcov[i][j]);
9951: fprintf(ficparo," %.5le",matcov[i][j]);
1.126 brouard 9952: }
9953: fscanf(ficpar,"\n");
9954: numlinepar++;
9955: if(mle==1)
1.220 brouard 9956: printf("\n");
1.126 brouard 9957: fprintf(ficlog,"\n");
9958: fprintf(ficparo,"\n");
9959: }
1.194 brouard 9960: /* End of read covariance matrix npar lines */
1.126 brouard 9961: for(i=1; i <=npar; i++)
9962: for(j=i+1;j<=npar;j++)
1.226 brouard 9963: matcov[i][j]=matcov[j][i];
1.126 brouard 9964:
9965: if(mle==1)
9966: printf("\n");
9967: fprintf(ficlog,"\n");
9968:
9969: fflush(ficlog);
9970:
9971: /*-------- Rewriting parameter file ----------*/
9972: strcpy(rfileres,"r"); /* "Rparameterfile */
9973: strcat(rfileres,optionfilefiname); /* Parameter file first name*/
9974: strcat(rfileres,"."); /* */
9975: strcat(rfileres,optionfilext); /* Other files have txt extension */
9976: if((ficres =fopen(rfileres,"w"))==NULL) {
1.201 brouard 9977: printf("Problem writing new parameter file: %s\n", rfileres);goto end;
9978: fprintf(ficlog,"Problem writing new parameter file: %s\n", rfileres);goto end;
1.126 brouard 9979: }
9980: fprintf(ficres,"#%s\n",version);
9981: } /* End of mle != -3 */
1.218 brouard 9982:
1.186 brouard 9983: /* Main data
9984: */
1.126 brouard 9985: n= lastobs;
9986: num=lvector(1,n);
9987: moisnais=vector(1,n);
9988: annais=vector(1,n);
9989: moisdc=vector(1,n);
9990: andc=vector(1,n);
1.220 brouard 9991: weight=vector(1,n);
1.126 brouard 9992: agedc=vector(1,n);
9993: cod=ivector(1,n);
1.220 brouard 9994: for(i=1;i<=n;i++){
1.234 brouard 9995: num[i]=0;
9996: moisnais[i]=0;
9997: annais[i]=0;
9998: moisdc[i]=0;
9999: andc[i]=0;
10000: agedc[i]=0;
10001: cod[i]=0;
10002: weight[i]=1.0; /* Equal weights, 1 by default */
10003: }
1.126 brouard 10004: mint=matrix(1,maxwav,1,n);
10005: anint=matrix(1,maxwav,1,n);
1.131 brouard 10006: s=imatrix(1,maxwav+1,1,n); /* s[i][j] health state for wave i and individual j */
1.126 brouard 10007: tab=ivector(1,NCOVMAX);
1.144 brouard 10008: ncodemax=ivector(1,NCOVMAX); /* Number of code per covariate; if O and 1 only, 2**ncov; V1+V2+V3+V4=>16 */
1.192 brouard 10009: 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 10010:
1.136 brouard 10011: /* Reads data from file datafile */
10012: if (readdata(datafile, firstobs, lastobs, &imx)==1)
10013: goto end;
10014:
10015: /* Calculation of the number of parameters from char model */
1.234 brouard 10016: /* modelsav=V2+V1+V4+age*V3 strb=age*V3 stra=V2+V1+V4
1.137 brouard 10017: k=4 (age*V3) Tvar[k=4]= 3 (from V3) Tag[cptcovage=1]=4
10018: k=3 V4 Tvar[k=3]= 4 (from V4)
10019: k=2 V1 Tvar[k=2]= 1 (from V1)
10020: k=1 Tvar[1]=2 (from V2)
1.234 brouard 10021: */
10022:
10023: Tvar=ivector(1,NCOVMAX); /* Was 15 changed to NCOVMAX. */
10024: TvarsDind=ivector(1,NCOVMAX); /* */
10025: TvarsD=ivector(1,NCOVMAX); /* */
10026: TvarsQind=ivector(1,NCOVMAX); /* */
10027: TvarsQ=ivector(1,NCOVMAX); /* */
1.232 brouard 10028: TvarF=ivector(1,NCOVMAX); /* */
10029: TvarFind=ivector(1,NCOVMAX); /* */
10030: TvarV=ivector(1,NCOVMAX); /* */
10031: TvarVind=ivector(1,NCOVMAX); /* */
10032: TvarA=ivector(1,NCOVMAX); /* */
10033: TvarAind=ivector(1,NCOVMAX); /* */
1.231 brouard 10034: TvarFD=ivector(1,NCOVMAX); /* */
10035: TvarFDind=ivector(1,NCOVMAX); /* */
10036: TvarFQ=ivector(1,NCOVMAX); /* */
10037: TvarFQind=ivector(1,NCOVMAX); /* */
10038: TvarVD=ivector(1,NCOVMAX); /* */
10039: TvarVDind=ivector(1,NCOVMAX); /* */
10040: TvarVQ=ivector(1,NCOVMAX); /* */
10041: TvarVQind=ivector(1,NCOVMAX); /* */
10042:
1.230 brouard 10043: Tvalsel=vector(1,NCOVMAX); /* */
1.233 brouard 10044: Tvarsel=ivector(1,NCOVMAX); /* */
1.226 brouard 10045: Typevar=ivector(-1,NCOVMAX); /* -1 to 2 */
10046: Fixed=ivector(-1,NCOVMAX); /* -1 to 3 */
10047: Dummy=ivector(-1,NCOVMAX); /* -1 to 3 */
1.238 brouard 10048: DummyV=ivector(1,NCOVMAX); /* 1 to 3 */
10049: FixedV=ivector(1,NCOVMAX); /* 1 to 3 */
1.137 brouard 10050: /* V2+V1+V4+age*V3 is a model with 4 covariates (3 plus signs).
10051: For each model-covariate stores the data-covariate id. Tvar[1]=2, Tvar[2]=1, Tvar[3]=4,
10052: Tvar[4=age*V3] is 3 and 'age' is recorded in Tage.
10053: */
10054: /* For model-covariate k tells which data-covariate to use but
10055: because this model-covariate is a construction we invent a new column
10056: ncovcol + k1
10057: If already ncovcol=4 and model=V2+V1+V1*V4+age*V3
10058: Tvar[3=V1*V4]=4+1 etc */
1.227 brouard 10059: Tprod=ivector(1,NCOVMAX); /* Gives the k position of the k1 product */
10060: Tposprod=ivector(1,NCOVMAX); /* Gives the k1 product from the k position */
1.137 brouard 10061: /* Tprod[k1=1]=3(=V1*V4) for V2+V1+V1*V4+age*V3
10062: if V2+V1+V1*V4+age*V3+V3*V2 TProd[k1=2]=5 (V3*V2)
1.227 brouard 10063: Tposprod[k]=k1 , Tposprod[3]=1, Tposprod[5]=2
1.137 brouard 10064: */
1.145 brouard 10065: Tvaraff=ivector(1,NCOVMAX); /* Unclear */
10066: 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 10067: * For V3*V2 (in V2+V1+V1*V4+age*V3+V3*V2), V3*V2 position is 2nd.
10068: * Tvard[k1=2][1]=3 (V3) Tvard[k1=2][2]=2(V2) */
1.145 brouard 10069: Tage=ivector(1,NCOVMAX); /* Gives the covariate id of covariates associated with age: V2 + V1 + age*V4 + V3*age
1.137 brouard 10070: 4 covariates (3 plus signs)
10071: Tage[1=V3*age]= 4; Tage[2=age*V4] = 3
10072: */
1.230 brouard 10073: Tmodelind=ivector(1,NCOVMAX);/** gives the k model position of an
1.227 brouard 10074: * individual dummy, fixed or varying:
10075: * Tmodelind[Tvaraff[3]]=9,Tvaraff[1]@9={4,
10076: * 3, 1, 0, 0, 0, 0, 0, 0},
1.230 brouard 10077: * model=V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 ,
10078: * V1 df, V2 qf, V3 & V4 dv, V5 qv
10079: * Tmodelind[1]@9={9,0,3,2,}*/
10080: TmodelInvind=ivector(1,NCOVMAX); /* TmodelInvind=Tvar[k]- ncovcol-nqv={5-2-1=2,*/
10081: TmodelInvQind=ivector(1,NCOVMAX);/** gives the k model position of an
1.228 brouard 10082: * individual quantitative, fixed or varying:
10083: * Tmodelqind[1]=1,Tvaraff[1]@9={4,
10084: * 3, 1, 0, 0, 0, 0, 0, 0},
10085: * model=V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1*/
1.186 brouard 10086: /* Main decodemodel */
10087:
1.187 brouard 10088:
1.223 brouard 10089: if(decodemodel(model, lastobs) == 1) /* In order to get Tvar[k] V4+V3+V5 p Tvar[1]@3 = {4, 3, 5}*/
1.136 brouard 10090: goto end;
10091:
1.137 brouard 10092: if((double)(lastobs-imx)/(double)imx > 1.10){
10093: nbwarn++;
10094: 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);
10095: 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);
10096: }
1.136 brouard 10097: /* if(mle==1){*/
1.137 brouard 10098: if (weightopt != 1) { /* Maximisation without weights. We can have weights different from 1 but want no weight*/
10099: for(i=1;i<=imx;i++) weight[i]=1.0; /* changed to imx */
1.136 brouard 10100: }
10101:
10102: /*-calculation of age at interview from date of interview and age at death -*/
10103: agev=matrix(1,maxwav,1,imx);
10104:
10105: if(calandcheckages(imx, maxwav, &agemin, &agemax, &nberr, &nbwarn) == 1)
10106: goto end;
10107:
1.126 brouard 10108:
1.136 brouard 10109: agegomp=(int)agemin;
10110: free_vector(moisnais,1,n);
10111: free_vector(annais,1,n);
1.126 brouard 10112: /* free_matrix(mint,1,maxwav,1,n);
10113: free_matrix(anint,1,maxwav,1,n);*/
1.215 brouard 10114: /* free_vector(moisdc,1,n); */
10115: /* free_vector(andc,1,n); */
1.145 brouard 10116: /* */
10117:
1.126 brouard 10118: wav=ivector(1,imx);
1.214 brouard 10119: /* dh=imatrix(1,lastpass-firstpass+1,1,imx); */
10120: /* bh=imatrix(1,lastpass-firstpass+1,1,imx); */
10121: /* mw=imatrix(1,lastpass-firstpass+1,1,imx); */
10122: 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.*/
10123: bh=imatrix(1,lastpass-firstpass+2,1,imx);
10124: mw=imatrix(1,lastpass-firstpass+2,1,imx);
1.126 brouard 10125:
10126: /* Concatenates waves */
1.214 brouard 10127: /* Concatenates waves: wav[i] is the number of effective (useful waves) of individual i.
10128: Death is a valid wave (if date is known).
10129: mw[mi][i] is the number of (mi=1 to wav[i]) effective wave out of mi of individual i
10130: dh[m][i] or dh[mw[mi][i]][i] is the delay between two effective waves m=mw[mi][i]
10131: and mw[mi+1][i]. dh depends on stepm.
10132: */
10133:
1.126 brouard 10134: concatwav(wav, dh, bh, mw, s, agedc, agev, firstpass, lastpass, imx, nlstate, stepm);
1.145 brouard 10135: /* */
10136:
1.215 brouard 10137: free_vector(moisdc,1,n);
10138: free_vector(andc,1,n);
10139:
1.126 brouard 10140: /* Routine tricode is to calculate cptcoveff (real number of unique covariates) and to associate covariable number and modality */
10141: nbcode=imatrix(0,NCOVMAX,0,NCOVMAX);
10142: ncodemax[1]=1;
1.145 brouard 10143: Ndum =ivector(-1,NCOVMAX);
1.225 brouard 10144: cptcoveff=0;
1.220 brouard 10145: if (ncovmodel-nagesqr > 2 ){ /* That is if covariate other than cst, age and age*age */
10146: tricode(&cptcoveff,Tvar,nbcode,imx, Ndum); /**< Fills nbcode[Tvar[j]][l]; */
1.227 brouard 10147: }
10148:
10149: ncovcombmax=pow(2,cptcoveff);
10150: invalidvarcomb=ivector(1, ncovcombmax);
10151: for(i=1;i<ncovcombmax;i++)
10152: invalidvarcomb[i]=0;
10153:
1.211 brouard 10154: /* Nbcode gives the value of the lth modality (currently 1 to 2) of jth covariate, in
1.186 brouard 10155: V2+V1*age, there are 3 covariates Tvar[2]=1 (V1).*/
1.211 brouard 10156: /* 1 to ncodemax[j] which is the maximum value of this jth covariate */
1.227 brouard 10157:
1.200 brouard 10158: /* codtab=imatrix(1,100,1,10);*/ /* codtab[h,k]=( (h-1) - mod(k-1,2**(k-1) )/2**(k-1) */
1.198 brouard 10159: /*printf(" codtab[1,1],codtab[100,10]=%d,%d\n", codtab[1][1],codtabm(100,10));*/
1.186 brouard 10160: /* codtab gives the value 1 or 2 of the hth combination of k covariates (1 or 2).*/
1.211 brouard 10161: /* nbcode[Tvaraff[j]][codtabm(h,j)]) : if there are only 2 modalities for a covariate j,
10162: * codtabm(h,j) gives its value classified at position h and nbcode gives how it is coded
10163: * (currently 0 or 1) in the data.
10164: * In a loop on h=1 to 2**k, and a loop on j (=1 to k), we get the value of
10165: * corresponding modality (h,j).
10166: */
10167:
1.145 brouard 10168: h=0;
10169: /*if (cptcovn > 0) */
1.126 brouard 10170: m=pow(2,cptcoveff);
10171:
1.144 brouard 10172: /**< codtab(h,k) k = codtab[h,k]=( (h-1) - mod(k-1,2**(k-1) )/2**(k-1) + 1
1.211 brouard 10173: * For k=4 covariates, h goes from 1 to m=2**k
10174: * codtabm(h,k)= (1 & (h-1) >> (k-1)) + 1;
10175: * #define codtabm(h,k) (1 & (h-1) >> (k-1))+1
1.186 brouard 10176: * h\k 1 2 3 4
1.143 brouard 10177: *______________________________
10178: * 1 i=1 1 i=1 1 i=1 1 i=1 1
10179: * 2 2 1 1 1
10180: * 3 i=2 1 2 1 1
10181: * 4 2 2 1 1
10182: * 5 i=3 1 i=2 1 2 1
10183: * 6 2 1 2 1
10184: * 7 i=4 1 2 2 1
10185: * 8 2 2 2 1
1.197 brouard 10186: * 9 i=5 1 i=3 1 i=2 1 2
10187: * 10 2 1 1 2
10188: * 11 i=6 1 2 1 2
10189: * 12 2 2 1 2
10190: * 13 i=7 1 i=4 1 2 2
10191: * 14 2 1 2 2
10192: * 15 i=8 1 2 2 2
10193: * 16 2 2 2 2
1.143 brouard 10194: */
1.212 brouard 10195: /* How to do the opposite? From combination h (=1 to 2**k) how to get the value on the covariates? */
1.211 brouard 10196: /* from h=5 and m, we get then number of covariates k=log(m)/log(2)=4
10197: * and the value of each covariate?
10198: * V1=1, V2=1, V3=2, V4=1 ?
10199: * h-1=4 and 4 is 0100 or reverse 0010, and +1 is 1121 ok.
10200: * h=6, 6-1=5, 5 is 0101, 1010, 2121, V1=2nd, V2=1st, V3=2nd, V4=1st.
10201: * In order to get the real value in the data, we use nbcode
10202: * nbcode[Tvar[3][2nd]]=1 and nbcode[Tvar[4][1]]=0
10203: * We are keeping this crazy system in order to be able (in the future?)
10204: * to have more than 2 values (0 or 1) for a covariate.
10205: * #define codtabm(h,k) (1 & (h-1) >> (k-1))+1
10206: * h=6, k=2? h-1=5=0101, reverse 1010, +1=2121, k=2nd position: value is 1: codtabm(6,2)=1
10207: * bbbbbbbb
10208: * 76543210
10209: * h-1 00000101 (6-1=5)
1.219 brouard 10210: *(h-1)>>(k-1)= 00000010 >> (2-1) = 1 right shift
1.211 brouard 10211: * &
10212: * 1 00000001 (1)
1.219 brouard 10213: * 00000000 = 1 & ((h-1) >> (k-1))
10214: * +1= 00000001 =1
1.211 brouard 10215: *
10216: * h=14, k=3 => h'=h-1=13, k'=k-1=2
10217: * h' 1101 =2^3+2^2+0x2^1+2^0
10218: * >>k' 11
10219: * & 00000001
10220: * = 00000001
10221: * +1 = 00000010=2 = codtabm(14,3)
10222: * Reverse h=6 and m=16?
10223: * cptcoveff=log(16)/log(2)=4 covariate: 6-1=5=0101 reversed=1010 +1=2121 =>V1=2, V2=1, V3=2, V4=1.
10224: * for (j=1 to cptcoveff) Vj=decodtabm(j,h,cptcoveff)
10225: * decodtabm(h,j,cptcoveff)= (((h-1) >> (j-1)) & 1) +1
10226: * decodtabm(h,j,cptcoveff)= (h <= (1<<cptcoveff)?(((h-1) >> (j-1)) & 1) +1 : -1)
10227: * V3=decodtabm(14,3,2**4)=2
10228: * h'=13 1101 =2^3+2^2+0x2^1+2^0
10229: *(h-1) >> (j-1) 0011 =13 >> 2
10230: * &1 000000001
10231: * = 000000001
10232: * +1= 000000010 =2
10233: * 2211
10234: * V1=1+1, V2=0+1, V3=1+1, V4=1+1
10235: * V3=2
1.220 brouard 10236: * codtabm and decodtabm are identical
1.211 brouard 10237: */
10238:
1.145 brouard 10239:
10240: free_ivector(Ndum,-1,NCOVMAX);
10241:
10242:
1.126 brouard 10243:
1.186 brouard 10244: /* Initialisation of ----------- gnuplot -------------*/
1.126 brouard 10245: strcpy(optionfilegnuplot,optionfilefiname);
10246: if(mle==-3)
1.201 brouard 10247: strcat(optionfilegnuplot,"-MORT_");
1.126 brouard 10248: strcat(optionfilegnuplot,".gp");
10249:
10250: if((ficgp=fopen(optionfilegnuplot,"w"))==NULL) {
10251: printf("Problem with file %s",optionfilegnuplot);
10252: }
10253: else{
1.204 brouard 10254: fprintf(ficgp,"\n# IMaCh-%s\n", version);
1.126 brouard 10255: fprintf(ficgp,"# %s\n", optionfilegnuplot);
1.141 brouard 10256: //fprintf(ficgp,"set missing 'NaNq'\n");
10257: fprintf(ficgp,"set datafile missing 'NaNq'\n");
1.126 brouard 10258: }
10259: /* fclose(ficgp);*/
1.186 brouard 10260:
10261:
10262: /* Initialisation of --------- index.htm --------*/
1.126 brouard 10263:
10264: strcpy(optionfilehtm,optionfilefiname); /* Main html file */
10265: if(mle==-3)
1.201 brouard 10266: strcat(optionfilehtm,"-MORT_");
1.126 brouard 10267: strcat(optionfilehtm,".htm");
10268: if((fichtm=fopen(optionfilehtm,"w"))==NULL) {
1.131 brouard 10269: printf("Problem with %s \n",optionfilehtm);
10270: exit(0);
1.126 brouard 10271: }
10272:
10273: strcpy(optionfilehtmcov,optionfilefiname); /* Only for matrix of covariance */
10274: strcat(optionfilehtmcov,"-cov.htm");
10275: if((fichtmcov=fopen(optionfilehtmcov,"w"))==NULL) {
10276: printf("Problem with %s \n",optionfilehtmcov), exit(0);
10277: }
10278: else{
10279: fprintf(fichtmcov,"<html><head>\n<title>IMaCh Cov %s</title></head>\n <body><font size=\"2\">%s <br> %s</font> \
10280: <hr size=\"2\" color=\"#EC5E5E\"> \n\
1.204 brouard 10281: Title=%s <br>Datafile=%s Firstpass=%d Lastpass=%d Stepm=%d Weight=%d Model=1+age+%s<br>\n",\
1.126 brouard 10282: optionfilehtmcov,version,fullversion,title,datafile,firstpass,lastpass,stepm, weightopt, model);
10283: }
10284:
1.213 brouard 10285: 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 10286: <hr size=\"2\" color=\"#EC5E5E\"> \n\
10287: <font size=\"2\">IMaCh-%s <br> %s</font> \
1.126 brouard 10288: <hr size=\"2\" color=\"#EC5E5E\"> \n\
1.204 brouard 10289: Title=%s <br>Datafile=%s Firstpass=%d Lastpass=%d Stepm=%d Weight=%d Model=1+age+%s<br>\n\
1.126 brouard 10290: \n\
10291: <hr size=\"2\" color=\"#EC5E5E\">\
10292: <ul><li><h4>Parameter files</h4>\n\
10293: - Parameter file: <a href=\"%s.%s\">%s.%s</a><br>\n\
10294: - Copy of the parameter file: <a href=\"o%s\">o%s</a><br>\n\
10295: - Log file of the run: <a href=\"%s\">%s</a><br>\n\
10296: - Gnuplot file name: <a href=\"%s\">%s</a><br>\n\
10297: - Date and time at start: %s</ul>\n",\
10298: optionfilehtm,version,fullversion,title,datafile,firstpass,lastpass,stepm, weightopt, model,\
10299: optionfilefiname,optionfilext,optionfilefiname,optionfilext,\
10300: fileres,fileres,\
10301: filelog,filelog,optionfilegnuplot,optionfilegnuplot,strstart);
10302: fflush(fichtm);
10303:
10304: strcpy(pathr,path);
10305: strcat(pathr,optionfilefiname);
1.184 brouard 10306: #ifdef WIN32
10307: _chdir(optionfilefiname); /* Move to directory named optionfile */
10308: #else
1.126 brouard 10309: chdir(optionfilefiname); /* Move to directory named optionfile */
1.184 brouard 10310: #endif
10311:
1.126 brouard 10312:
1.220 brouard 10313: /* Calculates basic frequencies. Computes observed prevalence at single age
10314: and for any valid combination of covariates
1.126 brouard 10315: and prints on file fileres'p'. */
1.227 brouard 10316: freqsummary(fileres, agemin, agemax, s, agev, nlstate, imx, Tvaraff, invalidvarcomb, nbcode, ncodemax,mint,anint,strstart, \
10317: firstpass, lastpass, stepm, weightopt, model);
1.126 brouard 10318:
10319: fprintf(fichtm,"\n");
10320: fprintf(fichtm,"<br>Total number of observations=%d <br>\n\
10321: Youngest age at first (selected) pass %.2f, oldest age %.2f<br>\n\
10322: Interval (in months) between two waves: Min=%d Max=%d Mean=%.2lf<br>\n",\
10323: imx,agemin,agemax,jmin,jmax,jmean);
10324: pmmij= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
1.220 brouard 10325: oldms= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
10326: newms= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
10327: savms= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
10328: oldm=oldms; newm=newms; savm=savms; /* Keeps fixed addresses to free */
1.218 brouard 10329:
1.126 brouard 10330: /* For Powell, parameters are in a vector p[] starting at p[1]
10331: so we point p on param[1][1] so that p[1] maps on param[1][1][1] */
10332: p=param[1][1]; /* *(*(*(param +1)+1)+0) */
10333:
10334: globpr=0; /* To get the number ipmx of contributions and the sum of weights*/
1.186 brouard 10335: /* For mortality only */
1.126 brouard 10336: if (mle==-3){
1.136 brouard 10337: ximort=matrix(1,NDIM,1,NDIM);
1.220 brouard 10338: for(i=1;i<=NDIM;i++)
10339: for(j=1;j<=NDIM;j++)
10340: ximort[i][j]=0.;
1.186 brouard 10341: /* ximort=gsl_matrix_alloc(1,NDIM,1,NDIM); */
1.126 brouard 10342: cens=ivector(1,n);
10343: ageexmed=vector(1,n);
10344: agecens=vector(1,n);
10345: dcwave=ivector(1,n);
1.223 brouard 10346:
1.126 brouard 10347: for (i=1; i<=imx; i++){
10348: dcwave[i]=-1;
10349: for (m=firstpass; m<=lastpass; m++)
1.226 brouard 10350: if (s[m][i]>nlstate) {
10351: dcwave[i]=m;
10352: /* printf("i=%d j=%d s=%d dcwave=%d\n",i,j, s[j][i],dcwave[i]);*/
10353: break;
10354: }
1.126 brouard 10355: }
1.226 brouard 10356:
1.126 brouard 10357: for (i=1; i<=imx; i++) {
10358: if (wav[i]>0){
1.226 brouard 10359: ageexmed[i]=agev[mw[1][i]][i];
10360: j=wav[i];
10361: agecens[i]=1.;
10362:
10363: if (ageexmed[i]> 1 && wav[i] > 0){
10364: agecens[i]=agev[mw[j][i]][i];
10365: cens[i]= 1;
10366: }else if (ageexmed[i]< 1)
10367: cens[i]= -1;
10368: if (agedc[i]< AGESUP && agedc[i]>1 && dcwave[i]>firstpass && dcwave[i]<=lastpass)
10369: cens[i]=0 ;
1.126 brouard 10370: }
10371: else cens[i]=-1;
10372: }
10373:
10374: for (i=1;i<=NDIM;i++) {
10375: for (j=1;j<=NDIM;j++)
1.226 brouard 10376: ximort[i][j]=(i == j ? 1.0 : 0.0);
1.126 brouard 10377: }
10378:
1.145 brouard 10379: /*p[1]=0.0268; p[NDIM]=0.083;*/
1.126 brouard 10380: /*printf("%lf %lf", p[1], p[2]);*/
10381:
10382:
1.136 brouard 10383: #ifdef GSL
10384: printf("GSL optimization\n"); fprintf(ficlog,"Powell\n");
1.162 brouard 10385: #else
1.126 brouard 10386: printf("Powell\n"); fprintf(ficlog,"Powell\n");
1.136 brouard 10387: #endif
1.201 brouard 10388: strcpy(filerespow,"POW-MORT_");
10389: strcat(filerespow,fileresu);
1.126 brouard 10390: if((ficrespow=fopen(filerespow,"w"))==NULL) {
10391: printf("Problem with resultfile: %s\n", filerespow);
10392: fprintf(ficlog,"Problem with resultfile: %s\n", filerespow);
10393: }
1.136 brouard 10394: #ifdef GSL
10395: fprintf(ficrespow,"# GSL optimization\n# iter -2*LL");
1.162 brouard 10396: #else
1.126 brouard 10397: fprintf(ficrespow,"# Powell\n# iter -2*LL");
1.136 brouard 10398: #endif
1.126 brouard 10399: /* for (i=1;i<=nlstate;i++)
10400: for(j=1;j<=nlstate+ndeath;j++)
10401: if(j!=i)fprintf(ficrespow," p%1d%1d",i,j);
10402: */
10403: fprintf(ficrespow,"\n");
1.136 brouard 10404: #ifdef GSL
10405: /* gsl starts here */
10406: T = gsl_multimin_fminimizer_nmsimplex;
10407: gsl_multimin_fminimizer *sfm = NULL;
10408: gsl_vector *ss, *x;
10409: gsl_multimin_function minex_func;
10410:
10411: /* Initial vertex size vector */
10412: ss = gsl_vector_alloc (NDIM);
10413:
10414: if (ss == NULL){
10415: GSL_ERROR_VAL ("failed to allocate space for ss", GSL_ENOMEM, 0);
10416: }
10417: /* Set all step sizes to 1 */
10418: gsl_vector_set_all (ss, 0.001);
10419:
10420: /* Starting point */
1.126 brouard 10421:
1.136 brouard 10422: x = gsl_vector_alloc (NDIM);
10423:
10424: if (x == NULL){
10425: gsl_vector_free(ss);
10426: GSL_ERROR_VAL ("failed to allocate space for x", GSL_ENOMEM, 0);
10427: }
10428:
10429: /* Initialize method and iterate */
10430: /* p[1]=0.0268; p[NDIM]=0.083; */
1.186 brouard 10431: /* gsl_vector_set(x, 0, 0.0268); */
10432: /* gsl_vector_set(x, 1, 0.083); */
1.136 brouard 10433: gsl_vector_set(x, 0, p[1]);
10434: gsl_vector_set(x, 1, p[2]);
10435:
10436: minex_func.f = &gompertz_f;
10437: minex_func.n = NDIM;
10438: minex_func.params = (void *)&p; /* ??? */
10439:
10440: sfm = gsl_multimin_fminimizer_alloc (T, NDIM);
10441: gsl_multimin_fminimizer_set (sfm, &minex_func, x, ss);
10442:
10443: printf("Iterations beginning .....\n\n");
10444: printf("Iter. # Intercept Slope -Log Likelihood Simplex size\n");
10445:
10446: iteri=0;
10447: while (rval == GSL_CONTINUE){
10448: iteri++;
10449: status = gsl_multimin_fminimizer_iterate(sfm);
10450:
10451: if (status) printf("error: %s\n", gsl_strerror (status));
10452: fflush(0);
10453:
10454: if (status)
10455: break;
10456:
10457: rval = gsl_multimin_test_size (gsl_multimin_fminimizer_size (sfm), 1e-6);
10458: ssval = gsl_multimin_fminimizer_size (sfm);
10459:
10460: if (rval == GSL_SUCCESS)
10461: printf ("converged to a local maximum at\n");
10462:
10463: printf("%5d ", iteri);
10464: for (it = 0; it < NDIM; it++){
10465: printf ("%10.5f ", gsl_vector_get (sfm->x, it));
10466: }
10467: printf("f() = %-10.5f ssize = %.7f\n", sfm->fval, ssval);
10468: }
10469:
10470: printf("\n\n Please note: Program should be run many times with varying starting points to detemine global maximum\n\n");
10471:
10472: gsl_vector_free(x); /* initial values */
10473: gsl_vector_free(ss); /* inital step size */
10474: for (it=0; it<NDIM; it++){
10475: p[it+1]=gsl_vector_get(sfm->x,it);
10476: fprintf(ficrespow," %.12lf", p[it]);
10477: }
10478: gsl_multimin_fminimizer_free (sfm); /* p *(sfm.x.data) et p *(sfm.x.data+1) */
10479: #endif
10480: #ifdef POWELL
10481: powell(p,ximort,NDIM,ftol,&iter,&fret,gompertz);
10482: #endif
1.126 brouard 10483: fclose(ficrespow);
10484:
1.203 brouard 10485: hesscov(matcov, hess, p, NDIM, delti, 1e-4, gompertz);
1.126 brouard 10486:
10487: for(i=1; i <=NDIM; i++)
10488: for(j=i+1;j<=NDIM;j++)
1.220 brouard 10489: matcov[i][j]=matcov[j][i];
1.126 brouard 10490:
10491: printf("\nCovariance matrix\n ");
1.203 brouard 10492: fprintf(ficlog,"\nCovariance matrix\n ");
1.126 brouard 10493: for(i=1; i <=NDIM; i++) {
10494: for(j=1;j<=NDIM;j++){
1.220 brouard 10495: printf("%f ",matcov[i][j]);
10496: fprintf(ficlog,"%f ",matcov[i][j]);
1.126 brouard 10497: }
1.203 brouard 10498: printf("\n "); fprintf(ficlog,"\n ");
1.126 brouard 10499: }
10500:
10501: printf("iter=%d MLE=%f Eq=%lf*exp(%lf*(age-%d))\n",iter,-gompertz(p),p[1],p[2],agegomp);
1.193 brouard 10502: for (i=1;i<=NDIM;i++) {
1.126 brouard 10503: printf("%f [%f ; %f]\n",p[i],p[i]-2*sqrt(matcov[i][i]),p[i]+2*sqrt(matcov[i][i]));
1.193 brouard 10504: fprintf(ficlog,"%f [%f ; %f]\n",p[i],p[i]-2*sqrt(matcov[i][i]),p[i]+2*sqrt(matcov[i][i]));
10505: }
1.126 brouard 10506: lsurv=vector(1,AGESUP);
10507: lpop=vector(1,AGESUP);
10508: tpop=vector(1,AGESUP);
10509: lsurv[agegomp]=100000;
10510:
10511: for (k=agegomp;k<=AGESUP;k++) {
10512: agemortsup=k;
10513: if (p[1]*exp(p[2]*(k-agegomp))>1) break;
10514: }
10515:
10516: for (k=agegomp;k<agemortsup;k++)
10517: lsurv[k+1]=lsurv[k]-lsurv[k]*(p[1]*exp(p[2]*(k-agegomp)));
10518:
10519: for (k=agegomp;k<agemortsup;k++){
10520: lpop[k]=(lsurv[k]+lsurv[k+1])/2.;
10521: sumlpop=sumlpop+lpop[k];
10522: }
10523:
10524: tpop[agegomp]=sumlpop;
10525: for (k=agegomp;k<(agemortsup-3);k++){
10526: /* tpop[k+1]=2;*/
10527: tpop[k+1]=tpop[k]-lpop[k];
10528: }
10529:
10530:
10531: printf("\nAge lx qx dx Lx Tx e(x)\n");
10532: for (k=agegomp;k<(agemortsup-2);k++)
10533: 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]);
10534:
10535:
10536: replace_back_to_slash(pathc,pathcd); /* Even gnuplot wants a / */
1.220 brouard 10537: ageminpar=50;
10538: agemaxpar=100;
1.194 brouard 10539: if(ageminpar == AGEOVERFLOW ||agemaxpar == AGEOVERFLOW){
10540: printf("Warning! Error in gnuplot file with ageminpar %f or agemaxpar %f overflow\n\
10541: This is probably because your parameter file doesn't \n contain the exact number of lines (or columns) corresponding to your model line.\n\
10542: Please run with mle=-1 to get a correct covariance matrix.\n",ageminpar,agemaxpar);
10543: fprintf(ficlog,"Warning! Error in gnuplot file with ageminpar %f or agemaxpar %f overflow\n\
10544: This is probably because your parameter file doesn't \n contain the exact number of lines (or columns) corresponding to your model line.\n\
10545: Please run with mle=-1 to get a correct covariance matrix.\n",ageminpar,agemaxpar);
1.220 brouard 10546: }else{
10547: printf("Warning! ageminpar %f and agemaxpar %f have been fixed because for simplification until it is fixed...\n\n",ageminpar,agemaxpar);
10548: fprintf(ficlog,"Warning! ageminpar %f and agemaxpar %f have been fixed because for simplification until it is fixed...\n\n",ageminpar,agemaxpar);
1.201 brouard 10549: printinggnuplotmort(fileresu, optionfilefiname,ageminpar,agemaxpar,fage, pathc,p);
1.220 brouard 10550: }
1.201 brouard 10551: printinghtmlmort(fileresu,title,datafile, firstpass, lastpass, \
1.126 brouard 10552: stepm, weightopt,\
10553: model,imx,p,matcov,agemortsup);
10554:
10555: free_vector(lsurv,1,AGESUP);
10556: free_vector(lpop,1,AGESUP);
10557: free_vector(tpop,1,AGESUP);
1.220 brouard 10558: free_matrix(ximort,1,NDIM,1,NDIM);
1.136 brouard 10559: free_ivector(cens,1,n);
10560: free_vector(agecens,1,n);
10561: free_ivector(dcwave,1,n);
1.220 brouard 10562: #ifdef GSL
1.136 brouard 10563: #endif
1.186 brouard 10564: } /* Endof if mle==-3 mortality only */
1.205 brouard 10565: /* Standard */
10566: else{ /* For mle !=- 3, could be 0 or 1 or 4 etc. */
10567: globpr=0;/* Computes sum of likelihood for globpr=1 and funcone */
10568: /* Computes likelihood for initial parameters, uses funcone to compute gpimx and gsw */
1.132 brouard 10569: likelione(ficres, p, npar, nlstate, &globpr, &ipmx, &sw, &fretone, funcone); /* Prints the contributions to the likelihood */
1.126 brouard 10570: printf("First Likeli=%12.6f ipmx=%ld sw=%12.6f",fretone,ipmx,sw);
10571: for (k=1; k<=npar;k++)
10572: printf(" %d %8.5f",k,p[k]);
10573: printf("\n");
1.205 brouard 10574: if(mle>=1){ /* Could be 1 or 2, Real Maximization */
10575: /* mlikeli uses func not funcone */
10576: mlikeli(ficres,p, npar, ncovmodel, nlstate, ftol, func);
10577: }
10578: if(mle==0) {/* No optimization, will print the likelihoods for the datafile */
10579: globpr=0;/* Computes sum of likelihood for globpr=1 and funcone */
10580: /* Computes likelihood for initial parameters, uses funcone to compute gpimx and gsw */
10581: likelione(ficres, p, npar, nlstate, &globpr, &ipmx, &sw, &fretone, funcone); /* Prints the contributions to the likelihood */
10582: }
10583: globpr=1; /* again, to print the individual contributions using computed gpimx and gsw */
1.126 brouard 10584: likelione(ficres, p, npar, nlstate, &globpr, &ipmx, &sw, &fretone, funcone); /* Prints the contributions to the likelihood */
10585: printf("Second Likeli=%12.6f ipmx=%ld sw=%12.6f",fretone,ipmx,sw);
10586: for (k=1; k<=npar;k++)
10587: printf(" %d %8.5f",k,p[k]);
10588: printf("\n");
10589:
10590: /*--------- results files --------------*/
1.224 brouard 10591: fprintf(ficres,"title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\nftol=%e stepm=%d ncovcol=%d nqv=%d ntv=%d nqtv=%d nlstate=%d ndeath=%d maxwav=%d mle= 0 weight=%d\nmodel=1+age+%s.\n", title, datafile, lastobs, firstpass,lastpass,ftol, stepm, ncovcol, nqv, ntv, nqtv, nlstate, ndeath, maxwav, weightopt,model);
1.126 brouard 10592:
10593:
10594: fprintf(ficres,"# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n");
10595: printf("# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n");
10596: fprintf(ficlog,"# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n");
10597: for(i=1,jk=1; i <=nlstate; i++){
10598: for(k=1; k <=(nlstate+ndeath); k++){
1.225 brouard 10599: if (k != i) {
10600: printf("%d%d ",i,k);
10601: fprintf(ficlog,"%d%d ",i,k);
10602: fprintf(ficres,"%1d%1d ",i,k);
10603: for(j=1; j <=ncovmodel; j++){
10604: printf("%12.7f ",p[jk]);
10605: fprintf(ficlog,"%12.7f ",p[jk]);
10606: fprintf(ficres,"%12.7f ",p[jk]);
10607: jk++;
10608: }
10609: printf("\n");
10610: fprintf(ficlog,"\n");
10611: fprintf(ficres,"\n");
10612: }
1.126 brouard 10613: }
10614: }
1.203 brouard 10615: if(mle != 0){
10616: /* Computing hessian and covariance matrix only at a peak of the Likelihood, that is after optimization */
1.126 brouard 10617: ftolhess=ftol; /* Usually correct */
1.203 brouard 10618: hesscov(matcov, hess, p, npar, delti, ftolhess, func);
10619: 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");
10620: 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");
10621: for(i=1,jk=1; i <=nlstate; i++){
1.225 brouard 10622: for(k=1; k <=(nlstate+ndeath); k++){
10623: if (k != i) {
10624: printf("%d%d ",i,k);
10625: fprintf(ficlog,"%d%d ",i,k);
10626: for(j=1; j <=ncovmodel; j++){
10627: 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]));
10628: 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]));
10629: jk++;
10630: }
10631: printf("\n");
10632: fprintf(ficlog,"\n");
10633: }
10634: }
1.193 brouard 10635: }
1.203 brouard 10636: } /* end of hesscov and Wald tests */
1.225 brouard 10637:
1.203 brouard 10638: /* */
1.126 brouard 10639: fprintf(ficres,"# Scales (for hessian or gradient estimation)\n");
10640: printf("# Scales (for hessian or gradient estimation)\n");
10641: fprintf(ficlog,"# Scales (for hessian or gradient estimation)\n");
10642: for(i=1,jk=1; i <=nlstate; i++){
10643: for(j=1; j <=nlstate+ndeath; j++){
1.225 brouard 10644: if (j!=i) {
10645: fprintf(ficres,"%1d%1d",i,j);
10646: printf("%1d%1d",i,j);
10647: fprintf(ficlog,"%1d%1d",i,j);
10648: for(k=1; k<=ncovmodel;k++){
10649: printf(" %.5e",delti[jk]);
10650: fprintf(ficlog," %.5e",delti[jk]);
10651: fprintf(ficres," %.5e",delti[jk]);
10652: jk++;
10653: }
10654: printf("\n");
10655: fprintf(ficlog,"\n");
10656: fprintf(ficres,"\n");
10657: }
1.126 brouard 10658: }
10659: }
10660:
10661: 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 10662: if(mle >= 1) /* To big for the screen */
1.126 brouard 10663: 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");
10664: 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");
10665: /* # 121 Var(a12)\n\ */
10666: /* # 122 Cov(b12,a12) Var(b12)\n\ */
10667: /* # 131 Cov(a13,a12) Cov(a13,b12, Var(a13)\n\ */
10668: /* # 132 Cov(b13,a12) Cov(b13,b12, Cov(b13,a13) Var(b13)\n\ */
10669: /* # 212 Cov(a21,a12) Cov(a21,b12, Cov(a21,a13) Cov(a21,b13) Var(a21)\n\ */
10670: /* # 212 Cov(b21,a12) Cov(b21,b12, Cov(b21,a13) Cov(b21,b13) Cov(b21,a21) Var(b21)\n\ */
10671: /* # 232 Cov(a23,a12) Cov(a23,b12, Cov(a23,a13) Cov(a23,b13) Cov(a23,a21) Cov(a23,b21) Var(a23)\n\ */
10672: /* # 232 Cov(b23,a12) Cov(b23,b12) ... Var (b23)\n" */
10673:
10674:
10675: /* Just to have a covariance matrix which will be more understandable
10676: even is we still don't want to manage dictionary of variables
10677: */
10678: for(itimes=1;itimes<=2;itimes++){
10679: jj=0;
10680: for(i=1; i <=nlstate; i++){
1.225 brouard 10681: for(j=1; j <=nlstate+ndeath; j++){
10682: if(j==i) continue;
10683: for(k=1; k<=ncovmodel;k++){
10684: jj++;
10685: ca[0]= k+'a'-1;ca[1]='\0';
10686: if(itimes==1){
10687: if(mle>=1)
10688: printf("#%1d%1d%d",i,j,k);
10689: fprintf(ficlog,"#%1d%1d%d",i,j,k);
10690: fprintf(ficres,"#%1d%1d%d",i,j,k);
10691: }else{
10692: if(mle>=1)
10693: printf("%1d%1d%d",i,j,k);
10694: fprintf(ficlog,"%1d%1d%d",i,j,k);
10695: fprintf(ficres,"%1d%1d%d",i,j,k);
10696: }
10697: ll=0;
10698: for(li=1;li <=nlstate; li++){
10699: for(lj=1;lj <=nlstate+ndeath; lj++){
10700: if(lj==li) continue;
10701: for(lk=1;lk<=ncovmodel;lk++){
10702: ll++;
10703: if(ll<=jj){
10704: cb[0]= lk +'a'-1;cb[1]='\0';
10705: if(ll<jj){
10706: if(itimes==1){
10707: if(mle>=1)
10708: printf(" Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj);
10709: fprintf(ficlog," Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj);
10710: fprintf(ficres," Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj);
10711: }else{
10712: if(mle>=1)
10713: printf(" %.5e",matcov[jj][ll]);
10714: fprintf(ficlog," %.5e",matcov[jj][ll]);
10715: fprintf(ficres," %.5e",matcov[jj][ll]);
10716: }
10717: }else{
10718: if(itimes==1){
10719: if(mle>=1)
10720: printf(" Var(%s%1d%1d)",ca,i,j);
10721: fprintf(ficlog," Var(%s%1d%1d)",ca,i,j);
10722: fprintf(ficres," Var(%s%1d%1d)",ca,i,j);
10723: }else{
10724: if(mle>=1)
10725: printf(" %.7e",matcov[jj][ll]);
10726: fprintf(ficlog," %.7e",matcov[jj][ll]);
10727: fprintf(ficres," %.7e",matcov[jj][ll]);
10728: }
10729: }
10730: }
10731: } /* end lk */
10732: } /* end lj */
10733: } /* end li */
10734: if(mle>=1)
10735: printf("\n");
10736: fprintf(ficlog,"\n");
10737: fprintf(ficres,"\n");
10738: numlinepar++;
10739: } /* end k*/
10740: } /*end j */
1.126 brouard 10741: } /* end i */
10742: } /* end itimes */
10743:
10744: fflush(ficlog);
10745: fflush(ficres);
1.225 brouard 10746: while(fgets(line, MAXLINE, ficpar)) {
10747: /* If line starts with a # it is a comment */
10748: if (line[0] == '#') {
10749: numlinepar++;
10750: fputs(line,stdout);
10751: fputs(line,ficparo);
10752: fputs(line,ficlog);
10753: continue;
10754: }else
10755: break;
10756: }
10757:
1.209 brouard 10758: /* while((c=getc(ficpar))=='#' && c!= EOF){ */
10759: /* ungetc(c,ficpar); */
10760: /* fgets(line, MAXLINE, ficpar); */
10761: /* fputs(line,stdout); */
10762: /* fputs(line,ficparo); */
10763: /* } */
10764: /* ungetc(c,ficpar); */
1.126 brouard 10765:
10766: estepm=0;
1.209 brouard 10767: if((num_filled=sscanf(line,"agemin=%lf agemax=%lf bage=%lf fage=%lf estepm=%d ftolpl=%lf\n",&ageminpar,&agemaxpar, &bage, &fage, &estepm, &ftolpl)) !=EOF){
1.225 brouard 10768:
10769: if (num_filled != 6) {
10770: printf("Error: Not 6 parameters in line, for example:agemin=60 agemax=95 bage=55 fage=95 estepm=24 ftolpl=6e-4\n, your line=%s . Probably you are running an older format.\n",line);
10771: fprintf(ficlog,"Error: Not 6 parameters in line, for example:agemin=60 agemax=95 bage=55 fage=95 estepm=24 ftolpl=6e-4\n, your line=%s . Probably you are running an older format.\n",line);
10772: goto end;
10773: }
10774: printf("agemin=%lf agemax=%lf bage=%lf fage=%lf estepm=%d ftolpl=%lf\n",ageminpar,agemaxpar, bage, fage, estepm, ftolpl);
10775: }
10776: /* ftolpl=6*ftol*1.e5; /\* 6.e-3 make convergences in less than 80 loops for the prevalence limit *\/ */
10777: /*ftolpl=6.e-4;*/ /* 6.e-3 make convergences in less than 80 loops for the prevalence limit */
10778:
1.209 brouard 10779: /* fscanf(ficpar,"agemin=%lf agemax=%lf bage=%lf fage=%lf estepm=%d ftolpl=%\n",&ageminpar,&agemaxpar, &bage, &fage, &estepm); */
1.126 brouard 10780: if (estepm==0 || estepm < stepm) estepm=stepm;
10781: if (fage <= 2) {
10782: bage = ageminpar;
10783: fage = agemaxpar;
10784: }
10785:
10786: fprintf(ficres,"# agemin agemax for life expectancy, bage fage (if mle==0 ie no data nor Max likelihood).\n");
1.211 brouard 10787: fprintf(ficres,"agemin=%.0f agemax=%.0f bage=%.0f fage=%.0f estepm=%d ftolpl=%e\n",ageminpar,agemaxpar,bage,fage, estepm, ftolpl);
10788: fprintf(ficparo,"agemin=%.0f agemax=%.0f bage=%.0f fage=%.0f estepm=%d, ftolpl=%e\n",ageminpar,agemaxpar,bage,fage, estepm, ftolpl);
1.220 brouard 10789:
1.186 brouard 10790: /* Other stuffs, more or less useful */
1.126 brouard 10791: while((c=getc(ficpar))=='#' && c!= EOF){
10792: ungetc(c,ficpar);
10793: fgets(line, MAXLINE, ficpar);
1.141 brouard 10794: fputs(line,stdout);
1.126 brouard 10795: fputs(line,ficparo);
10796: }
10797: ungetc(c,ficpar);
10798:
10799: 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);
10800: 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);
10801: 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);
10802: printf("begin-prev-date=%.lf/%.lf/%.lf end-prev-date=%.lf/%.lf/%.lf mov_average=%d\n",jprev1, mprev1,anprev1,jprev2, mprev2,anprev2,mobilav);
10803: 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);
10804:
10805: while((c=getc(ficpar))=='#' && c!= EOF){
10806: ungetc(c,ficpar);
10807: fgets(line, MAXLINE, ficpar);
1.141 brouard 10808: fputs(line,stdout);
1.126 brouard 10809: fputs(line,ficparo);
10810: }
10811: ungetc(c,ficpar);
10812:
10813:
10814: dateprev1=anprev1+(mprev1-1)/12.+(jprev1-1)/365.;
10815: dateprev2=anprev2+(mprev2-1)/12.+(jprev2-1)/365.;
10816:
10817: fscanf(ficpar,"pop_based=%d\n",&popbased);
1.193 brouard 10818: fprintf(ficlog,"pop_based=%d\n",popbased);
1.126 brouard 10819: fprintf(ficparo,"pop_based=%d\n",popbased);
10820: fprintf(ficres,"pop_based=%d\n",popbased);
10821:
10822: while((c=getc(ficpar))=='#' && c!= EOF){
10823: ungetc(c,ficpar);
10824: fgets(line, MAXLINE, ficpar);
1.141 brouard 10825: fputs(line,stdout);
1.238 brouard 10826: fputs(line,ficres);
1.126 brouard 10827: fputs(line,ficparo);
10828: }
10829: ungetc(c,ficpar);
10830:
10831: 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);
10832: 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);
10833: 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);
10834: 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);
10835: 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);
10836: /* day and month of proj2 are not used but only year anproj2.*/
10837:
1.217 brouard 10838: while((c=getc(ficpar))=='#' && c!= EOF){
10839: ungetc(c,ficpar);
10840: fgets(line, MAXLINE, ficpar);
10841: fputs(line,stdout);
10842: fputs(line,ficparo);
1.238 brouard 10843: fputs(line,ficres);
1.217 brouard 10844: }
10845: ungetc(c,ficpar);
10846:
10847: fscanf(ficpar,"backcast=%d starting-back-date=%lf/%lf/%lf final-back-date=%lf/%lf/%lf mobil_average=%d\n",&backcast,&jback1,&mback1,&anback1,&jback2,&mback2,&anback2,&mobilavproj);
1.223 brouard 10848: fprintf(ficparo,"backcast=%d starting-back-date=%.lf/%.lf/%.lf final-back-date=%.lf/%.lf/%.lf mobil_average=%d\n",backcast,jback1,mback1,anback1,jback2,mback2,anback2,mobilavproj);
10849: fprintf(ficlog,"backcast=%d starting-back-date=%.lf/%.lf/%.lf final-back-date=%.lf/%.lf/%.lf mobil_average=%d\n",backcast,jback1,mback1,anback1,jback2,mback2,anback2,mobilavproj);
10850: fprintf(ficres,"backcast=%d starting-back-date=%.lf/%.lf/%.lf final-back-date=%.lf/%.lf/%.lf mobil_average=%d\n",backcast,jback1,mback1,anback1,jback2,mback2,anback2,mobilavproj);
1.217 brouard 10851: /* day and month of proj2 are not used but only year anproj2.*/
1.126 brouard 10852:
1.230 brouard 10853: /* Results */
1.235 brouard 10854: nresult=0;
1.230 brouard 10855: while(fgets(line, MAXLINE, ficpar)) {
10856: /* If line starts with a # it is a comment */
10857: if (line[0] == '#') {
10858: numlinepar++;
10859: fputs(line,stdout);
10860: fputs(line,ficparo);
10861: fputs(line,ficlog);
1.238 brouard 10862: fputs(line,ficres);
1.230 brouard 10863: continue;
10864: }else
10865: break;
10866: }
10867: while((num_filled=sscanf(line,"result:%[^\n]\n",resultline)) !=EOF){
10868: if (num_filled == 0)
10869: resultline[0]='\0';
10870: else if (num_filled != 1){
10871: printf("ERROR %d: result line should be at minimum 'result=' %s\n",num_filled, line);
10872: }
1.235 brouard 10873: nresult++; /* Sum of resultlines */
10874: printf("Result %d: result=%s\n",nresult, resultline);
10875: if(nresult > MAXRESULTLINES){
10876: printf("ERROR: Current version of IMaCh limits the number of resultlines to %d, you used %d\n",MAXRESULTLINES,nresult);
10877: fprintf(ficlog,"ERROR: Current version of IMaCh limits the number of resultlines to %d, you used %d\n",MAXRESULTLINES,nresult);
10878: goto end;
10879: }
10880: decoderesult(resultline, nresult); /* Fills TKresult[nresult] combination and Tresult[nresult][k4+1] combination values */
1.238 brouard 10881: fprintf(ficparo,"result: %s\n",resultline);
10882: fprintf(ficres,"result: %s\n",resultline);
10883: fprintf(ficlog,"result: %s\n",resultline);
1.230 brouard 10884: while(fgets(line, MAXLINE, ficpar)) {
10885: /* If line starts with a # it is a comment */
10886: if (line[0] == '#') {
10887: numlinepar++;
10888: fputs(line,stdout);
10889: fputs(line,ficparo);
1.238 brouard 10890: fputs(line,ficres);
1.230 brouard 10891: fputs(line,ficlog);
10892: continue;
10893: }else
10894: break;
10895: }
10896: if (feof(ficpar))
10897: break;
10898: else{ /* Processess output results for this combination of covariate values */
10899: }
10900: }
10901:
10902:
1.126 brouard 10903:
1.230 brouard 10904: /* freqsummary(fileres, agemin, agemax, s, agev, nlstate, imx,Tvaraff,nbcode, ncodemax,mint,anint); */
1.145 brouard 10905: /* ,dateprev1,dateprev2,jprev1, mprev1,anprev1,jprev2, mprev2,anprev2); */
1.126 brouard 10906:
10907: replace_back_to_slash(pathc,pathcd); /* Even gnuplot wants a / */
1.194 brouard 10908: if(ageminpar == AGEOVERFLOW ||agemaxpar == -AGEOVERFLOW){
1.230 brouard 10909: printf("Warning! Error in gnuplot file with ageminpar %f or agemaxpar %f overflow\n\
1.194 brouard 10910: This is probably because your parameter file doesn't \n contain the exact number of lines (or columns) corresponding to your model line.\n\
10911: Please run with mle=-1 to get a correct covariance matrix.\n",ageminpar,agemaxpar);
1.230 brouard 10912: fprintf(ficlog,"Warning! Error in gnuplot file with ageminpar %f or agemaxpar %f overflow\n\
1.194 brouard 10913: This is probably because your parameter file doesn't \n contain the exact number of lines (or columns) corresponding to your model line.\n\
10914: Please run with mle=-1 to get a correct covariance matrix.\n",ageminpar,agemaxpar);
1.220 brouard 10915: }else{
1.218 brouard 10916: printinggnuplot(fileresu, optionfilefiname,ageminpar,agemaxpar,fage, prevfcast, backcast, pathc,p);
1.220 brouard 10917: }
10918: printinghtml(fileresu,title,datafile, firstpass, lastpass, stepm, weightopt, \
1.225 brouard 10919: model,imx,jmin,jmax,jmean,rfileres,popforecast,prevfcast,backcast, estepm, \
10920: jprev1,mprev1,anprev1,dateprev1,jprev2,mprev2,anprev2,dateprev2);
1.220 brouard 10921:
1.225 brouard 10922: /*------------ free_vector -------------*/
10923: /* chdir(path); */
1.220 brouard 10924:
1.215 brouard 10925: /* free_ivector(wav,1,imx); */ /* Moved after last prevalence call */
10926: /* free_imatrix(dh,1,lastpass-firstpass+2,1,imx); */
10927: /* free_imatrix(bh,1,lastpass-firstpass+2,1,imx); */
10928: /* free_imatrix(mw,1,lastpass-firstpass+2,1,imx); */
1.126 brouard 10929: free_lvector(num,1,n);
10930: free_vector(agedc,1,n);
10931: /*free_matrix(covar,0,NCOVMAX,1,n);*/
10932: /*free_matrix(covar,1,NCOVMAX,1,n);*/
10933: fclose(ficparo);
10934: fclose(ficres);
1.220 brouard 10935:
10936:
1.186 brouard 10937: /* Other results (useful)*/
1.220 brouard 10938:
10939:
1.126 brouard 10940: /*--------------- Prevalence limit (period or stable prevalence) --------------*/
1.180 brouard 10941: /*#include "prevlim.h"*/ /* Use ficrespl, ficlog */
10942: prlim=matrix(1,nlstate,1,nlstate);
1.209 brouard 10943: prevalence_limit(p, prlim, ageminpar, agemaxpar, ftolpl, &ncvyear);
1.126 brouard 10944: fclose(ficrespl);
10945:
10946: /*------------- h Pij x at various ages ------------*/
1.180 brouard 10947: /*#include "hpijx.h"*/
10948: hPijx(p, bage, fage);
1.145 brouard 10949: fclose(ficrespij);
1.227 brouard 10950:
1.220 brouard 10951: /* ncovcombmax= pow(2,cptcoveff); */
1.219 brouard 10952: /*-------------- Variance of one-step probabilities---*/
1.145 brouard 10953: k=1;
1.126 brouard 10954: varprob(optionfilefiname, matcov, p, delti, nlstate, bage, fage,k,Tvar,nbcode, ncodemax,strstart);
1.227 brouard 10955:
1.219 brouard 10956: /* Prevalence for each covariates in probs[age][status][cov] */
1.218 brouard 10957: probs= ma3x(1,AGESUP,1,nlstate+ndeath, 1,ncovcombmax);
1.126 brouard 10958: for(i=1;i<=AGESUP;i++)
1.219 brouard 10959: for(j=1;j<=nlstate+ndeath;j++) /* ndeath is useless but a necessity to be compared with mobaverages */
1.225 brouard 10960: for(k=1;k<=ncovcombmax;k++)
10961: probs[i][j][k]=0.;
1.219 brouard 10962: prevalence(probs, ageminpar, agemaxpar, s, agev, nlstate, imx, Tvar, nbcode, ncodemax, mint, anint, dateprev1, dateprev2, firstpass, lastpass);
10963: if (mobilav!=0 ||mobilavproj !=0 ) {
10964: mobaverages= ma3x(1, AGESUP,1,nlstate+ndeath, 1,ncovcombmax);
1.227 brouard 10965: for(i=1;i<=AGESUP;i++)
10966: for(j=1;j<=nlstate;j++)
10967: for(k=1;k<=ncovcombmax;k++)
10968: mobaverages[i][j][k]=0.;
1.219 brouard 10969: mobaverage=mobaverages;
10970: if (mobilav!=0) {
1.235 brouard 10971: printf("Movingaveraging observed prevalence\n");
1.227 brouard 10972: if (movingaverage(probs, ageminpar, agemaxpar, mobaverage, mobilav)!=0){
10973: fprintf(ficlog," Error in movingaverage mobilav=%d\n",mobilav);
10974: printf(" Error in movingaverage mobilav=%d\n",mobilav);
10975: }
1.219 brouard 10976: }
10977: /* /\* Prevalence for each covariates in probs[age][status][cov] *\/ */
10978: /* prevalence(probs, ageminpar, agemaxpar, s, agev, nlstate, imx, Tvar, nbcode, ncodemax, mint, anint, dateprev1, dateprev2, firstpass, lastpass); */
10979: else if (mobilavproj !=0) {
1.235 brouard 10980: printf("Movingaveraging projected observed prevalence\n");
1.227 brouard 10981: if (movingaverage(probs, ageminpar, agemaxpar, mobaverage, mobilavproj)!=0){
10982: fprintf(ficlog," Error in movingaverage mobilavproj=%d\n",mobilavproj);
10983: printf(" Error in movingaverage mobilavproj=%d\n",mobilavproj);
10984: }
1.219 brouard 10985: }
10986: }/* end if moving average */
1.227 brouard 10987:
1.126 brouard 10988: /*---------- Forecasting ------------------*/
10989: /*if((stepm == 1) && (strcmp(model,".")==0)){*/
10990: if(prevfcast==1){
10991: /* if(stepm ==1){*/
1.225 brouard 10992: prevforecast(fileresu, anproj1, mproj1, jproj1, agemin, agemax, dateprev1, dateprev2, mobilavproj, bage, fage, firstpass, lastpass, anproj2, p, cptcoveff);
1.126 brouard 10993: }
1.217 brouard 10994: if(backcast==1){
1.219 brouard 10995: ddnewms=matrix(1,nlstate+ndeath,1,nlstate+ndeath);
10996: ddoldms=matrix(1,nlstate+ndeath,1,nlstate+ndeath);
10997: ddsavms=matrix(1,nlstate+ndeath,1,nlstate+ndeath);
10998:
10999: /*--------------- Back Prevalence limit (period or stable prevalence) --------------*/
11000:
11001: bprlim=matrix(1,nlstate,1,nlstate);
11002: back_prevalence_limit(p, bprlim, ageminpar, agemaxpar, ftolpl, &ncvyear, dateprev1, dateprev2, firstpass, lastpass, mobilavproj);
11003: fclose(ficresplb);
11004:
1.222 brouard 11005: hBijx(p, bage, fage, mobaverage);
11006: fclose(ficrespijb);
1.219 brouard 11007: free_matrix(bprlim,1,nlstate,1,nlstate); /*here or after loop ? */
11008:
11009: /* prevbackforecast(fileresu, anback1, mback1, jback1, agemin, agemax, dateprev1, dateprev2, mobilavproj,
1.225 brouard 11010: bage, fage, firstpass, lastpass, anback2, p, cptcoveff); */
1.219 brouard 11011: free_matrix(ddnewms, 1, nlstate+ndeath, 1, nlstate+ndeath);
11012: free_matrix(ddsavms, 1, nlstate+ndeath, 1, nlstate+ndeath);
11013: free_matrix(ddoldms, 1, nlstate+ndeath, 1, nlstate+ndeath);
11014: }
1.217 brouard 11015:
1.186 brouard 11016:
11017: /* ------ Other prevalence ratios------------ */
1.126 brouard 11018:
1.215 brouard 11019: free_ivector(wav,1,imx);
11020: free_imatrix(dh,1,lastpass-firstpass+2,1,imx);
11021: free_imatrix(bh,1,lastpass-firstpass+2,1,imx);
11022: free_imatrix(mw,1,lastpass-firstpass+2,1,imx);
1.218 brouard 11023:
11024:
1.127 brouard 11025: /*---------- Health expectancies, no variances ------------*/
1.218 brouard 11026:
1.201 brouard 11027: strcpy(filerese,"E_");
11028: strcat(filerese,fileresu);
1.126 brouard 11029: if((ficreseij=fopen(filerese,"w"))==NULL) {
11030: printf("Problem with Health Exp. resultfile: %s\n", filerese); exit(0);
11031: fprintf(ficlog,"Problem with Health Exp. resultfile: %s\n", filerese); exit(0);
11032: }
1.208 brouard 11033: printf("Computing Health Expectancies: result on file '%s' ...", filerese);fflush(stdout);
11034: fprintf(ficlog,"Computing Health Expectancies: result on file '%s' ...", filerese);fflush(ficlog);
1.238 brouard 11035:
11036: pstamp(ficreseij);
1.219 brouard 11037:
1.235 brouard 11038: i1=pow(2,cptcoveff); /* Number of combination of dummy covariates */
11039: if (cptcovn < 1){i1=1;}
11040:
11041: for(nres=1; nres <= nresult; nres++) /* For each resultline */
11042: for(k=1; k<=i1;k++){ /* For any combination of dummy covariates, fixed and varying */
11043: if(TKresult[nres]!= k)
11044: continue;
1.219 brouard 11045: fprintf(ficreseij,"\n#****** ");
1.235 brouard 11046: printf("\n#****** ");
1.225 brouard 11047: for(j=1;j<=cptcoveff;j++) {
1.227 brouard 11048: fprintf(ficreseij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
1.235 brouard 11049: printf("V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
11050: }
11051: for (j=1; j<= nsq; j++){ /* For each selected (single) quantitative value */
11052: printf(" V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
11053: fprintf(ficreseij," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
1.219 brouard 11054: }
11055: fprintf(ficreseij,"******\n");
1.235 brouard 11056: printf("******\n");
1.219 brouard 11057:
11058: eij=ma3x(1,nlstate,1,nlstate,(int) bage, (int) fage);
11059: oldm=oldms;savm=savms;
1.235 brouard 11060: evsij(eij, p, nlstate, stepm, (int) bage, (int)fage, oldm, savm, k, estepm, strstart, nres);
1.127 brouard 11061:
1.219 brouard 11062: free_ma3x(eij,1,nlstate,1,nlstate,(int) bage, (int)fage);
1.127 brouard 11063: }
11064: fclose(ficreseij);
1.208 brouard 11065: printf("done evsij\n");fflush(stdout);
11066: fprintf(ficlog,"done evsij\n");fflush(ficlog);
1.218 brouard 11067:
1.227 brouard 11068: /*---------- State-specific expectancies and variances ------------*/
1.218 brouard 11069:
11070:
1.201 brouard 11071: strcpy(filerest,"T_");
11072: strcat(filerest,fileresu);
1.127 brouard 11073: if((ficrest=fopen(filerest,"w"))==NULL) {
11074: printf("Problem with total LE resultfile: %s\n", filerest);goto end;
11075: fprintf(ficlog,"Problem with total LE resultfile: %s\n", filerest);goto end;
11076: }
1.208 brouard 11077: printf("Computing Total Life expectancies with their standard errors: file '%s' ...\n", filerest); fflush(stdout);
11078: fprintf(ficlog,"Computing Total Life expectancies with their standard errors: file '%s' ...\n", filerest); fflush(ficlog);
1.218 brouard 11079:
1.126 brouard 11080:
1.201 brouard 11081: strcpy(fileresstde,"STDE_");
11082: strcat(fileresstde,fileresu);
1.126 brouard 11083: if((ficresstdeij=fopen(fileresstde,"w"))==NULL) {
1.227 brouard 11084: printf("Problem with State specific Exp. and std errors resultfile: %s\n", fileresstde); exit(0);
11085: fprintf(ficlog,"Problem with State specific Exp. and std errors resultfile: %s\n", fileresstde); exit(0);
1.126 brouard 11086: }
1.227 brouard 11087: printf(" Computing State-specific Expectancies and standard errors: result on file '%s' \n", fileresstde);
11088: fprintf(ficlog," Computing State-specific Expectancies and standard errors: result on file '%s' \n", fileresstde);
1.126 brouard 11089:
1.201 brouard 11090: strcpy(filerescve,"CVE_");
11091: strcat(filerescve,fileresu);
1.126 brouard 11092: if((ficrescveij=fopen(filerescve,"w"))==NULL) {
1.227 brouard 11093: printf("Problem with Covar. State-specific Exp. resultfile: %s\n", filerescve); exit(0);
11094: fprintf(ficlog,"Problem with Covar. State-specific Exp. resultfile: %s\n", filerescve); exit(0);
1.126 brouard 11095: }
1.227 brouard 11096: printf(" Computing Covar. of State-specific Expectancies: result on file '%s' \n", filerescve);
11097: fprintf(ficlog," Computing Covar. of State-specific Expectancies: result on file '%s' \n", filerescve);
1.126 brouard 11098:
1.201 brouard 11099: strcpy(fileresv,"V_");
11100: strcat(fileresv,fileresu);
1.126 brouard 11101: if((ficresvij=fopen(fileresv,"w"))==NULL) {
11102: printf("Problem with variance resultfile: %s\n", fileresv);exit(0);
11103: fprintf(ficlog,"Problem with variance resultfile: %s\n", fileresv);exit(0);
11104: }
1.227 brouard 11105: printf(" Computing Variance-covariance of State-specific Expectancies: file '%s' ... ", fileresv);fflush(stdout);
11106: fprintf(ficlog," Computing Variance-covariance of State-specific Expectancies: file '%s' ... ", fileresv);fflush(ficlog);
1.126 brouard 11107:
1.145 brouard 11108: /*for(cptcov=1,k=0;cptcov<=i1;cptcov++){
11109: for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){*/
11110:
1.235 brouard 11111: i1=pow(2,cptcoveff); /* Number of combination of dummy covariates */
11112: if (cptcovn < 1){i1=1;}
11113:
11114: for(nres=1; nres <= nresult; nres++) /* For each resultline */
11115: for(k=1; k<=i1;k++){ /* For any combination of dummy covariates, fixed and varying */
11116: if(TKresult[nres]!= k)
11117: continue;
11118: printf("\n#****** Selected:");
11119: fprintf(ficrest,"\n#****** Selected:");
11120: fprintf(ficlog,"\n#****** Selected:");
1.227 brouard 11121: for(j=1;j<=cptcoveff;j++){
11122: printf("V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
11123: fprintf(ficrest,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
11124: fprintf(ficlog,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
11125: }
1.235 brouard 11126: for (j=1; j<= nsq; j++){ /* For each selected (single) quantitative value */
11127: printf(" V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
11128: fprintf(ficrest," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
11129: fprintf(ficlog," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
11130: }
1.208 brouard 11131: fprintf(ficrest,"******\n");
1.227 brouard 11132: fprintf(ficlog,"******\n");
11133: printf("******\n");
1.208 brouard 11134:
11135: fprintf(ficresstdeij,"\n#****** ");
11136: fprintf(ficrescveij,"\n#****** ");
1.225 brouard 11137: for(j=1;j<=cptcoveff;j++) {
1.227 brouard 11138: fprintf(ficresstdeij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
11139: fprintf(ficrescveij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
1.208 brouard 11140: }
1.235 brouard 11141: for (j=1; j<= nsq; j++){ /* For each selected (single) quantitative value */
11142: fprintf(ficresstdeij," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
11143: fprintf(ficrescveij," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
11144: }
1.208 brouard 11145: fprintf(ficresstdeij,"******\n");
11146: fprintf(ficrescveij,"******\n");
11147:
11148: fprintf(ficresvij,"\n#****** ");
1.238 brouard 11149: /* pstamp(ficresvij); */
1.225 brouard 11150: for(j=1;j<=cptcoveff;j++)
1.227 brouard 11151: fprintf(ficresvij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
1.235 brouard 11152: for (j=1; j<= nsq; j++){ /* For each selected (single) quantitative value */
11153: fprintf(ficresvij," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
11154: }
1.208 brouard 11155: fprintf(ficresvij,"******\n");
11156:
11157: eij=ma3x(1,nlstate,1,nlstate,(int) bage, (int) fage);
11158: oldm=oldms;savm=savms;
1.235 brouard 11159: printf(" cvevsij ");
11160: fprintf(ficlog, " cvevsij ");
11161: cvevsij(eij, p, nlstate, stepm, (int) bage, (int)fage, oldm, savm, k, estepm, delti, matcov, strstart, nres);
1.208 brouard 11162: printf(" end cvevsij \n ");
11163: fprintf(ficlog, " end cvevsij \n ");
11164:
11165: /*
11166: */
11167: /* goto endfree; */
11168:
11169: vareij=ma3x(1,nlstate,1,nlstate,(int) bage, (int) fage);
11170: pstamp(ficrest);
11171:
11172:
11173: for(vpopbased=0; vpopbased <= popbased; vpopbased++){ /* Done for vpopbased=0 and vpopbased=1 if popbased==1*/
1.227 brouard 11174: oldm=oldms;savm=savms; /* ZZ Segmentation fault */
11175: cptcod= 0; /* To be deleted */
11176: printf("varevsij vpopbased=%d \n",vpopbased);
11177: fprintf(ficlog, "varevsij vpopbased=%d \n",vpopbased);
1.235 brouard 11178: varevsij(optionfilefiname, vareij, matcov, p, delti, nlstate, stepm, (int) bage, (int) fage, oldm, savm, prlim, ftolpl, &ncvyear, k, estepm, cptcov,cptcod,vpopbased,mobilav, strstart, nres); /* cptcod not initialized Intel */
1.227 brouard 11179: 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 ");
11180: if(vpopbased==1)
11181: 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);
11182: else
11183: fprintf(ficrest,"the age specific period (stable) prevalences in each health state \n");
11184: fprintf(ficrest,"# Age popbased mobilav e.. (std) ");
11185: for (i=1;i<=nlstate;i++) fprintf(ficrest,"e.%d (std) ",i);
11186: fprintf(ficrest,"\n");
11187: /* printf("Which p?\n"); for(i=1;i<=npar;i++)printf("p[i=%d]=%lf,",i,p[i]);printf("\n"); */
11188: epj=vector(1,nlstate+1);
11189: printf("Computing age specific period (stable) prevalences in each health state \n");
11190: fprintf(ficlog,"Computing age specific period (stable) prevalences in each health state \n");
11191: for(age=bage; age <=fage ;age++){
1.235 brouard 11192: prevalim(prlim, nlstate, p, age, oldm, savm, ftolpl, &ncvyear, k, nres); /*ZZ Is it the correct prevalim */
1.227 brouard 11193: if (vpopbased==1) {
11194: if(mobilav ==0){
11195: for(i=1; i<=nlstate;i++)
11196: prlim[i][i]=probs[(int)age][i][k];
11197: }else{ /* mobilav */
11198: for(i=1; i<=nlstate;i++)
11199: prlim[i][i]=mobaverage[(int)age][i][k];
11200: }
11201: }
1.219 brouard 11202:
1.227 brouard 11203: fprintf(ficrest," %4.0f %d %d",age, vpopbased, mobilav);
11204: /* fprintf(ficrest," %4.0f %d %d %d %d",age, vpopbased, mobilav,Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]); */ /* to be done */
11205: /* printf(" age %4.0f ",age); */
11206: for(j=1, epj[nlstate+1]=0.;j <=nlstate;j++){
11207: for(i=1, epj[j]=0.;i <=nlstate;i++) {
11208: epj[j] += prlim[i][i]*eij[i][j][(int)age];
11209: /*ZZZ printf("%lf %lf ", prlim[i][i] ,eij[i][j][(int)age]);*/
11210: /* printf("%lf %lf ", prlim[i][i] ,eij[i][j][(int)age]); */
11211: }
11212: epj[nlstate+1] +=epj[j];
11213: }
11214: /* printf(" age %4.0f \n",age); */
1.219 brouard 11215:
1.227 brouard 11216: for(i=1, vepp=0.;i <=nlstate;i++)
11217: for(j=1;j <=nlstate;j++)
11218: vepp += vareij[i][j][(int)age];
11219: fprintf(ficrest," %7.3f (%7.3f)", epj[nlstate+1],sqrt(vepp));
11220: for(j=1;j <=nlstate;j++){
11221: fprintf(ficrest," %7.3f (%7.3f)", epj[j],sqrt(vareij[j][j][(int)age]));
11222: }
11223: fprintf(ficrest,"\n");
11224: }
1.208 brouard 11225: } /* End vpopbased */
11226: free_ma3x(eij,1,nlstate,1,nlstate,(int) bage, (int)fage);
11227: free_ma3x(vareij,1,nlstate,1,nlstate,(int) bage, (int)fage);
11228: free_vector(epj,1,nlstate+1);
1.235 brouard 11229: printf("done selection\n");fflush(stdout);
11230: fprintf(ficlog,"done selection\n");fflush(ficlog);
1.208 brouard 11231:
1.145 brouard 11232: /*}*/
1.235 brouard 11233: } /* End k selection */
1.227 brouard 11234:
11235: printf("done State-specific expectancies\n");fflush(stdout);
11236: fprintf(ficlog,"done State-specific expectancies\n");fflush(ficlog);
11237:
1.126 brouard 11238: /*------- Variance of period (stable) prevalence------*/
1.227 brouard 11239:
1.201 brouard 11240: strcpy(fileresvpl,"VPL_");
11241: strcat(fileresvpl,fileresu);
1.126 brouard 11242: if((ficresvpl=fopen(fileresvpl,"w"))==NULL) {
11243: printf("Problem with variance of period (stable) prevalence resultfile: %s\n", fileresvpl);
11244: exit(0);
11245: }
1.208 brouard 11246: printf("Computing Variance-covariance of period (stable) prevalence: file '%s' ...", fileresvpl);fflush(stdout);
11247: fprintf(ficlog, "Computing Variance-covariance of period (stable) prevalence: file '%s' ...", fileresvpl);fflush(ficlog);
1.227 brouard 11248:
1.145 brouard 11249: /*for(cptcov=1,k=0;cptcov<=i1;cptcov++){
11250: for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){*/
1.227 brouard 11251:
1.235 brouard 11252: i1=pow(2,cptcoveff);
11253: if (cptcovn < 1){i1=1;}
11254:
11255: for(nres=1; nres <= nresult; nres++) /* For each resultline */
11256: for(k=1; k<=i1;k++){
11257: if(TKresult[nres]!= k)
11258: continue;
1.227 brouard 11259: fprintf(ficresvpl,"\n#****** ");
11260: printf("\n#****** ");
11261: fprintf(ficlog,"\n#****** ");
11262: for(j=1;j<=cptcoveff;j++) {
11263: fprintf(ficresvpl,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
11264: fprintf(ficlog,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
11265: printf("V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
11266: }
1.235 brouard 11267: for (j=1; j<= nsq; j++){ /* For each selected (single) quantitative value */
11268: printf(" V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
11269: fprintf(ficresvpl," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
11270: fprintf(ficlog," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
11271: }
1.227 brouard 11272: fprintf(ficresvpl,"******\n");
11273: printf("******\n");
11274: fprintf(ficlog,"******\n");
11275:
11276: varpl=matrix(1,nlstate,(int) bage, (int) fage);
11277: oldm=oldms;savm=savms;
1.235 brouard 11278: varprevlim(fileres, varpl, matcov, p, delti, nlstate, stepm, (int) bage, (int) fage, oldm, savm, prlim, ftolpl, &ncvyear, k, strstart, nres);
1.227 brouard 11279: free_matrix(varpl,1,nlstate,(int) bage, (int)fage);
1.145 brouard 11280: /*}*/
1.126 brouard 11281: }
1.227 brouard 11282:
1.126 brouard 11283: fclose(ficresvpl);
1.208 brouard 11284: printf("done variance-covariance of period prevalence\n");fflush(stdout);
11285: fprintf(ficlog,"done variance-covariance of period prevalence\n");fflush(ficlog);
1.227 brouard 11286:
11287: free_vector(weight,1,n);
11288: free_imatrix(Tvard,1,NCOVMAX,1,2);
11289: free_imatrix(s,1,maxwav+1,1,n);
11290: free_matrix(anint,1,maxwav,1,n);
11291: free_matrix(mint,1,maxwav,1,n);
11292: free_ivector(cod,1,n);
11293: free_ivector(tab,1,NCOVMAX);
11294: fclose(ficresstdeij);
11295: fclose(ficrescveij);
11296: fclose(ficresvij);
11297: fclose(ficrest);
11298: fclose(ficpar);
11299:
11300:
1.126 brouard 11301: /*---------- End : free ----------------*/
1.219 brouard 11302: if (mobilav!=0 ||mobilavproj !=0)
11303: free_ma3x(mobaverages,1, AGESUP,1,nlstate+ndeath, 1,ncovcombmax); /* We need to have a squared matrix with prevalence of the dead! */
1.218 brouard 11304: free_ma3x(probs,1,AGESUP,1,nlstate+ndeath, 1,ncovcombmax);
1.220 brouard 11305: free_matrix(prlim,1,nlstate,1,nlstate); /*here or after loop ? */
11306: free_matrix(pmmij,1,nlstate+ndeath,1,nlstate+ndeath);
1.126 brouard 11307: } /* mle==-3 arrives here for freeing */
1.227 brouard 11308: /* endfree:*/
11309: free_matrix(oldms, 1,nlstate+ndeath,1,nlstate+ndeath);
11310: free_matrix(newms, 1,nlstate+ndeath,1,nlstate+ndeath);
11311: free_matrix(savms, 1,nlstate+ndeath,1,nlstate+ndeath);
11312: free_ma3x(cotqvar,1,maxwav,1,nqtv,1,n);
1.233 brouard 11313: free_ma3x(cotvar,1,maxwav,1,ntv+nqtv,1,n);
1.227 brouard 11314: free_matrix(coqvar,1,maxwav,1,n);
11315: free_matrix(covar,0,NCOVMAX,1,n);
11316: free_matrix(matcov,1,npar,1,npar);
11317: free_matrix(hess,1,npar,1,npar);
11318: /*free_vector(delti,1,npar);*/
11319: free_ma3x(delti3,1,nlstate,1, nlstate+ndeath-1,1,ncovmodel);
11320: free_matrix(agev,1,maxwav,1,imx);
11321: free_ma3x(param,1,nlstate,1, nlstate+ndeath-1,1,ncovmodel);
11322:
11323: free_ivector(ncodemax,1,NCOVMAX);
11324: free_ivector(ncodemaxwundef,1,NCOVMAX);
11325: free_ivector(Dummy,-1,NCOVMAX);
11326: free_ivector(Fixed,-1,NCOVMAX);
1.238 brouard 11327: free_ivector(DummyV,1,NCOVMAX);
11328: free_ivector(FixedV,1,NCOVMAX);
1.227 brouard 11329: free_ivector(Typevar,-1,NCOVMAX);
11330: free_ivector(Tvar,1,NCOVMAX);
1.234 brouard 11331: free_ivector(TvarsQ,1,NCOVMAX);
11332: free_ivector(TvarsQind,1,NCOVMAX);
11333: free_ivector(TvarsD,1,NCOVMAX);
11334: free_ivector(TvarsDind,1,NCOVMAX);
1.231 brouard 11335: free_ivector(TvarFD,1,NCOVMAX);
11336: free_ivector(TvarFDind,1,NCOVMAX);
1.232 brouard 11337: free_ivector(TvarF,1,NCOVMAX);
11338: free_ivector(TvarFind,1,NCOVMAX);
11339: free_ivector(TvarV,1,NCOVMAX);
11340: free_ivector(TvarVind,1,NCOVMAX);
11341: free_ivector(TvarA,1,NCOVMAX);
11342: free_ivector(TvarAind,1,NCOVMAX);
1.231 brouard 11343: free_ivector(TvarFQ,1,NCOVMAX);
11344: free_ivector(TvarFQind,1,NCOVMAX);
11345: free_ivector(TvarVD,1,NCOVMAX);
11346: free_ivector(TvarVDind,1,NCOVMAX);
11347: free_ivector(TvarVQ,1,NCOVMAX);
11348: free_ivector(TvarVQind,1,NCOVMAX);
1.230 brouard 11349: free_ivector(Tvarsel,1,NCOVMAX);
11350: free_vector(Tvalsel,1,NCOVMAX);
1.227 brouard 11351: free_ivector(Tposprod,1,NCOVMAX);
11352: free_ivector(Tprod,1,NCOVMAX);
11353: free_ivector(Tvaraff,1,NCOVMAX);
11354: free_ivector(invalidvarcomb,1,ncovcombmax);
11355: free_ivector(Tage,1,NCOVMAX);
11356: free_ivector(Tmodelind,1,NCOVMAX);
1.228 brouard 11357: free_ivector(TmodelInvind,1,NCOVMAX);
11358: free_ivector(TmodelInvQind,1,NCOVMAX);
1.227 brouard 11359:
11360: free_imatrix(nbcode,0,NCOVMAX,0,NCOVMAX);
11361: /* free_imatrix(codtab,1,100,1,10); */
1.126 brouard 11362: fflush(fichtm);
11363: fflush(ficgp);
11364:
1.227 brouard 11365:
1.126 brouard 11366: if((nberr >0) || (nbwarn>0)){
1.216 brouard 11367: printf("End of Imach with %d errors and/or %d warnings. Please look at the log file for details.\n",nberr,nbwarn);
11368: 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 11369: }else{
11370: printf("End of Imach\n");
11371: fprintf(ficlog,"End of Imach\n");
11372: }
11373: printf("See log file on %s\n",filelog);
11374: /* gettimeofday(&end_time, (struct timezone*)0);*/ /* after time */
1.157 brouard 11375: /*(void) gettimeofday(&end_time,&tzp);*/
11376: rend_time = time(NULL);
11377: end_time = *localtime(&rend_time);
11378: /* tml = *localtime(&end_time.tm_sec); */
11379: strcpy(strtend,asctime(&end_time));
1.126 brouard 11380: printf("Local time at start %s\nLocal time at end %s",strstart, strtend);
11381: fprintf(ficlog,"Local time at start %s\nLocal time at end %s\n",strstart, strtend);
1.157 brouard 11382: printf("Total time used %s\n", asc_diff_time(rend_time -rstart_time,tmpout));
1.227 brouard 11383:
1.157 brouard 11384: printf("Total time was %.0lf Sec.\n", difftime(rend_time,rstart_time));
11385: fprintf(ficlog,"Total time used %s\n", asc_diff_time(rend_time -rstart_time,tmpout));
11386: fprintf(ficlog,"Total time was %.0lf Sec.\n", difftime(rend_time,rstart_time));
1.126 brouard 11387: /* printf("Total time was %d uSec.\n", total_usecs);*/
11388: /* if(fileappend(fichtm,optionfilehtm)){ */
11389: fprintf(fichtm,"<br>Local time at start %s<br>Local time at end %s<br>\n</body></html>",strstart, strtend);
11390: fclose(fichtm);
11391: fprintf(fichtmcov,"<br>Local time at start %s<br>Local time at end %s<br>\n</body></html>",strstart, strtend);
11392: fclose(fichtmcov);
11393: fclose(ficgp);
11394: fclose(ficlog);
11395: /*------ End -----------*/
1.227 brouard 11396:
11397:
11398: printf("Before Current directory %s!\n",pathcd);
1.184 brouard 11399: #ifdef WIN32
1.227 brouard 11400: if (_chdir(pathcd) != 0)
11401: printf("Can't move to directory %s!\n",path);
11402: if(_getcwd(pathcd,MAXLINE) > 0)
1.184 brouard 11403: #else
1.227 brouard 11404: if(chdir(pathcd) != 0)
11405: printf("Can't move to directory %s!\n", path);
11406: if (getcwd(pathcd, MAXLINE) > 0)
1.184 brouard 11407: #endif
1.126 brouard 11408: printf("Current directory %s!\n",pathcd);
11409: /*strcat(plotcmd,CHARSEPARATOR);*/
11410: sprintf(plotcmd,"gnuplot");
1.157 brouard 11411: #ifdef _WIN32
1.126 brouard 11412: sprintf(plotcmd,"\"%sgnuplot.exe\"",pathimach);
11413: #endif
11414: if(!stat(plotcmd,&info)){
1.158 brouard 11415: printf("Error or gnuplot program not found: '%s'\n",plotcmd);fflush(stdout);
1.126 brouard 11416: if(!stat(getenv("GNUPLOTBIN"),&info)){
1.158 brouard 11417: printf("Error or gnuplot program not found: '%s' Environment GNUPLOTBIN not set.\n",plotcmd);fflush(stdout);
1.126 brouard 11418: }else
11419: strcpy(pplotcmd,plotcmd);
1.157 brouard 11420: #ifdef __unix
1.126 brouard 11421: strcpy(plotcmd,GNUPLOTPROGRAM);
11422: if(!stat(plotcmd,&info)){
1.158 brouard 11423: printf("Error gnuplot program not found: '%s'\n",plotcmd);fflush(stdout);
1.126 brouard 11424: }else
11425: strcpy(pplotcmd,plotcmd);
11426: #endif
11427: }else
11428: strcpy(pplotcmd,plotcmd);
11429:
11430: sprintf(plotcmd,"%s %s",pplotcmd, optionfilegnuplot);
1.158 brouard 11431: printf("Starting graphs with: '%s'\n",plotcmd);fflush(stdout);
1.227 brouard 11432:
1.126 brouard 11433: if((outcmd=system(plotcmd)) != 0){
1.158 brouard 11434: printf("gnuplot command might not be in your path: '%s', err=%d\n", plotcmd, outcmd);
1.154 brouard 11435: printf("\n Trying if gnuplot resides on the same directory that IMaCh\n");
1.152 brouard 11436: sprintf(plotcmd,"%sgnuplot %s", pathimach, optionfilegnuplot);
1.150 brouard 11437: if((outcmd=system(plotcmd)) != 0)
1.153 brouard 11438: printf("\n Still a problem with gnuplot command %s, err=%d\n", plotcmd, outcmd);
1.126 brouard 11439: }
1.158 brouard 11440: printf(" Successful, please wait...");
1.126 brouard 11441: while (z[0] != 'q') {
11442: /* chdir(path); */
1.154 brouard 11443: printf("\nType e to edit results with your browser, g to graph again and q for exit: ");
1.126 brouard 11444: scanf("%s",z);
11445: /* if (z[0] == 'c') system("./imach"); */
11446: if (z[0] == 'e') {
1.158 brouard 11447: #ifdef __APPLE__
1.152 brouard 11448: sprintf(pplotcmd, "open %s", optionfilehtm);
1.157 brouard 11449: #elif __linux
11450: sprintf(pplotcmd, "xdg-open %s", optionfilehtm);
1.153 brouard 11451: #else
1.152 brouard 11452: sprintf(pplotcmd, "%s", optionfilehtm);
1.153 brouard 11453: #endif
11454: printf("Starting browser with: %s",pplotcmd);fflush(stdout);
11455: system(pplotcmd);
1.126 brouard 11456: }
11457: else if (z[0] == 'g') system(plotcmd);
11458: else if (z[0] == 'q') exit(0);
11459: }
1.227 brouard 11460: end:
1.126 brouard 11461: while (z[0] != 'q') {
1.195 brouard 11462: printf("\nType q for exiting: "); fflush(stdout);
1.126 brouard 11463: scanf("%s",z);
11464: }
11465: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>