Annotation of imach/src/imach.c, revision 1.244
1.244 ! brouard 1: /* $Id: imach.c,v 1.243 2016/09/02 06:45:35 brouard Exp $
1.126 brouard 2: $State: Exp $
1.163 brouard 3: $Log: imach.c,v $
1.244 ! brouard 4: Revision 1.243 2016/09/02 06:45:35 brouard
! 5: *** empty log message ***
! 6:
1.243 brouard 7: Revision 1.242 2016/08/30 15:01:20 brouard
8: Summary: Fixing a lots
9:
1.242 brouard 10: Revision 1.241 2016/08/29 17:17:25 brouard
11: Summary: gnuplot problem in Back projection to fix
12:
1.241 brouard 13: Revision 1.240 2016/08/29 07:53:18 brouard
14: Summary: Better
15:
1.240 brouard 16: Revision 1.239 2016/08/26 15:51:03 brouard
17: Summary: Improvement in Powell output in order to copy and paste
18:
19: Author:
20:
1.239 brouard 21: Revision 1.238 2016/08/26 14:23:35 brouard
22: Summary: Starting tests of 0.99
23:
1.238 brouard 24: Revision 1.237 2016/08/26 09:20:19 brouard
25: Summary: to valgrind
26:
1.237 brouard 27: Revision 1.236 2016/08/25 10:50:18 brouard
28: *** empty log message ***
29:
1.236 brouard 30: Revision 1.235 2016/08/25 06:59:23 brouard
31: *** empty log message ***
32:
1.235 brouard 33: Revision 1.234 2016/08/23 16:51:20 brouard
34: *** empty log message ***
35:
1.234 brouard 36: Revision 1.233 2016/08/23 07:40:50 brouard
37: Summary: not working
38:
1.233 brouard 39: Revision 1.232 2016/08/22 14:20:21 brouard
40: Summary: not working
41:
1.232 brouard 42: Revision 1.231 2016/08/22 07:17:15 brouard
43: Summary: not working
44:
1.231 brouard 45: Revision 1.230 2016/08/22 06:55:53 brouard
46: Summary: Not working
47:
1.230 brouard 48: Revision 1.229 2016/07/23 09:45:53 brouard
49: Summary: Completing for func too
50:
1.229 brouard 51: Revision 1.228 2016/07/22 17:45:30 brouard
52: Summary: Fixing some arrays, still debugging
53:
1.227 brouard 54: Revision 1.226 2016/07/12 18:42:34 brouard
55: Summary: temp
56:
1.226 brouard 57: Revision 1.225 2016/07/12 08:40:03 brouard
58: Summary: saving but not running
59:
1.225 brouard 60: Revision 1.224 2016/07/01 13:16:01 brouard
61: Summary: Fixes
62:
1.224 brouard 63: Revision 1.223 2016/02/19 09:23:35 brouard
64: Summary: temporary
65:
1.223 brouard 66: Revision 1.222 2016/02/17 08:14:50 brouard
67: Summary: Probably last 0.98 stable version 0.98r6
68:
1.222 brouard 69: Revision 1.221 2016/02/15 23:35:36 brouard
70: Summary: minor bug
71:
1.220 brouard 72: Revision 1.219 2016/02/15 00:48:12 brouard
73: *** empty log message ***
74:
1.219 brouard 75: Revision 1.218 2016/02/12 11:29:23 brouard
76: Summary: 0.99 Back projections
77:
1.218 brouard 78: Revision 1.217 2015/12/23 17:18:31 brouard
79: Summary: Experimental backcast
80:
1.217 brouard 81: Revision 1.216 2015/12/18 17:32:11 brouard
82: Summary: 0.98r4 Warning and status=-2
83:
84: Version 0.98r4 is now:
85: - displaying an error when status is -1, date of interview unknown and date of death known;
86: - permitting a status -2 when the vital status is unknown at a known date of right truncation.
87: Older changes concerning s=-2, dating from 2005 have been supersed.
88:
1.216 brouard 89: Revision 1.215 2015/12/16 08:52:24 brouard
90: Summary: 0.98r4 working
91:
1.215 brouard 92: Revision 1.214 2015/12/16 06:57:54 brouard
93: Summary: temporary not working
94:
1.214 brouard 95: Revision 1.213 2015/12/11 18:22:17 brouard
96: Summary: 0.98r4
97:
1.213 brouard 98: Revision 1.212 2015/11/21 12:47:24 brouard
99: Summary: minor typo
100:
1.212 brouard 101: Revision 1.211 2015/11/21 12:41:11 brouard
102: Summary: 0.98r3 with some graph of projected cross-sectional
103:
104: Author: Nicolas Brouard
105:
1.211 brouard 106: Revision 1.210 2015/11/18 17:41:20 brouard
107: Summary: Start working on projected prevalences
108:
1.210 brouard 109: Revision 1.209 2015/11/17 22:12:03 brouard
110: Summary: Adding ftolpl parameter
111: Author: N Brouard
112:
113: We had difficulties to get smoothed confidence intervals. It was due
114: to the period prevalence which wasn't computed accurately. The inner
115: parameter ftolpl is now an outer parameter of the .imach parameter
116: file after estepm. If ftolpl is small 1.e-4 and estepm too,
117: computation are long.
118:
1.209 brouard 119: Revision 1.208 2015/11/17 14:31:57 brouard
120: Summary: temporary
121:
1.208 brouard 122: Revision 1.207 2015/10/27 17:36:57 brouard
123: *** empty log message ***
124:
1.207 brouard 125: Revision 1.206 2015/10/24 07:14:11 brouard
126: *** empty log message ***
127:
1.206 brouard 128: Revision 1.205 2015/10/23 15:50:53 brouard
129: Summary: 0.98r3 some clarification for graphs on likelihood contributions
130:
1.205 brouard 131: Revision 1.204 2015/10/01 16:20:26 brouard
132: Summary: Some new graphs of contribution to likelihood
133:
1.204 brouard 134: Revision 1.203 2015/09/30 17:45:14 brouard
135: Summary: looking at better estimation of the hessian
136:
137: Also a better criteria for convergence to the period prevalence And
138: therefore adding the number of years needed to converge. (The
139: prevalence in any alive state shold sum to one
140:
1.203 brouard 141: Revision 1.202 2015/09/22 19:45:16 brouard
142: Summary: Adding some overall graph on contribution to likelihood. Might change
143:
1.202 brouard 144: Revision 1.201 2015/09/15 17:34:58 brouard
145: Summary: 0.98r0
146:
147: - Some new graphs like suvival functions
148: - Some bugs fixed like model=1+age+V2.
149:
1.201 brouard 150: Revision 1.200 2015/09/09 16:53:55 brouard
151: Summary: Big bug thanks to Flavia
152:
153: Even model=1+age+V2. did not work anymore
154:
1.200 brouard 155: Revision 1.199 2015/09/07 14:09:23 brouard
156: Summary: 0.98q6 changing default small png format for graph to vectorized svg.
157:
1.199 brouard 158: Revision 1.198 2015/09/03 07:14:39 brouard
159: Summary: 0.98q5 Flavia
160:
1.198 brouard 161: Revision 1.197 2015/09/01 18:24:39 brouard
162: *** empty log message ***
163:
1.197 brouard 164: Revision 1.196 2015/08/18 23:17:52 brouard
165: Summary: 0.98q5
166:
1.196 brouard 167: Revision 1.195 2015/08/18 16:28:39 brouard
168: Summary: Adding a hack for testing purpose
169:
170: After reading the title, ftol and model lines, if the comment line has
171: a q, starting with #q, the answer at the end of the run is quit. It
172: permits to run test files in batch with ctest. The former workaround was
173: $ echo q | imach foo.imach
174:
1.195 brouard 175: Revision 1.194 2015/08/18 13:32:00 brouard
176: Summary: Adding error when the covariance matrix doesn't contain the exact number of lines required by the model line.
177:
1.194 brouard 178: Revision 1.193 2015/08/04 07:17:42 brouard
179: Summary: 0.98q4
180:
1.193 brouard 181: Revision 1.192 2015/07/16 16:49:02 brouard
182: Summary: Fixing some outputs
183:
1.192 brouard 184: Revision 1.191 2015/07/14 10:00:33 brouard
185: Summary: Some fixes
186:
1.191 brouard 187: Revision 1.190 2015/05/05 08:51:13 brouard
188: Summary: Adding digits in output parameters (7 digits instead of 6)
189:
190: Fix 1+age+.
191:
1.190 brouard 192: Revision 1.189 2015/04/30 14:45:16 brouard
193: Summary: 0.98q2
194:
1.189 brouard 195: Revision 1.188 2015/04/30 08:27:53 brouard
196: *** empty log message ***
197:
1.188 brouard 198: Revision 1.187 2015/04/29 09:11:15 brouard
199: *** empty log message ***
200:
1.187 brouard 201: Revision 1.186 2015/04/23 12:01:52 brouard
202: Summary: V1*age is working now, version 0.98q1
203:
204: Some codes had been disabled in order to simplify and Vn*age was
205: working in the optimization phase, ie, giving correct MLE parameters,
206: but, as usual, outputs were not correct and program core dumped.
207:
1.186 brouard 208: Revision 1.185 2015/03/11 13:26:42 brouard
209: Summary: Inclusion of compile and links command line for Intel Compiler
210:
1.185 brouard 211: Revision 1.184 2015/03/11 11:52:39 brouard
212: Summary: Back from Windows 8. Intel Compiler
213:
1.184 brouard 214: Revision 1.183 2015/03/10 20:34:32 brouard
215: Summary: 0.98q0, trying with directest, mnbrak fixed
216:
217: We use directest instead of original Powell test; probably no
218: incidence on the results, but better justifications;
219: We fixed Numerical Recipes mnbrak routine which was wrong and gave
220: wrong results.
221:
1.183 brouard 222: Revision 1.182 2015/02/12 08:19:57 brouard
223: Summary: Trying to keep directest which seems simpler and more general
224: Author: Nicolas Brouard
225:
1.182 brouard 226: Revision 1.181 2015/02/11 23:22:24 brouard
227: Summary: Comments on Powell added
228:
229: Author:
230:
1.181 brouard 231: Revision 1.180 2015/02/11 17:33:45 brouard
232: Summary: Finishing move from main to function (hpijx and prevalence_limit)
233:
1.180 brouard 234: Revision 1.179 2015/01/04 09:57:06 brouard
235: Summary: back to OS/X
236:
1.179 brouard 237: Revision 1.178 2015/01/04 09:35:48 brouard
238: *** empty log message ***
239:
1.178 brouard 240: Revision 1.177 2015/01/03 18:40:56 brouard
241: Summary: Still testing ilc32 on OSX
242:
1.177 brouard 243: Revision 1.176 2015/01/03 16:45:04 brouard
244: *** empty log message ***
245:
1.176 brouard 246: Revision 1.175 2015/01/03 16:33:42 brouard
247: *** empty log message ***
248:
1.175 brouard 249: Revision 1.174 2015/01/03 16:15:49 brouard
250: Summary: Still in cross-compilation
251:
1.174 brouard 252: Revision 1.173 2015/01/03 12:06:26 brouard
253: Summary: trying to detect cross-compilation
254:
1.173 brouard 255: Revision 1.172 2014/12/27 12:07:47 brouard
256: Summary: Back from Visual Studio and Intel, options for compiling for Windows XP
257:
1.172 brouard 258: Revision 1.171 2014/12/23 13:26:59 brouard
259: Summary: Back from Visual C
260:
261: Still problem with utsname.h on Windows
262:
1.171 brouard 263: Revision 1.170 2014/12/23 11:17:12 brouard
264: Summary: Cleaning some \%% back to %%
265:
266: The escape was mandatory for a specific compiler (which one?), but too many warnings.
267:
1.170 brouard 268: Revision 1.169 2014/12/22 23:08:31 brouard
269: Summary: 0.98p
270:
271: Outputs some informations on compiler used, OS etc. Testing on different platforms.
272:
1.169 brouard 273: Revision 1.168 2014/12/22 15:17:42 brouard
1.170 brouard 274: Summary: update
1.169 brouard 275:
1.168 brouard 276: Revision 1.167 2014/12/22 13:50:56 brouard
277: Summary: Testing uname and compiler version and if compiled 32 or 64
278:
279: Testing on Linux 64
280:
1.167 brouard 281: Revision 1.166 2014/12/22 11:40:47 brouard
282: *** empty log message ***
283:
1.166 brouard 284: Revision 1.165 2014/12/16 11:20:36 brouard
285: Summary: After compiling on Visual C
286:
287: * imach.c (Module): Merging 1.61 to 1.162
288:
1.165 brouard 289: Revision 1.164 2014/12/16 10:52:11 brouard
290: Summary: Merging with Visual C after suppressing some warnings for unused variables. Also fixing Saito's bug 0.98Xn
291:
292: * imach.c (Module): Merging 1.61 to 1.162
293:
1.164 brouard 294: Revision 1.163 2014/12/16 10:30:11 brouard
295: * imach.c (Module): Merging 1.61 to 1.162
296:
1.163 brouard 297: Revision 1.162 2014/09/25 11:43:39 brouard
298: Summary: temporary backup 0.99!
299:
1.162 brouard 300: Revision 1.1 2014/09/16 11:06:58 brouard
301: Summary: With some code (wrong) for nlopt
302:
303: Author:
304:
305: Revision 1.161 2014/09/15 20:41:41 brouard
306: Summary: Problem with macro SQR on Intel compiler
307:
1.161 brouard 308: Revision 1.160 2014/09/02 09:24:05 brouard
309: *** empty log message ***
310:
1.160 brouard 311: Revision 1.159 2014/09/01 10:34:10 brouard
312: Summary: WIN32
313: Author: Brouard
314:
1.159 brouard 315: Revision 1.158 2014/08/27 17:11:51 brouard
316: *** empty log message ***
317:
1.158 brouard 318: Revision 1.157 2014/08/27 16:26:55 brouard
319: Summary: Preparing windows Visual studio version
320: Author: Brouard
321:
322: In order to compile on Visual studio, time.h is now correct and time_t
323: and tm struct should be used. difftime should be used but sometimes I
324: just make the differences in raw time format (time(&now).
325: Trying to suppress #ifdef LINUX
326: Add xdg-open for __linux in order to open default browser.
327:
1.157 brouard 328: Revision 1.156 2014/08/25 20:10:10 brouard
329: *** empty log message ***
330:
1.156 brouard 331: Revision 1.155 2014/08/25 18:32:34 brouard
332: Summary: New compile, minor changes
333: Author: Brouard
334:
1.155 brouard 335: Revision 1.154 2014/06/20 17:32:08 brouard
336: Summary: Outputs now all graphs of convergence to period prevalence
337:
1.154 brouard 338: Revision 1.153 2014/06/20 16:45:46 brouard
339: Summary: If 3 live state, convergence to period prevalence on same graph
340: Author: Brouard
341:
1.153 brouard 342: Revision 1.152 2014/06/18 17:54:09 brouard
343: Summary: open browser, use gnuplot on same dir than imach if not found in the path
344:
1.152 brouard 345: Revision 1.151 2014/06/18 16:43:30 brouard
346: *** empty log message ***
347:
1.151 brouard 348: Revision 1.150 2014/06/18 16:42:35 brouard
349: Summary: If gnuplot is not in the path try on same directory than imach binary (OSX)
350: Author: brouard
351:
1.150 brouard 352: Revision 1.149 2014/06/18 15:51:14 brouard
353: Summary: Some fixes in parameter files errors
354: Author: Nicolas Brouard
355:
1.149 brouard 356: Revision 1.148 2014/06/17 17:38:48 brouard
357: Summary: Nothing new
358: Author: Brouard
359:
360: Just a new packaging for OS/X version 0.98nS
361:
1.148 brouard 362: Revision 1.147 2014/06/16 10:33:11 brouard
363: *** empty log message ***
364:
1.147 brouard 365: Revision 1.146 2014/06/16 10:20:28 brouard
366: Summary: Merge
367: Author: Brouard
368:
369: Merge, before building revised version.
370:
1.146 brouard 371: Revision 1.145 2014/06/10 21:23:15 brouard
372: Summary: Debugging with valgrind
373: Author: Nicolas Brouard
374:
375: Lot of changes in order to output the results with some covariates
376: After the Edimburgh REVES conference 2014, it seems mandatory to
377: improve the code.
378: No more memory valgrind error but a lot has to be done in order to
379: continue the work of splitting the code into subroutines.
380: Also, decodemodel has been improved. Tricode is still not
381: optimal. nbcode should be improved. Documentation has been added in
382: the source code.
383:
1.144 brouard 384: Revision 1.143 2014/01/26 09:45:38 brouard
385: Summary: Version 0.98nR (to be improved, but gives same optimization results as 0.98k. Nice, promising
386:
387: * imach.c (Module): Trying to merge old staffs together while being at Tokyo. Not tested...
388: (Module): Version 0.98nR Running ok, but output format still only works for three covariates.
389:
1.143 brouard 390: Revision 1.142 2014/01/26 03:57:36 brouard
391: Summary: gnuplot changed plot w l 1 has to be changed to plot w l lt 2
392:
393: * imach.c (Module): Trying to merge old staffs together while being at Tokyo. Not tested...
394:
1.142 brouard 395: Revision 1.141 2014/01/26 02:42:01 brouard
396: * imach.c (Module): Trying to merge old staffs together while being at Tokyo. Not tested...
397:
1.141 brouard 398: Revision 1.140 2011/09/02 10:37:54 brouard
399: Summary: times.h is ok with mingw32 now.
400:
1.140 brouard 401: Revision 1.139 2010/06/14 07:50:17 brouard
402: After the theft of my laptop, I probably lost some lines of codes which were not uploaded to the CVS tree.
403: I remember having already fixed agemin agemax which are pointers now but not cvs saved.
404:
1.139 brouard 405: Revision 1.138 2010/04/30 18:19:40 brouard
406: *** empty log message ***
407:
1.138 brouard 408: Revision 1.137 2010/04/29 18:11:38 brouard
409: (Module): Checking covariates for more complex models
410: than V1+V2. A lot of change to be done. Unstable.
411:
1.137 brouard 412: Revision 1.136 2010/04/26 20:30:53 brouard
413: (Module): merging some libgsl code. Fixing computation
414: of likelione (using inter/intrapolation if mle = 0) in order to
415: get same likelihood as if mle=1.
416: Some cleaning of code and comments added.
417:
1.136 brouard 418: Revision 1.135 2009/10/29 15:33:14 brouard
419: (Module): Now imach stops if date of birth, at least year of birth, is not given. Some cleaning of the code.
420:
1.135 brouard 421: Revision 1.134 2009/10/29 13:18:53 brouard
422: (Module): Now imach stops if date of birth, at least year of birth, is not given. Some cleaning of the code.
423:
1.134 brouard 424: Revision 1.133 2009/07/06 10:21:25 brouard
425: just nforces
426:
1.133 brouard 427: Revision 1.132 2009/07/06 08:22:05 brouard
428: Many tings
429:
1.132 brouard 430: Revision 1.131 2009/06/20 16:22:47 brouard
431: Some dimensions resccaled
432:
1.131 brouard 433: Revision 1.130 2009/05/26 06:44:34 brouard
434: (Module): Max Covariate is now set to 20 instead of 8. A
435: lot of cleaning with variables initialized to 0. Trying to make
436: V2+V3*age+V1+V4 strb=V3*age+V1+V4 working better.
437:
1.130 brouard 438: Revision 1.129 2007/08/31 13:49:27 lievre
439: Modification of the way of exiting when the covariate is not binary in order to see on the window the error message before exiting
440:
1.129 lievre 441: Revision 1.128 2006/06/30 13:02:05 brouard
442: (Module): Clarifications on computing e.j
443:
1.128 brouard 444: Revision 1.127 2006/04/28 18:11:50 brouard
445: (Module): Yes the sum of survivors was wrong since
446: imach-114 because nhstepm was no more computed in the age
447: loop. Now we define nhstepma in the age loop.
448: (Module): In order to speed up (in case of numerous covariates) we
449: compute health expectancies (without variances) in a first step
450: and then all the health expectancies with variances or standard
451: deviation (needs data from the Hessian matrices) which slows the
452: computation.
453: In the future we should be able to stop the program is only health
454: expectancies and graph are needed without standard deviations.
455:
1.127 brouard 456: Revision 1.126 2006/04/28 17:23:28 brouard
457: (Module): Yes the sum of survivors was wrong since
458: imach-114 because nhstepm was no more computed in the age
459: loop. Now we define nhstepma in the age loop.
460: Version 0.98h
461:
1.126 brouard 462: Revision 1.125 2006/04/04 15:20:31 lievre
463: Errors in calculation of health expectancies. Age was not initialized.
464: Forecasting file added.
465:
466: Revision 1.124 2006/03/22 17:13:53 lievre
467: Parameters are printed with %lf instead of %f (more numbers after the comma).
468: The log-likelihood is printed in the log file
469:
470: Revision 1.123 2006/03/20 10:52:43 brouard
471: * imach.c (Module): <title> changed, corresponds to .htm file
472: name. <head> headers where missing.
473:
474: * imach.c (Module): Weights can have a decimal point as for
475: English (a comma might work with a correct LC_NUMERIC environment,
476: otherwise the weight is truncated).
477: Modification of warning when the covariates values are not 0 or
478: 1.
479: Version 0.98g
480:
481: Revision 1.122 2006/03/20 09:45:41 brouard
482: (Module): Weights can have a decimal point as for
483: English (a comma might work with a correct LC_NUMERIC environment,
484: otherwise the weight is truncated).
485: Modification of warning when the covariates values are not 0 or
486: 1.
487: Version 0.98g
488:
489: Revision 1.121 2006/03/16 17:45:01 lievre
490: * imach.c (Module): Comments concerning covariates added
491:
492: * imach.c (Module): refinements in the computation of lli if
493: status=-2 in order to have more reliable computation if stepm is
494: not 1 month. Version 0.98f
495:
496: Revision 1.120 2006/03/16 15:10:38 lievre
497: (Module): refinements in the computation of lli if
498: status=-2 in order to have more reliable computation if stepm is
499: not 1 month. Version 0.98f
500:
501: Revision 1.119 2006/03/15 17:42:26 brouard
502: (Module): Bug if status = -2, the loglikelihood was
503: computed as likelihood omitting the logarithm. Version O.98e
504:
505: Revision 1.118 2006/03/14 18:20:07 brouard
506: (Module): varevsij Comments added explaining the second
507: table of variances if popbased=1 .
508: (Module): Covariances of eij, ekl added, graphs fixed, new html link.
509: (Module): Function pstamp added
510: (Module): Version 0.98d
511:
512: Revision 1.117 2006/03/14 17:16:22 brouard
513: (Module): varevsij Comments added explaining the second
514: table of variances if popbased=1 .
515: (Module): Covariances of eij, ekl added, graphs fixed, new html link.
516: (Module): Function pstamp added
517: (Module): Version 0.98d
518:
519: Revision 1.116 2006/03/06 10:29:27 brouard
520: (Module): Variance-covariance wrong links and
521: varian-covariance of ej. is needed (Saito).
522:
523: Revision 1.115 2006/02/27 12:17:45 brouard
524: (Module): One freematrix added in mlikeli! 0.98c
525:
526: Revision 1.114 2006/02/26 12:57:58 brouard
527: (Module): Some improvements in processing parameter
528: filename with strsep.
529:
530: Revision 1.113 2006/02/24 14:20:24 brouard
531: (Module): Memory leaks checks with valgrind and:
532: datafile was not closed, some imatrix were not freed and on matrix
533: allocation too.
534:
535: Revision 1.112 2006/01/30 09:55:26 brouard
536: (Module): Back to gnuplot.exe instead of wgnuplot.exe
537:
538: Revision 1.111 2006/01/25 20:38:18 brouard
539: (Module): Lots of cleaning and bugs added (Gompertz)
540: (Module): Comments can be added in data file. Missing date values
541: can be a simple dot '.'.
542:
543: Revision 1.110 2006/01/25 00:51:50 brouard
544: (Module): Lots of cleaning and bugs added (Gompertz)
545:
546: Revision 1.109 2006/01/24 19:37:15 brouard
547: (Module): Comments (lines starting with a #) are allowed in data.
548:
549: Revision 1.108 2006/01/19 18:05:42 lievre
550: Gnuplot problem appeared...
551: To be fixed
552:
553: Revision 1.107 2006/01/19 16:20:37 brouard
554: Test existence of gnuplot in imach path
555:
556: Revision 1.106 2006/01/19 13:24:36 brouard
557: Some cleaning and links added in html output
558:
559: Revision 1.105 2006/01/05 20:23:19 lievre
560: *** empty log message ***
561:
562: Revision 1.104 2005/09/30 16:11:43 lievre
563: (Module): sump fixed, loop imx fixed, and simplifications.
564: (Module): If the status is missing at the last wave but we know
565: that the person is alive, then we can code his/her status as -2
566: (instead of missing=-1 in earlier versions) and his/her
567: contributions to the likelihood is 1 - Prob of dying from last
568: health status (= 1-p13= p11+p12 in the easiest case of somebody in
569: the healthy state at last known wave). Version is 0.98
570:
571: Revision 1.103 2005/09/30 15:54:49 lievre
572: (Module): sump fixed, loop imx fixed, and simplifications.
573:
574: Revision 1.102 2004/09/15 17:31:30 brouard
575: Add the possibility to read data file including tab characters.
576:
577: Revision 1.101 2004/09/15 10:38:38 brouard
578: Fix on curr_time
579:
580: Revision 1.100 2004/07/12 18:29:06 brouard
581: Add version for Mac OS X. Just define UNIX in Makefile
582:
583: Revision 1.99 2004/06/05 08:57:40 brouard
584: *** empty log message ***
585:
586: Revision 1.98 2004/05/16 15:05:56 brouard
587: New version 0.97 . First attempt to estimate force of mortality
588: directly from the data i.e. without the need of knowing the health
589: state at each age, but using a Gompertz model: log u =a + b*age .
590: This is the basic analysis of mortality and should be done before any
591: other analysis, in order to test if the mortality estimated from the
592: cross-longitudinal survey is different from the mortality estimated
593: from other sources like vital statistic data.
594:
595: The same imach parameter file can be used but the option for mle should be -3.
596:
1.133 brouard 597: Agnès, who wrote this part of the code, tried to keep most of the
1.126 brouard 598: former routines in order to include the new code within the former code.
599:
600: The output is very simple: only an estimate of the intercept and of
601: the slope with 95% confident intervals.
602:
603: Current limitations:
604: A) Even if you enter covariates, i.e. with the
605: model= V1+V2 equation for example, the programm does only estimate a unique global model without covariates.
606: B) There is no computation of Life Expectancy nor Life Table.
607:
608: Revision 1.97 2004/02/20 13:25:42 lievre
609: Version 0.96d. Population forecasting command line is (temporarily)
610: suppressed.
611:
612: Revision 1.96 2003/07/15 15:38:55 brouard
613: * imach.c (Repository): Errors in subdirf, 2, 3 while printing tmpout is
614: rewritten within the same printf. Workaround: many printfs.
615:
616: Revision 1.95 2003/07/08 07:54:34 brouard
617: * imach.c (Repository):
618: (Repository): Using imachwizard code to output a more meaningful covariance
619: matrix (cov(a12,c31) instead of numbers.
620:
621: Revision 1.94 2003/06/27 13:00:02 brouard
622: Just cleaning
623:
624: Revision 1.93 2003/06/25 16:33:55 brouard
625: (Module): On windows (cygwin) function asctime_r doesn't
626: exist so I changed back to asctime which exists.
627: (Module): Version 0.96b
628:
629: Revision 1.92 2003/06/25 16:30:45 brouard
630: (Module): On windows (cygwin) function asctime_r doesn't
631: exist so I changed back to asctime which exists.
632:
633: Revision 1.91 2003/06/25 15:30:29 brouard
634: * imach.c (Repository): Duplicated warning errors corrected.
635: (Repository): Elapsed time after each iteration is now output. It
636: helps to forecast when convergence will be reached. Elapsed time
637: is stamped in powell. We created a new html file for the graphs
638: concerning matrix of covariance. It has extension -cov.htm.
639:
640: Revision 1.90 2003/06/24 12:34:15 brouard
641: (Module): Some bugs corrected for windows. Also, when
642: mle=-1 a template is output in file "or"mypar.txt with the design
643: of the covariance matrix to be input.
644:
645: Revision 1.89 2003/06/24 12:30:52 brouard
646: (Module): Some bugs corrected for windows. Also, when
647: mle=-1 a template is output in file "or"mypar.txt with the design
648: of the covariance matrix to be input.
649:
650: Revision 1.88 2003/06/23 17:54:56 brouard
651: * 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.
652:
653: Revision 1.87 2003/06/18 12:26:01 brouard
654: Version 0.96
655:
656: Revision 1.86 2003/06/17 20:04:08 brouard
657: (Module): Change position of html and gnuplot routines and added
658: routine fileappend.
659:
660: Revision 1.85 2003/06/17 13:12:43 brouard
661: * imach.c (Repository): Check when date of death was earlier that
662: current date of interview. It may happen when the death was just
663: prior to the death. In this case, dh was negative and likelihood
664: was wrong (infinity). We still send an "Error" but patch by
665: assuming that the date of death was just one stepm after the
666: interview.
667: (Repository): Because some people have very long ID (first column)
668: we changed int to long in num[] and we added a new lvector for
669: memory allocation. But we also truncated to 8 characters (left
670: truncation)
671: (Repository): No more line truncation errors.
672:
673: Revision 1.84 2003/06/13 21:44:43 brouard
674: * imach.c (Repository): Replace "freqsummary" at a correct
675: place. It differs from routine "prevalence" which may be called
676: many times. Probs is memory consuming and must be used with
677: parcimony.
678: Version 0.95a3 (should output exactly the same maximization than 0.8a2)
679:
680: Revision 1.83 2003/06/10 13:39:11 lievre
681: *** empty log message ***
682:
683: Revision 1.82 2003/06/05 15:57:20 brouard
684: Add log in imach.c and fullversion number is now printed.
685:
686: */
687: /*
688: Interpolated Markov Chain
689:
690: Short summary of the programme:
691:
1.227 brouard 692: This program computes Healthy Life Expectancies or State-specific
693: (if states aren't health statuses) Expectancies from
694: cross-longitudinal data. Cross-longitudinal data consist in:
695:
696: -1- a first survey ("cross") where individuals from different ages
697: are interviewed on their health status or degree of disability (in
698: the case of a health survey which is our main interest)
699:
700: -2- at least a second wave of interviews ("longitudinal") which
701: measure each change (if any) in individual health status. Health
702: expectancies are computed from the time spent in each health state
703: according to a model. More health states you consider, more time is
704: necessary to reach the Maximum Likelihood of the parameters involved
705: in the model. The simplest model is the multinomial logistic model
706: where pij is the probability to be observed in state j at the second
707: wave conditional to be observed in state i at the first
708: wave. Therefore the model is: log(pij/pii)= aij + bij*age+ cij*sex +
709: etc , where 'age' is age and 'sex' is a covariate. If you want to
710: have a more complex model than "constant and age", you should modify
711: the program where the markup *Covariates have to be included here
712: again* invites you to do it. More covariates you add, slower the
1.126 brouard 713: convergence.
714:
715: The advantage of this computer programme, compared to a simple
716: multinomial logistic model, is clear when the delay between waves is not
717: identical for each individual. Also, if a individual missed an
718: intermediate interview, the information is lost, but taken into
719: account using an interpolation or extrapolation.
720:
721: hPijx is the probability to be observed in state i at age x+h
722: conditional to the observed state i at age x. The delay 'h' can be
723: split into an exact number (nh*stepm) of unobserved intermediate
724: states. This elementary transition (by month, quarter,
725: semester or year) is modelled as a multinomial logistic. The hPx
726: matrix is simply the matrix product of nh*stepm elementary matrices
727: and the contribution of each individual to the likelihood is simply
728: hPijx.
729:
730: Also this programme outputs the covariance matrix of the parameters but also
1.218 brouard 731: of the life expectancies. It also computes the period (stable) prevalence.
732:
733: Back prevalence and projections:
1.227 brouard 734:
735: - back_prevalence_limit(double *p, double **bprlim, double ageminpar,
736: double agemaxpar, double ftolpl, int *ncvyearp, double
737: dateprev1,double dateprev2, int firstpass, int lastpass, int
738: mobilavproj)
739:
740: Computes the back prevalence limit for any combination of
741: covariate values k at any age between ageminpar and agemaxpar and
742: returns it in **bprlim. In the loops,
743:
744: - **bprevalim(**bprlim, ***mobaverage, nlstate, *p, age, **oldm,
745: **savm, **dnewm, **doldm, **dsavm, ftolpl, ncvyearp, k);
746:
747: - hBijx Back Probability to be in state i at age x-h being in j at x
1.218 brouard 748: Computes for any combination of covariates k and any age between bage and fage
749: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
750: oldm=oldms;savm=savms;
1.227 brouard 751:
752: - hbxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k);
1.218 brouard 753: Computes the transition matrix starting at age 'age' over
754: 'nhstepm*hstepm*stepm' months (i.e. until
755: age (in years) age+nhstepm*hstepm*stepm/12) by multiplying
1.227 brouard 756: nhstepm*hstepm matrices.
757:
758: Returns p3mat[i][j][h] after calling
759: p3mat[i][j][h]=matprod2(newm,
760: bmij(pmmij,cov,ncovmodel,x,nlstate,prevacurrent, dnewm, doldm,
761: dsavm,ij),\ 1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath,
762: oldm);
1.226 brouard 763:
764: Important routines
765:
766: - func (or funcone), computes logit (pij) distinguishing
767: o fixed variables (single or product dummies or quantitative);
768: o varying variables by:
769: (1) wave (single, product dummies, quantitative),
770: (2) by age (can be month) age (done), age*age (done), age*Vn where Vn can be:
771: % fixed dummy (treated) or quantitative (not done because time-consuming);
772: % varying dummy (not done) or quantitative (not done);
773: - Tricode which tests the modality of dummy variables (in order to warn with wrong or empty modalities)
774: and returns the number of efficient covariates cptcoveff and modalities nbcode[Tvar[k]][1]= 0 and nbcode[Tvar[k]][2]= 1 usually.
775: - printinghtml which outputs results like life expectancy in and from a state for a combination of modalities of dummy variables
776: o There are 2*cptcoveff combinations of (0,1) for cptcoveff variables. Outputting only combinations with people, éliminating 1 1 if
777: race White (0 0), Black vs White (1 0), Hispanic (0 1) and 1 1 being meaningless.
1.218 brouard 778:
1.226 brouard 779:
780:
1.133 brouard 781: Authors: Nicolas Brouard (brouard@ined.fr) and Agnès Lièvre (lievre@ined.fr).
782: Institut national d'études démographiques, Paris.
1.126 brouard 783: This software have been partly granted by Euro-REVES, a concerted action
784: from the European Union.
785: It is copyrighted identically to a GNU software product, ie programme and
786: software can be distributed freely for non commercial use. Latest version
787: can be accessed at http://euroreves.ined.fr/imach .
788:
789: Help to debug: LD_PRELOAD=/usr/local/lib/libnjamd.so ./imach foo.imach
790: or better on gdb : set env LD_PRELOAD=/usr/local/lib/libnjamd.so
791:
792: **********************************************************************/
793: /*
794: main
795: read parameterfile
796: read datafile
797: concatwav
798: freqsummary
799: if (mle >= 1)
800: mlikeli
801: print results files
802: if mle==1
803: computes hessian
804: read end of parameter file: agemin, agemax, bage, fage, estepm
805: begin-prev-date,...
806: open gnuplot file
807: open html file
1.145 brouard 808: period (stable) prevalence | pl_nom 1-1 2-2 etc by covariate
809: for age prevalim() | #****** V1=0 V2=1 V3=1 V4=0 ******
810: | 65 1 0 2 1 3 1 4 0 0.96326 0.03674
811: freexexit2 possible for memory heap.
812:
813: h Pij x | pij_nom ficrestpij
814: # Cov Agex agex+h hpijx with i,j= 1-1 1-2 1-3 2-1 2-2 2-3
815: 1 85 85 1.00000 0.00000 0.00000 0.00000 1.00000 0.00000
816: 1 85 86 0.68299 0.22291 0.09410 0.71093 0.00000 0.28907
817:
818: 1 65 99 0.00364 0.00322 0.99314 0.00350 0.00310 0.99340
819: 1 65 100 0.00214 0.00204 0.99581 0.00206 0.00196 0.99597
820: variance of p one-step probabilities varprob | prob_nom ficresprob #One-step probabilities and stand. devi in ()
821: Standard deviation of one-step probabilities | probcor_nom ficresprobcor #One-step probabilities and correlation matrix
822: Matrix of variance covariance of one-step probabilities | probcov_nom ficresprobcov #One-step probabilities and covariance matrix
823:
1.126 brouard 824: forecasting if prevfcast==1 prevforecast call prevalence()
825: health expectancies
826: Variance-covariance of DFLE
827: prevalence()
828: movingaverage()
829: varevsij()
830: if popbased==1 varevsij(,popbased)
831: total life expectancies
832: Variance of period (stable) prevalence
833: end
834: */
835:
1.187 brouard 836: /* #define DEBUG */
837: /* #define DEBUGBRENT */
1.203 brouard 838: /* #define DEBUGLINMIN */
839: /* #define DEBUGHESS */
840: #define DEBUGHESSIJ
1.224 brouard 841: /* #define LINMINORIGINAL /\* Don't use loop on scale in linmin (accepting nan) *\/ */
1.165 brouard 842: #define POWELL /* Instead of NLOPT */
1.224 brouard 843: #define POWELLNOF3INFF1TEST /* Skip test */
1.186 brouard 844: /* #define POWELLORIGINAL /\* Don't use Directest to decide new direction but original Powell test *\/ */
845: /* #define MNBRAKORIGINAL /\* Don't use mnbrak fix *\/ */
1.126 brouard 846:
847: #include <math.h>
848: #include <stdio.h>
849: #include <stdlib.h>
850: #include <string.h>
1.226 brouard 851: #include <ctype.h>
1.159 brouard 852:
853: #ifdef _WIN32
854: #include <io.h>
1.172 brouard 855: #include <windows.h>
856: #include <tchar.h>
1.159 brouard 857: #else
1.126 brouard 858: #include <unistd.h>
1.159 brouard 859: #endif
1.126 brouard 860:
861: #include <limits.h>
862: #include <sys/types.h>
1.171 brouard 863:
864: #if defined(__GNUC__)
865: #include <sys/utsname.h> /* Doesn't work on Windows */
866: #endif
867:
1.126 brouard 868: #include <sys/stat.h>
869: #include <errno.h>
1.159 brouard 870: /* extern int errno; */
1.126 brouard 871:
1.157 brouard 872: /* #ifdef LINUX */
873: /* #include <time.h> */
874: /* #include "timeval.h" */
875: /* #else */
876: /* #include <sys/time.h> */
877: /* #endif */
878:
1.126 brouard 879: #include <time.h>
880:
1.136 brouard 881: #ifdef GSL
882: #include <gsl/gsl_errno.h>
883: #include <gsl/gsl_multimin.h>
884: #endif
885:
1.167 brouard 886:
1.162 brouard 887: #ifdef NLOPT
888: #include <nlopt.h>
889: typedef struct {
890: double (* function)(double [] );
891: } myfunc_data ;
892: #endif
893:
1.126 brouard 894: /* #include <libintl.h> */
895: /* #define _(String) gettext (String) */
896:
1.141 brouard 897: #define MAXLINE 1024 /* Was 256. Overflow with 312 with 2 states and 4 covariates. Should be ok */
1.126 brouard 898:
899: #define GNUPLOTPROGRAM "gnuplot"
900: /*#define GNUPLOTPROGRAM "..\\gp37mgw\\wgnuplot"*/
901: #define FILENAMELENGTH 132
902:
903: #define GLOCK_ERROR_NOPATH -1 /* empty path */
904: #define GLOCK_ERROR_GETCWD -2 /* cannot get cwd */
905:
1.144 brouard 906: #define MAXPARM 128 /**< Maximum number of parameters for the optimization */
907: #define NPARMAX 64 /**< (nlstate+ndeath-1)*nlstate*ncovmodel */
1.126 brouard 908:
909: #define NINTERVMAX 8
1.144 brouard 910: #define NLSTATEMAX 8 /**< Maximum number of live states (for func) */
911: #define NDEATHMAX 8 /**< Maximum number of dead states (for func) */
912: #define NCOVMAX 20 /**< Maximum number of covariates, including generated covariates V1*V2 */
1.197 brouard 913: #define codtabm(h,k) (1 & (h-1) >> (k-1))+1
1.211 brouard 914: /*#define decodtabm(h,k,cptcoveff)= (h <= (1<<cptcoveff)?(((h-1) >> (k-1)) & 1) +1 : -1)*/
915: #define decodtabm(h,k,cptcoveff) (((h-1) >> (k-1)) & 1) +1
1.126 brouard 916: #define MAXN 20000
1.144 brouard 917: #define YEARM 12. /**< Number of months per year */
1.218 brouard 918: /* #define AGESUP 130 */
919: #define AGESUP 150
920: #define AGEMARGE 25 /* Marge for agemin and agemax for(iage=agemin-AGEMARGE; iage <= agemax+3+AGEMARGE; iage++) */
1.126 brouard 921: #define AGEBASE 40
1.194 brouard 922: #define AGEOVERFLOW 1.e20
1.164 brouard 923: #define AGEGOMP 10 /**< Minimal age for Gompertz adjustment */
1.157 brouard 924: #ifdef _WIN32
925: #define DIRSEPARATOR '\\'
926: #define CHARSEPARATOR "\\"
927: #define ODIRSEPARATOR '/'
928: #else
1.126 brouard 929: #define DIRSEPARATOR '/'
930: #define CHARSEPARATOR "/"
931: #define ODIRSEPARATOR '\\'
932: #endif
933:
1.244 ! brouard 934: /* $Id: imach.c,v 1.243 2016/09/02 06:45:35 brouard Exp $ */
1.126 brouard 935: /* $State: Exp $ */
1.196 brouard 936: #include "version.h"
937: char version[]=__IMACH_VERSION__;
1.224 brouard 938: 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.244 ! brouard 939: char fullversion[]="$Revision: 1.243 $ $Date: 2016/09/02 06:45:35 $";
1.126 brouard 940: char strstart[80];
941: char optionfilext[10], optionfilefiname[FILENAMELENGTH];
1.130 brouard 942: int erreur=0, nberr=0, nbwarn=0; /* Error number, number of errors number of warnings */
1.187 brouard 943: int nagesqr=0, nforce=0; /* nagesqr=1 if model is including age*age, number of forces */
1.145 brouard 944: /* Number of covariates model=V2+V1+ V3*age+V2*V4 */
945: int cptcovn=0; /**< cptcovn number of covariates added in the model (excepting constant and age and age*product) */
946: int cptcovt=0; /**< cptcovt number of covariates added in the model (excepting constant and age) */
1.225 brouard 947: int cptcovs=0; /**< cptcovs number of simple covariates in the model V2+V1 =2 */
948: int cptcovsnq=0; /**< cptcovsnq number of simple covariates in the model but non quantitative V2+V1 =2 */
1.145 brouard 949: int cptcovage=0; /**< Number of covariates with age: V3*age only =1 */
950: int cptcovprodnoage=0; /**< Number of covariate products without age */
951: int cptcoveff=0; /* Total number of covariates to vary for printing results */
1.233 brouard 952: int ncovf=0; /* Total number of effective fixed covariates (dummy or quantitative) in the model */
953: int ncovv=0; /* Total number of effective (wave) varying covariates (dummy or quantitative) in the model */
1.232 brouard 954: int ncova=0; /* Total number of effective (wave and stepm) varying with age covariates (dummy of quantitative) in the model */
1.234 brouard 955: int nsd=0; /**< Total number of single dummy variables (output) */
956: int nsq=0; /**< Total number of single quantitative variables (output) */
1.232 brouard 957: int ncoveff=0; /* Total number of effective fixed dummy covariates in the model */
1.225 brouard 958: int nqfveff=0; /**< nqfveff Number of Quantitative Fixed Variables Effective */
1.224 brouard 959: int ntveff=0; /**< ntveff number of effective time varying variables */
960: int nqtveff=0; /**< ntqveff number of effective time varying quantitative variables */
1.145 brouard 961: int cptcov=0; /* Working variable */
1.218 brouard 962: int ncovcombmax=NCOVMAX; /* Maximum calculated number of covariate combination = pow(2, cptcoveff) */
1.126 brouard 963: int npar=NPARMAX;
964: int nlstate=2; /* Number of live states */
965: int ndeath=1; /* Number of dead states */
1.130 brouard 966: int ncovmodel=0, ncovcol=0; /* Total number of covariables including constant a12*1 +b12*x ncovmodel=2 */
1.223 brouard 967: int nqv=0, ntv=0, nqtv=0; /* Total number of quantitative variables, time variable (dummy), quantitative and time variable */
1.126 brouard 968: int popbased=0;
969:
970: int *wav; /* Number of waves for this individuual 0 is possible */
1.130 brouard 971: int maxwav=0; /* Maxim number of waves */
972: int jmin=0, jmax=0; /* min, max spacing between 2 waves */
973: int ijmin=0, ijmax=0; /* Individuals having jmin and jmax */
974: int gipmx=0, gsw=0; /* Global variables on the number of contributions
1.126 brouard 975: to the likelihood and the sum of weights (done by funcone)*/
1.130 brouard 976: int mle=1, weightopt=0;
1.126 brouard 977: int **mw; /* mw[mi][i] is number of the mi wave for this individual */
978: int **dh; /* dh[mi][i] is number of steps between mi,mi+1 for this individual */
979: int **bh; /* bh[mi][i] is the bias (+ or -) for this individual if the delay between
980: * wave mi and wave mi+1 is not an exact multiple of stepm. */
1.162 brouard 981: int countcallfunc=0; /* Count the number of calls to func */
1.230 brouard 982: int selected(int kvar); /* Is covariate kvar selected for printing results */
983:
1.130 brouard 984: double jmean=1; /* Mean space between 2 waves */
1.145 brouard 985: double **matprod2(); /* test */
1.126 brouard 986: double **oldm, **newm, **savm; /* Working pointers to matrices */
987: double **oldms, **newms, **savms; /* Fixed working pointers to matrices */
1.218 brouard 988: double **ddnewms, **ddoldms, **ddsavms; /* for freeing later */
989:
1.136 brouard 990: /*FILE *fic ; */ /* Used in readdata only */
1.217 brouard 991: FILE *ficpar, *ficparo,*ficres, *ficresp, *ficresphtm, *ficresphtmfr, *ficrespl, *ficresplb,*ficrespij, *ficrespijb, *ficrest,*ficresf, *ficresfb,*ficrespop;
1.126 brouard 992: FILE *ficlog, *ficrespow;
1.130 brouard 993: int globpr=0; /* Global variable for printing or not */
1.126 brouard 994: double fretone; /* Only one call to likelihood */
1.130 brouard 995: long ipmx=0; /* Number of contributions */
1.126 brouard 996: double sw; /* Sum of weights */
997: char filerespow[FILENAMELENGTH];
998: char fileresilk[FILENAMELENGTH]; /* File of individual contributions to the likelihood */
999: FILE *ficresilk;
1000: FILE *ficgp,*ficresprob,*ficpop, *ficresprobcov, *ficresprobcor;
1001: FILE *ficresprobmorprev;
1002: FILE *fichtm, *fichtmcov; /* Html File */
1003: FILE *ficreseij;
1004: char filerese[FILENAMELENGTH];
1005: FILE *ficresstdeij;
1006: char fileresstde[FILENAMELENGTH];
1007: FILE *ficrescveij;
1008: char filerescve[FILENAMELENGTH];
1009: FILE *ficresvij;
1010: char fileresv[FILENAMELENGTH];
1011: FILE *ficresvpl;
1012: char fileresvpl[FILENAMELENGTH];
1013: char title[MAXLINE];
1.234 brouard 1014: char model[MAXLINE]; /**< The model line */
1.217 brouard 1015: char optionfile[FILENAMELENGTH], datafile[FILENAMELENGTH], filerespl[FILENAMELENGTH], fileresplb[FILENAMELENGTH];
1.126 brouard 1016: char plotcmd[FILENAMELENGTH], pplotcmd[FILENAMELENGTH];
1017: char tmpout[FILENAMELENGTH], tmpout2[FILENAMELENGTH];
1018: char command[FILENAMELENGTH];
1019: int outcmd=0;
1020:
1.217 brouard 1021: char fileres[FILENAMELENGTH], filerespij[FILENAMELENGTH], filerespijb[FILENAMELENGTH], filereso[FILENAMELENGTH], rfileres[FILENAMELENGTH];
1.202 brouard 1022: char fileresu[FILENAMELENGTH]; /* fileres without r in front */
1.126 brouard 1023: char filelog[FILENAMELENGTH]; /* Log file */
1024: char filerest[FILENAMELENGTH];
1025: char fileregp[FILENAMELENGTH];
1026: char popfile[FILENAMELENGTH];
1027:
1028: char optionfilegnuplot[FILENAMELENGTH], optionfilehtm[FILENAMELENGTH], optionfilehtmcov[FILENAMELENGTH] ;
1029:
1.157 brouard 1030: /* struct timeval start_time, end_time, curr_time, last_time, forecast_time; */
1031: /* struct timezone tzp; */
1032: /* extern int gettimeofday(); */
1033: struct tm tml, *gmtime(), *localtime();
1034:
1035: extern time_t time();
1036:
1037: struct tm start_time, end_time, curr_time, last_time, forecast_time;
1038: time_t rstart_time, rend_time, rcurr_time, rlast_time, rforecast_time; /* raw time */
1039: struct tm tm;
1040:
1.126 brouard 1041: char strcurr[80], strfor[80];
1042:
1043: char *endptr;
1044: long lval;
1045: double dval;
1046:
1047: #define NR_END 1
1048: #define FREE_ARG char*
1049: #define FTOL 1.0e-10
1050:
1051: #define NRANSI
1.240 brouard 1052: #define ITMAX 200
1053: #define ITPOWMAX 20 /* This is now multiplied by the number of parameters */
1.126 brouard 1054:
1055: #define TOL 2.0e-4
1056:
1057: #define CGOLD 0.3819660
1058: #define ZEPS 1.0e-10
1059: #define SHFT(a,b,c,d) (a)=(b);(b)=(c);(c)=(d);
1060:
1061: #define GOLD 1.618034
1062: #define GLIMIT 100.0
1063: #define TINY 1.0e-20
1064:
1065: static double maxarg1,maxarg2;
1066: #define FMAX(a,b) (maxarg1=(a),maxarg2=(b),(maxarg1)>(maxarg2)? (maxarg1):(maxarg2))
1067: #define FMIN(a,b) (maxarg1=(a),maxarg2=(b),(maxarg1)<(maxarg2)? (maxarg1):(maxarg2))
1068:
1069: #define SIGN(a,b) ((b)>0.0 ? fabs(a) : -fabs(a))
1070: #define rint(a) floor(a+0.5)
1.166 brouard 1071: /* http://www.thphys.uni-heidelberg.de/~robbers/cmbeasy/doc/html/myutils_8h-source.html */
1.183 brouard 1072: #define mytinydouble 1.0e-16
1.166 brouard 1073: /* #define DEQUAL(a,b) (fabs((a)-(b))<mytinydouble) */
1074: /* http://www.thphys.uni-heidelberg.de/~robbers/cmbeasy/doc/html/mynrutils_8h-source.html */
1075: /* static double dsqrarg; */
1076: /* #define DSQR(a) (DEQUAL((dsqrarg=(a)),0.0) ? 0.0 : dsqrarg*dsqrarg) */
1.126 brouard 1077: static double sqrarg;
1078: #define SQR(a) ((sqrarg=(a)) == 0.0 ? 0.0 :sqrarg*sqrarg)
1079: #define SWAP(a,b) {temp=(a);(a)=(b);(b)=temp;}
1080: int agegomp= AGEGOMP;
1081:
1082: int imx;
1083: int stepm=1;
1084: /* Stepm, step in month: minimum step interpolation*/
1085:
1086: int estepm;
1087: /* Estepm, step in month to interpolate survival function in order to approximate Life Expectancy*/
1088:
1089: int m,nb;
1090: long *num;
1.197 brouard 1091: int firstpass=0, lastpass=4,*cod, *cens;
1.192 brouard 1092: int *ncodemax; /* ncodemax[j]= Number of modalities of the j th
1093: covariate for which somebody answered excluding
1094: undefined. Usually 2: 0 and 1. */
1095: int *ncodemaxwundef; /* ncodemax[j]= Number of modalities of the j th
1096: covariate for which somebody answered including
1097: undefined. Usually 3: -1, 0 and 1. */
1.126 brouard 1098: double **agev,*moisnais, *annais, *moisdc, *andc,**mint, **anint;
1.218 brouard 1099: double **pmmij, ***probs; /* Global pointer */
1.219 brouard 1100: double ***mobaverage, ***mobaverages; /* New global variable */
1.126 brouard 1101: double *ageexmed,*agecens;
1102: double dateintmean=0;
1103:
1104: double *weight;
1105: int **s; /* Status */
1.141 brouard 1106: double *agedc;
1.145 brouard 1107: double **covar; /**< covar[j,i], value of jth covariate for individual i,
1.141 brouard 1108: * covar=matrix(0,NCOVMAX,1,n);
1.187 brouard 1109: * cov[Tage[kk]+2]=covar[Tvar[Tage[kk]]][i]*age; */
1.225 brouard 1110: double **coqvar; /* Fixed quantitative covariate iqv */
1111: double ***cotvar; /* Time varying covariate itv */
1112: double ***cotqvar; /* Time varying quantitative covariate itqv */
1.141 brouard 1113: double idx;
1114: int **nbcode, *Tvar; /**< model=V2 => Tvar[1]= 2 */
1.234 brouard 1115: /* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
1116: /*k 1 2 3 4 5 6 7 8 9 */
1117: /*Tvar[k]= 5 4 3 6 5 2 7 1 1 */
1118: /* Tndvar[k] 1 2 3 4 5 */
1119: /*TDvar 4 3 6 7 1 */ /* For outputs only; combination of dummies fixed or varying */
1120: /* Tns[k] 1 2 2 4 5 */ /* Number of single cova */
1121: /* TvarsD[k] 1 2 3 */ /* Number of single dummy cova */
1122: /* TvarsDind 2 3 9 */ /* position K of single dummy cova */
1123: /* TvarsQ[k] 1 2 */ /* Number of single quantitative cova */
1124: /* TvarsQind 1 6 */ /* position K of single quantitative cova */
1125: /* Tprod[i]=k 4 7 */
1126: /* Tage[i]=k 5 8 */
1127: /* */
1128: /* Type */
1129: /* V 1 2 3 4 5 */
1130: /* F F V V V */
1131: /* D Q D D Q */
1132: /* */
1133: int *TvarsD;
1134: int *TvarsDind;
1135: int *TvarsQ;
1136: int *TvarsQind;
1137:
1.235 brouard 1138: #define MAXRESULTLINES 10
1139: int nresult=0;
1140: int TKresult[MAXRESULTLINES];
1.237 brouard 1141: int Tresult[MAXRESULTLINES][NCOVMAX];/* For dummy variable , value (output) */
1142: int Tinvresult[MAXRESULTLINES][NCOVMAX];/* For dummy variable , value (output) */
1.235 brouard 1143: int Tvresult[MAXRESULTLINES][NCOVMAX]; /* For dummy variable , variable # (output) */
1144: double Tqresult[MAXRESULTLINES][NCOVMAX]; /* For quantitative variable , value (output) */
1.237 brouard 1145: double Tqinvresult[MAXRESULTLINES][NCOVMAX]; /* For quantitative variable , value (output) */
1.235 brouard 1146: int Tvqresult[MAXRESULTLINES][NCOVMAX]; /* For quantitative variable , variable # (output) */
1147:
1.234 brouard 1148: /* 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 1149: 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 */
1150: 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 */
1151: 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 */
1152: int *TvarVind; /**< TvarVind[1]=1, TvarVind[2]=2 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
1153: 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 */
1154: 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 1155: int *TvarFD; /**< TvarFD[1]=V1 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
1156: int *TvarFDind; /* TvarFDind[1]=9 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
1157: int *TvarFQ; /* TvarFQ[1]=V2 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ /* Only simple fixed quantitative variable */
1158: int *TvarFQind; /* TvarFQind[1]=6 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ /* Only simple fixed quantitative variable */
1159: int *TvarVD; /* TvarVD[1]=V5 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ /* Only simple fixed quantitative variable */
1160: int *TvarVDind; /* TvarVDind[1]=1 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ /* Only simple fixed quantitative variable */
1161: 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 */
1162: 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 */
1163:
1.230 brouard 1164: int *Tvarsel; /**< Selected covariates for output */
1165: double *Tvalsel; /**< Selected modality value of covariate for output */
1.226 brouard 1166: int *Typevar; /**< 0 for simple covariate (dummy, quantitative, fixed or varying), 1 for age product, 2 for product */
1.227 brouard 1167: int *Fixed; /** Fixed[k] 0=fixed, 1 varying, 2 fixed with age product, 3 varying with age product */
1168: 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 1169: int *DummyV; /** Dummy[v] 0=dummy (0 1), 1 quantitative */
1170: int *FixedV; /** FixedV[v] 0 fixed, 1 varying */
1.197 brouard 1171: int *Tage;
1.227 brouard 1172: int anyvaryingduminmodel=0; /**< Any varying dummy in Model=1 yes, 0 no, to avoid a loop on waves in freq */
1.228 brouard 1173: 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 1174: 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*/
1175: 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 1176: int *Ndum; /** Freq of modality (tricode */
1.200 brouard 1177: /* int **codtab;*/ /**< codtab=imatrix(1,100,1,10); */
1.227 brouard 1178: int **Tvard;
1179: int *Tprod;/**< Gives the k position of the k1 product */
1.238 brouard 1180: /* Tprod[k1=1]=3(=V1*V4) for V2+V1+V1*V4+age*V3 */
1.227 brouard 1181: int *Tposprod; /**< Gives the k1 product from the k position */
1.238 brouard 1182: /* if V2+V1+V1*V4+age*V3+V3*V2 TProd[k1=2]=5 (V3*V2) */
1183: /* Tposprod[k]=k1 , Tposprod[3]=1, Tposprod[5(V3*V2)]=2 (2nd product without age) */
1.227 brouard 1184: int cptcovprod, *Tvaraff, *invalidvarcomb;
1.126 brouard 1185: double *lsurv, *lpop, *tpop;
1186:
1.231 brouard 1187: #define FD 1; /* Fixed dummy covariate */
1188: #define FQ 2; /* Fixed quantitative covariate */
1189: #define FP 3; /* Fixed product covariate */
1190: #define FPDD 7; /* Fixed product dummy*dummy covariate */
1191: #define FPDQ 8; /* Fixed product dummy*quantitative covariate */
1192: #define FPQQ 9; /* Fixed product quantitative*quantitative covariate */
1193: #define VD 10; /* Varying dummy covariate */
1194: #define VQ 11; /* Varying quantitative covariate */
1195: #define VP 12; /* Varying product covariate */
1196: #define VPDD 13; /* Varying product dummy*dummy covariate */
1197: #define VPDQ 14; /* Varying product dummy*quantitative covariate */
1198: #define VPQQ 15; /* Varying product quantitative*quantitative covariate */
1199: #define APFD 16; /* Age product * fixed dummy covariate */
1200: #define APFQ 17; /* Age product * fixed quantitative covariate */
1201: #define APVD 18; /* Age product * varying dummy covariate */
1202: #define APVQ 19; /* Age product * varying quantitative covariate */
1203:
1204: #define FTYPE 1; /* Fixed covariate */
1205: #define VTYPE 2; /* Varying covariate (loop in wave) */
1206: #define ATYPE 2; /* Age product covariate (loop in dh within wave)*/
1207:
1208: struct kmodel{
1209: int maintype; /* main type */
1210: int subtype; /* subtype */
1211: };
1212: struct kmodel modell[NCOVMAX];
1213:
1.143 brouard 1214: double ftol=FTOL; /**< Tolerance for computing Max Likelihood */
1215: double ftolhess; /**< Tolerance for computing hessian */
1.126 brouard 1216:
1217: /**************** split *************************/
1218: static int split( char *path, char *dirc, char *name, char *ext, char *finame )
1219: {
1220: /* From a file name with (full) path (either Unix or Windows) we extract the directory (dirc)
1221: the name of the file (name), its extension only (ext) and its first part of the name (finame)
1222: */
1223: char *ss; /* pointer */
1.186 brouard 1224: int l1=0, l2=0; /* length counters */
1.126 brouard 1225:
1226: l1 = strlen(path ); /* length of path */
1227: if ( l1 == 0 ) return( GLOCK_ERROR_NOPATH );
1228: ss= strrchr( path, DIRSEPARATOR ); /* find last / */
1229: if ( ss == NULL ) { /* no directory, so determine current directory */
1230: strcpy( name, path ); /* we got the fullname name because no directory */
1231: /*if(strrchr(path, ODIRSEPARATOR )==NULL)
1232: printf("Warning you should use %s as a separator\n",DIRSEPARATOR);*/
1233: /* get current working directory */
1234: /* extern char* getcwd ( char *buf , int len);*/
1.184 brouard 1235: #ifdef WIN32
1236: if (_getcwd( dirc, FILENAME_MAX ) == NULL ) {
1237: #else
1238: if (getcwd(dirc, FILENAME_MAX) == NULL) {
1239: #endif
1.126 brouard 1240: return( GLOCK_ERROR_GETCWD );
1241: }
1242: /* got dirc from getcwd*/
1243: printf(" DIRC = %s \n",dirc);
1.205 brouard 1244: } else { /* strip directory from path */
1.126 brouard 1245: ss++; /* after this, the filename */
1246: l2 = strlen( ss ); /* length of filename */
1247: if ( l2 == 0 ) return( GLOCK_ERROR_NOPATH );
1248: strcpy( name, ss ); /* save file name */
1249: strncpy( dirc, path, l1 - l2 ); /* now the directory */
1.186 brouard 1250: dirc[l1-l2] = '\0'; /* add zero */
1.126 brouard 1251: printf(" DIRC2 = %s \n",dirc);
1252: }
1253: /* We add a separator at the end of dirc if not exists */
1254: l1 = strlen( dirc ); /* length of directory */
1255: if( dirc[l1-1] != DIRSEPARATOR ){
1256: dirc[l1] = DIRSEPARATOR;
1257: dirc[l1+1] = 0;
1258: printf(" DIRC3 = %s \n",dirc);
1259: }
1260: ss = strrchr( name, '.' ); /* find last / */
1261: if (ss >0){
1262: ss++;
1263: strcpy(ext,ss); /* save extension */
1264: l1= strlen( name);
1265: l2= strlen(ss)+1;
1266: strncpy( finame, name, l1-l2);
1267: finame[l1-l2]= 0;
1268: }
1269:
1270: return( 0 ); /* we're done */
1271: }
1272:
1273:
1274: /******************************************/
1275:
1276: void replace_back_to_slash(char *s, char*t)
1277: {
1278: int i;
1279: int lg=0;
1280: i=0;
1281: lg=strlen(t);
1282: for(i=0; i<= lg; i++) {
1283: (s[i] = t[i]);
1284: if (t[i]== '\\') s[i]='/';
1285: }
1286: }
1287:
1.132 brouard 1288: char *trimbb(char *out, char *in)
1.137 brouard 1289: { /* Trim multiple blanks in line but keeps first blanks if line starts with blanks */
1.132 brouard 1290: char *s;
1291: s=out;
1292: while (*in != '\0'){
1.137 brouard 1293: while( *in == ' ' && *(in+1) == ' '){ /* && *(in+1) != '\0'){*/
1.132 brouard 1294: in++;
1295: }
1296: *out++ = *in++;
1297: }
1298: *out='\0';
1299: return s;
1300: }
1301:
1.187 brouard 1302: /* char *substrchaine(char *out, char *in, char *chain) */
1303: /* { */
1304: /* /\* Substract chain 'chain' from 'in', return and output 'out' *\/ */
1305: /* char *s, *t; */
1306: /* t=in;s=out; */
1307: /* while ((*in != *chain) && (*in != '\0')){ */
1308: /* *out++ = *in++; */
1309: /* } */
1310:
1311: /* /\* *in matches *chain *\/ */
1312: /* while ((*in++ == *chain++) && (*in != '\0')){ */
1313: /* printf("*in = %c, *out= %c *chain= %c \n", *in, *out, *chain); */
1314: /* } */
1315: /* in--; chain--; */
1316: /* while ( (*in != '\0')){ */
1317: /* printf("Bef *in = %c, *out= %c *chain= %c \n", *in, *out, *chain); */
1318: /* *out++ = *in++; */
1319: /* printf("Aft *in = %c, *out= %c *chain= %c \n", *in, *out, *chain); */
1320: /* } */
1321: /* *out='\0'; */
1322: /* out=s; */
1323: /* return out; */
1324: /* } */
1325: char *substrchaine(char *out, char *in, char *chain)
1326: {
1327: /* Substract chain 'chain' from 'in', return and output 'out' */
1328: /* in="V1+V1*age+age*age+V2", chain="age*age" */
1329:
1330: char *strloc;
1331:
1332: strcpy (out, in);
1333: strloc = strstr(out, chain); /* strloc points to out at age*age+V2 */
1334: printf("Bef strloc=%s chain=%s out=%s \n", strloc, chain, out);
1335: if(strloc != NULL){
1336: /* will affect out */ /* strloc+strlenc(chain)=+V2 */ /* Will also work in Unicode */
1337: memmove(strloc,strloc+strlen(chain), strlen(strloc+strlen(chain))+1);
1338: /* strcpy (strloc, strloc +strlen(chain));*/
1339: }
1340: printf("Aft strloc=%s chain=%s in=%s out=%s \n", strloc, chain, in, out);
1341: return out;
1342: }
1343:
1344:
1.145 brouard 1345: char *cutl(char *blocc, char *alocc, char *in, char occ)
1346: {
1.187 brouard 1347: /* cuts string in into blocc and alocc where blocc ends before FIRST occurence of char 'occ'
1.145 brouard 1348: and alocc starts after first occurence of char 'occ' : ex cutv(blocc,alocc,"abcdef2ghi2j",'2')
1.187 brouard 1349: gives blocc="abcdef" and alocc="ghi2j".
1.145 brouard 1350: If occ is not found blocc is null and alocc is equal to in. Returns blocc
1351: */
1.160 brouard 1352: char *s, *t;
1.145 brouard 1353: t=in;s=in;
1354: while ((*in != occ) && (*in != '\0')){
1355: *alocc++ = *in++;
1356: }
1357: if( *in == occ){
1358: *(alocc)='\0';
1359: s=++in;
1360: }
1361:
1362: if (s == t) {/* occ not found */
1363: *(alocc-(in-s))='\0';
1364: in=s;
1365: }
1366: while ( *in != '\0'){
1367: *blocc++ = *in++;
1368: }
1369:
1370: *blocc='\0';
1371: return t;
1372: }
1.137 brouard 1373: char *cutv(char *blocc, char *alocc, char *in, char occ)
1374: {
1.187 brouard 1375: /* cuts string in into blocc and alocc where blocc ends before LAST occurence of char 'occ'
1.137 brouard 1376: and alocc starts after last occurence of char 'occ' : ex cutv(blocc,alocc,"abcdef2ghi2j",'2')
1377: gives blocc="abcdef2ghi" and alocc="j".
1378: If occ is not found blocc is null and alocc is equal to in. Returns alocc
1379: */
1380: char *s, *t;
1381: t=in;s=in;
1382: while (*in != '\0'){
1383: while( *in == occ){
1384: *blocc++ = *in++;
1385: s=in;
1386: }
1387: *blocc++ = *in++;
1388: }
1389: if (s == t) /* occ not found */
1390: *(blocc-(in-s))='\0';
1391: else
1392: *(blocc-(in-s)-1)='\0';
1393: in=s;
1394: while ( *in != '\0'){
1395: *alocc++ = *in++;
1396: }
1397:
1398: *alocc='\0';
1399: return s;
1400: }
1401:
1.126 brouard 1402: int nbocc(char *s, char occ)
1403: {
1404: int i,j=0;
1405: int lg=20;
1406: i=0;
1407: lg=strlen(s);
1408: for(i=0; i<= lg; i++) {
1.234 brouard 1409: if (s[i] == occ ) j++;
1.126 brouard 1410: }
1411: return j;
1412: }
1413:
1.137 brouard 1414: /* void cutv(char *u,char *v, char*t, char occ) */
1415: /* { */
1416: /* /\* cuts string t into u and v where u ends before last occurence of char 'occ' */
1417: /* and v starts after last occurence of char 'occ' : ex cutv(u,v,"abcdef2ghi2j",'2') */
1418: /* gives u="abcdef2ghi" and v="j" *\/ */
1419: /* int i,lg,j,p=0; */
1420: /* i=0; */
1421: /* lg=strlen(t); */
1422: /* for(j=0; j<=lg-1; j++) { */
1423: /* if((t[j]!= occ) && (t[j+1]== occ)) p=j+1; */
1424: /* } */
1.126 brouard 1425:
1.137 brouard 1426: /* for(j=0; j<p; j++) { */
1427: /* (u[j] = t[j]); */
1428: /* } */
1429: /* u[p]='\0'; */
1.126 brouard 1430:
1.137 brouard 1431: /* for(j=0; j<= lg; j++) { */
1432: /* if (j>=(p+1))(v[j-p-1] = t[j]); */
1433: /* } */
1434: /* } */
1.126 brouard 1435:
1.160 brouard 1436: #ifdef _WIN32
1437: char * strsep(char **pp, const char *delim)
1438: {
1439: char *p, *q;
1440:
1441: if ((p = *pp) == NULL)
1442: return 0;
1443: if ((q = strpbrk (p, delim)) != NULL)
1444: {
1445: *pp = q + 1;
1446: *q = '\0';
1447: }
1448: else
1449: *pp = 0;
1450: return p;
1451: }
1452: #endif
1453:
1.126 brouard 1454: /********************** nrerror ********************/
1455:
1456: void nrerror(char error_text[])
1457: {
1458: fprintf(stderr,"ERREUR ...\n");
1459: fprintf(stderr,"%s\n",error_text);
1460: exit(EXIT_FAILURE);
1461: }
1462: /*********************** vector *******************/
1463: double *vector(int nl, int nh)
1464: {
1465: double *v;
1466: v=(double *) malloc((size_t)((nh-nl+1+NR_END)*sizeof(double)));
1467: if (!v) nrerror("allocation failure in vector");
1468: return v-nl+NR_END;
1469: }
1470:
1471: /************************ free vector ******************/
1472: void free_vector(double*v, int nl, int nh)
1473: {
1474: free((FREE_ARG)(v+nl-NR_END));
1475: }
1476:
1477: /************************ivector *******************************/
1478: int *ivector(long nl,long nh)
1479: {
1480: int *v;
1481: v=(int *) malloc((size_t)((nh-nl+1+NR_END)*sizeof(int)));
1482: if (!v) nrerror("allocation failure in ivector");
1483: return v-nl+NR_END;
1484: }
1485:
1486: /******************free ivector **************************/
1487: void free_ivector(int *v, long nl, long nh)
1488: {
1489: free((FREE_ARG)(v+nl-NR_END));
1490: }
1491:
1492: /************************lvector *******************************/
1493: long *lvector(long nl,long nh)
1494: {
1495: long *v;
1496: v=(long *) malloc((size_t)((nh-nl+1+NR_END)*sizeof(long)));
1497: if (!v) nrerror("allocation failure in ivector");
1498: return v-nl+NR_END;
1499: }
1500:
1501: /******************free lvector **************************/
1502: void free_lvector(long *v, long nl, long nh)
1503: {
1504: free((FREE_ARG)(v+nl-NR_END));
1505: }
1506:
1507: /******************* imatrix *******************************/
1508: int **imatrix(long nrl, long nrh, long ncl, long nch)
1509: /* allocate a int matrix with subscript range m[nrl..nrh][ncl..nch] */
1510: {
1511: long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
1512: int **m;
1513:
1514: /* allocate pointers to rows */
1515: m=(int **) malloc((size_t)((nrow+NR_END)*sizeof(int*)));
1516: if (!m) nrerror("allocation failure 1 in matrix()");
1517: m += NR_END;
1518: m -= nrl;
1519:
1520:
1521: /* allocate rows and set pointers to them */
1522: m[nrl]=(int *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(int)));
1523: if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
1524: m[nrl] += NR_END;
1525: m[nrl] -= ncl;
1526:
1527: for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
1528:
1529: /* return pointer to array of pointers to rows */
1530: return m;
1531: }
1532:
1533: /****************** free_imatrix *************************/
1534: void free_imatrix(m,nrl,nrh,ncl,nch)
1535: int **m;
1536: long nch,ncl,nrh,nrl;
1537: /* free an int matrix allocated by imatrix() */
1538: {
1539: free((FREE_ARG) (m[nrl]+ncl-NR_END));
1540: free((FREE_ARG) (m+nrl-NR_END));
1541: }
1542:
1543: /******************* matrix *******************************/
1544: double **matrix(long nrl, long nrh, long ncl, long nch)
1545: {
1546: long i, nrow=nrh-nrl+1, ncol=nch-ncl+1;
1547: double **m;
1548:
1549: m=(double **) malloc((size_t)((nrow+NR_END)*sizeof(double*)));
1550: if (!m) nrerror("allocation failure 1 in matrix()");
1551: m += NR_END;
1552: m -= nrl;
1553:
1554: m[nrl]=(double *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(double)));
1555: if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
1556: m[nrl] += NR_END;
1557: m[nrl] -= ncl;
1558:
1559: for (i=nrl+1; i<=nrh; i++) m[i]=m[i-1]+ncol;
1560: return m;
1.145 brouard 1561: /* print *(*(m+1)+70) or print m[1][70]; print m+1 or print &(m[1]) or &(m[1][0])
1562: m[i] = address of ith row of the table. &(m[i]) is its value which is another adress
1563: that of m[i][0]. In order to get the value p m[i][0] but it is unitialized.
1.126 brouard 1564: */
1565: }
1566:
1567: /*************************free matrix ************************/
1568: void free_matrix(double **m, long nrl, long nrh, long ncl, long nch)
1569: {
1570: free((FREE_ARG)(m[nrl]+ncl-NR_END));
1571: free((FREE_ARG)(m+nrl-NR_END));
1572: }
1573:
1574: /******************* ma3x *******************************/
1575: double ***ma3x(long nrl, long nrh, long ncl, long nch, long nll, long nlh)
1576: {
1577: long i, j, nrow=nrh-nrl+1, ncol=nch-ncl+1, nlay=nlh-nll+1;
1578: double ***m;
1579:
1580: m=(double ***) malloc((size_t)((nrow+NR_END)*sizeof(double*)));
1581: if (!m) nrerror("allocation failure 1 in matrix()");
1582: m += NR_END;
1583: m -= nrl;
1584:
1585: m[nrl]=(double **) malloc((size_t)((nrow*ncol+NR_END)*sizeof(double)));
1586: if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
1587: m[nrl] += NR_END;
1588: m[nrl] -= ncl;
1589:
1590: for (i=nrl+1; i<=nrh; i++) m[i]=m[i-1]+ncol;
1591:
1592: m[nrl][ncl]=(double *) malloc((size_t)((nrow*ncol*nlay+NR_END)*sizeof(double)));
1593: if (!m[nrl][ncl]) nrerror("allocation failure 3 in matrix()");
1594: m[nrl][ncl] += NR_END;
1595: m[nrl][ncl] -= nll;
1596: for (j=ncl+1; j<=nch; j++)
1597: m[nrl][j]=m[nrl][j-1]+nlay;
1598:
1599: for (i=nrl+1; i<=nrh; i++) {
1600: m[i][ncl]=m[i-1l][ncl]+ncol*nlay;
1601: for (j=ncl+1; j<=nch; j++)
1602: m[i][j]=m[i][j-1]+nlay;
1603: }
1604: return m;
1605: /* gdb: p *(m+1) <=> p m[1] and p (m+1) <=> p (m+1) <=> p &(m[1])
1606: &(m[i][j][k]) <=> *((*(m+i) + j)+k)
1607: */
1608: }
1609:
1610: /*************************free ma3x ************************/
1611: void free_ma3x(double ***m, long nrl, long nrh, long ncl, long nch,long nll, long nlh)
1612: {
1613: free((FREE_ARG)(m[nrl][ncl]+ nll-NR_END));
1614: free((FREE_ARG)(m[nrl]+ncl-NR_END));
1615: free((FREE_ARG)(m+nrl-NR_END));
1616: }
1617:
1618: /*************** function subdirf ***********/
1619: char *subdirf(char fileres[])
1620: {
1621: /* Caution optionfilefiname is hidden */
1622: strcpy(tmpout,optionfilefiname);
1623: strcat(tmpout,"/"); /* Add to the right */
1624: strcat(tmpout,fileres);
1625: return tmpout;
1626: }
1627:
1628: /*************** function subdirf2 ***********/
1629: char *subdirf2(char fileres[], char *preop)
1630: {
1631:
1632: /* Caution optionfilefiname is hidden */
1633: strcpy(tmpout,optionfilefiname);
1634: strcat(tmpout,"/");
1635: strcat(tmpout,preop);
1636: strcat(tmpout,fileres);
1637: return tmpout;
1638: }
1639:
1640: /*************** function subdirf3 ***********/
1641: char *subdirf3(char fileres[], char *preop, char *preop2)
1642: {
1643:
1644: /* Caution optionfilefiname is hidden */
1645: strcpy(tmpout,optionfilefiname);
1646: strcat(tmpout,"/");
1647: strcat(tmpout,preop);
1648: strcat(tmpout,preop2);
1649: strcat(tmpout,fileres);
1650: return tmpout;
1651: }
1.213 brouard 1652:
1653: /*************** function subdirfext ***********/
1654: char *subdirfext(char fileres[], char *preop, char *postop)
1655: {
1656:
1657: strcpy(tmpout,preop);
1658: strcat(tmpout,fileres);
1659: strcat(tmpout,postop);
1660: return tmpout;
1661: }
1.126 brouard 1662:
1.213 brouard 1663: /*************** function subdirfext3 ***********/
1664: char *subdirfext3(char fileres[], char *preop, char *postop)
1665: {
1666:
1667: /* Caution optionfilefiname is hidden */
1668: strcpy(tmpout,optionfilefiname);
1669: strcat(tmpout,"/");
1670: strcat(tmpout,preop);
1671: strcat(tmpout,fileres);
1672: strcat(tmpout,postop);
1673: return tmpout;
1674: }
1675:
1.162 brouard 1676: char *asc_diff_time(long time_sec, char ascdiff[])
1677: {
1678: long sec_left, days, hours, minutes;
1679: days = (time_sec) / (60*60*24);
1680: sec_left = (time_sec) % (60*60*24);
1681: hours = (sec_left) / (60*60) ;
1682: sec_left = (sec_left) %(60*60);
1683: minutes = (sec_left) /60;
1684: sec_left = (sec_left) % (60);
1685: sprintf(ascdiff,"%ld day(s) %ld hour(s) %ld minute(s) %ld second(s)",days, hours, minutes, sec_left);
1686: return ascdiff;
1687: }
1688:
1.126 brouard 1689: /***************** f1dim *************************/
1690: extern int ncom;
1691: extern double *pcom,*xicom;
1692: extern double (*nrfunc)(double []);
1693:
1694: double f1dim(double x)
1695: {
1696: int j;
1697: double f;
1698: double *xt;
1699:
1700: xt=vector(1,ncom);
1701: for (j=1;j<=ncom;j++) xt[j]=pcom[j]+x*xicom[j];
1702: f=(*nrfunc)(xt);
1703: free_vector(xt,1,ncom);
1704: return f;
1705: }
1706:
1707: /*****************brent *************************/
1708: double brent(double ax, double bx, double cx, double (*f)(double), double tol, double *xmin)
1.187 brouard 1709: {
1710: /* Given a function f, and given a bracketing triplet of abscissas ax, bx, cx (such that bx is
1711: * between ax and cx, and f(bx) is less than both f(ax) and f(cx) ), this routine isolates
1712: * the minimum to a fractional precision of about tol using Brent’s method. The abscissa of
1713: * the minimum is returned as xmin, and the minimum function value is returned as brent , the
1714: * returned function value.
1715: */
1.126 brouard 1716: int iter;
1717: double a,b,d,etemp;
1.159 brouard 1718: double fu=0,fv,fw,fx;
1.164 brouard 1719: double ftemp=0.;
1.126 brouard 1720: double p,q,r,tol1,tol2,u,v,w,x,xm;
1721: double e=0.0;
1722:
1723: a=(ax < cx ? ax : cx);
1724: b=(ax > cx ? ax : cx);
1725: x=w=v=bx;
1726: fw=fv=fx=(*f)(x);
1727: for (iter=1;iter<=ITMAX;iter++) {
1728: xm=0.5*(a+b);
1729: tol2=2.0*(tol1=tol*fabs(x)+ZEPS);
1730: /* if (2.0*fabs(fp-(*fret)) <= ftol*(fabs(fp)+fabs(*fret)))*/
1731: printf(".");fflush(stdout);
1732: fprintf(ficlog,".");fflush(ficlog);
1.162 brouard 1733: #ifdef DEBUGBRENT
1.126 brouard 1734: 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);
1735: 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);
1736: /* if ((fabs(x-xm) <= (tol2-0.5*(b-a)))||(2.0*fabs(fu-ftemp) <= ftol*1.e-2*(fabs(fu)+fabs(ftemp)))) { */
1737: #endif
1738: if (fabs(x-xm) <= (tol2-0.5*(b-a))){
1739: *xmin=x;
1740: return fx;
1741: }
1742: ftemp=fu;
1743: if (fabs(e) > tol1) {
1744: r=(x-w)*(fx-fv);
1745: q=(x-v)*(fx-fw);
1746: p=(x-v)*q-(x-w)*r;
1747: q=2.0*(q-r);
1748: if (q > 0.0) p = -p;
1749: q=fabs(q);
1750: etemp=e;
1751: e=d;
1752: if (fabs(p) >= fabs(0.5*q*etemp) || p <= q*(a-x) || p >= q*(b-x))
1.224 brouard 1753: d=CGOLD*(e=(x >= xm ? a-x : b-x));
1.126 brouard 1754: else {
1.224 brouard 1755: d=p/q;
1756: u=x+d;
1757: if (u-a < tol2 || b-u < tol2)
1758: d=SIGN(tol1,xm-x);
1.126 brouard 1759: }
1760: } else {
1761: d=CGOLD*(e=(x >= xm ? a-x : b-x));
1762: }
1763: u=(fabs(d) >= tol1 ? x+d : x+SIGN(tol1,d));
1764: fu=(*f)(u);
1765: if (fu <= fx) {
1766: if (u >= x) a=x; else b=x;
1767: SHFT(v,w,x,u)
1.183 brouard 1768: SHFT(fv,fw,fx,fu)
1769: } else {
1770: if (u < x) a=u; else b=u;
1771: if (fu <= fw || w == x) {
1.224 brouard 1772: v=w;
1773: w=u;
1774: fv=fw;
1775: fw=fu;
1.183 brouard 1776: } else if (fu <= fv || v == x || v == w) {
1.224 brouard 1777: v=u;
1778: fv=fu;
1.183 brouard 1779: }
1780: }
1.126 brouard 1781: }
1782: nrerror("Too many iterations in brent");
1783: *xmin=x;
1784: return fx;
1785: }
1786:
1787: /****************** mnbrak ***********************/
1788:
1789: void mnbrak(double *ax, double *bx, double *cx, double *fa, double *fb, double *fc,
1790: double (*func)(double))
1.183 brouard 1791: { /* Given a function func , and given distinct initial points ax and bx , this routine searches in
1792: the downhill direction (defined by the function as evaluated at the initial points) and returns
1793: new points ax , bx , cx that bracket a minimum of the function. Also returned are the function
1794: values at the three points, fa, fb , and fc such that fa > fb and fb < fc.
1795: */
1.126 brouard 1796: double ulim,u,r,q, dum;
1797: double fu;
1.187 brouard 1798:
1799: double scale=10.;
1800: int iterscale=0;
1801:
1802: *fa=(*func)(*ax); /* xta[j]=pcom[j]+(*ax)*xicom[j]; fa=f(xta[j])*/
1803: *fb=(*func)(*bx); /* xtb[j]=pcom[j]+(*bx)*xicom[j]; fb=f(xtb[j]) */
1804:
1805:
1806: /* while(*fb != *fb){ /\* *ax should be ok, reducing distance to *ax *\/ */
1807: /* printf("Warning mnbrak *fb = %lf, *bx=%lf *ax=%lf *fa==%lf iter=%d\n",*fb, *bx, *ax, *fa, iterscale++); */
1808: /* *bx = *ax - (*ax - *bx)/scale; */
1809: /* *fb=(*func)(*bx); /\* xtb[j]=pcom[j]+(*bx)*xicom[j]; fb=f(xtb[j]) *\/ */
1810: /* } */
1811:
1.126 brouard 1812: if (*fb > *fa) {
1813: SHFT(dum,*ax,*bx,dum)
1.183 brouard 1814: SHFT(dum,*fb,*fa,dum)
1815: }
1.126 brouard 1816: *cx=(*bx)+GOLD*(*bx-*ax);
1817: *fc=(*func)(*cx);
1.183 brouard 1818: #ifdef DEBUG
1.224 brouard 1819: printf("mnbrak0 a=%lf *fa=%lf, b=%lf *fb=%lf, c=%lf *fc=%lf\n",*ax,*fa,*bx,*fb,*cx, *fc);
1820: 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 1821: #endif
1.224 brouard 1822: 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 1823: r=(*bx-*ax)*(*fb-*fc);
1.224 brouard 1824: q=(*bx-*cx)*(*fb-*fa); /* What if fa=inf */
1.126 brouard 1825: u=(*bx)-((*bx-*cx)*q-(*bx-*ax)*r)/
1.183 brouard 1826: (2.0*SIGN(FMAX(fabs(q-r),TINY),q-r)); /* Minimum abscissa of a parabolic estimated from (a,fa), (b,fb) and (c,fc). */
1827: ulim=(*bx)+GLIMIT*(*cx-*bx); /* Maximum abscissa where function should be evaluated */
1828: if ((*bx-u)*(u-*cx) > 0.0) { /* if u_p is between b and c */
1.126 brouard 1829: fu=(*func)(u);
1.163 brouard 1830: #ifdef DEBUG
1831: /* f(x)=A(x-u)**2+f(u) */
1832: double A, fparabu;
1833: A= (*fb - *fa)/(*bx-*ax)/(*bx+*ax-2*u);
1834: fparabu= *fa - A*(*ax-u)*(*ax-u);
1.224 brouard 1835: 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);
1836: 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 1837: /* And thus,it can be that fu > *fc even if fparabu < *fc */
1838: /* mnbrak (*ax=7.666299858533, *fa=299039.693133272231), (*bx=8.595447774979, *fb=298976.598289369489),
1839: (*cx=10.098840694817, *fc=298946.631474258087), (*u=9.852501168332, fu=298948.773013752128, fparabu=298945.434711494134) */
1840: /* In that case, there is no bracket in the output! Routine is wrong with many consequences.*/
1.163 brouard 1841: #endif
1.184 brouard 1842: #ifdef MNBRAKORIGINAL
1.183 brouard 1843: #else
1.191 brouard 1844: /* if (fu > *fc) { */
1845: /* #ifdef DEBUG */
1846: /* printf("mnbrak4 fu > fc \n"); */
1847: /* fprintf(ficlog, "mnbrak4 fu > fc\n"); */
1848: /* #endif */
1849: /* /\* 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 *\\/ *\/ */
1850: /* /\* SHFT(*fa,*fc,fu,*fc) /\\* (b, u, c) is a bracket while test fb > fc will be fu > fc will exit *\\/ *\/ */
1851: /* dum=u; /\* Shifting c and u *\/ */
1852: /* u = *cx; */
1853: /* *cx = dum; */
1854: /* dum = fu; */
1855: /* fu = *fc; */
1856: /* *fc =dum; */
1857: /* } else { /\* end *\/ */
1858: /* #ifdef DEBUG */
1859: /* printf("mnbrak3 fu < fc \n"); */
1860: /* fprintf(ficlog, "mnbrak3 fu < fc\n"); */
1861: /* #endif */
1862: /* dum=u; /\* Shifting c and u *\/ */
1863: /* u = *cx; */
1864: /* *cx = dum; */
1865: /* dum = fu; */
1866: /* fu = *fc; */
1867: /* *fc =dum; */
1868: /* } */
1.224 brouard 1869: #ifdef DEBUGMNBRAK
1870: double A, fparabu;
1871: A= (*fb - *fa)/(*bx-*ax)/(*bx+*ax-2*u);
1872: fparabu= *fa - A*(*ax-u)*(*ax-u);
1873: 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);
1874: 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 1875: #endif
1.191 brouard 1876: dum=u; /* Shifting c and u */
1877: u = *cx;
1878: *cx = dum;
1879: dum = fu;
1880: fu = *fc;
1881: *fc =dum;
1.183 brouard 1882: #endif
1.162 brouard 1883: } else if ((*cx-u)*(u-ulim) > 0.0) { /* u is after c but before ulim */
1.183 brouard 1884: #ifdef DEBUG
1.224 brouard 1885: printf("\nmnbrak2 u=%lf after c=%lf but before ulim\n",u,*cx);
1886: fprintf(ficlog,"\nmnbrak2 u=%lf after c=%lf but before ulim\n",u,*cx);
1.183 brouard 1887: #endif
1.126 brouard 1888: fu=(*func)(u);
1889: if (fu < *fc) {
1.183 brouard 1890: #ifdef DEBUG
1.224 brouard 1891: printf("\nmnbrak2 u=%lf after c=%lf but before ulim=%lf AND fu=%lf < %lf=fc\n",u,*cx,ulim,fu, *fc);
1892: fprintf(ficlog,"\nmnbrak2 u=%lf after c=%lf but before ulim=%lf AND fu=%lf < %lf=fc\n",u,*cx,ulim,fu, *fc);
1893: #endif
1894: SHFT(*bx,*cx,u,*cx+GOLD*(*cx-*bx))
1895: SHFT(*fb,*fc,fu,(*func)(u))
1896: #ifdef DEBUG
1897: printf("\nmnbrak2 shift GOLD c=%lf",*cx+GOLD*(*cx-*bx));
1.183 brouard 1898: #endif
1899: }
1.162 brouard 1900: } else if ((u-ulim)*(ulim-*cx) >= 0.0) { /* u outside ulim (verifying that ulim is beyond c) */
1.183 brouard 1901: #ifdef DEBUG
1.224 brouard 1902: printf("\nmnbrak2 u=%lf outside ulim=%lf (verifying that ulim is beyond c=%lf)\n",u,ulim,*cx);
1903: fprintf(ficlog,"\nmnbrak2 u=%lf outside ulim=%lf (verifying that ulim is beyond c=%lf)\n",u,ulim,*cx);
1.183 brouard 1904: #endif
1.126 brouard 1905: u=ulim;
1906: fu=(*func)(u);
1.183 brouard 1907: } else { /* u could be left to b (if r > q parabola has a maximum) */
1908: #ifdef DEBUG
1.224 brouard 1909: printf("\nmnbrak2 u=%lf could be left to b=%lf (if r=%lf > q=%lf parabola has a maximum)\n",u,*bx,r,q);
1910: 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 1911: #endif
1.126 brouard 1912: u=(*cx)+GOLD*(*cx-*bx);
1913: fu=(*func)(u);
1.224 brouard 1914: #ifdef DEBUG
1915: printf("\nmnbrak2 new u=%lf fu=%lf shifted gold left from c=%lf and b=%lf \n",u,fu,*cx,*bx);
1916: fprintf(ficlog,"\nmnbrak2 new u=%lf fu=%lf shifted gold left from c=%lf and b=%lf \n",u,fu,*cx,*bx);
1917: #endif
1.183 brouard 1918: } /* end tests */
1.126 brouard 1919: SHFT(*ax,*bx,*cx,u)
1.183 brouard 1920: SHFT(*fa,*fb,*fc,fu)
1921: #ifdef DEBUG
1.224 brouard 1922: printf("\nmnbrak2 shift (*ax=%.12f, *fa=%.12lf), (*bx=%.12f, *fb=%.12lf), (*cx=%.12f, *fc=%.12lf)\n",*ax,*fa,*bx,*fb,*cx,*fc);
1923: 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 1924: #endif
1925: } /* 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 1926: }
1927:
1928: /*************** linmin ************************/
1.162 brouard 1929: /* Given an n -dimensional point p[1..n] and an n -dimensional direction xi[1..n] , moves and
1930: resets p to where the function func(p) takes on a minimum along the direction xi from p ,
1931: and replaces xi by the actual vector displacement that p was moved. Also returns as fret
1932: the value of func at the returned location p . This is actually all accomplished by calling the
1933: routines mnbrak and brent .*/
1.126 brouard 1934: int ncom;
1935: double *pcom,*xicom;
1936: double (*nrfunc)(double []);
1937:
1.224 brouard 1938: #ifdef LINMINORIGINAL
1.126 brouard 1939: void linmin(double p[], double xi[], int n, double *fret,double (*func)(double []))
1.224 brouard 1940: #else
1941: void linmin(double p[], double xi[], int n, double *fret,double (*func)(double []), int *flat)
1942: #endif
1.126 brouard 1943: {
1944: double brent(double ax, double bx, double cx,
1945: double (*f)(double), double tol, double *xmin);
1946: double f1dim(double x);
1947: void mnbrak(double *ax, double *bx, double *cx, double *fa, double *fb,
1948: double *fc, double (*func)(double));
1949: int j;
1950: double xx,xmin,bx,ax;
1951: double fx,fb,fa;
1.187 brouard 1952:
1.203 brouard 1953: #ifdef LINMINORIGINAL
1954: #else
1955: double scale=10., axs, xxs; /* Scale added for infinity */
1956: #endif
1957:
1.126 brouard 1958: ncom=n;
1959: pcom=vector(1,n);
1960: xicom=vector(1,n);
1961: nrfunc=func;
1962: for (j=1;j<=n;j++) {
1963: pcom[j]=p[j];
1.202 brouard 1964: xicom[j]=xi[j]; /* Former scale xi[j] of currrent direction i */
1.126 brouard 1965: }
1.187 brouard 1966:
1.203 brouard 1967: #ifdef LINMINORIGINAL
1968: xx=1.;
1969: #else
1970: axs=0.0;
1971: xxs=1.;
1972: do{
1973: xx= xxs;
1974: #endif
1.187 brouard 1975: ax=0.;
1976: mnbrak(&ax,&xx,&bx,&fa,&fx,&fb,f1dim); /* Outputs: xtx[j]=pcom[j]+(*xx)*xicom[j]; fx=f(xtx[j]) */
1977: /* brackets with inputs ax=0 and xx=1, but points, pcom=p, and directions values, xicom=xi, are sent via f1dim(x) */
1978: /* 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)) */
1979: /* Outputs: fa=f(p(j)) and fx=f(p(j) + xxs * xi(j) ) and f(bx)= f(p(j)+ bx* xi(j)) */
1980: /* Given input ax=axs and xx=xxs, xx might be too far from ax to get a finite f(xx) */
1981: /* Searches on line, outputs (ax, xx, bx) such that fx < min(fa and fb) */
1982: /* 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 1983: #ifdef LINMINORIGINAL
1984: #else
1985: if (fx != fx){
1.224 brouard 1986: xxs=xxs/scale; /* Trying a smaller xx, closer to initial ax=0 */
1987: printf("|");
1988: fprintf(ficlog,"|");
1.203 brouard 1989: #ifdef DEBUGLINMIN
1.224 brouard 1990: 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 1991: #endif
1992: }
1.224 brouard 1993: }while(fx != fx && xxs > 1.e-5);
1.203 brouard 1994: #endif
1995:
1.191 brouard 1996: #ifdef DEBUGLINMIN
1997: 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 1998: 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 1999: #endif
1.224 brouard 2000: #ifdef LINMINORIGINAL
2001: #else
2002: if(fb == fx){ /* Flat function in the direction */
2003: xmin=xx;
2004: *flat=1;
2005: }else{
2006: *flat=0;
2007: #endif
2008: /*Flat mnbrak2 shift (*ax=0.000000000000, *fa=51626.272983130431), (*bx=-1.618034000000, *fb=51590.149499362531), (*cx=-4.236068025156, *fc=51590.149499362531) */
1.187 brouard 2009: *fret=brent(ax,xx,bx,f1dim,TOL,&xmin); /* Giving a bracketting triplet (ax, xx, bx), find a minimum, xmin, according to f1dim, *fret(xmin),*/
2010: /* fa = f(p[j] + ax * xi[j]), fx = f(p[j] + xx * xi[j]), fb = f(p[j] + bx * xi[j]) */
2011: /* fmin = f(p[j] + xmin * xi[j]) */
2012: /* P+lambda n in that direction (lambdamin), with TOL between abscisses */
2013: /* f1dim(xmin): for (j=1;j<=ncom;j++) xt[j]=pcom[j]+xmin*xicom[j]; */
1.126 brouard 2014: #ifdef DEBUG
1.224 brouard 2015: 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);
2016: 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);
2017: #endif
2018: #ifdef LINMINORIGINAL
2019: #else
2020: }
1.126 brouard 2021: #endif
1.191 brouard 2022: #ifdef DEBUGLINMIN
2023: printf("linmin end ");
1.202 brouard 2024: fprintf(ficlog,"linmin end ");
1.191 brouard 2025: #endif
1.126 brouard 2026: for (j=1;j<=n;j++) {
1.203 brouard 2027: #ifdef LINMINORIGINAL
2028: xi[j] *= xmin;
2029: #else
2030: #ifdef DEBUGLINMIN
2031: if(xxs <1.0)
2032: printf(" before xi[%d]=%12.8f", j,xi[j]);
2033: #endif
2034: 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) */
2035: #ifdef DEBUGLINMIN
2036: if(xxs <1.0)
2037: 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 );
2038: #endif
2039: #endif
1.187 brouard 2040: p[j] += xi[j]; /* Parameters values are updated accordingly */
1.126 brouard 2041: }
1.191 brouard 2042: #ifdef DEBUGLINMIN
1.203 brouard 2043: printf("\n");
1.191 brouard 2044: printf("Comparing last *frec(xmin=%12.8f)=%12.8f from Brent and frec(0.)=%12.8f \n", xmin, *fret, (*func)(p));
1.202 brouard 2045: 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 2046: for (j=1;j<=n;j++) {
1.202 brouard 2047: printf(" xi[%d]= %14.10f p[%d]= %12.7f",j,xi[j],j,p[j]);
2048: fprintf(ficlog," xi[%d]= %14.10f p[%d]= %12.7f",j,xi[j],j,p[j]);
2049: if(j % ncovmodel == 0){
1.191 brouard 2050: printf("\n");
1.202 brouard 2051: fprintf(ficlog,"\n");
2052: }
1.191 brouard 2053: }
1.203 brouard 2054: #else
1.191 brouard 2055: #endif
1.126 brouard 2056: free_vector(xicom,1,n);
2057: free_vector(pcom,1,n);
2058: }
2059:
2060:
2061: /*************** powell ************************/
1.162 brouard 2062: /*
2063: Minimization of a function func of n variables. Input consists of an initial starting point
2064: p[1..n] ; an initial matrix xi[1..n][1..n] , whose columns contain the initial set of di-
2065: rections (usually the n unit vectors); and ftol , the fractional tolerance in the function value
2066: such that failure to decrease by more than this amount on one iteration signals doneness. On
2067: output, p is set to the best point found, xi is the then-current direction set, fret is the returned
2068: function value at p , and iter is the number of iterations taken. The routine linmin is used.
2069: */
1.224 brouard 2070: #ifdef LINMINORIGINAL
2071: #else
2072: int *flatdir; /* Function is vanishing in that direction */
1.225 brouard 2073: int flat=0, flatd=0; /* Function is vanishing in that direction */
1.224 brouard 2074: #endif
1.126 brouard 2075: void powell(double p[], double **xi, int n, double ftol, int *iter, double *fret,
2076: double (*func)(double []))
2077: {
1.224 brouard 2078: #ifdef LINMINORIGINAL
2079: void linmin(double p[], double xi[], int n, double *fret,
1.126 brouard 2080: double (*func)(double []));
1.224 brouard 2081: #else
1.241 brouard 2082: void linmin(double p[], double xi[], int n, double *fret,
2083: double (*func)(double []),int *flat);
1.224 brouard 2084: #endif
1.239 brouard 2085: int i,ibig,j,jk,k;
1.126 brouard 2086: double del,t,*pt,*ptt,*xit;
1.181 brouard 2087: double directest;
1.126 brouard 2088: double fp,fptt;
2089: double *xits;
2090: int niterf, itmp;
1.224 brouard 2091: #ifdef LINMINORIGINAL
2092: #else
2093:
2094: flatdir=ivector(1,n);
2095: for (j=1;j<=n;j++) flatdir[j]=0;
2096: #endif
1.126 brouard 2097:
2098: pt=vector(1,n);
2099: ptt=vector(1,n);
2100: xit=vector(1,n);
2101: xits=vector(1,n);
2102: *fret=(*func)(p);
2103: for (j=1;j<=n;j++) pt[j]=p[j];
1.202 brouard 2104: rcurr_time = time(NULL);
1.126 brouard 2105: for (*iter=1;;++(*iter)) {
1.187 brouard 2106: fp=(*fret); /* From former iteration or initial value */
1.126 brouard 2107: ibig=0;
2108: del=0.0;
1.157 brouard 2109: rlast_time=rcurr_time;
2110: /* (void) gettimeofday(&curr_time,&tzp); */
2111: rcurr_time = time(NULL);
2112: curr_time = *localtime(&rcurr_time);
2113: printf("\nPowell iter=%d -2*LL=%.12f %ld sec. %ld sec.",*iter,*fret, rcurr_time-rlast_time, rcurr_time-rstart_time);fflush(stdout);
2114: fprintf(ficlog,"\nPowell iter=%d -2*LL=%.12f %ld sec. %ld sec.",*iter,*fret,rcurr_time-rlast_time, rcurr_time-rstart_time); fflush(ficlog);
2115: /* fprintf(ficrespow,"%d %.12f %ld",*iter,*fret,curr_time.tm_sec-start_time.tm_sec); */
1.192 brouard 2116: for (i=1;i<=n;i++) {
1.126 brouard 2117: fprintf(ficrespow," %.12lf", p[i]);
2118: }
1.239 brouard 2119: fprintf(ficrespow,"\n");fflush(ficrespow);
2120: printf("\n#model= 1 + age ");
2121: fprintf(ficlog,"\n#model= 1 + age ");
2122: if(nagesqr==1){
1.241 brouard 2123: printf(" + age*age ");
2124: fprintf(ficlog," + age*age ");
1.239 brouard 2125: }
2126: for(j=1;j <=ncovmodel-2;j++){
2127: if(Typevar[j]==0) {
2128: printf(" + V%d ",Tvar[j]);
2129: fprintf(ficlog," + V%d ",Tvar[j]);
2130: }else if(Typevar[j]==1) {
2131: printf(" + V%d*age ",Tvar[j]);
2132: fprintf(ficlog," + V%d*age ",Tvar[j]);
2133: }else if(Typevar[j]==2) {
2134: printf(" + V%d*V%d ",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
2135: fprintf(ficlog," + V%d*V%d ",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
2136: }
2137: }
1.126 brouard 2138: printf("\n");
1.239 brouard 2139: /* printf("12 47.0114589 0.0154322 33.2424412 0.3279905 2.3731903 */
2140: /* 13 -21.5392400 0.1118147 1.2680506 1.2973408 -1.0663662 */
1.126 brouard 2141: fprintf(ficlog,"\n");
1.239 brouard 2142: for(i=1,jk=1; i <=nlstate; i++){
2143: for(k=1; k <=(nlstate+ndeath); k++){
2144: if (k != i) {
2145: printf("%d%d ",i,k);
2146: fprintf(ficlog,"%d%d ",i,k);
2147: for(j=1; j <=ncovmodel; j++){
2148: printf("%12.7f ",p[jk]);
2149: fprintf(ficlog,"%12.7f ",p[jk]);
2150: jk++;
2151: }
2152: printf("\n");
2153: fprintf(ficlog,"\n");
2154: }
2155: }
2156: }
1.241 brouard 2157: if(*iter <=3 && *iter >1){
1.157 brouard 2158: tml = *localtime(&rcurr_time);
2159: strcpy(strcurr,asctime(&tml));
2160: rforecast_time=rcurr_time;
1.126 brouard 2161: itmp = strlen(strcurr);
2162: if(strcurr[itmp-1]=='\n') /* Windows outputs with a new line */
1.241 brouard 2163: strcurr[itmp-1]='\0';
1.162 brouard 2164: printf("\nConsidering the time needed for the last iteration #%d: %ld seconds,\n",*iter,rcurr_time-rlast_time);
1.157 brouard 2165: fprintf(ficlog,"\nConsidering the time needed for this last iteration #%d: %ld seconds,\n",*iter,rcurr_time-rlast_time);
1.126 brouard 2166: for(niterf=10;niterf<=30;niterf+=10){
1.241 brouard 2167: rforecast_time=rcurr_time+(niterf-*iter)*(rcurr_time-rlast_time);
2168: forecast_time = *localtime(&rforecast_time);
2169: strcpy(strfor,asctime(&forecast_time));
2170: itmp = strlen(strfor);
2171: if(strfor[itmp-1]=='\n')
2172: strfor[itmp-1]='\0';
2173: 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);
2174: 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 2175: }
2176: }
1.187 brouard 2177: for (i=1;i<=n;i++) { /* For each direction i */
2178: for (j=1;j<=n;j++) xit[j]=xi[j][i]; /* Directions stored from previous iteration with previous scales */
1.126 brouard 2179: fptt=(*fret);
2180: #ifdef DEBUG
1.203 brouard 2181: printf("fret=%lf, %lf, %lf \n", *fret, *fret, *fret);
2182: fprintf(ficlog, "fret=%lf, %lf, %lf \n", *fret, *fret, *fret);
1.126 brouard 2183: #endif
1.203 brouard 2184: printf("%d",i);fflush(stdout); /* print direction (parameter) i */
1.126 brouard 2185: fprintf(ficlog,"%d",i);fflush(ficlog);
1.224 brouard 2186: #ifdef LINMINORIGINAL
1.188 brouard 2187: linmin(p,xit,n,fret,func); /* Point p[n]. xit[n] has been loaded for direction i as input.*/
1.224 brouard 2188: #else
2189: linmin(p,xit,n,fret,func,&flat); /* Point p[n]. xit[n] has been loaded for direction i as input.*/
2190: flatdir[i]=flat; /* Function is vanishing in that direction i */
2191: #endif
2192: /* Outputs are fret(new point p) p is updated and xit rescaled */
1.188 brouard 2193: if (fabs(fptt-(*fret)) > del) { /* We are keeping the max gain on each of the n directions */
1.224 brouard 2194: /* because that direction will be replaced unless the gain del is small */
2195: /* in comparison with the 'probable' gain, mu^2, with the last average direction. */
2196: /* Unless the n directions are conjugate some gain in the determinant may be obtained */
2197: /* with the new direction. */
2198: del=fabs(fptt-(*fret));
2199: ibig=i;
1.126 brouard 2200: }
2201: #ifdef DEBUG
2202: printf("%d %.12e",i,(*fret));
2203: fprintf(ficlog,"%d %.12e",i,(*fret));
2204: for (j=1;j<=n;j++) {
1.224 brouard 2205: xits[j]=FMAX(fabs(p[j]-pt[j]),1.e-5);
2206: printf(" x(%d)=%.12e",j,xit[j]);
2207: fprintf(ficlog," x(%d)=%.12e",j,xit[j]);
1.126 brouard 2208: }
2209: for(j=1;j<=n;j++) {
1.225 brouard 2210: printf(" p(%d)=%.12e",j,p[j]);
2211: fprintf(ficlog," p(%d)=%.12e",j,p[j]);
1.126 brouard 2212: }
2213: printf("\n");
2214: fprintf(ficlog,"\n");
2215: #endif
1.187 brouard 2216: } /* end loop on each direction i */
2217: /* Convergence test will use last linmin estimation (fret) and compare former iteration (fp) */
1.188 brouard 2218: /* But p and xit have been updated at the end of linmin, *fret corresponds to new p, xit */
1.187 brouard 2219: /* New value of last point Pn is not computed, P(n-1) */
1.224 brouard 2220: for(j=1;j<=n;j++) {
1.225 brouard 2221: if(flatdir[j] >0){
2222: printf(" p(%d)=%lf flat=%d ",j,p[j],flatdir[j]);
2223: fprintf(ficlog," p(%d)=%lf flat=%d ",j,p[j],flatdir[j]);
2224: }
2225: /* printf("\n"); */
2226: /* fprintf(ficlog,"\n"); */
2227: }
1.243 brouard 2228: /* if (2.0*fabs(fp-(*fret)) <= ftol*(fabs(fp)+fabs(*fret))) { /\* Did we reach enough precision? *\/ */
2229: if (2.0*fabs(fp-(*fret)) <= ftol) { /* Did we reach enough precision? */
1.188 brouard 2230: /* We could compare with a chi^2. chisquare(0.95,ddl=1)=3.84 */
2231: /* By adding age*age in a model, the new -2LL should be lower and the difference follows a */
2232: /* a chisquare statistics with 1 degree. To be significant at the 95% level, it should have */
2233: /* decreased of more than 3.84 */
2234: /* By adding age*age and V1*age the gain (-2LL) should be more than 5.99 (ddl=2) */
2235: /* By using V1+V2+V3, the gain should be 7.82, compared with basic 1+age. */
2236: /* By adding 10 parameters more the gain should be 18.31 */
1.224 brouard 2237:
1.188 brouard 2238: /* Starting the program with initial values given by a former maximization will simply change */
2239: /* the scales of the directions and the directions, because the are reset to canonical directions */
2240: /* Thus the first calls to linmin will give new points and better maximizations until fp-(*fret) is */
2241: /* under the tolerance value. If the tolerance is very small 1.e-9, it could last long. */
1.126 brouard 2242: #ifdef DEBUG
2243: int k[2],l;
2244: k[0]=1;
2245: k[1]=-1;
2246: printf("Max: %.12e",(*func)(p));
2247: fprintf(ficlog,"Max: %.12e",(*func)(p));
2248: for (j=1;j<=n;j++) {
2249: printf(" %.12e",p[j]);
2250: fprintf(ficlog," %.12e",p[j]);
2251: }
2252: printf("\n");
2253: fprintf(ficlog,"\n");
2254: for(l=0;l<=1;l++) {
2255: for (j=1;j<=n;j++) {
2256: ptt[j]=p[j]+(p[j]-pt[j])*k[l];
2257: printf("l=%d j=%d ptt=%.12e, xits=%.12e, p=%.12e, xit=%.12e", l,j,ptt[j],xits[j],p[j],xit[j]);
2258: fprintf(ficlog,"l=%d j=%d ptt=%.12e, xits=%.12e, p=%.12e, xit=%.12e", l,j,ptt[j],xits[j],p[j],xit[j]);
2259: }
2260: printf("func(ptt)=%.12e, deriv=%.12e\n",(*func)(ptt),(ptt[j]-p[j])/((*func)(ptt)-(*func)(p)));
2261: fprintf(ficlog,"func(ptt)=%.12e, deriv=%.12e\n",(*func)(ptt),(ptt[j]-p[j])/((*func)(ptt)-(*func)(p)));
2262: }
2263: #endif
2264:
1.224 brouard 2265: #ifdef LINMINORIGINAL
2266: #else
2267: free_ivector(flatdir,1,n);
2268: #endif
1.126 brouard 2269: free_vector(xit,1,n);
2270: free_vector(xits,1,n);
2271: free_vector(ptt,1,n);
2272: free_vector(pt,1,n);
2273: return;
1.192 brouard 2274: } /* enough precision */
1.240 brouard 2275: if (*iter == ITMAX*n) nrerror("powell exceeding maximum iterations.");
1.181 brouard 2276: for (j=1;j<=n;j++) { /* Computes the extrapolated point P_0 + 2 (P_n-P_0) */
1.126 brouard 2277: ptt[j]=2.0*p[j]-pt[j];
2278: xit[j]=p[j]-pt[j];
2279: pt[j]=p[j];
2280: }
1.181 brouard 2281: fptt=(*func)(ptt); /* f_3 */
1.224 brouard 2282: #ifdef NODIRECTIONCHANGEDUNTILNITER /* No change in drections until some iterations are done */
2283: if (*iter <=4) {
1.225 brouard 2284: #else
2285: #endif
1.224 brouard 2286: #ifdef POWELLNOF3INFF1TEST /* skips test F3 <F1 */
1.192 brouard 2287: #else
1.161 brouard 2288: if (fptt < fp) { /* If extrapolated point is better, decide if we keep that new direction or not */
1.192 brouard 2289: #endif
1.162 brouard 2290: /* (x1 f1=fp), (x2 f2=*fret), (x3 f3=fptt), (xm fm) */
1.161 brouard 2291: /* From x1 (P0) distance of x2 is at h and x3 is 2h */
1.162 brouard 2292: /* Let f"(x2) be the 2nd derivative equal everywhere. */
2293: /* Then the parabolic through (x1,f1), (x2,f2) and (x3,f3) */
2294: /* will reach at f3 = fm + h^2/2 f"m ; f" = (f1 -2f2 +f3 ) / h**2 */
1.224 brouard 2295: /* Conditional for using this new direction is that mu^2 = (f1-2f2+f3)^2 /2 < del or directest <0 */
2296: /* also lamda^2=(f1-f2)^2/mu² is a parasite solution of powell */
2297: /* For powell, inclusion of this average direction is only if t(del)<0 or del inbetween mu^2 and lambda^2 */
1.161 brouard 2298: /* t=2.0*(fp-2.0*(*fret)+fptt)*SQR(fp-(*fret)-del)-del*SQR(fp-fptt); */
1.224 brouard 2299: /* Even if f3 <f1, directest can be negative and t >0 */
2300: /* mu² and del² are equal when f3=f1 */
2301: /* f3 < f1 : mu² < del <= lambda^2 both test are equivalent */
2302: /* f3 < f1 : mu² < lambda^2 < del then directtest is negative and powell t is positive */
2303: /* f3 > f1 : lambda² < mu^2 < del then t is negative and directest >0 */
2304: /* f3 > f1 : lambda² < del < mu^2 then t is positive and directest >0 */
1.183 brouard 2305: #ifdef NRCORIGINAL
2306: t=2.0*(fp-2.0*(*fret)+fptt)*SQR(fp-(*fret)-del)- del*SQR(fp-fptt); /* Original Numerical Recipes in C*/
2307: #else
2308: 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 2309: t= t- del*SQR(fp-fptt);
1.183 brouard 2310: #endif
1.202 brouard 2311: directest = fp-2.0*(*fret)+fptt - 2.0 * del; /* If delta was big enough we change it for a new direction */
1.161 brouard 2312: #ifdef DEBUG
1.181 brouard 2313: 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);
2314: 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 2315: printf("t3= %.12lf, t4= %.12lf, t3*= %.12lf, t4*= %.12lf\n",SQR(fp-(*fret)-del),SQR(fp-fptt),
2316: (fp-(*fret)-del)*(fp-(*fret)-del),(fp-fptt)*(fp-fptt));
2317: fprintf(ficlog,"t3= %.12lf, t4= %.12lf, t3*= %.12lf, t4*= %.12lf\n",SQR(fp-(*fret)-del),SQR(fp-fptt),
2318: (fp-(*fret)-del)*(fp-(*fret)-del),(fp-fptt)*(fp-fptt));
2319: 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);
2320: 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);
2321: #endif
1.183 brouard 2322: #ifdef POWELLORIGINAL
2323: if (t < 0.0) { /* Then we use it for new direction */
2324: #else
1.182 brouard 2325: if (directest*t < 0.0) { /* Contradiction between both tests */
1.224 brouard 2326: 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 2327: 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 2328: 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 2329: fprintf(ficlog,"f1-2f2+f3= %.12lf, f1-f2-del= %.12lf, f1-f3= %.12lf\n",fp-2.0*(*fret)+fptt, fp -(*fret) -del, fp-fptt);
2330: }
1.181 brouard 2331: if (directest < 0.0) { /* Then we use it for new direction */
2332: #endif
1.191 brouard 2333: #ifdef DEBUGLINMIN
1.234 brouard 2334: printf("Before linmin in direction P%d-P0\n",n);
2335: for (j=1;j<=n;j++) {
2336: printf(" Before xit[%d]= %12.7f p[%d]= %12.7f",j,xit[j],j,p[j]);
2337: fprintf(ficlog," Before xit[%d]= %12.7f p[%d]= %12.7f",j,xit[j],j,p[j]);
2338: if(j % ncovmodel == 0){
2339: printf("\n");
2340: fprintf(ficlog,"\n");
2341: }
2342: }
1.224 brouard 2343: #endif
2344: #ifdef LINMINORIGINAL
1.234 brouard 2345: linmin(p,xit,n,fret,func); /* computes minimum on the extrapolated direction: changes p and rescales xit.*/
1.224 brouard 2346: #else
1.234 brouard 2347: linmin(p,xit,n,fret,func,&flat); /* computes minimum on the extrapolated direction: changes p and rescales xit.*/
2348: flatdir[i]=flat; /* Function is vanishing in that direction i */
1.191 brouard 2349: #endif
1.234 brouard 2350:
1.191 brouard 2351: #ifdef DEBUGLINMIN
1.234 brouard 2352: for (j=1;j<=n;j++) {
2353: printf("After xit[%d]= %12.7f p[%d]= %12.7f",j,xit[j],j,p[j]);
2354: fprintf(ficlog,"After xit[%d]= %12.7f p[%d]= %12.7f",j,xit[j],j,p[j]);
2355: if(j % ncovmodel == 0){
2356: printf("\n");
2357: fprintf(ficlog,"\n");
2358: }
2359: }
1.224 brouard 2360: #endif
1.234 brouard 2361: for (j=1;j<=n;j++) {
2362: xi[j][ibig]=xi[j][n]; /* Replace direction with biggest decrease by last direction n */
2363: xi[j][n]=xit[j]; /* and this nth direction by the by the average p_0 p_n */
2364: }
1.224 brouard 2365: #ifdef LINMINORIGINAL
2366: #else
1.234 brouard 2367: for (j=1, flatd=0;j<=n;j++) {
2368: if(flatdir[j]>0)
2369: flatd++;
2370: }
2371: if(flatd >0){
2372: printf("%d flat directions\n",flatd);
2373: fprintf(ficlog,"%d flat directions\n",flatd);
2374: for (j=1;j<=n;j++) {
2375: if(flatdir[j]>0){
2376: printf("%d ",j);
2377: fprintf(ficlog,"%d ",j);
2378: }
2379: }
2380: printf("\n");
2381: fprintf(ficlog,"\n");
2382: }
1.191 brouard 2383: #endif
1.234 brouard 2384: printf("Gaining to use new average direction of P0 P%d instead of biggest increase direction %d :\n",n,ibig);
2385: fprintf(ficlog,"Gaining to use new average direction of P0 P%d instead of biggest increase direction %d :\n",n,ibig);
2386:
1.126 brouard 2387: #ifdef DEBUG
1.234 brouard 2388: printf("Direction changed last moved %d in place of ibig=%d, new last is the average:\n",n,ibig);
2389: fprintf(ficlog,"Direction changed last moved %d in place of ibig=%d, new last is the average:\n",n,ibig);
2390: for(j=1;j<=n;j++){
2391: printf(" %lf",xit[j]);
2392: fprintf(ficlog," %lf",xit[j]);
2393: }
2394: printf("\n");
2395: fprintf(ficlog,"\n");
1.126 brouard 2396: #endif
1.192 brouard 2397: } /* end of t or directest negative */
1.224 brouard 2398: #ifdef POWELLNOF3INFF1TEST
1.192 brouard 2399: #else
1.234 brouard 2400: } /* end if (fptt < fp) */
1.192 brouard 2401: #endif
1.225 brouard 2402: #ifdef NODIRECTIONCHANGEDUNTILNITER /* No change in drections until some iterations are done */
1.234 brouard 2403: } /*NODIRECTIONCHANGEDUNTILNITER No change in drections until some iterations are done */
1.225 brouard 2404: #else
1.224 brouard 2405: #endif
1.234 brouard 2406: } /* loop iteration */
1.126 brouard 2407: }
1.234 brouard 2408:
1.126 brouard 2409: /**** Prevalence limit (stable or period prevalence) ****************/
1.234 brouard 2410:
1.235 brouard 2411: 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 2412: {
1.235 brouard 2413: /* Computes the prevalence limit in each live state at age x and for covariate combination ij
2414: (and selected quantitative values in nres)
2415: by left multiplying the unit
1.234 brouard 2416: matrix by transitions matrix until convergence is reached with precision ftolpl */
1.206 brouard 2417: /* Wx= Wx-1 Px-1= Wx-2 Px-2 Px-1 = Wx-n Px-n ... Px-2 Px-1 I */
2418: /* Wx is row vector: population in state 1, population in state 2, population dead */
2419: /* or prevalence in state 1, prevalence in state 2, 0 */
2420: /* newm is the matrix after multiplications, its rows are identical at a factor */
2421: /* Initial matrix pimij */
2422: /* {0.85204250825084937, 0.13044499163996345, 0.017512500109187184, */
2423: /* 0.090851990222114765, 0.88271245433047185, 0.026435555447413338, */
2424: /* 0, 0 , 1} */
2425: /*
2426: * and after some iteration: */
2427: /* {0.45504275246439968, 0.42731458730878791, 0.11764266022681241, */
2428: /* 0.45201005341706885, 0.42865420071559901, 0.11933574586733192, */
2429: /* 0, 0 , 1} */
2430: /* And prevalence by suppressing the deaths are close to identical rows in prlim: */
2431: /* {0.51571254859325999, 0.4842874514067399, */
2432: /* 0.51326036147820708, 0.48673963852179264} */
2433: /* If we start from prlim again, prlim tends to a constant matrix */
1.234 brouard 2434:
1.126 brouard 2435: int i, ii,j,k;
1.209 brouard 2436: double *min, *max, *meandiff, maxmax,sumnew=0.;
1.145 brouard 2437: /* double **matprod2(); */ /* test */
1.218 brouard 2438: double **out, cov[NCOVMAX+1], **pmij(); /* **pmmij is a global variable feeded with oldms etc */
1.126 brouard 2439: double **newm;
1.209 brouard 2440: double agefin, delaymax=200. ; /* 100 Max number of years to converge */
1.203 brouard 2441: int ncvloop=0;
1.169 brouard 2442:
1.209 brouard 2443: min=vector(1,nlstate);
2444: max=vector(1,nlstate);
2445: meandiff=vector(1,nlstate);
2446:
1.218 brouard 2447: /* Starting with matrix unity */
1.126 brouard 2448: for (ii=1;ii<=nlstate+ndeath;ii++)
2449: for (j=1;j<=nlstate+ndeath;j++){
2450: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
2451: }
1.169 brouard 2452:
2453: cov[1]=1.;
2454:
2455: /* Even if hstepm = 1, at least one multiplication by the unit matrix */
1.202 brouard 2456: /* Start at agefin= age, computes the matrix of passage and loops decreasing agefin until convergence is reached */
1.126 brouard 2457: for(agefin=age-stepm/YEARM; agefin>=age-delaymax; agefin=agefin-stepm/YEARM){
1.202 brouard 2458: ncvloop++;
1.126 brouard 2459: newm=savm;
2460: /* Covariates have to be included here again */
1.138 brouard 2461: cov[2]=agefin;
1.187 brouard 2462: if(nagesqr==1)
2463: cov[3]= agefin*agefin;;
1.234 brouard 2464: for (k=1; k<=nsd;k++) { /* For single dummy covariates only */
2465: /* Here comes the value of the covariate 'ij' after renumbering k with single dummy covariates */
2466: cov[2+nagesqr+TvarsDind[k]]=nbcode[TvarsD[k]][codtabm(ij,k)];
1.235 brouard 2467: /* 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 2468: }
2469: for (k=1; k<=nsq;k++) { /* For single varying covariates only */
2470: /* Here comes the value of quantitative after renumbering k with single quantitative covariates */
1.235 brouard 2471: cov[2+nagesqr+TvarsQind[k]]=Tqresult[nres][k];
2472: /* 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 2473: }
1.237 brouard 2474: for (k=1; k<=cptcovage;k++){ /* For product with age */
1.234 brouard 2475: if(Dummy[Tvar[Tage[k]]]){
2476: cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,k)]*cov[2];
2477: } else{
1.235 brouard 2478: cov[2+nagesqr+Tage[k]]=Tqresult[nres][k];
1.234 brouard 2479: }
1.235 brouard 2480: /* 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 2481: }
1.237 brouard 2482: for (k=1; k<=cptcovprod;k++){ /* For product without age */
1.235 brouard 2483: /* 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 2484: if(Dummy[Tvard[k][1]==0]){
2485: if(Dummy[Tvard[k][2]==0]){
2486: cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)] * nbcode[Tvard[k][2]][codtabm(ij,k)];
2487: }else{
2488: cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)] * Tqresult[nres][k];
2489: }
2490: }else{
2491: if(Dummy[Tvard[k][2]==0]){
2492: cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][2]][codtabm(ij,k)] * Tqinvresult[nres][Tvard[k][1]];
2493: }else{
2494: cov[2+nagesqr+Tprod[k]]=Tqinvresult[nres][Tvard[k][1]]* Tqinvresult[nres][Tvard[k][2]];
2495: }
2496: }
1.234 brouard 2497: }
1.138 brouard 2498: /*printf("ij=%d cptcovprod=%d tvar=%d ", ij, cptcovprod, Tvar[1]);*/
2499: /*printf("ij=%d cov[3]=%lf cov[4]=%lf \n",ij, cov[3],cov[4]);*/
2500: /*printf("ij=%d cov[3]=%lf \n",ij, cov[3]);*/
1.145 brouard 2501: /* savm=pmij(pmmij,cov,ncovmodel,x,nlstate); */
2502: /* out=matprod2(newm, pmij(pmmij,cov,ncovmodel,x,nlstate),1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm); /\* Bug Valgrind *\/ */
1.218 brouard 2503: /* age and covariate values of ij are in 'cov' */
1.142 brouard 2504: out=matprod2(newm, pmij(pmmij,cov,ncovmodel,x,nlstate),1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm); /* Bug Valgrind */
1.138 brouard 2505:
1.126 brouard 2506: savm=oldm;
2507: oldm=newm;
1.209 brouard 2508:
2509: for(j=1; j<=nlstate; j++){
2510: max[j]=0.;
2511: min[j]=1.;
2512: }
2513: for(i=1;i<=nlstate;i++){
2514: sumnew=0;
2515: for(k=1; k<=ndeath; k++) sumnew+=newm[i][nlstate+k];
2516: for(j=1; j<=nlstate; j++){
2517: prlim[i][j]= newm[i][j]/(1-sumnew);
2518: max[j]=FMAX(max[j],prlim[i][j]);
2519: min[j]=FMIN(min[j],prlim[i][j]);
2520: }
2521: }
2522:
1.126 brouard 2523: maxmax=0.;
1.209 brouard 2524: for(j=1; j<=nlstate; j++){
2525: meandiff[j]=(max[j]-min[j])/(max[j]+min[j])*2.; /* mean difference for each column */
2526: maxmax=FMAX(maxmax,meandiff[j]);
2527: /* 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 2528: } /* j loop */
1.203 brouard 2529: *ncvyear= (int)age- (int)agefin;
1.208 brouard 2530: /* 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 2531: if(maxmax < ftolpl){
1.209 brouard 2532: /* printf("maxmax=%lf ncvloop=%ld, age=%d, agefin=%d ncvyear=%d \n", maxmax, ncvloop, (int)age, (int)agefin, *ncvyear); */
2533: free_vector(min,1,nlstate);
2534: free_vector(max,1,nlstate);
2535: free_vector(meandiff,1,nlstate);
1.126 brouard 2536: return prlim;
2537: }
1.169 brouard 2538: } /* age loop */
1.208 brouard 2539: /* After some age loop it doesn't converge */
1.209 brouard 2540: 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 2541: 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 2542: /* 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); */
2543: free_vector(min,1,nlstate);
2544: free_vector(max,1,nlstate);
2545: free_vector(meandiff,1,nlstate);
1.208 brouard 2546:
1.169 brouard 2547: return prlim; /* should not reach here */
1.126 brouard 2548: }
2549:
1.217 brouard 2550:
2551: /**** Back Prevalence limit (stable or period prevalence) ****************/
2552:
1.218 brouard 2553: /* 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) */
2554: /* 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) */
1.242 brouard 2555: double **bprevalim(double **bprlim, double ***prevacurrent, int nlstate, double x[], double age, double ftolpl, int *ncvyear, int ij, int nres)
1.217 brouard 2556: {
1.218 brouard 2557: /* Computes the prevalence limit in each live state at age x and covariate ij by left multiplying the unit
1.217 brouard 2558: matrix by transitions matrix until convergence is reached with precision ftolpl */
2559: /* Wx= Wx-1 Px-1= Wx-2 Px-2 Px-1 = Wx-n Px-n ... Px-2 Px-1 I */
2560: /* Wx is row vector: population in state 1, population in state 2, population dead */
2561: /* or prevalence in state 1, prevalence in state 2, 0 */
2562: /* newm is the matrix after multiplications, its rows are identical at a factor */
2563: /* Initial matrix pimij */
2564: /* {0.85204250825084937, 0.13044499163996345, 0.017512500109187184, */
2565: /* 0.090851990222114765, 0.88271245433047185, 0.026435555447413338, */
2566: /* 0, 0 , 1} */
2567: /*
2568: * and after some iteration: */
2569: /* {0.45504275246439968, 0.42731458730878791, 0.11764266022681241, */
2570: /* 0.45201005341706885, 0.42865420071559901, 0.11933574586733192, */
2571: /* 0, 0 , 1} */
2572: /* And prevalence by suppressing the deaths are close to identical rows in prlim: */
2573: /* {0.51571254859325999, 0.4842874514067399, */
2574: /* 0.51326036147820708, 0.48673963852179264} */
2575: /* If we start from prlim again, prlim tends to a constant matrix */
2576:
2577: int i, ii,j,k;
2578: double *min, *max, *meandiff, maxmax,sumnew=0.;
2579: /* double **matprod2(); */ /* test */
2580: double **out, cov[NCOVMAX+1], **bmij();
2581: double **newm;
1.218 brouard 2582: double **dnewm, **doldm, **dsavm; /* for use */
2583: double **oldm, **savm; /* for use */
2584:
1.217 brouard 2585: double agefin, delaymax=200. ; /* 100 Max number of years to converge */
2586: int ncvloop=0;
2587:
2588: min=vector(1,nlstate);
2589: max=vector(1,nlstate);
2590: meandiff=vector(1,nlstate);
2591:
1.218 brouard 2592: dnewm=ddnewms; doldm=ddoldms; dsavm=ddsavms;
2593: oldm=oldms; savm=savms;
2594:
2595: /* Starting with matrix unity */
2596: for (ii=1;ii<=nlstate+ndeath;ii++)
2597: for (j=1;j<=nlstate+ndeath;j++){
1.217 brouard 2598: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
2599: }
2600:
2601: cov[1]=1.;
2602:
2603: /* Even if hstepm = 1, at least one multiplication by the unit matrix */
2604: /* Start at agefin= age, computes the matrix of passage and loops decreasing agefin until convergence is reached */
1.218 brouard 2605: /* for(agefin=age+stepm/YEARM; agefin<=age+delaymax; agefin=agefin+stepm/YEARM){ /\* A changer en age *\/ */
2606: for(agefin=age; agefin<AGESUP; agefin=agefin+stepm/YEARM){ /* A changer en age */
1.217 brouard 2607: ncvloop++;
1.218 brouard 2608: newm=savm; /* oldm should be kept from previous iteration or unity at start */
2609: /* newm points to the allocated table savm passed by the function it can be written, savm could be reallocated */
1.217 brouard 2610: /* Covariates have to be included here again */
2611: cov[2]=agefin;
2612: if(nagesqr==1)
2613: cov[3]= agefin*agefin;;
1.242 brouard 2614: for (k=1; k<=nsd;k++) { /* For single dummy covariates only */
2615: /* Here comes the value of the covariate 'ij' after renumbering k with single dummy covariates */
2616: cov[2+nagesqr+TvarsDind[k]]=nbcode[TvarsD[k]][codtabm(ij,k)];
2617: /* printf("bprevalim 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)); */
2618: }
2619: /* for (k=1; k<=cptcovn;k++) { */
2620: /* /\* cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,Tvar[k])]; *\/ */
2621: /* cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,k)]; */
2622: /* /\* 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])]); *\/ */
2623: /* } */
2624: for (k=1; k<=nsq;k++) { /* For single varying covariates only */
2625: /* Here comes the value of quantitative after renumbering k with single quantitative covariates */
2626: cov[2+nagesqr+TvarsQind[k]]=Tqresult[nres][k];
2627: /* 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]); */
2628: }
2629: /* for (k=1; k<=cptcovage;k++) cov[2+nagesqr+Tage[k]]=nbcode[Tvar[k]][codtabm(ij,k)]*cov[2]; */
2630: /* for (k=1; k<=cptcovprod;k++) /\* Useless *\/ */
2631: /* /\* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,Tvard[k][1])] * nbcode[Tvard[k][2]][codtabm(ij,Tvard[k][2])]; *\/ */
2632: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)] * nbcode[Tvard[k][2]][codtabm(ij,k)]; */
2633: for (k=1; k<=cptcovage;k++){ /* For product with age */
2634: if(Dummy[Tvar[Tage[k]]]){
2635: cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,k)]*cov[2];
2636: } else{
2637: cov[2+nagesqr+Tage[k]]=Tqresult[nres][k];
2638: }
2639: /* 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]); */
2640: }
2641: for (k=1; k<=cptcovprod;k++){ /* For product without age */
2642: /* 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]); */
2643: if(Dummy[Tvard[k][1]==0]){
2644: if(Dummy[Tvard[k][2]==0]){
2645: cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)] * nbcode[Tvard[k][2]][codtabm(ij,k)];
2646: }else{
2647: cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)] * Tqresult[nres][k];
2648: }
2649: }else{
2650: if(Dummy[Tvard[k][2]==0]){
2651: cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][2]][codtabm(ij,k)] * Tqinvresult[nres][Tvard[k][1]];
2652: }else{
2653: cov[2+nagesqr+Tprod[k]]=Tqinvresult[nres][Tvard[k][1]]* Tqinvresult[nres][Tvard[k][2]];
2654: }
2655: }
1.217 brouard 2656: }
2657:
2658: /*printf("ij=%d cptcovprod=%d tvar=%d ", ij, cptcovprod, Tvar[1]);*/
2659: /*printf("ij=%d cov[3]=%lf cov[4]=%lf \n",ij, cov[3],cov[4]);*/
2660: /*printf("ij=%d cov[3]=%lf \n",ij, cov[3]);*/
2661: /* savm=pmij(pmmij,cov,ncovmodel,x,nlstate); */
2662: /* out=matprod2(newm, pmij(pmmij,cov,ncovmodel,x,nlstate),1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm); /\* Bug Valgrind *\/ */
1.218 brouard 2663: /* ij should be linked to the correct index of cov */
2664: /* age and covariate values ij are in 'cov', but we need to pass
2665: * ij for the observed prevalence at age and status and covariate
2666: * number: prevacurrent[(int)agefin][ii][ij]
2667: */
2668: /* 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 *\/ */
2669: /* 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 *\/ */
2670: 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 2671: savm=oldm;
2672: oldm=newm;
2673: for(j=1; j<=nlstate; j++){
2674: max[j]=0.;
2675: min[j]=1.;
2676: }
2677: for(j=1; j<=nlstate; j++){
2678: for(i=1;i<=nlstate;i++){
1.234 brouard 2679: /* bprlim[i][j]= newm[i][j]/(1-sumnew); */
2680: bprlim[i][j]= newm[i][j];
2681: max[i]=FMAX(max[i],bprlim[i][j]); /* Max in line */
2682: min[i]=FMIN(min[i],bprlim[i][j]);
1.217 brouard 2683: }
2684: }
1.218 brouard 2685:
1.217 brouard 2686: maxmax=0.;
2687: for(i=1; i<=nlstate; i++){
2688: meandiff[i]=(max[i]-min[i])/(max[i]+min[i])*2.; /* mean difference for each column */
2689: maxmax=FMAX(maxmax,meandiff[i]);
2690: /* 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); */
2691: } /* j loop */
2692: *ncvyear= -( (int)age- (int)agefin);
1.218 brouard 2693: /* printf("Back maxmax=%lf ncvloop=%d, age=%d, agefin=%d ncvyear=%d \n", maxmax, ncvloop, (int)age, (int)agefin, *ncvyear);*/
1.217 brouard 2694: if(maxmax < ftolpl){
1.220 brouard 2695: /* printf("OK Back maxmax=%lf ncvloop=%d, age=%d, agefin=%d ncvyear=%d \n", maxmax, ncvloop, (int)age, (int)agefin, *ncvyear); */
1.217 brouard 2696: free_vector(min,1,nlstate);
2697: free_vector(max,1,nlstate);
2698: free_vector(meandiff,1,nlstate);
2699: return bprlim;
2700: }
2701: } /* age loop */
2702: /* After some age loop it doesn't converge */
2703: 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\
2704: 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);
2705: /* 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); */
2706: free_vector(min,1,nlstate);
2707: free_vector(max,1,nlstate);
2708: free_vector(meandiff,1,nlstate);
2709:
2710: return bprlim; /* should not reach here */
2711: }
2712:
1.126 brouard 2713: /*************** transition probabilities ***************/
2714:
2715: double **pmij(double **ps, double *cov, int ncovmodel, double *x, int nlstate )
2716: {
1.138 brouard 2717: /* According to parameters values stored in x and the covariate's values stored in cov,
2718: computes the probability to be observed in state j being in state i by appying the
2719: model to the ncovmodel covariates (including constant and age).
2720: lnpijopii=ln(pij/pii)= aij+bij*age+cij*v1+dij*v2+... = sum_nc=1^ncovmodel xij(nc)*cov[nc]
2721: and, according on how parameters are entered, the position of the coefficient xij(nc) of the
2722: ncth covariate in the global vector x is given by the formula:
2723: j<i nc+((i-1)*(nlstate+ndeath-1)+j-1)*ncovmodel
2724: j>=i nc + ((i-1)*(nlstate+ndeath-1)+(j-2))*ncovmodel
2725: Computes ln(pij/pii) (lnpijopii), deduces pij/pii by exponentiation,
2726: sums on j different of i to get 1-pii/pii, deduces pii, and then all pij.
2727: Outputs ps[i][j] the probability to be observed in j being in j according to
2728: the values of the covariates cov[nc] and corresponding parameter values x[nc+shiftij]
2729: */
2730: double s1, lnpijopii;
1.126 brouard 2731: /*double t34;*/
1.164 brouard 2732: int i,j, nc, ii, jj;
1.126 brouard 2733:
1.223 brouard 2734: for(i=1; i<= nlstate; i++){
2735: for(j=1; j<i;j++){
2736: for (nc=1, lnpijopii=0.;nc <=ncovmodel; nc++){
2737: /*lnpijopii += param[i][j][nc]*cov[nc];*/
2738: lnpijopii += x[nc+((i-1)*(nlstate+ndeath-1)+j-1)*ncovmodel]*cov[nc];
2739: /* printf("Int j<i s1=%.17e, lnpijopii=%.17e\n",s1,lnpijopii); */
2740: }
2741: ps[i][j]=lnpijopii; /* In fact ln(pij/pii) */
2742: /* printf("s1=%.17e, lnpijopii=%.17e\n",s1,lnpijopii); */
2743: }
2744: for(j=i+1; j<=nlstate+ndeath;j++){
2745: for (nc=1, lnpijopii=0.;nc <=ncovmodel; nc++){
2746: /*lnpijopii += x[(i-1)*nlstate*ncovmodel+(j-2)*ncovmodel+nc+(i-1)*(ndeath-1)*ncovmodel]*cov[nc];*/
2747: lnpijopii += x[nc + ((i-1)*(nlstate+ndeath-1)+(j-2))*ncovmodel]*cov[nc];
2748: /* printf("Int j>i s1=%.17e, lnpijopii=%.17e %lx %lx\n",s1,lnpijopii,s1,lnpijopii); */
2749: }
2750: ps[i][j]=lnpijopii; /* In fact ln(pij/pii) */
2751: }
2752: }
1.218 brouard 2753:
1.223 brouard 2754: for(i=1; i<= nlstate; i++){
2755: s1=0;
2756: for(j=1; j<i; j++){
2757: s1+=exp(ps[i][j]); /* In fact sums pij/pii */
2758: /*printf("debug1 %d %d ps=%lf exp(ps)=%lf s1+=%lf\n",i,j,ps[i][j],exp(ps[i][j]),s1); */
2759: }
2760: for(j=i+1; j<=nlstate+ndeath; j++){
2761: s1+=exp(ps[i][j]); /* In fact sums pij/pii */
2762: /*printf("debug2 %d %d ps=%lf exp(ps)=%lf s1+=%lf\n",i,j,ps[i][j],exp(ps[i][j]),s1); */
2763: }
2764: /* s1= sum_{j<>i} pij/pii=(1-pii)/pii and thus pii is known from s1 */
2765: ps[i][i]=1./(s1+1.);
2766: /* Computing other pijs */
2767: for(j=1; j<i; j++)
2768: ps[i][j]= exp(ps[i][j])*ps[i][i];
2769: for(j=i+1; j<=nlstate+ndeath; j++)
2770: ps[i][j]= exp(ps[i][j])*ps[i][i];
2771: /* ps[i][nlstate+1]=1.-s1- ps[i][i];*/ /* Sum should be 1 */
2772: } /* end i */
1.218 brouard 2773:
1.223 brouard 2774: for(ii=nlstate+1; ii<= nlstate+ndeath; ii++){
2775: for(jj=1; jj<= nlstate+ndeath; jj++){
2776: ps[ii][jj]=0;
2777: ps[ii][ii]=1;
2778: }
2779: }
1.218 brouard 2780:
2781:
1.223 brouard 2782: /* for(ii=1; ii<= nlstate+ndeath; ii++){ */
2783: /* for(jj=1; jj<= nlstate+ndeath; jj++){ */
2784: /* printf(" pmij ps[%d][%d]=%lf ",ii,jj,ps[ii][jj]); */
2785: /* } */
2786: /* printf("\n "); */
2787: /* } */
2788: /* printf("\n ");printf("%lf ",cov[2]);*/
2789: /*
2790: for(i=1; i<= npar; i++) printf("%f ",x[i]);
1.218 brouard 2791: goto end;*/
1.223 brouard 2792: return ps;
1.126 brouard 2793: }
2794:
1.218 brouard 2795: /*************** backward transition probabilities ***************/
2796:
2797: /* 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 ) */
2798: /* double **bmij(double **ps, double *cov, int ncovmodel, double *x, int nlstate, double ***prevacurrent, double ***dnewm, double **doldm, double **dsavm, int ij ) */
2799: double **bmij(double **ps, double *cov, int ncovmodel, double *x, int nlstate, double ***prevacurrent, int ij )
2800: {
1.222 brouard 2801: /* Computes the backward probability at age agefin and covariate ij
2802: * and returns in **ps as well as **bmij.
2803: */
1.218 brouard 2804: int i, ii, j,k;
1.222 brouard 2805:
2806: double **out, **pmij();
2807: double sumnew=0.;
1.218 brouard 2808: double agefin;
1.222 brouard 2809:
2810: double **dnewm, **dsavm, **doldm;
2811: double **bbmij;
2812:
1.218 brouard 2813: doldm=ddoldms; /* global pointers */
1.222 brouard 2814: dnewm=ddnewms;
2815: dsavm=ddsavms;
2816:
2817: agefin=cov[2];
2818: /* bmij *//* age is cov[2], ij is included in cov, but we need for
2819: the observed prevalence (with this covariate ij) */
2820: dsavm=pmij(pmmij,cov,ncovmodel,x,nlstate);
2821: /* We do have the matrix Px in savm and we need pij */
2822: for (j=1;j<=nlstate+ndeath;j++){
2823: sumnew=0.; /* w1 p11 + w2 p21 only on live states */
2824: for (ii=1;ii<=nlstate;ii++){
2825: sumnew+=dsavm[ii][j]*prevacurrent[(int)agefin][ii][ij];
2826: } /* sumnew is (N11+N21)/N..= N.1/N.. = sum on i of w_i pij */
2827: for (ii=1;ii<=nlstate+ndeath;ii++){
2828: if(sumnew >= 1.e-10){
2829: /* if(agefin >= agemaxpar && agefin <= agemaxpar+stepm/YEARM){ */
2830: /* doldm[ii][j]=(ii==j ? 1./sumnew : 0.0); */
2831: /* }else if(agefin >= agemaxpar+stepm/YEARM){ */
2832: /* doldm[ii][j]=(ii==j ? 1./sumnew : 0.0); */
2833: /* }else */
2834: doldm[ii][j]=(ii==j ? 1./sumnew : 0.0);
2835: }else{
1.242 brouard 2836: ;
2837: /* 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); */
1.222 brouard 2838: }
2839: } /*End ii */
2840: } /* End j, At the end doldm is diag[1/(w_1p1i+w_2 p2i)] */
2841: /* left Product of this diag matrix by dsavm=Px (newm=dsavm*doldm) */
2842: bbmij=matprod2(dnewm, dsavm,1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, doldm); /* Bug Valgrind */
2843: /* dsavm=doldm; /\* dsavm is now diag [1/(w_1p1i+w_2 p2i)] but can be overwritten*\/ */
2844: /* doldm=dnewm; /\* doldm is now Px * diag [1/(w_1p1i+w_2 p2i)] *\/ */
2845: /* dnewm=dsavm; /\* doldm is now Px * diag [1/(w_1p1i+w_2 p2i)] *\/ */
2846: /* left Product of this matrix by diag matrix of prevalences (savm) */
2847: for (j=1;j<=nlstate+ndeath;j++){
2848: for (ii=1;ii<=nlstate+ndeath;ii++){
2849: dsavm[ii][j]=(ii==j ? prevacurrent[(int)agefin][ii][ij] : 0.0);
2850: }
2851: } /* End j, At the end oldm is diag[1/(w_1p1i+w_2 p2i)] */
2852: ps=matprod2(doldm, dsavm,1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, dnewm); /* Bug Valgrind */
2853: /* newm or out is now diag[w_i] * Px * diag [1/(w_1p1i+w_2 p2i)] */
2854: /* end bmij */
2855: return ps;
1.218 brouard 2856: }
1.217 brouard 2857: /*************** transition probabilities ***************/
2858:
1.218 brouard 2859: double **bpmij(double **ps, double *cov, int ncovmodel, double *x, int nlstate )
1.217 brouard 2860: {
2861: /* According to parameters values stored in x and the covariate's values stored in cov,
2862: computes the probability to be observed in state j being in state i by appying the
2863: model to the ncovmodel covariates (including constant and age).
2864: lnpijopii=ln(pij/pii)= aij+bij*age+cij*v1+dij*v2+... = sum_nc=1^ncovmodel xij(nc)*cov[nc]
2865: and, according on how parameters are entered, the position of the coefficient xij(nc) of the
2866: ncth covariate in the global vector x is given by the formula:
2867: j<i nc+((i-1)*(nlstate+ndeath-1)+j-1)*ncovmodel
2868: j>=i nc + ((i-1)*(nlstate+ndeath-1)+(j-2))*ncovmodel
2869: Computes ln(pij/pii) (lnpijopii), deduces pij/pii by exponentiation,
2870: sums on j different of i to get 1-pii/pii, deduces pii, and then all pij.
2871: Outputs ps[i][j] the probability to be observed in j being in j according to
2872: the values of the covariates cov[nc] and corresponding parameter values x[nc+shiftij]
2873: */
2874: double s1, lnpijopii;
2875: /*double t34;*/
2876: int i,j, nc, ii, jj;
2877:
1.234 brouard 2878: for(i=1; i<= nlstate; i++){
2879: for(j=1; j<i;j++){
2880: for (nc=1, lnpijopii=0.;nc <=ncovmodel; nc++){
2881: /*lnpijopii += param[i][j][nc]*cov[nc];*/
2882: lnpijopii += x[nc+((i-1)*(nlstate+ndeath-1)+j-1)*ncovmodel]*cov[nc];
2883: /* printf("Int j<i s1=%.17e, lnpijopii=%.17e\n",s1,lnpijopii); */
2884: }
2885: ps[i][j]=lnpijopii; /* In fact ln(pij/pii) */
2886: /* printf("s1=%.17e, lnpijopii=%.17e\n",s1,lnpijopii); */
2887: }
2888: for(j=i+1; j<=nlstate+ndeath;j++){
2889: for (nc=1, lnpijopii=0.;nc <=ncovmodel; nc++){
2890: /*lnpijopii += x[(i-1)*nlstate*ncovmodel+(j-2)*ncovmodel+nc+(i-1)*(ndeath-1)*ncovmodel]*cov[nc];*/
2891: lnpijopii += x[nc + ((i-1)*(nlstate+ndeath-1)+(j-2))*ncovmodel]*cov[nc];
2892: /* printf("Int j>i s1=%.17e, lnpijopii=%.17e %lx %lx\n",s1,lnpijopii,s1,lnpijopii); */
2893: }
2894: ps[i][j]=lnpijopii; /* In fact ln(pij/pii) */
2895: }
2896: }
2897:
2898: for(i=1; i<= nlstate; i++){
2899: s1=0;
2900: for(j=1; j<i; j++){
2901: s1+=exp(ps[i][j]); /* In fact sums pij/pii */
2902: /*printf("debug1 %d %d ps=%lf exp(ps)=%lf s1+=%lf\n",i,j,ps[i][j],exp(ps[i][j]),s1); */
2903: }
2904: for(j=i+1; j<=nlstate+ndeath; j++){
2905: s1+=exp(ps[i][j]); /* In fact sums pij/pii */
2906: /*printf("debug2 %d %d ps=%lf exp(ps)=%lf s1+=%lf\n",i,j,ps[i][j],exp(ps[i][j]),s1); */
2907: }
2908: /* s1= sum_{j<>i} pij/pii=(1-pii)/pii and thus pii is known from s1 */
2909: ps[i][i]=1./(s1+1.);
2910: /* Computing other pijs */
2911: for(j=1; j<i; j++)
2912: ps[i][j]= exp(ps[i][j])*ps[i][i];
2913: for(j=i+1; j<=nlstate+ndeath; j++)
2914: ps[i][j]= exp(ps[i][j])*ps[i][i];
2915: /* ps[i][nlstate+1]=1.-s1- ps[i][i];*/ /* Sum should be 1 */
2916: } /* end i */
2917:
2918: for(ii=nlstate+1; ii<= nlstate+ndeath; ii++){
2919: for(jj=1; jj<= nlstate+ndeath; jj++){
2920: ps[ii][jj]=0;
2921: ps[ii][ii]=1;
2922: }
2923: }
2924: /* Added for backcast */ /* Transposed matrix too */
2925: for(jj=1; jj<= nlstate+ndeath; jj++){
2926: s1=0.;
2927: for(ii=1; ii<= nlstate+ndeath; ii++){
2928: s1+=ps[ii][jj];
2929: }
2930: for(ii=1; ii<= nlstate; ii++){
2931: ps[ii][jj]=ps[ii][jj]/s1;
2932: }
2933: }
2934: /* Transposition */
2935: for(jj=1; jj<= nlstate+ndeath; jj++){
2936: for(ii=jj; ii<= nlstate+ndeath; ii++){
2937: s1=ps[ii][jj];
2938: ps[ii][jj]=ps[jj][ii];
2939: ps[jj][ii]=s1;
2940: }
2941: }
2942: /* for(ii=1; ii<= nlstate+ndeath; ii++){ */
2943: /* for(jj=1; jj<= nlstate+ndeath; jj++){ */
2944: /* printf(" pmij ps[%d][%d]=%lf ",ii,jj,ps[ii][jj]); */
2945: /* } */
2946: /* printf("\n "); */
2947: /* } */
2948: /* printf("\n ");printf("%lf ",cov[2]);*/
2949: /*
2950: for(i=1; i<= npar; i++) printf("%f ",x[i]);
2951: goto end;*/
2952: return ps;
1.217 brouard 2953: }
2954:
2955:
1.126 brouard 2956: /**************** Product of 2 matrices ******************/
2957:
1.145 brouard 2958: double **matprod2(double **out, double **in,int nrl, int nrh, int ncl, int nch, int ncolol, int ncoloh, double **b)
1.126 brouard 2959: {
2960: /* Computes the matrix product of in(1,nrh-nrl+1)(1,nch-ncl+1) times
2961: b(1,nch-ncl+1)(1,ncoloh-ncolol+1) into out(...) */
2962: /* in, b, out are matrice of pointers which should have been initialized
2963: before: only the contents of out is modified. The function returns
2964: a pointer to pointers identical to out */
1.145 brouard 2965: int i, j, k;
1.126 brouard 2966: for(i=nrl; i<= nrh; i++)
1.145 brouard 2967: for(k=ncolol; k<=ncoloh; k++){
2968: out[i][k]=0.;
2969: for(j=ncl; j<=nch; j++)
2970: out[i][k] +=in[i][j]*b[j][k];
2971: }
1.126 brouard 2972: return out;
2973: }
2974:
2975:
2976: /************* Higher Matrix Product ***************/
2977:
1.235 brouard 2978: 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 2979: {
1.218 brouard 2980: /* Computes the transition matrix starting at age 'age' and combination of covariate values corresponding to ij over
1.126 brouard 2981: 'nhstepm*hstepm*stepm' months (i.e. until
2982: age (in years) age+nhstepm*hstepm*stepm/12) by multiplying
2983: nhstepm*hstepm matrices.
2984: Output is stored in matrix po[i][j][h] for h every 'hstepm' step
2985: (typically every 2 years instead of every month which is too big
2986: for the memory).
2987: Model is determined by parameters x and covariates have to be
2988: included manually here.
2989:
2990: */
2991:
2992: int i, j, d, h, k;
1.131 brouard 2993: double **out, cov[NCOVMAX+1];
1.126 brouard 2994: double **newm;
1.187 brouard 2995: double agexact;
1.214 brouard 2996: double agebegin, ageend;
1.126 brouard 2997:
2998: /* Hstepm could be zero and should return the unit matrix */
2999: for (i=1;i<=nlstate+ndeath;i++)
3000: for (j=1;j<=nlstate+ndeath;j++){
3001: oldm[i][j]=(i==j ? 1.0 : 0.0);
3002: po[i][j][0]=(i==j ? 1.0 : 0.0);
3003: }
3004: /* Even if hstepm = 1, at least one multiplication by the unit matrix */
3005: for(h=1; h <=nhstepm; h++){
3006: for(d=1; d <=hstepm; d++){
3007: newm=savm;
3008: /* Covariates have to be included here again */
3009: cov[1]=1.;
1.214 brouard 3010: agexact=age+((h-1)*hstepm + (d-1))*stepm/YEARM; /* age just before transition */
1.187 brouard 3011: cov[2]=agexact;
3012: if(nagesqr==1)
1.227 brouard 3013: cov[3]= agexact*agexact;
1.235 brouard 3014: for (k=1; k<=nsd;k++) { /* For single dummy covariates only */
3015: /* Here comes the value of the covariate 'ij' after renumbering k with single dummy covariates */
3016: cov[2+nagesqr+TvarsDind[k]]=nbcode[TvarsD[k]][codtabm(ij,k)];
3017: /* 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)); */
3018: }
3019: for (k=1; k<=nsq;k++) { /* For single varying covariates only */
3020: /* Here comes the value of quantitative after renumbering k with single quantitative covariates */
3021: cov[2+nagesqr+TvarsQind[k]]=Tqresult[nres][k];
3022: /* 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]); */
3023: }
3024: for (k=1; k<=cptcovage;k++){
3025: if(Dummy[Tvar[Tage[k]]]){
3026: cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,k)]*cov[2];
3027: } else{
3028: cov[2+nagesqr+Tage[k]]=Tqresult[nres][k];
3029: }
3030: /* 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]); */
3031: }
3032: for (k=1; k<=cptcovprod;k++){ /* */
3033: /* 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]); */
3034: cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)] * nbcode[Tvard[k][2]][codtabm(ij,k)];
3035: }
3036: /* for (k=1; k<=cptcovn;k++) */
3037: /* cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,k)]; */
3038: /* for (k=1; k<=cptcovage;k++) /\* Should start at cptcovn+1 *\/ */
3039: /* cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,k)]*cov[2]; */
3040: /* for (k=1; k<=cptcovprod;k++) /\* Useless because included in cptcovn *\/ */
3041: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)]*nbcode[Tvard[k][2]][codtabm(ij,k)]; */
1.227 brouard 3042:
3043:
1.126 brouard 3044: /*printf("hxi cptcov=%d cptcode=%d\n",cptcov,cptcode);*/
3045: /*printf("h=%d d=%d age=%f cov=%f\n",h,d,age,cov[2]);*/
1.218 brouard 3046: /* right multiplication of oldm by the current matrix */
1.126 brouard 3047: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath,
3048: pmij(pmmij,cov,ncovmodel,x,nlstate));
1.217 brouard 3049: /* if((int)age == 70){ */
3050: /* printf(" Forward hpxij age=%d agexact=%f d=%d nhstepm=%d hstepm=%d\n", (int) age, agexact, d, nhstepm, hstepm); */
3051: /* for(i=1; i<=nlstate+ndeath; i++) { */
3052: /* printf("%d pmmij ",i); */
3053: /* for(j=1;j<=nlstate+ndeath;j++) { */
3054: /* printf("%f ",pmmij[i][j]); */
3055: /* } */
3056: /* printf(" oldm "); */
3057: /* for(j=1;j<=nlstate+ndeath;j++) { */
3058: /* printf("%f ",oldm[i][j]); */
3059: /* } */
3060: /* printf("\n"); */
3061: /* } */
3062: /* } */
1.126 brouard 3063: savm=oldm;
3064: oldm=newm;
3065: }
3066: for(i=1; i<=nlstate+ndeath; i++)
3067: for(j=1;j<=nlstate+ndeath;j++) {
1.218 brouard 3068: po[i][j][h]=newm[i][j];
3069: /*if(h==nhstepm) printf("po[%d][%d][%d]=%f ",i,j,h,po[i][j][h]);*/
1.126 brouard 3070: }
1.128 brouard 3071: /*printf("h=%d ",h);*/
1.126 brouard 3072: } /* end h */
1.218 brouard 3073: /* printf("\n H=%d \n",h); */
1.126 brouard 3074: return po;
3075: }
3076:
1.217 brouard 3077: /************* Higher Back Matrix Product ***************/
1.218 brouard 3078: /* 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 3079: double ***hbxij(double ***po, int nhstepm, double age, int hstepm, double *x, double ***prevacurrent, int nlstate, int stepm, int ij )
1.217 brouard 3080: {
1.218 brouard 3081: /* Computes the transition matrix starting at age 'age' over
1.217 brouard 3082: 'nhstepm*hstepm*stepm' months (i.e. until
1.218 brouard 3083: age (in years) age+nhstepm*hstepm*stepm/12) by multiplying
3084: nhstepm*hstepm matrices.
3085: Output is stored in matrix po[i][j][h] for h every 'hstepm' step
3086: (typically every 2 years instead of every month which is too big
1.217 brouard 3087: for the memory).
1.218 brouard 3088: Model is determined by parameters x and covariates have to be
3089: included manually here.
1.217 brouard 3090:
1.222 brouard 3091: */
1.217 brouard 3092:
3093: int i, j, d, h, k;
3094: double **out, cov[NCOVMAX+1];
3095: double **newm;
3096: double agexact;
3097: double agebegin, ageend;
1.222 brouard 3098: double **oldm, **savm;
1.217 brouard 3099:
1.222 brouard 3100: oldm=oldms;savm=savms;
1.217 brouard 3101: /* Hstepm could be zero and should return the unit matrix */
3102: for (i=1;i<=nlstate+ndeath;i++)
3103: for (j=1;j<=nlstate+ndeath;j++){
3104: oldm[i][j]=(i==j ? 1.0 : 0.0);
3105: po[i][j][0]=(i==j ? 1.0 : 0.0);
3106: }
3107: /* Even if hstepm = 1, at least one multiplication by the unit matrix */
3108: for(h=1; h <=nhstepm; h++){
3109: for(d=1; d <=hstepm; d++){
3110: newm=savm;
3111: /* Covariates have to be included here again */
3112: cov[1]=1.;
3113: agexact=age-((h-1)*hstepm + (d-1))*stepm/YEARM; /* age just before transition */
3114: /* agexact=age+((h-1)*hstepm + (d-1))*stepm/YEARM; /\* age just before transition *\/ */
3115: cov[2]=agexact;
3116: if(nagesqr==1)
1.222 brouard 3117: cov[3]= agexact*agexact;
1.218 brouard 3118: for (k=1; k<=cptcovn;k++)
1.222 brouard 3119: cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,k)];
3120: /* cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,Tvar[k])]; */
1.217 brouard 3121: for (k=1; k<=cptcovage;k++) /* Should start at cptcovn+1 */
1.222 brouard 3122: /* cov[2+Tage[k]]=cov[2+Tage[k]]*cov[2]; */
3123: cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,k)]*cov[2];
3124: /* cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,Tvar[Tage[k]])]*cov[2]; */
1.217 brouard 3125: for (k=1; k<=cptcovprod;k++) /* Useless because included in cptcovn */
1.222 brouard 3126: cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)]*nbcode[Tvard[k][2]][codtabm(ij,k)];
3127: /* 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 3128:
3129:
1.217 brouard 3130: /*printf("hxi cptcov=%d cptcode=%d\n",cptcov,cptcode);*/
3131: /*printf("h=%d d=%d age=%f cov=%f\n",h,d,age,cov[2]);*/
1.218 brouard 3132: /* Careful transposed matrix */
1.222 brouard 3133: /* age is in cov[2] */
1.218 brouard 3134: /* out=matprod2(newm, bmij(pmmij,cov,ncovmodel,x,nlstate,prevacurrent, dnewm, doldm, dsavm,ij),\ */
1.222 brouard 3135: /* 1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm); */
1.218 brouard 3136: out=matprod2(newm, bmij(pmmij,cov,ncovmodel,x,nlstate,prevacurrent,ij),\
1.222 brouard 3137: 1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm);
1.217 brouard 3138: /* if((int)age == 70){ */
3139: /* printf(" Backward hbxij age=%d agexact=%f d=%d nhstepm=%d hstepm=%d\n", (int) age, agexact, d, nhstepm, hstepm); */
3140: /* for(i=1; i<=nlstate+ndeath; i++) { */
3141: /* printf("%d pmmij ",i); */
3142: /* for(j=1;j<=nlstate+ndeath;j++) { */
3143: /* printf("%f ",pmmij[i][j]); */
3144: /* } */
3145: /* printf(" oldm "); */
3146: /* for(j=1;j<=nlstate+ndeath;j++) { */
3147: /* printf("%f ",oldm[i][j]); */
3148: /* } */
3149: /* printf("\n"); */
3150: /* } */
3151: /* } */
3152: savm=oldm;
3153: oldm=newm;
3154: }
3155: for(i=1; i<=nlstate+ndeath; i++)
3156: for(j=1;j<=nlstate+ndeath;j++) {
1.222 brouard 3157: po[i][j][h]=newm[i][j];
3158: /*if(h==nhstepm) printf("po[%d][%d][%d]=%f ",i,j,h,po[i][j][h]);*/
1.217 brouard 3159: }
3160: /*printf("h=%d ",h);*/
3161: } /* end h */
1.222 brouard 3162: /* printf("\n H=%d \n",h); */
1.217 brouard 3163: return po;
3164: }
3165:
3166:
1.162 brouard 3167: #ifdef NLOPT
3168: double myfunc(unsigned n, const double *p1, double *grad, void *pd){
3169: double fret;
3170: double *xt;
3171: int j;
3172: myfunc_data *d2 = (myfunc_data *) pd;
3173: /* xt = (p1-1); */
3174: xt=vector(1,n);
3175: for (j=1;j<=n;j++) xt[j]=p1[j-1]; /* xt[1]=p1[0] */
3176:
3177: fret=(d2->function)(xt); /* p xt[1]@8 is fine */
3178: /* fret=(*func)(xt); /\* p xt[1]@8 is fine *\/ */
3179: printf("Function = %.12lf ",fret);
3180: for (j=1;j<=n;j++) printf(" %d %.8lf", j, xt[j]);
3181: printf("\n");
3182: free_vector(xt,1,n);
3183: return fret;
3184: }
3185: #endif
1.126 brouard 3186:
3187: /*************** log-likelihood *************/
3188: double func( double *x)
3189: {
1.226 brouard 3190: int i, ii, j, k, mi, d, kk;
3191: int ioffset=0;
3192: double l, ll[NLSTATEMAX+1], cov[NCOVMAX+1];
3193: double **out;
3194: double lli; /* Individual log likelihood */
3195: int s1, s2;
1.228 brouard 3196: 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 3197: double bbh, survp;
3198: long ipmx;
3199: double agexact;
3200: /*extern weight */
3201: /* We are differentiating ll according to initial status */
3202: /* for (i=1;i<=npar;i++) printf("%f ", x[i]);*/
3203: /*for(i=1;i<imx;i++)
3204: printf(" %d\n",s[4][i]);
3205: */
1.162 brouard 3206:
1.226 brouard 3207: ++countcallfunc;
1.162 brouard 3208:
1.226 brouard 3209: cov[1]=1.;
1.126 brouard 3210:
1.226 brouard 3211: for(k=1; k<=nlstate; k++) ll[k]=0.;
1.224 brouard 3212: ioffset=0;
1.226 brouard 3213: if(mle==1){
3214: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
3215: /* Computes the values of the ncovmodel covariates of the model
3216: depending if the covariates are fixed or varying (age dependent) and stores them in cov[]
3217: Then computes with function pmij which return a matrix p[i][j] giving the elementary probability
3218: to be observed in j being in i according to the model.
3219: */
1.243 brouard 3220: ioffset=2+nagesqr ;
1.233 brouard 3221: /* Fixed */
1.234 brouard 3222: for (k=1; k<=ncovf;k++){ /* Simple and product fixed covariates without age* products */
3223: 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)*/
3224: }
1.226 brouard 3225: /* In model V2+V1*V4+age*V3+V3*V2 Tvar[1] is V2, Tvar[2=V1*V4]
3226: is 6, Tvar[3=age*V3] should not be computed because of age Tvar[4=V3*V2]
3227: has been calculated etc */
3228: /* For an individual i, wav[i] gives the number of effective waves */
3229: /* We compute the contribution to Likelihood of each effective transition
3230: mw[mi][i] is real wave of the mi th effectve wave */
3231: /* Then statuses are computed at each begin and end of an effective wave s1=s[ mw[mi][i] ][i];
3232: s2=s[mw[mi+1][i]][i];
3233: And the iv th varying covariate is the cotvar[mw[mi+1][i]][iv][i]
3234: But if the variable is not in the model TTvar[iv] is the real variable effective in the model:
3235: meaning that decodemodel should be used cotvar[mw[mi+1][i]][TTvar[iv]][i]
3236: */
3237: for(mi=1; mi<= wav[i]-1; mi++){
1.234 brouard 3238: for(k=1; k <= ncovv ; k++){ /* Varying covariates (single and product but no age )*/
1.242 brouard 3239: /* cov[ioffset+TvarVind[k]]=cotvar[mw[mi][i]][Tvar[TvarVind[k]]][i]; */
3240: cov[ioffset+TvarVind[k]]=cotvar[mw[mi][i]][Tvar[TvarVind[k]]-ncovcol-nqv][i];
1.234 brouard 3241: }
3242: for (ii=1;ii<=nlstate+ndeath;ii++)
3243: for (j=1;j<=nlstate+ndeath;j++){
3244: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
3245: savm[ii][j]=(ii==j ? 1.0 : 0.0);
3246: }
3247: for(d=0; d<dh[mi][i]; d++){
3248: newm=savm;
3249: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM;
3250: cov[2]=agexact;
3251: if(nagesqr==1)
3252: cov[3]= agexact*agexact; /* Should be changed here */
3253: for (kk=1; kk<=cptcovage;kk++) {
1.242 brouard 3254: if(!FixedV[Tvar[Tage[kk]]])
1.234 brouard 3255: cov[Tage[kk]+2+nagesqr]=covar[Tvar[Tage[kk]]][i]*agexact; /* Tage[kk] gives the data-covariate associated with age */
1.242 brouard 3256: else
3257: cov[Tage[kk]+2+nagesqr]=cotvar[mw[mi][i]][Tvar[Tage[kk]]-ncovcol-nqv][i]*agexact;
1.234 brouard 3258: }
3259: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
3260: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
3261: savm=oldm;
3262: oldm=newm;
3263: } /* end mult */
3264:
3265: /*lli=log(out[s[mw[mi][i]][i]][s[mw[mi+1][i]][i]]);*/ /* Original formula */
3266: /* But now since version 0.9 we anticipate for bias at large stepm.
3267: * If stepm is larger than one month (smallest stepm) and if the exact delay
3268: * (in months) between two waves is not a multiple of stepm, we rounded to
3269: * the nearest (and in case of equal distance, to the lowest) interval but now
3270: * we keep into memory the bias bh[mi][i] and also the previous matrix product
3271: * (i.e to dh[mi][i]-1) saved in 'savm'. Then we inter(extra)polate the
3272: * probability in order to take into account the bias as a fraction of the way
1.231 brouard 3273: * from savm to out if bh is negative or even beyond if bh is positive. bh varies
3274: * -stepm/2 to stepm/2 .
3275: * For stepm=1 the results are the same as for previous versions of Imach.
3276: * For stepm > 1 the results are less biased than in previous versions.
3277: */
1.234 brouard 3278: s1=s[mw[mi][i]][i];
3279: s2=s[mw[mi+1][i]][i];
3280: bbh=(double)bh[mi][i]/(double)stepm;
3281: /* bias bh is positive if real duration
3282: * is higher than the multiple of stepm and negative otherwise.
3283: */
3284: /* lli= (savm[s1][s2]>1.e-8 ?(1.+bbh)*log(out[s1][s2])- bbh*log(savm[s1][s2]):log((1.+bbh)*out[s1][s2]));*/
3285: if( s2 > nlstate){
3286: /* i.e. if s2 is a death state and if the date of death is known
3287: then the contribution to the likelihood is the probability to
3288: die between last step unit time and current step unit time,
3289: which is also equal to probability to die before dh
3290: minus probability to die before dh-stepm .
3291: In version up to 0.92 likelihood was computed
3292: as if date of death was unknown. Death was treated as any other
3293: health state: the date of the interview describes the actual state
3294: and not the date of a change in health state. The former idea was
3295: to consider that at each interview the state was recorded
3296: (healthy, disable or death) and IMaCh was corrected; but when we
3297: introduced the exact date of death then we should have modified
3298: the contribution of an exact death to the likelihood. This new
3299: contribution is smaller and very dependent of the step unit
3300: stepm. It is no more the probability to die between last interview
3301: and month of death but the probability to survive from last
3302: interview up to one month before death multiplied by the
3303: probability to die within a month. Thanks to Chris
3304: Jackson for correcting this bug. Former versions increased
3305: mortality artificially. The bad side is that we add another loop
3306: which slows down the processing. The difference can be up to 10%
3307: lower mortality.
3308: */
3309: /* If, at the beginning of the maximization mostly, the
3310: cumulative probability or probability to be dead is
3311: constant (ie = 1) over time d, the difference is equal to
3312: 0. out[s1][3] = savm[s1][3]: probability, being at state
3313: s1 at precedent wave, to be dead a month before current
3314: wave is equal to probability, being at state s1 at
3315: precedent wave, to be dead at mont of the current
3316: wave. Then the observed probability (that this person died)
3317: is null according to current estimated parameter. In fact,
3318: it should be very low but not zero otherwise the log go to
3319: infinity.
3320: */
1.183 brouard 3321: /* #ifdef INFINITYORIGINAL */
3322: /* lli=log(out[s1][s2] - savm[s1][s2]); */
3323: /* #else */
3324: /* if ((out[s1][s2] - savm[s1][s2]) < mytinydouble) */
3325: /* lli=log(mytinydouble); */
3326: /* else */
3327: /* lli=log(out[s1][s2] - savm[s1][s2]); */
3328: /* #endif */
1.226 brouard 3329: lli=log(out[s1][s2] - savm[s1][s2]);
1.216 brouard 3330:
1.226 brouard 3331: } else if ( s2==-1 ) { /* alive */
3332: for (j=1,survp=0. ; j<=nlstate; j++)
3333: survp += (1.+bbh)*out[s1][j]- bbh*savm[s1][j];
3334: /*survp += out[s1][j]; */
3335: lli= log(survp);
3336: }
3337: else if (s2==-4) {
3338: for (j=3,survp=0. ; j<=nlstate; j++)
3339: survp += (1.+bbh)*out[s1][j]- bbh*savm[s1][j];
3340: lli= log(survp);
3341: }
3342: else if (s2==-5) {
3343: for (j=1,survp=0. ; j<=2; j++)
3344: survp += (1.+bbh)*out[s1][j]- bbh*savm[s1][j];
3345: lli= log(survp);
3346: }
3347: else{
3348: lli= log((1.+bbh)*out[s1][s2]- bbh*savm[s1][s2]); /* linear interpolation */
3349: /* 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 */
3350: }
3351: /*lli=(1.+bbh)*log(out[s1][s2])- bbh*log(savm[s1][s2]);*/
3352: /*if(lli ==000.0)*/
3353: /*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); */
3354: ipmx +=1;
3355: sw += weight[i];
3356: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
3357: /* if (lli < log(mytinydouble)){ */
3358: /* 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); */
3359: /* 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]); */
3360: /* } */
3361: } /* end of wave */
3362: } /* end of individual */
3363: } else if(mle==2){
3364: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
3365: for (k=1; k<=cptcovn;k++) cov[2+nagesqr+k]=covar[Tvar[k]][i];
3366: for(mi=1; mi<= wav[i]-1; mi++){
3367: for (ii=1;ii<=nlstate+ndeath;ii++)
3368: for (j=1;j<=nlstate+ndeath;j++){
3369: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
3370: savm[ii][j]=(ii==j ? 1.0 : 0.0);
3371: }
3372: for(d=0; d<=dh[mi][i]; d++){
3373: newm=savm;
3374: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM;
3375: cov[2]=agexact;
3376: if(nagesqr==1)
3377: cov[3]= agexact*agexact;
3378: for (kk=1; kk<=cptcovage;kk++) {
3379: cov[Tage[kk]+2+nagesqr]=covar[Tvar[Tage[kk]]][i]*agexact;
3380: }
3381: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
3382: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
3383: savm=oldm;
3384: oldm=newm;
3385: } /* end mult */
3386:
3387: s1=s[mw[mi][i]][i];
3388: s2=s[mw[mi+1][i]][i];
3389: bbh=(double)bh[mi][i]/(double)stepm;
3390: 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 */
3391: ipmx +=1;
3392: sw += weight[i];
3393: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
3394: } /* end of wave */
3395: } /* end of individual */
3396: } else if(mle==3){ /* exponential inter-extrapolation */
3397: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
3398: for (k=1; k<=cptcovn;k++) cov[2+nagesqr+k]=covar[Tvar[k]][i];
3399: for(mi=1; mi<= wav[i]-1; mi++){
3400: for (ii=1;ii<=nlstate+ndeath;ii++)
3401: for (j=1;j<=nlstate+ndeath;j++){
3402: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
3403: savm[ii][j]=(ii==j ? 1.0 : 0.0);
3404: }
3405: for(d=0; d<dh[mi][i]; d++){
3406: newm=savm;
3407: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM;
3408: cov[2]=agexact;
3409: if(nagesqr==1)
3410: cov[3]= agexact*agexact;
3411: for (kk=1; kk<=cptcovage;kk++) {
3412: cov[Tage[kk]+2+nagesqr]=covar[Tvar[Tage[kk]]][i]*agexact;
3413: }
3414: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
3415: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
3416: savm=oldm;
3417: oldm=newm;
3418: } /* end mult */
3419:
3420: s1=s[mw[mi][i]][i];
3421: s2=s[mw[mi+1][i]][i];
3422: bbh=(double)bh[mi][i]/(double)stepm;
3423: 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 */
3424: ipmx +=1;
3425: sw += weight[i];
3426: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
3427: } /* end of wave */
3428: } /* end of individual */
3429: }else if (mle==4){ /* ml=4 no inter-extrapolation */
3430: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
3431: for (k=1; k<=cptcovn;k++) cov[2+nagesqr+k]=covar[Tvar[k]][i];
3432: for(mi=1; mi<= wav[i]-1; mi++){
3433: for (ii=1;ii<=nlstate+ndeath;ii++)
3434: for (j=1;j<=nlstate+ndeath;j++){
3435: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
3436: savm[ii][j]=(ii==j ? 1.0 : 0.0);
3437: }
3438: for(d=0; d<dh[mi][i]; d++){
3439: newm=savm;
3440: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM;
3441: cov[2]=agexact;
3442: if(nagesqr==1)
3443: cov[3]= agexact*agexact;
3444: for (kk=1; kk<=cptcovage;kk++) {
3445: cov[Tage[kk]+2+nagesqr]=covar[Tvar[Tage[kk]]][i]*agexact;
3446: }
1.126 brouard 3447:
1.226 brouard 3448: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
3449: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
3450: savm=oldm;
3451: oldm=newm;
3452: } /* end mult */
3453:
3454: s1=s[mw[mi][i]][i];
3455: s2=s[mw[mi+1][i]][i];
3456: if( s2 > nlstate){
3457: lli=log(out[s1][s2] - savm[s1][s2]);
3458: } else if ( s2==-1 ) { /* alive */
3459: for (j=1,survp=0. ; j<=nlstate; j++)
3460: survp += out[s1][j];
3461: lli= log(survp);
3462: }else{
3463: lli=log(out[s[mw[mi][i]][i]][s[mw[mi+1][i]][i]]); /* Original formula */
3464: }
3465: ipmx +=1;
3466: sw += weight[i];
3467: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
1.126 brouard 3468: /* 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 3469: } /* end of wave */
3470: } /* end of individual */
3471: }else{ /* ml=5 no inter-extrapolation no jackson =0.8a */
3472: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
3473: for (k=1; k<=cptcovn;k++) cov[2+nagesqr+k]=covar[Tvar[k]][i];
3474: for(mi=1; mi<= wav[i]-1; mi++){
3475: for (ii=1;ii<=nlstate+ndeath;ii++)
3476: for (j=1;j<=nlstate+ndeath;j++){
3477: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
3478: savm[ii][j]=(ii==j ? 1.0 : 0.0);
3479: }
3480: for(d=0; d<dh[mi][i]; d++){
3481: newm=savm;
3482: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM;
3483: cov[2]=agexact;
3484: if(nagesqr==1)
3485: cov[3]= agexact*agexact;
3486: for (kk=1; kk<=cptcovage;kk++) {
3487: cov[Tage[kk]+2+nagesqr]=covar[Tvar[Tage[kk]]][i]*agexact;
3488: }
1.126 brouard 3489:
1.226 brouard 3490: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
3491: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
3492: savm=oldm;
3493: oldm=newm;
3494: } /* end mult */
3495:
3496: s1=s[mw[mi][i]][i];
3497: s2=s[mw[mi+1][i]][i];
3498: lli=log(out[s[mw[mi][i]][i]][s[mw[mi+1][i]][i]]); /* Original formula */
3499: ipmx +=1;
3500: sw += weight[i];
3501: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
3502: /*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]);*/
3503: } /* end of wave */
3504: } /* end of individual */
3505: } /* End of if */
3506: for(k=1,l=0.; k<=nlstate; k++) l += ll[k];
3507: /* printf("l1=%f l2=%f ",ll[1],ll[2]); */
3508: l= l*ipmx/sw; /* To get the same order of magnitude as if weight=1 for every body */
3509: return -l;
1.126 brouard 3510: }
3511:
3512: /*************** log-likelihood *************/
3513: double funcone( double *x)
3514: {
1.228 brouard 3515: /* Same as func but slower because of a lot of printf and if */
1.126 brouard 3516: int i, ii, j, k, mi, d, kk;
1.228 brouard 3517: int ioffset=0;
1.131 brouard 3518: double l, ll[NLSTATEMAX+1], cov[NCOVMAX+1];
1.126 brouard 3519: double **out;
3520: double lli; /* Individual log likelihood */
3521: double llt;
3522: int s1, s2;
1.228 brouard 3523: int iv=0, iqv=0, itv=0, iqtv=0 ; /* Index of varying covariate, fixed quantitative cov, time varying covariate, quantitative time varying covariate */
3524:
1.126 brouard 3525: double bbh, survp;
1.187 brouard 3526: double agexact;
1.214 brouard 3527: double agebegin, ageend;
1.126 brouard 3528: /*extern weight */
3529: /* We are differentiating ll according to initial status */
3530: /* for (i=1;i<=npar;i++) printf("%f ", x[i]);*/
3531: /*for(i=1;i<imx;i++)
3532: printf(" %d\n",s[4][i]);
3533: */
3534: cov[1]=1.;
3535:
3536: for(k=1; k<=nlstate; k++) ll[k]=0.;
1.224 brouard 3537: ioffset=0;
3538: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
1.243 brouard 3539: /* ioffset=2+nagesqr+cptcovage; */
3540: ioffset=2+nagesqr;
1.232 brouard 3541: /* Fixed */
1.224 brouard 3542: /* for (k=1; k<=cptcovn;k++) cov[2+nagesqr+k]=covar[Tvar[k]][i]; */
1.232 brouard 3543: /* for (k=1; k<=ncoveff;k++){ /\* Simple and product fixed Dummy covariates without age* products *\/ */
3544: for (k=1; k<=ncovf;k++){ /* Simple and product fixed covariates without age* products */
3545: 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)*/
3546: /* cov[ioffset+TvarFind[1]]=covar[Tvar[TvarFind[1]]][i]; */
3547: /* cov[2+6]=covar[Tvar[6]][i]; */
3548: /* cov[2+6]=covar[2][i]; V2 */
3549: /* cov[TvarFind[2]]=covar[Tvar[TvarFind[2]]][i]; */
3550: /* cov[2+7]=covar[Tvar[7]][i]; */
3551: /* cov[2+7]=covar[7][i]; V7=V1*V2 */
3552: /* cov[TvarFind[3]]=covar[Tvar[TvarFind[3]]][i]; */
3553: /* cov[2+9]=covar[Tvar[9]][i]; */
3554: /* cov[2+9]=covar[1][i]; V1 */
1.225 brouard 3555: }
1.232 brouard 3556: /* for (k=1; k<=nqfveff;k++){ /\* Simple and product fixed Quantitative covariates without age* products *\/ */
3557: /* 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?)*\/ */
3558: /* } */
1.231 brouard 3559: /* for(iqv=1; iqv <= nqfveff; iqv++){ /\* Quantitative fixed covariates *\/ */
3560: /* cov[++ioffset]=coqvar[Tvar[iqv]][i]; /\* Only V2 k=6 and V1*V2 7 *\/ */
3561: /* } */
1.225 brouard 3562:
1.233 brouard 3563:
3564: for(mi=1; mi<= wav[i]-1; mi++){ /* Varying with waves */
1.232 brouard 3565: /* Wave varying (but not age varying) */
3566: for(k=1; k <= ncovv ; k++){ /* Varying covariates (single and product but no age )*/
1.242 brouard 3567: /* cov[ioffset+TvarVind[k]]=cotvar[mw[mi][i]][Tvar[TvarVind[k]]][i]; */
3568: cov[ioffset+TvarVind[k]]=cotvar[mw[mi][i]][Tvar[TvarVind[k]]-ncovcol-nqv][i];
3569: }
1.232 brouard 3570: /* for(itv=1; itv <= ntveff; itv++){ /\* Varying dummy covariates (single??)*\/ */
1.242 brouard 3571: /* iv= Tvar[Tmodelind[ioffset-2-nagesqr-cptcovage+itv]]-ncovcol-nqv; /\* Counting the # varying covariate from 1 to ntveff *\/ */
3572: /* cov[ioffset+iv]=cotvar[mw[mi][i]][iv][i]; */
3573: /* k=ioffset-2-nagesqr-cptcovage+itv; /\* position in simple model *\/ */
3574: /* cov[ioffset+itv]=cotvar[mw[mi][i]][TmodelInvind[itv]][i]; */
3575: /* 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 3576: /* for(iqtv=1; iqtv <= nqtveff; iqtv++){ /\* Varying quantitatives covariates *\/ */
1.242 brouard 3577: /* iv=TmodelInvQind[iqtv]; /\* Counting the # varying covariate from 1 to ntveff *\/ */
3578: /* /\* 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]); *\/ */
3579: /* cov[ioffset+ntveff+iqtv]=cotqvar[mw[mi][i]][TmodelInvQind[iqtv]][i]; */
1.232 brouard 3580: /* } */
1.126 brouard 3581: for (ii=1;ii<=nlstate+ndeath;ii++)
1.242 brouard 3582: for (j=1;j<=nlstate+ndeath;j++){
3583: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
3584: savm[ii][j]=(ii==j ? 1.0 : 0.0);
3585: }
1.214 brouard 3586:
3587: agebegin=agev[mw[mi][i]][i]; /* Age at beginning of effective wave */
3588: ageend=agev[mw[mi][i]][i] + (dh[mi][i])*stepm/YEARM; /* Age at end of effective wave and at the end of transition */
3589: for(d=0; d<dh[mi][i]; d++){ /* Delay between two effective waves */
1.242 brouard 3590: /*dh[m][i] or dh[mw[mi][i]][i] is the delay between two effective waves m=mw[mi][i]
3591: and mw[mi+1][i]. dh depends on stepm.*/
3592: newm=savm;
3593: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM;
3594: cov[2]=agexact;
3595: if(nagesqr==1)
3596: cov[3]= agexact*agexact;
3597: for (kk=1; kk<=cptcovage;kk++) {
3598: if(!FixedV[Tvar[Tage[kk]]])
3599: cov[Tage[kk]+2+nagesqr]=covar[Tvar[Tage[kk]]][i]*agexact;
3600: else
3601: cov[Tage[kk]+2+nagesqr]=cotvar[mw[mi][i]][Tvar[Tage[kk]]-ncovcol-nqv][i]*agexact;
3602: }
3603: /* printf("i=%d,mi=%d,d=%d,mw[mi][i]=%d\n",i, mi,d,mw[mi][i]); */
3604: /* savm=pmij(pmmij,cov,ncovmodel,x,nlstate); */
3605: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
3606: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
3607: /* out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath, */
3608: /* 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate)); */
3609: savm=oldm;
3610: oldm=newm;
1.126 brouard 3611: } /* end mult */
3612:
3613: s1=s[mw[mi][i]][i];
3614: s2=s[mw[mi+1][i]][i];
1.217 brouard 3615: /* if(s2==-1){ */
3616: /* printf(" s1=%d, s2=%d i=%d \n", s1, s2, i); */
3617: /* /\* exit(1); *\/ */
3618: /* } */
1.126 brouard 3619: bbh=(double)bh[mi][i]/(double)stepm;
3620: /* bias is positive if real duration
3621: * is higher than the multiple of stepm and negative otherwise.
3622: */
3623: if( s2 > nlstate && (mle <5) ){ /* Jackson */
1.242 brouard 3624: lli=log(out[s1][s2] - savm[s1][s2]);
1.216 brouard 3625: } else if ( s2==-1 ) { /* alive */
1.242 brouard 3626: for (j=1,survp=0. ; j<=nlstate; j++)
3627: survp += (1.+bbh)*out[s1][j]- bbh*savm[s1][j];
3628: lli= log(survp);
1.126 brouard 3629: }else if (mle==1){
1.242 brouard 3630: lli= log((1.+bbh)*out[s1][s2]- bbh*savm[s1][s2]); /* linear interpolation */
1.126 brouard 3631: } else if(mle==2){
1.242 brouard 3632: 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 3633: } else if(mle==3){ /* exponential inter-extrapolation */
1.242 brouard 3634: 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 3635: } else if (mle==4){ /* mle=4 no inter-extrapolation */
1.242 brouard 3636: lli=log(out[s1][s2]); /* Original formula */
1.136 brouard 3637: } else{ /* mle=0 back to 1 */
1.242 brouard 3638: lli= log((1.+bbh)*out[s1][s2]- bbh*savm[s1][s2]); /* linear interpolation */
3639: /*lli=log(out[s1][s2]); */ /* Original formula */
1.126 brouard 3640: } /* End of if */
3641: ipmx +=1;
3642: sw += weight[i];
3643: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
1.132 brouard 3644: /*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 3645: if(globpr){
1.242 brouard 3646: fprintf(ficresilk,"%9ld %6.1f %6.1f %6d %2d %2d %2d %2d %3d %15.6f %8.4f %8.3f\
1.126 brouard 3647: %11.6f %11.6f %11.6f ", \
1.242 brouard 3648: num[i], agebegin, ageend, i,s1,s2,mi,mw[mi][i],dh[mi][i],exp(lli),weight[i],weight[i]*gipmx/gsw,
3649: 2*weight[i]*lli,out[s1][s2],savm[s1][s2]);
3650: for(k=1,llt=0.,l=0.; k<=nlstate; k++){
3651: llt +=ll[k]*gipmx/gsw;
3652: fprintf(ficresilk," %10.6f",-ll[k]*gipmx/gsw);
3653: }
3654: fprintf(ficresilk," %10.6f\n", -llt);
1.126 brouard 3655: }
1.232 brouard 3656: } /* end of wave */
3657: } /* end of individual */
3658: for(k=1,l=0.; k<=nlstate; k++) l += ll[k];
3659: /* printf("l1=%f l2=%f ",ll[1],ll[2]); */
3660: l= l*ipmx/sw; /* To get the same order of magnitude as if weight=1 for every body */
3661: if(globpr==0){ /* First time we count the contributions and weights */
3662: gipmx=ipmx;
3663: gsw=sw;
3664: }
3665: return -l;
1.126 brouard 3666: }
3667:
3668:
3669: /*************** function likelione ***********/
3670: void likelione(FILE *ficres,double p[], int npar, int nlstate, int *globpri, long *ipmx, double *sw, double *fretone, double (*funcone)(double []))
3671: {
3672: /* This routine should help understanding what is done with
3673: the selection of individuals/waves and
3674: to check the exact contribution to the likelihood.
3675: Plotting could be done.
3676: */
3677: int k;
3678:
3679: if(*globpri !=0){ /* Just counts and sums, no printings */
1.201 brouard 3680: strcpy(fileresilk,"ILK_");
1.202 brouard 3681: strcat(fileresilk,fileresu);
1.126 brouard 3682: if((ficresilk=fopen(fileresilk,"w"))==NULL) {
3683: printf("Problem with resultfile: %s\n", fileresilk);
3684: fprintf(ficlog,"Problem with resultfile: %s\n", fileresilk);
3685: }
1.214 brouard 3686: 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");
3687: fprintf(ficresilk, "#num_i ageb agend i s1 s2 mi mw dh likeli weight %%weight 2wlli out sav ");
1.126 brouard 3688: /* i,s1,s2,mi,mw[mi][i],dh[mi][i],exp(lli),weight[i],2*weight[i]*lli,out[s1][s2],savm[s1][s2]); */
3689: for(k=1; k<=nlstate; k++)
3690: fprintf(ficresilk," -2*gipw/gsw*weight*ll[%d]++",k);
3691: fprintf(ficresilk," -2*gipw/gsw*weight*ll(total)\n");
3692: }
3693:
3694: *fretone=(*funcone)(p);
3695: if(*globpri !=0){
3696: fclose(ficresilk);
1.205 brouard 3697: if (mle ==0)
3698: fprintf(fichtm,"\n<br>File of contributions to the likelihood computed with initial parameters and mle = %d.",mle);
3699: else if(mle >=1)
3700: fprintf(fichtm,"\n<br>File of contributions to the likelihood computed with optimized parameters mle = %d.",mle);
3701: 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 3702:
1.208 brouard 3703:
3704: for (k=1; k<= nlstate ; k++) {
1.211 brouard 3705: 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 3706: <img src=\"%s-p%dj.png\">",k,k,subdirf2(optionfilefiname,"ILK_"),k,subdirf2(optionfilefiname,"ILK_"),k,subdirf2(optionfilefiname,"ILK_"),k);
3707: }
1.207 brouard 3708: 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 3709: <img src=\"%s-ori.png\">",subdirf2(optionfilefiname,"ILK_"),subdirf2(optionfilefiname,"ILK_"),subdirf2(optionfilefiname,"ILK_"));
1.207 brouard 3710: fprintf(fichtm,"<br>- and by state of destination <a href=\"%s-dest.png\">%s-dest.png</a><br> \
1.204 brouard 3711: <img src=\"%s-dest.png\">",subdirf2(optionfilefiname,"ILK_"),subdirf2(optionfilefiname,"ILK_"),subdirf2(optionfilefiname,"ILK_"));
1.207 brouard 3712: fflush(fichtm);
1.205 brouard 3713: }
1.126 brouard 3714: return;
3715: }
3716:
3717:
3718: /*********** Maximum Likelihood Estimation ***************/
3719:
3720: void mlikeli(FILE *ficres,double p[], int npar, int ncovmodel, int nlstate, double ftol, double (*func)(double []))
3721: {
1.165 brouard 3722: int i,j, iter=0;
1.126 brouard 3723: double **xi;
3724: double fret;
3725: double fretone; /* Only one call to likelihood */
3726: /* char filerespow[FILENAMELENGTH];*/
1.162 brouard 3727:
3728: #ifdef NLOPT
3729: int creturn;
3730: nlopt_opt opt;
3731: /* double lb[9] = { -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL }; /\* lower bounds *\/ */
3732: double *lb;
3733: double minf; /* the minimum objective value, upon return */
3734: double * p1; /* Shifted parameters from 0 instead of 1 */
3735: myfunc_data dinst, *d = &dinst;
3736: #endif
3737:
3738:
1.126 brouard 3739: xi=matrix(1,npar,1,npar);
3740: for (i=1;i<=npar;i++)
3741: for (j=1;j<=npar;j++)
3742: xi[i][j]=(i==j ? 1.0 : 0.0);
3743: printf("Powell\n"); fprintf(ficlog,"Powell\n");
1.201 brouard 3744: strcpy(filerespow,"POW_");
1.126 brouard 3745: strcat(filerespow,fileres);
3746: if((ficrespow=fopen(filerespow,"w"))==NULL) {
3747: printf("Problem with resultfile: %s\n", filerespow);
3748: fprintf(ficlog,"Problem with resultfile: %s\n", filerespow);
3749: }
3750: fprintf(ficrespow,"# Powell\n# iter -2*LL");
3751: for (i=1;i<=nlstate;i++)
3752: for(j=1;j<=nlstate+ndeath;j++)
3753: if(j!=i)fprintf(ficrespow," p%1d%1d",i,j);
3754: fprintf(ficrespow,"\n");
1.162 brouard 3755: #ifdef POWELL
1.126 brouard 3756: powell(p,xi,npar,ftol,&iter,&fret,func);
1.162 brouard 3757: #endif
1.126 brouard 3758:
1.162 brouard 3759: #ifdef NLOPT
3760: #ifdef NEWUOA
3761: opt = nlopt_create(NLOPT_LN_NEWUOA,npar);
3762: #else
3763: opt = nlopt_create(NLOPT_LN_BOBYQA,npar);
3764: #endif
3765: lb=vector(0,npar-1);
3766: for (i=0;i<npar;i++) lb[i]= -HUGE_VAL;
3767: nlopt_set_lower_bounds(opt, lb);
3768: nlopt_set_initial_step1(opt, 0.1);
3769:
3770: p1= (p+1); /* p *(p+1)@8 and p *(p1)@8 are equal p1[0]=p[1] */
3771: d->function = func;
3772: printf(" Func %.12lf \n",myfunc(npar,p1,NULL,d));
3773: nlopt_set_min_objective(opt, myfunc, d);
3774: nlopt_set_xtol_rel(opt, ftol);
3775: if ((creturn=nlopt_optimize(opt, p1, &minf)) < 0) {
3776: printf("nlopt failed! %d\n",creturn);
3777: }
3778: else {
3779: printf("found minimum after %d evaluations (NLOPT=%d)\n", countcallfunc ,NLOPT);
3780: printf("found minimum at f(%g,%g) = %0.10g\n", p[0], p[1], minf);
3781: iter=1; /* not equal */
3782: }
3783: nlopt_destroy(opt);
3784: #endif
1.126 brouard 3785: free_matrix(xi,1,npar,1,npar);
3786: fclose(ficrespow);
1.203 brouard 3787: printf("\n#Number of iterations & function calls = %d & %d, -2 Log likelihood = %.12f\n",iter, countcallfunc,func(p));
3788: fprintf(ficlog,"\n#Number of iterations & function calls = %d & %d, -2 Log likelihood = %.12f\n",iter, countcallfunc,func(p));
1.180 brouard 3789: fprintf(ficres,"#Number of iterations & function calls = %d & %d, -2 Log likelihood = %.12f\n",iter, countcallfunc,func(p));
1.126 brouard 3790:
3791: }
3792:
3793: /**** Computes Hessian and covariance matrix ***/
1.203 brouard 3794: void hesscov(double **matcov, double **hess, double p[], int npar, double delti[], double ftolhess, double (*func)(double []))
1.126 brouard 3795: {
3796: double **a,**y,*x,pd;
1.203 brouard 3797: /* double **hess; */
1.164 brouard 3798: int i, j;
1.126 brouard 3799: int *indx;
3800:
3801: double hessii(double p[], double delta, int theta, double delti[],double (*func)(double []),int npar);
1.203 brouard 3802: double hessij(double p[], double **hess, double delti[], int i, int j,double (*func)(double []),int npar);
1.126 brouard 3803: void lubksb(double **a, int npar, int *indx, double b[]) ;
3804: void ludcmp(double **a, int npar, int *indx, double *d) ;
3805: double gompertz(double p[]);
1.203 brouard 3806: /* hess=matrix(1,npar,1,npar); */
1.126 brouard 3807:
3808: printf("\nCalculation of the hessian matrix. Wait...\n");
3809: fprintf(ficlog,"\nCalculation of the hessian matrix. Wait...\n");
3810: for (i=1;i<=npar;i++){
1.203 brouard 3811: printf("%d-",i);fflush(stdout);
3812: fprintf(ficlog,"%d-",i);fflush(ficlog);
1.126 brouard 3813:
3814: hess[i][i]=hessii(p,ftolhess,i,delti,func,npar);
3815:
3816: /* printf(" %f ",p[i]);
3817: printf(" %lf %lf %lf",hess[i][i],ftolhess,delti[i]);*/
3818: }
3819:
3820: for (i=1;i<=npar;i++) {
3821: for (j=1;j<=npar;j++) {
3822: if (j>i) {
1.203 brouard 3823: printf(".%d-%d",i,j);fflush(stdout);
3824: fprintf(ficlog,".%d-%d",i,j);fflush(ficlog);
3825: hess[i][j]=hessij(p,hess, delti,i,j,func,npar);
1.126 brouard 3826:
3827: hess[j][i]=hess[i][j];
3828: /*printf(" %lf ",hess[i][j]);*/
3829: }
3830: }
3831: }
3832: printf("\n");
3833: fprintf(ficlog,"\n");
3834:
3835: printf("\nInverting the hessian to get the covariance matrix. Wait...\n");
3836: fprintf(ficlog,"\nInverting the hessian to get the covariance matrix. Wait...\n");
3837:
3838: a=matrix(1,npar,1,npar);
3839: y=matrix(1,npar,1,npar);
3840: x=vector(1,npar);
3841: indx=ivector(1,npar);
3842: for (i=1;i<=npar;i++)
3843: for (j=1;j<=npar;j++) a[i][j]=hess[i][j];
3844: ludcmp(a,npar,indx,&pd);
3845:
3846: for (j=1;j<=npar;j++) {
3847: for (i=1;i<=npar;i++) x[i]=0;
3848: x[j]=1;
3849: lubksb(a,npar,indx,x);
3850: for (i=1;i<=npar;i++){
3851: matcov[i][j]=x[i];
3852: }
3853: }
3854:
3855: printf("\n#Hessian matrix#\n");
3856: fprintf(ficlog,"\n#Hessian matrix#\n");
3857: for (i=1;i<=npar;i++) {
3858: for (j=1;j<=npar;j++) {
1.203 brouard 3859: printf("%.6e ",hess[i][j]);
3860: fprintf(ficlog,"%.6e ",hess[i][j]);
1.126 brouard 3861: }
3862: printf("\n");
3863: fprintf(ficlog,"\n");
3864: }
3865:
1.203 brouard 3866: /* printf("\n#Covariance matrix#\n"); */
3867: /* fprintf(ficlog,"\n#Covariance matrix#\n"); */
3868: /* for (i=1;i<=npar;i++) { */
3869: /* for (j=1;j<=npar;j++) { */
3870: /* printf("%.6e ",matcov[i][j]); */
3871: /* fprintf(ficlog,"%.6e ",matcov[i][j]); */
3872: /* } */
3873: /* printf("\n"); */
3874: /* fprintf(ficlog,"\n"); */
3875: /* } */
3876:
1.126 brouard 3877: /* Recompute Inverse */
1.203 brouard 3878: /* for (i=1;i<=npar;i++) */
3879: /* for (j=1;j<=npar;j++) a[i][j]=matcov[i][j]; */
3880: /* ludcmp(a,npar,indx,&pd); */
3881:
3882: /* printf("\n#Hessian matrix recomputed#\n"); */
3883:
3884: /* for (j=1;j<=npar;j++) { */
3885: /* for (i=1;i<=npar;i++) x[i]=0; */
3886: /* x[j]=1; */
3887: /* lubksb(a,npar,indx,x); */
3888: /* for (i=1;i<=npar;i++){ */
3889: /* y[i][j]=x[i]; */
3890: /* printf("%.3e ",y[i][j]); */
3891: /* fprintf(ficlog,"%.3e ",y[i][j]); */
3892: /* } */
3893: /* printf("\n"); */
3894: /* fprintf(ficlog,"\n"); */
3895: /* } */
3896:
3897: /* Verifying the inverse matrix */
3898: #ifdef DEBUGHESS
3899: y=matprod2(y,hess,1,npar,1,npar,1,npar,matcov);
1.126 brouard 3900:
1.203 brouard 3901: printf("\n#Verification: multiplying the matrix of covariance by the Hessian matrix, should be unity:#\n");
3902: fprintf(ficlog,"\n#Verification: multiplying the matrix of covariance by the Hessian matrix. Should be unity:#\n");
1.126 brouard 3903:
3904: for (j=1;j<=npar;j++) {
3905: for (i=1;i<=npar;i++){
1.203 brouard 3906: printf("%.2f ",y[i][j]);
3907: fprintf(ficlog,"%.2f ",y[i][j]);
1.126 brouard 3908: }
3909: printf("\n");
3910: fprintf(ficlog,"\n");
3911: }
1.203 brouard 3912: #endif
1.126 brouard 3913:
3914: free_matrix(a,1,npar,1,npar);
3915: free_matrix(y,1,npar,1,npar);
3916: free_vector(x,1,npar);
3917: free_ivector(indx,1,npar);
1.203 brouard 3918: /* free_matrix(hess,1,npar,1,npar); */
1.126 brouard 3919:
3920:
3921: }
3922:
3923: /*************** hessian matrix ****************/
3924: double hessii(double x[], double delta, int theta, double delti[], double (*func)(double []), int npar)
1.203 brouard 3925: { /* Around values of x, computes the function func and returns the scales delti and hessian */
1.126 brouard 3926: int i;
3927: int l=1, lmax=20;
1.203 brouard 3928: double k1,k2, res, fx;
1.132 brouard 3929: double p2[MAXPARM+1]; /* identical to x */
1.126 brouard 3930: double delt=0.0001, delts, nkhi=10.,nkhif=1., khi=1.e-4;
3931: int k=0,kmax=10;
3932: double l1;
3933:
3934: fx=func(x);
3935: for (i=1;i<=npar;i++) p2[i]=x[i];
1.145 brouard 3936: for(l=0 ; l <=lmax; l++){ /* Enlarging the zone around the Maximum */
1.126 brouard 3937: l1=pow(10,l);
3938: delts=delt;
3939: for(k=1 ; k <kmax; k=k+1){
3940: delt = delta*(l1*k);
3941: p2[theta]=x[theta] +delt;
1.145 brouard 3942: k1=func(p2)-fx; /* Might be negative if too close to the theoretical maximum */
1.126 brouard 3943: p2[theta]=x[theta]-delt;
3944: k2=func(p2)-fx;
3945: /*res= (k1-2.0*fx+k2)/delt/delt; */
1.203 brouard 3946: res= (k1+k2)/delt/delt/2.; /* Divided by 2 because L and not 2*L */
1.126 brouard 3947:
1.203 brouard 3948: #ifdef DEBUGHESSII
1.126 brouard 3949: 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);
3950: 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);
3951: #endif
3952: /*if(fabs(k1-2.0*fx+k2) <1.e-13){ */
3953: if((k1 <khi/nkhi/2.) || (k2 <khi/nkhi/2.)){
3954: k=kmax;
3955: }
3956: else if((k1 >khi/nkhif) || (k2 >khi/nkhif)){ /* Keeps lastvalue before 3.84/2 KHI2 5% 1d.f. */
1.164 brouard 3957: k=kmax; l=lmax*10;
1.126 brouard 3958: }
3959: else if((k1 >khi/nkhi) || (k2 >khi/nkhi)){
3960: delts=delt;
3961: }
1.203 brouard 3962: } /* End loop k */
1.126 brouard 3963: }
3964: delti[theta]=delts;
3965: return res;
3966:
3967: }
3968:
1.203 brouard 3969: double hessij( double x[], double **hess, double delti[], int thetai,int thetaj,double (*func)(double []),int npar)
1.126 brouard 3970: {
3971: int i;
1.164 brouard 3972: int l=1, lmax=20;
1.126 brouard 3973: double k1,k2,k3,k4,res,fx;
1.132 brouard 3974: double p2[MAXPARM+1];
1.203 brouard 3975: int k, kmax=1;
3976: double v1, v2, cv12, lc1, lc2;
1.208 brouard 3977:
3978: int firstime=0;
1.203 brouard 3979:
1.126 brouard 3980: fx=func(x);
1.203 brouard 3981: for (k=1; k<=kmax; k=k+10) {
1.126 brouard 3982: for (i=1;i<=npar;i++) p2[i]=x[i];
1.203 brouard 3983: p2[thetai]=x[thetai]+delti[thetai]*k;
3984: p2[thetaj]=x[thetaj]+delti[thetaj]*k;
1.126 brouard 3985: k1=func(p2)-fx;
3986:
1.203 brouard 3987: p2[thetai]=x[thetai]+delti[thetai]*k;
3988: p2[thetaj]=x[thetaj]-delti[thetaj]*k;
1.126 brouard 3989: k2=func(p2)-fx;
3990:
1.203 brouard 3991: p2[thetai]=x[thetai]-delti[thetai]*k;
3992: p2[thetaj]=x[thetaj]+delti[thetaj]*k;
1.126 brouard 3993: k3=func(p2)-fx;
3994:
1.203 brouard 3995: p2[thetai]=x[thetai]-delti[thetai]*k;
3996: p2[thetaj]=x[thetaj]-delti[thetaj]*k;
1.126 brouard 3997: k4=func(p2)-fx;
1.203 brouard 3998: res=(k1-k2-k3+k4)/4.0/delti[thetai]/k/delti[thetaj]/k/2.; /* Because of L not 2*L */
3999: if(k1*k2*k3*k4 <0.){
1.208 brouard 4000: firstime=1;
1.203 brouard 4001: kmax=kmax+10;
1.208 brouard 4002: }
4003: if(kmax >=10 || firstime ==1){
1.218 brouard 4004: 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);
4005: 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 4006: 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);
4007: 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);
4008: }
4009: #ifdef DEBUGHESSIJ
4010: v1=hess[thetai][thetai];
4011: v2=hess[thetaj][thetaj];
4012: cv12=res;
4013: /* Computing eigen value of Hessian matrix */
4014: lc1=((v1+v2)+sqrt((v1+v2)*(v1+v2) - 4*(v1*v2-cv12*cv12)))/2.;
4015: lc2=((v1+v2)-sqrt((v1+v2)*(v1+v2) - 4*(v1*v2-cv12*cv12)))/2.;
4016: if ((lc2 <0) || (lc1 <0) ){
4017: printf("Warning: sub Hessian matrix '%d%d' does not have positive eigen values \n",thetai,thetaj);
4018: fprintf(ficlog, "Warning: sub Hessian matrix '%d%d' does not have positive eigen values \n",thetai,thetaj);
4019: 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);
4020: 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);
4021: }
1.126 brouard 4022: #endif
4023: }
4024: return res;
4025: }
4026:
1.203 brouard 4027: /* Not done yet: Was supposed to fix if not exactly at the maximum */
4028: /* double hessij( double x[], double delti[], int thetai,int thetaj,double (*func)(double []),int npar) */
4029: /* { */
4030: /* int i; */
4031: /* int l=1, lmax=20; */
4032: /* double k1,k2,k3,k4,res,fx; */
4033: /* double p2[MAXPARM+1]; */
4034: /* double delt=0.0001, delts, nkhi=10.,nkhif=1., khi=1.e-4; */
4035: /* int k=0,kmax=10; */
4036: /* double l1; */
4037:
4038: /* fx=func(x); */
4039: /* for(l=0 ; l <=lmax; l++){ /\* Enlarging the zone around the Maximum *\/ */
4040: /* l1=pow(10,l); */
4041: /* delts=delt; */
4042: /* for(k=1 ; k <kmax; k=k+1){ */
4043: /* delt = delti*(l1*k); */
4044: /* for (i=1;i<=npar;i++) p2[i]=x[i]; */
4045: /* p2[thetai]=x[thetai]+delti[thetai]/k; */
4046: /* p2[thetaj]=x[thetaj]+delti[thetaj]/k; */
4047: /* k1=func(p2)-fx; */
4048:
4049: /* p2[thetai]=x[thetai]+delti[thetai]/k; */
4050: /* p2[thetaj]=x[thetaj]-delti[thetaj]/k; */
4051: /* k2=func(p2)-fx; */
4052:
4053: /* p2[thetai]=x[thetai]-delti[thetai]/k; */
4054: /* p2[thetaj]=x[thetaj]+delti[thetaj]/k; */
4055: /* k3=func(p2)-fx; */
4056:
4057: /* p2[thetai]=x[thetai]-delti[thetai]/k; */
4058: /* p2[thetaj]=x[thetaj]-delti[thetaj]/k; */
4059: /* k4=func(p2)-fx; */
4060: /* res=(k1-k2-k3+k4)/4.0/delti[thetai]*k/delti[thetaj]*k/2.; /\* Because of L not 2*L *\/ */
4061: /* #ifdef DEBUGHESSIJ */
4062: /* 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); */
4063: /* 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); */
4064: /* #endif */
4065: /* if((k1 <khi/nkhi/2.) || (k2 <khi/nkhi/2.)|| (k4 <khi/nkhi/2.)|| (k4 <khi/nkhi/2.)){ */
4066: /* k=kmax; */
4067: /* } */
4068: /* else if((k1 >khi/nkhif) || (k2 >khi/nkhif) || (k4 >khi/nkhif) || (k4 >khi/nkhif)){ /\* Keeps lastvalue before 3.84/2 KHI2 5% 1d.f. *\/ */
4069: /* k=kmax; l=lmax*10; */
4070: /* } */
4071: /* else if((k1 >khi/nkhi) || (k2 >khi/nkhi)){ */
4072: /* delts=delt; */
4073: /* } */
4074: /* } /\* End loop k *\/ */
4075: /* } */
4076: /* delti[theta]=delts; */
4077: /* return res; */
4078: /* } */
4079:
4080:
1.126 brouard 4081: /************** Inverse of matrix **************/
4082: void ludcmp(double **a, int n, int *indx, double *d)
4083: {
4084: int i,imax,j,k;
4085: double big,dum,sum,temp;
4086: double *vv;
4087:
4088: vv=vector(1,n);
4089: *d=1.0;
4090: for (i=1;i<=n;i++) {
4091: big=0.0;
4092: for (j=1;j<=n;j++)
4093: if ((temp=fabs(a[i][j])) > big) big=temp;
4094: if (big == 0.0) nrerror("Singular matrix in routine ludcmp");
4095: vv[i]=1.0/big;
4096: }
4097: for (j=1;j<=n;j++) {
4098: for (i=1;i<j;i++) {
4099: sum=a[i][j];
4100: for (k=1;k<i;k++) sum -= a[i][k]*a[k][j];
4101: a[i][j]=sum;
4102: }
4103: big=0.0;
4104: for (i=j;i<=n;i++) {
4105: sum=a[i][j];
4106: for (k=1;k<j;k++)
4107: sum -= a[i][k]*a[k][j];
4108: a[i][j]=sum;
4109: if ( (dum=vv[i]*fabs(sum)) >= big) {
4110: big=dum;
4111: imax=i;
4112: }
4113: }
4114: if (j != imax) {
4115: for (k=1;k<=n;k++) {
4116: dum=a[imax][k];
4117: a[imax][k]=a[j][k];
4118: a[j][k]=dum;
4119: }
4120: *d = -(*d);
4121: vv[imax]=vv[j];
4122: }
4123: indx[j]=imax;
4124: if (a[j][j] == 0.0) a[j][j]=TINY;
4125: if (j != n) {
4126: dum=1.0/(a[j][j]);
4127: for (i=j+1;i<=n;i++) a[i][j] *= dum;
4128: }
4129: }
4130: free_vector(vv,1,n); /* Doesn't work */
4131: ;
4132: }
4133:
4134: void lubksb(double **a, int n, int *indx, double b[])
4135: {
4136: int i,ii=0,ip,j;
4137: double sum;
4138:
4139: for (i=1;i<=n;i++) {
4140: ip=indx[i];
4141: sum=b[ip];
4142: b[ip]=b[i];
4143: if (ii)
4144: for (j=ii;j<=i-1;j++) sum -= a[i][j]*b[j];
4145: else if (sum) ii=i;
4146: b[i]=sum;
4147: }
4148: for (i=n;i>=1;i--) {
4149: sum=b[i];
4150: for (j=i+1;j<=n;j++) sum -= a[i][j]*b[j];
4151: b[i]=sum/a[i][i];
4152: }
4153: }
4154:
4155: void pstamp(FILE *fichier)
4156: {
1.196 brouard 4157: fprintf(fichier,"# %s.%s\n#IMaCh version %s, %s\n#%s\n# %s", optionfilefiname,optionfilext,version,copyright, fullversion, strstart);
1.126 brouard 4158: }
4159:
4160: /************ Frequencies ********************/
1.226 brouard 4161: void freqsummary(char fileres[], int iagemin, int iagemax, int **s, double **agev, int nlstate, int imx, \
4162: int *Tvaraff, int *invalidvarcomb, int **nbcode, int *ncodemax,double **mint,double **anint, char strstart[], \
4163: int firstpass, int lastpass, int stepm, int weightopt, char model[])
4164: { /* Some frequencies */
4165:
1.227 brouard 4166: int i, m, jk, j1, bool, z1,j, k, iv;
1.226 brouard 4167: int iind=0, iage=0;
4168: int mi; /* Effective wave */
4169: int first;
4170: double ***freq; /* Frequencies */
4171: double *meanq;
4172: double **meanqt;
4173: double *pp, **prop, *posprop, *pospropt;
4174: double pos=0., posproptt=0., pospropta=0., k2, dateintsum=0,k2cpt=0;
4175: char fileresp[FILENAMELENGTH], fileresphtm[FILENAMELENGTH], fileresphtmfr[FILENAMELENGTH];
4176: double agebegin, ageend;
4177:
4178: pp=vector(1,nlstate);
4179: prop=matrix(1,nlstate,iagemin-AGEMARGE,iagemax+3+AGEMARGE);
4180: posprop=vector(1,nlstate); /* Counting the number of transition starting from a live state per age */
4181: pospropt=vector(1,nlstate); /* Counting the number of transition starting from a live state */
4182: /* prop=matrix(1,nlstate,iagemin,iagemax+3); */
4183: meanq=vector(1,nqfveff); /* Number of Quantitative Fixed Variables Effective */
4184: meanqt=matrix(1,lastpass,1,nqtveff);
4185: strcpy(fileresp,"P_");
4186: strcat(fileresp,fileresu);
4187: /*strcat(fileresphtm,fileresu);*/
4188: if((ficresp=fopen(fileresp,"w"))==NULL) {
4189: printf("Problem with prevalence resultfile: %s\n", fileresp);
4190: fprintf(ficlog,"Problem with prevalence resultfile: %s\n", fileresp);
4191: exit(0);
4192: }
1.240 brouard 4193:
1.226 brouard 4194: strcpy(fileresphtm,subdirfext(optionfilefiname,"PHTM_",".htm"));
4195: if((ficresphtm=fopen(fileresphtm,"w"))==NULL) {
4196: printf("Problem with prevalence HTM resultfile '%s' with errno='%s'\n",fileresphtm,strerror(errno));
4197: fprintf(ficlog,"Problem with prevalence HTM resultfile '%s' with errno='%s'\n",fileresphtm,strerror(errno));
4198: fflush(ficlog);
4199: exit(70);
4200: }
4201: else{
4202: fprintf(ficresphtm,"<html><head>\n<title>IMaCh PHTM_ %s</title></head>\n <body><font size=\"2\">%s <br> %s</font> \
1.240 brouard 4203: <hr size=\"2\" color=\"#EC5E5E\"> \n \
1.214 brouard 4204: Title=%s <br>Datafile=%s Firstpass=%d Lastpass=%d Stepm=%d Weight=%d Model=1+age+%s<br>\n",\
1.226 brouard 4205: fileresphtm,version,fullversion,title,datafile,firstpass,lastpass,stepm, weightopt, model);
4206: }
1.237 brouard 4207: 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.240 brouard 4208:
1.226 brouard 4209: strcpy(fileresphtmfr,subdirfext(optionfilefiname,"PHTMFR_",".htm"));
4210: if((ficresphtmfr=fopen(fileresphtmfr,"w"))==NULL) {
4211: printf("Problem with frequency table HTM resultfile '%s' with errno='%s'\n",fileresphtmfr,strerror(errno));
4212: fprintf(ficlog,"Problem with frequency table HTM resultfile '%s' with errno='%s'\n",fileresphtmfr,strerror(errno));
4213: fflush(ficlog);
4214: exit(70);
1.240 brouard 4215: } else{
1.226 brouard 4216: fprintf(ficresphtmfr,"<html><head>\n<title>IMaCh PHTM_Frequency table %s</title></head>\n <body><font size=\"2\">%s <br> %s</font> \
1.240 brouard 4217: <hr size=\"2\" color=\"#EC5E5E\"> \n \
1.214 brouard 4218: Title=%s <br>Datafile=%s Firstpass=%d Lastpass=%d Stepm=%d Weight=%d Model=1+age+%s<br>\n",\
1.226 brouard 4219: fileresphtmfr,version,fullversion,title,datafile,firstpass,lastpass,stepm, weightopt, model);
4220: }
1.240 brouard 4221: fprintf(ficresphtmfr,"Current page is file <a href=\"%s\">%s</a><br>\n\n<h4>Frequencies of all effective transitions of the model, by age at begin of transition, and covariate value at the begin of transition (if the covariate is a varying covariate) </h4>Unknown status is -1<br/>\n",fileresphtmfr, fileresphtmfr);
4222:
1.226 brouard 4223: freq= ma3x(-5,nlstate+ndeath,-5,nlstate+ndeath,iagemin-AGEMARGE,iagemax+3+AGEMARGE);
4224: j1=0;
1.126 brouard 4225:
1.227 brouard 4226: /* j=ncoveff; /\* Only fixed dummy covariates *\/ */
4227: j=cptcoveff; /* Only dummy covariates of the model */
1.226 brouard 4228: if (cptcovn<1) {j=1;ncodemax[1]=1;}
1.240 brouard 4229:
1.226 brouard 4230: first=1;
1.240 brouard 4231:
1.226 brouard 4232: /* Detects if a combination j1 is empty: for a multinomial variable like 3 education levels:
4233: reference=low_education V1=0,V2=0
4234: med_educ V1=1 V2=0,
4235: high_educ V1=0 V2=1
4236: Then V1=1 and V2=1 is a noisy combination that we want to exclude for the list 2**cptcoveff
4237: */
1.240 brouard 4238:
1.227 brouard 4239: 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 4240: posproptt=0.;
4241: /*printf("cptcoveff=%d Tvaraff=%d", cptcoveff,Tvaraff[1]);
4242: scanf("%d", i);*/
4243: for (i=-5; i<=nlstate+ndeath; i++)
4244: for (jk=-5; jk<=nlstate+ndeath; jk++)
1.240 brouard 4245: for(m=iagemin; m <= iagemax+3; m++)
4246: freq[i][jk][m]=0;
4247:
1.226 brouard 4248: for (i=1; i<=nlstate; i++) {
4249: for(m=iagemin; m <= iagemax+3; m++)
1.240 brouard 4250: prop[i][m]=0;
1.226 brouard 4251: posprop[i]=0;
4252: pospropt[i]=0;
4253: }
1.227 brouard 4254: /* for (z1=1; z1<= nqfveff; z1++) { */
4255: /* meanq[z1]+=0.; */
4256: /* for(m=1;m<=lastpass;m++){ */
4257: /* meanqt[m][z1]=0.; */
4258: /* } */
4259: /* } */
1.240 brouard 4260:
1.226 brouard 4261: dateintsum=0;
4262: k2cpt=0;
1.227 brouard 4263: /* For that combination of covariate j1, we count and print the frequencies in one pass */
1.226 brouard 4264: for (iind=1; iind<=imx; iind++) { /* For each individual iind */
4265: bool=1;
1.227 brouard 4266: if(anyvaryingduminmodel==0){ /* If All fixed covariates */
1.234 brouard 4267: if (cptcoveff >0) { /* Filter is here: Must be looked at for model=V1+V2+V3+V4 */
1.227 brouard 4268: /* for (z1=1; z1<= nqfveff; z1++) { */
4269: /* meanq[z1]+=coqvar[Tvar[z1]][iind]; /\* Computes mean of quantitative with selected filter *\/ */
4270: /* } */
1.234 brouard 4271: for (z1=1; z1<=cptcoveff; z1++) {
4272: /* if(Tvaraff[z1] ==-20){ */
4273: /* /\* sumnew+=cotvar[mw[mi][iind]][z1][iind]; *\/ */
4274: /* }else if(Tvaraff[z1] ==-10){ */
4275: /* /\* sumnew+=coqvar[z1][iind]; *\/ */
4276: /* }else */
4277: if (covar[Tvaraff[z1]][iind]!= nbcode[Tvaraff[z1]][codtabm(j1,z1)]){
4278: /* Tests if this individual iind responded to j1 (V4=1 V3=0) */
4279: bool=0;
4280: /* 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",
4281: bool,i,z1, z1, Tvaraff[z1],i,covar[Tvaraff[z1]][i],j1,z1,codtabm(j1,z1),
4282: j1,z1,nbcode[Tvaraff[z1]][codtabm(j1,z1)],j1);*/
4283: /* For j1=7 in V1+V2+V3+V4 = 0 1 1 0 and codtabm(7,3)=1 and nbcde[3][?]=1*/
4284: } /* Onlyf fixed */
4285: } /* end z1 */
4286: } /* cptcovn > 0 */
1.227 brouard 4287: } /* end any */
4288: if (bool==1){ /* We selected an individual iind satisfying combination j1 or all fixed */
1.234 brouard 4289: /* for(m=firstpass; m<=lastpass; m++){ */
4290: for(mi=1; mi<wav[iind];mi++){ /* For that wave */
4291: m=mw[mi][iind];
4292: if(anyvaryingduminmodel==1){ /* Some are varying covariates */
4293: for (z1=1; z1<=cptcoveff; z1++) {
4294: if( Fixed[Tmodelind[z1]]==1){
4295: iv= Tvar[Tmodelind[z1]]-ncovcol-nqv;
4296: if (cotvar[m][iv][iind]!= nbcode[Tvaraff[z1]][codtabm(j1,z1)]) /* iv=1 to ntv, right modality */
4297: bool=0;
4298: }else if( Fixed[Tmodelind[z1]]== 0) { /* fixed */
4299: if (covar[Tvaraff[z1]][iind]!= nbcode[Tvaraff[z1]][codtabm(j1,z1)]) {
4300: bool=0;
4301: }
4302: }
4303: }
4304: }/* Some are varying covariates, we tried to speed up if all fixed covariates in the model, avoiding waves loop */
4305: /* bool =0 we keep that guy which corresponds to the combination of dummy values */
4306: if(bool==1){
4307: /* dh[m][iind] or dh[mw[mi][iind]][iind] is the delay between two effective (mi) waves m=mw[mi][iind]
4308: and mw[mi+1][iind]. dh depends on stepm. */
4309: agebegin=agev[m][iind]; /* Age at beginning of wave before transition*/
4310: ageend=agev[m][iind]+(dh[m][iind])*stepm/YEARM; /* Age at end of wave and transition */
4311: if(m >=firstpass && m <=lastpass){
4312: k2=anint[m][iind]+(mint[m][iind]/12.);
4313: /*if ((k2>=dateprev1) && (k2<=dateprev2)) {*/
4314: if(agev[m][iind]==0) agev[m][iind]=iagemax+1; /* All ages equal to 0 are in iagemax+1 */
4315: if(agev[m][iind]==1) agev[m][iind]=iagemax+2; /* All ages equal to 1 are in iagemax+2 */
4316: if (s[m][iind]>0 && s[m][iind]<=nlstate) /* If status at wave m is known and a live state */
4317: prop[s[m][iind]][(int)agev[m][iind]] += weight[iind]; /* At age of beginning of transition, where status is known */
4318: if (m<lastpass) {
4319: /* if(s[m][iind]==4 && s[m+1][iind]==4) */
4320: /* 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]); */
4321: if(s[m][iind]==-1)
4322: 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.));
4323: freq[s[m][iind]][s[m+1][iind]][(int)agev[m][iind]] += weight[iind]; /* At age of beginning of transition, where status is known */
4324: /* freq[s[m][iind]][s[m+1][iind]][(int)((agebegin+ageend)/2.)] += weight[iind]; */
4325: 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 */
4326: }
4327: } /* end if between passes */
4328: if ((agev[m][iind]>1) && (agev[m][iind]< (iagemax+3)) && (anint[m][iind]!=9999) && (mint[m][iind]!=99)) {
4329: dateintsum=dateintsum+k2;
4330: k2cpt++;
4331: /* printf("iind=%ld dateintmean = %lf dateintsum=%lf k2cpt=%lf k2=%lf\n",iind, dateintsum/k2cpt, dateintsum,k2cpt, k2); */
4332: }
4333: } /* end bool 2 */
4334: } /* end m */
1.226 brouard 4335: } /* end bool */
4336: } /* end iind = 1 to imx */
4337: /* prop[s][age] is feeded for any initial and valid live state as well as
4338: freq[s1][s2][age] at single age of beginning the transition, for a combination j1 */
1.240 brouard 4339:
4340:
1.226 brouard 4341: /* fprintf(ficresp, "#Count between %.lf/%.lf/%.lf and %.lf/%.lf/%.lf\n",jprev1, mprev1,anprev1,jprev2, mprev2,anprev2);*/
4342: pstamp(ficresp);
1.240 brouard 4343: if (cptcoveff>0){
1.226 brouard 4344: fprintf(ficresp, "\n#********** Variable ");
4345: fprintf(ficresphtm, "\n<br/><br/><h3>********** Variable ");
4346: fprintf(ficresphtmfr, "\n<br/><br/><h3>********** Variable ");
1.240 brouard 4347: fprintf(ficlog, "\n#********** Variable ");
1.227 brouard 4348: for (z1=1; z1<=cptcoveff; z1++){
1.240 brouard 4349: if(DummyV[z1]){
4350: fprintf(ficresp, "V%d (fixed)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
4351: fprintf(ficresphtm, "V%d (fixed)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
4352: fprintf(ficresphtmfr, "V%d (fixed)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
4353: fprintf(ficlog, "V%d (fixed)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
4354: }else{
4355: fprintf(ficresp, "V%d(varying)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
4356: fprintf(ficresphtm, "V%d(varying)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
4357: fprintf(ficresphtmfr, "V%d(varying)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
4358: fprintf(ficlog, "V%d(varying)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
4359: }
1.226 brouard 4360: }
4361: fprintf(ficresp, "**********\n#");
4362: fprintf(ficresphtm, "**********</h3>\n");
4363: fprintf(ficresphtmfr, "**********</h3>\n");
4364: fprintf(ficlog, "**********\n");
4365: }
4366: fprintf(ficresphtm,"<table style=\"text-align:center; border: 1px solid\">");
4367: for(i=1; i<=nlstate;i++) {
1.240 brouard 4368: fprintf(ficresp, " Age Prev(%d) N(%d) N ",i,i);
1.226 brouard 4369: fprintf(ficresphtm, "<th>Age</th><th>Prev(%d)</th><th>N(%d)</th><th>N</th>",i,i);
4370: }
4371: fprintf(ficresp, "\n");
4372: fprintf(ficresphtm, "\n");
1.240 brouard 4373:
1.226 brouard 4374: /* Header of frequency table by age */
4375: fprintf(ficresphtmfr,"<table style=\"text-align:center; border: 1px solid\">");
4376: fprintf(ficresphtmfr,"<th>Age</th> ");
4377: for(jk=-1; jk <=nlstate+ndeath; jk++){
4378: for(m=-1; m <=nlstate+ndeath; m++){
1.234 brouard 4379: if(jk!=0 && m!=0)
4380: fprintf(ficresphtmfr,"<th>%d%d</th> ",jk,m);
1.226 brouard 4381: }
4382: }
4383: fprintf(ficresphtmfr, "\n");
1.240 brouard 4384:
1.226 brouard 4385: /* For each age */
4386: for(iage=iagemin; iage <= iagemax+3; iage++){
4387: fprintf(ficresphtm,"<tr>");
4388: if(iage==iagemax+1){
1.240 brouard 4389: fprintf(ficlog,"1");
4390: fprintf(ficresphtmfr,"<tr><th>0</th> ");
1.226 brouard 4391: }else if(iage==iagemax+2){
1.240 brouard 4392: fprintf(ficlog,"0");
4393: fprintf(ficresphtmfr,"<tr><th>Unknown</th> ");
1.226 brouard 4394: }else if(iage==iagemax+3){
1.240 brouard 4395: fprintf(ficlog,"Total");
4396: fprintf(ficresphtmfr,"<tr><th>Total</th> ");
1.226 brouard 4397: }else{
1.240 brouard 4398: if(first==1){
4399: first=0;
4400: printf("See log file for details...\n");
4401: }
4402: fprintf(ficresphtmfr,"<tr><th>%d</th> ",iage);
4403: fprintf(ficlog,"Age %d", iage);
1.226 brouard 4404: }
4405: for(jk=1; jk <=nlstate ; jk++){
1.240 brouard 4406: for(m=-1, pp[jk]=0; m <=nlstate+ndeath ; m++)
4407: pp[jk] += freq[jk][m][iage];
1.226 brouard 4408: }
4409: for(jk=1; jk <=nlstate ; jk++){
1.240 brouard 4410: for(m=-1, pos=0; m <=0 ; m++)
4411: pos += freq[jk][m][iage];
4412: if(pp[jk]>=1.e-10){
4413: if(first==1){
4414: printf(" %d.=%.0f loss[%d]=%.1f%%",jk,pp[jk],jk,100*pos/pp[jk]);
4415: }
4416: fprintf(ficlog," %d.=%.0f loss[%d]=%.1f%%",jk,pp[jk],jk,100*pos/pp[jk]);
4417: }else{
4418: if(first==1)
4419: printf(" %d.=%.0f loss[%d]=NaNQ%%",jk,pp[jk],jk);
4420: fprintf(ficlog," %d.=%.0f loss[%d]=NaNQ%%",jk,pp[jk],jk);
4421: }
1.226 brouard 4422: }
1.240 brouard 4423:
1.226 brouard 4424: for(jk=1; jk <=nlstate ; jk++){
1.240 brouard 4425: /* posprop[jk]=0; */
4426: for(m=0, pp[jk]=0; m <=nlstate+ndeath; m++)/* Summing on all ages */
4427: pp[jk] += freq[jk][m][iage];
1.226 brouard 4428: } /* pp[jk] is the total number of transitions starting from state jk and any ending status until this age */
1.240 brouard 4429:
1.226 brouard 4430: for(jk=1,pos=0, pospropta=0.; jk <=nlstate ; jk++){
1.240 brouard 4431: pos += pp[jk]; /* pos is the total number of transitions until this age */
4432: posprop[jk] += prop[jk][iage]; /* prop is the number of transitions from a live state
4433: from jk at age iage prop[s[m][iind]][(int)agev[m][iind]] += weight[iind] */
4434: pospropta += prop[jk][iage]; /* prop is the number of transitions from a live state
4435: from jk at age iage prop[s[m][iind]][(int)agev[m][iind]] += weight[iind] */
1.226 brouard 4436: }
4437: for(jk=1; jk <=nlstate ; jk++){
1.240 brouard 4438: if(pos>=1.e-5){
4439: if(first==1)
4440: printf(" %d.=%.0f prev[%d]=%.1f%%",jk,pp[jk],jk,100*pp[jk]/pos);
4441: fprintf(ficlog," %d.=%.0f prev[%d]=%.1f%%",jk,pp[jk],jk,100*pp[jk]/pos);
4442: }else{
4443: if(first==1)
4444: printf(" %d.=%.0f prev[%d]=NaNQ%%",jk,pp[jk],jk);
4445: fprintf(ficlog," %d.=%.0f prev[%d]=NaNQ%%",jk,pp[jk],jk);
4446: }
4447: if( iage <= iagemax){
4448: if(pos>=1.e-5){
4449: fprintf(ficresp," %d %.5f %.0f %.0f",iage,prop[jk][iage]/pospropta, prop[jk][iage],pospropta);
4450: fprintf(ficresphtm,"<th>%d</th><td>%.5f</td><td>%.0f</td><td>%.0f</td>",iage,prop[jk][iage]/pospropta, prop[jk][iage],pospropta);
4451: /*probs[iage][jk][j1]= pp[jk]/pos;*/
4452: /*printf("\niage=%d jk=%d j1=%d %.5f %.0f %.0f %f",iage,jk,j1,pp[jk]/pos, pp[jk],pos,probs[iage][jk][j1]);*/
4453: }
4454: else{
4455: fprintf(ficresp," %d NaNq %.0f %.0f",iage,prop[jk][iage],pospropta);
4456: fprintf(ficresphtm,"<th>%d</th><td>NaNq</td><td>%.0f</td><td>%.0f</td>",iage, prop[jk][iage],pospropta);
4457: }
4458: }
4459: pospropt[jk] +=posprop[jk];
1.226 brouard 4460: } /* end loop jk */
4461: /* pospropt=0.; */
4462: for(jk=-1; jk <=nlstate+ndeath; jk++){
1.240 brouard 4463: for(m=-1; m <=nlstate+ndeath; m++){
4464: if(freq[jk][m][iage] !=0 ) { /* minimizing output */
4465: if(first==1){
4466: printf(" %d%d=%.0f",jk,m,freq[jk][m][iage]);
4467: }
4468: fprintf(ficlog," %d%d=%.0f",jk,m,freq[jk][m][iage]);
4469: }
4470: if(jk!=0 && m!=0)
4471: fprintf(ficresphtmfr,"<td>%.0f</td> ",freq[jk][m][iage]);
4472: }
1.226 brouard 4473: } /* end loop jk */
4474: posproptt=0.;
4475: for(jk=1; jk <=nlstate; jk++){
1.240 brouard 4476: posproptt += pospropt[jk];
1.226 brouard 4477: }
4478: fprintf(ficresphtmfr,"</tr>\n ");
4479: if(iage <= iagemax){
1.240 brouard 4480: fprintf(ficresp,"\n");
4481: fprintf(ficresphtm,"</tr>\n");
1.226 brouard 4482: }
4483: if(first==1)
1.240 brouard 4484: printf("Others in log...\n");
1.226 brouard 4485: fprintf(ficlog,"\n");
4486: } /* end loop age iage */
4487: fprintf(ficresphtm,"<tr><th>Tot</th>");
4488: for(jk=1; jk <=nlstate ; jk++){
4489: if(posproptt < 1.e-5){
1.240 brouard 4490: fprintf(ficresphtm,"<td>Nanq</td><td>%.0f</td><td>%.0f</td>",pospropt[jk],posproptt);
1.226 brouard 4491: }else{
1.240 brouard 4492: fprintf(ficresphtm,"<td>%.5f</td><td>%.0f</td><td>%.0f</td>",pospropt[jk]/posproptt,pospropt[jk],posproptt);
1.226 brouard 4493: }
4494: }
4495: fprintf(ficresphtm,"</tr>\n");
4496: fprintf(ficresphtm,"</table>\n");
4497: fprintf(ficresphtmfr,"</table>\n");
4498: if(posproptt < 1.e-5){
4499: fprintf(ficresphtm,"\n <p><b> This combination (%d) is not valid and no result will be produced</b></p>",j1);
4500: fprintf(ficresphtmfr,"\n <p><b> This combination (%d) is not valid and no result will be produced</b></p>",j1);
4501: fprintf(ficres,"\n This combination (%d) is not valid and no result will be produced\n\n",j1);
4502: invalidvarcomb[j1]=1;
4503: }else{
4504: fprintf(ficresphtm,"\n <p> This combination (%d) is valid and result will be produced.</p>",j1);
4505: invalidvarcomb[j1]=0;
4506: }
4507: fprintf(ficresphtmfr,"</table>\n");
4508: } /* end selected combination of covariate j1 */
4509: dateintmean=dateintsum/k2cpt;
1.240 brouard 4510:
1.226 brouard 4511: fclose(ficresp);
4512: fclose(ficresphtm);
4513: fclose(ficresphtmfr);
4514: free_vector(meanq,1,nqfveff);
4515: free_matrix(meanqt,1,lastpass,1,nqtveff);
4516: free_ma3x(freq,-5,nlstate+ndeath,-5,nlstate+ndeath, iagemin-AGEMARGE, iagemax+3+AGEMARGE);
4517: free_vector(pospropt,1,nlstate);
4518: free_vector(posprop,1,nlstate);
4519: free_matrix(prop,1,nlstate,iagemin-AGEMARGE, iagemax+3+AGEMARGE);
4520: free_vector(pp,1,nlstate);
4521: /* End of freqsummary */
4522: }
1.126 brouard 4523:
4524: /************ Prevalence ********************/
1.227 brouard 4525: 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)
4526: {
4527: /* Compute observed prevalence between dateprev1 and dateprev2 by counting the number of people
4528: in each health status at the date of interview (if between dateprev1 and dateprev2).
4529: We still use firstpass and lastpass as another selection.
4530: */
1.126 brouard 4531:
1.227 brouard 4532: int i, m, jk, j1, bool, z1,j, iv;
4533: int mi; /* Effective wave */
4534: int iage;
4535: double agebegin, ageend;
4536:
4537: double **prop;
4538: double posprop;
4539: double y2; /* in fractional years */
4540: int iagemin, iagemax;
4541: int first; /** to stop verbosity which is redirected to log file */
4542:
4543: iagemin= (int) agemin;
4544: iagemax= (int) agemax;
4545: /*pp=vector(1,nlstate);*/
4546: prop=matrix(1,nlstate,iagemin-AGEMARGE,iagemax+3+AGEMARGE);
4547: /* freq=ma3x(-1,nlstate+ndeath,-1,nlstate+ndeath,iagemin,iagemax+3);*/
4548: j1=0;
1.222 brouard 4549:
1.227 brouard 4550: /*j=cptcoveff;*/
4551: if (cptcovn<1) {j=1;ncodemax[1]=1;}
1.222 brouard 4552:
1.227 brouard 4553: first=1;
4554: for(j1=1; j1<= (int) pow(2,cptcoveff);j1++){ /* For each combination of covariate */
4555: for (i=1; i<=nlstate; i++)
4556: for(iage=iagemin-AGEMARGE; iage <= iagemax+3+AGEMARGE; iage++)
4557: prop[i][iage]=0.0;
4558: printf("Prevalence combination of varying and fixed dummies %d\n",j1);
4559: /* fprintf(ficlog," V%d=%d ",Tvaraff[j1],nbcode[Tvaraff[j1]][codtabm(k,j1)]); */
4560: fprintf(ficlog,"Prevalence combination of varying and fixed dummies %d\n",j1);
4561:
4562: for (i=1; i<=imx; i++) { /* Each individual */
4563: bool=1;
4564: /* for(m=firstpass; m<=lastpass; m++){/\* Other selection (we can limit to certain interviews*\/ */
4565: for(mi=1; mi<wav[i];mi++){ /* For this wave too look where individual can be counted V4=0 V3=0 */
4566: m=mw[mi][i];
4567: /* Tmodelind[z1]=k is the position of the varying covariate in the model, but which # within 1 to ntv? */
4568: /* Tvar[Tmodelind[z1]] is the n of Vn; n-ncovcol-nqv is the first time varying covariate or iv */
4569: for (z1=1; z1<=cptcoveff; z1++){
4570: if( Fixed[Tmodelind[z1]]==1){
4571: iv= Tvar[Tmodelind[z1]]-ncovcol-nqv;
4572: if (cotvar[m][iv][i]!= nbcode[Tvaraff[z1]][codtabm(j1,z1)]) /* iv=1 to ntv, right modality */
4573: bool=0;
4574: }else if( Fixed[Tmodelind[z1]]== 0) /* fixed */
4575: if (covar[Tvaraff[z1]][i]!= nbcode[Tvaraff[z1]][codtabm(j1,z1)]) {
4576: bool=0;
4577: }
4578: }
4579: if(bool==1){ /* Otherwise we skip that wave/person */
4580: agebegin=agev[m][i]; /* Age at beginning of wave before transition*/
4581: /* ageend=agev[m][i]+(dh[m][i])*stepm/YEARM; /\* Age at end of wave and transition *\/ */
4582: if(m >=firstpass && m <=lastpass){
4583: y2=anint[m][i]+(mint[m][i]/12.); /* Fractional date in year */
4584: if ((y2>=dateprev1) && (y2<=dateprev2)) { /* Here is the main selection (fractional years) */
4585: if(agev[m][i]==0) agev[m][i]=iagemax+1;
4586: if(agev[m][i]==1) agev[m][i]=iagemax+2;
4587: if((int)agev[m][i] <iagemin-AGEMARGE || (int)agev[m][i] >iagemax+3+AGEMARGE){
4588: 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);
4589: exit(1);
4590: }
4591: if (s[m][i]>0 && s[m][i]<=nlstate) {
4592: /*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]]);*/
4593: prop[s[m][i]][(int)agev[m][i]] += weight[i];/* At age of beginning of transition, where status is known */
4594: prop[s[m][i]][iagemax+3] += weight[i];
4595: } /* end valid statuses */
4596: } /* end selection of dates */
4597: } /* end selection of waves */
4598: } /* end bool */
4599: } /* end wave */
4600: } /* end individual */
4601: for(i=iagemin; i <= iagemax+3; i++){
4602: for(jk=1,posprop=0; jk <=nlstate ; jk++) {
4603: posprop += prop[jk][i];
4604: }
4605:
4606: for(jk=1; jk <=nlstate ; jk++){
4607: if( i <= iagemax){
4608: if(posprop>=1.e-5){
4609: probs[i][jk][j1]= prop[jk][i]/posprop;
4610: } else{
4611: if(first==1){
4612: first=0;
4613: 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]);
4614: }
4615: }
4616: }
4617: }/* end jk */
4618: }/* end i */
1.222 brouard 4619: /*} *//* end i1 */
1.227 brouard 4620: } /* end j1 */
1.222 brouard 4621:
1.227 brouard 4622: /* free_ma3x(freq,-1,nlstate+ndeath,-1,nlstate+ndeath, iagemin, iagemax+3);*/
4623: /*free_vector(pp,1,nlstate);*/
4624: free_matrix(prop,1,nlstate, iagemin-AGEMARGE,iagemax+3+AGEMARGE);
4625: } /* End of prevalence */
1.126 brouard 4626:
4627: /************* Waves Concatenation ***************/
4628:
4629: 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)
4630: {
4631: /* Concatenates waves: wav[i] is the number of effective (useful waves) of individual i.
4632: Death is a valid wave (if date is known).
4633: mw[mi][i] is the mi (mi=1 to wav[i]) effective wave of individual i
4634: dh[m][i] or dh[mw[mi][i]][i] is the delay between two effective waves m=mw[mi][i]
4635: and mw[mi+1][i]. dh depends on stepm.
1.227 brouard 4636: */
1.126 brouard 4637:
1.224 brouard 4638: int i=0, mi=0, m=0, mli=0;
1.126 brouard 4639: /* int j, k=0,jk, ju, jl,jmin=1e+5, jmax=-1;
4640: double sum=0., jmean=0.;*/
1.224 brouard 4641: int first=0, firstwo=0, firsthree=0, firstfour=0, firstfiv=0;
1.126 brouard 4642: int j, k=0,jk, ju, jl;
4643: double sum=0.;
4644: first=0;
1.214 brouard 4645: firstwo=0;
1.217 brouard 4646: firsthree=0;
1.218 brouard 4647: firstfour=0;
1.164 brouard 4648: jmin=100000;
1.126 brouard 4649: jmax=-1;
4650: jmean=0.;
1.224 brouard 4651:
4652: /* Treating live states */
1.214 brouard 4653: for(i=1; i<=imx; i++){ /* For simple cases and if state is death */
1.224 brouard 4654: mi=0; /* First valid wave */
1.227 brouard 4655: mli=0; /* Last valid wave */
1.126 brouard 4656: m=firstpass;
1.214 brouard 4657: while(s[m][i] <= nlstate){ /* a live state */
1.227 brouard 4658: 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 */
4659: mli=m-1;/* mw[++mi][i]=m-1; */
4660: }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 */
4661: mw[++mi][i]=m;
4662: mli=m;
1.224 brouard 4663: } /* else might be a useless wave -1 and mi is not incremented and mw[mi] not updated */
4664: if(m < lastpass){ /* m < lastpass, standard case */
1.227 brouard 4665: m++; /* mi gives the "effective" current wave, m the current wave, go to next wave by incrementing m */
1.216 brouard 4666: }
1.227 brouard 4667: else{ /* m >= lastpass, eventual special issue with warning */
1.224 brouard 4668: #ifdef UNKNOWNSTATUSNOTCONTRIBUTING
1.227 brouard 4669: break;
1.224 brouard 4670: #else
1.227 brouard 4671: if(s[m][i]==-1 && (int) andc[i] == 9999 && (int)anint[m][i] != 9999){
4672: if(firsthree == 0){
4673: 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);
4674: firsthree=1;
4675: }
4676: 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);
4677: mw[++mi][i]=m;
4678: mli=m;
4679: }
4680: if(s[m][i]==-2){ /* Vital status is really unknown */
4681: nbwarn++;
4682: if((int)anint[m][i] == 9999){ /* Has the vital status really been verified? */
4683: 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);
4684: 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);
4685: }
4686: break;
4687: }
4688: break;
1.224 brouard 4689: #endif
1.227 brouard 4690: }/* End m >= lastpass */
1.126 brouard 4691: }/* end while */
1.224 brouard 4692:
1.227 brouard 4693: /* 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 4694: /* After last pass */
1.224 brouard 4695: /* Treating death states */
1.214 brouard 4696: if (s[m][i] > nlstate){ /* In a death state */
1.227 brouard 4697: /* if( mint[m][i]==mdc[m][i] && anint[m][i]==andc[m][i]){ /\* same date of death and date of interview *\/ */
4698: /* } */
1.126 brouard 4699: mi++; /* Death is another wave */
4700: /* if(mi==0) never been interviewed correctly before death */
1.227 brouard 4701: /* Only death is a correct wave */
1.126 brouard 4702: mw[mi][i]=m;
1.224 brouard 4703: }
4704: #ifndef DISPATCHINGKNOWNDEATHAFTERLASTWAVE
1.227 brouard 4705: 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 4706: /* m++; */
4707: /* mi++; */
4708: /* s[m][i]=nlstate+1; /\* We are setting the status to the last of non live state *\/ */
4709: /* mw[mi][i]=m; */
1.218 brouard 4710: if ((int)anint[m][i]!= 9999) { /* date of last interview is known */
1.227 brouard 4711: 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 */
4712: nbwarn++;
4713: if(firstfiv==0){
4714: 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 );
4715: firstfiv=1;
4716: }else{
4717: 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 );
4718: }
4719: }else{ /* Death occured afer last wave potential bias */
4720: nberr++;
4721: if(firstwo==0){
4722: 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 );
4723: firstwo=1;
4724: }
4725: 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 );
4726: }
1.218 brouard 4727: }else{ /* end date of interview is known */
1.227 brouard 4728: /* death is known but not confirmed by death status at any wave */
4729: if(firstfour==0){
4730: 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 );
4731: firstfour=1;
4732: }
4733: 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 4734: }
1.224 brouard 4735: } /* end if date of death is known */
4736: #endif
4737: wav[i]=mi; /* mi should be the last effective wave (or mli) */
4738: /* wav[i]=mw[mi][i]; */
1.126 brouard 4739: if(mi==0){
4740: nbwarn++;
4741: if(first==0){
1.227 brouard 4742: printf("Warning! No valid information for individual %ld line=%d (skipped) and may be others, see log file\n",num[i],i);
4743: first=1;
1.126 brouard 4744: }
4745: if(first==1){
1.227 brouard 4746: fprintf(ficlog,"Warning! No valid information for individual %ld line=%d (skipped)\n",num[i],i);
1.126 brouard 4747: }
4748: } /* end mi==0 */
4749: } /* End individuals */
1.214 brouard 4750: /* wav and mw are no more changed */
1.223 brouard 4751:
1.214 brouard 4752:
1.126 brouard 4753: for(i=1; i<=imx; i++){
4754: for(mi=1; mi<wav[i];mi++){
4755: if (stepm <=0)
1.227 brouard 4756: dh[mi][i]=1;
1.126 brouard 4757: else{
1.227 brouard 4758: if (s[mw[mi+1][i]][i] > nlstate) { /* A death */
4759: if (agedc[i] < 2*AGESUP) {
4760: j= rint(agedc[i]*12-agev[mw[mi][i]][i]*12);
4761: if(j==0) j=1; /* Survives at least one month after exam */
4762: else if(j<0){
4763: nberr++;
4764: 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]);
4765: j=1; /* Temporary Dangerous patch */
4766: 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);
4767: 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]);
4768: 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);
4769: }
4770: k=k+1;
4771: if (j >= jmax){
4772: jmax=j;
4773: ijmax=i;
4774: }
4775: if (j <= jmin){
4776: jmin=j;
4777: ijmin=i;
4778: }
4779: sum=sum+j;
4780: /*if (j<0) printf("j=%d num=%d \n",j,i);*/
4781: /* printf("%d %d %d %d\n", s[mw[mi][i]][i] ,s[mw[mi+1][i]][i],j,i);*/
4782: }
4783: }
4784: else{
4785: j= rint( (agev[mw[mi+1][i]][i]*12 - agev[mw[mi][i]][i]*12));
1.126 brouard 4786: /* 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 4787:
1.227 brouard 4788: k=k+1;
4789: if (j >= jmax) {
4790: jmax=j;
4791: ijmax=i;
4792: }
4793: else if (j <= jmin){
4794: jmin=j;
4795: ijmin=i;
4796: }
4797: /* if (j<10) printf("j=%d jmin=%d num=%d ",j,jmin,i); */
4798: /*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]);*/
4799: if(j<0){
4800: nberr++;
4801: 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]);
4802: 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]);
4803: }
4804: sum=sum+j;
4805: }
4806: jk= j/stepm;
4807: jl= j -jk*stepm;
4808: ju= j -(jk+1)*stepm;
4809: if(mle <=1){ /* only if we use a the linear-interpoloation pseudo-likelihood */
4810: if(jl==0){
4811: dh[mi][i]=jk;
4812: bh[mi][i]=0;
4813: }else{ /* We want a negative bias in order to only have interpolation ie
4814: * to avoid the price of an extra matrix product in likelihood */
4815: dh[mi][i]=jk+1;
4816: bh[mi][i]=ju;
4817: }
4818: }else{
4819: if(jl <= -ju){
4820: dh[mi][i]=jk;
4821: bh[mi][i]=jl; /* bias is positive if real duration
4822: * is higher than the multiple of stepm and negative otherwise.
4823: */
4824: }
4825: else{
4826: dh[mi][i]=jk+1;
4827: bh[mi][i]=ju;
4828: }
4829: if(dh[mi][i]==0){
4830: dh[mi][i]=1; /* At least one step */
4831: bh[mi][i]=ju; /* At least one step */
4832: /* 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);*/
4833: }
4834: } /* end if mle */
1.126 brouard 4835: }
4836: } /* end wave */
4837: }
4838: jmean=sum/k;
4839: 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 4840: 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 4841: }
1.126 brouard 4842:
4843: /*********** Tricode ****************************/
1.220 brouard 4844: void tricode(int *cptcov, int *Tvar, int **nbcode, int imx, int *Ndum)
1.242 brouard 4845: {
4846: /**< Uses cptcovn+2*cptcovprod as the number of covariates */
4847: /* Tvar[i]=atoi(stre); find 'n' in Vn and stores in Tvar. If model=V2+V1 Tvar[1]=2 and Tvar[2]=1
4848: * Boring subroutine which should only output nbcode[Tvar[j]][k]
4849: * Tvar[5] in V2+V1+V3*age+V2*V4 is 4 (V4) even it is a time varying or quantitative variable
4850: * nbcode[Tvar[5]][1]= nbcode[4][1]=0, nbcode[4][2]=1 (usually);
4851: */
1.130 brouard 4852:
1.242 brouard 4853: int ij=1, k=0, j=0, i=0, maxncov=NCOVMAX;
4854: int modmaxcovj=0; /* Modality max of covariates j */
4855: int cptcode=0; /* Modality max of covariates j */
4856: int modmincovj=0; /* Modality min of covariates j */
1.145 brouard 4857:
4858:
1.242 brouard 4859: /* cptcoveff=0; */
4860: /* *cptcov=0; */
1.126 brouard 4861:
1.242 brouard 4862: for (k=1; k <= maxncov; k++) ncodemax[k]=0; /* Horrible constant again replaced by NCOVMAX */
1.126 brouard 4863:
1.242 brouard 4864: /* Loop on covariates without age and products and no quantitative variable */
4865: /* for (j=1; j<=(cptcovs); j++) { /\* From model V1 + V2*age+ V3 + V3*V4 keeps V1 + V3 = 2 only *\/ */
4866: for (k=1; k<=cptcovt; k++) { /* From model V1 + V2*age + V3 + V3*V4 keeps V1 + V3 = 2 only */
4867: for (j=-1; (j < maxncov); j++) Ndum[j]=0;
4868: if(Dummy[k]==0 && Typevar[k] !=1){ /* Dummy covariate and not age product */
4869: switch(Fixed[k]) {
4870: case 0: /* Testing on fixed dummy covariate, simple or product of fixed */
4871: 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*/
4872: ij=(int)(covar[Tvar[k]][i]);
4873: /* ij=0 or 1 or -1. Value of the covariate Tvar[j] for individual i
4874: * If product of Vn*Vm, still boolean *:
4875: * If it was coded 1, 2, 3, 4 should be splitted into 3 boolean variables
4876: * 1 => 0 0 0, 2 => 0 0 1, 3 => 0 1 1, 4=1 0 0 */
4877: /* Finds for covariate j, n=Tvar[j] of Vn . ij is the
4878: modality of the nth covariate of individual i. */
4879: if (ij > modmaxcovj)
4880: modmaxcovj=ij;
4881: else if (ij < modmincovj)
4882: modmincovj=ij;
4883: if ((ij < -1) && (ij > NCOVMAX)){
4884: printf( "Error: minimal is less than -1 or maximal is bigger than %d. Exiting. \n", NCOVMAX );
4885: exit(1);
4886: }else
4887: Ndum[ij]++; /*counts and stores the occurence of this modality 0, 1, -1*/
4888: /* If coded 1, 2, 3 , counts the number of 1 Ndum[1], number of 2, Ndum[2], etc */
4889: /*printf("i=%d ij=%d Ndum[ij]=%d imx=%d",i,ij,Ndum[ij],imx);*/
4890: /* getting the maximum value of the modality of the covariate
4891: (should be 0 or 1 now) Tvar[j]. If V=sex and male is coded 0 and
4892: female ies 1, then modmaxcovj=1.
4893: */
4894: } /* end for loop on individuals i */
4895: printf(" Minimal and maximal values of %d th (fixed) covariate V%d: min=%d max=%d \n", k, Tvar[k], modmincovj, modmaxcovj);
4896: fprintf(ficlog," Minimal and maximal values of %d th (fixed) covariate V%d: min=%d max=%d \n", k, Tvar[k], modmincovj, modmaxcovj);
4897: cptcode=modmaxcovj;
4898: /* Ndum[0] = frequency of 0 for model-covariate j, Ndum[1] frequency of 1 etc. */
4899: /*for (i=0; i<=cptcode; i++) {*/
4900: for (j=modmincovj; j<=modmaxcovj; j++) { /* j=-1 ? 0 and 1*//* For each value j of the modality of model-cov k */
4901: printf("Frequencies of (fixed) covariate %d ie V%d with value %d: %d\n", k, Tvar[k], j, Ndum[j]);
4902: fprintf(ficlog, "Frequencies of (fixed) covariate %d ie V%d with value %d: %d\n", k, Tvar[k], j, Ndum[j]);
4903: if( Ndum[j] != 0 ){ /* Counts if nobody answered modality j ie empty modality, we skip it and reorder */
4904: if( j != -1){
4905: ncodemax[k]++; /* ncodemax[k]= Number of modalities of the k th
4906: covariate for which somebody answered excluding
4907: undefined. Usually 2: 0 and 1. */
4908: }
4909: ncodemaxwundef[k]++; /* ncodemax[j]= Number of modalities of the k th
4910: covariate for which somebody answered including
4911: undefined. Usually 3: -1, 0 and 1. */
4912: } /* In fact ncodemax[k]=2 (dichotom. variables only) but it could be more for
4913: * historical reasons: 3 if coded 1, 2, 3 and 4 and Ndum[2]=0 */
4914: } /* Ndum[-1] number of undefined modalities */
1.231 brouard 4915:
1.242 brouard 4916: /* j is a covariate, n=Tvar[j] of Vn; Fills nbcode */
4917: /* For covariate j, modalities could be 1, 2, 3, 4, 5, 6, 7. */
4918: /* If Ndum[1]=0, Ndum[2]=0, Ndum[3]= 635, Ndum[4]=0, Ndum[5]=0, Ndum[6]=27, Ndum[7]=125; */
4919: /* modmincovj=3; modmaxcovj = 7; */
4920: /* There are only 3 modalities non empty 3, 6, 7 (or 2 if 27 is too few) : ncodemax[j]=3; */
4921: /* which will be coded 0, 1, 2 which in binary on 2=3-1 digits are 0=00 1=01, 2=10; */
4922: /* defining two dummy variables: variables V1_1 and V1_2.*/
4923: /* nbcode[Tvar[j]][ij]=k; */
4924: /* nbcode[Tvar[j]][1]=0; */
4925: /* nbcode[Tvar[j]][2]=1; */
4926: /* nbcode[Tvar[j]][3]=2; */
4927: /* To be continued (not working yet). */
4928: ij=0; /* ij is similar to i but can jump over null modalities */
4929: 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*/
4930: if (Ndum[i] == 0) { /* If nobody responded to this modality k */
4931: break;
4932: }
4933: ij++;
4934: 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*/
4935: cptcode = ij; /* New max modality for covar j */
4936: } /* end of loop on modality i=-1 to 1 or more */
4937: break;
4938: case 1: /* Testing on varying covariate, could be simple and
4939: * should look at waves or product of fixed *
4940: * varying. No time to test -1, assuming 0 and 1 only */
4941: ij=0;
4942: for(i=0; i<=1;i++){
4943: nbcode[Tvar[k]][++ij]=i;
4944: }
4945: break;
4946: default:
4947: break;
4948: } /* end switch */
4949: } /* end dummy test */
4950:
4951: /* for (k=0; k<= cptcode; k++) { /\* k=-1 ? k=0 to 1 *\//\* Could be 1 to 4 *\//\* cptcode=modmaxcovj *\/ */
4952: /* /\*recode from 0 *\/ */
4953: /* k is a modality. If we have model=V1+V1*sex */
4954: /* then: nbcode[1][1]=0 ; nbcode[1][2]=1; nbcode[2][1]=0 ; nbcode[2][2]=1; */
4955: /* But if some modality were not used, it is recoded from 0 to a newer modmaxcovj=cptcode *\/ */
4956: /* } */
4957: /* /\* cptcode = ij; *\/ /\* New max modality for covar j *\/ */
4958: /* if (ij > ncodemax[j]) { */
4959: /* printf( " Error ij=%d > ncodemax[%d]=%d\n", ij, j, ncodemax[j]); */
4960: /* fprintf(ficlog, " Error ij=%d > ncodemax[%d]=%d\n", ij, j, ncodemax[j]); */
4961: /* break; */
4962: /* } */
4963: /* } /\* end of loop on modality k *\/ */
4964: } /* end of loop on model-covariate j. nbcode[Tvarj][1]=0 and nbcode[Tvarj][2]=1 sets the value of covariate j*/
4965:
4966: for (k=-1; k< maxncov; k++) Ndum[k]=0;
4967: /* Look at fixed dummy (single or product) covariates to check empty modalities */
4968: for (i=1; i<=ncovmodel-2-nagesqr; i++) { /* -2, cste and age and eventually age*age */
4969: /* Listing of all covariables in statement model to see if some covariates appear twice. For example, V1 appears twice in V1+V1*V2.*/
4970: 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 */
4971: 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 */
4972: /* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1, {2, 1, 1, 1, 2, 1, 1, 0, 0} */
4973: } /* V4+V3+V5, Ndum[1]@5={0, 0, 1, 1, 1} */
4974:
4975: ij=0;
4976: /* for (i=0; i<= maxncov-1; i++) { /\* modmaxcovj is unknown here. Only Ndum[2(V2),3(age*V3), 5(V3*V2) 6(V1*V4) *\/ */
4977: for (k=1; k<= cptcovt; k++) { /* modmaxcovj is unknown here. Only Ndum[2(V2),3(age*V3), 5(V3*V2) 6(V1*V4) */
4978: /*printf("Ndum[%d]=%d\n",i, Ndum[i]);*/
4979: /* if((Ndum[i]!=0) && (i<=ncovcol)){ /\* Tvar[i] <= ncovmodel ? *\/ */
4980: if(Ndum[Tvar[k]]!=0 && Dummy[k] == 0 && Typevar[k]==0){ /* Only Dummy and non empty in the model */
4981: /* If product not in single variable we don't print results */
4982: /*printf("diff Ndum[%d]=%d\n",i, Ndum[i]);*/
4983: ++ij;/* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1, */
4984: 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*/
4985: Tmodelind[ij]=k; /* Tmodelind: index in model of dummies Tmodelind[1]=2 V4: pos=2; V3: pos=3, V1=9 {2, 3, 9, ?, ?,} */
4986: 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 */
4987: if(Fixed[k]!=0)
4988: anyvaryingduminmodel=1;
4989: /* }else if((Ndum[i]!=0) && (i<=ncovcol+nqv)){ */
4990: /* Tvaraff[++ij]=-10; /\* Dont'n know how to treat quantitative variables yet *\/ */
4991: /* }else if((Ndum[i]!=0) && (i<=ncovcol+nqv+ntv)){ */
4992: /* Tvaraff[++ij]=i; /\*For printing (unclear) *\/ */
4993: /* }else if((Ndum[i]!=0) && (i<=ncovcol+nqv+ntv+nqtv)){ */
4994: /* Tvaraff[++ij]=-20; /\* Dont'n know how to treat quantitative variables yet *\/ */
4995: }
4996: } /* Tvaraff[1]@5 {3, 4, -20, 0, 0} Very strange */
4997: /* ij--; */
4998: /* cptcoveff=ij; /\*Number of total covariates*\/ */
4999: *cptcov=ij; /*Number of total real effective covariates: effective
5000: * because they can be excluded from the model and real
5001: * if in the model but excluded because missing values, but how to get k from ij?*/
5002: for(j=ij+1; j<= cptcovt; j++){
5003: Tvaraff[j]=0;
5004: Tmodelind[j]=0;
5005: }
5006: for(j=ntveff+1; j<= cptcovt; j++){
5007: TmodelInvind[j]=0;
5008: }
5009: /* To be sorted */
5010: ;
5011: }
1.126 brouard 5012:
1.145 brouard 5013:
1.126 brouard 5014: /*********** Health Expectancies ****************/
5015:
1.235 brouard 5016: 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 5017:
5018: {
5019: /* Health expectancies, no variances */
1.164 brouard 5020: int i, j, nhstepm, hstepm, h, nstepm;
1.126 brouard 5021: int nhstepma, nstepma; /* Decreasing with age */
5022: double age, agelim, hf;
5023: double ***p3mat;
5024: double eip;
5025:
1.238 brouard 5026: /* pstamp(ficreseij); */
1.126 brouard 5027: fprintf(ficreseij,"# (a) Life expectancies by health status at initial age and (b) health expectancies by health status at initial age\n");
5028: fprintf(ficreseij,"# Age");
5029: for(i=1; i<=nlstate;i++){
5030: for(j=1; j<=nlstate;j++){
5031: fprintf(ficreseij," e%1d%1d ",i,j);
5032: }
5033: fprintf(ficreseij," e%1d. ",i);
5034: }
5035: fprintf(ficreseij,"\n");
5036:
5037:
5038: if(estepm < stepm){
5039: printf ("Problem %d lower than %d\n",estepm, stepm);
5040: }
5041: else hstepm=estepm;
5042: /* We compute the life expectancy from trapezoids spaced every estepm months
5043: * This is mainly to measure the difference between two models: for example
5044: * if stepm=24 months pijx are given only every 2 years and by summing them
5045: * we are calculating an estimate of the Life Expectancy assuming a linear
5046: * progression in between and thus overestimating or underestimating according
5047: * to the curvature of the survival function. If, for the same date, we
5048: * estimate the model with stepm=1 month, we can keep estepm to 24 months
5049: * to compare the new estimate of Life expectancy with the same linear
5050: * hypothesis. A more precise result, taking into account a more precise
5051: * curvature will be obtained if estepm is as small as stepm. */
5052:
5053: /* For example we decided to compute the life expectancy with the smallest unit */
5054: /* hstepm beeing the number of stepms, if hstepm=1 the length of hstepm is stepm.
5055: nhstepm is the number of hstepm from age to agelim
5056: nstepm is the number of stepm from age to agelin.
5057: Look at hpijx to understand the reason of that which relies in memory size
5058: and note for a fixed period like estepm months */
5059: /* We decided (b) to get a life expectancy respecting the most precise curvature of the
5060: survival function given by stepm (the optimization length). Unfortunately it
5061: means that if the survival funtion is printed only each two years of age and if
5062: you sum them up and add 1 year (area under the trapezoids) you won't get the same
5063: results. So we changed our mind and took the option of the best precision.
5064: */
5065: hstepm=hstepm/stepm; /* Typically in stepm units, if stepm=6 & estepm=24 , = 24/6 months = 4 */
5066:
5067: agelim=AGESUP;
5068: /* If stepm=6 months */
5069: /* Computed by stepm unit matrices, product of hstepm matrices, stored
5070: in an array of nhstepm length: nhstepm=10, hstepm=4, stepm=6 months */
5071:
5072: /* nhstepm age range expressed in number of stepm */
5073: nstepm=(int) rint((agelim-bage)*YEARM/stepm); /* Biggest nstepm */
5074: /* Typically if 20 years nstepm = 20*12/6=40 stepm */
5075: /* if (stepm >= YEARM) hstepm=1;*/
5076: nhstepm = nstepm/hstepm;/* Expressed in hstepm, typically nhstepm=40/4=10 */
5077: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
5078:
5079: for (age=bage; age<=fage; age ++){
5080: nstepma=(int) rint((agelim-bage)*YEARM/stepm); /* Biggest nstepm */
5081: /* Typically if 20 years nstepm = 20*12/6=40 stepm */
5082: /* if (stepm >= YEARM) hstepm=1;*/
5083: nhstepma = nstepma/hstepm;/* Expressed in hstepm, typically nhstepma=40/4=10 */
5084:
5085: /* If stepm=6 months */
5086: /* Computed by stepm unit matrices, product of hstepma matrices, stored
5087: in an array of nhstepma length: nhstepma=10, hstepm=4, stepm=6 months */
5088:
1.235 brouard 5089: hpxij(p3mat,nhstepma,age,hstepm,x,nlstate,stepm,oldm, savm, cij, nres);
1.126 brouard 5090:
5091: hf=hstepm*stepm/YEARM; /* Duration of hstepm expressed in year unit. */
5092:
5093: printf("%d|",(int)age);fflush(stdout);
5094: fprintf(ficlog,"%d|",(int)age);fflush(ficlog);
5095:
5096: /* Computing expectancies */
5097: for(i=1; i<=nlstate;i++)
5098: for(j=1; j<=nlstate;j++)
5099: for (h=0, eij[i][j][(int)age]=0; h<=nhstepm-1; h++){
5100: eij[i][j][(int)age] += (p3mat[i][j][h]+p3mat[i][j][h+1])/2.0*hf;
5101:
5102: /* 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]);*/
5103:
5104: }
5105:
5106: fprintf(ficreseij,"%3.0f",age );
5107: for(i=1; i<=nlstate;i++){
5108: eip=0;
5109: for(j=1; j<=nlstate;j++){
5110: eip +=eij[i][j][(int)age];
5111: fprintf(ficreseij,"%9.4f", eij[i][j][(int)age] );
5112: }
5113: fprintf(ficreseij,"%9.4f", eip );
5114: }
5115: fprintf(ficreseij,"\n");
5116:
5117: }
5118: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
5119: printf("\n");
5120: fprintf(ficlog,"\n");
5121:
5122: }
5123:
1.235 brouard 5124: 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 5125:
5126: {
5127: /* Covariances of health expectancies eij and of total life expectancies according
1.222 brouard 5128: to initial status i, ei. .
1.126 brouard 5129: */
5130: int i, j, nhstepm, hstepm, h, nstepm, k, cptj, cptj2, i2, j2, ij, ji;
5131: int nhstepma, nstepma; /* Decreasing with age */
5132: double age, agelim, hf;
5133: double ***p3matp, ***p3matm, ***varhe;
5134: double **dnewm,**doldm;
5135: double *xp, *xm;
5136: double **gp, **gm;
5137: double ***gradg, ***trgradg;
5138: int theta;
5139:
5140: double eip, vip;
5141:
5142: varhe=ma3x(1,nlstate*nlstate,1,nlstate*nlstate,(int) bage, (int) fage);
5143: xp=vector(1,npar);
5144: xm=vector(1,npar);
5145: dnewm=matrix(1,nlstate*nlstate,1,npar);
5146: doldm=matrix(1,nlstate*nlstate,1,nlstate*nlstate);
5147:
5148: pstamp(ficresstdeij);
5149: fprintf(ficresstdeij,"# Health expectancies with standard errors\n");
5150: fprintf(ficresstdeij,"# Age");
5151: for(i=1; i<=nlstate;i++){
5152: for(j=1; j<=nlstate;j++)
5153: fprintf(ficresstdeij," e%1d%1d (SE)",i,j);
5154: fprintf(ficresstdeij," e%1d. ",i);
5155: }
5156: fprintf(ficresstdeij,"\n");
5157:
5158: pstamp(ficrescveij);
5159: fprintf(ficrescveij,"# Subdiagonal matrix of covariances of health expectancies by age: cov(eij,ekl)\n");
5160: fprintf(ficrescveij,"# Age");
5161: for(i=1; i<=nlstate;i++)
5162: for(j=1; j<=nlstate;j++){
5163: cptj= (j-1)*nlstate+i;
5164: for(i2=1; i2<=nlstate;i2++)
5165: for(j2=1; j2<=nlstate;j2++){
5166: cptj2= (j2-1)*nlstate+i2;
5167: if(cptj2 <= cptj)
5168: fprintf(ficrescveij," %1d%1d,%1d%1d",i,j,i2,j2);
5169: }
5170: }
5171: fprintf(ficrescveij,"\n");
5172:
5173: if(estepm < stepm){
5174: printf ("Problem %d lower than %d\n",estepm, stepm);
5175: }
5176: else hstepm=estepm;
5177: /* We compute the life expectancy from trapezoids spaced every estepm months
5178: * This is mainly to measure the difference between two models: for example
5179: * if stepm=24 months pijx are given only every 2 years and by summing them
5180: * we are calculating an estimate of the Life Expectancy assuming a linear
5181: * progression in between and thus overestimating or underestimating according
5182: * to the curvature of the survival function. If, for the same date, we
5183: * estimate the model with stepm=1 month, we can keep estepm to 24 months
5184: * to compare the new estimate of Life expectancy with the same linear
5185: * hypothesis. A more precise result, taking into account a more precise
5186: * curvature will be obtained if estepm is as small as stepm. */
5187:
5188: /* For example we decided to compute the life expectancy with the smallest unit */
5189: /* hstepm beeing the number of stepms, if hstepm=1 the length of hstepm is stepm.
5190: nhstepm is the number of hstepm from age to agelim
5191: nstepm is the number of stepm from age to agelin.
5192: Look at hpijx to understand the reason of that which relies in memory size
5193: and note for a fixed period like estepm months */
5194: /* We decided (b) to get a life expectancy respecting the most precise curvature of the
5195: survival function given by stepm (the optimization length). Unfortunately it
5196: means that if the survival funtion is printed only each two years of age and if
5197: you sum them up and add 1 year (area under the trapezoids) you won't get the same
5198: results. So we changed our mind and took the option of the best precision.
5199: */
5200: hstepm=hstepm/stepm; /* Typically in stepm units, if stepm=6 & estepm=24 , = 24/6 months = 4 */
5201:
5202: /* If stepm=6 months */
5203: /* nhstepm age range expressed in number of stepm */
5204: agelim=AGESUP;
5205: nstepm=(int) rint((agelim-bage)*YEARM/stepm);
5206: /* Typically if 20 years nstepm = 20*12/6=40 stepm */
5207: /* if (stepm >= YEARM) hstepm=1;*/
5208: nhstepm = nstepm/hstepm;/* Expressed in hstepm, typically nhstepm=40/4=10 */
5209:
5210: p3matp=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
5211: p3matm=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
5212: gradg=ma3x(0,nhstepm,1,npar,1,nlstate*nlstate);
5213: trgradg =ma3x(0,nhstepm,1,nlstate*nlstate,1,npar);
5214: gp=matrix(0,nhstepm,1,nlstate*nlstate);
5215: gm=matrix(0,nhstepm,1,nlstate*nlstate);
5216:
5217: for (age=bage; age<=fage; age ++){
5218: nstepma=(int) rint((agelim-bage)*YEARM/stepm); /* Biggest nstepm */
5219: /* Typically if 20 years nstepm = 20*12/6=40 stepm */
5220: /* if (stepm >= YEARM) hstepm=1;*/
5221: nhstepma = nstepma/hstepm;/* Expressed in hstepm, typically nhstepma=40/4=10 */
1.218 brouard 5222:
1.126 brouard 5223: /* If stepm=6 months */
5224: /* Computed by stepm unit matrices, product of hstepma matrices, stored
5225: in an array of nhstepma length: nhstepma=10, hstepm=4, stepm=6 months */
5226:
5227: hf=hstepm*stepm/YEARM; /* Duration of hstepm expressed in year unit. */
1.218 brouard 5228:
1.126 brouard 5229: /* Computing Variances of health expectancies */
5230: /* Gradient is computed with plus gp and minus gm. Code is duplicated in order to
5231: decrease memory allocation */
5232: for(theta=1; theta <=npar; theta++){
5233: for(i=1; i<=npar; i++){
1.222 brouard 5234: xp[i] = x[i] + (i==theta ?delti[theta]:0);
5235: xm[i] = x[i] - (i==theta ?delti[theta]:0);
1.126 brouard 5236: }
1.235 brouard 5237: hpxij(p3matp,nhstepm,age,hstepm,xp,nlstate,stepm,oldm,savm, cij, nres);
5238: hpxij(p3matm,nhstepm,age,hstepm,xm,nlstate,stepm,oldm,savm, cij, nres);
1.218 brouard 5239:
1.126 brouard 5240: for(j=1; j<= nlstate; j++){
1.222 brouard 5241: for(i=1; i<=nlstate; i++){
5242: for(h=0; h<=nhstepm-1; h++){
5243: gp[h][(j-1)*nlstate + i] = (p3matp[i][j][h]+p3matp[i][j][h+1])/2.;
5244: gm[h][(j-1)*nlstate + i] = (p3matm[i][j][h]+p3matm[i][j][h+1])/2.;
5245: }
5246: }
1.126 brouard 5247: }
1.218 brouard 5248:
1.126 brouard 5249: for(ij=1; ij<= nlstate*nlstate; ij++)
1.222 brouard 5250: for(h=0; h<=nhstepm-1; h++){
5251: gradg[h][theta][ij]= (gp[h][ij]-gm[h][ij])/2./delti[theta];
5252: }
1.126 brouard 5253: }/* End theta */
5254:
5255:
5256: for(h=0; h<=nhstepm-1; h++)
5257: for(j=1; j<=nlstate*nlstate;j++)
1.222 brouard 5258: for(theta=1; theta <=npar; theta++)
5259: trgradg[h][j][theta]=gradg[h][theta][j];
1.126 brouard 5260:
1.218 brouard 5261:
1.222 brouard 5262: for(ij=1;ij<=nlstate*nlstate;ij++)
1.126 brouard 5263: for(ji=1;ji<=nlstate*nlstate;ji++)
1.222 brouard 5264: varhe[ij][ji][(int)age] =0.;
1.218 brouard 5265:
1.222 brouard 5266: printf("%d|",(int)age);fflush(stdout);
5267: fprintf(ficlog,"%d|",(int)age);fflush(ficlog);
5268: for(h=0;h<=nhstepm-1;h++){
1.126 brouard 5269: for(k=0;k<=nhstepm-1;k++){
1.222 brouard 5270: matprod2(dnewm,trgradg[h],1,nlstate*nlstate,1,npar,1,npar,matcov);
5271: matprod2(doldm,dnewm,1,nlstate*nlstate,1,npar,1,nlstate*nlstate,gradg[k]);
5272: for(ij=1;ij<=nlstate*nlstate;ij++)
5273: for(ji=1;ji<=nlstate*nlstate;ji++)
5274: varhe[ij][ji][(int)age] += doldm[ij][ji]*hf*hf;
1.126 brouard 5275: }
5276: }
1.218 brouard 5277:
1.126 brouard 5278: /* Computing expectancies */
1.235 brouard 5279: hpxij(p3matm,nhstepm,age,hstepm,x,nlstate,stepm,oldm, savm, cij,nres);
1.126 brouard 5280: for(i=1; i<=nlstate;i++)
5281: for(j=1; j<=nlstate;j++)
1.222 brouard 5282: for (h=0, eij[i][j][(int)age]=0; h<=nhstepm-1; h++){
5283: eij[i][j][(int)age] += (p3matm[i][j][h]+p3matm[i][j][h+1])/2.0*hf;
1.218 brouard 5284:
1.222 brouard 5285: /* 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 5286:
1.222 brouard 5287: }
1.218 brouard 5288:
1.126 brouard 5289: fprintf(ficresstdeij,"%3.0f",age );
5290: for(i=1; i<=nlstate;i++){
5291: eip=0.;
5292: vip=0.;
5293: for(j=1; j<=nlstate;j++){
1.222 brouard 5294: eip += eij[i][j][(int)age];
5295: for(k=1; k<=nlstate;k++) /* Sum on j and k of cov(eij,eik) */
5296: vip += varhe[(j-1)*nlstate+i][(k-1)*nlstate+i][(int)age];
5297: 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 5298: }
5299: fprintf(ficresstdeij," %9.4f (%.4f)", eip, sqrt(vip));
5300: }
5301: fprintf(ficresstdeij,"\n");
1.218 brouard 5302:
1.126 brouard 5303: fprintf(ficrescveij,"%3.0f",age );
5304: for(i=1; i<=nlstate;i++)
5305: for(j=1; j<=nlstate;j++){
1.222 brouard 5306: cptj= (j-1)*nlstate+i;
5307: for(i2=1; i2<=nlstate;i2++)
5308: for(j2=1; j2<=nlstate;j2++){
5309: cptj2= (j2-1)*nlstate+i2;
5310: if(cptj2 <= cptj)
5311: fprintf(ficrescveij," %.4f", varhe[cptj][cptj2][(int)age]);
5312: }
1.126 brouard 5313: }
5314: fprintf(ficrescveij,"\n");
1.218 brouard 5315:
1.126 brouard 5316: }
5317: free_matrix(gm,0,nhstepm,1,nlstate*nlstate);
5318: free_matrix(gp,0,nhstepm,1,nlstate*nlstate);
5319: free_ma3x(gradg,0,nhstepm,1,npar,1,nlstate*nlstate);
5320: free_ma3x(trgradg,0,nhstepm,1,nlstate*nlstate,1,npar);
5321: free_ma3x(p3matm,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
5322: free_ma3x(p3matp,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
5323: printf("\n");
5324: fprintf(ficlog,"\n");
1.218 brouard 5325:
1.126 brouard 5326: free_vector(xm,1,npar);
5327: free_vector(xp,1,npar);
5328: free_matrix(dnewm,1,nlstate*nlstate,1,npar);
5329: free_matrix(doldm,1,nlstate*nlstate,1,nlstate*nlstate);
5330: free_ma3x(varhe,1,nlstate*nlstate,1,nlstate*nlstate,(int) bage, (int)fage);
5331: }
1.218 brouard 5332:
1.126 brouard 5333: /************ Variance ******************/
1.235 brouard 5334: 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 5335: {
5336: /* Variance of health expectancies */
5337: /* double **prevalim(double **prlim, int nlstate, double *xp, double age, double **oldm, double ** savm,double ftolpl);*/
5338: /* double **newm;*/
5339: /* int movingaverage(double ***probs, double bage,double fage, double ***mobaverage, int mobilav)*/
5340:
5341: /* int movingaverage(); */
5342: double **dnewm,**doldm;
5343: double **dnewmp,**doldmp;
5344: int i, j, nhstepm, hstepm, h, nstepm ;
5345: int k;
5346: double *xp;
5347: double **gp, **gm; /* for var eij */
5348: double ***gradg, ***trgradg; /*for var eij */
5349: double **gradgp, **trgradgp; /* for var p point j */
5350: double *gpp, *gmp; /* for var p point j */
5351: double **varppt; /* for var p point j nlstate to nlstate+ndeath */
5352: double ***p3mat;
5353: double age,agelim, hf;
5354: /* double ***mobaverage; */
5355: int theta;
5356: char digit[4];
5357: char digitp[25];
5358:
5359: char fileresprobmorprev[FILENAMELENGTH];
5360:
5361: if(popbased==1){
5362: if(mobilav!=0)
5363: strcpy(digitp,"-POPULBASED-MOBILAV_");
5364: else strcpy(digitp,"-POPULBASED-NOMOBIL_");
5365: }
5366: else
5367: strcpy(digitp,"-STABLBASED_");
1.126 brouard 5368:
1.218 brouard 5369: /* if (mobilav!=0) { */
5370: /* mobaverage= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
5371: /* if (movingaverage(probs, bage, fage, mobaverage,mobilav)!=0){ */
5372: /* fprintf(ficlog," Error in movingaverage mobilav=%d\n",mobilav); */
5373: /* printf(" Error in movingaverage mobilav=%d\n",mobilav); */
5374: /* } */
5375: /* } */
5376:
5377: strcpy(fileresprobmorprev,"PRMORPREV-");
5378: sprintf(digit,"%-d",ij);
5379: /*printf("DIGIT=%s, ij=%d ijr=%-d|\n",digit, ij,ij);*/
5380: strcat(fileresprobmorprev,digit); /* Tvar to be done */
5381: strcat(fileresprobmorprev,digitp); /* Popbased or not, mobilav or not */
5382: strcat(fileresprobmorprev,fileresu);
5383: if((ficresprobmorprev=fopen(fileresprobmorprev,"w"))==NULL) {
5384: printf("Problem with resultfile: %s\n", fileresprobmorprev);
5385: fprintf(ficlog,"Problem with resultfile: %s\n", fileresprobmorprev);
5386: }
5387: printf("Computing total mortality p.j=w1*p1j+w2*p2j+..: result on file '%s' \n",fileresprobmorprev);
5388: fprintf(ficlog,"Computing total mortality p.j=w1*p1j+w2*p2j+..: result on file '%s' \n",fileresprobmorprev);
5389: pstamp(ficresprobmorprev);
5390: 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 5391: fprintf(ficresprobmorprev,"# Selected quantitative variables and dummies");
5392: for (j=1; j<= nsq; j++){ /* For each selected (single) quantitative value */
5393: fprintf(ficresprobmorprev," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
5394: }
5395: for(j=1;j<=cptcoveff;j++)
5396: fprintf(ficresprobmorprev,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(ij,j)]);
5397: fprintf(ficresprobmorprev,"\n");
5398:
1.218 brouard 5399: fprintf(ficresprobmorprev,"# Age cov=%-d",ij);
5400: for(j=nlstate+1; j<=(nlstate+ndeath);j++){
5401: fprintf(ficresprobmorprev," p.%-d SE",j);
5402: for(i=1; i<=nlstate;i++)
5403: fprintf(ficresprobmorprev," w%1d p%-d%-d",i,i,j);
5404: }
5405: fprintf(ficresprobmorprev,"\n");
5406:
5407: fprintf(ficgp,"\n# Routine varevsij");
5408: fprintf(ficgp,"\nunset title \n");
5409: /* fprintf(fichtm, "#Local time at start: %s", strstart);*/
5410: 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");
5411: fprintf(fichtm,"\n<br>%s <br>\n",digitp);
5412: /* } */
5413: varppt = matrix(nlstate+1,nlstate+ndeath,nlstate+1,nlstate+ndeath);
5414: pstamp(ficresvij);
5415: fprintf(ficresvij,"# Variance and covariance of health expectancies e.j \n# (weighted average of eij where weights are ");
5416: if(popbased==1)
5417: 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);
5418: else
5419: fprintf(ficresvij,"the age specific period (stable) prevalences in each health state \n");
5420: fprintf(ficresvij,"# Age");
5421: for(i=1; i<=nlstate;i++)
5422: for(j=1; j<=nlstate;j++)
5423: fprintf(ficresvij," Cov(e.%1d, e.%1d)",i,j);
5424: fprintf(ficresvij,"\n");
5425:
5426: xp=vector(1,npar);
5427: dnewm=matrix(1,nlstate,1,npar);
5428: doldm=matrix(1,nlstate,1,nlstate);
5429: dnewmp= matrix(nlstate+1,nlstate+ndeath,1,npar);
5430: doldmp= matrix(nlstate+1,nlstate+ndeath,nlstate+1,nlstate+ndeath);
5431:
5432: gradgp=matrix(1,npar,nlstate+1,nlstate+ndeath);
5433: gpp=vector(nlstate+1,nlstate+ndeath);
5434: gmp=vector(nlstate+1,nlstate+ndeath);
5435: trgradgp =matrix(nlstate+1,nlstate+ndeath,1,npar); /* mu or p point j*/
1.126 brouard 5436:
1.218 brouard 5437: if(estepm < stepm){
5438: printf ("Problem %d lower than %d\n",estepm, stepm);
5439: }
5440: else hstepm=estepm;
5441: /* For example we decided to compute the life expectancy with the smallest unit */
5442: /* hstepm beeing the number of stepms, if hstepm=1 the length of hstepm is stepm.
5443: nhstepm is the number of hstepm from age to agelim
5444: nstepm is the number of stepm from age to agelim.
5445: Look at function hpijx to understand why because of memory size limitations,
5446: we decided (b) to get a life expectancy respecting the most precise curvature of the
5447: survival function given by stepm (the optimization length). Unfortunately it
5448: means that if the survival funtion is printed every two years of age and if
5449: you sum them up and add 1 year (area under the trapezoids) you won't get the same
5450: results. So we changed our mind and took the option of the best precision.
5451: */
5452: hstepm=hstepm/stepm; /* Typically in stepm units, if stepm=6 & estepm=24 , = 24/6 months = 4 */
5453: agelim = AGESUP;
5454: for (age=bage; age<=fage; age ++){ /* If stepm=6 months */
5455: nstepm=(int) rint((agelim-age)*YEARM/stepm); /* Typically 20 years = 20*12/6=40 */
5456: nhstepm = nstepm/hstepm;/* Expressed in hstepm, typically nhstepm=40/4=10 */
5457: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
5458: gradg=ma3x(0,nhstepm,1,npar,1,nlstate);
5459: gp=matrix(0,nhstepm,1,nlstate);
5460: gm=matrix(0,nhstepm,1,nlstate);
5461:
5462:
5463: for(theta=1; theta <=npar; theta++){
5464: for(i=1; i<=npar; i++){ /* Computes gradient x + delta*/
5465: xp[i] = x[i] + (i==theta ?delti[theta]:0);
5466: }
5467:
1.242 brouard 5468: prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp,ij, nres);
1.218 brouard 5469:
5470: if (popbased==1) {
5471: if(mobilav ==0){
5472: for(i=1; i<=nlstate;i++)
5473: prlim[i][i]=probs[(int)age][i][ij];
5474: }else{ /* mobilav */
5475: for(i=1; i<=nlstate;i++)
5476: prlim[i][i]=mobaverage[(int)age][i][ij];
5477: }
5478: }
5479:
1.235 brouard 5480: 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 5481: for(j=1; j<= nlstate; j++){
5482: for(h=0; h<=nhstepm; h++){
5483: for(i=1, gp[h][j]=0.;i<=nlstate;i++)
5484: gp[h][j] += prlim[i][i]*p3mat[i][j][h];
5485: }
5486: }
5487: /* Next for computing probability of death (h=1 means
5488: computed over hstepm matrices product = hstepm*stepm months)
5489: as a weighted average of prlim.
5490: */
5491: for(j=nlstate+1;j<=nlstate+ndeath;j++){
5492: for(i=1,gpp[j]=0.; i<= nlstate; i++)
5493: gpp[j] += prlim[i][i]*p3mat[i][j][1];
5494: }
5495: /* end probability of death */
5496:
5497: for(i=1; i<=npar; i++) /* Computes gradient x - delta */
5498: xp[i] = x[i] - (i==theta ?delti[theta]:0);
5499:
1.242 brouard 5500: prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp, ij, nres);
1.218 brouard 5501:
5502: if (popbased==1) {
5503: if(mobilav ==0){
5504: for(i=1; i<=nlstate;i++)
5505: prlim[i][i]=probs[(int)age][i][ij];
5506: }else{ /* mobilav */
5507: for(i=1; i<=nlstate;i++)
5508: prlim[i][i]=mobaverage[(int)age][i][ij];
5509: }
5510: }
5511:
1.235 brouard 5512: hpxij(p3mat,nhstepm,age,hstepm,xp,nlstate,stepm,oldm,savm, ij,nres);
1.218 brouard 5513:
5514: for(j=1; j<= nlstate; j++){ /* Sum of wi * eij = e.j */
5515: for(h=0; h<=nhstepm; h++){
5516: for(i=1, gm[h][j]=0.;i<=nlstate;i++)
5517: gm[h][j] += prlim[i][i]*p3mat[i][j][h];
5518: }
5519: }
5520: /* This for computing probability of death (h=1 means
5521: computed over hstepm matrices product = hstepm*stepm months)
5522: as a weighted average of prlim.
5523: */
5524: for(j=nlstate+1;j<=nlstate+ndeath;j++){
5525: for(i=1,gmp[j]=0.; i<= nlstate; i++)
5526: gmp[j] += prlim[i][i]*p3mat[i][j][1];
5527: }
5528: /* end probability of death */
5529:
5530: for(j=1; j<= nlstate; j++) /* vareij */
5531: for(h=0; h<=nhstepm; h++){
5532: gradg[h][theta][j]= (gp[h][j]-gm[h][j])/2./delti[theta];
5533: }
5534:
5535: for(j=nlstate+1; j<= nlstate+ndeath; j++){ /* var mu */
5536: gradgp[theta][j]= (gpp[j]-gmp[j])/2./delti[theta];
5537: }
5538:
5539: } /* End theta */
5540:
5541: trgradg =ma3x(0,nhstepm,1,nlstate,1,npar); /* veij */
5542:
5543: for(h=0; h<=nhstepm; h++) /* veij */
5544: for(j=1; j<=nlstate;j++)
5545: for(theta=1; theta <=npar; theta++)
5546: trgradg[h][j][theta]=gradg[h][theta][j];
5547:
5548: for(j=nlstate+1; j<=nlstate+ndeath;j++) /* mu */
5549: for(theta=1; theta <=npar; theta++)
5550: trgradgp[j][theta]=gradgp[theta][j];
5551:
5552:
5553: hf=hstepm*stepm/YEARM; /* Duration of hstepm expressed in year unit. */
5554: for(i=1;i<=nlstate;i++)
5555: for(j=1;j<=nlstate;j++)
5556: vareij[i][j][(int)age] =0.;
5557:
5558: for(h=0;h<=nhstepm;h++){
5559: for(k=0;k<=nhstepm;k++){
5560: matprod2(dnewm,trgradg[h],1,nlstate,1,npar,1,npar,matcov);
5561: matprod2(doldm,dnewm,1,nlstate,1,npar,1,nlstate,gradg[k]);
5562: for(i=1;i<=nlstate;i++)
5563: for(j=1;j<=nlstate;j++)
5564: vareij[i][j][(int)age] += doldm[i][j]*hf*hf;
5565: }
5566: }
5567:
5568: /* pptj */
5569: matprod2(dnewmp,trgradgp,nlstate+1,nlstate+ndeath,1,npar,1,npar,matcov);
5570: matprod2(doldmp,dnewmp,nlstate+1,nlstate+ndeath,1,npar,nlstate+1,nlstate+ndeath,gradgp);
5571: for(j=nlstate+1;j<=nlstate+ndeath;j++)
5572: for(i=nlstate+1;i<=nlstate+ndeath;i++)
5573: varppt[j][i]=doldmp[j][i];
5574: /* end ppptj */
5575: /* x centered again */
5576:
1.242 brouard 5577: prevalim(prlim,nlstate,x,age,oldm,savm,ftolpl,ncvyearp,ij, nres);
1.218 brouard 5578:
5579: if (popbased==1) {
5580: if(mobilav ==0){
5581: for(i=1; i<=nlstate;i++)
5582: prlim[i][i]=probs[(int)age][i][ij];
5583: }else{ /* mobilav */
5584: for(i=1; i<=nlstate;i++)
5585: prlim[i][i]=mobaverage[(int)age][i][ij];
5586: }
5587: }
5588:
5589: /* This for computing probability of death (h=1 means
5590: computed over hstepm (estepm) matrices product = hstepm*stepm months)
5591: as a weighted average of prlim.
5592: */
1.235 brouard 5593: hpxij(p3mat,nhstepm,age,hstepm,x,nlstate,stepm,oldm,savm, ij, nres);
1.218 brouard 5594: for(j=nlstate+1;j<=nlstate+ndeath;j++){
5595: for(i=1,gmp[j]=0.;i<= nlstate; i++)
5596: gmp[j] += prlim[i][i]*p3mat[i][j][1];
5597: }
5598: /* end probability of death */
5599:
5600: fprintf(ficresprobmorprev,"%3d %d ",(int) age, ij);
5601: for(j=nlstate+1; j<=(nlstate+ndeath);j++){
5602: fprintf(ficresprobmorprev," %11.3e %11.3e",gmp[j], sqrt(varppt[j][j]));
5603: for(i=1; i<=nlstate;i++){
5604: fprintf(ficresprobmorprev," %11.3e %11.3e ",prlim[i][i],p3mat[i][j][1]);
5605: }
5606: }
5607: fprintf(ficresprobmorprev,"\n");
5608:
5609: fprintf(ficresvij,"%.0f ",age );
5610: for(i=1; i<=nlstate;i++)
5611: for(j=1; j<=nlstate;j++){
5612: fprintf(ficresvij," %.4f", vareij[i][j][(int)age]);
5613: }
5614: fprintf(ficresvij,"\n");
5615: free_matrix(gp,0,nhstepm,1,nlstate);
5616: free_matrix(gm,0,nhstepm,1,nlstate);
5617: free_ma3x(gradg,0,nhstepm,1,npar,1,nlstate);
5618: free_ma3x(trgradg,0,nhstepm,1,nlstate,1,npar);
5619: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
5620: } /* End age */
5621: free_vector(gpp,nlstate+1,nlstate+ndeath);
5622: free_vector(gmp,nlstate+1,nlstate+ndeath);
5623: free_matrix(gradgp,1,npar,nlstate+1,nlstate+ndeath);
5624: free_matrix(trgradgp,nlstate+1,nlstate+ndeath,1,npar); /* mu or p point j*/
5625: /* fprintf(ficgp,"\nunset parametric;unset label; set ter png small size 320, 240"); */
5626: fprintf(ficgp,"\nunset parametric;unset label; set ter svg size 640, 480");
5627: /* for(j=nlstate+1; j<= nlstate+ndeath; j++){ *//* Only the first actually */
5628: fprintf(ficgp,"\n set log y; unset log x;set xlabel \"Age\"; set ylabel \"Force of mortality (year-1)\";");
5629: fprintf(ficgp,"\nset out \"%s%s.svg\";",subdirf3(optionfilefiname,"VARMUPTJGR-",digitp),digit);
5630: /* fprintf(ficgp,"\n plot \"%s\" u 1:($3*%6.3f) not w l 1 ",fileresprobmorprev,YEARM/estepm); */
5631: /* fprintf(ficgp,"\n replot \"%s\" u 1:(($3+1.96*$4)*%6.3f) t \"95\%% interval\" w l 2 ",fileresprobmorprev,YEARM/estepm); */
5632: /* fprintf(ficgp,"\n replot \"%s\" u 1:(($3-1.96*$4)*%6.3f) not w l 2 ",fileresprobmorprev,YEARM/estepm); */
5633: fprintf(ficgp,"\n plot \"%s\" u 1:($3) not w l lt 1 ",subdirf(fileresprobmorprev));
5634: fprintf(ficgp,"\n replot \"%s\" u 1:(($3+1.96*$4)) t \"95%% interval\" w l lt 2 ",subdirf(fileresprobmorprev));
5635: fprintf(ficgp,"\n replot \"%s\" u 1:(($3-1.96*$4)) not w l lt 2 ",subdirf(fileresprobmorprev));
5636: fprintf(fichtm,"\n<br> File (multiple files are possible if covariates are present): <A href=\"%s\">%s</a>\n",subdirf(fileresprobmorprev),subdirf(fileresprobmorprev));
5637: 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);
5638: /* 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 5639: */
1.218 brouard 5640: /* fprintf(ficgp,"\nset out \"varmuptjgr%s%s%s.svg\";replot;",digitp,optionfilefiname,digit); */
5641: fprintf(ficgp,"\nset out;\nset out \"%s%s.svg\";replot;set out;\n",subdirf3(optionfilefiname,"VARMUPTJGR-",digitp),digit);
1.126 brouard 5642:
1.218 brouard 5643: free_vector(xp,1,npar);
5644: free_matrix(doldm,1,nlstate,1,nlstate);
5645: free_matrix(dnewm,1,nlstate,1,npar);
5646: free_matrix(doldmp,nlstate+1,nlstate+ndeath,nlstate+1,nlstate+ndeath);
5647: free_matrix(dnewmp,nlstate+1,nlstate+ndeath,1,npar);
5648: free_matrix(varppt,nlstate+1,nlstate+ndeath,nlstate+1,nlstate+ndeath);
5649: /* if (mobilav!=0) free_ma3x(mobaverage,1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
5650: fclose(ficresprobmorprev);
5651: fflush(ficgp);
5652: fflush(fichtm);
5653: } /* end varevsij */
1.126 brouard 5654:
5655: /************ Variance of prevlim ******************/
1.235 brouard 5656: 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 5657: {
1.205 brouard 5658: /* Variance of prevalence limit for each state ij using current parameters x[] and estimates of neighbourhood give by delti*/
1.126 brouard 5659: /* double **prevalim(double **prlim, int nlstate, double *xp, double age, double **oldm, double **savm,double ftolpl);*/
1.164 brouard 5660:
1.126 brouard 5661: double **dnewm,**doldm;
5662: int i, j, nhstepm, hstepm;
5663: double *xp;
5664: double *gp, *gm;
5665: double **gradg, **trgradg;
1.208 brouard 5666: double **mgm, **mgp;
1.126 brouard 5667: double age,agelim;
5668: int theta;
5669:
5670: pstamp(ficresvpl);
5671: fprintf(ficresvpl,"# Standard deviation of period (stable) prevalences \n");
1.241 brouard 5672: fprintf(ficresvpl,"# Age ");
5673: if(nresult >=1)
5674: fprintf(ficresvpl," Result# ");
1.126 brouard 5675: for(i=1; i<=nlstate;i++)
5676: fprintf(ficresvpl," %1d-%1d",i,i);
5677: fprintf(ficresvpl,"\n");
5678:
5679: xp=vector(1,npar);
5680: dnewm=matrix(1,nlstate,1,npar);
5681: doldm=matrix(1,nlstate,1,nlstate);
5682:
5683: hstepm=1*YEARM; /* Every year of age */
5684: hstepm=hstepm/stepm; /* Typically in stepm units, if j= 2 years, = 2/6 months = 4 */
5685: agelim = AGESUP;
5686: for (age=bage; age<=fage; age ++){ /* If stepm=6 months */
5687: nhstepm=(int) rint((agelim-age)*YEARM/stepm); /* Typically 20 years = 20*12/6=40 */
5688: if (stepm >= YEARM) hstepm=1;
5689: nhstepm = nhstepm/hstepm; /* Typically 40/4=10 */
5690: gradg=matrix(1,npar,1,nlstate);
1.208 brouard 5691: mgp=matrix(1,npar,1,nlstate);
5692: mgm=matrix(1,npar,1,nlstate);
1.126 brouard 5693: gp=vector(1,nlstate);
5694: gm=vector(1,nlstate);
5695:
5696: for(theta=1; theta <=npar; theta++){
5697: for(i=1; i<=npar; i++){ /* Computes gradient */
5698: xp[i] = x[i] + (i==theta ?delti[theta]:0);
5699: }
1.209 brouard 5700: if((int)age==79 ||(int)age== 80 ||(int)age== 81 )
1.235 brouard 5701: prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp,ij,nres);
1.209 brouard 5702: else
1.235 brouard 5703: prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp,ij,nres);
1.208 brouard 5704: for(i=1;i<=nlstate;i++){
1.126 brouard 5705: gp[i] = prlim[i][i];
1.208 brouard 5706: mgp[theta][i] = prlim[i][i];
5707: }
1.126 brouard 5708: for(i=1; i<=npar; i++) /* Computes gradient */
5709: xp[i] = x[i] - (i==theta ?delti[theta]:0);
1.209 brouard 5710: if((int)age==79 ||(int)age== 80 ||(int)age== 81 )
1.235 brouard 5711: prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp,ij,nres);
1.209 brouard 5712: else
1.235 brouard 5713: prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp,ij,nres);
1.208 brouard 5714: for(i=1;i<=nlstate;i++){
1.126 brouard 5715: gm[i] = prlim[i][i];
1.208 brouard 5716: mgm[theta][i] = prlim[i][i];
5717: }
1.126 brouard 5718: for(i=1;i<=nlstate;i++)
5719: gradg[theta][i]= (gp[i]-gm[i])/2./delti[theta];
1.209 brouard 5720: /* gradg[theta][2]= -gradg[theta][1]; */ /* For testing if nlstate=2 */
1.126 brouard 5721: } /* End theta */
5722:
5723: trgradg =matrix(1,nlstate,1,npar);
5724:
5725: for(j=1; j<=nlstate;j++)
5726: for(theta=1; theta <=npar; theta++)
5727: trgradg[j][theta]=gradg[theta][j];
1.209 brouard 5728: /* if((int)age==79 ||(int)age== 80 ||(int)age== 81 ){ */
5729: /* printf("\nmgm mgp %d ",(int)age); */
5730: /* for(j=1; j<=nlstate;j++){ */
5731: /* printf(" %d ",j); */
5732: /* for(theta=1; theta <=npar; theta++) */
5733: /* printf(" %d %lf %lf",theta,mgm[theta][j],mgp[theta][j]); */
5734: /* printf("\n "); */
5735: /* } */
5736: /* } */
5737: /* if((int)age==79 ||(int)age== 80 ||(int)age== 81 ){ */
5738: /* printf("\n gradg %d ",(int)age); */
5739: /* for(j=1; j<=nlstate;j++){ */
5740: /* printf("%d ",j); */
5741: /* for(theta=1; theta <=npar; theta++) */
5742: /* printf("%d %lf ",theta,gradg[theta][j]); */
5743: /* printf("\n "); */
5744: /* } */
5745: /* } */
1.126 brouard 5746:
5747: for(i=1;i<=nlstate;i++)
5748: varpl[i][(int)age] =0.;
1.209 brouard 5749: if((int)age==79 ||(int)age== 80 ||(int)age== 81){
1.205 brouard 5750: matprod2(dnewm,trgradg,1,nlstate,1,npar,1,npar,matcov);
5751: matprod2(doldm,dnewm,1,nlstate,1,npar,1,nlstate,gradg);
5752: }else{
1.126 brouard 5753: matprod2(dnewm,trgradg,1,nlstate,1,npar,1,npar,matcov);
5754: matprod2(doldm,dnewm,1,nlstate,1,npar,1,nlstate,gradg);
1.205 brouard 5755: }
1.126 brouard 5756: for(i=1;i<=nlstate;i++)
5757: varpl[i][(int)age] = doldm[i][i]; /* Covariances are useless */
5758:
5759: fprintf(ficresvpl,"%.0f ",age );
1.241 brouard 5760: if(nresult >=1)
5761: fprintf(ficresvpl,"%d ",nres );
1.126 brouard 5762: for(i=1; i<=nlstate;i++)
5763: fprintf(ficresvpl," %.5f (%.5f)",prlim[i][i],sqrt(varpl[i][(int)age]));
5764: fprintf(ficresvpl,"\n");
5765: free_vector(gp,1,nlstate);
5766: free_vector(gm,1,nlstate);
1.208 brouard 5767: free_matrix(mgm,1,npar,1,nlstate);
5768: free_matrix(mgp,1,npar,1,nlstate);
1.126 brouard 5769: free_matrix(gradg,1,npar,1,nlstate);
5770: free_matrix(trgradg,1,nlstate,1,npar);
5771: } /* End age */
5772:
5773: free_vector(xp,1,npar);
5774: free_matrix(doldm,1,nlstate,1,npar);
5775: free_matrix(dnewm,1,nlstate,1,nlstate);
5776:
5777: }
5778:
5779: /************ Variance of one-step probabilities ******************/
5780: 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 5781: {
5782: int i, j=0, k1, l1, tj;
5783: int k2, l2, j1, z1;
5784: int k=0, l;
5785: int first=1, first1, first2;
5786: double cv12, mu1, mu2, lc1, lc2, v12, v21, v11, v22,v1,v2, c12, tnalp;
5787: double **dnewm,**doldm;
5788: double *xp;
5789: double *gp, *gm;
5790: double **gradg, **trgradg;
5791: double **mu;
5792: double age, cov[NCOVMAX+1];
5793: double std=2.0; /* Number of standard deviation wide of confidence ellipsoids */
5794: int theta;
5795: char fileresprob[FILENAMELENGTH];
5796: char fileresprobcov[FILENAMELENGTH];
5797: char fileresprobcor[FILENAMELENGTH];
5798: double ***varpij;
5799:
5800: strcpy(fileresprob,"PROB_");
5801: strcat(fileresprob,fileres);
5802: if((ficresprob=fopen(fileresprob,"w"))==NULL) {
5803: printf("Problem with resultfile: %s\n", fileresprob);
5804: fprintf(ficlog,"Problem with resultfile: %s\n", fileresprob);
5805: }
5806: strcpy(fileresprobcov,"PROBCOV_");
5807: strcat(fileresprobcov,fileresu);
5808: if((ficresprobcov=fopen(fileresprobcov,"w"))==NULL) {
5809: printf("Problem with resultfile: %s\n", fileresprobcov);
5810: fprintf(ficlog,"Problem with resultfile: %s\n", fileresprobcov);
5811: }
5812: strcpy(fileresprobcor,"PROBCOR_");
5813: strcat(fileresprobcor,fileresu);
5814: if((ficresprobcor=fopen(fileresprobcor,"w"))==NULL) {
5815: printf("Problem with resultfile: %s\n", fileresprobcor);
5816: fprintf(ficlog,"Problem with resultfile: %s\n", fileresprobcor);
5817: }
5818: printf("Computing standard deviation of one-step probabilities: result on file '%s' \n",fileresprob);
5819: fprintf(ficlog,"Computing standard deviation of one-step probabilities: result on file '%s' \n",fileresprob);
5820: printf("Computing matrix of variance covariance of one-step probabilities: result on file '%s' \n",fileresprobcov);
5821: fprintf(ficlog,"Computing matrix of variance covariance of one-step probabilities: result on file '%s' \n",fileresprobcov);
5822: printf("and correlation matrix of one-step probabilities: result on file '%s' \n",fileresprobcor);
5823: fprintf(ficlog,"and correlation matrix of one-step probabilities: result on file '%s' \n",fileresprobcor);
5824: pstamp(ficresprob);
5825: fprintf(ficresprob,"#One-step probabilities and stand. devi in ()\n");
5826: fprintf(ficresprob,"# Age");
5827: pstamp(ficresprobcov);
5828: fprintf(ficresprobcov,"#One-step probabilities and covariance matrix\n");
5829: fprintf(ficresprobcov,"# Age");
5830: pstamp(ficresprobcor);
5831: fprintf(ficresprobcor,"#One-step probabilities and correlation matrix\n");
5832: fprintf(ficresprobcor,"# Age");
1.126 brouard 5833:
5834:
1.222 brouard 5835: for(i=1; i<=nlstate;i++)
5836: for(j=1; j<=(nlstate+ndeath);j++){
5837: fprintf(ficresprob," p%1d-%1d (SE)",i,j);
5838: fprintf(ficresprobcov," p%1d-%1d ",i,j);
5839: fprintf(ficresprobcor," p%1d-%1d ",i,j);
5840: }
5841: /* fprintf(ficresprob,"\n");
5842: fprintf(ficresprobcov,"\n");
5843: fprintf(ficresprobcor,"\n");
5844: */
5845: xp=vector(1,npar);
5846: dnewm=matrix(1,(nlstate)*(nlstate+ndeath),1,npar);
5847: doldm=matrix(1,(nlstate)*(nlstate+ndeath),1,(nlstate)*(nlstate+ndeath));
5848: mu=matrix(1,(nlstate)*(nlstate+ndeath), (int) bage, (int)fage);
5849: varpij=ma3x(1,nlstate*(nlstate+ndeath),1,nlstate*(nlstate+ndeath),(int) bage, (int) fage);
5850: first=1;
5851: fprintf(ficgp,"\n# Routine varprob");
5852: fprintf(fichtm,"\n<li><h4> Computing and drawing one step probabilities with their confidence intervals</h4></li>\n");
5853: fprintf(fichtm,"\n");
5854:
5855: 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);
5856: 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);
5857: fprintf(fichtmcov,"\nEllipsoids of confidence centered on point (p<inf>ij</inf>, p<inf>kl</inf>) are estimated \
1.126 brouard 5858: and drawn. It helps understanding how is the covariance between two incidences.\
5859: They are expressed in year<sup>-1</sup> in order to be less dependent of stepm.<br>\n");
1.222 brouard 5860: 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 5861: It can be understood this way: if pij and pkl where uncorrelated the (2x2) matrix of covariance \
5862: would have been (1/(var pij), 0 , 0, 1/(var pkl)), and the confidence interval would be 2 \
5863: standard deviations wide on each axis. <br>\
5864: Now, if both incidences are correlated (usual case) we diagonalised the inverse of the covariance matrix\
5865: and made the appropriate rotation to look at the uncorrelated principal directions.<br>\
5866: To be simple, these graphs help to understand the significativity of each parameter in relation to a second other one.<br> \n");
5867:
1.222 brouard 5868: cov[1]=1;
5869: /* tj=cptcoveff; */
1.225 brouard 5870: tj = (int) pow(2,cptcoveff);
1.222 brouard 5871: if (cptcovn<1) {tj=1;ncodemax[1]=1;}
5872: j1=0;
1.224 brouard 5873: for(j1=1; j1<=tj;j1++){ /* For each valid combination of covariates or only once*/
1.222 brouard 5874: if (cptcovn>0) {
5875: fprintf(ficresprob, "\n#********** Variable ");
1.225 brouard 5876: for (z1=1; z1<=cptcoveff; z1++) fprintf(ficresprob, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
1.222 brouard 5877: fprintf(ficresprob, "**********\n#\n");
5878: fprintf(ficresprobcov, "\n#********** Variable ");
1.225 brouard 5879: for (z1=1; z1<=cptcoveff; z1++) fprintf(ficresprobcov, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
1.222 brouard 5880: fprintf(ficresprobcov, "**********\n#\n");
1.220 brouard 5881:
1.222 brouard 5882: fprintf(ficgp, "\n#********** Variable ");
1.225 brouard 5883: for (z1=1; z1<=cptcoveff; z1++) fprintf(ficgp, " V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
1.222 brouard 5884: fprintf(ficgp, "**********\n#\n");
1.220 brouard 5885:
5886:
1.222 brouard 5887: fprintf(fichtmcov, "\n<hr size=\"2\" color=\"#EC5E5E\">********** Variable ");
1.225 brouard 5888: for (z1=1; z1<=cptcoveff; z1++) fprintf(fichtm, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
1.222 brouard 5889: fprintf(fichtmcov, "**********\n<hr size=\"2\" color=\"#EC5E5E\">");
1.220 brouard 5890:
1.222 brouard 5891: fprintf(ficresprobcor, "\n#********** Variable ");
1.225 brouard 5892: for (z1=1; z1<=cptcoveff; z1++) fprintf(ficresprobcor, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
1.222 brouard 5893: fprintf(ficresprobcor, "**********\n#");
5894: if(invalidvarcomb[j1]){
5895: fprintf(ficgp,"\n#Combination (%d) ignored because no cases \n",j1);
5896: fprintf(fichtmcov,"\n<h3>Combination (%d) ignored because no cases </h3>\n",j1);
5897: continue;
5898: }
5899: }
5900: gradg=matrix(1,npar,1,(nlstate)*(nlstate+ndeath));
5901: trgradg=matrix(1,(nlstate)*(nlstate+ndeath),1,npar);
5902: gp=vector(1,(nlstate)*(nlstate+ndeath));
5903: gm=vector(1,(nlstate)*(nlstate+ndeath));
5904: for (age=bage; age<=fage; age ++){
5905: cov[2]=age;
5906: if(nagesqr==1)
5907: cov[3]= age*age;
5908: for (k=1; k<=cptcovn;k++) {
5909: cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(j1,k)];
5910: /*cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(j1,Tvar[k])];*//* j1 1 2 3 4
5911: * 1 1 1 1 1
5912: * 2 2 1 1 1
5913: * 3 1 2 1 1
5914: */
5915: /* nbcode[1][1]=0 nbcode[1][2]=1;*/
5916: }
5917: /* for (k=1; k<=cptcovage;k++) cov[2+Tage[k]]=cov[2+Tage[k]]*cov[2]; */
5918: for (k=1; k<=cptcovage;k++) cov[2+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,k)]*cov[2];
5919: for (k=1; k<=cptcovprod;k++)
5920: cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)]*nbcode[Tvard[k][2]][codtabm(ij,k)];
1.220 brouard 5921:
5922:
1.222 brouard 5923: for(theta=1; theta <=npar; theta++){
5924: for(i=1; i<=npar; i++)
5925: xp[i] = x[i] + (i==theta ?delti[theta]:(double)0);
1.220 brouard 5926:
1.222 brouard 5927: pmij(pmmij,cov,ncovmodel,xp,nlstate);
1.220 brouard 5928:
1.222 brouard 5929: k=0;
5930: for(i=1; i<= (nlstate); i++){
5931: for(j=1; j<=(nlstate+ndeath);j++){
5932: k=k+1;
5933: gp[k]=pmmij[i][j];
5934: }
5935: }
1.220 brouard 5936:
1.222 brouard 5937: for(i=1; i<=npar; i++)
5938: xp[i] = x[i] - (i==theta ?delti[theta]:(double)0);
1.220 brouard 5939:
1.222 brouard 5940: pmij(pmmij,cov,ncovmodel,xp,nlstate);
5941: k=0;
5942: for(i=1; i<=(nlstate); i++){
5943: for(j=1; j<=(nlstate+ndeath);j++){
5944: k=k+1;
5945: gm[k]=pmmij[i][j];
5946: }
5947: }
1.220 brouard 5948:
1.222 brouard 5949: for(i=1; i<= (nlstate)*(nlstate+ndeath); i++)
5950: gradg[theta][i]=(gp[i]-gm[i])/(double)2./delti[theta];
5951: }
1.126 brouard 5952:
1.222 brouard 5953: for(j=1; j<=(nlstate)*(nlstate+ndeath);j++)
5954: for(theta=1; theta <=npar; theta++)
5955: trgradg[j][theta]=gradg[theta][j];
1.220 brouard 5956:
1.222 brouard 5957: matprod2(dnewm,trgradg,1,(nlstate)*(nlstate+ndeath),1,npar,1,npar,matcov);
5958: matprod2(doldm,dnewm,1,(nlstate)*(nlstate+ndeath),1,npar,1,(nlstate)*(nlstate+ndeath),gradg);
1.220 brouard 5959:
1.222 brouard 5960: pmij(pmmij,cov,ncovmodel,x,nlstate);
1.220 brouard 5961:
1.222 brouard 5962: k=0;
5963: for(i=1; i<=(nlstate); i++){
5964: for(j=1; j<=(nlstate+ndeath);j++){
5965: k=k+1;
5966: mu[k][(int) age]=pmmij[i][j];
5967: }
5968: }
5969: for(i=1;i<=(nlstate)*(nlstate+ndeath);i++)
5970: for(j=1;j<=(nlstate)*(nlstate+ndeath);j++)
5971: varpij[i][j][(int)age] = doldm[i][j];
1.220 brouard 5972:
1.222 brouard 5973: /*printf("\n%d ",(int)age);
5974: for (i=1; i<=(nlstate)*(nlstate+ndeath);i++){
5975: printf("%e [%e ;%e] ",gm[i],gm[i]-2*sqrt(doldm[i][i]),gm[i]+2*sqrt(doldm[i][i]));
5976: fprintf(ficlog,"%e [%e ;%e] ",gm[i],gm[i]-2*sqrt(doldm[i][i]),gm[i]+2*sqrt(doldm[i][i]));
5977: }*/
1.220 brouard 5978:
1.222 brouard 5979: fprintf(ficresprob,"\n%d ",(int)age);
5980: fprintf(ficresprobcov,"\n%d ",(int)age);
5981: fprintf(ficresprobcor,"\n%d ",(int)age);
1.220 brouard 5982:
1.222 brouard 5983: for (i=1; i<=(nlstate)*(nlstate+ndeath);i++)
5984: fprintf(ficresprob,"%11.3e (%11.3e) ",mu[i][(int) age],sqrt(varpij[i][i][(int)age]));
5985: for (i=1; i<=(nlstate)*(nlstate+ndeath);i++){
5986: fprintf(ficresprobcov,"%11.3e ",mu[i][(int) age]);
5987: fprintf(ficresprobcor,"%11.3e ",mu[i][(int) age]);
5988: }
5989: i=0;
5990: for (k=1; k<=(nlstate);k++){
5991: for (l=1; l<=(nlstate+ndeath);l++){
5992: i++;
5993: fprintf(ficresprobcov,"\n%d %d-%d",(int)age,k,l);
5994: fprintf(ficresprobcor,"\n%d %d-%d",(int)age,k,l);
5995: for (j=1; j<=i;j++){
5996: /* printf(" k=%d l=%d i=%d j=%d\n",k,l,i,j);fflush(stdout); */
5997: fprintf(ficresprobcov," %11.3e",varpij[i][j][(int)age]);
5998: fprintf(ficresprobcor," %11.3e",varpij[i][j][(int) age]/sqrt(varpij[i][i][(int) age])/sqrt(varpij[j][j][(int)age]));
5999: }
6000: }
6001: }/* end of loop for state */
6002: } /* end of loop for age */
6003: free_vector(gp,1,(nlstate+ndeath)*(nlstate+ndeath));
6004: free_vector(gm,1,(nlstate+ndeath)*(nlstate+ndeath));
6005: free_matrix(trgradg,1,(nlstate+ndeath)*(nlstate+ndeath),1,npar);
6006: free_matrix(gradg,1,(nlstate+ndeath)*(nlstate+ndeath),1,npar);
6007:
6008: /* Confidence intervalle of pij */
6009: /*
6010: fprintf(ficgp,"\nunset parametric;unset label");
6011: fprintf(ficgp,"\nset log y;unset log x; set xlabel \"Age\";set ylabel \"probability (year-1)\"");
6012: fprintf(ficgp,"\nset ter png small\nset size 0.65,0.65");
6013: 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);
6014: fprintf(fichtm,"\n<br><img src=\"pijgr%s.png\"> ",optionfilefiname);
6015: fprintf(ficgp,"\nset out \"pijgr%s.png\"",optionfilefiname);
6016: fprintf(ficgp,"\nplot \"%s\" every :::%d::%d u 1:2 \"\%%lf",k1,k2,xfilevarprob);
6017: */
6018:
6019: /* Drawing ellipsoids of confidence of two variables p(k1-l1,k2-l2)*/
6020: first1=1;first2=2;
6021: for (k2=1; k2<=(nlstate);k2++){
6022: for (l2=1; l2<=(nlstate+ndeath);l2++){
6023: if(l2==k2) continue;
6024: j=(k2-1)*(nlstate+ndeath)+l2;
6025: for (k1=1; k1<=(nlstate);k1++){
6026: for (l1=1; l1<=(nlstate+ndeath);l1++){
6027: if(l1==k1) continue;
6028: i=(k1-1)*(nlstate+ndeath)+l1;
6029: if(i<=j) continue;
6030: for (age=bage; age<=fage; age ++){
6031: if ((int)age %5==0){
6032: v1=varpij[i][i][(int)age]/stepm*YEARM/stepm*YEARM;
6033: v2=varpij[j][j][(int)age]/stepm*YEARM/stepm*YEARM;
6034: cv12=varpij[i][j][(int)age]/stepm*YEARM/stepm*YEARM;
6035: mu1=mu[i][(int) age]/stepm*YEARM ;
6036: mu2=mu[j][(int) age]/stepm*YEARM;
6037: c12=cv12/sqrt(v1*v2);
6038: /* Computing eigen value of matrix of covariance */
6039: lc1=((v1+v2)+sqrt((v1+v2)*(v1+v2) - 4*(v1*v2-cv12*cv12)))/2.;
6040: lc2=((v1+v2)-sqrt((v1+v2)*(v1+v2) - 4*(v1*v2-cv12*cv12)))/2.;
6041: if ((lc2 <0) || (lc1 <0) ){
6042: if(first2==1){
6043: first1=0;
6044: 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);
6045: }
6046: 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);
6047: /* lc1=fabs(lc1); */ /* If we want to have them positive */
6048: /* lc2=fabs(lc2); */
6049: }
1.220 brouard 6050:
1.222 brouard 6051: /* Eigen vectors */
6052: v11=(1./sqrt(1+(v1-lc1)*(v1-lc1)/cv12/cv12));
6053: /*v21=sqrt(1.-v11*v11); *//* error */
6054: v21=(lc1-v1)/cv12*v11;
6055: v12=-v21;
6056: v22=v11;
6057: tnalp=v21/v11;
6058: if(first1==1){
6059: first1=0;
6060: 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);
6061: }
6062: 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);
6063: /*printf(fignu*/
6064: /* mu1+ v11*lc1*cost + v12*lc2*sin(t) */
6065: /* mu2+ v21*lc1*cost + v22*lc2*sin(t) */
6066: if(first==1){
6067: first=0;
6068: fprintf(ficgp,"\n# Ellipsoids of confidence\n#\n");
6069: fprintf(ficgp,"\nset parametric;unset label");
6070: 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);
6071: fprintf(ficgp,"\nset ter svg size 640, 480");
6072: fprintf(fichtmcov,"\n<br>Ellipsoids of confidence cov(p%1d%1d,p%1d%1d) expressed in year<sup>-1</sup>\
1.220 brouard 6073: :<a href=\"%s_%d%1d%1d-%1d%1d.svg\"> \
1.201 brouard 6074: %s_%d%1d%1d-%1d%1d.svg</A>, ",k1,l1,k2,l2,\
1.222 brouard 6075: subdirf2(optionfilefiname,"VARPIJGR_"), j1,k1,l1,k2,l2, \
6076: subdirf2(optionfilefiname,"VARPIJGR_"), j1,k1,l1,k2,l2);
6077: fprintf(fichtmcov,"\n<br><img src=\"%s_%d%1d%1d-%1d%1d.svg\"> ",subdirf2(optionfilefiname,"VARPIJGR_"), j1,k1,l1,k2,l2);
6078: fprintf(fichtmcov,"\n<br> Correlation at age %d (%.3f),",(int) age, c12);
6079: fprintf(ficgp,"\nset out \"%s_%d%1d%1d-%1d%1d.svg\"",subdirf2(optionfilefiname,"VARPIJGR_"), j1,k1,l1,k2,l2);
6080: fprintf(ficgp,"\nset label \"%d\" at %11.3e,%11.3e center",(int) age, mu1,mu2);
6081: fprintf(ficgp,"\n# Age %d, p%1d%1d - p%1d%1d",(int) age, k1,l1,k2,l2);
6082: 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", \
6083: mu1,std,v11,sqrt(lc1),v12,sqrt(lc2), \
6084: mu2,std,v21,sqrt(lc1),v22,sqrt(lc2));
6085: }else{
6086: first=0;
6087: fprintf(fichtmcov," %d (%.3f),",(int) age, c12);
6088: fprintf(ficgp,"\n# Age %d, p%1d%1d - p%1d%1d",(int) age, k1,l1,k2,l2);
6089: fprintf(ficgp,"\nset label \"%d\" at %11.3e,%11.3e center",(int) age, mu1,mu2);
6090: 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", \
6091: mu1,std,v11,sqrt(lc1),v12,sqrt(lc2), \
6092: mu2,std,v21,sqrt(lc1),v22,sqrt(lc2));
6093: }/* if first */
6094: } /* age mod 5 */
6095: } /* end loop age */
6096: fprintf(ficgp,"\nset out;\nset out \"%s_%d%1d%1d-%1d%1d.svg\";replot;set out;",subdirf2(optionfilefiname,"VARPIJGR_"), j1,k1,l1,k2,l2);
6097: first=1;
6098: } /*l12 */
6099: } /* k12 */
6100: } /*l1 */
6101: }/* k1 */
6102: } /* loop on combination of covariates j1 */
6103: free_ma3x(varpij,1,nlstate,1,nlstate+ndeath,(int) bage, (int)fage);
6104: free_matrix(mu,1,(nlstate+ndeath)*(nlstate+ndeath),(int) bage, (int)fage);
6105: free_matrix(doldm,1,(nlstate)*(nlstate+ndeath),1,(nlstate)*(nlstate+ndeath));
6106: free_matrix(dnewm,1,(nlstate)*(nlstate+ndeath),1,npar);
6107: free_vector(xp,1,npar);
6108: fclose(ficresprob);
6109: fclose(ficresprobcov);
6110: fclose(ficresprobcor);
6111: fflush(ficgp);
6112: fflush(fichtmcov);
6113: }
1.126 brouard 6114:
6115:
6116: /******************* Printing html file ***********/
1.201 brouard 6117: void printinghtml(char fileresu[], char title[], char datafile[], int firstpass, \
1.126 brouard 6118: int lastpass, int stepm, int weightopt, char model[],\
6119: int imx,int jmin, int jmax, double jmeanint,char rfileres[],\
1.217 brouard 6120: int popforecast, int prevfcast, int backcast, int estepm , \
1.213 brouard 6121: double jprev1, double mprev1,double anprev1, double dateprev1, \
6122: double jprev2, double mprev2,double anprev2, double dateprev2){
1.237 brouard 6123: int jj1, k1, i1, cpt, k4, nres;
1.126 brouard 6124:
6125: fprintf(fichtm,"<ul><li><a href='#firstorder'>Result files (first order: no variance)</a>\n \
6126: <li><a href='#secondorder'>Result files (second order (variance)</a>\n \
6127: </ul>");
1.237 brouard 6128: fprintf(fichtm,"<ul><li> model=1+age+%s\n \
6129: </ul>", model);
1.214 brouard 6130: fprintf(fichtm,"<ul><li><h4><a name='firstorder'>Result files (first order: no variance)</a></h4>\n");
6131: 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",
6132: jprev1, mprev1,anprev1,jprev2, mprev2,anprev2,subdirfext3(optionfilefiname,"PHTMFR_",".htm"),subdirfext3(optionfilefiname,"PHTMFR_",".htm"));
6133: 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 6134: jprev1, mprev1,anprev1,jprev2, mprev2,anprev2,subdirfext3(optionfilefiname,"PHTM_",".htm"),subdirfext3(optionfilefiname,"PHTM_",".htm"));
6135: fprintf(fichtm,", <a href=\"%s\">%s</a> (text file) <br>\n",subdirf2(fileresu,"P_"),subdirf2(fileresu,"P_"));
1.126 brouard 6136: fprintf(fichtm,"\
6137: - Estimated transition probabilities over %d (stepm) months: <a href=\"%s\">%s</a><br>\n ",
1.201 brouard 6138: stepm,subdirf2(fileresu,"PIJ_"),subdirf2(fileresu,"PIJ_"));
1.126 brouard 6139: fprintf(fichtm,"\
1.217 brouard 6140: - Estimated back transition probabilities over %d (stepm) months: <a href=\"%s\">%s</a><br>\n ",
6141: stepm,subdirf2(fileresu,"PIJB_"),subdirf2(fileresu,"PIJB_"));
6142: fprintf(fichtm,"\
1.126 brouard 6143: - Period (stable) prevalence in each health state: <a href=\"%s\">%s</a> <br>\n",
1.201 brouard 6144: subdirf2(fileresu,"PL_"),subdirf2(fileresu,"PL_"));
1.126 brouard 6145: fprintf(fichtm,"\
1.217 brouard 6146: - Period (stable) back prevalence in each health state: <a href=\"%s\">%s</a> <br>\n",
6147: subdirf2(fileresu,"PLB_"),subdirf2(fileresu,"PLB_"));
6148: fprintf(fichtm,"\
1.211 brouard 6149: - (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 6150: <a href=\"%s\">%s</a> <br>\n",
1.201 brouard 6151: estepm,subdirf2(fileresu,"E_"),subdirf2(fileresu,"E_"));
1.211 brouard 6152: if(prevfcast==1){
6153: fprintf(fichtm,"\
6154: - Prevalence projections by age and states: \
1.201 brouard 6155: <a href=\"%s\">%s</a> <br>\n</li>", subdirf2(fileresu,"F_"),subdirf2(fileresu,"F_"));
1.211 brouard 6156: }
1.126 brouard 6157:
1.222 brouard 6158: fprintf(fichtm," \n<ul><li><b>Graphs</b></li><p>");
1.126 brouard 6159:
1.225 brouard 6160: m=pow(2,cptcoveff);
1.222 brouard 6161: if (cptcovn < 1) {m=1;ncodemax[1]=1;}
1.126 brouard 6162:
1.222 brouard 6163: jj1=0;
1.237 brouard 6164:
6165: for(nres=1; nres <= nresult; nres++) /* For each resultline */
1.241 brouard 6166: for(k1=1; k1<=m;k1++){ /* For each combination of covariate */
1.237 brouard 6167: if(TKresult[nres]!= k1)
6168: continue;
1.220 brouard 6169:
1.222 brouard 6170: /* for(i1=1; i1<=ncodemax[k1];i1++){ */
6171: jj1++;
6172: if (cptcovn > 0) {
6173: fprintf(fichtm,"<hr size=\"2\" color=\"#EC5E5E\">************ Results for covariates");
1.225 brouard 6174: for (cpt=1; cpt<=cptcoveff;cpt++){
1.237 brouard 6175: fprintf(fichtm," V%d=%d ",Tvresult[nres][cpt],(int)Tresult[nres][cpt]);
6176: printf(" V%d=%d ",Tvresult[nres][cpt],Tresult[nres][cpt]);fflush(stdout);
6177: /* fprintf(fichtm," V%d=%d ",Tvaraff[cpt],nbcode[Tvaraff[cpt]][codtabm(jj1,cpt)]); */
6178: /* printf(" V%d=%d ",Tvaraff[cpt],nbcode[Tvaraff[cpt]][codtabm(jj1,cpt)]);fflush(stdout); */
1.222 brouard 6179: }
1.237 brouard 6180: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
6181: fprintf(fichtm," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
6182: printf(" V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);fflush(stdout);
6183: }
6184:
1.230 brouard 6185: /* if(nqfveff+nqtveff 0) */ /* Test to be done */
1.222 brouard 6186: fprintf(fichtm," ************\n<hr size=\"2\" color=\"#EC5E5E\">");
6187: if(invalidvarcomb[k1]){
6188: fprintf(fichtm,"\n<h3>Combination (%d) ignored because no cases </h3>\n",k1);
6189: printf("\nCombination (%d) ignored because no cases \n",k1);
6190: continue;
6191: }
6192: }
6193: /* aij, bij */
1.241 brouard 6194: 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-%d.svg\">%s_%d-1-%d.svg</a><br> \
6195: <img src=\"%s_%d-1-%d.svg\">",model,subdirf2(optionfilefiname,"PE_"),k1,nres,subdirf2(optionfilefiname,"PE_"),k1,nres,subdirf2(optionfilefiname,"PE_"),k1,nres);
1.222 brouard 6196: /* Pij */
1.241 brouard 6197: fprintf(fichtm,"<br>\n- P<sub>ij</sub> or conditional probabilities to be observed in state j being in state i, %d (stepm) months before: <a href=\"%s_%d-2-%d.svg\">%s_%d-2-%d.svg</a><br> \
6198: <img src=\"%s_%d-2-%d.svg\">",stepm,subdirf2(optionfilefiname,"PE_"),k1,nres,subdirf2(optionfilefiname,"PE_"),k1,nres,subdirf2(optionfilefiname,"PE_"),k1,nres);
1.222 brouard 6199: /* Quasi-incidences */
6200: 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 6201: before but expressed in per year i.e. quasi incidences if stepm is small and probabilities too, \
1.211 brouard 6202: incidence (rates) are the limit when h tends to zero of the ratio of the probability <sub>h</sub>P<sub>ij</sub> \
1.241 brouard 6203: divided by h: <sub>h</sub>P<sub>ij</sub>/h : <a href=\"%s_%d-3-%d.svg\">%s_%d-3-%d.svg</a><br> \
6204: <img src=\"%s_%d-3-%d.svg\">",stepm,subdirf2(optionfilefiname,"PE_"),k1,nres,subdirf2(optionfilefiname,"PE_"),k1,nres,subdirf2(optionfilefiname,"PE_"),k1,nres);
1.222 brouard 6205: /* Survival functions (period) in state j */
6206: for(cpt=1; cpt<=nlstate;cpt++){
1.241 brouard 6207: 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-%d.svg\">%s_%d-%d-%d.svg</a><br> \
6208: <img src=\"%s_%d-%d-%d.svg\">", cpt, cpt, nlstate, subdirf2(optionfilefiname,"LIJ_"),cpt,k1,nres,subdirf2(optionfilefiname,"LIJ_"),cpt,k1,nres,subdirf2(optionfilefiname,"LIJ_"),cpt,k1,nres);
1.222 brouard 6209: }
6210: /* State specific survival functions (period) */
6211: for(cpt=1; cpt<=nlstate;cpt++){
6212: fprintf(fichtm,"<br>\n- Survival functions from state %d in each live state and total.\
1.220 brouard 6213: Or probability to survive in various states (1 to %d) being in state %d at different ages. \
1.241 brouard 6214: <a href=\"%s_%d-%d-%d.svg\">%s_%d%d-%d.svg</a><br> <img src=\"%s_%d-%d-%d.svg\">", cpt, nlstate, cpt, subdirf2(optionfilefiname,"LIJT_"),cpt,k1,nres,subdirf2(optionfilefiname,"LIJT_"),cpt,k1,nres,subdirf2(optionfilefiname,"LIJT_"),cpt,k1,nres);
1.222 brouard 6215: }
6216: /* Period (stable) prevalence in each health state */
6217: for(cpt=1; cpt<=nlstate;cpt++){
1.241 brouard 6218: 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-%d.svg\">%s_%d-%d-%d.svg</a><br> \
6219: <img src=\"%s_%d-%d-%d.svg\">", cpt, cpt, nlstate, subdirf2(optionfilefiname,"P_"),cpt,k1,nres,subdirf2(optionfilefiname,"P_"),cpt,k1,nres,subdirf2(optionfilefiname,"P_"),cpt,k1,nres);
1.222 brouard 6220: }
6221: if(backcast==1){
6222: /* Period (stable) back prevalence in each health state */
6223: for(cpt=1; cpt<=nlstate;cpt++){
1.241 brouard 6224: 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-%d.svg\">%s_%d-%d-%d.svg</a><br> \
6225: <img src=\"%s_%d-%d-%d.svg\">", cpt, cpt, nlstate, subdirf2(optionfilefiname,"PB_"),cpt,k1,nres,subdirf2(optionfilefiname,"PB_"),cpt,k1,nres,subdirf2(optionfilefiname,"PB_"),cpt,k1,nres);
1.222 brouard 6226: }
1.217 brouard 6227: }
1.222 brouard 6228: if(prevfcast==1){
6229: /* Projection of prevalence up to period (stable) prevalence in each health state */
6230: for(cpt=1; cpt<=nlstate;cpt++){
1.241 brouard 6231: 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-%d.svg\">%s_%d-%d-%d.svg</a><br> \
6232: <img src=\"%s_%d-%d-%d.svg\">", dateprev1, dateprev2, cpt, cpt, nlstate, subdirf2(optionfilefiname,"PROJ_"),cpt,k1,nres,subdirf2(optionfilefiname,"PROJ_"),cpt,k1,nres,subdirf2(optionfilefiname,"PROJ_"),cpt,k1,nres);
1.222 brouard 6233: }
6234: }
1.220 brouard 6235:
1.222 brouard 6236: for(cpt=1; cpt<=nlstate;cpt++) {
1.241 brouard 6237: fprintf(fichtm,"\n<br>- Life expectancy by health state (%d) at initial age and its decomposition into health expectancies in each alive state (1 to %d) (or area under each survival functions): <a href=\"%s_%d-%d-%d.svg\">%s_%d-%d-%d.svg</a> <br> \
6238: <img src=\"%s_%d-%d-%d.svg\">",cpt,nlstate,subdirf2(optionfilefiname,"EXP_"),cpt,k1,nres,subdirf2(optionfilefiname,"EXP_"),cpt,k1,nres,subdirf2(optionfilefiname,"EXP_"),cpt,k1,nres);
1.222 brouard 6239: }
6240: /* } /\* end i1 *\/ */
6241: }/* End k1 */
6242: fprintf(fichtm,"</ul>");
1.126 brouard 6243:
1.222 brouard 6244: fprintf(fichtm,"\
1.126 brouard 6245: \n<br><li><h4> <a name='secondorder'>Result files (second order: variances)</a></h4>\n\
1.193 brouard 6246: - Parameter file with estimated parameters and covariance matrix: <a href=\"%s\">%s</a> <br> \
1.203 brouard 6247: - 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 6248: But because parameters are usually highly correlated (a higher incidence of disability \
6249: and a higher incidence of recovery can give very close observed transition) it might \
6250: be very useful to look not only at linear confidence intervals estimated from the \
6251: variances but at the covariance matrix. And instead of looking at the estimated coefficients \
6252: (parameters) of the logistic regression, it might be more meaningful to visualize the \
6253: covariance matrix of the one-step probabilities. \
6254: See page 'Matrix of variance-covariance of one-step probabilities' below. \n", rfileres,rfileres);
1.126 brouard 6255:
1.222 brouard 6256: fprintf(fichtm," - Standard deviation of one-step probabilities: <a href=\"%s\">%s</a> <br>\n",
6257: subdirf2(fileresu,"PROB_"),subdirf2(fileresu,"PROB_"));
6258: fprintf(fichtm,"\
1.126 brouard 6259: - Variance-covariance of one-step probabilities: <a href=\"%s\">%s</a> <br>\n",
1.222 brouard 6260: subdirf2(fileresu,"PROBCOV_"),subdirf2(fileresu,"PROBCOV_"));
1.126 brouard 6261:
1.222 brouard 6262: fprintf(fichtm,"\
1.126 brouard 6263: - Correlation matrix of one-step probabilities: <a href=\"%s\">%s</a> <br>\n",
1.222 brouard 6264: subdirf2(fileresu,"PROBCOR_"),subdirf2(fileresu,"PROBCOR_"));
6265: fprintf(fichtm,"\
1.126 brouard 6266: - 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): \
6267: <a href=\"%s\">%s</a> <br>\n</li>",
1.201 brouard 6268: estepm,subdirf2(fileresu,"CVE_"),subdirf2(fileresu,"CVE_"));
1.222 brouard 6269: fprintf(fichtm,"\
1.126 brouard 6270: - (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): \
6271: <a href=\"%s\">%s</a> <br>\n</li>",
1.201 brouard 6272: estepm,subdirf2(fileresu,"STDE_"),subdirf2(fileresu,"STDE_"));
1.222 brouard 6273: fprintf(fichtm,"\
1.128 brouard 6274: - 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 6275: estepm, subdirf2(fileresu,"V_"),subdirf2(fileresu,"V_"));
6276: fprintf(fichtm,"\
1.128 brouard 6277: - 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 6278: estepm, subdirf2(fileresu,"T_"),subdirf2(fileresu,"T_"));
6279: fprintf(fichtm,"\
1.126 brouard 6280: - Standard deviation of period (stable) prevalences: <a href=\"%s\">%s</a> <br>\n",\
1.222 brouard 6281: subdirf2(fileresu,"VPL_"),subdirf2(fileresu,"VPL_"));
1.126 brouard 6282:
6283: /* if(popforecast==1) fprintf(fichtm,"\n */
6284: /* - Prevalences forecasting: <a href=\"f%s\">f%s</a> <br>\n */
6285: /* - Population forecasting (if popforecast=1): <a href=\"pop%s\">pop%s</a> <br>\n */
6286: /* <br>",fileres,fileres,fileres,fileres); */
6287: /* else */
6288: /* 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 6289: fflush(fichtm);
6290: fprintf(fichtm," <ul><li><b>Graphs</b></li><p>");
1.126 brouard 6291:
1.225 brouard 6292: m=pow(2,cptcoveff);
1.222 brouard 6293: if (cptcovn < 1) {m=1;ncodemax[1]=1;}
1.126 brouard 6294:
1.222 brouard 6295: jj1=0;
1.237 brouard 6296:
1.241 brouard 6297: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
1.222 brouard 6298: for(k1=1; k1<=m;k1++){
1.237 brouard 6299: if(TKresult[nres]!= k1)
6300: continue;
1.222 brouard 6301: /* for(i1=1; i1<=ncodemax[k1];i1++){ */
6302: jj1++;
1.126 brouard 6303: if (cptcovn > 0) {
6304: fprintf(fichtm,"<hr size=\"2\" color=\"#EC5E5E\">************ Results for covariates");
1.225 brouard 6305: for (cpt=1; cpt<=cptcoveff;cpt++) /**< cptcoveff number of variables */
1.237 brouard 6306: fprintf(fichtm," V%d=%d ",Tvresult[nres][cpt],Tresult[nres][cpt]);
6307: /* fprintf(fichtm," V%d=%d ",Tvaraff[cpt],nbcode[Tvaraff[cpt]][codtabm(jj1,cpt)]); */
6308: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
6309: fprintf(fichtm," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
6310: }
6311:
1.126 brouard 6312: fprintf(fichtm," ************\n<hr size=\"2\" color=\"#EC5E5E\">");
1.220 brouard 6313:
1.222 brouard 6314: if(invalidvarcomb[k1]){
6315: fprintf(fichtm,"\n<h4>Combination (%d) ignored because no cases </h4>\n",k1);
6316: continue;
6317: }
1.126 brouard 6318: }
6319: for(cpt=1; cpt<=nlstate;cpt++) {
1.218 brouard 6320: fprintf(fichtm,"\n<br>- Observed (cross-sectional) and period (incidence based) \
1.241 brouard 6321: prevalence (with 95%% confidence interval) in state (%d): <a href=\"%s_%d-%d-%d.svg\"> %s_%d-%d-%d.svg</a>\n <br>\
6322: <img src=\"%s_%d-%d-%d.svg\">",cpt,subdirf2(optionfilefiname,"V_"),cpt,k1,nres,subdirf2(optionfilefiname,"V_"),cpt,k1,nres,subdirf2(optionfilefiname,"V_"),cpt,k1,nres);
1.126 brouard 6323: }
6324: fprintf(fichtm,"\n<br>- Total life expectancy by age and \
1.128 brouard 6325: health expectancies in states (1) and (2). If popbased=1 the smooth (due to the model) \
6326: true period expectancies (those weighted with period prevalences are also\
6327: drawn in addition to the population based expectancies computed using\
1.241 brouard 6328: observed and cahotic prevalences: <a href=\"%s_%d-%d.svg\">%s_%d-%d.svg</a>\n<br>\
6329: <img src=\"%s_%d-%d.svg\">",subdirf2(optionfilefiname,"E_"),k1,nres,subdirf2(optionfilefiname,"E_"),k1,nres,subdirf2(optionfilefiname,"E_"),k1,nres);
1.222 brouard 6330: /* } /\* end i1 *\/ */
6331: }/* End k1 */
1.241 brouard 6332: }/* End nres */
1.222 brouard 6333: fprintf(fichtm,"</ul>");
6334: fflush(fichtm);
1.126 brouard 6335: }
6336:
6337: /******************* Gnuplot file **************/
1.223 brouard 6338: void printinggnuplot(char fileresu[], char optionfilefiname[], double ageminpar, double agemaxpar, double fage , int prevfcast, int backcast, char pathc[], double p[]){
1.126 brouard 6339:
6340: char dirfileres[132],optfileres[132];
1.223 brouard 6341: char gplotcondition[132];
1.237 brouard 6342: 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 6343: int lv=0, vlv=0, kl=0;
1.130 brouard 6344: int ng=0;
1.201 brouard 6345: int vpopbased;
1.223 brouard 6346: int ioffset; /* variable offset for columns */
1.235 brouard 6347: int nres=0; /* Index of resultline */
1.219 brouard 6348:
1.126 brouard 6349: /* if((ficgp=fopen(optionfilegnuplot,"a"))==NULL) { */
6350: /* printf("Problem with file %s",optionfilegnuplot); */
6351: /* fprintf(ficlog,"Problem with file %s",optionfilegnuplot); */
6352: /* } */
6353:
6354: /*#ifdef windows */
6355: fprintf(ficgp,"cd \"%s\" \n",pathc);
1.223 brouard 6356: /*#endif */
1.225 brouard 6357: m=pow(2,cptcoveff);
1.126 brouard 6358:
1.202 brouard 6359: /* Contribution to likelihood */
6360: /* Plot the probability implied in the likelihood */
1.223 brouard 6361: fprintf(ficgp,"\n# Contributions to the Likelihood, mle >=1. For mle=4 no interpolation, pure matrix products.\n#\n");
6362: fprintf(ficgp,"\n set log y; unset log x;set xlabel \"Age\"; set ylabel \"Likelihood (-2Log(L))\";");
6363: /* fprintf(ficgp,"\nset ter svg size 640, 480"); */ /* Too big for svg */
6364: fprintf(ficgp,"\nset ter pngcairo size 640, 480");
1.204 brouard 6365: /* nice for mle=4 plot by number of matrix products.
1.202 brouard 6366: replot "rrtest1/toto.txt" u 2:($4 == 1 && $5==2 ? $9 : 1/0):5 t "p12" with point lc 1 */
6367: /* replot exp(p1+p2*x)/(1+exp(p1+p2*x)+exp(p3+p4*x)+exp(p5+p6*x)) t "p12(x)" */
1.223 brouard 6368: /* fprintf(ficgp,"\nset out \"%s.svg\";",subdirf2(optionfilefiname,"ILK_")); */
6369: fprintf(ficgp,"\nset out \"%s-dest.png\";",subdirf2(optionfilefiname,"ILK_"));
6370: 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));
6371: fprintf(ficgp,"\nset out \"%s-ori.png\";",subdirf2(optionfilefiname,"ILK_"));
6372: 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));
6373: for (i=1; i<= nlstate ; i ++) {
6374: fprintf(ficgp,"\nset out \"%s-p%dj.png\";set ylabel \"Probability for each individual/wave\";",subdirf2(optionfilefiname,"ILK_"),i);
6375: fprintf(ficgp,"unset log;\n# plot weighted, mean weight should have point size of 0.5\n plot \"%s\"",subdirf(fileresilk));
6376: 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);
6377: for (j=2; j<= nlstate+ndeath ; j ++) {
6378: 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);
6379: }
6380: fprintf(ficgp,";\nset out; unset ylabel;\n");
6381: }
6382: /* 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 */
6383: /* fprintf(ficgp,"\nset log y;plot \"%s\" u 2:(-$11):3 t \"All sample, all transitions\" with dots lc variable",subdirf(fileresilk)); */
6384: /* fprintf(ficgp,"\nreplot \"%s\" u 2:($3 <= 3 ? -$11 : 1/0):3 t \"First 3 individuals\" with line lc variable", subdirf(fileresilk)); */
6385: fprintf(ficgp,"\nset out;unset log\n");
6386: /* fprintf(ficgp,"\nset out \"%s.svg\"; replot; set out; # bug gnuplot",subdirf2(optionfilefiname,"ILK_")); */
1.202 brouard 6387:
1.126 brouard 6388: strcpy(dirfileres,optionfilefiname);
6389: strcpy(optfileres,"vpl");
1.223 brouard 6390: /* 1eme*/
1.238 brouard 6391: for (cpt=1; cpt<= nlstate ; cpt ++){ /* For each live state */
6392: for (k1=1; k1<= m ; k1 ++){ /* For each valid combination of covariate */
1.236 brouard 6393: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
1.238 brouard 6394: /* plot [100000000000000000000:-100000000000000000000] "mysbiaspar/vplrmysbiaspar.txt to check */
6395: if(TKresult[nres]!= k1)
6396: continue;
6397: /* We are interested in selected combination by the resultline */
6398: printf("\n# 1st: Period (stable) prevalence with CI: 'VPL_' files and live state =%d ", cpt);
6399: fprintf(ficgp,"\n# 1st: Period (stable) prevalence with CI: 'VPL_' files and live state =%d ", cpt);
6400: for (k=1; k<=cptcoveff; k++){ /* For each covariate k get corresponding value lv for combination k1 */
6401: lv= decodtabm(k1,k,cptcoveff); /* Should be the value of the covariate corresponding to k1 combination */
6402: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
6403: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
6404: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
6405: vlv= nbcode[Tvaraff[k]][lv]; /* vlv is the value of the covariate lv, 0 or 1 */
6406: /* For each combination of covariate k1 (V1=1, V3=0), we printed the current covariate k and its value vlv */
6407: printf(" V%d=%d ",Tvaraff[k],vlv);
6408: fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv);
6409: }
6410: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
6411: printf(" V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
6412: fprintf(ficgp," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
6413: }
6414: printf("\n#\n");
6415: fprintf(ficgp,"\n#\n");
6416: if(invalidvarcomb[k1]){
6417: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
6418: continue;
6419: }
1.235 brouard 6420:
1.241 brouard 6421: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" \n",subdirf2(optionfilefiname,"V_"),cpt,k1,nres);
6422: fprintf(ficgp,"\n#set out \"V_%s_%d-%d-%d.svg\" \n",optionfilefiname,cpt,k1,nres);
6423: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability\" \nset ter svg size 640, 480\nplot [%.f:%.f] \"%s\" every :::%d::%d u 1:($2==%d ? $3:1/0) \"%%lf %%lf",ageminpar,fage,subdirf2(fileresu,"VPL_"),k1-1,k1-1,nres);
1.235 brouard 6424:
1.238 brouard 6425: for (i=1; i<= nlstate ; i ++) {
6426: if (i==cpt) fprintf(ficgp," %%lf (%%lf)");
6427: else fprintf(ficgp," %%*lf (%%*lf)");
6428: }
1.242 brouard 6429: fprintf(ficgp,"\" t\"Period (stable) prevalence\" w l lt 0,\"%s\" every :::%d::%d u 1:($2==%d ? $3+1.96*$4 : 1/0) \"%%lf %%lf",subdirf2(fileresu,"VPL_"),k1-1,k1-1,nres);
1.238 brouard 6430: for (i=1; i<= nlstate ; i ++) {
6431: if (i==cpt) fprintf(ficgp," %%lf (%%lf)");
6432: else fprintf(ficgp," %%*lf (%%*lf)");
6433: }
1.242 brouard 6434: fprintf(ficgp,"\" t\"95%% CI\" w l lt 1,\"%s\" every :::%d::%d u 1:($2==%d ? $3-1.96*$4 : 1/0) \"%%lf %%lf",subdirf2(fileresu,"VPL_"),k1-1,k1-1,nres);
1.238 brouard 6435: for (i=1; i<= nlstate ; i ++) {
6436: if (i==cpt) fprintf(ficgp," %%lf (%%lf)");
6437: else fprintf(ficgp," %%*lf (%%*lf)");
6438: }
6439: 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));
6440: if(backcast==1){ /* We need to get the corresponding values of the covariates involved in this combination k1 */
6441: /* 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); */
1.242 brouard 6442: fprintf(ficgp,",\"%s\" u 1:((",subdirf2(fileresu,"PLB_")); /* Age is in 1, nres in 2 to be fixed */
1.238 brouard 6443: if(cptcoveff ==0){
6444: fprintf(ficgp,"$%d)) t 'Backward prevalence in state %d' with line ", 2+(cpt-1), cpt );
6445: }else{
6446: kl=0;
6447: for (k=1; k<=cptcoveff; k++){ /* For each combination of covariate */
6448: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate value corresponding to k1 combination and kth covariate */
6449: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
6450: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
6451: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
6452: vlv= nbcode[Tvaraff[k]][lv];
1.223 brouard 6453: kl++;
1.238 brouard 6454: /* 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 *\/ */
6455: /*6+(cpt-1)*(nlstate+1)+1+(i-1)+(nlstate+1)*nlstate; 6+(1-1)*(2+1)+1+(1-1) +(2+1)*2=13 */
6456: /*6+1+(i-1)+(nlstate+1)*nlstate; 6+1+(1-1) +(2+1)*2=13 */
6457: /* '' 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*/
6458: if(k==cptcoveff){
6459: 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], \
1.242 brouard 6460: 2+cptcoveff*2+(cpt-1), cpt ); /* 4 or 6 ?*/
1.238 brouard 6461: }else{
6462: fprintf(ficgp,"$%d==%d && $%d==%d && ",kl+1, Tvaraff[k],kl+1+1,nbcode[Tvaraff[k]][lv]);
6463: kl++;
6464: }
6465: } /* end covariate */
6466: } /* end if no covariate */
6467: } /* end if backcast */
6468: fprintf(ficgp,"\nset out \n");
6469: } /* nres */
1.201 brouard 6470: } /* k1 */
6471: } /* cpt */
1.235 brouard 6472:
6473:
1.126 brouard 6474: /*2 eme*/
1.238 brouard 6475: for (k1=1; k1<= m ; k1 ++){
6476: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
6477: if(TKresult[nres]!= k1)
6478: continue;
6479: fprintf(ficgp,"\n# 2nd: Total life expectancy with CI: 't' files ");
6480: for (k=1; k<=cptcoveff; k++){ /* For each covariate and each value */
1.225 brouard 6481: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate number corresponding to k1 combination */
1.223 brouard 6482: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
6483: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
6484: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
6485: vlv= nbcode[Tvaraff[k]][lv];
6486: fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv);
1.211 brouard 6487: }
1.237 brouard 6488: /* for(k=1; k <= ncovds; k++){ */
1.236 brouard 6489: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
1.238 brouard 6490: printf(" V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
1.236 brouard 6491: fprintf(ficgp," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
1.238 brouard 6492: }
1.211 brouard 6493: fprintf(ficgp,"\n#\n");
1.223 brouard 6494: if(invalidvarcomb[k1]){
6495: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
6496: continue;
6497: }
1.219 brouard 6498:
1.241 brouard 6499: fprintf(ficgp,"\nset out \"%s_%d-%d.svg\" \n",subdirf2(optionfilefiname,"E_"),k1,nres);
1.238 brouard 6500: for(vpopbased=0; vpopbased <= popbased; vpopbased++){ /* Done for vpopbased=0 and vpopbased=1 if popbased==1*/
6501: if(vpopbased==0)
6502: fprintf(ficgp,"set ylabel \"Years\" \nset ter svg size 640, 480\nplot [%.f:%.f] ",ageminpar,fage);
6503: else
6504: fprintf(ficgp,"\nreplot ");
6505: for (i=1; i<= nlstate+1 ; i ++) {
6506: k=2*i;
6507: 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);
6508: for (j=1; j<= nlstate+1 ; j ++) {
6509: if (j==i) fprintf(ficgp," %%lf (%%lf)");
6510: else fprintf(ficgp," %%*lf (%%*lf)");
6511: }
6512: if (i== 1) fprintf(ficgp,"\" t\"TLE\" w l lt %d, \\\n",i);
6513: else fprintf(ficgp,"\" t\"LE in state (%d)\" w l lt %d, \\\n",i-1,i+1);
6514: 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);
6515: for (j=1; j<= nlstate+1 ; j ++) {
6516: if (j==i) fprintf(ficgp," %%lf (%%lf)");
6517: else fprintf(ficgp," %%*lf (%%*lf)");
6518: }
6519: fprintf(ficgp,"\" t\"\" w l lt 0,");
6520: 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);
6521: for (j=1; j<= nlstate+1 ; j ++) {
6522: if (j==i) fprintf(ficgp," %%lf (%%lf)");
6523: else fprintf(ficgp," %%*lf (%%*lf)");
6524: }
6525: if (i== (nlstate+1)) fprintf(ficgp,"\" t\"\" w l lt 0");
6526: else fprintf(ficgp,"\" t\"\" w l lt 0,\\\n");
6527: } /* state */
6528: } /* vpopbased */
1.244 ! brouard 6529: fprintf(ficgp,"\nset out;set out \"%s_%d-%d.svg\"; replot; set out; \n",subdirf2(optionfilefiname,"E_"),k1,nres); /* Buggy gnuplot */
1.238 brouard 6530: } /* end nres */
6531: } /* k1 end 2 eme*/
6532:
6533:
6534: /*3eme*/
6535: for (k1=1; k1<= m ; k1 ++){
6536: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
1.240 brouard 6537: if(TKresult[nres]!= k1)
1.238 brouard 6538: continue;
6539:
6540: for (cpt=1; cpt<= nlstate ; cpt ++) {
6541: fprintf(ficgp,"\n# 3d: Life expectancy with EXP_ files: combination=%d state=%d",k1, cpt);
6542: for (k=1; k<=cptcoveff; k++){ /* For each covariate and each value */
6543: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate number corresponding to k1 combination */
6544: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
6545: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
6546: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
6547: vlv= nbcode[Tvaraff[k]][lv];
6548: fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv);
6549: }
6550: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
6551: fprintf(ficgp," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
6552: }
6553: fprintf(ficgp,"\n#\n");
6554: if(invalidvarcomb[k1]){
6555: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
6556: continue;
6557: }
6558:
6559: /* k=2+nlstate*(2*cpt-2); */
6560: k=2+(nlstate+1)*(cpt-1);
1.241 brouard 6561: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" \n",subdirf2(optionfilefiname,"EXP_"),cpt,k1,nres);
1.238 brouard 6562: fprintf(ficgp,"set ter svg size 640, 480\n\
1.201 brouard 6563: 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 6564: /*fprintf(ficgp,",\"e%s\" every :::%d::%d u 1:($%d-2*$%d) \"\%%lf ",fileres,k1-1,k1-1,k,k+1);
6565: for (i=1; i<= nlstate*2 ; i ++) fprintf(ficgp,"\%%lf (\%%lf) ");
6566: fprintf(ficgp,"\" t \"e%d1\" w l",cpt);
6567: fprintf(ficgp,",\"e%s\" every :::%d::%d u 1:($%d+2*$%d) \"\%%lf ",fileres,k1-1,k1-1,k,k+1);
6568: for (i=1; i<= nlstate*2 ; i ++) fprintf(ficgp,"\%%lf (\%%lf) ");
6569: fprintf(ficgp,"\" t \"e%d1\" w l",cpt);
1.219 brouard 6570:
1.238 brouard 6571: */
6572: for (i=1; i< nlstate ; i ++) {
6573: 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);
6574: /* 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 6575:
1.238 brouard 6576: }
6577: fprintf(ficgp," ,\"%s\" every :::%d::%d u 1:%d t \"e%d.\" w l",subdirf2(fileresu,"E_"),k1-1,k1-1,k+nlstate,cpt);
6578: }
6579: } /* end nres */
6580: } /* end kl 3eme */
1.126 brouard 6581:
1.223 brouard 6582: /* 4eme */
1.201 brouard 6583: /* Survival functions (period) from state i in state j by initial state i */
1.238 brouard 6584: for (k1=1; k1<=m; k1++){ /* For each covariate and each value */
6585: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
6586: if(TKresult[nres]!= k1)
1.223 brouard 6587: continue;
1.238 brouard 6588: for (cpt=1; cpt<=nlstate ; cpt ++) { /* For each life state cpt*/
6589: fprintf(ficgp,"\n#\n#\n# Survival functions in state j : 'LIJ_' files, cov=%d state=%d",k1, cpt);
6590: for (k=1; k<=cptcoveff; k++){ /* For each covariate and each value */
6591: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate number corresponding to k1 combination */
6592: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
6593: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
6594: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
6595: vlv= nbcode[Tvaraff[k]][lv];
6596: fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv);
6597: }
6598: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
6599: fprintf(ficgp," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
6600: }
6601: fprintf(ficgp,"\n#\n");
6602: if(invalidvarcomb[k1]){
6603: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
6604: continue;
1.223 brouard 6605: }
1.238 brouard 6606:
1.241 brouard 6607: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" \n",subdirf2(optionfilefiname,"LIJ_"),cpt,k1,nres);
1.238 brouard 6608: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability to be alive\" \n\
6609: set ter svg size 640, 480\nunset log y\nplot [%.f:%.f] ", ageminpar, agemaxpar);
6610: k=3;
6611: for (i=1; i<= nlstate ; i ++){
6612: if(i==1){
6613: fprintf(ficgp,"\"%s\"",subdirf2(fileresu,"PIJ_"));
6614: }else{
6615: fprintf(ficgp,", '' ");
6616: }
6617: l=(nlstate+ndeath)*(i-1)+1;
6618: fprintf(ficgp," u ($1==%d ? ($3):1/0):($%d/($%d",k1,k+l+(cpt-1),k+l);
6619: for (j=2; j<= nlstate+ndeath ; j ++)
6620: fprintf(ficgp,"+$%d",k+l+j-1);
6621: fprintf(ficgp,")) t \"l(%d,%d)\" w l",i,cpt);
6622: } /* nlstate */
6623: fprintf(ficgp,"\nset out\n");
6624: } /* end cpt state*/
6625: } /* end nres */
6626: } /* end covariate k1 */
6627:
1.220 brouard 6628: /* 5eme */
1.201 brouard 6629: /* Survival functions (period) from state i in state j by final state j */
1.238 brouard 6630: for (k1=1; k1<= m ; k1++){ /* For each covariate combination if any */
6631: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
6632: if(TKresult[nres]!= k1)
1.227 brouard 6633: continue;
1.238 brouard 6634: for (cpt=1; cpt<=nlstate ; cpt ++) { /* For each inital state */
6635: 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);
6636: for (k=1; k<=cptcoveff; k++){ /* For each covariate and each value */
6637: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate number corresponding to k1 combination */
6638: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
6639: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
6640: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
6641: vlv= nbcode[Tvaraff[k]][lv];
6642: fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv);
6643: }
6644: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
6645: fprintf(ficgp," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
6646: }
6647: fprintf(ficgp,"\n#\n");
6648: if(invalidvarcomb[k1]){
6649: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
6650: continue;
6651: }
1.227 brouard 6652:
1.241 brouard 6653: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" \n",subdirf2(optionfilefiname,"LIJT_"),cpt,k1,nres);
1.238 brouard 6654: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability to be alive\" \n\
6655: set ter svg size 640, 480\nunset log y\nplot [%.f:%.f] ", ageminpar, agemaxpar);
6656: k=3;
6657: for (j=1; j<= nlstate ; j ++){ /* Lived in state j */
6658: if(j==1)
6659: fprintf(ficgp,"\"%s\"",subdirf2(fileresu,"PIJ_"));
6660: else
6661: fprintf(ficgp,", '' ");
6662: l=(nlstate+ndeath)*(cpt-1) +j;
6663: fprintf(ficgp," u (($1==%d && (floor($2)%%5 == 0)) ? ($3):1/0):($%d",k1,k+l);
6664: /* for (i=2; i<= nlstate+ndeath ; i ++) */
6665: /* fprintf(ficgp,"+$%d",k+l+i-1); */
6666: fprintf(ficgp,") t \"l(%d,%d)\" w l",cpt,j);
6667: } /* nlstate */
6668: fprintf(ficgp,", '' ");
6669: fprintf(ficgp," u (($1==%d && (floor($2)%%5 == 0)) ? ($3):1/0):(",k1);
6670: for (j=1; j<= nlstate ; j ++){ /* Lived in state j */
6671: l=(nlstate+ndeath)*(cpt-1) +j;
6672: if(j < nlstate)
6673: fprintf(ficgp,"$%d +",k+l);
6674: else
6675: fprintf(ficgp,"$%d) t\"l(%d,.)\" w l",k+l,cpt);
6676: }
6677: fprintf(ficgp,"\nset out\n");
6678: } /* end cpt state*/
6679: } /* end covariate */
6680: } /* end nres */
1.227 brouard 6681:
1.220 brouard 6682: /* 6eme */
1.202 brouard 6683: /* CV preval stable (period) for each covariate */
1.237 brouard 6684: for (k1=1; k1<= m ; k1 ++) /* For each covariate combination if any */
6685: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
6686: if(TKresult[nres]!= k1)
6687: continue;
1.153 brouard 6688: for (cpt=1; cpt<=nlstate ; cpt ++) { /* For each life state */
1.227 brouard 6689:
1.211 brouard 6690: fprintf(ficgp,"\n#\n#\n#CV preval stable (period): 'pij' files, covariatecombination#=%d state=%d",k1, cpt);
1.225 brouard 6691: for (k=1; k<=cptcoveff; k++){ /* For each covariate and each value */
1.227 brouard 6692: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate number corresponding to k1 combination */
6693: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
6694: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
6695: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
6696: vlv= nbcode[Tvaraff[k]][lv];
6697: fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv);
1.211 brouard 6698: }
1.237 brouard 6699: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
6700: fprintf(ficgp," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
6701: }
1.211 brouard 6702: fprintf(ficgp,"\n#\n");
1.223 brouard 6703: if(invalidvarcomb[k1]){
1.227 brouard 6704: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
6705: continue;
1.223 brouard 6706: }
1.227 brouard 6707:
1.241 brouard 6708: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" \n",subdirf2(optionfilefiname,"P_"),cpt,k1,nres);
1.126 brouard 6709: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability\" \n\
1.238 brouard 6710: set ter svg size 640, 480\nunset log y\nplot [%.f:%.f] ", ageminpar, agemaxpar);
1.211 brouard 6711: k=3; /* Offset */
1.153 brouard 6712: for (i=1; i<= nlstate ; i ++){
1.227 brouard 6713: if(i==1)
6714: fprintf(ficgp,"\"%s\"",subdirf2(fileresu,"PIJ_"));
6715: else
6716: fprintf(ficgp,", '' ");
6717: l=(nlstate+ndeath)*(i-1)+1;
6718: fprintf(ficgp," u ($1==%d ? ($3):1/0):($%d/($%d",k1,k+l+(cpt-1),k+l);
6719: for (j=2; j<= nlstate ; j ++)
6720: fprintf(ficgp,"+$%d",k+l+j-1);
6721: fprintf(ficgp,")) t \"prev(%d,%d)\" w l",i,cpt);
1.153 brouard 6722: } /* nlstate */
1.201 brouard 6723: fprintf(ficgp,"\nset out\n");
1.153 brouard 6724: } /* end cpt state*/
6725: } /* end covariate */
1.227 brouard 6726:
6727:
1.220 brouard 6728: /* 7eme */
1.218 brouard 6729: if(backcast == 1){
1.217 brouard 6730: /* CV back preval stable (period) for each covariate */
1.237 brouard 6731: for (k1=1; k1<= m ; k1 ++) /* For each covariate combination if any */
6732: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
6733: if(TKresult[nres]!= k1)
6734: continue;
1.218 brouard 6735: for (cpt=1; cpt<=nlstate ; cpt ++) { /* For each life state */
1.227 brouard 6736: fprintf(ficgp,"\n#\n#\n#CV Back preval stable (period): 'pij' files, covariatecombination#=%d state=%d",k1, cpt);
6737: for (k=1; k<=cptcoveff; k++){ /* For each covariate and each value */
6738: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate number corresponding to k1 combination */
6739: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
6740: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
1.223 brouard 6741: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
1.227 brouard 6742: vlv= nbcode[Tvaraff[k]][lv];
6743: fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv);
6744: }
1.237 brouard 6745: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
6746: fprintf(ficgp," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
6747: }
1.227 brouard 6748: fprintf(ficgp,"\n#\n");
6749: if(invalidvarcomb[k1]){
6750: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
6751: continue;
6752: }
6753:
1.241 brouard 6754: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" \n",subdirf2(optionfilefiname,"PB_"),cpt,k1,nres);
1.227 brouard 6755: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability\" \n\
1.238 brouard 6756: set ter svg size 640, 480\nunset log y\nplot [%.f:%.f] ", ageminpar, agemaxpar);
1.227 brouard 6757: k=3; /* Offset */
6758: for (i=1; i<= nlstate ; i ++){
6759: if(i==1)
6760: fprintf(ficgp,"\"%s\"",subdirf2(fileresu,"PIJB_"));
6761: else
6762: fprintf(ficgp,", '' ");
6763: /* l=(nlstate+ndeath)*(i-1)+1; */
6764: l=(nlstate+ndeath)*(cpt-1)+1;
6765: /* fprintf(ficgp," u ($1==%d ? ($3):1/0):($%d/($%d",k1,k+l+(cpt-1),k+l); /\* a vérifier *\/ */
6766: /* fprintf(ficgp," u ($1==%d ? ($3):1/0):($%d/($%d",k1,k+l+(cpt-1),k+l+(cpt-1)+i-1); /\* a vérifier *\/ */
6767: fprintf(ficgp," u ($1==%d ? ($3):1/0):($%d",k1,k+l+(cpt-1)+i-1); /* a vérifier */
6768: /* for (j=2; j<= nlstate ; j ++) */
6769: /* fprintf(ficgp,"+$%d",k+l+j-1); */
6770: /* /\* fprintf(ficgp,"+$%d",k+l+j-1); *\/ */
6771: fprintf(ficgp,") t \"bprev(%d,%d)\" w l",i,cpt);
6772: } /* nlstate */
6773: fprintf(ficgp,"\nset out\n");
1.218 brouard 6774: } /* end cpt state*/
6775: } /* end covariate */
6776: } /* End if backcast */
6777:
1.223 brouard 6778: /* 8eme */
1.218 brouard 6779: if(prevfcast==1){
6780: /* Projection from cross-sectional to stable (period) for each covariate */
6781:
1.237 brouard 6782: for (k1=1; k1<= m ; k1 ++) /* For each covariate combination if any */
6783: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
6784: if(TKresult[nres]!= k1)
6785: continue;
1.211 brouard 6786: for (cpt=1; cpt<=nlstate ; cpt ++) { /* For each life state */
1.227 brouard 6787: fprintf(ficgp,"\n#\n#\n#Projection of prevalence to stable (period): 'PROJ_' files, covariatecombination#=%d state=%d",k1, cpt);
6788: for (k=1; k<=cptcoveff; k++){ /* For each correspondig covariate value */
6789: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate value corresponding to k1 combination and kth covariate */
6790: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
6791: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
6792: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
6793: vlv= nbcode[Tvaraff[k]][lv];
6794: fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv);
6795: }
1.237 brouard 6796: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
6797: fprintf(ficgp," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
6798: }
1.227 brouard 6799: fprintf(ficgp,"\n#\n");
6800: if(invalidvarcomb[k1]){
6801: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
6802: continue;
6803: }
6804:
6805: fprintf(ficgp,"# hpijx=probability over h years, hp.jx is weighted by observed prev\n ");
1.241 brouard 6806: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" \n",subdirf2(optionfilefiname,"PROJ_"),cpt,k1,nres);
1.227 brouard 6807: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Prevalence\" \n\
1.238 brouard 6808: set ter svg size 640, 480\nunset log y\nplot [%.f:%.f] ", ageminpar, agemaxpar);
1.227 brouard 6809: for (i=1; i<= nlstate+1 ; i ++){ /* nlstate +1 p11 p21 p.1 */
6810: /*# V1 = 1 V2 = 0 yearproj age p11 p21 p.1 p12 p22 p.2 p13 p23 p.3*/
6811: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
6812: /*# yearproj age p11 p21 p.1 p12 p22 p.2 p13 p23 p.3*/
6813: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
6814: if(i==1){
6815: fprintf(ficgp,"\"%s\"",subdirf2(fileresu,"F_"));
6816: }else{
6817: fprintf(ficgp,",\\\n '' ");
6818: }
6819: if(cptcoveff ==0){ /* No covariate */
6820: ioffset=2; /* Age is in 2 */
6821: /*# yearproj age p11 p21 p31 p.1 p12 p22 p32 p.2 p13 p23 p33 p.3 p14 p24 p34 p.4*/
6822: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 */
6823: /*# V1 = 1 yearproj age p11 p21 p31 p.1 p12 p22 p32 p.2 p13 p23 p33 p.3 p14 p24 p34 p.4*/
6824: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 */
6825: fprintf(ficgp," u %d:(", ioffset);
6826: if(i==nlstate+1)
6827: fprintf(ficgp," $%d/(1.-$%d)) t 'pw.%d' with line ", \
6828: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset+1+(i-1)+(nlstate+1)*nlstate,cpt );
6829: else
6830: fprintf(ficgp," $%d/(1.-$%d)) t 'p%d%d' with line ", \
6831: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset+1+(i-1)+(nlstate+1)*nlstate,i,cpt );
6832: }else{ /* more than 2 covariates */
6833: if(cptcoveff ==1){
6834: ioffset=4; /* Age is in 4 */
6835: }else{
6836: ioffset=6; /* Age is in 6 */
6837: /*# V1 = 1 V2 = 0 yearproj age p11 p21 p.1 p12 p22 p.2 p13 p23 p.3*/
6838: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
6839: }
6840: fprintf(ficgp," u %d:(",ioffset);
6841: kl=0;
6842: strcpy(gplotcondition,"(");
6843: for (k=1; k<=cptcoveff; k++){ /* For each covariate writing the chain of conditions */
6844: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate value corresponding to combination k1 and covariate k */
6845: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
6846: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
6847: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
6848: vlv= nbcode[Tvaraff[k]][lv]; /* Value of the modality of Tvaraff[k] */
6849: kl++;
6850: sprintf(gplotcondition+strlen(gplotcondition),"$%d==%d && $%d==%d " ,kl,Tvaraff[k], kl+1, nbcode[Tvaraff[k]][lv]);
6851: kl++;
6852: if(k <cptcoveff && cptcoveff>1)
6853: sprintf(gplotcondition+strlen(gplotcondition)," && ");
6854: }
6855: strcpy(gplotcondition+strlen(gplotcondition),")");
6856: /* 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 *\/ */
6857: /*6+(cpt-1)*(nlstate+1)+1+(i-1)+(nlstate+1)*nlstate; 6+(1-1)*(2+1)+1+(1-1) +(2+1)*2=13 */
6858: /*6+1+(i-1)+(nlstate+1)*nlstate; 6+1+(1-1) +(2+1)*2=13 */
6859: /* '' 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*/
6860: if(i==nlstate+1){
6861: fprintf(ficgp,"%s ? $%d/(1.-$%d) : 1/0) t 'p.%d' with line ", gplotcondition, \
6862: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset+1+(i-1)+(nlstate+1)*nlstate,cpt );
6863: }else{
6864: fprintf(ficgp,"%s ? $%d/(1.-$%d) : 1/0) t 'p%d%d' with line ", gplotcondition, \
6865: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset +1+(i-1)+(nlstate+1)*nlstate,i,cpt );
6866: }
6867: } /* end if covariate */
6868: } /* nlstate */
6869: fprintf(ficgp,"\nset out\n");
1.223 brouard 6870: } /* end cpt state*/
6871: } /* end covariate */
6872: } /* End if prevfcast */
1.227 brouard 6873:
6874:
1.238 brouard 6875: /* 9eme writing MLE parameters */
6876: fprintf(ficgp,"\n##############\n#9eme MLE estimated parameters\n#############\n");
1.126 brouard 6877: for(i=1,jk=1; i <=nlstate; i++){
1.187 brouard 6878: fprintf(ficgp,"# initial state %d\n",i);
1.126 brouard 6879: for(k=1; k <=(nlstate+ndeath); k++){
6880: if (k != i) {
1.227 brouard 6881: fprintf(ficgp,"# current state %d\n",k);
6882: for(j=1; j <=ncovmodel; j++){
6883: fprintf(ficgp,"p%d=%f; ",jk,p[jk]);
6884: jk++;
6885: }
6886: fprintf(ficgp,"\n");
1.126 brouard 6887: }
6888: }
1.223 brouard 6889: }
1.187 brouard 6890: fprintf(ficgp,"##############\n#\n");
1.227 brouard 6891:
1.145 brouard 6892: /*goto avoid;*/
1.238 brouard 6893: /* 10eme Graphics of probabilities or incidences using written MLE parameters */
6894: fprintf(ficgp,"\n##############\n#10eme Graphics of probabilities or incidences\n#############\n");
1.187 brouard 6895: fprintf(ficgp,"# logi(p12/p11)=a12+b12*age+c12age*age+d12*V1+e12*V1*age\n");
6896: fprintf(ficgp,"# logi(p12/p11)=p1 +p2*age +p3*age*age+ p4*V1+ p5*V1*age\n");
6897: fprintf(ficgp,"# logi(p13/p11)=a13+b13*age+c13age*age+d13*V1+e13*V1*age\n");
6898: fprintf(ficgp,"# logi(p13/p11)=p6 +p7*age +p8*age*age+ p9*V1+ p10*V1*age\n");
6899: fprintf(ficgp,"# p12+p13+p14+p11=1=p11(1+exp(a12+b12*age+c12age*age+d12*V1+e12*V1*age)\n");
6900: fprintf(ficgp,"# +exp(a13+b13*age+c13age*age+d13*V1+e13*V1*age)+...)\n");
6901: fprintf(ficgp,"# p11=1/(1+exp(a12+b12*age+c12age*age+d12*V1+e12*V1*age)\n");
6902: fprintf(ficgp,"# +exp(a13+b13*age+c13age*age+d13*V1+e13*V1*age)+...)\n");
6903: fprintf(ficgp,"# p12=exp(a12+b12*age+c12age*age+d12*V1+e12*V1*age)/\n");
6904: fprintf(ficgp,"# (1+exp(a12+b12*age+c12age*age+d12*V1+e12*V1*age)\n");
6905: fprintf(ficgp,"# +exp(a13+b13*age+c13age*age+d13*V1+e13*V1*age))\n");
6906: fprintf(ficgp,"# +exp(a14+b14*age+c14age*age+d14*V1+e14*V1*age)+...)\n");
6907: fprintf(ficgp,"#\n");
1.223 brouard 6908: for(ng=1; ng<=3;ng++){ /* Number of graphics: first is logit, 2nd is probabilities, third is incidences per year*/
1.238 brouard 6909: fprintf(ficgp,"#Number of graphics: first is logit, 2nd is probabilities, third is incidences per year\n");
1.237 brouard 6910: fprintf(ficgp,"#model=%s \n",model);
1.238 brouard 6911: fprintf(ficgp,"# Type of graphic ng=%d\n",ng);
1.237 brouard 6912: fprintf(ficgp,"# jk=1 to 2^%d=%d\n",cptcoveff,m);/* to be checked */
6913: for(jk=1; jk <=m; jk++) /* For each combination of covariate */
6914: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
6915: if(TKresult[nres]!= jk)
6916: continue;
6917: fprintf(ficgp,"# Combination of dummy jk=%d and ",jk);
6918: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
6919: fprintf(ficgp," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
6920: }
6921: fprintf(ficgp,"\n#\n");
1.241 brouard 6922: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" ",subdirf2(optionfilefiname,"PE_"),jk,ng,nres);
1.223 brouard 6923: fprintf(ficgp,"\nset ter svg size 640, 480 ");
6924: if (ng==1){
6925: fprintf(ficgp,"\nset ylabel \"Value of the logit of the model\"\n"); /* exp(a12+b12*x) could be nice */
6926: fprintf(ficgp,"\nunset log y");
6927: }else if (ng==2){
6928: fprintf(ficgp,"\nset ylabel \"Probability\"\n");
6929: fprintf(ficgp,"\nset log y");
6930: }else if (ng==3){
6931: fprintf(ficgp,"\nset ylabel \"Quasi-incidence per year\"\n");
6932: fprintf(ficgp,"\nset log y");
6933: }else
6934: fprintf(ficgp,"\nunset title ");
6935: fprintf(ficgp,"\nplot [%.f:%.f] ",ageminpar,agemaxpar);
6936: i=1;
6937: for(k2=1; k2<=nlstate; k2++) {
6938: k3=i;
6939: for(k=1; k<=(nlstate+ndeath); k++) {
6940: if (k != k2){
6941: switch( ng) {
6942: case 1:
6943: if(nagesqr==0)
6944: fprintf(ficgp," p%d+p%d*x",i,i+1);
6945: else /* nagesqr =1 */
6946: fprintf(ficgp," p%d+p%d*x+p%d*x*x",i,i+1,i+1+nagesqr);
6947: break;
6948: case 2: /* ng=2 */
6949: if(nagesqr==0)
6950: fprintf(ficgp," exp(p%d+p%d*x",i,i+1);
6951: else /* nagesqr =1 */
6952: fprintf(ficgp," exp(p%d+p%d*x+p%d*x*x",i,i+1,i+1+nagesqr);
6953: break;
6954: case 3:
6955: if(nagesqr==0)
6956: fprintf(ficgp," %f*exp(p%d+p%d*x",YEARM/stepm,i,i+1);
6957: else /* nagesqr =1 */
6958: fprintf(ficgp," %f*exp(p%d+p%d*x+p%d*x*x",YEARM/stepm,i,i+1,i+1+nagesqr);
6959: break;
6960: }
6961: ij=1;/* To be checked else nbcode[0][0] wrong */
1.237 brouard 6962: ijp=1; /* product no age */
6963: /* for(j=3; j <=ncovmodel-nagesqr; j++) { */
6964: for(j=1; j <=cptcovt; j++) { /* For each covariate of the simplified model */
1.223 brouard 6965: /* printf("Tage[%d]=%d, j=%d\n", ij, Tage[ij], j); */
1.237 brouard 6966: if(j==Tage[ij]) { /* Product by age */
6967: if(ij <=cptcovage) { /* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1, 2 V5 and V1 */
1.238 brouard 6968: if(DummyV[j]==0){
1.237 brouard 6969: fprintf(ficgp,"+p%d*%d*x",i+j+2+nagesqr-1,Tinvresult[nres][Tvar[j]]);;
6970: }else{ /* quantitative */
6971: fprintf(ficgp,"+p%d*%f*x",i+j+2+nagesqr-1,Tqinvresult[nres][Tvar[j]]); /* Tqinvresult in decoderesult */
6972: /* fprintf(ficgp,"+p%d*%d*x",i+j+nagesqr-1,nbcode[Tvar[j-2]][codtabm(jk,Tvar[j-2])]); */
6973: }
6974: ij++;
6975: }
6976: }else if(j==Tprod[ijp]) { /* */
6977: /* printf("Tprod[%d]=%d, j=%d\n", ij, Tprod[ijp], j); */
6978: if(ijp <=cptcovprod) { /* Product */
1.238 brouard 6979: if(DummyV[Tvard[ijp][1]]==0){/* Vn is dummy */
6980: if(DummyV[Tvard[ijp][2]]==0){/* Vn and Vm are dummy */
1.237 brouard 6981: /* 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)]); */
6982: fprintf(ficgp,"+p%d*%d*%d",i+j+2+nagesqr-1,Tinvresult[nres][Tvard[ijp][1]],Tinvresult[nres][Tvard[ijp][2]]);
6983: }else{ /* Vn is dummy and Vm is quanti */
6984: /* fprintf(ficgp,"+p%d*%d*%f",i+j+2+nagesqr-1,nbcode[Tvard[ijp][1]][codtabm(jk,j)],Tqinvresult[nres][Tvard[ijp][2]]); */
6985: fprintf(ficgp,"+p%d*%d*%f",i+j+2+nagesqr-1,Tinvresult[nres][Tvard[ijp][1]],Tqinvresult[nres][Tvard[ijp][2]]);
6986: }
6987: }else{ /* Vn*Vm Vn is quanti */
1.238 brouard 6988: if(DummyV[Tvard[ijp][2]]==0){
1.237 brouard 6989: fprintf(ficgp,"+p%d*%d*%f",i+j+2+nagesqr-1,Tinvresult[nres][Tvard[ijp][2]],Tqinvresult[nres][Tvard[ijp][1]]);
6990: }else{ /* Both quanti */
6991: fprintf(ficgp,"+p%d*%f*%f",i+j+2+nagesqr-1,Tqinvresult[nres][Tvard[ijp][1]],Tqinvresult[nres][Tvard[ijp][2]]);
6992: }
6993: }
1.238 brouard 6994: ijp++;
1.237 brouard 6995: }
6996: } else{ /* simple covariate */
6997: /* fprintf(ficgp,"+p%d*%d",i+j+2+nagesqr-1,nbcode[Tvar[j]][codtabm(jk,j)]); /\* Valgrind bug nbcode *\/ */
6998: if(Dummy[j]==0){
6999: fprintf(ficgp,"+p%d*%d",i+j+2+nagesqr-1,Tinvresult[nres][Tvar[j]]); /* */
7000: }else{ /* quantitative */
7001: fprintf(ficgp,"+p%d*%f",i+j+2+nagesqr-1,Tqinvresult[nres][Tvar[j]]); /* */
1.223 brouard 7002: /* fprintf(ficgp,"+p%d*%d*x",i+j+nagesqr-1,nbcode[Tvar[j-2]][codtabm(jk,Tvar[j-2])]); */
7003: }
1.237 brouard 7004: } /* end simple */
7005: } /* end j */
1.223 brouard 7006: }else{
7007: i=i-ncovmodel;
7008: if(ng !=1 ) /* For logit formula of log p11 is more difficult to get */
7009: fprintf(ficgp," (1.");
7010: }
1.227 brouard 7011:
1.223 brouard 7012: if(ng != 1){
7013: fprintf(ficgp,")/(1");
1.227 brouard 7014:
1.223 brouard 7015: for(k1=1; k1 <=nlstate; k1++){
7016: if(nagesqr==0)
7017: fprintf(ficgp,"+exp(p%d+p%d*x",k3+(k1-1)*ncovmodel,k3+(k1-1)*ncovmodel+1);
7018: else /* nagesqr =1 */
7019: 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 7020:
1.223 brouard 7021: ij=1;
7022: for(j=3; j <=ncovmodel-nagesqr; j++){
1.237 brouard 7023: if((j-2)==Tage[ij]) { /* Bug valgrind */
7024: if(ij <=cptcovage) { /* Bug valgrind */
1.223 brouard 7025: fprintf(ficgp,"+p%d*%d*x",k3+(k1-1)*ncovmodel+1+j-2+nagesqr,nbcode[Tvar[j-2]][codtabm(jk,j-2)]);
7026: /* fprintf(ficgp,"+p%d*%d*x",k3+(k1-1)*ncovmodel+1+j-2+nagesqr,nbcode[Tvar[j-2]][codtabm(jk,Tvar[j-2])]); */
7027: ij++;
7028: }
7029: }
7030: else
1.225 brouard 7031: 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 7032: }
7033: fprintf(ficgp,")");
7034: }
7035: fprintf(ficgp,")");
7036: if(ng ==2)
7037: fprintf(ficgp," t \"p%d%d\" ", k2,k);
7038: else /* ng= 3 */
7039: fprintf(ficgp," t \"i%d%d\" ", k2,k);
7040: }else{ /* end ng <> 1 */
7041: if( k !=k2) /* logit p11 is hard to draw */
7042: fprintf(ficgp," t \"logit(p%d%d)\" ", k2,k);
7043: }
7044: if ((k+k2)!= (nlstate*2+ndeath) && ng != 1)
7045: fprintf(ficgp,",");
7046: if (ng == 1 && k!=k2 && (k+k2)!= (nlstate*2+ndeath))
7047: fprintf(ficgp,",");
7048: i=i+ncovmodel;
7049: } /* end k */
7050: } /* end k2 */
7051: fprintf(ficgp,"\n set out\n");
7052: } /* end jk */
7053: } /* end ng */
7054: /* avoid: */
7055: fflush(ficgp);
1.126 brouard 7056: } /* end gnuplot */
7057:
7058:
7059: /*************** Moving average **************/
1.219 brouard 7060: /* int movingaverage(double ***probs, double bage, double fage, double ***mobaverage, int mobilav, double bageout, double fageout){ */
1.222 brouard 7061: int movingaverage(double ***probs, double bage, double fage, double ***mobaverage, int mobilav){
1.218 brouard 7062:
1.222 brouard 7063: int i, cpt, cptcod;
7064: int modcovmax =1;
7065: int mobilavrange, mob;
7066: int iage=0;
7067:
7068: double sum=0.;
7069: double age;
7070: double *sumnewp, *sumnewm;
7071: double *agemingood, *agemaxgood; /* Currently identical for all covariates */
7072:
7073:
1.225 brouard 7074: /* modcovmax=2*cptcoveff;/\* Max number of modalities. We suppose */
1.222 brouard 7075: /* a covariate has 2 modalities, should be equal to ncovcombmax *\/ */
7076:
7077: sumnewp = vector(1,ncovcombmax);
7078: sumnewm = vector(1,ncovcombmax);
7079: agemingood = vector(1,ncovcombmax);
7080: agemaxgood = vector(1,ncovcombmax);
7081:
7082: for (cptcod=1;cptcod<=ncovcombmax;cptcod++){
7083: sumnewm[cptcod]=0.;
7084: sumnewp[cptcod]=0.;
7085: agemingood[cptcod]=0;
7086: agemaxgood[cptcod]=0;
7087: }
7088: if (cptcovn<1) ncovcombmax=1; /* At least 1 pass */
7089:
7090: if(mobilav==1||mobilav ==3 ||mobilav==5 ||mobilav== 7){
7091: if(mobilav==1) mobilavrange=5; /* default */
7092: else mobilavrange=mobilav;
7093: for (age=bage; age<=fage; age++)
7094: for (i=1; i<=nlstate;i++)
7095: for (cptcod=1;cptcod<=ncovcombmax;cptcod++)
7096: mobaverage[(int)age][i][cptcod]=probs[(int)age][i][cptcod];
7097: /* We keep the original values on the extreme ages bage, fage and for
7098: fage+1 and bage-1 we use a 3 terms moving average; for fage+2 bage+2
7099: we use a 5 terms etc. until the borders are no more concerned.
7100: */
7101: for (mob=3;mob <=mobilavrange;mob=mob+2){
7102: for (age=bage+(mob-1)/2; age<=fage-(mob-1)/2; age++){
7103: for (i=1; i<=nlstate;i++){
7104: for (cptcod=1;cptcod<=ncovcombmax;cptcod++){
7105: mobaverage[(int)age][i][cptcod] =probs[(int)age][i][cptcod];
7106: for (cpt=1;cpt<=(mob-1)/2;cpt++){
7107: mobaverage[(int)age][i][cptcod] +=probs[(int)age-cpt][i][cptcod];
7108: mobaverage[(int)age][i][cptcod] +=probs[(int)age+cpt][i][cptcod];
7109: }
7110: mobaverage[(int)age][i][cptcod]=mobaverage[(int)age][i][cptcod]/mob;
7111: }
7112: }
7113: }/* end age */
7114: }/* end mob */
7115: }else
7116: return -1;
7117: for (cptcod=1;cptcod<=ncovcombmax;cptcod++){
7118: /* for (age=bage+(mob-1)/2; age<=fage-(mob-1)/2; age++){ */
7119: if(invalidvarcomb[cptcod]){
7120: printf("\nCombination (%d) ignored because no cases \n",cptcod);
7121: continue;
7122: }
1.219 brouard 7123:
1.222 brouard 7124: agemingood[cptcod]=fage-(mob-1)/2;
7125: for (age=fage-(mob-1)/2; age>=bage; age--){/* From oldest to youngest, finding the youngest wrong */
7126: sumnewm[cptcod]=0.;
7127: for (i=1; i<=nlstate;i++){
7128: sumnewm[cptcod]+=mobaverage[(int)age][i][cptcod];
7129: }
7130: if(fabs(sumnewm[cptcod] - 1.) <= 1.e-3) { /* good */
7131: agemingood[cptcod]=age;
7132: }else{ /* bad */
7133: for (i=1; i<=nlstate;i++){
7134: mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemingood[cptcod]][i][cptcod];
7135: } /* i */
7136: } /* end bad */
7137: }/* age */
7138: sum=0.;
7139: for (i=1; i<=nlstate;i++){
7140: sum+=mobaverage[(int)agemingood[cptcod]][i][cptcod];
7141: }
7142: if(fabs(sum - 1.) > 1.e-3) { /* bad */
7143: 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);
7144: /* for (i=1; i<=nlstate;i++){ */
7145: /* mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemingood[cptcod]][i][cptcod]; */
7146: /* } /\* i *\/ */
7147: } /* end bad */
7148: /* else{ /\* We found some ages summing to one, we will smooth the oldest *\/ */
7149: /* From youngest, finding the oldest wrong */
7150: agemaxgood[cptcod]=bage+(mob-1)/2;
7151: for (age=bage+(mob-1)/2; age<=fage; age++){
7152: sumnewm[cptcod]=0.;
7153: for (i=1; i<=nlstate;i++){
7154: sumnewm[cptcod]+=mobaverage[(int)age][i][cptcod];
7155: }
7156: if(fabs(sumnewm[cptcod] - 1.) <= 1.e-3) { /* good */
7157: agemaxgood[cptcod]=age;
7158: }else{ /* bad */
7159: for (i=1; i<=nlstate;i++){
7160: mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemaxgood[cptcod]][i][cptcod];
7161: } /* i */
7162: } /* end bad */
7163: }/* age */
7164: sum=0.;
7165: for (i=1; i<=nlstate;i++){
7166: sum+=mobaverage[(int)agemaxgood[cptcod]][i][cptcod];
7167: }
7168: if(fabs(sum - 1.) > 1.e-3) { /* bad */
7169: 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);
7170: /* for (i=1; i<=nlstate;i++){ */
7171: /* mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemingood[cptcod]][i][cptcod]; */
7172: /* } /\* i *\/ */
7173: } /* end bad */
7174:
7175: for (age=bage; age<=fage; age++){
1.235 brouard 7176: /* printf("%d %d ", cptcod, (int)age); */
1.222 brouard 7177: sumnewp[cptcod]=0.;
7178: sumnewm[cptcod]=0.;
7179: for (i=1; i<=nlstate;i++){
7180: sumnewp[cptcod]+=probs[(int)age][i][cptcod];
7181: sumnewm[cptcod]+=mobaverage[(int)age][i][cptcod];
7182: /* printf("%.4f %.4f ",probs[(int)age][i][cptcod], mobaverage[(int)age][i][cptcod]); */
7183: }
7184: /* printf("%.4f %.4f \n",sumnewp[cptcod], sumnewm[cptcod]); */
7185: }
7186: /* printf("\n"); */
7187: /* } */
7188: /* brutal averaging */
7189: for (i=1; i<=nlstate;i++){
7190: for (age=1; age<=bage; age++){
7191: mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemingood[cptcod]][i][cptcod];
7192: /* printf("age=%d i=%d cptcod=%d mobaverage=%.4f \n",(int)age,i, cptcod, mobaverage[(int)age][i][cptcod]); */
7193: }
7194: for (age=fage; age<=AGESUP; age++){
7195: mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemaxgood[cptcod]][i][cptcod];
7196: /* printf("age=%d i=%d cptcod=%d mobaverage=%.4f \n",(int)age,i, cptcod, mobaverage[(int)age][i][cptcod]); */
7197: }
7198: } /* end i status */
7199: for (i=nlstate+1; i<=nlstate+ndeath;i++){
7200: for (age=1; age<=AGESUP; age++){
7201: /*printf("i=%d, age=%d, cptcod=%d\n",i, (int)age, cptcod);*/
7202: mobaverage[(int)age][i][cptcod]=0.;
7203: }
7204: }
7205: }/* end cptcod */
7206: free_vector(sumnewm,1, ncovcombmax);
7207: free_vector(sumnewp,1, ncovcombmax);
7208: free_vector(agemaxgood,1, ncovcombmax);
7209: free_vector(agemingood,1, ncovcombmax);
7210: return 0;
7211: }/* End movingaverage */
1.218 brouard 7212:
1.126 brouard 7213:
7214: /************** Forecasting ******************/
1.235 brouard 7215: 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 7216: /* proj1, year, month, day of starting projection
7217: agemin, agemax range of age
7218: dateprev1 dateprev2 range of dates during which prevalence is computed
7219: anproj2 year of en of projection (same day and month as proj1).
7220: */
1.235 brouard 7221: int yearp, stepsize, hstepm, nhstepm, j, k, cptcod, i, h, i1, k4, nres=0;
1.126 brouard 7222: double agec; /* generic age */
7223: double agelim, ppij, yp,yp1,yp2,jprojmean,mprojmean,anprojmean;
7224: double *popeffectif,*popcount;
7225: double ***p3mat;
1.218 brouard 7226: /* double ***mobaverage; */
1.126 brouard 7227: char fileresf[FILENAMELENGTH];
7228:
7229: agelim=AGESUP;
1.211 brouard 7230: /* Compute observed prevalence between dateprev1 and dateprev2 by counting the number of people
7231: in each health status at the date of interview (if between dateprev1 and dateprev2).
7232: We still use firstpass and lastpass as another selection.
7233: */
1.214 brouard 7234: /* freqsummary(fileres, agemin, agemax, s, agev, nlstate, imx,Tvaraff,nbcode, ncodemax,mint,anint,strstart,\ */
7235: /* firstpass, lastpass, stepm, weightopt, model); */
1.126 brouard 7236:
1.201 brouard 7237: strcpy(fileresf,"F_");
7238: strcat(fileresf,fileresu);
1.126 brouard 7239: if((ficresf=fopen(fileresf,"w"))==NULL) {
7240: printf("Problem with forecast resultfile: %s\n", fileresf);
7241: fprintf(ficlog,"Problem with forecast resultfile: %s\n", fileresf);
7242: }
1.235 brouard 7243: printf("\nComputing forecasting: result on file '%s', please wait... \n", fileresf);
7244: fprintf(ficlog,"\nComputing forecasting: result on file '%s', please wait... \n", fileresf);
1.126 brouard 7245:
1.225 brouard 7246: if (cptcoveff==0) ncodemax[cptcoveff]=1;
1.126 brouard 7247:
7248:
7249: stepsize=(int) (stepm+YEARM-1)/YEARM;
7250: if (stepm<=12) stepsize=1;
7251: if(estepm < stepm){
7252: printf ("Problem %d lower than %d\n",estepm, stepm);
7253: }
7254: else hstepm=estepm;
7255:
7256: hstepm=hstepm/stepm;
7257: yp1=modf(dateintmean,&yp);/* extracts integral of datemean in yp and
7258: fractional in yp1 */
7259: anprojmean=yp;
7260: yp2=modf((yp1*12),&yp);
7261: mprojmean=yp;
7262: yp1=modf((yp2*30.5),&yp);
7263: jprojmean=yp;
7264: if(jprojmean==0) jprojmean=1;
7265: if(mprojmean==0) jprojmean=1;
7266:
1.227 brouard 7267: i1=pow(2,cptcoveff);
1.126 brouard 7268: if (cptcovn < 1){i1=1;}
7269:
7270: fprintf(ficresf,"# Mean day of interviews %.lf/%.lf/%.lf (%.2f) between %.2f and %.2f \n",jprojmean,mprojmean,anprojmean,dateintmean,dateprev1,dateprev2);
7271:
7272: fprintf(ficresf,"#****** Routine prevforecast **\n");
1.227 brouard 7273:
1.126 brouard 7274: /* if (h==(int)(YEARM*yearp)){ */
1.235 brouard 7275: for(nres=1; nres <= nresult; nres++) /* For each resultline */
7276: for(k=1; k<=i1;k++){
7277: if(TKresult[nres]!= k)
7278: continue;
1.227 brouard 7279: if(invalidvarcomb[k]){
7280: printf("\nCombination (%d) projection ignored because no cases \n",k);
7281: continue;
7282: }
7283: fprintf(ficresf,"\n#****** hpijx=probability over h years, hp.jx is weighted by observed prev \n#");
7284: for(j=1;j<=cptcoveff;j++) {
7285: fprintf(ficresf," V%d (=) %d",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
7286: }
1.235 brouard 7287: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
1.238 brouard 7288: fprintf(ficresf," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
1.235 brouard 7289: }
1.227 brouard 7290: fprintf(ficresf," yearproj age");
7291: for(j=1; j<=nlstate+ndeath;j++){
7292: for(i=1; i<=nlstate;i++)
7293: fprintf(ficresf," p%d%d",i,j);
7294: fprintf(ficresf," wp.%d",j);
7295: }
7296: for (yearp=0; yearp<=(anproj2-anproj1);yearp +=stepsize) {
7297: fprintf(ficresf,"\n");
7298: fprintf(ficresf,"\n# Forecasting at date %.lf/%.lf/%.lf ",jproj1,mproj1,anproj1+yearp);
7299: for (agec=fage; agec>=(ageminpar-1); agec--){
7300: nhstepm=(int) rint((agelim-agec)*YEARM/stepm);
7301: nhstepm = nhstepm/hstepm;
7302: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
7303: oldm=oldms;savm=savms;
1.235 brouard 7304: hpxij(p3mat,nhstepm,agec,hstepm,p,nlstate,stepm,oldm,savm, k,nres);
1.227 brouard 7305:
7306: for (h=0; h<=nhstepm; h++){
7307: if (h*hstepm/YEARM*stepm ==yearp) {
7308: fprintf(ficresf,"\n");
7309: for(j=1;j<=cptcoveff;j++)
7310: fprintf(ficresf,"%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
7311: fprintf(ficresf,"%.f %.f ",anproj1+yearp,agec+h*hstepm/YEARM*stepm);
7312: }
7313: for(j=1; j<=nlstate+ndeath;j++) {
7314: ppij=0.;
7315: for(i=1; i<=nlstate;i++) {
7316: if (mobilav==1)
7317: ppij=ppij+p3mat[i][j][h]*mobaverage[(int)agec][i][k];
7318: else {
7319: ppij=ppij+p3mat[i][j][h]*probs[(int)(agec)][i][k];
7320: }
7321: if (h*hstepm/YEARM*stepm== yearp) {
7322: fprintf(ficresf," %.3f", p3mat[i][j][h]);
7323: }
7324: } /* end i */
7325: if (h*hstepm/YEARM*stepm==yearp) {
7326: fprintf(ficresf," %.3f", ppij);
7327: }
7328: }/* end j */
7329: } /* end h */
7330: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
7331: } /* end agec */
7332: } /* end yearp */
7333: } /* end k */
1.219 brouard 7334:
1.126 brouard 7335: fclose(ficresf);
1.215 brouard 7336: printf("End of Computing forecasting \n");
7337: fprintf(ficlog,"End of Computing forecasting\n");
7338:
1.126 brouard 7339: }
7340:
1.218 brouard 7341: /* /\************** Back Forecasting ******************\/ */
1.225 brouard 7342: /* 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 7343: /* /\* back1, year, month, day of starting backection */
7344: /* agemin, agemax range of age */
7345: /* dateprev1 dateprev2 range of dates during which prevalence is computed */
7346: /* anback2 year of en of backection (same day and month as back1). */
7347: /* *\/ */
7348: /* int yearp, stepsize, hstepm, nhstepm, j, k, cptcod, i, h, i1; */
7349: /* double agec; /\* generic age *\/ */
7350: /* double agelim, ppij, yp,yp1,yp2,jprojmean,mprojmean,anprojmean; */
7351: /* double *popeffectif,*popcount; */
7352: /* double ***p3mat; */
7353: /* /\* double ***mobaverage; *\/ */
7354: /* char fileresfb[FILENAMELENGTH]; */
7355:
7356: /* agelim=AGESUP; */
7357: /* /\* Compute observed prevalence between dateprev1 and dateprev2 by counting the number of people */
7358: /* in each health status at the date of interview (if between dateprev1 and dateprev2). */
7359: /* We still use firstpass and lastpass as another selection. */
7360: /* *\/ */
7361: /* /\* freqsummary(fileres, agemin, agemax, s, agev, nlstate, imx,Tvaraff,nbcode, ncodemax,mint,anint,strstart,\ *\/ */
7362: /* /\* firstpass, lastpass, stepm, weightopt, model); *\/ */
7363: /* prevalence(probs, ageminpar, agemax, s, agev, nlstate, imx, Tvar, nbcode, ncodemax, mint, anint, dateprev1, dateprev2, firstpass, lastpass); */
7364:
7365: /* strcpy(fileresfb,"FB_"); */
7366: /* strcat(fileresfb,fileresu); */
7367: /* if((ficresfb=fopen(fileresfb,"w"))==NULL) { */
7368: /* printf("Problem with back forecast resultfile: %s\n", fileresfb); */
7369: /* fprintf(ficlog,"Problem with back forecast resultfile: %s\n", fileresfb); */
7370: /* } */
7371: /* printf("Computing back forecasting: result on file '%s', please wait... \n", fileresfb); */
7372: /* fprintf(ficlog,"Computing back forecasting: result on file '%s', please wait... \n", fileresfb); */
7373:
1.225 brouard 7374: /* if (cptcoveff==0) ncodemax[cptcoveff]=1; */
1.218 brouard 7375:
7376: /* /\* if (mobilav!=0) { *\/ */
7377: /* /\* mobaverage= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX); *\/ */
7378: /* /\* if (movingaverage(probs, ageminpar, fage, mobaverage,mobilav)!=0){ *\/ */
7379: /* /\* fprintf(ficlog," Error in movingaverage mobilav=%d\n",mobilav); *\/ */
7380: /* /\* printf(" Error in movingaverage mobilav=%d\n",mobilav); *\/ */
7381: /* /\* } *\/ */
7382: /* /\* } *\/ */
7383:
7384: /* stepsize=(int) (stepm+YEARM-1)/YEARM; */
7385: /* if (stepm<=12) stepsize=1; */
7386: /* if(estepm < stepm){ */
7387: /* printf ("Problem %d lower than %d\n",estepm, stepm); */
7388: /* } */
7389: /* else hstepm=estepm; */
7390:
7391: /* hstepm=hstepm/stepm; */
7392: /* yp1=modf(dateintmean,&yp);/\* extracts integral of datemean in yp and */
7393: /* fractional in yp1 *\/ */
7394: /* anprojmean=yp; */
7395: /* yp2=modf((yp1*12),&yp); */
7396: /* mprojmean=yp; */
7397: /* yp1=modf((yp2*30.5),&yp); */
7398: /* jprojmean=yp; */
7399: /* if(jprojmean==0) jprojmean=1; */
7400: /* if(mprojmean==0) jprojmean=1; */
7401:
1.225 brouard 7402: /* i1=cptcoveff; */
1.218 brouard 7403: /* if (cptcovn < 1){i1=1;} */
1.217 brouard 7404:
1.218 brouard 7405: /* fprintf(ficresfb,"# Mean day of interviews %.lf/%.lf/%.lf (%.2f) between %.2f and %.2f \n",jprojmean,mprojmean,anprojmean,dateintmean,dateprev1,dateprev2); */
1.217 brouard 7406:
1.218 brouard 7407: /* fprintf(ficresfb,"#****** Routine prevbackforecast **\n"); */
7408:
7409: /* /\* if (h==(int)(YEARM*yearp)){ *\/ */
7410: /* for(cptcov=1, k=0;cptcov<=i1;cptcov++){ */
1.225 brouard 7411: /* for(cptcod=1;cptcod<=ncodemax[cptcoveff];cptcod++){ */
1.218 brouard 7412: /* k=k+1; */
7413: /* fprintf(ficresfb,"\n#****** hbijx=probability over h years, hp.jx is weighted by observed prev \n#"); */
1.225 brouard 7414: /* for(j=1;j<=cptcoveff;j++) { */
1.218 brouard 7415: /* fprintf(ficresfb," V%d (=) %d",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]); */
7416: /* } */
7417: /* fprintf(ficresfb," yearbproj age"); */
7418: /* for(j=1; j<=nlstate+ndeath;j++){ */
7419: /* for(i=1; i<=nlstate;i++) */
7420: /* fprintf(ficresfb," p%d%d",i,j); */
7421: /* fprintf(ficresfb," p.%d",j); */
7422: /* } */
7423: /* for (yearp=0; yearp>=(anback2-anback1);yearp -=stepsize) { */
7424: /* /\* for (yearp=0; yearp<=(anproj2-anproj1);yearp +=stepsize) { *\/ */
7425: /* fprintf(ficresfb,"\n"); */
7426: /* fprintf(ficresfb,"\n# Back Forecasting at date %.lf/%.lf/%.lf ",jback1,mback1,anback1+yearp); */
7427: /* for (agec=fage; agec>=(ageminpar-1); agec--){ */
7428: /* nhstepm=(int) rint((agelim-agec)*YEARM/stepm); */
7429: /* nhstepm = nhstepm/hstepm; */
7430: /* p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); */
7431: /* oldm=oldms;savm=savms; */
7432: /* hbxij(p3mat,nhstepm,agec,hstepm,p,prevacurrent,nlstate,stepm,oldm,savm,oldm,savm, dnewm, doldm, dsavm, k); */
7433: /* for (h=0; h<=nhstepm; h++){ */
7434: /* if (h*hstepm/YEARM*stepm ==yearp) { */
7435: /* fprintf(ficresfb,"\n"); */
1.225 brouard 7436: /* for(j=1;j<=cptcoveff;j++) */
1.218 brouard 7437: /* fprintf(ficresfb,"%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]); */
7438: /* fprintf(ficresfb,"%.f %.f ",anback1+yearp,agec+h*hstepm/YEARM*stepm); */
7439: /* } */
7440: /* for(j=1; j<=nlstate+ndeath;j++) { */
7441: /* ppij=0.; */
7442: /* for(i=1; i<=nlstate;i++) { */
7443: /* if (mobilav==1) */
7444: /* ppij=ppij+p3mat[i][j][h]*mobaverage[(int)agec][i][cptcod]; */
7445: /* else { */
7446: /* ppij=ppij+p3mat[i][j][h]*probs[(int)(agec)][i][cptcod]; */
7447: /* } */
7448: /* if (h*hstepm/YEARM*stepm== yearp) { */
7449: /* fprintf(ficresfb," %.3f", p3mat[i][j][h]); */
7450: /* } */
7451: /* } /\* end i *\/ */
7452: /* if (h*hstepm/YEARM*stepm==yearp) { */
7453: /* fprintf(ficresfb," %.3f", ppij); */
7454: /* } */
7455: /* }/\* end j *\/ */
7456: /* } /\* end h *\/ */
7457: /* free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); */
7458: /* } /\* end agec *\/ */
7459: /* } /\* end yearp *\/ */
7460: /* } /\* end cptcod *\/ */
7461: /* } /\* end cptcov *\/ */
7462:
7463: /* /\* if (mobilav!=0) free_ma3x(mobaverage,1, AGESUP,1,NCOVMAX, 1,NCOVMAX); *\/ */
7464:
7465: /* fclose(ficresfb); */
7466: /* printf("End of Computing Back forecasting \n"); */
7467: /* fprintf(ficlog,"End of Computing Back forecasting\n"); */
1.217 brouard 7468:
1.218 brouard 7469: /* } */
1.217 brouard 7470:
1.126 brouard 7471: /************** Forecasting *****not tested NB*************/
1.227 brouard 7472: /* 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 7473:
1.227 brouard 7474: /* int cpt, stepsize, hstepm, nhstepm, j,k,c, cptcod, i,h; */
7475: /* int *popage; */
7476: /* double calagedatem, agelim, kk1, kk2; */
7477: /* double *popeffectif,*popcount; */
7478: /* double ***p3mat,***tabpop,***tabpopprev; */
7479: /* /\* double ***mobaverage; *\/ */
7480: /* char filerespop[FILENAMELENGTH]; */
1.126 brouard 7481:
1.227 brouard 7482: /* tabpop= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
7483: /* tabpopprev= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
7484: /* agelim=AGESUP; */
7485: /* calagedatem=(anpyram+mpyram/12.+jpyram/365.-dateintmean)*YEARM; */
1.126 brouard 7486:
1.227 brouard 7487: /* prevalence(probs, ageminpar, agemax, s, agev, nlstate, imx, Tvar, nbcode, ncodemax, mint, anint, dateprev1, dateprev2, firstpass, lastpass); */
1.126 brouard 7488:
7489:
1.227 brouard 7490: /* strcpy(filerespop,"POP_"); */
7491: /* strcat(filerespop,fileresu); */
7492: /* if((ficrespop=fopen(filerespop,"w"))==NULL) { */
7493: /* printf("Problem with forecast resultfile: %s\n", filerespop); */
7494: /* fprintf(ficlog,"Problem with forecast resultfile: %s\n", filerespop); */
7495: /* } */
7496: /* printf("Computing forecasting: result on file '%s' \n", filerespop); */
7497: /* fprintf(ficlog,"Computing forecasting: result on file '%s' \n", filerespop); */
1.126 brouard 7498:
1.227 brouard 7499: /* if (cptcoveff==0) ncodemax[cptcoveff]=1; */
1.126 brouard 7500:
1.227 brouard 7501: /* /\* if (mobilav!=0) { *\/ */
7502: /* /\* mobaverage= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX); *\/ */
7503: /* /\* if (movingaverage(probs, ageminpar, fage, mobaverage,mobilav)!=0){ *\/ */
7504: /* /\* fprintf(ficlog," Error in movingaverage mobilav=%d\n",mobilav); *\/ */
7505: /* /\* printf(" Error in movingaverage mobilav=%d\n",mobilav); *\/ */
7506: /* /\* } *\/ */
7507: /* /\* } *\/ */
1.126 brouard 7508:
1.227 brouard 7509: /* stepsize=(int) (stepm+YEARM-1)/YEARM; */
7510: /* if (stepm<=12) stepsize=1; */
1.126 brouard 7511:
1.227 brouard 7512: /* agelim=AGESUP; */
1.126 brouard 7513:
1.227 brouard 7514: /* hstepm=1; */
7515: /* hstepm=hstepm/stepm; */
1.218 brouard 7516:
1.227 brouard 7517: /* if (popforecast==1) { */
7518: /* if((ficpop=fopen(popfile,"r"))==NULL) { */
7519: /* printf("Problem with population file : %s\n",popfile);exit(0); */
7520: /* fprintf(ficlog,"Problem with population file : %s\n",popfile);exit(0); */
7521: /* } */
7522: /* popage=ivector(0,AGESUP); */
7523: /* popeffectif=vector(0,AGESUP); */
7524: /* popcount=vector(0,AGESUP); */
1.126 brouard 7525:
1.227 brouard 7526: /* i=1; */
7527: /* while ((c=fscanf(ficpop,"%d %lf\n",&popage[i],&popcount[i])) != EOF) i=i+1; */
1.218 brouard 7528:
1.227 brouard 7529: /* imx=i; */
7530: /* for (i=1; i<imx;i++) popeffectif[popage[i]]=popcount[i]; */
7531: /* } */
1.218 brouard 7532:
1.227 brouard 7533: /* for(cptcov=1,k=0;cptcov<=i2;cptcov++){ */
7534: /* for(cptcod=1;cptcod<=ncodemax[cptcoveff];cptcod++){ */
7535: /* k=k+1; */
7536: /* fprintf(ficrespop,"\n#******"); */
7537: /* for(j=1;j<=cptcoveff;j++) { */
7538: /* fprintf(ficrespop," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]); */
7539: /* } */
7540: /* fprintf(ficrespop,"******\n"); */
7541: /* fprintf(ficrespop,"# Age"); */
7542: /* for(j=1; j<=nlstate+ndeath;j++) fprintf(ficrespop," P.%d",j); */
7543: /* if (popforecast==1) fprintf(ficrespop," [Population]"); */
1.126 brouard 7544:
1.227 brouard 7545: /* for (cpt=0; cpt<=0;cpt++) { */
7546: /* fprintf(ficrespop,"\n\n# Forecasting at date %.lf/%.lf/%.lf ",jpyram,mpyram,anpyram+cpt); */
1.126 brouard 7547:
1.227 brouard 7548: /* for (agedeb=(fage-((int)calagedatem %12/12.)); agedeb>=(ageminpar-((int)calagedatem %12)/12.); agedeb--){ */
7549: /* nhstepm=(int) rint((agelim-agedeb)*YEARM/stepm); */
7550: /* nhstepm = nhstepm/hstepm; */
1.126 brouard 7551:
1.227 brouard 7552: /* p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); */
7553: /* oldm=oldms;savm=savms; */
7554: /* hpxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k); */
1.218 brouard 7555:
1.227 brouard 7556: /* for (h=0; h<=nhstepm; h++){ */
7557: /* if (h==(int) (calagedatem+YEARM*cpt)) { */
7558: /* fprintf(ficrespop,"\n %3.f ",agedeb+h*hstepm/YEARM*stepm); */
7559: /* } */
7560: /* for(j=1; j<=nlstate+ndeath;j++) { */
7561: /* kk1=0.;kk2=0; */
7562: /* for(i=1; i<=nlstate;i++) { */
7563: /* if (mobilav==1) */
7564: /* kk1=kk1+p3mat[i][j][h]*mobaverage[(int)agedeb+1][i][cptcod]; */
7565: /* else { */
7566: /* kk1=kk1+p3mat[i][j][h]*probs[(int)(agedeb+1)][i][cptcod]; */
7567: /* } */
7568: /* } */
7569: /* if (h==(int)(calagedatem+12*cpt)){ */
7570: /* tabpop[(int)(agedeb)][j][cptcod]=kk1; */
7571: /* /\*fprintf(ficrespop," %.3f", kk1); */
7572: /* if (popforecast==1) fprintf(ficrespop," [%.f]", kk1*popeffectif[(int)agedeb+1]);*\/ */
7573: /* } */
7574: /* } */
7575: /* for(i=1; i<=nlstate;i++){ */
7576: /* kk1=0.; */
7577: /* for(j=1; j<=nlstate;j++){ */
7578: /* kk1= kk1+tabpop[(int)(agedeb)][j][cptcod]; */
7579: /* } */
7580: /* tabpopprev[(int)(agedeb)][i][cptcod]=tabpop[(int)(agedeb)][i][cptcod]/kk1*popeffectif[(int)(agedeb+(calagedatem+12*cpt)*hstepm/YEARM*stepm-1)]; */
7581: /* } */
1.218 brouard 7582:
1.227 brouard 7583: /* if (h==(int)(calagedatem+12*cpt)) */
7584: /* for(j=1; j<=nlstate;j++) */
7585: /* fprintf(ficrespop," %15.2f",tabpopprev[(int)(agedeb+1)][j][cptcod]); */
7586: /* } */
7587: /* free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); */
7588: /* } */
7589: /* } */
1.218 brouard 7590:
1.227 brouard 7591: /* /\******\/ */
1.218 brouard 7592:
1.227 brouard 7593: /* for (cpt=1; cpt<=(anpyram1-anpyram);cpt++) { */
7594: /* fprintf(ficrespop,"\n\n# Forecasting at date %.lf/%.lf/%.lf ",jpyram,mpyram,anpyram+cpt); */
7595: /* for (agedeb=(fage-((int)calagedatem %12/12.)); agedeb>=(ageminpar-((int)calagedatem %12)/12.); agedeb--){ */
7596: /* nhstepm=(int) rint((agelim-agedeb)*YEARM/stepm); */
7597: /* nhstepm = nhstepm/hstepm; */
1.126 brouard 7598:
1.227 brouard 7599: /* p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); */
7600: /* oldm=oldms;savm=savms; */
7601: /* hpxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k); */
7602: /* for (h=0; h<=nhstepm; h++){ */
7603: /* if (h==(int) (calagedatem+YEARM*cpt)) { */
7604: /* fprintf(ficresf,"\n %3.f ",agedeb+h*hstepm/YEARM*stepm); */
7605: /* } */
7606: /* for(j=1; j<=nlstate+ndeath;j++) { */
7607: /* kk1=0.;kk2=0; */
7608: /* for(i=1; i<=nlstate;i++) { */
7609: /* kk1=kk1+p3mat[i][j][h]*tabpopprev[(int)agedeb+1][i][cptcod]; */
7610: /* } */
7611: /* if (h==(int)(calagedatem+12*cpt)) fprintf(ficresf," %15.2f", kk1); */
7612: /* } */
7613: /* } */
7614: /* free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); */
7615: /* } */
7616: /* } */
7617: /* } */
7618: /* } */
1.218 brouard 7619:
1.227 brouard 7620: /* /\* if (mobilav!=0) free_ma3x(mobaverage,1, AGESUP,1,NCOVMAX, 1,NCOVMAX); *\/ */
1.218 brouard 7621:
1.227 brouard 7622: /* if (popforecast==1) { */
7623: /* free_ivector(popage,0,AGESUP); */
7624: /* free_vector(popeffectif,0,AGESUP); */
7625: /* free_vector(popcount,0,AGESUP); */
7626: /* } */
7627: /* free_ma3x(tabpop,1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
7628: /* free_ma3x(tabpopprev,1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
7629: /* fclose(ficrespop); */
7630: /* } /\* End of popforecast *\/ */
1.218 brouard 7631:
1.126 brouard 7632: int fileappend(FILE *fichier, char *optionfich)
7633: {
7634: if((fichier=fopen(optionfich,"a"))==NULL) {
7635: printf("Problem with file: %s\n", optionfich);
7636: fprintf(ficlog,"Problem with file: %s\n", optionfich);
7637: return (0);
7638: }
7639: fflush(fichier);
7640: return (1);
7641: }
7642:
7643:
7644: /**************** function prwizard **********************/
7645: void prwizard(int ncovmodel, int nlstate, int ndeath, char model[], FILE *ficparo)
7646: {
7647:
7648: /* Wizard to print covariance matrix template */
7649:
1.164 brouard 7650: char ca[32], cb[32];
7651: int i,j, k, li, lj, lk, ll, jj, npar, itimes;
1.126 brouard 7652: int numlinepar;
7653:
7654: printf("# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n");
7655: fprintf(ficparo,"# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n");
7656: for(i=1; i <=nlstate; i++){
7657: jj=0;
7658: for(j=1; j <=nlstate+ndeath; j++){
7659: if(j==i) continue;
7660: jj++;
7661: /*ca[0]= k+'a'-1;ca[1]='\0';*/
7662: printf("%1d%1d",i,j);
7663: fprintf(ficparo,"%1d%1d",i,j);
7664: for(k=1; k<=ncovmodel;k++){
7665: /* printf(" %lf",param[i][j][k]); */
7666: /* fprintf(ficparo," %lf",param[i][j][k]); */
7667: printf(" 0.");
7668: fprintf(ficparo," 0.");
7669: }
7670: printf("\n");
7671: fprintf(ficparo,"\n");
7672: }
7673: }
7674: printf("# Scales (for hessian or gradient estimation)\n");
7675: fprintf(ficparo,"# Scales (for hessian or gradient estimation)\n");
7676: npar= (nlstate+ndeath-1)*nlstate*ncovmodel; /* Number of parameters*/
7677: for(i=1; i <=nlstate; i++){
7678: jj=0;
7679: for(j=1; j <=nlstate+ndeath; j++){
7680: if(j==i) continue;
7681: jj++;
7682: fprintf(ficparo,"%1d%1d",i,j);
7683: printf("%1d%1d",i,j);
7684: fflush(stdout);
7685: for(k=1; k<=ncovmodel;k++){
7686: /* printf(" %le",delti3[i][j][k]); */
7687: /* fprintf(ficparo," %le",delti3[i][j][k]); */
7688: printf(" 0.");
7689: fprintf(ficparo," 0.");
7690: }
7691: numlinepar++;
7692: printf("\n");
7693: fprintf(ficparo,"\n");
7694: }
7695: }
7696: printf("# Covariance matrix\n");
7697: /* # 121 Var(a12)\n\ */
7698: /* # 122 Cov(b12,a12) Var(b12)\n\ */
7699: /* # 131 Cov(a13,a12) Cov(a13,b12, Var(a13)\n\ */
7700: /* # 132 Cov(b13,a12) Cov(b13,b12, Cov(b13,a13) Var(b13)\n\ */
7701: /* # 212 Cov(a21,a12) Cov(a21,b12, Cov(a21,a13) Cov(a21,b13) Var(a21)\n\ */
7702: /* # 212 Cov(b21,a12) Cov(b21,b12, Cov(b21,a13) Cov(b21,b13) Cov(b21,a21) Var(b21)\n\ */
7703: /* # 232 Cov(a23,a12) Cov(a23,b12, Cov(a23,a13) Cov(a23,b13) Cov(a23,a21) Cov(a23,b21) Var(a23)\n\ */
7704: /* # 232 Cov(b23,a12) Cov(b23,b12) ... Var (b23)\n" */
7705: fflush(stdout);
7706: fprintf(ficparo,"# Covariance matrix\n");
7707: /* # 121 Var(a12)\n\ */
7708: /* # 122 Cov(b12,a12) Var(b12)\n\ */
7709: /* # ...\n\ */
7710: /* # 232 Cov(b23,a12) Cov(b23,b12) ... Var (b23)\n" */
7711:
7712: for(itimes=1;itimes<=2;itimes++){
7713: jj=0;
7714: for(i=1; i <=nlstate; i++){
7715: for(j=1; j <=nlstate+ndeath; j++){
7716: if(j==i) continue;
7717: for(k=1; k<=ncovmodel;k++){
7718: jj++;
7719: ca[0]= k+'a'-1;ca[1]='\0';
7720: if(itimes==1){
7721: printf("#%1d%1d%d",i,j,k);
7722: fprintf(ficparo,"#%1d%1d%d",i,j,k);
7723: }else{
7724: printf("%1d%1d%d",i,j,k);
7725: fprintf(ficparo,"%1d%1d%d",i,j,k);
7726: /* printf(" %.5le",matcov[i][j]); */
7727: }
7728: ll=0;
7729: for(li=1;li <=nlstate; li++){
7730: for(lj=1;lj <=nlstate+ndeath; lj++){
7731: if(lj==li) continue;
7732: for(lk=1;lk<=ncovmodel;lk++){
7733: ll++;
7734: if(ll<=jj){
7735: cb[0]= lk +'a'-1;cb[1]='\0';
7736: if(ll<jj){
7737: if(itimes==1){
7738: printf(" Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj);
7739: fprintf(ficparo," Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj);
7740: }else{
7741: printf(" 0.");
7742: fprintf(ficparo," 0.");
7743: }
7744: }else{
7745: if(itimes==1){
7746: printf(" Var(%s%1d%1d)",ca,i,j);
7747: fprintf(ficparo," Var(%s%1d%1d)",ca,i,j);
7748: }else{
7749: printf(" 0.");
7750: fprintf(ficparo," 0.");
7751: }
7752: }
7753: }
7754: } /* end lk */
7755: } /* end lj */
7756: } /* end li */
7757: printf("\n");
7758: fprintf(ficparo,"\n");
7759: numlinepar++;
7760: } /* end k*/
7761: } /*end j */
7762: } /* end i */
7763: } /* end itimes */
7764:
7765: } /* end of prwizard */
7766: /******************* Gompertz Likelihood ******************************/
7767: double gompertz(double x[])
7768: {
7769: double A,B,L=0.0,sump=0.,num=0.;
7770: int i,n=0; /* n is the size of the sample */
7771:
1.220 brouard 7772: for (i=1;i<=imx ; i++) {
1.126 brouard 7773: sump=sump+weight[i];
7774: /* sump=sump+1;*/
7775: num=num+1;
7776: }
7777:
7778:
7779: /* for (i=0; i<=imx; i++)
7780: 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]);*/
7781:
7782: for (i=1;i<=imx ; i++)
7783: {
7784: if (cens[i] == 1 && wav[i]>1)
7785: A=-x[1]/(x[2])*(exp(x[2]*(agecens[i]-agegomp))-exp(x[2]*(ageexmed[i]-agegomp)));
7786:
7787: if (cens[i] == 0 && wav[i]>1)
7788: A=-x[1]/(x[2])*(exp(x[2]*(agedc[i]-agegomp))-exp(x[2]*(ageexmed[i]-agegomp)))
7789: +log(x[1]/YEARM)+x[2]*(agedc[i]-agegomp)+log(YEARM);
7790:
7791: /*if (wav[i] > 1 && agecens[i] > 15) {*/ /* ??? */
7792: if (wav[i] > 1 ) { /* ??? */
7793: L=L+A*weight[i];
7794: /* 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]);*/
7795: }
7796: }
7797:
7798: /*printf("x1=%2.9f x2=%2.9f x3=%2.9f L=%f\n",x[1],x[2],x[3],L);*/
7799:
7800: return -2*L*num/sump;
7801: }
7802:
1.136 brouard 7803: #ifdef GSL
7804: /******************* Gompertz_f Likelihood ******************************/
7805: double gompertz_f(const gsl_vector *v, void *params)
7806: {
7807: double A,B,LL=0.0,sump=0.,num=0.;
7808: double *x= (double *) v->data;
7809: int i,n=0; /* n is the size of the sample */
7810:
7811: for (i=0;i<=imx-1 ; i++) {
7812: sump=sump+weight[i];
7813: /* sump=sump+1;*/
7814: num=num+1;
7815: }
7816:
7817:
7818: /* for (i=0; i<=imx; i++)
7819: 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]);*/
7820: printf("x[0]=%lf x[1]=%lf\n",x[0],x[1]);
7821: for (i=1;i<=imx ; i++)
7822: {
7823: if (cens[i] == 1 && wav[i]>1)
7824: A=-x[0]/(x[1])*(exp(x[1]*(agecens[i]-agegomp))-exp(x[1]*(ageexmed[i]-agegomp)));
7825:
7826: if (cens[i] == 0 && wav[i]>1)
7827: A=-x[0]/(x[1])*(exp(x[1]*(agedc[i]-agegomp))-exp(x[1]*(ageexmed[i]-agegomp)))
7828: +log(x[0]/YEARM)+x[1]*(agedc[i]-agegomp)+log(YEARM);
7829:
7830: /*if (wav[i] > 1 && agecens[i] > 15) {*/ /* ??? */
7831: if (wav[i] > 1 ) { /* ??? */
7832: LL=LL+A*weight[i];
7833: /* 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]);*/
7834: }
7835: }
7836:
7837: /*printf("x1=%2.9f x2=%2.9f x3=%2.9f L=%f\n",x[1],x[2],x[3],L);*/
7838: printf("x[0]=%lf x[1]=%lf -2*LL*num/sump=%lf\n",x[0],x[1],-2*LL*num/sump);
7839:
7840: return -2*LL*num/sump;
7841: }
7842: #endif
7843:
1.126 brouard 7844: /******************* Printing html file ***********/
1.201 brouard 7845: void printinghtmlmort(char fileresu[], char title[], char datafile[], int firstpass, \
1.126 brouard 7846: int lastpass, int stepm, int weightopt, char model[],\
7847: int imx, double p[],double **matcov,double agemortsup){
7848: int i,k;
7849:
7850: fprintf(fichtm,"<ul><li><h4>Result files </h4>\n Force of mortality. Parameters of the Gompertz fit (with confidence interval in brackets):<br>");
7851: fprintf(fichtm," mu(age) =%lf*exp(%lf*(age-%d)) per year<br><br>",p[1],p[2],agegomp);
7852: for (i=1;i<=2;i++)
7853: 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 7854: fprintf(fichtm,"<br><br><img src=\"graphmort.svg\">");
1.126 brouard 7855: fprintf(fichtm,"</ul>");
7856:
7857: fprintf(fichtm,"<ul><li><h4>Life table</h4>\n <br>");
7858:
7859: 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>");
7860:
7861: for (k=agegomp;k<(agemortsup-2);k++)
7862: 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]);
7863:
7864:
7865: fflush(fichtm);
7866: }
7867:
7868: /******************* Gnuplot file **************/
1.201 brouard 7869: void printinggnuplotmort(char fileresu[], char optionfilefiname[], double ageminpar, double agemaxpar, double fage , char pathc[], double p[]){
1.126 brouard 7870:
7871: char dirfileres[132],optfileres[132];
1.164 brouard 7872:
1.126 brouard 7873: int ng;
7874:
7875:
7876: /*#ifdef windows */
7877: fprintf(ficgp,"cd \"%s\" \n",pathc);
7878: /*#endif */
7879:
7880:
7881: strcpy(dirfileres,optionfilefiname);
7882: strcpy(optfileres,"vpl");
1.199 brouard 7883: fprintf(ficgp,"set out \"graphmort.svg\"\n ");
1.126 brouard 7884: fprintf(ficgp,"set xlabel \"Age\"\n set ylabel \"Force of mortality (per year)\" \n ");
1.199 brouard 7885: fprintf(ficgp, "set ter svg size 640, 480\n set log y\n");
1.145 brouard 7886: /* fprintf(ficgp, "set size 0.65,0.65\n"); */
1.126 brouard 7887: fprintf(ficgp,"plot [%d:100] %lf*exp(%lf*(x-%d))",agegomp,p[1],p[2],agegomp);
7888:
7889: }
7890:
1.136 brouard 7891: int readdata(char datafile[], int firstobs, int lastobs, int *imax)
7892: {
1.126 brouard 7893:
1.136 brouard 7894: /*-------- data file ----------*/
7895: FILE *fic;
7896: char dummy[]=" ";
1.240 brouard 7897: int i=0, j=0, n=0, iv=0, v;
1.223 brouard 7898: int lstra;
1.136 brouard 7899: int linei, month, year,iout;
7900: char line[MAXLINE], linetmp[MAXLINE];
1.164 brouard 7901: char stra[MAXLINE], strb[MAXLINE];
1.136 brouard 7902: char *stratrunc;
1.223 brouard 7903:
1.240 brouard 7904: DummyV=ivector(1,NCOVMAX); /* 1 to 3 */
7905: FixedV=ivector(1,NCOVMAX); /* 1 to 3 */
1.126 brouard 7906:
1.240 brouard 7907: for(v=1; v <=ncovcol;v++){
7908: DummyV[v]=0;
7909: FixedV[v]=0;
7910: }
7911: for(v=ncovcol+1; v <=ncovcol+nqv;v++){
7912: DummyV[v]=1;
7913: FixedV[v]=0;
7914: }
7915: for(v=ncovcol+nqv+1; v <=ncovcol+nqv+ntv;v++){
7916: DummyV[v]=0;
7917: FixedV[v]=1;
7918: }
7919: for(v=ncovcol+nqv+ntv+1; v <=ncovcol+nqv+ntv+nqtv;v++){
7920: DummyV[v]=1;
7921: FixedV[v]=1;
7922: }
7923: for(v=1; v <=ncovcol+nqv+ntv+nqtv;v++){
7924: printf("Covariate type in the data: V%d, DummyV(V%d)=%d, FixedV(V%d)=%d\n",v,v,DummyV[v],v,FixedV[v]);
7925: fprintf(ficlog,"Covariate type in the data: V%d, DummyV(V%d)=%d, FixedV(V%d)=%d\n",v,v,DummyV[v],v,FixedV[v]);
7926: }
1.126 brouard 7927:
1.136 brouard 7928: if((fic=fopen(datafile,"r"))==NULL) {
1.218 brouard 7929: printf("Problem while opening datafile: %s with errno='%s'\n", datafile,strerror(errno));fflush(stdout);
7930: fprintf(ficlog,"Problem while opening datafile: %s with errno='%s'\n", datafile,strerror(errno));fflush(ficlog);return 1;
1.136 brouard 7931: }
1.126 brouard 7932:
1.136 brouard 7933: i=1;
7934: linei=0;
7935: while ((fgets(line, MAXLINE, fic) != NULL) &&((i >= firstobs) && (i <=lastobs))) {
7936: linei=linei+1;
7937: for(j=strlen(line); j>=0;j--){ /* Untabifies line */
7938: if(line[j] == '\t')
7939: line[j] = ' ';
7940: }
7941: for(j=strlen(line)-1; (line[j]==' ')||(line[j]==10)||(line[j]==13);j--){
7942: ;
7943: };
7944: line[j+1]=0; /* Trims blanks at end of line */
7945: if(line[0]=='#'){
7946: fprintf(ficlog,"Comment line\n%s\n",line);
7947: printf("Comment line\n%s\n",line);
7948: continue;
7949: }
7950: trimbb(linetmp,line); /* Trims multiple blanks in line */
1.164 brouard 7951: strcpy(line, linetmp);
1.223 brouard 7952:
7953: /* Loops on waves */
7954: for (j=maxwav;j>=1;j--){
7955: for (iv=nqtv;iv>=1;iv--){ /* Loop on time varying quantitative variables */
1.238 brouard 7956: cutv(stra, strb, line, ' ');
7957: if(strb[0]=='.') { /* Missing value */
7958: lval=-1;
7959: cotqvar[j][iv][i]=-1; /* 0.0/0.0 */
7960: cotvar[j][ntv+iv][i]=-1; /* For performance reasons */
7961: if(isalpha(strb[1])) { /* .m or .d Really Missing value */
7962: 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);
7963: 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);
7964: return 1;
7965: }
7966: }else{
7967: errno=0;
7968: /* what_kind_of_number(strb); */
7969: dval=strtod(strb,&endptr);
7970: /* if( strb[0]=='\0' || (*endptr != '\0')){ */
7971: /* if(strb != endptr && *endptr == '\0') */
7972: /* dval=dlval; */
7973: /* if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN)) */
7974: if( strb[0]=='\0' || (*endptr != '\0')){
7975: 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);
7976: 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);
7977: return 1;
7978: }
7979: cotqvar[j][iv][i]=dval;
7980: cotvar[j][ntv+iv][i]=dval;
7981: }
7982: strcpy(line,stra);
1.223 brouard 7983: }/* end loop ntqv */
1.225 brouard 7984:
1.223 brouard 7985: for (iv=ntv;iv>=1;iv--){ /* Loop on time varying dummies */
1.238 brouard 7986: cutv(stra, strb, line, ' ');
7987: if(strb[0]=='.') { /* Missing value */
7988: lval=-1;
7989: }else{
7990: errno=0;
7991: lval=strtol(strb,&endptr,10);
7992: /* if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN))*/
7993: if( strb[0]=='\0' || (*endptr != '\0')){
7994: 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);
7995: 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);
7996: return 1;
7997: }
7998: }
7999: if(lval <-1 || lval >1){
8000: printf("Error reading data around '%ld' at line number %d for individual %d, '%s'\n \
1.223 brouard 8001: Should be a value of %d(nth) covariate (0 should be the value for the reference and 1\n \
8002: for the alternative. IMaCh does not build design variables automatically, do it yourself.\n \
1.238 brouard 8003: For example, for multinomial values like 1, 2 and 3,\n \
8004: build V1=0 V2=0 for the reference value (1),\n \
8005: V1=1 V2=0 for (2) \n \
1.223 brouard 8006: and V1=0 V2=1 for (3). V1=1 V2=1 should not exist and the corresponding\n \
1.238 brouard 8007: output of IMaCh is often meaningless.\n \
1.223 brouard 8008: Exiting.\n",lval,linei, i,line,j);
1.238 brouard 8009: fprintf(ficlog,"Error reading data around '%ld' at line number %d for individual %d, '%s'\n \
1.223 brouard 8010: Should be a value of %d(nth) covariate (0 should be the value for the reference and 1\n \
8011: for the alternative. IMaCh does not build design variables automatically, do it yourself.\n \
1.238 brouard 8012: For example, for multinomial values like 1, 2 and 3,\n \
8013: build V1=0 V2=0 for the reference value (1),\n \
8014: V1=1 V2=0 for (2) \n \
1.223 brouard 8015: and V1=0 V2=1 for (3). V1=1 V2=1 should not exist and the corresponding\n \
1.238 brouard 8016: output of IMaCh is often meaningless.\n \
1.223 brouard 8017: Exiting.\n",lval,linei, i,line,j);fflush(ficlog);
1.238 brouard 8018: return 1;
8019: }
8020: cotvar[j][iv][i]=(double)(lval);
8021: strcpy(line,stra);
1.223 brouard 8022: }/* end loop ntv */
1.225 brouard 8023:
1.223 brouard 8024: /* Statuses at wave */
1.137 brouard 8025: cutv(stra, strb, line, ' ');
1.223 brouard 8026: if(strb[0]=='.') { /* Missing value */
1.238 brouard 8027: lval=-1;
1.136 brouard 8028: }else{
1.238 brouard 8029: errno=0;
8030: lval=strtol(strb,&endptr,10);
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 a status of wave %d. Setting maxwav=%d might be wrong. Exiting.\n", strb, linei,i,line,j,maxwav);
8034: 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);
8035: return 1;
8036: }
1.136 brouard 8037: }
1.225 brouard 8038:
1.136 brouard 8039: s[j][i]=lval;
1.225 brouard 8040:
1.223 brouard 8041: /* Date of Interview */
1.136 brouard 8042: strcpy(line,stra);
8043: cutv(stra, strb,line,' ');
1.169 brouard 8044: if( (iout=sscanf(strb,"%d/%d",&month, &year)) != 0){
1.136 brouard 8045: }
1.169 brouard 8046: else if( (iout=sscanf(strb,"%s.",dummy)) != 0){
1.225 brouard 8047: month=99;
8048: year=9999;
1.136 brouard 8049: }else{
1.225 brouard 8050: 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);
8051: 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);
8052: return 1;
1.136 brouard 8053: }
8054: anint[j][i]= (double) year;
8055: mint[j][i]= (double)month;
8056: strcpy(line,stra);
1.223 brouard 8057: } /* End loop on waves */
1.225 brouard 8058:
1.223 brouard 8059: /* Date of death */
1.136 brouard 8060: cutv(stra, strb,line,' ');
1.169 brouard 8061: if( (iout=sscanf(strb,"%d/%d",&month, &year)) != 0){
1.136 brouard 8062: }
1.169 brouard 8063: else if( (iout=sscanf(strb,"%s.",dummy)) != 0){
1.136 brouard 8064: month=99;
8065: year=9999;
8066: }else{
1.141 brouard 8067: 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 8068: 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);
8069: return 1;
1.136 brouard 8070: }
8071: andc[i]=(double) year;
8072: moisdc[i]=(double) month;
8073: strcpy(line,stra);
8074:
1.223 brouard 8075: /* Date of birth */
1.136 brouard 8076: cutv(stra, strb,line,' ');
1.169 brouard 8077: if( (iout=sscanf(strb,"%d/%d",&month, &year)) != 0){
1.136 brouard 8078: }
1.169 brouard 8079: else if( (iout=sscanf(strb,"%s.", dummy)) != 0){
1.136 brouard 8080: month=99;
8081: year=9999;
8082: }else{
1.141 brouard 8083: 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);
8084: 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 8085: return 1;
1.136 brouard 8086: }
8087: if (year==9999) {
1.141 brouard 8088: 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);
8089: 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 8090: return 1;
8091:
1.136 brouard 8092: }
8093: annais[i]=(double)(year);
8094: moisnais[i]=(double)(month);
8095: strcpy(line,stra);
1.225 brouard 8096:
1.223 brouard 8097: /* Sample weight */
1.136 brouard 8098: cutv(stra, strb,line,' ');
8099: errno=0;
8100: dval=strtod(strb,&endptr);
8101: if( strb[0]=='\0' || (*endptr != '\0')){
1.141 brouard 8102: printf("Error reading data around '%f' at line number %d, \"%s\" for individual %d\nShould be a weight. Exiting.\n",dval, i,line,linei);
8103: 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 8104: fflush(ficlog);
8105: return 1;
8106: }
8107: weight[i]=dval;
8108: strcpy(line,stra);
1.225 brouard 8109:
1.223 brouard 8110: for (iv=nqv;iv>=1;iv--){ /* Loop on fixed quantitative variables */
8111: cutv(stra, strb, line, ' ');
8112: if(strb[0]=='.') { /* Missing value */
1.225 brouard 8113: lval=-1;
1.223 brouard 8114: }else{
1.225 brouard 8115: errno=0;
8116: /* what_kind_of_number(strb); */
8117: dval=strtod(strb,&endptr);
8118: /* if(strb != endptr && *endptr == '\0') */
8119: /* dval=dlval; */
8120: /* if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN)) */
8121: if( strb[0]=='\0' || (*endptr != '\0')){
8122: 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);
8123: 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);
8124: return 1;
8125: }
8126: coqvar[iv][i]=dval;
1.226 brouard 8127: covar[ncovcol+iv][i]=dval; /* including qvar in standard covar for performance reasons */
1.223 brouard 8128: }
8129: strcpy(line,stra);
8130: }/* end loop nqv */
1.136 brouard 8131:
1.223 brouard 8132: /* Covariate values */
1.136 brouard 8133: for (j=ncovcol;j>=1;j--){
8134: cutv(stra, strb,line,' ');
1.223 brouard 8135: if(strb[0]=='.') { /* Missing covariate value */
1.225 brouard 8136: lval=-1;
1.136 brouard 8137: }else{
1.225 brouard 8138: errno=0;
8139: lval=strtol(strb,&endptr,10);
8140: if( strb[0]=='\0' || (*endptr != '\0')){
8141: 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);
8142: 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);
8143: return 1;
8144: }
1.136 brouard 8145: }
8146: if(lval <-1 || lval >1){
1.225 brouard 8147: printf("Error reading data around '%ld' at line number %d for individual %d, '%s'\n \
1.136 brouard 8148: Should be a value of %d(nth) covariate (0 should be the value for the reference and 1\n \
8149: for the alternative. IMaCh does not build design variables automatically, do it yourself.\n \
1.225 brouard 8150: For example, for multinomial values like 1, 2 and 3,\n \
8151: build V1=0 V2=0 for the reference value (1),\n \
8152: V1=1 V2=0 for (2) \n \
1.136 brouard 8153: and V1=0 V2=1 for (3). V1=1 V2=1 should not exist and the corresponding\n \
1.225 brouard 8154: output of IMaCh is often meaningless.\n \
1.136 brouard 8155: Exiting.\n",lval,linei, i,line,j);
1.225 brouard 8156: fprintf(ficlog,"Error reading data around '%ld' at line number %d for individual %d, '%s'\n \
1.136 brouard 8157: Should be a value of %d(nth) covariate (0 should be the value for the reference and 1\n \
8158: for the alternative. IMaCh does not build design variables automatically, do it yourself.\n \
1.225 brouard 8159: For example, for multinomial values like 1, 2 and 3,\n \
8160: build V1=0 V2=0 for the reference value (1),\n \
8161: V1=1 V2=0 for (2) \n \
1.136 brouard 8162: and V1=0 V2=1 for (3). V1=1 V2=1 should not exist and the corresponding\n \
1.225 brouard 8163: output of IMaCh is often meaningless.\n \
1.136 brouard 8164: Exiting.\n",lval,linei, i,line,j);fflush(ficlog);
1.225 brouard 8165: return 1;
1.136 brouard 8166: }
8167: covar[j][i]=(double)(lval);
8168: strcpy(line,stra);
8169: }
8170: lstra=strlen(stra);
1.225 brouard 8171:
1.136 brouard 8172: if(lstra > 9){ /* More than 2**32 or max of what printf can write with %ld */
8173: stratrunc = &(stra[lstra-9]);
8174: num[i]=atol(stratrunc);
8175: }
8176: else
8177: num[i]=atol(stra);
8178: /*if((s[2][i]==2) && (s[3][i]==-1)&&(s[4][i]==9)){
8179: 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;}*/
8180:
8181: i=i+1;
8182: } /* End loop reading data */
1.225 brouard 8183:
1.136 brouard 8184: *imax=i-1; /* Number of individuals */
8185: fclose(fic);
1.225 brouard 8186:
1.136 brouard 8187: return (0);
1.164 brouard 8188: /* endread: */
1.225 brouard 8189: printf("Exiting readdata: ");
8190: fclose(fic);
8191: return (1);
1.223 brouard 8192: }
1.126 brouard 8193:
1.234 brouard 8194: void removefirstspace(char **stri){/*, char stro[]) {*/
1.230 brouard 8195: char *p1 = *stri, *p2 = *stri;
1.235 brouard 8196: while (*p2 == ' ')
1.234 brouard 8197: p2++;
8198: /* while ((*p1++ = *p2++) !=0) */
8199: /* ; */
8200: /* do */
8201: /* while (*p2 == ' ') */
8202: /* p2++; */
8203: /* while (*p1++ == *p2++); */
8204: *stri=p2;
1.145 brouard 8205: }
8206:
1.235 brouard 8207: int decoderesult ( char resultline[], int nres)
1.230 brouard 8208: /**< This routine decode one result line and returns the combination # of dummy covariates only **/
8209: {
1.235 brouard 8210: int j=0, k=0, k1=0, k2=0, k3=0, k4=0, match=0, k2q=0, k3q=0, k4q=0;
1.230 brouard 8211: char resultsav[MAXLINE];
1.234 brouard 8212: int resultmodel[MAXLINE];
8213: int modelresult[MAXLINE];
1.230 brouard 8214: char stra[80], strb[80], strc[80], strd[80],stre[80];
8215:
1.234 brouard 8216: removefirstspace(&resultline);
1.233 brouard 8217: printf("decoderesult:%s\n",resultline);
1.230 brouard 8218:
8219: if (strstr(resultline,"v") !=0){
8220: printf("Error. 'v' must be in upper case 'V' result: %s ",resultline);
8221: fprintf(ficlog,"Error. 'v' must be in upper case result: %s ",resultline);fflush(ficlog);
8222: return 1;
8223: }
8224: trimbb(resultsav, resultline);
8225: if (strlen(resultsav) >1){
8226: j=nbocc(resultsav,'='); /**< j=Number of covariate values'=' */
8227: }
1.234 brouard 8228: if( j != cptcovs ){ /* Be careful if a variable is in a product but not single */
8229: 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);
8230: 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);
8231: }
8232: for(k=1; k<=j;k++){ /* Loop on any covariate of the result line */
8233: if(nbocc(resultsav,'=') >1){
8234: cutl(stra,strb,resultsav,' '); /* keeps in strb after the first ' '
8235: resultsav= V4=1 V5=25.1 V3=0 strb=V3=0 stra= V4=1 V5=25.1 */
8236: cutl(strc,strd,strb,'='); /* strb:V4=1 strc=1 strd=V4 */
8237: }else
8238: cutl(strc,strd,resultsav,'=');
1.230 brouard 8239: Tvalsel[k]=atof(strc); /* 1 */
1.234 brouard 8240:
1.230 brouard 8241: cutl(strc,stre,strd,'V'); /* strd='V4' strc=4 stre='V' */;
8242: Tvarsel[k]=atoi(strc);
8243: /* Typevarsel[k]=1; /\* 1 for age product *\/ */
8244: /* cptcovsel++; */
8245: if (nbocc(stra,'=') >0)
8246: strcpy(resultsav,stra); /* and analyzes it */
8247: }
1.235 brouard 8248: /* Checking for missing or useless values in comparison of current model needs */
1.236 brouard 8249: for(k1=1; k1<= cptcovt ;k1++){ /* model line V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
8250: if(Typevar[k1]==0){ /* Single covariate in model */
1.234 brouard 8251: match=0;
1.236 brouard 8252: for(k2=1; k2 <=j;k2++){/* result line V4=1 V5=24.1 V3=1 V2=8 V1=0 */
1.237 brouard 8253: if(Tvar[k1]==Tvarsel[k2]) {/* Tvar[1]=5 == Tvarsel[2]=5 */
1.236 brouard 8254: modelresult[k2]=k1;/* modelresult[2]=1 modelresult[1]=2 modelresult[3]=3 modelresult[6]=4 modelresult[9]=5 */
1.234 brouard 8255: match=1;
8256: break;
8257: }
8258: }
8259: if(match == 0){
8260: printf("Error in result line: %d value missing; result: %s, model=%s\n",k1, resultline, model);
8261: }
8262: }
8263: }
1.235 brouard 8264: /* Checking for missing or useless values in comparison of current model needs */
8265: for(k2=1; k2 <=j;k2++){ /* result line V4=1 V5=24.1 V3=1 V2=8 V1=0 */
1.234 brouard 8266: match=0;
1.235 brouard 8267: for(k1=1; k1<= cptcovt ;k1++){ /* model line V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
8268: if(Typevar[k1]==0){ /* Single */
1.237 brouard 8269: if(Tvar[k1]==Tvarsel[k2]) { /* Tvar[2]=4 == Tvarsel[1]=4 */
1.235 brouard 8270: resultmodel[k1]=k2; /* resultmodel[2]=1 resultmodel[1]=2 resultmodel[3]=3 resultmodel[6]=4 resultmodel[9]=5 */
1.234 brouard 8271: ++match;
8272: }
8273: }
8274: }
8275: if(match == 0){
8276: printf("Error in result line: %d value missing; result: %s, model=%s\n",k1, resultline, model);
8277: }else if(match > 1){
8278: printf("Error in result line: %d doubled; result: %s, model=%s\n",k2, resultline, model);
8279: }
8280: }
1.235 brouard 8281:
1.234 brouard 8282: /* We need to deduce which combination number is chosen and save quantitative values */
1.235 brouard 8283: /* model line V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
8284: /* result line V4=1 V5=25.1 V3=0 V2=8 V1=1 */
8285: /* should give a combination of dummy V4=1, V3=0, V1=1 => V4*2**(0) + V3*2**(1) + V1*2**(2) = 5 + (1offset) = 6*/
8286: /* result line V4=1 V5=24.1 V3=1 V2=8 V1=0 */
8287: /* should give a combination of dummy V4=1, V3=1, V1=0 => V4*2**(0) + V3*2**(1) + V1*2**(2) = 3 + (1offset) = 4*/
8288: /* 1 0 0 0 */
8289: /* 2 1 0 0 */
8290: /* 3 0 1 0 */
8291: /* 4 1 1 0 */ /* V4=1, V3=1, V1=0 */
8292: /* 5 0 0 1 */
8293: /* 6 1 0 1 */ /* V4=1, V3=0, V1=1 */
8294: /* 7 0 1 1 */
8295: /* 8 1 1 1 */
1.237 brouard 8296: /* V(Tvresult)=Tresult V4=1 V3=0 V1=1 Tresult[nres=1][2]=0 */
8297: /* V(Tvqresult)=Tqresult V5=25.1 V2=8 Tqresult[nres=1][1]=25.1 */
8298: /* V5*age V5 known which value for nres? */
8299: /* Tqinvresult[2]=8 Tqinvresult[1]=25.1 */
1.235 brouard 8300: for(k1=1, k=0, k4=0, k4q=0; k1 <=cptcovt;k1++){ /* model line */
8301: if( Dummy[k1]==0 && Typevar[k1]==0 ){ /* Single dummy */
1.237 brouard 8302: k3= resultmodel[k1]; /* resultmodel[2(V4)] = 1=k3 */
1.235 brouard 8303: k2=(int)Tvarsel[k3]; /* Tvarsel[resultmodel[2]]= Tvarsel[1] = 4=k2 */
8304: k+=Tvalsel[k3]*pow(2,k4); /* Tvalsel[1]=1 */
1.237 brouard 8305: Tresult[nres][k4+1]=Tvalsel[k3];/* Tresult[nres][1]=1(V4=1) Tresult[nres][2]=0(V3=0) */
8306: Tvresult[nres][k4+1]=(int)Tvarsel[k3];/* Tvresult[nres][1]=4 Tvresult[nres][3]=1 */
8307: Tinvresult[nres][(int)Tvarsel[k3]]=Tvalsel[k3]; /* Tinvresult[nres][4]=1 */
1.235 brouard 8308: printf("Decoderesult Dummy k=%d, V(k2=V%d)= Tvalsel[%d]=%d, 2**(%d)\n",k, k2, k3, (int)Tvalsel[k3], k4);
8309: k4++;;
8310: } else if( Dummy[k1]==1 && Typevar[k1]==0 ){ /* Single quantitative */
8311: k3q= resultmodel[k1]; /* resultmodel[2] = 1=k3 */
8312: k2q=(int)Tvarsel[k3q]; /* Tvarsel[resultmodel[2]]= Tvarsel[1] = 4=k2 */
1.237 brouard 8313: Tqresult[nres][k4q+1]=Tvalsel[k3q]; /* Tqresult[nres][1]=25.1 */
8314: Tvqresult[nres][k4q+1]=(int)Tvarsel[k3q]; /* Tvqresult[nres][1]=5 */
8315: Tqinvresult[nres][(int)Tvarsel[k3q]]=Tvalsel[k3q]; /* Tqinvresult[nres][5]=25.1 */
1.235 brouard 8316: printf("Decoderesult Quantitative nres=%d, V(k2q=V%d)= Tvalsel[%d]=%d, Tvarsel[%d]=%f\n",nres, k2q, k3q, Tvarsel[k3q], k3q, Tvalsel[k3q]);
8317: k4q++;;
8318: }
8319: }
1.234 brouard 8320:
1.235 brouard 8321: TKresult[nres]=++k; /* Combination for the nresult and the model */
1.230 brouard 8322: return (0);
8323: }
1.235 brouard 8324:
1.230 brouard 8325: int decodemodel( char model[], int lastobs)
8326: /**< This routine decodes the model and returns:
1.224 brouard 8327: * Model V1+V2+V3+V8+V7*V8+V5*V6+V8*age+V3*age+age*age
8328: * - nagesqr = 1 if age*age in the model, otherwise 0.
8329: * - cptcovt total number of covariates of the model nbocc(+)+1 = 8 excepting constant and age and age*age
8330: * - cptcovn or number of covariates k of the models excluding age*products =6 and age*age
8331: * - cptcovage number of covariates with age*products =2
8332: * - cptcovs number of simple covariates
8333: * - 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
8334: * which is a new column after the 9 (ncovcol) variables.
8335: * - if k is a product Vn*Vm covar[k][i] is filled with correct values for each individual
8336: * - Tprod[l] gives the kth covariates of the product Vn*Vm l=1 to cptcovprod-cptcovage
8337: * Tprod[1]@2 {5, 6}: position of first product V7*V8 is 5, and second V5*V6 is 6.
8338: * - Tvard[k] p Tvard[1][1]@4 {7, 8, 5, 6} for V7*V8 and V5*V6 .
8339: */
1.136 brouard 8340: {
1.238 brouard 8341: int i, j, k, ks, v;
1.227 brouard 8342: int j1, k1, k2, k3, k4;
1.136 brouard 8343: char modelsav[80];
1.145 brouard 8344: char stra[80], strb[80], strc[80], strd[80],stre[80];
1.187 brouard 8345: char *strpt;
1.136 brouard 8346:
1.145 brouard 8347: /*removespace(model);*/
1.136 brouard 8348: if (strlen(model) >1){ /* If there is at least 1 covariate */
1.145 brouard 8349: j=0, j1=0, k1=0, k2=-1, ks=0, cptcovn=0;
1.137 brouard 8350: if (strstr(model,"AGE") !=0){
1.192 brouard 8351: printf("Error. AGE must be in lower case 'age' model=1+age+%s. ",model);
8352: fprintf(ficlog,"Error. AGE must be in lower case model=1+age+%s. ",model);fflush(ficlog);
1.136 brouard 8353: return 1;
8354: }
1.141 brouard 8355: if (strstr(model,"v") !=0){
8356: printf("Error. 'v' must be in upper case 'V' model=%s ",model);
8357: fprintf(ficlog,"Error. 'v' must be in upper case model=%s ",model);fflush(ficlog);
8358: return 1;
8359: }
1.187 brouard 8360: strcpy(modelsav,model);
8361: if ((strpt=strstr(model,"age*age")) !=0){
8362: printf(" strpt=%s, model=%s\n",strpt, model);
8363: if(strpt != model){
1.234 brouard 8364: printf("Error in model: 'model=%s'; 'age*age' should in first place before other covariates\n \
1.192 brouard 8365: 'model=1+age+age*age+V1.' or 'model=1+age+age*age+V1+V1*age.', please swap as well as \n \
1.187 brouard 8366: corresponding column of parameters.\n",model);
1.234 brouard 8367: fprintf(ficlog,"Error in model: 'model=%s'; 'age*age' should in first place before other covariates\n \
1.192 brouard 8368: 'model=1+age+age*age+V1.' or 'model=1+age+age*age+V1+V1*age.', please swap as well as \n \
1.187 brouard 8369: corresponding column of parameters.\n",model); fflush(ficlog);
1.234 brouard 8370: return 1;
1.225 brouard 8371: }
1.187 brouard 8372: nagesqr=1;
8373: if (strstr(model,"+age*age") !=0)
1.234 brouard 8374: substrchaine(modelsav, model, "+age*age");
1.187 brouard 8375: else if (strstr(model,"age*age+") !=0)
1.234 brouard 8376: substrchaine(modelsav, model, "age*age+");
1.187 brouard 8377: else
1.234 brouard 8378: substrchaine(modelsav, model, "age*age");
1.187 brouard 8379: }else
8380: nagesqr=0;
8381: if (strlen(modelsav) >1){
8382: j=nbocc(modelsav,'+'); /**< j=Number of '+' */
8383: j1=nbocc(modelsav,'*'); /**< j1=Number of '*' */
1.224 brouard 8384: cptcovs=j+1-j1; /**< Number of simple covariates V1+V1*age+V3 +V3*V4+age*age=> V1 + V3 =5-3=2 */
1.187 brouard 8385: cptcovt= j+1; /* Number of total covariates in the model, not including
1.225 brouard 8386: * cst, age and age*age
8387: * V1+V1*age+ V3 + V3*V4+age*age=> 3+1=4*/
8388: /* including age products which are counted in cptcovage.
8389: * but the covariates which are products must be treated
8390: * separately: ncovn=4- 2=2 (V1+V3). */
1.187 brouard 8391: cptcovprod=j1; /**< Number of products V1*V2 +v3*age = 2 */
8392: cptcovprodnoage=0; /**< Number of covariate products without age: V3*V4 =1 */
1.225 brouard 8393:
8394:
1.187 brouard 8395: /* Design
8396: * V1 V2 V3 V4 V5 V6 V7 V8 V9 Weight
8397: * < ncovcol=8 >
8398: * Model V2 + V1 + V3*age + V3 + V5*V6 + V7*V8 + V8*age + V8
8399: * k= 1 2 3 4 5 6 7 8
8400: * cptcovn number of covariates (not including constant and age ) = # of + plus 1 = 7+1=8
8401: * covar[k,i], value of kth covariate if not including age for individual i:
1.224 brouard 8402: * covar[1][i]= (V1), covar[4][i]=(V4), covar[8][i]=(V8)
8403: * Tvar[k] # of the kth covariate: Tvar[1]=2 Tvar[2]=1 Tvar[4]=3 Tvar[8]=8
1.187 brouard 8404: * if multiplied by age: V3*age Tvar[3=V3*age]=3 (V3) Tvar[7]=8 and
8405: * Tage[++cptcovage]=k
8406: * if products, new covar are created after ncovcol with k1
8407: * Tvar[k]=ncovcol+k1; # of the kth covariate product: Tvar[5]=ncovcol+1=10 Tvar[6]=ncovcol+1=11
8408: * Tprod[k1]=k; Tprod[1]=5 Tprod[2]= 6; gives the position of the k1th product
8409: * 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
8410: * Tvar[cptcovn+k2]=Tvard[k1][1];Tvar[cptcovn+k2+1]=Tvard[k1][2];
8411: * Tvar[8+1]=5;Tvar[8+2]=6;Tvar[8+3]=7;Tvar[8+4]=8 inverted
8412: * V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11
8413: * < ncovcol=8 >
8414: * Model V2 + V1 + V3*age + V3 + V5*V6 + V7*V8 + V8*age + V8 d1 d1 d2 d2
8415: * k= 1 2 3 4 5 6 7 8 9 10 11 12
8416: * Tvar[k]= 2 1 3 3 10 11 8 8 5 6 7 8
8417: * p Tvar[1]@12={2, 1, 3, 3, 11, 10, 8, 8, 7, 8, 5, 6}
8418: * p Tprod[1]@2={ 6, 5}
8419: *p Tvard[1][1]@4= {7, 8, 5, 6}
8420: * covar[k][i]= V2 V1 ? V3 V5*V6? V7*V8? ? V8
8421: * cov[Tage[kk]+2]=covar[Tvar[Tage[kk]]][i]*cov[2];
8422: *How to reorganize?
8423: * Model V1 + V2 + V3 + V8 + V5*V6 + V7*V8 + V3*age + V8*age
8424: * Tvars {2, 1, 3, 3, 11, 10, 8, 8, 7, 8, 5, 6}
8425: * {2, 1, 4, 8, 5, 6, 3, 7}
8426: * Struct []
8427: */
1.225 brouard 8428:
1.187 brouard 8429: /* This loop fills the array Tvar from the string 'model'.*/
8430: /* j is the number of + signs in the model V1+V2+V3 j=2 i=3 to 1 */
8431: /* modelsav=V2+V1+V4+age*V3 strb=age*V3 stra=V2+V1+V4 */
8432: /* k=4 (age*V3) Tvar[k=4]= 3 (from V3) Tage[cptcovage=1]=4 */
8433: /* k=3 V4 Tvar[k=3]= 4 (from V4) */
8434: /* k=2 V1 Tvar[k=2]= 1 (from V1) */
8435: /* k=1 Tvar[1]=2 (from V2) */
8436: /* k=5 Tvar[5] */
8437: /* for (k=1; k<=cptcovn;k++) { */
1.198 brouard 8438: /* cov[2+k]=nbcode[Tvar[k]][codtabm(ij,Tvar[k])]; */
1.187 brouard 8439: /* } */
1.198 brouard 8440: /* for (k=1; k<=cptcovage;k++) cov[2+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,Tvar[Tage[k])]]*cov[2]; */
1.187 brouard 8441: /*
8442: * Treating invertedly V2+V1+V3*age+V2*V4 is as if written V2*V4 +V3*age + V1 + V2 */
1.227 brouard 8443: for(k=cptcovt; k>=1;k--){ /**< Number of covariates not including constant and age, neither age*age*/
8444: Tvar[k]=0; Tprod[k]=0; Tposprod[k]=0;
8445: }
1.187 brouard 8446: cptcovage=0;
8447: for(k=1; k<=cptcovt;k++){ /* Loop on total covariates of the model */
1.234 brouard 8448: cutl(stra,strb,modelsav,'+'); /* keeps in strb after the first '+'
1.225 brouard 8449: modelsav==V2+V1+V4+V3*age strb=V3*age stra=V2+V1+V4 */
1.234 brouard 8450: if (nbocc(modelsav,'+')==0) strcpy(strb,modelsav); /* and analyzes it */
8451: /* printf("i=%d a=%s b=%s sav=%s\n",i, stra,strb,modelsav);*/
8452: /*scanf("%d",i);*/
8453: if (strchr(strb,'*')) { /**< Model includes a product V2+V1+V4+V3*age strb=V3*age */
8454: cutl(strc,strd,strb,'*'); /**< strd*strc Vm*Vn: strb=V3*age(input) strc=age strd=V3 ; V3*V2 strc=V2, strd=V3 */
8455: if (strcmp(strc,"age")==0) { /**< Model includes age: Vn*age */
8456: /* covar is not filled and then is empty */
8457: cptcovprod--;
8458: cutl(stre,strb,strd,'V'); /* strd=V3(input): stre="3" */
8459: Tvar[k]=atoi(stre); /* V2+V1+V4+V3*age Tvar[4]=3 ; V1+V2*age Tvar[2]=2; V1+V1*age Tvar[2]=1 */
8460: Typevar[k]=1; /* 1 for age product */
8461: cptcovage++; /* Sums the number of covariates which include age as a product */
8462: Tage[cptcovage]=k; /* Tvar[4]=3, Tage[1] = 4 or V1+V1*age Tvar[2]=1, Tage[1]=2 */
8463: /*printf("stre=%s ", stre);*/
8464: } else if (strcmp(strd,"age")==0) { /* or age*Vn */
8465: cptcovprod--;
8466: cutl(stre,strb,strc,'V');
8467: Tvar[k]=atoi(stre);
8468: Typevar[k]=1; /* 1 for age product */
8469: cptcovage++;
8470: Tage[cptcovage]=k;
8471: } else { /* Age is not in the model product V2+V1+V1*V4+V3*age+V3*V2 strb=V3*V2*/
8472: /* loops on k1=1 (V3*V2) and k1=2 V4*V3 */
8473: cptcovn++;
8474: cptcovprodnoage++;k1++;
8475: cutl(stre,strb,strc,'V'); /* strc= Vn, stre is n; strb=V3*V2 stre=3 strc=*/
8476: Tvar[k]=ncovcol+nqv+ntv+nqtv+k1; /* For model-covariate k tells which data-covariate to use but
8477: because this model-covariate is a construction we invent a new column
8478: which is after existing variables ncovcol+nqv+ntv+nqtv + k1
8479: If already ncovcol=4 and model=V2+V1+V1*V4+age*V3+V3*V2
8480: Tvar[3=V1*V4]=4+1 Tvar[5=V3*V2]=4 + 2= 6, etc */
8481: Typevar[k]=2; /* 2 for double fixed dummy covariates */
8482: cutl(strc,strb,strd,'V'); /* strd was Vm, strc is m */
8483: Tprod[k1]=k; /* Tprod[1]=3(=V1*V4) for V2+V1+V1*V4+age*V3+V3*V2 */
8484: Tposprod[k]=k1; /* Tpsprod[3]=1, Tposprod[2]=5 */
8485: Tvard[k1][1] =atoi(strc); /* m 1 for V1*/
8486: Tvard[k1][2] =atoi(stre); /* n 4 for V4*/
8487: k2=k2+2; /* k2 is initialize to -1, We want to store the n and m in Vn*Vm at the end of Tvar */
8488: /* Tvar[cptcovt+k2]=Tvard[k1][1]; /\* Tvar[(cptcovt=4+k2=1)=5]= 1 (V1) *\/ */
8489: /* Tvar[cptcovt+k2+1]=Tvard[k1][2]; /\* Tvar[(cptcovt=4+(k2=1)+1)=6]= 4 (V4) *\/ */
1.225 brouard 8490: /*ncovcol=4 and model=V2+V1+V1*V4+age*V3+V3*V2, Tvar[3]=5, Tvar[4]=6, cptcovt=5 */
1.234 brouard 8491: /* 1 2 3 4 5 | Tvar[5+1)=1, Tvar[7]=2 */
8492: for (i=1; i<=lastobs;i++){
8493: /* Computes the new covariate which is a product of
8494: covar[n][i]* covar[m][i] and stores it at ncovol+k1 May not be defined */
8495: covar[ncovcol+k1][i]=covar[atoi(stre)][i]*covar[atoi(strc)][i];
8496: }
8497: } /* End age is not in the model */
8498: } /* End if model includes a product */
8499: else { /* no more sum */
8500: /*printf("d=%s c=%s b=%s\n", strd,strc,strb);*/
8501: /* scanf("%d",i);*/
8502: cutl(strd,strc,strb,'V');
8503: ks++; /**< Number of simple covariates dummy or quantitative, fixe or varying */
8504: cptcovn++; /** V4+V3+V5: V4 and V3 timevarying dummy covariates, V5 timevarying quantitative */
8505: Tvar[k]=atoi(strd);
8506: Typevar[k]=0; /* 0 for simple covariates */
8507: }
8508: strcpy(modelsav,stra); /* modelsav=V2+V1+V4 stra=V2+V1+V4 */
1.223 brouard 8509: /*printf("a=%s b=%s sav=%s\n", stra,strb,modelsav);
1.225 brouard 8510: scanf("%d",i);*/
1.187 brouard 8511: } /* end of loop + on total covariates */
8512: } /* end if strlen(modelsave == 0) age*age might exist */
8513: } /* end if strlen(model == 0) */
1.136 brouard 8514:
8515: /*The number n of Vn is stored in Tvar. cptcovage =number of age covariate. Tage gives the position of age. cptcovprod= number of products.
8516: If model=V1+V1*age then Tvar[1]=1 Tvar[2]=1 cptcovage=1 Tage[1]=2 cptcovprod=0*/
1.225 brouard 8517:
1.136 brouard 8518: /* printf("tvar1=%d tvar2=%d tvar3=%d cptcovage=%d Tage=%d",Tvar[1],Tvar[2],Tvar[3],cptcovage,Tage[1]);
1.225 brouard 8519: printf("cptcovprod=%d ", cptcovprod);
8520: fprintf(ficlog,"cptcovprod=%d ", cptcovprod);
8521: scanf("%d ",i);*/
8522:
8523:
1.230 brouard 8524: /* Until here, decodemodel knows only the grammar (simple, product, age*) of the model but not what kind
8525: of variable (dummy vs quantitative, fixed vs time varying) is behind. But we know the # of each. */
1.226 brouard 8526: /* ncovcol= 1, nqv=1 | ntv=2, nqtv= 1 = 5 possible variables data: 2 fixed 3, varying
8527: model= V5 + V4 +V3 + V4*V3 + V5*age + V2 + V1*V2 + V1*age + V5*age, V1 is not used saving its place
8528: k = 1 2 3 4 5 6 7 8 9
8529: Tvar[k]= 5 4 3 1+1+2+1+1=6 5 2 7 1 5
8530: Typevar[k]= 0 0 0 2 1 0 2 1 1
1.227 brouard 8531: Fixed[k] 1 1 1 1 3 0 0 or 2 2 3
8532: Dummy[k] 1 0 0 0 3 1 1 2 3
8533: Tmodelind[combination of covar]=k;
1.225 brouard 8534: */
8535: /* Dispatching between quantitative and time varying covariates */
1.226 brouard 8536: /* If Tvar[k] >ncovcol it is a product */
1.225 brouard 8537: /* 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 8538: /* Computing effective variables, ie used by the model, that is from the cptcovt variables */
1.227 brouard 8539: printf("Model=%s\n\
8540: Typevar: 0 for simple covariate (dummy, quantitative, fixed or varying), 1 for age product, 2 for product \n\
8541: Fixed[k] 0=fixed (product or simple), 1 varying, 2 fixed with age product, 3 varying with age product \n\
8542: 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);
8543: fprintf(ficlog,"Model=%s\n\
8544: Typevar: 0 for simple covariate (dummy, quantitative, fixed or varying), 1 for age product, 2 for product \n\
8545: Fixed[k] 0=fixed (product or simple), 1 varying, 2 fixed with age product, 3 varying with age product \n\
8546: 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);
1.240 brouard 8547: for(k=1;k<=cptcovt; k++){ Fixed[k]=0; Dummy[k]=0;}
1.234 brouard 8548: 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 */
8549: if (Tvar[k] <=ncovcol && Typevar[k]==0 ){ /* Simple fixed dummy (<=ncovcol) covariates */
1.227 brouard 8550: Fixed[k]= 0;
8551: Dummy[k]= 0;
1.225 brouard 8552: ncoveff++;
1.232 brouard 8553: ncovf++;
1.234 brouard 8554: nsd++;
8555: modell[k].maintype= FTYPE;
8556: TvarsD[nsd]=Tvar[k];
8557: TvarsDind[nsd]=k;
8558: TvarF[ncovf]=Tvar[k];
8559: TvarFind[ncovf]=k;
8560: TvarFD[ncoveff]=Tvar[k]; /* TvarFD[1]=V1 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
8561: TvarFDind[ncoveff]=k; /* TvarFDind[1]=9 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
8562: }else if( Tvar[k] <=ncovcol && Typevar[k]==2){ /* Product of fixed dummy (<=ncovcol) covariates */
8563: Fixed[k]= 0;
8564: Dummy[k]= 0;
8565: ncoveff++;
8566: ncovf++;
8567: modell[k].maintype= FTYPE;
8568: TvarF[ncovf]=Tvar[k];
8569: TvarFind[ncovf]=k;
1.230 brouard 8570: TvarFD[ncoveff]=Tvar[k]; /* TvarFD[1]=V1 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
1.231 brouard 8571: TvarFDind[ncoveff]=k; /* TvarFDind[1]=9 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
1.240 brouard 8572: }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 8573: Fixed[k]= 0;
8574: Dummy[k]= 1;
1.230 brouard 8575: nqfveff++;
1.234 brouard 8576: modell[k].maintype= FTYPE;
8577: modell[k].subtype= FQ;
8578: nsq++;
8579: TvarsQ[nsq]=Tvar[k];
8580: TvarsQind[nsq]=k;
1.232 brouard 8581: ncovf++;
1.234 brouard 8582: TvarF[ncovf]=Tvar[k];
8583: TvarFind[ncovf]=k;
1.231 brouard 8584: 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 8585: 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.242 brouard 8586: }else if( Tvar[k] <=ncovcol+nqv+ntv && Typevar[k]==0){/* Only simple time varying dummy variables */
1.227 brouard 8587: Fixed[k]= 1;
8588: Dummy[k]= 0;
1.225 brouard 8589: ntveff++; /* Only simple time varying dummy variable */
1.234 brouard 8590: modell[k].maintype= VTYPE;
8591: modell[k].subtype= VD;
8592: nsd++;
8593: TvarsD[nsd]=Tvar[k];
8594: TvarsDind[nsd]=k;
8595: ncovv++; /* Only simple time varying variables */
8596: TvarV[ncovv]=Tvar[k];
1.242 brouard 8597: TvarVind[ncovv]=k; /* TvarVind[2]=2 TvarVind[3]=3 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ /* Any time varying singele */
1.231 brouard 8598: 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 */
8599: 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 8600: 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);
8601: printf("Quasi TmodelInvind[%d]=%d\n",k,Tvar[k]- ncovcol-nqv);
1.231 brouard 8602: }else if( Tvar[k] <=ncovcol+nqv+ntv+nqtv && Typevar[k]==0){ /* Only simple time varying quantitative variable V5*/
1.234 brouard 8603: Fixed[k]= 1;
8604: Dummy[k]= 1;
8605: nqtveff++;
8606: modell[k].maintype= VTYPE;
8607: modell[k].subtype= VQ;
8608: ncovv++; /* Only simple time varying variables */
8609: nsq++;
8610: TvarsQ[nsq]=Tvar[k];
8611: TvarsQind[nsq]=k;
8612: TvarV[ncovv]=Tvar[k];
1.242 brouard 8613: TvarVind[ncovv]=k; /* TvarVind[1]=1 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ /* Any time varying singele */
1.231 brouard 8614: 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 */
8615: 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 8616: TmodelInvQind[nqtveff]=Tvar[k]- ncovcol-nqv-ntv;/* Only simple time varying quantitative variable */
8617: /* Tmodeliqind[k]=nqtveff;/\* Only simple time varying quantitative variable *\/ */
8618: 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 8619: printf("Quasi TmodelInvQind[%d]=%d\n",k,Tvar[k]- ncovcol-nqv-ntv);
1.227 brouard 8620: }else if (Typevar[k] == 1) { /* product with age */
1.234 brouard 8621: ncova++;
8622: TvarA[ncova]=Tvar[k];
8623: TvarAind[ncova]=k;
1.231 brouard 8624: if (Tvar[k] <=ncovcol ){ /* Product age with fixed dummy covariatee */
1.240 brouard 8625: Fixed[k]= 2;
8626: Dummy[k]= 2;
8627: modell[k].maintype= ATYPE;
8628: modell[k].subtype= APFD;
8629: /* ncoveff++; */
1.227 brouard 8630: }else if( Tvar[k] <=ncovcol+nqv) { /* Remind that product Vn*Vm are added in k*/
1.240 brouard 8631: Fixed[k]= 2;
8632: Dummy[k]= 3;
8633: modell[k].maintype= ATYPE;
8634: modell[k].subtype= APFQ; /* Product age * fixed quantitative */
8635: /* nqfveff++; /\* Only simple fixed quantitative variable *\/ */
1.227 brouard 8636: }else if( Tvar[k] <=ncovcol+nqv+ntv ){
1.240 brouard 8637: Fixed[k]= 3;
8638: Dummy[k]= 2;
8639: modell[k].maintype= ATYPE;
8640: modell[k].subtype= APVD; /* Product age * varying dummy */
8641: /* ntveff++; /\* Only simple time varying dummy variable *\/ */
1.227 brouard 8642: }else if( Tvar[k] <=ncovcol+nqv+ntv+nqtv){
1.240 brouard 8643: Fixed[k]= 3;
8644: Dummy[k]= 3;
8645: modell[k].maintype= ATYPE;
8646: modell[k].subtype= APVQ; /* Product age * varying quantitative */
8647: /* nqtveff++;/\* Only simple time varying quantitative variable *\/ */
1.227 brouard 8648: }
8649: }else if (Typevar[k] == 2) { /* product without age */
8650: k1=Tposprod[k];
8651: if(Tvard[k1][1] <=ncovcol){
1.240 brouard 8652: if(Tvard[k1][2] <=ncovcol){
8653: Fixed[k]= 1;
8654: Dummy[k]= 0;
8655: modell[k].maintype= FTYPE;
8656: modell[k].subtype= FPDD; /* Product fixed dummy * fixed dummy */
8657: ncovf++; /* Fixed variables without age */
8658: TvarF[ncovf]=Tvar[k];
8659: TvarFind[ncovf]=k;
8660: }else if(Tvard[k1][2] <=ncovcol+nqv){
8661: Fixed[k]= 0; /* or 2 ?*/
8662: Dummy[k]= 1;
8663: modell[k].maintype= FTYPE;
8664: modell[k].subtype= FPDQ; /* Product fixed dummy * fixed quantitative */
8665: ncovf++; /* Varying variables without age */
8666: TvarF[ncovf]=Tvar[k];
8667: TvarFind[ncovf]=k;
8668: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv){
8669: Fixed[k]= 1;
8670: Dummy[k]= 0;
8671: modell[k].maintype= VTYPE;
8672: modell[k].subtype= VPDD; /* Product fixed dummy * varying dummy */
8673: ncovv++; /* Varying variables without age */
8674: TvarV[ncovv]=Tvar[k];
8675: TvarVind[ncovv]=k;
8676: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv+nqtv){
8677: Fixed[k]= 1;
8678: Dummy[k]= 1;
8679: modell[k].maintype= VTYPE;
8680: modell[k].subtype= VPDQ; /* Product fixed dummy * varying quantitative */
8681: ncovv++; /* Varying variables without age */
8682: TvarV[ncovv]=Tvar[k];
8683: TvarVind[ncovv]=k;
8684: }
1.227 brouard 8685: }else if(Tvard[k1][1] <=ncovcol+nqv){
1.240 brouard 8686: if(Tvard[k1][2] <=ncovcol){
8687: Fixed[k]= 0; /* or 2 ?*/
8688: Dummy[k]= 1;
8689: modell[k].maintype= FTYPE;
8690: modell[k].subtype= FPDQ; /* Product fixed quantitative * fixed dummy */
8691: ncovf++; /* Fixed variables without age */
8692: TvarF[ncovf]=Tvar[k];
8693: TvarFind[ncovf]=k;
8694: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv){
8695: Fixed[k]= 1;
8696: Dummy[k]= 1;
8697: modell[k].maintype= VTYPE;
8698: modell[k].subtype= VPDQ; /* Product fixed quantitative * varying dummy */
8699: ncovv++; /* Varying variables without age */
8700: TvarV[ncovv]=Tvar[k];
8701: TvarVind[ncovv]=k;
8702: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv+nqtv){
8703: Fixed[k]= 1;
8704: Dummy[k]= 1;
8705: modell[k].maintype= VTYPE;
8706: modell[k].subtype= VPQQ; /* Product fixed quantitative * varying quantitative */
8707: ncovv++; /* Varying variables without age */
8708: TvarV[ncovv]=Tvar[k];
8709: TvarVind[ncovv]=k;
8710: ncovv++; /* Varying variables without age */
8711: TvarV[ncovv]=Tvar[k];
8712: TvarVind[ncovv]=k;
8713: }
1.227 brouard 8714: }else if(Tvard[k1][1] <=ncovcol+nqv+ntv){
1.240 brouard 8715: if(Tvard[k1][2] <=ncovcol){
8716: Fixed[k]= 1;
8717: Dummy[k]= 1;
8718: modell[k].maintype= VTYPE;
8719: modell[k].subtype= VPDD; /* Product time varying dummy * fixed dummy */
8720: ncovv++; /* Varying variables without age */
8721: TvarV[ncovv]=Tvar[k];
8722: TvarVind[ncovv]=k;
8723: }else if(Tvard[k1][2] <=ncovcol+nqv){
8724: Fixed[k]= 1;
8725: Dummy[k]= 1;
8726: modell[k].maintype= VTYPE;
8727: modell[k].subtype= VPDQ; /* Product time varying dummy * fixed quantitative */
8728: ncovv++; /* Varying variables without age */
8729: TvarV[ncovv]=Tvar[k];
8730: TvarVind[ncovv]=k;
8731: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv){
8732: Fixed[k]= 1;
8733: Dummy[k]= 0;
8734: modell[k].maintype= VTYPE;
8735: modell[k].subtype= VPDD; /* Product time varying dummy * time varying dummy */
8736: ncovv++; /* Varying variables without age */
8737: TvarV[ncovv]=Tvar[k];
8738: TvarVind[ncovv]=k;
8739: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv+nqtv){
8740: Fixed[k]= 1;
8741: Dummy[k]= 1;
8742: modell[k].maintype= VTYPE;
8743: modell[k].subtype= VPDQ; /* Product time varying dummy * time varying quantitative */
8744: ncovv++; /* Varying variables without age */
8745: TvarV[ncovv]=Tvar[k];
8746: TvarVind[ncovv]=k;
8747: }
1.227 brouard 8748: }else if(Tvard[k1][1] <=ncovcol+nqv+ntv+nqtv){
1.240 brouard 8749: if(Tvard[k1][2] <=ncovcol){
8750: Fixed[k]= 1;
8751: Dummy[k]= 1;
8752: modell[k].maintype= VTYPE;
8753: modell[k].subtype= VPDQ; /* Product time varying quantitative * fixed dummy */
8754: ncovv++; /* Varying variables without age */
8755: TvarV[ncovv]=Tvar[k];
8756: TvarVind[ncovv]=k;
8757: }else if(Tvard[k1][2] <=ncovcol+nqv){
8758: Fixed[k]= 1;
8759: Dummy[k]= 1;
8760: modell[k].maintype= VTYPE;
8761: modell[k].subtype= VPQQ; /* Product time varying quantitative * fixed quantitative */
8762: ncovv++; /* Varying variables without age */
8763: TvarV[ncovv]=Tvar[k];
8764: TvarVind[ncovv]=k;
8765: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv){
8766: Fixed[k]= 1;
8767: Dummy[k]= 1;
8768: modell[k].maintype= VTYPE;
8769: modell[k].subtype= VPDQ; /* Product time varying quantitative * time varying dummy */
8770: ncovv++; /* Varying variables without age */
8771: TvarV[ncovv]=Tvar[k];
8772: TvarVind[ncovv]=k;
8773: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv+nqtv){
8774: Fixed[k]= 1;
8775: Dummy[k]= 1;
8776: modell[k].maintype= VTYPE;
8777: modell[k].subtype= VPQQ; /* Product time varying quantitative * time varying quantitative */
8778: ncovv++; /* Varying variables without age */
8779: TvarV[ncovv]=Tvar[k];
8780: TvarVind[ncovv]=k;
8781: }
1.227 brouard 8782: }else{
1.240 brouard 8783: printf("Error unknown type of covariate: Tvard[%d][1]=%d,Tvard[%d][2]=%d\n",k1,Tvard[k1][1],k1,Tvard[k1][2]);
8784: fprintf(ficlog,"Error unknown type of covariate: Tvard[%d][1]=%d,Tvard[%d][2]=%d\n",k1,Tvard[k1][1],k1,Tvard[k1][2]);
8785: } /*end k1*/
1.225 brouard 8786: }else{
1.226 brouard 8787: printf("Error, current version can't treat for performance reasons, Tvar[%d]=%d, Typevar[%d]=%d\n", k, Tvar[k], k, Typevar[k]);
8788: 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 8789: }
1.227 brouard 8790: 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 8791: printf(" modell[%d].maintype=%d, modell[%d].subtype=%d\n",k,modell[k].maintype,k,modell[k].subtype);
1.227 brouard 8792: 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]);
8793: }
8794: /* Searching for doublons in the model */
8795: for(k1=1; k1<= cptcovt;k1++){
8796: for(k2=1; k2 <k1;k2++){
8797: if((Typevar[k1]==Typevar[k2]) && (Fixed[Tvar[k1]]==Fixed[Tvar[k2]]) && (Dummy[Tvar[k1]]==Dummy[Tvar[k2]] )){
1.234 brouard 8798: if((Typevar[k1] == 0 || Typevar[k1] == 1)){ /* Simple or age product */
8799: if(Tvar[k1]==Tvar[k2]){
8800: 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]]);
8801: 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);
8802: return(1);
8803: }
8804: }else if (Typevar[k1] ==2){
8805: k3=Tposprod[k1];
8806: k4=Tposprod[k2];
8807: 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])) ){
8808: 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]]);
8809: 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);
8810: return(1);
8811: }
8812: }
1.227 brouard 8813: }
8814: }
1.225 brouard 8815: }
8816: printf("ncoveff=%d, nqfveff=%d, ntveff=%d, nqtveff=%d, cptcovn=%d\n",ncoveff,nqfveff,ntveff,nqtveff,cptcovn);
8817: fprintf(ficlog,"ncoveff=%d, nqfveff=%d, ntveff=%d, nqtveff=%d, cptcovn=%d\n",ncoveff,nqfveff,ntveff,nqtveff,cptcovn);
1.234 brouard 8818: printf("ncovf=%d, ncovv=%d, ncova=%d, nsd=%d, nsq=%d\n",ncovf,ncovv,ncova,nsd,nsq);
8819: fprintf(ficlog,"ncovf=%d, ncovv=%d, ncova=%d, nsd=%d, nsq=%d\n",ncovf,ncovv,ncova,nsd, nsq);
1.137 brouard 8820: return (0); /* with covar[new additional covariate if product] and Tage if age */
1.164 brouard 8821: /*endread:*/
1.225 brouard 8822: printf("Exiting decodemodel: ");
8823: return (1);
1.136 brouard 8824: }
8825:
1.169 brouard 8826: int calandcheckages(int imx, int maxwav, double *agemin, double *agemax, int *nberr, int *nbwarn )
1.136 brouard 8827: {
8828: int i, m;
1.218 brouard 8829: int firstone=0;
8830:
1.136 brouard 8831: for (i=1; i<=imx; i++) {
8832: for(m=2; (m<= maxwav); m++) {
8833: if (((int)mint[m][i]== 99) && (s[m][i] <= nlstate)){
8834: anint[m][i]=9999;
1.216 brouard 8835: if (s[m][i] != -2) /* Keeping initial status of unknown vital status */
8836: s[m][i]=-1;
1.136 brouard 8837: }
8838: if((int)moisdc[i]==99 && (int)andc[i]==9999 && s[m][i]>nlstate){
1.169 brouard 8839: *nberr = *nberr + 1;
1.218 brouard 8840: if(firstone == 0){
8841: firstone=1;
8842: 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);
8843: }
8844: 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 8845: s[m][i]=-1;
8846: }
8847: if((int)moisdc[i]==99 && (int)andc[i]!=9999 && s[m][i]>nlstate){
1.169 brouard 8848: (*nberr)++;
1.136 brouard 8849: 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]);
8850: 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]);
8851: s[m][i]=-1; /* We prefer to skip it (and to skip it in version 0.8a1 too */
8852: }
8853: }
8854: }
8855:
8856: for (i=1; i<=imx; i++) {
8857: agedc[i]=(moisdc[i]/12.+andc[i])-(moisnais[i]/12.+annais[i]);
8858: for(m=firstpass; (m<= lastpass); m++){
1.214 brouard 8859: 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 8860: if (s[m][i] >= nlstate+1) {
1.169 brouard 8861: if(agedc[i]>0){
8862: if((int)moisdc[i]!=99 && (int)andc[i]!=9999){
1.136 brouard 8863: agev[m][i]=agedc[i];
1.214 brouard 8864: /*if(moisdc[i]==99 && andc[i]==9999) s[m][i]=-1;*/
1.169 brouard 8865: }else {
1.136 brouard 8866: if ((int)andc[i]!=9999){
8867: nbwarn++;
8868: printf("Warning negative age at death: %ld line:%d\n",num[i],i);
8869: fprintf(ficlog,"Warning negative age at death: %ld line:%d\n",num[i],i);
8870: agev[m][i]=-1;
8871: }
8872: }
1.169 brouard 8873: } /* agedc > 0 */
1.214 brouard 8874: } /* end if */
1.136 brouard 8875: else if(s[m][i] !=9){ /* Standard case, age in fractional
8876: years but with the precision of a month */
8877: agev[m][i]=(mint[m][i]/12.+1./24.+anint[m][i])-(moisnais[i]/12.+1./24.+annais[i]);
8878: if((int)mint[m][i]==99 || (int)anint[m][i]==9999)
8879: agev[m][i]=1;
8880: else if(agev[m][i] < *agemin){
8881: *agemin=agev[m][i];
8882: printf(" Min anint[%d][%d]=%.2f annais[%d]=%.2f, agemin=%.2f\n",m,i,anint[m][i], i,annais[i], *agemin);
8883: }
8884: else if(agev[m][i] >*agemax){
8885: *agemax=agev[m][i];
1.156 brouard 8886: /* printf(" Max anint[%d][%d]=%.0f annais[%d]=%.0f, agemax=%.2f\n",m,i,anint[m][i], i,annais[i], *agemax);*/
1.136 brouard 8887: }
8888: /*agev[m][i]=anint[m][i]-annais[i];*/
8889: /* agev[m][i] = age[i]+2*m;*/
1.214 brouard 8890: } /* en if 9*/
1.136 brouard 8891: else { /* =9 */
1.214 brouard 8892: /* printf("Debug num[%d]=%ld s[%d][%d]=%d\n",i,num[i], m,i, s[m][i]); */
1.136 brouard 8893: agev[m][i]=1;
8894: s[m][i]=-1;
8895: }
8896: }
1.214 brouard 8897: else if(s[m][i]==0) /*= 0 Unknown */
1.136 brouard 8898: agev[m][i]=1;
1.214 brouard 8899: else{
8900: printf("Warning, num[%d]=%ld, s[%d][%d]=%d\n", i, num[i], m, i,s[m][i]);
8901: fprintf(ficlog, "Warning, num[%d]=%ld, s[%d][%d]=%d\n", i, num[i], m, i,s[m][i]);
8902: agev[m][i]=0;
8903: }
8904: } /* End for lastpass */
8905: }
1.136 brouard 8906:
8907: for (i=1; i<=imx; i++) {
8908: for(m=firstpass; (m<=lastpass); m++){
8909: if (s[m][i] > (nlstate+ndeath)) {
1.169 brouard 8910: (*nberr)++;
1.136 brouard 8911: 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);
8912: 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);
8913: return 1;
8914: }
8915: }
8916: }
8917:
8918: /*for (i=1; i<=imx; i++){
8919: for (m=firstpass; (m<lastpass); m++){
8920: printf("%ld %d %.lf %d %d\n", num[i],(covar[1][i]),agev[m][i],s[m][i],s[m+1][i]);
8921: }
8922:
8923: }*/
8924:
8925:
1.139 brouard 8926: printf("Total number of individuals= %d, Agemin = %.2f, Agemax= %.2f\n\n", imx, *agemin, *agemax);
8927: fprintf(ficlog,"Total number of individuals= %d, Agemin = %.2f, Agemax= %.2f\n\n", imx, *agemin, *agemax);
1.136 brouard 8928:
8929: return (0);
1.164 brouard 8930: /* endread:*/
1.136 brouard 8931: printf("Exiting calandcheckages: ");
8932: return (1);
8933: }
8934:
1.172 brouard 8935: #if defined(_MSC_VER)
8936: /*printf("Visual C++ compiler: %s \n;", _MSC_FULL_VER);*/
8937: /*fprintf(ficlog, "Visual C++ compiler: %s \n;", _MSC_FULL_VER);*/
8938: //#include "stdafx.h"
8939: //#include <stdio.h>
8940: //#include <tchar.h>
8941: //#include <windows.h>
8942: //#include <iostream>
8943: typedef BOOL(WINAPI *LPFN_ISWOW64PROCESS) (HANDLE, PBOOL);
8944:
8945: LPFN_ISWOW64PROCESS fnIsWow64Process;
8946:
8947: BOOL IsWow64()
8948: {
8949: BOOL bIsWow64 = FALSE;
8950:
8951: //typedef BOOL (APIENTRY *LPFN_ISWOW64PROCESS)
8952: // (HANDLE, PBOOL);
8953:
8954: //LPFN_ISWOW64PROCESS fnIsWow64Process;
8955:
8956: HMODULE module = GetModuleHandle(_T("kernel32"));
8957: const char funcName[] = "IsWow64Process";
8958: fnIsWow64Process = (LPFN_ISWOW64PROCESS)
8959: GetProcAddress(module, funcName);
8960:
8961: if (NULL != fnIsWow64Process)
8962: {
8963: if (!fnIsWow64Process(GetCurrentProcess(),
8964: &bIsWow64))
8965: //throw std::exception("Unknown error");
8966: printf("Unknown error\n");
8967: }
8968: return bIsWow64 != FALSE;
8969: }
8970: #endif
1.177 brouard 8971:
1.191 brouard 8972: void syscompilerinfo(int logged)
1.167 brouard 8973: {
8974: /* #include "syscompilerinfo.h"*/
1.185 brouard 8975: /* command line Intel compiler 32bit windows, XP compatible:*/
8976: /* /GS /W3 /Gy
8977: /Zc:wchar_t /Zi /O2 /Fd"Release\vc120.pdb" /D "WIN32" /D "NDEBUG" /D
8978: "_CONSOLE" /D "_LIB" /D "_USING_V110_SDK71_" /D "_UNICODE" /D
8979: "UNICODE" /Qipo /Zc:forScope /Gd /Oi /MT /Fa"Release\" /EHsc /nologo
1.186 brouard 8980: /Fo"Release\" /Qprof-dir "Release\" /Fp"Release\IMaCh.pch"
8981: */
8982: /* 64 bits */
1.185 brouard 8983: /*
8984: /GS /W3 /Gy
8985: /Zc:wchar_t /Zi /O2 /Fd"x64\Release\vc120.pdb" /D "WIN32" /D "NDEBUG"
8986: /D "_CONSOLE" /D "_LIB" /D "_UNICODE" /D "UNICODE" /Qipo /Zc:forScope
8987: /Oi /MD /Fa"x64\Release\" /EHsc /nologo /Fo"x64\Release\" /Qprof-dir
8988: "x64\Release\" /Fp"x64\Release\IMaCh.pch" */
8989: /* Optimization are useless and O3 is slower than O2 */
8990: /*
8991: /GS /W3 /Gy /Zc:wchar_t /Zi /O3 /Fd"x64\Release\vc120.pdb" /D "WIN32"
8992: /D "NDEBUG" /D "_CONSOLE" /D "_LIB" /D "_UNICODE" /D "UNICODE" /Qipo
8993: /Zc:forScope /Oi /MD /Fa"x64\Release\" /EHsc /nologo /Qparallel
8994: /Fo"x64\Release\" /Qprof-dir "x64\Release\" /Fp"x64\Release\IMaCh.pch"
8995: */
1.186 brouard 8996: /* Link is */ /* /OUT:"visual studio
1.185 brouard 8997: 2013\Projects\IMaCh\Release\IMaCh.exe" /MANIFEST /NXCOMPAT
8998: /PDB:"visual studio
8999: 2013\Projects\IMaCh\Release\IMaCh.pdb" /DYNAMICBASE
9000: "kernel32.lib" "user32.lib" "gdi32.lib" "winspool.lib"
9001: "comdlg32.lib" "advapi32.lib" "shell32.lib" "ole32.lib"
9002: "oleaut32.lib" "uuid.lib" "odbc32.lib" "odbccp32.lib"
9003: /MACHINE:X86 /OPT:REF /SAFESEH /INCREMENTAL:NO
9004: /SUBSYSTEM:CONSOLE",5.01" /MANIFESTUAC:"level='asInvoker'
9005: uiAccess='false'"
9006: /ManifestFile:"Release\IMaCh.exe.intermediate.manifest" /OPT:ICF
9007: /NOLOGO /TLBID:1
9008: */
1.177 brouard 9009: #if defined __INTEL_COMPILER
1.178 brouard 9010: #if defined(__GNUC__)
9011: struct utsname sysInfo; /* For Intel on Linux and OS/X */
9012: #endif
1.177 brouard 9013: #elif defined(__GNUC__)
1.179 brouard 9014: #ifndef __APPLE__
1.174 brouard 9015: #include <gnu/libc-version.h> /* Only on gnu */
1.179 brouard 9016: #endif
1.177 brouard 9017: struct utsname sysInfo;
1.178 brouard 9018: int cross = CROSS;
9019: if (cross){
9020: printf("Cross-");
1.191 brouard 9021: if(logged) fprintf(ficlog, "Cross-");
1.178 brouard 9022: }
1.174 brouard 9023: #endif
9024:
1.171 brouard 9025: #include <stdint.h>
1.178 brouard 9026:
1.191 brouard 9027: printf("Compiled with:");if(logged)fprintf(ficlog,"Compiled with:");
1.169 brouard 9028: #if defined(__clang__)
1.191 brouard 9029: printf(" Clang/LLVM");if(logged)fprintf(ficlog," Clang/LLVM"); /* Clang/LLVM. ---------------------------------------------- */
1.169 brouard 9030: #endif
9031: #if defined(__ICC) || defined(__INTEL_COMPILER)
1.191 brouard 9032: printf(" Intel ICC/ICPC");if(logged)fprintf(ficlog," Intel ICC/ICPC");/* Intel ICC/ICPC. ------------------------------------------ */
1.169 brouard 9033: #endif
9034: #if defined(__GNUC__) || defined(__GNUG__)
1.191 brouard 9035: printf(" GNU GCC/G++");if(logged)fprintf(ficlog," GNU GCC/G++");/* GNU GCC/G++. --------------------------------------------- */
1.169 brouard 9036: #endif
9037: #if defined(__HP_cc) || defined(__HP_aCC)
1.191 brouard 9038: printf(" Hewlett-Packard C/aC++");if(logged)fprintf(fcilog," Hewlett-Packard C/aC++"); /* Hewlett-Packard C/aC++. ---------------------------------- */
1.169 brouard 9039: #endif
9040: #if defined(__IBMC__) || defined(__IBMCPP__)
1.191 brouard 9041: printf(" IBM XL C/C++"); if(logged) fprintf(ficlog," IBM XL C/C++");/* IBM XL C/C++. -------------------------------------------- */
1.169 brouard 9042: #endif
9043: #if defined(_MSC_VER)
1.191 brouard 9044: printf(" Microsoft Visual Studio");if(logged)fprintf(ficlog," Microsoft Visual Studio");/* Microsoft Visual Studio. --------------------------------- */
1.169 brouard 9045: #endif
9046: #if defined(__PGI)
1.191 brouard 9047: printf(" Portland Group PGCC/PGCPP");if(logged) fprintf(ficlog," Portland Group PGCC/PGCPP");/* Portland Group PGCC/PGCPP. ------------------------------- */
1.169 brouard 9048: #endif
9049: #if defined(__SUNPRO_C) || defined(__SUNPRO_CC)
1.191 brouard 9050: printf(" Oracle Solaris Studio");if(logged)fprintf(ficlog," Oracle Solaris Studio\n");/* Oracle Solaris Studio. ----------------------------------- */
1.167 brouard 9051: #endif
1.191 brouard 9052: printf(" for "); if (logged) fprintf(ficlog, " for ");
1.169 brouard 9053:
1.167 brouard 9054: // http://stackoverflow.com/questions/4605842/how-to-identify-platform-compiler-from-preprocessor-macros
9055: #ifdef _WIN32 // note the underscore: without it, it's not msdn official!
9056: // Windows (x64 and x86)
1.191 brouard 9057: printf("Windows (x64 and x86) ");if(logged) fprintf(ficlog,"Windows (x64 and x86) ");
1.167 brouard 9058: #elif __unix__ // all unices, not all compilers
9059: // Unix
1.191 brouard 9060: printf("Unix ");if(logged) fprintf(ficlog,"Unix ");
1.167 brouard 9061: #elif __linux__
9062: // linux
1.191 brouard 9063: printf("linux ");if(logged) fprintf(ficlog,"linux ");
1.167 brouard 9064: #elif __APPLE__
1.174 brouard 9065: // Mac OS, not sure if this is covered by __posix__ and/or __unix__ though..
1.191 brouard 9066: printf("Mac OS ");if(logged) fprintf(ficlog,"Mac OS ");
1.167 brouard 9067: #endif
9068:
9069: /* __MINGW32__ */
9070: /* __CYGWIN__ */
9071: /* __MINGW64__ */
9072: // http://msdn.microsoft.com/en-us/library/b0084kay.aspx
9073: /* _MSC_VER //the Visual C++ compiler is 17.00.51106.1, the _MSC_VER macro evaluates to 1700. Type cl /? */
9074: /* _MSC_FULL_VER //the Visual C++ compiler is 15.00.20706.01, the _MSC_FULL_VER macro evaluates to 150020706 */
9075: /* _WIN64 // Defined for applications for Win64. */
9076: /* _M_X64 // Defined for compilations that target x64 processors. */
9077: /* _DEBUG // Defined when you compile with /LDd, /MDd, and /MTd. */
1.171 brouard 9078:
1.167 brouard 9079: #if UINTPTR_MAX == 0xffffffff
1.191 brouard 9080: printf(" 32-bit"); if(logged) fprintf(ficlog," 32-bit");/* 32-bit */
1.167 brouard 9081: #elif UINTPTR_MAX == 0xffffffffffffffff
1.191 brouard 9082: printf(" 64-bit"); if(logged) fprintf(ficlog," 64-bit");/* 64-bit */
1.167 brouard 9083: #else
1.191 brouard 9084: printf(" wtf-bit"); if(logged) fprintf(ficlog," wtf-bit");/* wtf */
1.167 brouard 9085: #endif
9086:
1.169 brouard 9087: #if defined(__GNUC__)
9088: # if defined(__GNUC_PATCHLEVEL__)
9089: # define __GNUC_VERSION__ (__GNUC__ * 10000 \
9090: + __GNUC_MINOR__ * 100 \
9091: + __GNUC_PATCHLEVEL__)
9092: # else
9093: # define __GNUC_VERSION__ (__GNUC__ * 10000 \
9094: + __GNUC_MINOR__ * 100)
9095: # endif
1.174 brouard 9096: printf(" using GNU C version %d.\n", __GNUC_VERSION__);
1.191 brouard 9097: if(logged) fprintf(ficlog, " using GNU C version %d.\n", __GNUC_VERSION__);
1.176 brouard 9098:
9099: if (uname(&sysInfo) != -1) {
9100: printf("Running on: %s %s %s %s %s\n",sysInfo.sysname, sysInfo.nodename, sysInfo.release, sysInfo.version, sysInfo.machine);
1.191 brouard 9101: 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 9102: }
9103: else
9104: perror("uname() error");
1.179 brouard 9105: //#ifndef __INTEL_COMPILER
9106: #if !defined (__INTEL_COMPILER) && !defined(__APPLE__)
1.174 brouard 9107: printf("GNU libc version: %s\n", gnu_get_libc_version());
1.191 brouard 9108: if(logged) fprintf(ficlog,"GNU libc version: %s\n", gnu_get_libc_version());
1.177 brouard 9109: #endif
1.169 brouard 9110: #endif
1.172 brouard 9111:
9112: // void main()
9113: // {
1.169 brouard 9114: #if defined(_MSC_VER)
1.174 brouard 9115: if (IsWow64()){
1.191 brouard 9116: printf("\nThe program (probably compiled for 32bit) is running under WOW64 (64bit) emulation.\n");
9117: if (logged) fprintf(ficlog, "\nThe program (probably compiled for 32bit) is running under WOW64 (64bit) emulation.\n");
1.174 brouard 9118: }
9119: else{
1.191 brouard 9120: printf("\nThe program is not running under WOW64 (i.e probably on a 64bit Windows).\n");
9121: if (logged) fprintf(ficlog, "\nThe programm is not running under WOW64 (i.e probably on a 64bit Windows).\n");
1.174 brouard 9122: }
1.172 brouard 9123: // printf("\nPress Enter to continue...");
9124: // getchar();
9125: // }
9126:
1.169 brouard 9127: #endif
9128:
1.167 brouard 9129:
1.219 brouard 9130: }
1.136 brouard 9131:
1.219 brouard 9132: int prevalence_limit(double *p, double **prlim, double ageminpar, double agemaxpar, double ftolpl, int *ncvyearp){
1.180 brouard 9133: /*--------------- Prevalence limit (period or stable prevalence) --------------*/
1.235 brouard 9134: int i, j, k, i1, k4=0, nres=0 ;
1.202 brouard 9135: /* double ftolpl = 1.e-10; */
1.180 brouard 9136: double age, agebase, agelim;
1.203 brouard 9137: double tot;
1.180 brouard 9138:
1.202 brouard 9139: strcpy(filerespl,"PL_");
9140: strcat(filerespl,fileresu);
9141: if((ficrespl=fopen(filerespl,"w"))==NULL) {
9142: printf("Problem with period (stable) prevalence resultfile: %s\n", filerespl);return 1;
9143: fprintf(ficlog,"Problem with period (stable) prevalence resultfile: %s\n", filerespl);return 1;
9144: }
1.227 brouard 9145: printf("\nComputing period (stable) prevalence: result on file '%s' \n", filerespl);
9146: fprintf(ficlog,"\nComputing period (stable) prevalence: result on file '%s' \n", filerespl);
1.202 brouard 9147: pstamp(ficrespl);
1.203 brouard 9148: fprintf(ficrespl,"# Period (stable) prevalence. Precision given by ftolpl=%g \n", ftolpl);
1.202 brouard 9149: fprintf(ficrespl,"#Age ");
9150: for(i=1; i<=nlstate;i++) fprintf(ficrespl,"%d-%d ",i,i);
9151: fprintf(ficrespl,"\n");
1.180 brouard 9152:
1.219 brouard 9153: /* prlim=matrix(1,nlstate,1,nlstate);*/ /* back in main */
1.180 brouard 9154:
1.219 brouard 9155: agebase=ageminpar;
9156: agelim=agemaxpar;
1.180 brouard 9157:
1.227 brouard 9158: /* i1=pow(2,ncoveff); */
1.234 brouard 9159: i1=pow(2,cptcoveff); /* Number of combination of dummy covariates */
1.219 brouard 9160: if (cptcovn < 1){i1=1;}
1.180 brouard 9161:
1.238 brouard 9162: for(k=1; k<=i1;k++){ /* For each combination k of dummy covariates in the model */
9163: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
9164: if(TKresult[nres]!= k)
9165: continue;
1.235 brouard 9166:
1.238 brouard 9167: /* for(cptcov=1,k=0;cptcov<=i1;cptcov++){ */
9168: /* for(cptcov=1,k=0;cptcov<=1;cptcov++){ */
9169: //for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){
9170: /* k=k+1; */
9171: /* to clean */
9172: //printf("cptcov=%d cptcod=%d codtab=%d\n",cptcov, cptcod,codtabm(cptcod,cptcov));
9173: fprintf(ficrespl,"#******");
9174: printf("#******");
9175: fprintf(ficlog,"#******");
9176: for(j=1;j<=cptcoveff ;j++) {/* all covariates */
9177: fprintf(ficrespl," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]); /* Here problem for varying dummy*/
9178: printf(" V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
9179: fprintf(ficlog," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
9180: }
9181: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
9182: printf(" V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
9183: fprintf(ficrespl," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
9184: fprintf(ficlog," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
9185: }
9186: fprintf(ficrespl,"******\n");
9187: printf("******\n");
9188: fprintf(ficlog,"******\n");
9189: if(invalidvarcomb[k]){
9190: printf("\nCombination (%d) ignored because no case \n",k);
9191: fprintf(ficrespl,"#Combination (%d) ignored because no case \n",k);
9192: fprintf(ficlog,"\nCombination (%d) ignored because no case \n",k);
9193: continue;
9194: }
1.219 brouard 9195:
1.238 brouard 9196: fprintf(ficrespl,"#Age ");
9197: for(j=1;j<=cptcoveff;j++) {
9198: fprintf(ficrespl,"V%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
9199: }
9200: for(i=1; i<=nlstate;i++) fprintf(ficrespl," %d-%d ",i,i);
9201: fprintf(ficrespl,"Total Years_to_converge\n");
1.227 brouard 9202:
1.238 brouard 9203: for (age=agebase; age<=agelim; age++){
9204: /* for (age=agebase; age<=agebase; age++){ */
9205: prevalim(prlim, nlstate, p, age, oldm, savm, ftolpl, ncvyearp, k, nres);
9206: fprintf(ficrespl,"%.0f ",age );
9207: for(j=1;j<=cptcoveff;j++)
9208: fprintf(ficrespl,"%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
9209: tot=0.;
9210: for(i=1; i<=nlstate;i++){
9211: tot += prlim[i][i];
9212: fprintf(ficrespl," %.5f", prlim[i][i]);
9213: }
9214: fprintf(ficrespl," %.3f %d\n", tot, *ncvyearp);
9215: } /* Age */
9216: /* was end of cptcod */
9217: } /* cptcov */
9218: } /* nres */
1.219 brouard 9219: return 0;
1.180 brouard 9220: }
9221:
1.218 brouard 9222: 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){
9223: /*--------------- Back Prevalence limit (period or stable prevalence) --------------*/
9224:
9225: /* Computes the back prevalence limit for any combination of covariate values
9226: * at any age between ageminpar and agemaxpar
9227: */
1.235 brouard 9228: int i, j, k, i1, nres=0 ;
1.217 brouard 9229: /* double ftolpl = 1.e-10; */
9230: double age, agebase, agelim;
9231: double tot;
1.218 brouard 9232: /* double ***mobaverage; */
9233: /* double **dnewm, **doldm, **dsavm; /\* for use *\/ */
1.217 brouard 9234:
9235: strcpy(fileresplb,"PLB_");
9236: strcat(fileresplb,fileresu);
9237: if((ficresplb=fopen(fileresplb,"w"))==NULL) {
9238: printf("Problem with period (stable) back prevalence resultfile: %s\n", fileresplb);return 1;
9239: fprintf(ficlog,"Problem with period (stable) back prevalence resultfile: %s\n", fileresplb);return 1;
9240: }
9241: printf("Computing period (stable) back prevalence: result on file '%s' \n", fileresplb);
9242: fprintf(ficlog,"Computing period (stable) back prevalence: result on file '%s' \n", fileresplb);
9243: pstamp(ficresplb);
9244: fprintf(ficresplb,"# Period (stable) back prevalence. Precision given by ftolpl=%g \n", ftolpl);
9245: fprintf(ficresplb,"#Age ");
9246: for(i=1; i<=nlstate;i++) fprintf(ficresplb,"%d-%d ",i,i);
9247: fprintf(ficresplb,"\n");
9248:
1.218 brouard 9249:
9250: /* prlim=matrix(1,nlstate,1,nlstate);*/ /* back in main */
9251:
9252: agebase=ageminpar;
9253: agelim=agemaxpar;
9254:
9255:
1.227 brouard 9256: i1=pow(2,cptcoveff);
1.218 brouard 9257: if (cptcovn < 1){i1=1;}
1.227 brouard 9258:
1.238 brouard 9259: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
9260: for(k=1; k<=i1;k++){ /* For any combination of dummy covariates, fixed and varying */
9261: if(TKresult[nres]!= k)
9262: continue;
9263: //printf("cptcov=%d cptcod=%d codtab=%d\n",cptcov, cptcod,codtabm(cptcod,cptcov));
9264: fprintf(ficresplb,"#******");
9265: printf("#******");
9266: fprintf(ficlog,"#******");
9267: for(j=1;j<=cptcoveff ;j++) {/* all covariates */
9268: fprintf(ficresplb," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
9269: printf(" V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
9270: fprintf(ficlog," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
9271: }
9272: for (j=1; j<= nsq; j++){ /* For each selected (single) quantitative value */
9273: printf(" V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
9274: fprintf(ficresplb," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
9275: fprintf(ficlog," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
9276: }
9277: fprintf(ficresplb,"******\n");
9278: printf("******\n");
9279: fprintf(ficlog,"******\n");
9280: if(invalidvarcomb[k]){
9281: printf("\nCombination (%d) ignored because no cases \n",k);
9282: fprintf(ficresplb,"#Combination (%d) ignored because no cases \n",k);
9283: fprintf(ficlog,"\nCombination (%d) ignored because no cases \n",k);
9284: continue;
9285: }
1.218 brouard 9286:
1.238 brouard 9287: fprintf(ficresplb,"#Age ");
9288: for(j=1;j<=cptcoveff;j++) {
9289: fprintf(ficresplb,"V%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
9290: }
9291: for(i=1; i<=nlstate;i++) fprintf(ficresplb," %d-%d ",i,i);
9292: fprintf(ficresplb,"Total Years_to_converge\n");
1.218 brouard 9293:
9294:
1.238 brouard 9295: for (age=agebase; age<=agelim; age++){
9296: /* for (age=agebase; age<=agebase; age++){ */
9297: if(mobilavproj > 0){
9298: /* bprevalim(bprlim, mobaverage, nlstate, p, age, ageminpar, agemaxpar, oldm, savm, doldm, dsavm, ftolpl, ncvyearp, k); */
9299: /* bprevalim(bprlim, mobaverage, nlstate, p, age, oldm, savm, dnewm, doldm, dsavm, ftolpl, ncvyearp, k); */
1.242 brouard 9300: bprevalim(bprlim, mobaverage, nlstate, p, age, ftolpl, ncvyearp, k, nres);
1.238 brouard 9301: }else if (mobilavproj == 0){
9302: 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);
9303: 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);
9304: exit(1);
9305: }else{
9306: /* bprevalim(bprlim, probs, nlstate, p, age, oldm, savm, dnewm, doldm, dsavm, ftolpl, ncvyearp, k); */
1.242 brouard 9307: bprevalim(bprlim, probs, nlstate, p, age, ftolpl, ncvyearp, k,nres);
1.238 brouard 9308: }
9309: fprintf(ficresplb,"%.0f ",age );
9310: for(j=1;j<=cptcoveff;j++)
9311: fprintf(ficresplb,"%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
9312: tot=0.;
9313: for(i=1; i<=nlstate;i++){
9314: tot += bprlim[i][i];
9315: fprintf(ficresplb," %.5f", bprlim[i][i]);
9316: }
9317: fprintf(ficresplb," %.3f %d\n", tot, *ncvyearp);
9318: } /* Age */
9319: /* was end of cptcod */
9320: } /* end of any combination */
9321: } /* end of nres */
1.218 brouard 9322: /* hBijx(p, bage, fage); */
9323: /* fclose(ficrespijb); */
9324:
9325: return 0;
1.217 brouard 9326: }
1.218 brouard 9327:
1.180 brouard 9328: int hPijx(double *p, int bage, int fage){
9329: /*------------- h Pij x at various ages ------------*/
9330:
9331: int stepsize;
9332: int agelim;
9333: int hstepm;
9334: int nhstepm;
1.235 brouard 9335: int h, i, i1, j, k, k4, nres=0;
1.180 brouard 9336:
9337: double agedeb;
9338: double ***p3mat;
9339:
1.201 brouard 9340: strcpy(filerespij,"PIJ_"); strcat(filerespij,fileresu);
1.180 brouard 9341: if((ficrespij=fopen(filerespij,"w"))==NULL) {
9342: printf("Problem with Pij resultfile: %s\n", filerespij); return 1;
9343: fprintf(ficlog,"Problem with Pij resultfile: %s\n", filerespij); return 1;
9344: }
9345: printf("Computing pij: result on file '%s' \n", filerespij);
9346: fprintf(ficlog,"Computing pij: result on file '%s' \n", filerespij);
9347:
9348: stepsize=(int) (stepm+YEARM-1)/YEARM;
9349: /*if (stepm<=24) stepsize=2;*/
9350:
9351: agelim=AGESUP;
9352: hstepm=stepsize*YEARM; /* Every year of age */
9353: hstepm=hstepm/stepm; /* Typically 2 years, = 2/6 months = 4 */
1.218 brouard 9354:
1.180 brouard 9355: /* hstepm=1; aff par mois*/
9356: pstamp(ficrespij);
9357: fprintf(ficrespij,"#****** h Pij x Probability to be in state j at age x+h being in i at x ");
1.227 brouard 9358: i1= pow(2,cptcoveff);
1.218 brouard 9359: /* for(cptcov=1,k=0;cptcov<=i1;cptcov++){ */
9360: /* /\*for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){*\/ */
9361: /* k=k+1; */
1.235 brouard 9362: for(nres=1; nres <= nresult; nres++) /* For each resultline */
9363: for(k=1; k<=i1;k++){
9364: if(TKresult[nres]!= k)
9365: continue;
1.183 brouard 9366: fprintf(ficrespij,"\n#****** ");
1.227 brouard 9367: for(j=1;j<=cptcoveff;j++)
1.198 brouard 9368: fprintf(ficrespij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
1.235 brouard 9369: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
9370: printf(" V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
9371: fprintf(ficrespij," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
9372: }
1.183 brouard 9373: fprintf(ficrespij,"******\n");
9374:
9375: for (agedeb=fage; agedeb>=bage; agedeb--){ /* If stepm=6 months */
9376: nhstepm=(int) rint((agelim-agedeb)*YEARM/stepm); /* Typically 20 years = 20*12/6=40 */
9377: nhstepm = nhstepm/hstepm; /* Typically 40/4=10 */
9378:
9379: /* nhstepm=nhstepm*YEARM; aff par mois*/
1.180 brouard 9380:
1.183 brouard 9381: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
9382: oldm=oldms;savm=savms;
1.235 brouard 9383: hpxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k, nres);
1.183 brouard 9384: fprintf(ficrespij,"# Cov Agex agex+h hpijx with i,j=");
9385: for(i=1; i<=nlstate;i++)
9386: for(j=1; j<=nlstate+ndeath;j++)
9387: fprintf(ficrespij," %1d-%1d",i,j);
9388: fprintf(ficrespij,"\n");
9389: for (h=0; h<=nhstepm; h++){
9390: /*agedebphstep = agedeb + h*hstepm/YEARM*stepm;*/
9391: fprintf(ficrespij,"%d %3.f %3.f",k, agedeb, agedeb + h*hstepm/YEARM*stepm );
1.180 brouard 9392: for(i=1; i<=nlstate;i++)
9393: for(j=1; j<=nlstate+ndeath;j++)
1.183 brouard 9394: fprintf(ficrespij," %.5f", p3mat[i][j][h]);
1.180 brouard 9395: fprintf(ficrespij,"\n");
9396: }
1.183 brouard 9397: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
9398: fprintf(ficrespij,"\n");
9399: }
1.180 brouard 9400: /*}*/
9401: }
1.218 brouard 9402: return 0;
1.180 brouard 9403: }
1.218 brouard 9404:
9405: int hBijx(double *p, int bage, int fage, double ***prevacurrent){
1.217 brouard 9406: /*------------- h Bij x at various ages ------------*/
9407:
9408: int stepsize;
1.218 brouard 9409: /* int agelim; */
9410: int ageminl;
1.217 brouard 9411: int hstepm;
9412: int nhstepm;
1.238 brouard 9413: int h, i, i1, j, k, nres;
1.218 brouard 9414:
1.217 brouard 9415: double agedeb;
9416: double ***p3mat;
1.218 brouard 9417:
9418: strcpy(filerespijb,"PIJB_"); strcat(filerespijb,fileresu);
9419: if((ficrespijb=fopen(filerespijb,"w"))==NULL) {
9420: printf("Problem with Pij back resultfile: %s\n", filerespijb); return 1;
9421: fprintf(ficlog,"Problem with Pij back resultfile: %s\n", filerespijb); return 1;
9422: }
9423: printf("Computing pij back: result on file '%s' \n", filerespijb);
9424: fprintf(ficlog,"Computing pij back: result on file '%s' \n", filerespijb);
9425:
9426: stepsize=(int) (stepm+YEARM-1)/YEARM;
9427: /*if (stepm<=24) stepsize=2;*/
1.217 brouard 9428:
1.218 brouard 9429: /* agelim=AGESUP; */
9430: ageminl=30;
9431: hstepm=stepsize*YEARM; /* Every year of age */
9432: hstepm=hstepm/stepm; /* Typically 2 years, = 2/6 months = 4 */
9433:
9434: /* hstepm=1; aff par mois*/
9435: pstamp(ficrespijb);
9436: fprintf(ficrespijb,"#****** h Pij x Back Probability to be in state i at age x-h being in j at x ");
1.227 brouard 9437: i1= pow(2,cptcoveff);
1.218 brouard 9438: /* for(cptcov=1,k=0;cptcov<=i1;cptcov++){ */
9439: /* /\*for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){*\/ */
9440: /* k=k+1; */
1.238 brouard 9441: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
9442: for(k=1; k<=i1;k++){ /* For any combination of dummy covariates, fixed and varying */
9443: if(TKresult[nres]!= k)
9444: continue;
9445: fprintf(ficrespijb,"\n#****** ");
9446: for(j=1;j<=cptcoveff;j++)
9447: fprintf(ficrespijb,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
9448: for (j=1; j<= nsq; j++){ /* For each selected (single) quantitative value */
9449: fprintf(ficrespijb," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
9450: }
9451: fprintf(ficrespijb,"******\n");
9452: if(invalidvarcomb[k]){
9453: fprintf(ficrespijb,"\n#Combination (%d) ignored because no cases \n",k);
9454: continue;
9455: }
9456:
9457: /* for (agedeb=fage; agedeb>=bage; agedeb--){ /\* If stepm=6 months *\/ */
9458: for (agedeb=bage; agedeb<=fage; agedeb++){ /* If stepm=6 months and estepm=24 (2 years) */
9459: /* nhstepm=(int) rint((agelim-agedeb)*YEARM/stepm); /\* Typically 20 years = 20*12/6=40 *\/ */
9460: nhstepm=(int) rint((agedeb-ageminl)*YEARM/stepm); /* Typically 20 years = 20*12/6=40 */
9461: nhstepm = nhstepm/hstepm; /* Typically 40/4=10, because estepm=24 stepm=6 => hstepm=24/6=4 */
9462:
9463: /* nhstepm=nhstepm*YEARM; aff par mois*/
9464:
9465: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
9466: /* oldm=oldms;savm=savms; */
9467: /* hbxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k); */
9468: hbxij(p3mat,nhstepm,agedeb,hstepm,p,prevacurrent,nlstate,stepm, k);
9469: /* hbxij(p3mat,nhstepm,agedeb,hstepm,p,prevacurrent,nlstate,stepm,oldm,savm, dnewm, doldm, dsavm, k); */
9470: fprintf(ficrespijb,"# Cov Agex agex-h hpijx with i,j=");
1.217 brouard 9471: for(i=1; i<=nlstate;i++)
9472: for(j=1; j<=nlstate+ndeath;j++)
1.238 brouard 9473: fprintf(ficrespijb," %1d-%1d",i,j);
1.217 brouard 9474: fprintf(ficrespijb,"\n");
1.238 brouard 9475: for (h=0; h<=nhstepm; h++){
9476: /*agedebphstep = agedeb + h*hstepm/YEARM*stepm;*/
9477: fprintf(ficrespijb,"%d %3.f %3.f",k, agedeb, agedeb - h*hstepm/YEARM*stepm );
9478: /* fprintf(ficrespijb,"%d %3.f %3.f",k, agedeb, agedeb + h*hstepm/YEARM*stepm ); */
9479: for(i=1; i<=nlstate;i++)
9480: for(j=1; j<=nlstate+ndeath;j++)
9481: fprintf(ficrespijb," %.5f", p3mat[i][j][h]);
9482: fprintf(ficrespijb,"\n");
9483: }
9484: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
9485: fprintf(ficrespijb,"\n");
9486: } /* end age deb */
9487: } /* end combination */
9488: } /* end nres */
1.218 brouard 9489: return 0;
9490: } /* hBijx */
1.217 brouard 9491:
1.180 brouard 9492:
1.136 brouard 9493: /***********************************************/
9494: /**************** Main Program *****************/
9495: /***********************************************/
9496:
9497: int main(int argc, char *argv[])
9498: {
9499: #ifdef GSL
9500: const gsl_multimin_fminimizer_type *T;
9501: size_t iteri = 0, it;
9502: int rval = GSL_CONTINUE;
9503: int status = GSL_SUCCESS;
9504: double ssval;
9505: #endif
9506: int movingaverage(double ***probs, double bage,double fage, double ***mobaverage, int mobilav);
1.164 brouard 9507: int i,j, k, n=MAXN,iter=0,m,size=100, cptcod;
1.209 brouard 9508: int ncvyear=0; /* Number of years needed for the period prevalence to converge */
1.164 brouard 9509: int jj, ll, li, lj, lk;
1.136 brouard 9510: int numlinepar=0; /* Current linenumber of parameter file */
1.197 brouard 9511: int num_filled;
1.136 brouard 9512: int itimes;
9513: int NDIM=2;
9514: int vpopbased=0;
1.235 brouard 9515: int nres=0;
1.136 brouard 9516:
1.164 brouard 9517: char ca[32], cb[32];
1.136 brouard 9518: /* FILE *fichtm; *//* Html File */
9519: /* FILE *ficgp;*/ /*Gnuplot File */
9520: struct stat info;
1.191 brouard 9521: double agedeb=0.;
1.194 brouard 9522:
9523: double ageminpar=AGEOVERFLOW,agemin=AGEOVERFLOW, agemaxpar=-AGEOVERFLOW, agemax=-AGEOVERFLOW;
1.219 brouard 9524: double ageminout=-AGEOVERFLOW,agemaxout=AGEOVERFLOW; /* Smaller Age range redefined after movingaverage */
1.136 brouard 9525:
1.165 brouard 9526: double fret;
1.191 brouard 9527: double dum=0.; /* Dummy variable */
1.136 brouard 9528: double ***p3mat;
1.218 brouard 9529: /* double ***mobaverage; */
1.164 brouard 9530:
9531: char line[MAXLINE];
1.197 brouard 9532: char path[MAXLINE],pathc[MAXLINE],pathcd[MAXLINE],pathtot[MAXLINE];
9533:
1.234 brouard 9534: char modeltemp[MAXLINE];
1.230 brouard 9535: char resultline[MAXLINE];
9536:
1.136 brouard 9537: char pathr[MAXLINE], pathimach[MAXLINE];
1.164 brouard 9538: char *tok, *val; /* pathtot */
1.136 brouard 9539: int firstobs=1, lastobs=10;
1.195 brouard 9540: int c, h , cpt, c2;
1.191 brouard 9541: int jl=0;
9542: int i1, j1, jk, stepsize=0;
1.194 brouard 9543: int count=0;
9544:
1.164 brouard 9545: int *tab;
1.136 brouard 9546: int mobilavproj=0 , prevfcast=0 ; /* moving average of prev, If prevfcast=1 prevalence projection */
1.217 brouard 9547: int backcast=0;
1.136 brouard 9548: int mobilav=0,popforecast=0;
1.191 brouard 9549: int hstepm=0, nhstepm=0;
1.136 brouard 9550: int agemortsup;
9551: float sumlpop=0.;
9552: double jprev1=1, mprev1=1,anprev1=2000,jprev2=1, mprev2=1,anprev2=2000;
9553: double jpyram=1, mpyram=1,anpyram=2000,jpyram1=1, mpyram1=1,anpyram1=2000;
9554:
1.191 brouard 9555: double bage=0, fage=110., age, agelim=0., agebase=0.;
1.136 brouard 9556: double ftolpl=FTOL;
9557: double **prlim;
1.217 brouard 9558: double **bprlim;
1.136 brouard 9559: double ***param; /* Matrix of parameters */
9560: double *p;
9561: double **matcov; /* Matrix of covariance */
1.203 brouard 9562: double **hess; /* Hessian matrix */
1.136 brouard 9563: double ***delti3; /* Scale */
9564: double *delti; /* Scale */
9565: double ***eij, ***vareij;
9566: double **varpl; /* Variances of prevalence limits by age */
9567: double *epj, vepp;
1.164 brouard 9568:
1.136 brouard 9569: double dateprev1, dateprev2,jproj1=1,mproj1=1,anproj1=2000,jproj2=1,mproj2=1,anproj2=2000;
1.217 brouard 9570: double jback1=1,mback1=1,anback1=2000,jback2=1,mback2=1,anback2=2000;
9571:
1.136 brouard 9572: double **ximort;
1.145 brouard 9573: char *alph[]={"a","a","b","c","d","e"}, str[4]="1234";
1.136 brouard 9574: int *dcwave;
9575:
1.164 brouard 9576: char z[1]="c";
1.136 brouard 9577:
9578: /*char *strt;*/
9579: char strtend[80];
1.126 brouard 9580:
1.164 brouard 9581:
1.126 brouard 9582: /* setlocale (LC_ALL, ""); */
9583: /* bindtextdomain (PACKAGE, LOCALEDIR); */
9584: /* textdomain (PACKAGE); */
9585: /* setlocale (LC_CTYPE, ""); */
9586: /* setlocale (LC_MESSAGES, ""); */
9587:
9588: /* gettimeofday(&start_time, (struct timezone*)0); */ /* at first time */
1.157 brouard 9589: rstart_time = time(NULL);
9590: /* (void) gettimeofday(&start_time,&tzp);*/
9591: start_time = *localtime(&rstart_time);
1.126 brouard 9592: curr_time=start_time;
1.157 brouard 9593: /*tml = *localtime(&start_time.tm_sec);*/
9594: /* strcpy(strstart,asctime(&tml)); */
9595: strcpy(strstart,asctime(&start_time));
1.126 brouard 9596:
9597: /* printf("Localtime (at start)=%s",strstart); */
1.157 brouard 9598: /* tp.tm_sec = tp.tm_sec +86400; */
9599: /* tm = *localtime(&start_time.tm_sec); */
1.126 brouard 9600: /* tmg.tm_year=tmg.tm_year +dsign*dyear; */
9601: /* tmg.tm_mon=tmg.tm_mon +dsign*dmonth; */
9602: /* tmg.tm_hour=tmg.tm_hour + 1; */
1.157 brouard 9603: /* tp.tm_sec = mktime(&tmg); */
1.126 brouard 9604: /* strt=asctime(&tmg); */
9605: /* printf("Time(after) =%s",strstart); */
9606: /* (void) time (&time_value);
9607: * printf("time=%d,t-=%d\n",time_value,time_value-86400);
9608: * tm = *localtime(&time_value);
9609: * strstart=asctime(&tm);
9610: * printf("tim_value=%d,asctime=%s\n",time_value,strstart);
9611: */
9612:
9613: nberr=0; /* Number of errors and warnings */
9614: nbwarn=0;
1.184 brouard 9615: #ifdef WIN32
9616: _getcwd(pathcd, size);
9617: #else
1.126 brouard 9618: getcwd(pathcd, size);
1.184 brouard 9619: #endif
1.191 brouard 9620: syscompilerinfo(0);
1.196 brouard 9621: printf("\nIMaCh version %s, %s\n%s",version, copyright, fullversion);
1.126 brouard 9622: if(argc <=1){
9623: printf("\nEnter the parameter file name: ");
1.205 brouard 9624: if(!fgets(pathr,FILENAMELENGTH,stdin)){
9625: printf("ERROR Empty parameter file name\n");
9626: goto end;
9627: }
1.126 brouard 9628: i=strlen(pathr);
9629: if(pathr[i-1]=='\n')
9630: pathr[i-1]='\0';
1.156 brouard 9631: i=strlen(pathr);
1.205 brouard 9632: if(i >= 1 && pathr[i-1]==' ') {/* This may happen when dragging on oS/X! */
1.156 brouard 9633: pathr[i-1]='\0';
1.205 brouard 9634: }
9635: i=strlen(pathr);
9636: if( i==0 ){
9637: printf("ERROR Empty parameter file name\n");
9638: goto end;
9639: }
9640: for (tok = pathr; tok != NULL; ){
1.126 brouard 9641: printf("Pathr |%s|\n",pathr);
9642: while ((val = strsep(&tok, "\"" )) != NULL && *val == '\0');
9643: printf("val= |%s| pathr=%s\n",val,pathr);
9644: strcpy (pathtot, val);
9645: if(pathr[0] == '\0') break; /* Dirty */
9646: }
9647: }
9648: else{
9649: strcpy(pathtot,argv[1]);
9650: }
9651: /*if(getcwd(pathcd, MAXLINE)!= NULL)printf ("Error pathcd\n");*/
9652: /*cygwin_split_path(pathtot,path,optionfile);
9653: printf("pathtot=%s, path=%s, optionfile=%s\n",pathtot,path,optionfile);*/
9654: /* cutv(path,optionfile,pathtot,'\\');*/
9655:
9656: /* Split argv[0], imach program to get pathimach */
9657: printf("\nargv[0]=%s argv[1]=%s, \n",argv[0],argv[1]);
9658: split(argv[0],pathimach,optionfile,optionfilext,optionfilefiname);
9659: printf("\nargv[0]=%s pathimach=%s, \noptionfile=%s \noptionfilext=%s \noptionfilefiname=%s\n",argv[0],pathimach,optionfile,optionfilext,optionfilefiname);
9660: /* strcpy(pathimach,argv[0]); */
9661: /* Split argv[1]=pathtot, parameter file name to get path, optionfile, extension and name */
9662: split(pathtot,path,optionfile,optionfilext,optionfilefiname);
9663: printf("\npathtot=%s,\npath=%s,\noptionfile=%s \noptionfilext=%s \noptionfilefiname=%s\n",pathtot,path,optionfile,optionfilext,optionfilefiname);
1.184 brouard 9664: #ifdef WIN32
9665: _chdir(path); /* Can be a relative path */
9666: if(_getcwd(pathcd,MAXLINE) > 0) /* So pathcd is the full path */
9667: #else
1.126 brouard 9668: chdir(path); /* Can be a relative path */
1.184 brouard 9669: if (getcwd(pathcd, MAXLINE) > 0) /* So pathcd is the full path */
9670: #endif
9671: printf("Current directory %s!\n",pathcd);
1.126 brouard 9672: strcpy(command,"mkdir ");
9673: strcat(command,optionfilefiname);
9674: if((outcmd=system(command)) != 0){
1.169 brouard 9675: printf("Directory already exists (or can't create it) %s%s, err=%d\n",path,optionfilefiname,outcmd);
1.126 brouard 9676: /* fprintf(ficlog,"Problem creating directory %s%s\n",path,optionfilefiname); */
9677: /* fclose(ficlog); */
9678: /* exit(1); */
9679: }
9680: /* if((imk=mkdir(optionfilefiname))<0){ */
9681: /* perror("mkdir"); */
9682: /* } */
9683:
9684: /*-------- arguments in the command line --------*/
9685:
1.186 brouard 9686: /* Main Log file */
1.126 brouard 9687: strcat(filelog, optionfilefiname);
9688: strcat(filelog,".log"); /* */
9689: if((ficlog=fopen(filelog,"w"))==NULL) {
9690: printf("Problem with logfile %s\n",filelog);
9691: goto end;
9692: }
9693: fprintf(ficlog,"Log filename:%s\n",filelog);
1.197 brouard 9694: fprintf(ficlog,"Version %s %s",version,fullversion);
1.126 brouard 9695: fprintf(ficlog,"\nEnter the parameter file name: \n");
9696: fprintf(ficlog,"pathimach=%s\npathtot=%s\n\
9697: path=%s \n\
9698: optionfile=%s\n\
9699: optionfilext=%s\n\
1.156 brouard 9700: optionfilefiname='%s'\n",pathimach,pathtot,path,optionfile,optionfilext,optionfilefiname);
1.126 brouard 9701:
1.197 brouard 9702: syscompilerinfo(1);
1.167 brouard 9703:
1.126 brouard 9704: printf("Local time (at start):%s",strstart);
9705: fprintf(ficlog,"Local time (at start): %s",strstart);
9706: fflush(ficlog);
9707: /* (void) gettimeofday(&curr_time,&tzp); */
1.157 brouard 9708: /* printf("Elapsed time %d\n", asc_diff_time(curr_time.tm_sec-start_time.tm_sec,tmpout)); */
1.126 brouard 9709:
9710: /* */
9711: strcpy(fileres,"r");
9712: strcat(fileres, optionfilefiname);
1.201 brouard 9713: strcat(fileresu, optionfilefiname); /* Without r in front */
1.126 brouard 9714: strcat(fileres,".txt"); /* Other files have txt extension */
1.201 brouard 9715: strcat(fileresu,".txt"); /* Other files have txt extension */
1.126 brouard 9716:
1.186 brouard 9717: /* Main ---------arguments file --------*/
1.126 brouard 9718:
9719: if((ficpar=fopen(optionfile,"r"))==NULL) {
1.155 brouard 9720: printf("Problem with optionfile '%s' with errno='%s'\n",optionfile,strerror(errno));
9721: fprintf(ficlog,"Problem with optionfile '%s' with errno='%s'\n",optionfile,strerror(errno));
1.126 brouard 9722: fflush(ficlog);
1.149 brouard 9723: /* goto end; */
9724: exit(70);
1.126 brouard 9725: }
9726:
9727:
9728:
9729: strcpy(filereso,"o");
1.201 brouard 9730: strcat(filereso,fileresu);
1.126 brouard 9731: if((ficparo=fopen(filereso,"w"))==NULL) { /* opened on subdirectory */
9732: printf("Problem with Output resultfile: %s\n", filereso);
9733: fprintf(ficlog,"Problem with Output resultfile: %s\n", filereso);
9734: fflush(ficlog);
9735: goto end;
9736: }
9737:
9738: /* Reads comments: lines beginning with '#' */
9739: numlinepar=0;
1.197 brouard 9740:
9741: /* First parameter line */
9742: while(fgets(line, MAXLINE, ficpar)) {
9743: /* If line starts with a # it is a comment */
9744: if (line[0] == '#') {
9745: numlinepar++;
9746: fputs(line,stdout);
9747: fputs(line,ficparo);
9748: fputs(line,ficlog);
9749: continue;
9750: }else
9751: break;
9752: }
9753: if((num_filled=sscanf(line,"title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\n", \
9754: title, datafile, &lastobs, &firstpass,&lastpass)) !=EOF){
9755: if (num_filled != 5) {
9756: printf("Should be 5 parameters\n");
9757: }
1.126 brouard 9758: numlinepar++;
1.197 brouard 9759: printf("title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\n", title, datafile, lastobs, firstpass,lastpass);
9760: }
9761: /* Second parameter line */
9762: while(fgets(line, MAXLINE, ficpar)) {
9763: /* If line starts with a # it is a comment */
9764: if (line[0] == '#') {
9765: numlinepar++;
9766: fputs(line,stdout);
9767: fputs(line,ficparo);
9768: fputs(line,ficlog);
9769: continue;
9770: }else
9771: break;
9772: }
1.223 brouard 9773: 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", \
9774: &ftol, &stepm, &ncovcol, &nqv, &ntv, &nqtv, &nlstate, &ndeath, &maxwav, &mle, &weightopt)) !=EOF){
9775: if (num_filled != 11) {
9776: 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 9777: printf("but line=%s\n",line);
1.197 brouard 9778: }
1.223 brouard 9779: 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 9780: }
1.203 brouard 9781: /* ftolpl=6*ftol*1.e5; /\* 6.e-3 make convergences in less than 80 loops for the prevalence limit *\/ */
1.209 brouard 9782: /*ftolpl=6.e-4; *//* 6.e-3 make convergences in less than 80 loops for the prevalence limit */
1.197 brouard 9783: /* Third parameter line */
9784: while(fgets(line, MAXLINE, ficpar)) {
9785: /* If line starts with a # it is a comment */
9786: if (line[0] == '#') {
9787: numlinepar++;
9788: fputs(line,stdout);
9789: fputs(line,ficparo);
9790: fputs(line,ficlog);
9791: continue;
9792: }else
9793: break;
9794: }
1.201 brouard 9795: if((num_filled=sscanf(line,"model=1+age%[^.\n]", model)) !=EOF){
9796: if (num_filled == 0)
9797: model[0]='\0';
9798: else if (num_filled != 1){
1.197 brouard 9799: printf("ERROR %d: Model should be at minimum 'model=1+age.' %s\n",num_filled, line);
9800: fprintf(ficlog,"ERROR %d: Model should be at minimum 'model=1+age.' %s\n",num_filled, line);
9801: model[0]='\0';
9802: goto end;
9803: }
9804: else{
9805: if (model[0]=='+'){
9806: for(i=1; i<=strlen(model);i++)
9807: modeltemp[i-1]=model[i];
1.201 brouard 9808: strcpy(model,modeltemp);
1.197 brouard 9809: }
9810: }
1.199 brouard 9811: /* printf(" model=1+age%s modeltemp= %s, model=%s\n",model, modeltemp, model);fflush(stdout); */
1.203 brouard 9812: printf("model=1+age+%s\n",model);fflush(stdout);
1.197 brouard 9813: }
9814: /* 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); */
9815: /* numlinepar=numlinepar+3; /\* In general *\/ */
9816: /* 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 9817: 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);
9818: 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 9819: fflush(ficlog);
1.190 brouard 9820: /* if(model[0]=='#'|| model[0]== '\0'){ */
9821: if(model[0]=='#'){
1.187 brouard 9822: printf("Error in 'model' line: model should start with 'model=1+age+' and end with '.' \n \
9823: 'model=1+age+.' or 'model=1+age+V1.' or 'model=1+age+age*age+V1+V1*age.' or \n \
9824: 'model=1+age+V1+V2.' or 'model=1+age+V1+V2+V1*V2.' etc. \n"); \
9825: if(mle != -1){
9826: printf("Fix the model line and run imach with mle=-1 to get a correct template of the parameter file.\n");
9827: exit(1);
9828: }
9829: }
1.126 brouard 9830: while((c=getc(ficpar))=='#' && c!= EOF){
9831: ungetc(c,ficpar);
9832: fgets(line, MAXLINE, ficpar);
9833: numlinepar++;
1.195 brouard 9834: if(line[1]=='q'){ /* This #q will quit imach (the answer is q) */
9835: z[0]=line[1];
9836: }
9837: /* printf("****line [1] = %c \n",line[1]); */
1.141 brouard 9838: fputs(line, stdout);
9839: //puts(line);
1.126 brouard 9840: fputs(line,ficparo);
9841: fputs(line,ficlog);
9842: }
9843: ungetc(c,ficpar);
9844:
9845:
1.145 brouard 9846: covar=matrix(0,NCOVMAX,1,n); /**< used in readdata */
1.225 brouard 9847: coqvar=matrix(1,nqv,1,n); /**< Fixed quantitative covariate */
1.233 brouard 9848: cotvar=ma3x(1,maxwav,1,ntv+nqtv,1,n); /**< Time varying covariate (dummy and quantitative)*/
1.225 brouard 9849: cotqvar=ma3x(1,maxwav,1,nqtv,1,n); /**< Time varying quantitative covariate */
1.136 brouard 9850: cptcovn=0; /*Number of covariates, i.e. number of '+' in model statement plus one, indepently of n in Vn*/
9851: /* v1+v2+v3+v2*v4+v5*age makes cptcovn = 5
9852: v1+v2*age+v2*v3 makes cptcovn = 3
9853: */
9854: if (strlen(model)>1)
1.187 brouard 9855: 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 9856: else
1.187 brouard 9857: ncovmodel=2; /* Constant and age */
1.133 brouard 9858: nforce= (nlstate+ndeath-1)*nlstate; /* Number of forces ij from state i to j */
9859: npar= nforce*ncovmodel; /* Number of parameters like aij*/
1.131 brouard 9860: if(npar >MAXPARM || nlstate >NLSTATEMAX || ndeath >NDEATHMAX || ncovmodel>NCOVMAX){
9861: 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);
9862: 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);
9863: fflush(stdout);
9864: fclose (ficlog);
9865: goto end;
9866: }
1.126 brouard 9867: delti3= ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel);
9868: delti=delti3[1][1];
9869: /*delti=vector(1,npar); *//* Scale of each paramater (output from hesscov)*/
9870: if(mle==-1){ /* Print a wizard for help writing covariance matrix */
9871: prwizard(ncovmodel, nlstate, ndeath, model, ficparo);
1.191 brouard 9872: printf(" You chose mle=-1, look at file %s for a template of covariance matrix \n",filereso);
9873: fprintf(ficlog," You chose mle=-1, look at file %s for a template of covariance matrix \n",filereso);
1.126 brouard 9874: free_ma3x(delti3,1,nlstate,1, nlstate+ndeath-1,1,ncovmodel);
9875: fclose (ficparo);
9876: fclose (ficlog);
9877: goto end;
9878: exit(0);
1.220 brouard 9879: } else if(mle==-5) { /* Main Wizard */
1.126 brouard 9880: prwizard(ncovmodel, nlstate, ndeath, model, ficparo);
1.192 brouard 9881: printf(" You chose mle=-3, look at file %s for a template of covariance matrix \n",filereso);
9882: fprintf(ficlog," You chose mle=-3, look at file %s for a template of covariance matrix \n",filereso);
1.126 brouard 9883: param= ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel);
9884: matcov=matrix(1,npar,1,npar);
1.203 brouard 9885: hess=matrix(1,npar,1,npar);
1.220 brouard 9886: } else{ /* Begin of mle != -1 or -5 */
1.145 brouard 9887: /* Read guessed parameters */
1.126 brouard 9888: /* Reads comments: lines beginning with '#' */
9889: while((c=getc(ficpar))=='#' && c!= EOF){
9890: ungetc(c,ficpar);
9891: fgets(line, MAXLINE, ficpar);
9892: numlinepar++;
1.141 brouard 9893: fputs(line,stdout);
1.126 brouard 9894: fputs(line,ficparo);
9895: fputs(line,ficlog);
9896: }
9897: ungetc(c,ficpar);
9898:
9899: param= ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel);
9900: for(i=1; i <=nlstate; i++){
1.234 brouard 9901: j=0;
1.126 brouard 9902: for(jj=1; jj <=nlstate+ndeath; jj++){
1.234 brouard 9903: if(jj==i) continue;
9904: j++;
9905: fscanf(ficpar,"%1d%1d",&i1,&j1);
9906: if ((i1 != i) || (j1 != jj)){
9907: printf("Error in line parameters number %d, %1d%1d instead of %1d%1d \n \
1.126 brouard 9908: It might be a problem of design; if ncovcol and the model are correct\n \
9909: run imach with mle=-1 to get a correct template of the parameter file.\n",numlinepar, i,j, i1, j1);
1.234 brouard 9910: exit(1);
9911: }
9912: fprintf(ficparo,"%1d%1d",i1,j1);
9913: if(mle==1)
9914: printf("%1d%1d",i,jj);
9915: fprintf(ficlog,"%1d%1d",i,jj);
9916: for(k=1; k<=ncovmodel;k++){
9917: fscanf(ficpar," %lf",¶m[i][j][k]);
9918: if(mle==1){
9919: printf(" %lf",param[i][j][k]);
9920: fprintf(ficlog," %lf",param[i][j][k]);
9921: }
9922: else
9923: fprintf(ficlog," %lf",param[i][j][k]);
9924: fprintf(ficparo," %lf",param[i][j][k]);
9925: }
9926: fscanf(ficpar,"\n");
9927: numlinepar++;
9928: if(mle==1)
9929: printf("\n");
9930: fprintf(ficlog,"\n");
9931: fprintf(ficparo,"\n");
1.126 brouard 9932: }
9933: }
9934: fflush(ficlog);
1.234 brouard 9935:
1.145 brouard 9936: /* Reads scales values */
1.126 brouard 9937: p=param[1][1];
9938:
9939: /* Reads comments: lines beginning with '#' */
9940: while((c=getc(ficpar))=='#' && c!= EOF){
9941: ungetc(c,ficpar);
9942: fgets(line, MAXLINE, ficpar);
9943: numlinepar++;
1.141 brouard 9944: fputs(line,stdout);
1.126 brouard 9945: fputs(line,ficparo);
9946: fputs(line,ficlog);
9947: }
9948: ungetc(c,ficpar);
9949:
9950: for(i=1; i <=nlstate; i++){
9951: for(j=1; j <=nlstate+ndeath-1; j++){
1.234 brouard 9952: fscanf(ficpar,"%1d%1d",&i1,&j1);
9953: if ( (i1-i) * (j1-j) != 0){
9954: printf("Error in line parameters number %d, %1d%1d instead of %1d%1d \n",numlinepar, i,j, i1, j1);
9955: exit(1);
9956: }
9957: printf("%1d%1d",i,j);
9958: fprintf(ficparo,"%1d%1d",i1,j1);
9959: fprintf(ficlog,"%1d%1d",i1,j1);
9960: for(k=1; k<=ncovmodel;k++){
9961: fscanf(ficpar,"%le",&delti3[i][j][k]);
9962: printf(" %le",delti3[i][j][k]);
9963: fprintf(ficparo," %le",delti3[i][j][k]);
9964: fprintf(ficlog," %le",delti3[i][j][k]);
9965: }
9966: fscanf(ficpar,"\n");
9967: numlinepar++;
9968: printf("\n");
9969: fprintf(ficparo,"\n");
9970: fprintf(ficlog,"\n");
1.126 brouard 9971: }
9972: }
9973: fflush(ficlog);
1.234 brouard 9974:
1.145 brouard 9975: /* Reads covariance matrix */
1.126 brouard 9976: delti=delti3[1][1];
1.220 brouard 9977:
9978:
1.126 brouard 9979: /* 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 9980:
1.126 brouard 9981: /* Reads comments: lines beginning with '#' */
9982: while((c=getc(ficpar))=='#' && c!= EOF){
9983: ungetc(c,ficpar);
9984: fgets(line, MAXLINE, ficpar);
9985: numlinepar++;
1.141 brouard 9986: fputs(line,stdout);
1.126 brouard 9987: fputs(line,ficparo);
9988: fputs(line,ficlog);
9989: }
9990: ungetc(c,ficpar);
1.220 brouard 9991:
1.126 brouard 9992: matcov=matrix(1,npar,1,npar);
1.203 brouard 9993: hess=matrix(1,npar,1,npar);
1.131 brouard 9994: for(i=1; i <=npar; i++)
9995: for(j=1; j <=npar; j++) matcov[i][j]=0.;
1.220 brouard 9996:
1.194 brouard 9997: /* Scans npar lines */
1.126 brouard 9998: for(i=1; i <=npar; i++){
1.226 brouard 9999: count=fscanf(ficpar,"%1d%1d%d",&i1,&j1,&jk);
1.194 brouard 10000: if(count != 3){
1.226 brouard 10001: printf("Error! Error in parameter file %s at line %d after line starting with %1d%1d%1d\n\
1.194 brouard 10002: This is probably because your covariance matrix doesn't \n contain exactly %d lines corresponding to your model line '1+age+%s'.\n\
10003: Please run with mle=-1 to get a correct covariance matrix.\n",optionfile,numlinepar, i1,j1,jk, npar, model);
1.226 brouard 10004: fprintf(ficlog,"Error! Error in parameter file %s at line %d after line starting with %1d%1d%1d\n\
1.194 brouard 10005: This is probably because your covariance matrix doesn't \n contain exactly %d lines corresponding to your model line '1+age+%s'.\n\
10006: Please run with mle=-1 to get a correct covariance matrix.\n",optionfile,numlinepar, i1,j1,jk, npar, model);
1.226 brouard 10007: exit(1);
1.220 brouard 10008: }else{
1.226 brouard 10009: if(mle==1)
10010: printf("%1d%1d%d",i1,j1,jk);
10011: }
10012: fprintf(ficlog,"%1d%1d%d",i1,j1,jk);
10013: fprintf(ficparo,"%1d%1d%d",i1,j1,jk);
1.126 brouard 10014: for(j=1; j <=i; j++){
1.226 brouard 10015: fscanf(ficpar," %le",&matcov[i][j]);
10016: if(mle==1){
10017: printf(" %.5le",matcov[i][j]);
10018: }
10019: fprintf(ficlog," %.5le",matcov[i][j]);
10020: fprintf(ficparo," %.5le",matcov[i][j]);
1.126 brouard 10021: }
10022: fscanf(ficpar,"\n");
10023: numlinepar++;
10024: if(mle==1)
1.220 brouard 10025: printf("\n");
1.126 brouard 10026: fprintf(ficlog,"\n");
10027: fprintf(ficparo,"\n");
10028: }
1.194 brouard 10029: /* End of read covariance matrix npar lines */
1.126 brouard 10030: for(i=1; i <=npar; i++)
10031: for(j=i+1;j<=npar;j++)
1.226 brouard 10032: matcov[i][j]=matcov[j][i];
1.126 brouard 10033:
10034: if(mle==1)
10035: printf("\n");
10036: fprintf(ficlog,"\n");
10037:
10038: fflush(ficlog);
10039:
10040: /*-------- Rewriting parameter file ----------*/
10041: strcpy(rfileres,"r"); /* "Rparameterfile */
10042: strcat(rfileres,optionfilefiname); /* Parameter file first name*/
10043: strcat(rfileres,"."); /* */
10044: strcat(rfileres,optionfilext); /* Other files have txt extension */
10045: if((ficres =fopen(rfileres,"w"))==NULL) {
1.201 brouard 10046: printf("Problem writing new parameter file: %s\n", rfileres);goto end;
10047: fprintf(ficlog,"Problem writing new parameter file: %s\n", rfileres);goto end;
1.126 brouard 10048: }
10049: fprintf(ficres,"#%s\n",version);
10050: } /* End of mle != -3 */
1.218 brouard 10051:
1.186 brouard 10052: /* Main data
10053: */
1.126 brouard 10054: n= lastobs;
10055: num=lvector(1,n);
10056: moisnais=vector(1,n);
10057: annais=vector(1,n);
10058: moisdc=vector(1,n);
10059: andc=vector(1,n);
1.220 brouard 10060: weight=vector(1,n);
1.126 brouard 10061: agedc=vector(1,n);
10062: cod=ivector(1,n);
1.220 brouard 10063: for(i=1;i<=n;i++){
1.234 brouard 10064: num[i]=0;
10065: moisnais[i]=0;
10066: annais[i]=0;
10067: moisdc[i]=0;
10068: andc[i]=0;
10069: agedc[i]=0;
10070: cod[i]=0;
10071: weight[i]=1.0; /* Equal weights, 1 by default */
10072: }
1.126 brouard 10073: mint=matrix(1,maxwav,1,n);
10074: anint=matrix(1,maxwav,1,n);
1.131 brouard 10075: s=imatrix(1,maxwav+1,1,n); /* s[i][j] health state for wave i and individual j */
1.126 brouard 10076: tab=ivector(1,NCOVMAX);
1.144 brouard 10077: ncodemax=ivector(1,NCOVMAX); /* Number of code per covariate; if O and 1 only, 2**ncov; V1+V2+V3+V4=>16 */
1.192 brouard 10078: 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 10079:
1.136 brouard 10080: /* Reads data from file datafile */
10081: if (readdata(datafile, firstobs, lastobs, &imx)==1)
10082: goto end;
10083:
10084: /* Calculation of the number of parameters from char model */
1.234 brouard 10085: /* modelsav=V2+V1+V4+age*V3 strb=age*V3 stra=V2+V1+V4
1.137 brouard 10086: k=4 (age*V3) Tvar[k=4]= 3 (from V3) Tag[cptcovage=1]=4
10087: k=3 V4 Tvar[k=3]= 4 (from V4)
10088: k=2 V1 Tvar[k=2]= 1 (from V1)
10089: k=1 Tvar[1]=2 (from V2)
1.234 brouard 10090: */
10091:
10092: Tvar=ivector(1,NCOVMAX); /* Was 15 changed to NCOVMAX. */
10093: TvarsDind=ivector(1,NCOVMAX); /* */
10094: TvarsD=ivector(1,NCOVMAX); /* */
10095: TvarsQind=ivector(1,NCOVMAX); /* */
10096: TvarsQ=ivector(1,NCOVMAX); /* */
1.232 brouard 10097: TvarF=ivector(1,NCOVMAX); /* */
10098: TvarFind=ivector(1,NCOVMAX); /* */
10099: TvarV=ivector(1,NCOVMAX); /* */
10100: TvarVind=ivector(1,NCOVMAX); /* */
10101: TvarA=ivector(1,NCOVMAX); /* */
10102: TvarAind=ivector(1,NCOVMAX); /* */
1.231 brouard 10103: TvarFD=ivector(1,NCOVMAX); /* */
10104: TvarFDind=ivector(1,NCOVMAX); /* */
10105: TvarFQ=ivector(1,NCOVMAX); /* */
10106: TvarFQind=ivector(1,NCOVMAX); /* */
10107: TvarVD=ivector(1,NCOVMAX); /* */
10108: TvarVDind=ivector(1,NCOVMAX); /* */
10109: TvarVQ=ivector(1,NCOVMAX); /* */
10110: TvarVQind=ivector(1,NCOVMAX); /* */
10111:
1.230 brouard 10112: Tvalsel=vector(1,NCOVMAX); /* */
1.233 brouard 10113: Tvarsel=ivector(1,NCOVMAX); /* */
1.226 brouard 10114: Typevar=ivector(-1,NCOVMAX); /* -1 to 2 */
10115: Fixed=ivector(-1,NCOVMAX); /* -1 to 3 */
10116: Dummy=ivector(-1,NCOVMAX); /* -1 to 3 */
1.137 brouard 10117: /* V2+V1+V4+age*V3 is a model with 4 covariates (3 plus signs).
10118: For each model-covariate stores the data-covariate id. Tvar[1]=2, Tvar[2]=1, Tvar[3]=4,
10119: Tvar[4=age*V3] is 3 and 'age' is recorded in Tage.
10120: */
10121: /* For model-covariate k tells which data-covariate to use but
10122: because this model-covariate is a construction we invent a new column
10123: ncovcol + k1
10124: If already ncovcol=4 and model=V2+V1+V1*V4+age*V3
10125: Tvar[3=V1*V4]=4+1 etc */
1.227 brouard 10126: Tprod=ivector(1,NCOVMAX); /* Gives the k position of the k1 product */
10127: Tposprod=ivector(1,NCOVMAX); /* Gives the k1 product from the k position */
1.137 brouard 10128: /* Tprod[k1=1]=3(=V1*V4) for V2+V1+V1*V4+age*V3
10129: if V2+V1+V1*V4+age*V3+V3*V2 TProd[k1=2]=5 (V3*V2)
1.227 brouard 10130: Tposprod[k]=k1 , Tposprod[3]=1, Tposprod[5]=2
1.137 brouard 10131: */
1.145 brouard 10132: Tvaraff=ivector(1,NCOVMAX); /* Unclear */
10133: 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 10134: * For V3*V2 (in V2+V1+V1*V4+age*V3+V3*V2), V3*V2 position is 2nd.
10135: * Tvard[k1=2][1]=3 (V3) Tvard[k1=2][2]=2(V2) */
1.145 brouard 10136: Tage=ivector(1,NCOVMAX); /* Gives the covariate id of covariates associated with age: V2 + V1 + age*V4 + V3*age
1.137 brouard 10137: 4 covariates (3 plus signs)
10138: Tage[1=V3*age]= 4; Tage[2=age*V4] = 3
10139: */
1.230 brouard 10140: Tmodelind=ivector(1,NCOVMAX);/** gives the k model position of an
1.227 brouard 10141: * individual dummy, fixed or varying:
10142: * Tmodelind[Tvaraff[3]]=9,Tvaraff[1]@9={4,
10143: * 3, 1, 0, 0, 0, 0, 0, 0},
1.230 brouard 10144: * model=V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 ,
10145: * V1 df, V2 qf, V3 & V4 dv, V5 qv
10146: * Tmodelind[1]@9={9,0,3,2,}*/
10147: TmodelInvind=ivector(1,NCOVMAX); /* TmodelInvind=Tvar[k]- ncovcol-nqv={5-2-1=2,*/
10148: TmodelInvQind=ivector(1,NCOVMAX);/** gives the k model position of an
1.228 brouard 10149: * individual quantitative, fixed or varying:
10150: * Tmodelqind[1]=1,Tvaraff[1]@9={4,
10151: * 3, 1, 0, 0, 0, 0, 0, 0},
10152: * model=V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1*/
1.186 brouard 10153: /* Main decodemodel */
10154:
1.187 brouard 10155:
1.223 brouard 10156: if(decodemodel(model, lastobs) == 1) /* In order to get Tvar[k] V4+V3+V5 p Tvar[1]@3 = {4, 3, 5}*/
1.136 brouard 10157: goto end;
10158:
1.137 brouard 10159: if((double)(lastobs-imx)/(double)imx > 1.10){
10160: nbwarn++;
10161: 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);
10162: 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);
10163: }
1.136 brouard 10164: /* if(mle==1){*/
1.137 brouard 10165: if (weightopt != 1) { /* Maximisation without weights. We can have weights different from 1 but want no weight*/
10166: for(i=1;i<=imx;i++) weight[i]=1.0; /* changed to imx */
1.136 brouard 10167: }
10168:
10169: /*-calculation of age at interview from date of interview and age at death -*/
10170: agev=matrix(1,maxwav,1,imx);
10171:
10172: if(calandcheckages(imx, maxwav, &agemin, &agemax, &nberr, &nbwarn) == 1)
10173: goto end;
10174:
1.126 brouard 10175:
1.136 brouard 10176: agegomp=(int)agemin;
10177: free_vector(moisnais,1,n);
10178: free_vector(annais,1,n);
1.126 brouard 10179: /* free_matrix(mint,1,maxwav,1,n);
10180: free_matrix(anint,1,maxwav,1,n);*/
1.215 brouard 10181: /* free_vector(moisdc,1,n); */
10182: /* free_vector(andc,1,n); */
1.145 brouard 10183: /* */
10184:
1.126 brouard 10185: wav=ivector(1,imx);
1.214 brouard 10186: /* dh=imatrix(1,lastpass-firstpass+1,1,imx); */
10187: /* bh=imatrix(1,lastpass-firstpass+1,1,imx); */
10188: /* mw=imatrix(1,lastpass-firstpass+1,1,imx); */
10189: 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.*/
10190: bh=imatrix(1,lastpass-firstpass+2,1,imx);
10191: mw=imatrix(1,lastpass-firstpass+2,1,imx);
1.126 brouard 10192:
10193: /* Concatenates waves */
1.214 brouard 10194: /* Concatenates waves: wav[i] is the number of effective (useful waves) of individual i.
10195: Death is a valid wave (if date is known).
10196: mw[mi][i] is the number of (mi=1 to wav[i]) effective wave out of mi of individual i
10197: dh[m][i] or dh[mw[mi][i]][i] is the delay between two effective waves m=mw[mi][i]
10198: and mw[mi+1][i]. dh depends on stepm.
10199: */
10200:
1.126 brouard 10201: concatwav(wav, dh, bh, mw, s, agedc, agev, firstpass, lastpass, imx, nlstate, stepm);
1.145 brouard 10202: /* */
10203:
1.215 brouard 10204: free_vector(moisdc,1,n);
10205: free_vector(andc,1,n);
10206:
1.126 brouard 10207: /* Routine tricode is to calculate cptcoveff (real number of unique covariates) and to associate covariable number and modality */
10208: nbcode=imatrix(0,NCOVMAX,0,NCOVMAX);
10209: ncodemax[1]=1;
1.145 brouard 10210: Ndum =ivector(-1,NCOVMAX);
1.225 brouard 10211: cptcoveff=0;
1.220 brouard 10212: if (ncovmodel-nagesqr > 2 ){ /* That is if covariate other than cst, age and age*age */
10213: tricode(&cptcoveff,Tvar,nbcode,imx, Ndum); /**< Fills nbcode[Tvar[j]][l]; */
1.227 brouard 10214: }
10215:
10216: ncovcombmax=pow(2,cptcoveff);
10217: invalidvarcomb=ivector(1, ncovcombmax);
10218: for(i=1;i<ncovcombmax;i++)
10219: invalidvarcomb[i]=0;
10220:
1.211 brouard 10221: /* Nbcode gives the value of the lth modality (currently 1 to 2) of jth covariate, in
1.186 brouard 10222: V2+V1*age, there are 3 covariates Tvar[2]=1 (V1).*/
1.211 brouard 10223: /* 1 to ncodemax[j] which is the maximum value of this jth covariate */
1.227 brouard 10224:
1.200 brouard 10225: /* codtab=imatrix(1,100,1,10);*/ /* codtab[h,k]=( (h-1) - mod(k-1,2**(k-1) )/2**(k-1) */
1.198 brouard 10226: /*printf(" codtab[1,1],codtab[100,10]=%d,%d\n", codtab[1][1],codtabm(100,10));*/
1.186 brouard 10227: /* codtab gives the value 1 or 2 of the hth combination of k covariates (1 or 2).*/
1.211 brouard 10228: /* nbcode[Tvaraff[j]][codtabm(h,j)]) : if there are only 2 modalities for a covariate j,
10229: * codtabm(h,j) gives its value classified at position h and nbcode gives how it is coded
10230: * (currently 0 or 1) in the data.
10231: * In a loop on h=1 to 2**k, and a loop on j (=1 to k), we get the value of
10232: * corresponding modality (h,j).
10233: */
10234:
1.145 brouard 10235: h=0;
10236: /*if (cptcovn > 0) */
1.126 brouard 10237: m=pow(2,cptcoveff);
10238:
1.144 brouard 10239: /**< codtab(h,k) k = codtab[h,k]=( (h-1) - mod(k-1,2**(k-1) )/2**(k-1) + 1
1.211 brouard 10240: * For k=4 covariates, h goes from 1 to m=2**k
10241: * codtabm(h,k)= (1 & (h-1) >> (k-1)) + 1;
10242: * #define codtabm(h,k) (1 & (h-1) >> (k-1))+1
1.186 brouard 10243: * h\k 1 2 3 4
1.143 brouard 10244: *______________________________
10245: * 1 i=1 1 i=1 1 i=1 1 i=1 1
10246: * 2 2 1 1 1
10247: * 3 i=2 1 2 1 1
10248: * 4 2 2 1 1
10249: * 5 i=3 1 i=2 1 2 1
10250: * 6 2 1 2 1
10251: * 7 i=4 1 2 2 1
10252: * 8 2 2 2 1
1.197 brouard 10253: * 9 i=5 1 i=3 1 i=2 1 2
10254: * 10 2 1 1 2
10255: * 11 i=6 1 2 1 2
10256: * 12 2 2 1 2
10257: * 13 i=7 1 i=4 1 2 2
10258: * 14 2 1 2 2
10259: * 15 i=8 1 2 2 2
10260: * 16 2 2 2 2
1.143 brouard 10261: */
1.212 brouard 10262: /* How to do the opposite? From combination h (=1 to 2**k) how to get the value on the covariates? */
1.211 brouard 10263: /* from h=5 and m, we get then number of covariates k=log(m)/log(2)=4
10264: * and the value of each covariate?
10265: * V1=1, V2=1, V3=2, V4=1 ?
10266: * h-1=4 and 4 is 0100 or reverse 0010, and +1 is 1121 ok.
10267: * h=6, 6-1=5, 5 is 0101, 1010, 2121, V1=2nd, V2=1st, V3=2nd, V4=1st.
10268: * In order to get the real value in the data, we use nbcode
10269: * nbcode[Tvar[3][2nd]]=1 and nbcode[Tvar[4][1]]=0
10270: * We are keeping this crazy system in order to be able (in the future?)
10271: * to have more than 2 values (0 or 1) for a covariate.
10272: * #define codtabm(h,k) (1 & (h-1) >> (k-1))+1
10273: * h=6, k=2? h-1=5=0101, reverse 1010, +1=2121, k=2nd position: value is 1: codtabm(6,2)=1
10274: * bbbbbbbb
10275: * 76543210
10276: * h-1 00000101 (6-1=5)
1.219 brouard 10277: *(h-1)>>(k-1)= 00000010 >> (2-1) = 1 right shift
1.211 brouard 10278: * &
10279: * 1 00000001 (1)
1.219 brouard 10280: * 00000000 = 1 & ((h-1) >> (k-1))
10281: * +1= 00000001 =1
1.211 brouard 10282: *
10283: * h=14, k=3 => h'=h-1=13, k'=k-1=2
10284: * h' 1101 =2^3+2^2+0x2^1+2^0
10285: * >>k' 11
10286: * & 00000001
10287: * = 00000001
10288: * +1 = 00000010=2 = codtabm(14,3)
10289: * Reverse h=6 and m=16?
10290: * cptcoveff=log(16)/log(2)=4 covariate: 6-1=5=0101 reversed=1010 +1=2121 =>V1=2, V2=1, V3=2, V4=1.
10291: * for (j=1 to cptcoveff) Vj=decodtabm(j,h,cptcoveff)
10292: * decodtabm(h,j,cptcoveff)= (((h-1) >> (j-1)) & 1) +1
10293: * decodtabm(h,j,cptcoveff)= (h <= (1<<cptcoveff)?(((h-1) >> (j-1)) & 1) +1 : -1)
10294: * V3=decodtabm(14,3,2**4)=2
10295: * h'=13 1101 =2^3+2^2+0x2^1+2^0
10296: *(h-1) >> (j-1) 0011 =13 >> 2
10297: * &1 000000001
10298: * = 000000001
10299: * +1= 000000010 =2
10300: * 2211
10301: * V1=1+1, V2=0+1, V3=1+1, V4=1+1
10302: * V3=2
1.220 brouard 10303: * codtabm and decodtabm are identical
1.211 brouard 10304: */
10305:
1.145 brouard 10306:
10307: free_ivector(Ndum,-1,NCOVMAX);
10308:
10309:
1.126 brouard 10310:
1.186 brouard 10311: /* Initialisation of ----------- gnuplot -------------*/
1.126 brouard 10312: strcpy(optionfilegnuplot,optionfilefiname);
10313: if(mle==-3)
1.201 brouard 10314: strcat(optionfilegnuplot,"-MORT_");
1.126 brouard 10315: strcat(optionfilegnuplot,".gp");
10316:
10317: if((ficgp=fopen(optionfilegnuplot,"w"))==NULL) {
10318: printf("Problem with file %s",optionfilegnuplot);
10319: }
10320: else{
1.204 brouard 10321: fprintf(ficgp,"\n# IMaCh-%s\n", version);
1.126 brouard 10322: fprintf(ficgp,"# %s\n", optionfilegnuplot);
1.141 brouard 10323: //fprintf(ficgp,"set missing 'NaNq'\n");
10324: fprintf(ficgp,"set datafile missing 'NaNq'\n");
1.126 brouard 10325: }
10326: /* fclose(ficgp);*/
1.186 brouard 10327:
10328:
10329: /* Initialisation of --------- index.htm --------*/
1.126 brouard 10330:
10331: strcpy(optionfilehtm,optionfilefiname); /* Main html file */
10332: if(mle==-3)
1.201 brouard 10333: strcat(optionfilehtm,"-MORT_");
1.126 brouard 10334: strcat(optionfilehtm,".htm");
10335: if((fichtm=fopen(optionfilehtm,"w"))==NULL) {
1.131 brouard 10336: printf("Problem with %s \n",optionfilehtm);
10337: exit(0);
1.126 brouard 10338: }
10339:
10340: strcpy(optionfilehtmcov,optionfilefiname); /* Only for matrix of covariance */
10341: strcat(optionfilehtmcov,"-cov.htm");
10342: if((fichtmcov=fopen(optionfilehtmcov,"w"))==NULL) {
10343: printf("Problem with %s \n",optionfilehtmcov), exit(0);
10344: }
10345: else{
10346: fprintf(fichtmcov,"<html><head>\n<title>IMaCh Cov %s</title></head>\n <body><font size=\"2\">%s <br> %s</font> \
10347: <hr size=\"2\" color=\"#EC5E5E\"> \n\
1.204 brouard 10348: Title=%s <br>Datafile=%s Firstpass=%d Lastpass=%d Stepm=%d Weight=%d Model=1+age+%s<br>\n",\
1.126 brouard 10349: optionfilehtmcov,version,fullversion,title,datafile,firstpass,lastpass,stepm, weightopt, model);
10350: }
10351:
1.213 brouard 10352: 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 10353: <hr size=\"2\" color=\"#EC5E5E\"> \n\
10354: <font size=\"2\">IMaCh-%s <br> %s</font> \
1.126 brouard 10355: <hr size=\"2\" color=\"#EC5E5E\"> \n\
1.204 brouard 10356: Title=%s <br>Datafile=%s Firstpass=%d Lastpass=%d Stepm=%d Weight=%d Model=1+age+%s<br>\n\
1.126 brouard 10357: \n\
10358: <hr size=\"2\" color=\"#EC5E5E\">\
10359: <ul><li><h4>Parameter files</h4>\n\
10360: - Parameter file: <a href=\"%s.%s\">%s.%s</a><br>\n\
10361: - Copy of the parameter file: <a href=\"o%s\">o%s</a><br>\n\
10362: - Log file of the run: <a href=\"%s\">%s</a><br>\n\
10363: - Gnuplot file name: <a href=\"%s\">%s</a><br>\n\
10364: - Date and time at start: %s</ul>\n",\
10365: optionfilehtm,version,fullversion,title,datafile,firstpass,lastpass,stepm, weightopt, model,\
10366: optionfilefiname,optionfilext,optionfilefiname,optionfilext,\
10367: fileres,fileres,\
10368: filelog,filelog,optionfilegnuplot,optionfilegnuplot,strstart);
10369: fflush(fichtm);
10370:
10371: strcpy(pathr,path);
10372: strcat(pathr,optionfilefiname);
1.184 brouard 10373: #ifdef WIN32
10374: _chdir(optionfilefiname); /* Move to directory named optionfile */
10375: #else
1.126 brouard 10376: chdir(optionfilefiname); /* Move to directory named optionfile */
1.184 brouard 10377: #endif
10378:
1.126 brouard 10379:
1.220 brouard 10380: /* Calculates basic frequencies. Computes observed prevalence at single age
10381: and for any valid combination of covariates
1.126 brouard 10382: and prints on file fileres'p'. */
1.227 brouard 10383: freqsummary(fileres, agemin, agemax, s, agev, nlstate, imx, Tvaraff, invalidvarcomb, nbcode, ncodemax,mint,anint,strstart, \
10384: firstpass, lastpass, stepm, weightopt, model);
1.126 brouard 10385:
10386: fprintf(fichtm,"\n");
10387: fprintf(fichtm,"<br>Total number of observations=%d <br>\n\
10388: Youngest age at first (selected) pass %.2f, oldest age %.2f<br>\n\
10389: Interval (in months) between two waves: Min=%d Max=%d Mean=%.2lf<br>\n",\
10390: imx,agemin,agemax,jmin,jmax,jmean);
10391: pmmij= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
1.220 brouard 10392: oldms= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
10393: newms= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
10394: savms= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
10395: oldm=oldms; newm=newms; savm=savms; /* Keeps fixed addresses to free */
1.218 brouard 10396:
1.126 brouard 10397: /* For Powell, parameters are in a vector p[] starting at p[1]
10398: so we point p on param[1][1] so that p[1] maps on param[1][1][1] */
10399: p=param[1][1]; /* *(*(*(param +1)+1)+0) */
10400:
10401: globpr=0; /* To get the number ipmx of contributions and the sum of weights*/
1.186 brouard 10402: /* For mortality only */
1.126 brouard 10403: if (mle==-3){
1.136 brouard 10404: ximort=matrix(1,NDIM,1,NDIM);
1.220 brouard 10405: for(i=1;i<=NDIM;i++)
10406: for(j=1;j<=NDIM;j++)
10407: ximort[i][j]=0.;
1.186 brouard 10408: /* ximort=gsl_matrix_alloc(1,NDIM,1,NDIM); */
1.126 brouard 10409: cens=ivector(1,n);
10410: ageexmed=vector(1,n);
10411: agecens=vector(1,n);
10412: dcwave=ivector(1,n);
1.223 brouard 10413:
1.126 brouard 10414: for (i=1; i<=imx; i++){
10415: dcwave[i]=-1;
10416: for (m=firstpass; m<=lastpass; m++)
1.226 brouard 10417: if (s[m][i]>nlstate) {
10418: dcwave[i]=m;
10419: /* printf("i=%d j=%d s=%d dcwave=%d\n",i,j, s[j][i],dcwave[i]);*/
10420: break;
10421: }
1.126 brouard 10422: }
1.226 brouard 10423:
1.126 brouard 10424: for (i=1; i<=imx; i++) {
10425: if (wav[i]>0){
1.226 brouard 10426: ageexmed[i]=agev[mw[1][i]][i];
10427: j=wav[i];
10428: agecens[i]=1.;
10429:
10430: if (ageexmed[i]> 1 && wav[i] > 0){
10431: agecens[i]=agev[mw[j][i]][i];
10432: cens[i]= 1;
10433: }else if (ageexmed[i]< 1)
10434: cens[i]= -1;
10435: if (agedc[i]< AGESUP && agedc[i]>1 && dcwave[i]>firstpass && dcwave[i]<=lastpass)
10436: cens[i]=0 ;
1.126 brouard 10437: }
10438: else cens[i]=-1;
10439: }
10440:
10441: for (i=1;i<=NDIM;i++) {
10442: for (j=1;j<=NDIM;j++)
1.226 brouard 10443: ximort[i][j]=(i == j ? 1.0 : 0.0);
1.126 brouard 10444: }
10445:
1.145 brouard 10446: /*p[1]=0.0268; p[NDIM]=0.083;*/
1.126 brouard 10447: /*printf("%lf %lf", p[1], p[2]);*/
10448:
10449:
1.136 brouard 10450: #ifdef GSL
10451: printf("GSL optimization\n"); fprintf(ficlog,"Powell\n");
1.162 brouard 10452: #else
1.126 brouard 10453: printf("Powell\n"); fprintf(ficlog,"Powell\n");
1.136 brouard 10454: #endif
1.201 brouard 10455: strcpy(filerespow,"POW-MORT_");
10456: strcat(filerespow,fileresu);
1.126 brouard 10457: if((ficrespow=fopen(filerespow,"w"))==NULL) {
10458: printf("Problem with resultfile: %s\n", filerespow);
10459: fprintf(ficlog,"Problem with resultfile: %s\n", filerespow);
10460: }
1.136 brouard 10461: #ifdef GSL
10462: fprintf(ficrespow,"# GSL optimization\n# iter -2*LL");
1.162 brouard 10463: #else
1.126 brouard 10464: fprintf(ficrespow,"# Powell\n# iter -2*LL");
1.136 brouard 10465: #endif
1.126 brouard 10466: /* for (i=1;i<=nlstate;i++)
10467: for(j=1;j<=nlstate+ndeath;j++)
10468: if(j!=i)fprintf(ficrespow," p%1d%1d",i,j);
10469: */
10470: fprintf(ficrespow,"\n");
1.136 brouard 10471: #ifdef GSL
10472: /* gsl starts here */
10473: T = gsl_multimin_fminimizer_nmsimplex;
10474: gsl_multimin_fminimizer *sfm = NULL;
10475: gsl_vector *ss, *x;
10476: gsl_multimin_function minex_func;
10477:
10478: /* Initial vertex size vector */
10479: ss = gsl_vector_alloc (NDIM);
10480:
10481: if (ss == NULL){
10482: GSL_ERROR_VAL ("failed to allocate space for ss", GSL_ENOMEM, 0);
10483: }
10484: /* Set all step sizes to 1 */
10485: gsl_vector_set_all (ss, 0.001);
10486:
10487: /* Starting point */
1.126 brouard 10488:
1.136 brouard 10489: x = gsl_vector_alloc (NDIM);
10490:
10491: if (x == NULL){
10492: gsl_vector_free(ss);
10493: GSL_ERROR_VAL ("failed to allocate space for x", GSL_ENOMEM, 0);
10494: }
10495:
10496: /* Initialize method and iterate */
10497: /* p[1]=0.0268; p[NDIM]=0.083; */
1.186 brouard 10498: /* gsl_vector_set(x, 0, 0.0268); */
10499: /* gsl_vector_set(x, 1, 0.083); */
1.136 brouard 10500: gsl_vector_set(x, 0, p[1]);
10501: gsl_vector_set(x, 1, p[2]);
10502:
10503: minex_func.f = &gompertz_f;
10504: minex_func.n = NDIM;
10505: minex_func.params = (void *)&p; /* ??? */
10506:
10507: sfm = gsl_multimin_fminimizer_alloc (T, NDIM);
10508: gsl_multimin_fminimizer_set (sfm, &minex_func, x, ss);
10509:
10510: printf("Iterations beginning .....\n\n");
10511: printf("Iter. # Intercept Slope -Log Likelihood Simplex size\n");
10512:
10513: iteri=0;
10514: while (rval == GSL_CONTINUE){
10515: iteri++;
10516: status = gsl_multimin_fminimizer_iterate(sfm);
10517:
10518: if (status) printf("error: %s\n", gsl_strerror (status));
10519: fflush(0);
10520:
10521: if (status)
10522: break;
10523:
10524: rval = gsl_multimin_test_size (gsl_multimin_fminimizer_size (sfm), 1e-6);
10525: ssval = gsl_multimin_fminimizer_size (sfm);
10526:
10527: if (rval == GSL_SUCCESS)
10528: printf ("converged to a local maximum at\n");
10529:
10530: printf("%5d ", iteri);
10531: for (it = 0; it < NDIM; it++){
10532: printf ("%10.5f ", gsl_vector_get (sfm->x, it));
10533: }
10534: printf("f() = %-10.5f ssize = %.7f\n", sfm->fval, ssval);
10535: }
10536:
10537: printf("\n\n Please note: Program should be run many times with varying starting points to detemine global maximum\n\n");
10538:
10539: gsl_vector_free(x); /* initial values */
10540: gsl_vector_free(ss); /* inital step size */
10541: for (it=0; it<NDIM; it++){
10542: p[it+1]=gsl_vector_get(sfm->x,it);
10543: fprintf(ficrespow," %.12lf", p[it]);
10544: }
10545: gsl_multimin_fminimizer_free (sfm); /* p *(sfm.x.data) et p *(sfm.x.data+1) */
10546: #endif
10547: #ifdef POWELL
10548: powell(p,ximort,NDIM,ftol,&iter,&fret,gompertz);
10549: #endif
1.126 brouard 10550: fclose(ficrespow);
10551:
1.203 brouard 10552: hesscov(matcov, hess, p, NDIM, delti, 1e-4, gompertz);
1.126 brouard 10553:
10554: for(i=1; i <=NDIM; i++)
10555: for(j=i+1;j<=NDIM;j++)
1.220 brouard 10556: matcov[i][j]=matcov[j][i];
1.126 brouard 10557:
10558: printf("\nCovariance matrix\n ");
1.203 brouard 10559: fprintf(ficlog,"\nCovariance matrix\n ");
1.126 brouard 10560: for(i=1; i <=NDIM; i++) {
10561: for(j=1;j<=NDIM;j++){
1.220 brouard 10562: printf("%f ",matcov[i][j]);
10563: fprintf(ficlog,"%f ",matcov[i][j]);
1.126 brouard 10564: }
1.203 brouard 10565: printf("\n "); fprintf(ficlog,"\n ");
1.126 brouard 10566: }
10567:
10568: printf("iter=%d MLE=%f Eq=%lf*exp(%lf*(age-%d))\n",iter,-gompertz(p),p[1],p[2],agegomp);
1.193 brouard 10569: for (i=1;i<=NDIM;i++) {
1.126 brouard 10570: printf("%f [%f ; %f]\n",p[i],p[i]-2*sqrt(matcov[i][i]),p[i]+2*sqrt(matcov[i][i]));
1.193 brouard 10571: fprintf(ficlog,"%f [%f ; %f]\n",p[i],p[i]-2*sqrt(matcov[i][i]),p[i]+2*sqrt(matcov[i][i]));
10572: }
1.126 brouard 10573: lsurv=vector(1,AGESUP);
10574: lpop=vector(1,AGESUP);
10575: tpop=vector(1,AGESUP);
10576: lsurv[agegomp]=100000;
10577:
10578: for (k=agegomp;k<=AGESUP;k++) {
10579: agemortsup=k;
10580: if (p[1]*exp(p[2]*(k-agegomp))>1) break;
10581: }
10582:
10583: for (k=agegomp;k<agemortsup;k++)
10584: lsurv[k+1]=lsurv[k]-lsurv[k]*(p[1]*exp(p[2]*(k-agegomp)));
10585:
10586: for (k=agegomp;k<agemortsup;k++){
10587: lpop[k]=(lsurv[k]+lsurv[k+1])/2.;
10588: sumlpop=sumlpop+lpop[k];
10589: }
10590:
10591: tpop[agegomp]=sumlpop;
10592: for (k=agegomp;k<(agemortsup-3);k++){
10593: /* tpop[k+1]=2;*/
10594: tpop[k+1]=tpop[k]-lpop[k];
10595: }
10596:
10597:
10598: printf("\nAge lx qx dx Lx Tx e(x)\n");
10599: for (k=agegomp;k<(agemortsup-2);k++)
10600: 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]);
10601:
10602:
10603: replace_back_to_slash(pathc,pathcd); /* Even gnuplot wants a / */
1.220 brouard 10604: ageminpar=50;
10605: agemaxpar=100;
1.194 brouard 10606: if(ageminpar == AGEOVERFLOW ||agemaxpar == AGEOVERFLOW){
10607: printf("Warning! Error in gnuplot file with ageminpar %f or agemaxpar %f overflow\n\
10608: This is probably because your parameter file doesn't \n contain the exact number of lines (or columns) corresponding to your model line.\n\
10609: Please run with mle=-1 to get a correct covariance matrix.\n",ageminpar,agemaxpar);
10610: fprintf(ficlog,"Warning! Error in gnuplot file with ageminpar %f or agemaxpar %f overflow\n\
10611: This is probably because your parameter file doesn't \n contain the exact number of lines (or columns) corresponding to your model line.\n\
10612: Please run with mle=-1 to get a correct covariance matrix.\n",ageminpar,agemaxpar);
1.220 brouard 10613: }else{
10614: printf("Warning! ageminpar %f and agemaxpar %f have been fixed because for simplification until it is fixed...\n\n",ageminpar,agemaxpar);
10615: 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 10616: printinggnuplotmort(fileresu, optionfilefiname,ageminpar,agemaxpar,fage, pathc,p);
1.220 brouard 10617: }
1.201 brouard 10618: printinghtmlmort(fileresu,title,datafile, firstpass, lastpass, \
1.126 brouard 10619: stepm, weightopt,\
10620: model,imx,p,matcov,agemortsup);
10621:
10622: free_vector(lsurv,1,AGESUP);
10623: free_vector(lpop,1,AGESUP);
10624: free_vector(tpop,1,AGESUP);
1.220 brouard 10625: free_matrix(ximort,1,NDIM,1,NDIM);
1.136 brouard 10626: free_ivector(cens,1,n);
10627: free_vector(agecens,1,n);
10628: free_ivector(dcwave,1,n);
1.220 brouard 10629: #ifdef GSL
1.136 brouard 10630: #endif
1.186 brouard 10631: } /* Endof if mle==-3 mortality only */
1.205 brouard 10632: /* Standard */
10633: else{ /* For mle !=- 3, could be 0 or 1 or 4 etc. */
10634: globpr=0;/* Computes sum of likelihood for globpr=1 and funcone */
10635: /* Computes likelihood for initial parameters, uses funcone to compute gpimx and gsw */
1.132 brouard 10636: likelione(ficres, p, npar, nlstate, &globpr, &ipmx, &sw, &fretone, funcone); /* Prints the contributions to the likelihood */
1.126 brouard 10637: printf("First Likeli=%12.6f ipmx=%ld sw=%12.6f",fretone,ipmx,sw);
10638: for (k=1; k<=npar;k++)
10639: printf(" %d %8.5f",k,p[k]);
10640: printf("\n");
1.205 brouard 10641: if(mle>=1){ /* Could be 1 or 2, Real Maximization */
10642: /* mlikeli uses func not funcone */
10643: mlikeli(ficres,p, npar, ncovmodel, nlstate, ftol, func);
10644: }
10645: if(mle==0) {/* No optimization, will print the likelihoods for the datafile */
10646: globpr=0;/* Computes sum of likelihood for globpr=1 and funcone */
10647: /* Computes likelihood for initial parameters, uses funcone to compute gpimx and gsw */
10648: likelione(ficres, p, npar, nlstate, &globpr, &ipmx, &sw, &fretone, funcone); /* Prints the contributions to the likelihood */
10649: }
10650: globpr=1; /* again, to print the individual contributions using computed gpimx and gsw */
1.126 brouard 10651: likelione(ficres, p, npar, nlstate, &globpr, &ipmx, &sw, &fretone, funcone); /* Prints the contributions to the likelihood */
10652: printf("Second Likeli=%12.6f ipmx=%ld sw=%12.6f",fretone,ipmx,sw);
10653: for (k=1; k<=npar;k++)
10654: printf(" %d %8.5f",k,p[k]);
10655: printf("\n");
10656:
10657: /*--------- results files --------------*/
1.224 brouard 10658: 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 10659:
10660:
10661: fprintf(ficres,"# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n");
10662: printf("# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n");
10663: fprintf(ficlog,"# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n");
10664: for(i=1,jk=1; i <=nlstate; i++){
10665: for(k=1; k <=(nlstate+ndeath); k++){
1.225 brouard 10666: if (k != i) {
10667: printf("%d%d ",i,k);
10668: fprintf(ficlog,"%d%d ",i,k);
10669: fprintf(ficres,"%1d%1d ",i,k);
10670: for(j=1; j <=ncovmodel; j++){
10671: printf("%12.7f ",p[jk]);
10672: fprintf(ficlog,"%12.7f ",p[jk]);
10673: fprintf(ficres,"%12.7f ",p[jk]);
10674: jk++;
10675: }
10676: printf("\n");
10677: fprintf(ficlog,"\n");
10678: fprintf(ficres,"\n");
10679: }
1.126 brouard 10680: }
10681: }
1.203 brouard 10682: if(mle != 0){
10683: /* Computing hessian and covariance matrix only at a peak of the Likelihood, that is after optimization */
1.126 brouard 10684: ftolhess=ftol; /* Usually correct */
1.203 brouard 10685: hesscov(matcov, hess, p, npar, delti, ftolhess, func);
10686: 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");
10687: 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");
10688: for(i=1,jk=1; i <=nlstate; i++){
1.225 brouard 10689: for(k=1; k <=(nlstate+ndeath); k++){
10690: if (k != i) {
10691: printf("%d%d ",i,k);
10692: fprintf(ficlog,"%d%d ",i,k);
10693: for(j=1; j <=ncovmodel; j++){
10694: 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]));
10695: 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]));
10696: jk++;
10697: }
10698: printf("\n");
10699: fprintf(ficlog,"\n");
10700: }
10701: }
1.193 brouard 10702: }
1.203 brouard 10703: } /* end of hesscov and Wald tests */
1.225 brouard 10704:
1.203 brouard 10705: /* */
1.126 brouard 10706: fprintf(ficres,"# Scales (for hessian or gradient estimation)\n");
10707: printf("# Scales (for hessian or gradient estimation)\n");
10708: fprintf(ficlog,"# Scales (for hessian or gradient estimation)\n");
10709: for(i=1,jk=1; i <=nlstate; i++){
10710: for(j=1; j <=nlstate+ndeath; j++){
1.225 brouard 10711: if (j!=i) {
10712: fprintf(ficres,"%1d%1d",i,j);
10713: printf("%1d%1d",i,j);
10714: fprintf(ficlog,"%1d%1d",i,j);
10715: for(k=1; k<=ncovmodel;k++){
10716: printf(" %.5e",delti[jk]);
10717: fprintf(ficlog," %.5e",delti[jk]);
10718: fprintf(ficres," %.5e",delti[jk]);
10719: jk++;
10720: }
10721: printf("\n");
10722: fprintf(ficlog,"\n");
10723: fprintf(ficres,"\n");
10724: }
1.126 brouard 10725: }
10726: }
10727:
10728: 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 10729: if(mle >= 1) /* To big for the screen */
1.126 brouard 10730: 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");
10731: 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");
10732: /* # 121 Var(a12)\n\ */
10733: /* # 122 Cov(b12,a12) Var(b12)\n\ */
10734: /* # 131 Cov(a13,a12) Cov(a13,b12, Var(a13)\n\ */
10735: /* # 132 Cov(b13,a12) Cov(b13,b12, Cov(b13,a13) Var(b13)\n\ */
10736: /* # 212 Cov(a21,a12) Cov(a21,b12, Cov(a21,a13) Cov(a21,b13) Var(a21)\n\ */
10737: /* # 212 Cov(b21,a12) Cov(b21,b12, Cov(b21,a13) Cov(b21,b13) Cov(b21,a21) Var(b21)\n\ */
10738: /* # 232 Cov(a23,a12) Cov(a23,b12, Cov(a23,a13) Cov(a23,b13) Cov(a23,a21) Cov(a23,b21) Var(a23)\n\ */
10739: /* # 232 Cov(b23,a12) Cov(b23,b12) ... Var (b23)\n" */
10740:
10741:
10742: /* Just to have a covariance matrix which will be more understandable
10743: even is we still don't want to manage dictionary of variables
10744: */
10745: for(itimes=1;itimes<=2;itimes++){
10746: jj=0;
10747: for(i=1; i <=nlstate; i++){
1.225 brouard 10748: for(j=1; j <=nlstate+ndeath; j++){
10749: if(j==i) continue;
10750: for(k=1; k<=ncovmodel;k++){
10751: jj++;
10752: ca[0]= k+'a'-1;ca[1]='\0';
10753: if(itimes==1){
10754: if(mle>=1)
10755: printf("#%1d%1d%d",i,j,k);
10756: fprintf(ficlog,"#%1d%1d%d",i,j,k);
10757: fprintf(ficres,"#%1d%1d%d",i,j,k);
10758: }else{
10759: if(mle>=1)
10760: printf("%1d%1d%d",i,j,k);
10761: fprintf(ficlog,"%1d%1d%d",i,j,k);
10762: fprintf(ficres,"%1d%1d%d",i,j,k);
10763: }
10764: ll=0;
10765: for(li=1;li <=nlstate; li++){
10766: for(lj=1;lj <=nlstate+ndeath; lj++){
10767: if(lj==li) continue;
10768: for(lk=1;lk<=ncovmodel;lk++){
10769: ll++;
10770: if(ll<=jj){
10771: cb[0]= lk +'a'-1;cb[1]='\0';
10772: if(ll<jj){
10773: if(itimes==1){
10774: if(mle>=1)
10775: printf(" Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj);
10776: fprintf(ficlog," Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj);
10777: fprintf(ficres," Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj);
10778: }else{
10779: if(mle>=1)
10780: printf(" %.5e",matcov[jj][ll]);
10781: fprintf(ficlog," %.5e",matcov[jj][ll]);
10782: fprintf(ficres," %.5e",matcov[jj][ll]);
10783: }
10784: }else{
10785: if(itimes==1){
10786: if(mle>=1)
10787: printf(" Var(%s%1d%1d)",ca,i,j);
10788: fprintf(ficlog," Var(%s%1d%1d)",ca,i,j);
10789: fprintf(ficres," Var(%s%1d%1d)",ca,i,j);
10790: }else{
10791: if(mle>=1)
10792: printf(" %.7e",matcov[jj][ll]);
10793: fprintf(ficlog," %.7e",matcov[jj][ll]);
10794: fprintf(ficres," %.7e",matcov[jj][ll]);
10795: }
10796: }
10797: }
10798: } /* end lk */
10799: } /* end lj */
10800: } /* end li */
10801: if(mle>=1)
10802: printf("\n");
10803: fprintf(ficlog,"\n");
10804: fprintf(ficres,"\n");
10805: numlinepar++;
10806: } /* end k*/
10807: } /*end j */
1.126 brouard 10808: } /* end i */
10809: } /* end itimes */
10810:
10811: fflush(ficlog);
10812: fflush(ficres);
1.225 brouard 10813: while(fgets(line, MAXLINE, ficpar)) {
10814: /* If line starts with a # it is a comment */
10815: if (line[0] == '#') {
10816: numlinepar++;
10817: fputs(line,stdout);
10818: fputs(line,ficparo);
10819: fputs(line,ficlog);
10820: continue;
10821: }else
10822: break;
10823: }
10824:
1.209 brouard 10825: /* while((c=getc(ficpar))=='#' && c!= EOF){ */
10826: /* ungetc(c,ficpar); */
10827: /* fgets(line, MAXLINE, ficpar); */
10828: /* fputs(line,stdout); */
10829: /* fputs(line,ficparo); */
10830: /* } */
10831: /* ungetc(c,ficpar); */
1.126 brouard 10832:
10833: estepm=0;
1.209 brouard 10834: 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 10835:
10836: if (num_filled != 6) {
10837: 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);
10838: 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);
10839: goto end;
10840: }
10841: printf("agemin=%lf agemax=%lf bage=%lf fage=%lf estepm=%d ftolpl=%lf\n",ageminpar,agemaxpar, bage, fage, estepm, ftolpl);
10842: }
10843: /* ftolpl=6*ftol*1.e5; /\* 6.e-3 make convergences in less than 80 loops for the prevalence limit *\/ */
10844: /*ftolpl=6.e-4;*/ /* 6.e-3 make convergences in less than 80 loops for the prevalence limit */
10845:
1.209 brouard 10846: /* fscanf(ficpar,"agemin=%lf agemax=%lf bage=%lf fage=%lf estepm=%d ftolpl=%\n",&ageminpar,&agemaxpar, &bage, &fage, &estepm); */
1.126 brouard 10847: if (estepm==0 || estepm < stepm) estepm=stepm;
10848: if (fage <= 2) {
10849: bage = ageminpar;
10850: fage = agemaxpar;
10851: }
10852:
10853: fprintf(ficres,"# agemin agemax for life expectancy, bage fage (if mle==0 ie no data nor Max likelihood).\n");
1.211 brouard 10854: fprintf(ficres,"agemin=%.0f agemax=%.0f bage=%.0f fage=%.0f estepm=%d ftolpl=%e\n",ageminpar,agemaxpar,bage,fage, estepm, ftolpl);
10855: fprintf(ficparo,"agemin=%.0f agemax=%.0f bage=%.0f fage=%.0f estepm=%d, ftolpl=%e\n",ageminpar,agemaxpar,bage,fage, estepm, ftolpl);
1.220 brouard 10856:
1.186 brouard 10857: /* Other stuffs, more or less useful */
1.126 brouard 10858: while((c=getc(ficpar))=='#' && c!= EOF){
10859: ungetc(c,ficpar);
10860: fgets(line, MAXLINE, ficpar);
1.141 brouard 10861: fputs(line,stdout);
1.126 brouard 10862: fputs(line,ficparo);
10863: }
10864: ungetc(c,ficpar);
10865:
10866: 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);
10867: 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);
10868: 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);
10869: printf("begin-prev-date=%.lf/%.lf/%.lf end-prev-date=%.lf/%.lf/%.lf mov_average=%d\n",jprev1, mprev1,anprev1,jprev2, mprev2,anprev2,mobilav);
10870: 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);
10871:
10872: while((c=getc(ficpar))=='#' && c!= EOF){
10873: ungetc(c,ficpar);
10874: fgets(line, MAXLINE, ficpar);
1.141 brouard 10875: fputs(line,stdout);
1.126 brouard 10876: fputs(line,ficparo);
10877: }
10878: ungetc(c,ficpar);
10879:
10880:
10881: dateprev1=anprev1+(mprev1-1)/12.+(jprev1-1)/365.;
10882: dateprev2=anprev2+(mprev2-1)/12.+(jprev2-1)/365.;
10883:
10884: fscanf(ficpar,"pop_based=%d\n",&popbased);
1.193 brouard 10885: fprintf(ficlog,"pop_based=%d\n",popbased);
1.126 brouard 10886: fprintf(ficparo,"pop_based=%d\n",popbased);
10887: fprintf(ficres,"pop_based=%d\n",popbased);
10888:
10889: while((c=getc(ficpar))=='#' && c!= EOF){
10890: ungetc(c,ficpar);
10891: fgets(line, MAXLINE, ficpar);
1.141 brouard 10892: fputs(line,stdout);
1.238 brouard 10893: fputs(line,ficres);
1.126 brouard 10894: fputs(line,ficparo);
10895: }
10896: ungetc(c,ficpar);
10897:
10898: 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);
10899: 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);
10900: 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);
10901: 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);
10902: 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);
10903: /* day and month of proj2 are not used but only year anproj2.*/
10904:
1.217 brouard 10905: while((c=getc(ficpar))=='#' && c!= EOF){
10906: ungetc(c,ficpar);
10907: fgets(line, MAXLINE, ficpar);
10908: fputs(line,stdout);
10909: fputs(line,ficparo);
1.238 brouard 10910: fputs(line,ficres);
1.217 brouard 10911: }
10912: ungetc(c,ficpar);
10913:
10914: 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 10915: 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);
10916: 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);
10917: 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 10918: /* day and month of proj2 are not used but only year anproj2.*/
1.126 brouard 10919:
1.230 brouard 10920: /* Results */
1.235 brouard 10921: nresult=0;
1.230 brouard 10922: while(fgets(line, MAXLINE, ficpar)) {
10923: /* If line starts with a # it is a comment */
10924: if (line[0] == '#') {
10925: numlinepar++;
10926: fputs(line,stdout);
10927: fputs(line,ficparo);
10928: fputs(line,ficlog);
1.238 brouard 10929: fputs(line,ficres);
1.230 brouard 10930: continue;
10931: }else
10932: break;
10933: }
1.240 brouard 10934: if (!feof(ficpar))
1.230 brouard 10935: while((num_filled=sscanf(line,"result:%[^\n]\n",resultline)) !=EOF){
1.240 brouard 10936: if (num_filled == 0){
1.230 brouard 10937: resultline[0]='\0';
1.240 brouard 10938: break;
10939: } else if (num_filled != 1){
1.230 brouard 10940: printf("ERROR %d: result line should be at minimum 'result=' %s\n",num_filled, line);
10941: }
1.235 brouard 10942: nresult++; /* Sum of resultlines */
10943: printf("Result %d: result=%s\n",nresult, resultline);
10944: if(nresult > MAXRESULTLINES){
10945: printf("ERROR: Current version of IMaCh limits the number of resultlines to %d, you used %d\n",MAXRESULTLINES,nresult);
10946: fprintf(ficlog,"ERROR: Current version of IMaCh limits the number of resultlines to %d, you used %d\n",MAXRESULTLINES,nresult);
10947: goto end;
10948: }
10949: decoderesult(resultline, nresult); /* Fills TKresult[nresult] combination and Tresult[nresult][k4+1] combination values */
1.238 brouard 10950: fprintf(ficparo,"result: %s\n",resultline);
10951: fprintf(ficres,"result: %s\n",resultline);
10952: fprintf(ficlog,"result: %s\n",resultline);
1.230 brouard 10953: while(fgets(line, MAXLINE, ficpar)) {
10954: /* If line starts with a # it is a comment */
10955: if (line[0] == '#') {
10956: numlinepar++;
10957: fputs(line,stdout);
10958: fputs(line,ficparo);
1.238 brouard 10959: fputs(line,ficres);
1.230 brouard 10960: fputs(line,ficlog);
10961: continue;
10962: }else
10963: break;
10964: }
10965: if (feof(ficpar))
10966: break;
10967: else{ /* Processess output results for this combination of covariate values */
10968: }
1.240 brouard 10969: } /* end while */
1.230 brouard 10970:
10971:
1.126 brouard 10972:
1.230 brouard 10973: /* freqsummary(fileres, agemin, agemax, s, agev, nlstate, imx,Tvaraff,nbcode, ncodemax,mint,anint); */
1.145 brouard 10974: /* ,dateprev1,dateprev2,jprev1, mprev1,anprev1,jprev2, mprev2,anprev2); */
1.126 brouard 10975:
10976: replace_back_to_slash(pathc,pathcd); /* Even gnuplot wants a / */
1.194 brouard 10977: if(ageminpar == AGEOVERFLOW ||agemaxpar == -AGEOVERFLOW){
1.230 brouard 10978: printf("Warning! Error in gnuplot file with ageminpar %f or agemaxpar %f overflow\n\
1.194 brouard 10979: This is probably because your parameter file doesn't \n contain the exact number of lines (or columns) corresponding to your model line.\n\
10980: Please run with mle=-1 to get a correct covariance matrix.\n",ageminpar,agemaxpar);
1.230 brouard 10981: fprintf(ficlog,"Warning! Error in gnuplot file with ageminpar %f or agemaxpar %f overflow\n\
1.194 brouard 10982: This is probably because your parameter file doesn't \n contain the exact number of lines (or columns) corresponding to your model line.\n\
10983: Please run with mle=-1 to get a correct covariance matrix.\n",ageminpar,agemaxpar);
1.220 brouard 10984: }else{
1.218 brouard 10985: printinggnuplot(fileresu, optionfilefiname,ageminpar,agemaxpar,fage, prevfcast, backcast, pathc,p);
1.220 brouard 10986: }
10987: printinghtml(fileresu,title,datafile, firstpass, lastpass, stepm, weightopt, \
1.225 brouard 10988: model,imx,jmin,jmax,jmean,rfileres,popforecast,prevfcast,backcast, estepm, \
10989: jprev1,mprev1,anprev1,dateprev1,jprev2,mprev2,anprev2,dateprev2);
1.220 brouard 10990:
1.225 brouard 10991: /*------------ free_vector -------------*/
10992: /* chdir(path); */
1.220 brouard 10993:
1.215 brouard 10994: /* free_ivector(wav,1,imx); */ /* Moved after last prevalence call */
10995: /* free_imatrix(dh,1,lastpass-firstpass+2,1,imx); */
10996: /* free_imatrix(bh,1,lastpass-firstpass+2,1,imx); */
10997: /* free_imatrix(mw,1,lastpass-firstpass+2,1,imx); */
1.126 brouard 10998: free_lvector(num,1,n);
10999: free_vector(agedc,1,n);
11000: /*free_matrix(covar,0,NCOVMAX,1,n);*/
11001: /*free_matrix(covar,1,NCOVMAX,1,n);*/
11002: fclose(ficparo);
11003: fclose(ficres);
1.220 brouard 11004:
11005:
1.186 brouard 11006: /* Other results (useful)*/
1.220 brouard 11007:
11008:
1.126 brouard 11009: /*--------------- Prevalence limit (period or stable prevalence) --------------*/
1.180 brouard 11010: /*#include "prevlim.h"*/ /* Use ficrespl, ficlog */
11011: prlim=matrix(1,nlstate,1,nlstate);
1.209 brouard 11012: prevalence_limit(p, prlim, ageminpar, agemaxpar, ftolpl, &ncvyear);
1.126 brouard 11013: fclose(ficrespl);
11014:
11015: /*------------- h Pij x at various ages ------------*/
1.180 brouard 11016: /*#include "hpijx.h"*/
11017: hPijx(p, bage, fage);
1.145 brouard 11018: fclose(ficrespij);
1.227 brouard 11019:
1.220 brouard 11020: /* ncovcombmax= pow(2,cptcoveff); */
1.219 brouard 11021: /*-------------- Variance of one-step probabilities---*/
1.145 brouard 11022: k=1;
1.126 brouard 11023: varprob(optionfilefiname, matcov, p, delti, nlstate, bage, fage,k,Tvar,nbcode, ncodemax,strstart);
1.227 brouard 11024:
1.219 brouard 11025: /* Prevalence for each covariates in probs[age][status][cov] */
1.218 brouard 11026: probs= ma3x(1,AGESUP,1,nlstate+ndeath, 1,ncovcombmax);
1.126 brouard 11027: for(i=1;i<=AGESUP;i++)
1.219 brouard 11028: for(j=1;j<=nlstate+ndeath;j++) /* ndeath is useless but a necessity to be compared with mobaverages */
1.225 brouard 11029: for(k=1;k<=ncovcombmax;k++)
11030: probs[i][j][k]=0.;
1.219 brouard 11031: prevalence(probs, ageminpar, agemaxpar, s, agev, nlstate, imx, Tvar, nbcode, ncodemax, mint, anint, dateprev1, dateprev2, firstpass, lastpass);
11032: if (mobilav!=0 ||mobilavproj !=0 ) {
11033: mobaverages= ma3x(1, AGESUP,1,nlstate+ndeath, 1,ncovcombmax);
1.227 brouard 11034: for(i=1;i<=AGESUP;i++)
11035: for(j=1;j<=nlstate;j++)
11036: for(k=1;k<=ncovcombmax;k++)
11037: mobaverages[i][j][k]=0.;
1.219 brouard 11038: mobaverage=mobaverages;
11039: if (mobilav!=0) {
1.235 brouard 11040: printf("Movingaveraging observed prevalence\n");
1.227 brouard 11041: if (movingaverage(probs, ageminpar, agemaxpar, mobaverage, mobilav)!=0){
11042: fprintf(ficlog," Error in movingaverage mobilav=%d\n",mobilav);
11043: printf(" Error in movingaverage mobilav=%d\n",mobilav);
11044: }
1.219 brouard 11045: }
11046: /* /\* Prevalence for each covariates in probs[age][status][cov] *\/ */
11047: /* prevalence(probs, ageminpar, agemaxpar, s, agev, nlstate, imx, Tvar, nbcode, ncodemax, mint, anint, dateprev1, dateprev2, firstpass, lastpass); */
11048: else if (mobilavproj !=0) {
1.235 brouard 11049: printf("Movingaveraging projected observed prevalence\n");
1.227 brouard 11050: if (movingaverage(probs, ageminpar, agemaxpar, mobaverage, mobilavproj)!=0){
11051: fprintf(ficlog," Error in movingaverage mobilavproj=%d\n",mobilavproj);
11052: printf(" Error in movingaverage mobilavproj=%d\n",mobilavproj);
11053: }
1.219 brouard 11054: }
11055: }/* end if moving average */
1.227 brouard 11056:
1.126 brouard 11057: /*---------- Forecasting ------------------*/
11058: /*if((stepm == 1) && (strcmp(model,".")==0)){*/
11059: if(prevfcast==1){
11060: /* if(stepm ==1){*/
1.225 brouard 11061: prevforecast(fileresu, anproj1, mproj1, jproj1, agemin, agemax, dateprev1, dateprev2, mobilavproj, bage, fage, firstpass, lastpass, anproj2, p, cptcoveff);
1.126 brouard 11062: }
1.217 brouard 11063: if(backcast==1){
1.219 brouard 11064: ddnewms=matrix(1,nlstate+ndeath,1,nlstate+ndeath);
11065: ddoldms=matrix(1,nlstate+ndeath,1,nlstate+ndeath);
11066: ddsavms=matrix(1,nlstate+ndeath,1,nlstate+ndeath);
11067:
11068: /*--------------- Back Prevalence limit (period or stable prevalence) --------------*/
11069:
11070: bprlim=matrix(1,nlstate,1,nlstate);
11071: back_prevalence_limit(p, bprlim, ageminpar, agemaxpar, ftolpl, &ncvyear, dateprev1, dateprev2, firstpass, lastpass, mobilavproj);
11072: fclose(ficresplb);
11073:
1.222 brouard 11074: hBijx(p, bage, fage, mobaverage);
11075: fclose(ficrespijb);
1.219 brouard 11076: free_matrix(bprlim,1,nlstate,1,nlstate); /*here or after loop ? */
11077:
11078: /* prevbackforecast(fileresu, anback1, mback1, jback1, agemin, agemax, dateprev1, dateprev2, mobilavproj,
1.225 brouard 11079: bage, fage, firstpass, lastpass, anback2, p, cptcoveff); */
1.219 brouard 11080: free_matrix(ddnewms, 1, nlstate+ndeath, 1, nlstate+ndeath);
11081: free_matrix(ddsavms, 1, nlstate+ndeath, 1, nlstate+ndeath);
11082: free_matrix(ddoldms, 1, nlstate+ndeath, 1, nlstate+ndeath);
11083: }
1.217 brouard 11084:
1.186 brouard 11085:
11086: /* ------ Other prevalence ratios------------ */
1.126 brouard 11087:
1.215 brouard 11088: free_ivector(wav,1,imx);
11089: free_imatrix(dh,1,lastpass-firstpass+2,1,imx);
11090: free_imatrix(bh,1,lastpass-firstpass+2,1,imx);
11091: free_imatrix(mw,1,lastpass-firstpass+2,1,imx);
1.218 brouard 11092:
11093:
1.127 brouard 11094: /*---------- Health expectancies, no variances ------------*/
1.218 brouard 11095:
1.201 brouard 11096: strcpy(filerese,"E_");
11097: strcat(filerese,fileresu);
1.126 brouard 11098: if((ficreseij=fopen(filerese,"w"))==NULL) {
11099: printf("Problem with Health Exp. resultfile: %s\n", filerese); exit(0);
11100: fprintf(ficlog,"Problem with Health Exp. resultfile: %s\n", filerese); exit(0);
11101: }
1.208 brouard 11102: printf("Computing Health Expectancies: result on file '%s' ...", filerese);fflush(stdout);
11103: fprintf(ficlog,"Computing Health Expectancies: result on file '%s' ...", filerese);fflush(ficlog);
1.238 brouard 11104:
11105: pstamp(ficreseij);
1.219 brouard 11106:
1.235 brouard 11107: i1=pow(2,cptcoveff); /* Number of combination of dummy covariates */
11108: if (cptcovn < 1){i1=1;}
11109:
11110: for(nres=1; nres <= nresult; nres++) /* For each resultline */
11111: for(k=1; k<=i1;k++){ /* For any combination of dummy covariates, fixed and varying */
11112: if(TKresult[nres]!= k)
11113: continue;
1.219 brouard 11114: fprintf(ficreseij,"\n#****** ");
1.235 brouard 11115: printf("\n#****** ");
1.225 brouard 11116: for(j=1;j<=cptcoveff;j++) {
1.227 brouard 11117: fprintf(ficreseij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
1.235 brouard 11118: printf("V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
11119: }
11120: for (j=1; j<= nsq; j++){ /* For each selected (single) quantitative value */
11121: printf(" V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
11122: fprintf(ficreseij," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
1.219 brouard 11123: }
11124: fprintf(ficreseij,"******\n");
1.235 brouard 11125: printf("******\n");
1.219 brouard 11126:
11127: eij=ma3x(1,nlstate,1,nlstate,(int) bage, (int) fage);
11128: oldm=oldms;savm=savms;
1.235 brouard 11129: evsij(eij, p, nlstate, stepm, (int) bage, (int)fage, oldm, savm, k, estepm, strstart, nres);
1.127 brouard 11130:
1.219 brouard 11131: free_ma3x(eij,1,nlstate,1,nlstate,(int) bage, (int)fage);
1.127 brouard 11132: }
11133: fclose(ficreseij);
1.208 brouard 11134: printf("done evsij\n");fflush(stdout);
11135: fprintf(ficlog,"done evsij\n");fflush(ficlog);
1.218 brouard 11136:
1.227 brouard 11137: /*---------- State-specific expectancies and variances ------------*/
1.218 brouard 11138:
11139:
1.201 brouard 11140: strcpy(filerest,"T_");
11141: strcat(filerest,fileresu);
1.127 brouard 11142: if((ficrest=fopen(filerest,"w"))==NULL) {
11143: printf("Problem with total LE resultfile: %s\n", filerest);goto end;
11144: fprintf(ficlog,"Problem with total LE resultfile: %s\n", filerest);goto end;
11145: }
1.208 brouard 11146: printf("Computing Total Life expectancies with their standard errors: file '%s' ...\n", filerest); fflush(stdout);
11147: fprintf(ficlog,"Computing Total Life expectancies with their standard errors: file '%s' ...\n", filerest); fflush(ficlog);
1.218 brouard 11148:
1.126 brouard 11149:
1.201 brouard 11150: strcpy(fileresstde,"STDE_");
11151: strcat(fileresstde,fileresu);
1.126 brouard 11152: if((ficresstdeij=fopen(fileresstde,"w"))==NULL) {
1.227 brouard 11153: printf("Problem with State specific Exp. and std errors resultfile: %s\n", fileresstde); exit(0);
11154: fprintf(ficlog,"Problem with State specific Exp. and std errors resultfile: %s\n", fileresstde); exit(0);
1.126 brouard 11155: }
1.227 brouard 11156: printf(" Computing State-specific Expectancies and standard errors: result on file '%s' \n", fileresstde);
11157: fprintf(ficlog," Computing State-specific Expectancies and standard errors: result on file '%s' \n", fileresstde);
1.126 brouard 11158:
1.201 brouard 11159: strcpy(filerescve,"CVE_");
11160: strcat(filerescve,fileresu);
1.126 brouard 11161: if((ficrescveij=fopen(filerescve,"w"))==NULL) {
1.227 brouard 11162: printf("Problem with Covar. State-specific Exp. resultfile: %s\n", filerescve); exit(0);
11163: fprintf(ficlog,"Problem with Covar. State-specific Exp. resultfile: %s\n", filerescve); exit(0);
1.126 brouard 11164: }
1.227 brouard 11165: printf(" Computing Covar. of State-specific Expectancies: result on file '%s' \n", filerescve);
11166: fprintf(ficlog," Computing Covar. of State-specific Expectancies: result on file '%s' \n", filerescve);
1.126 brouard 11167:
1.201 brouard 11168: strcpy(fileresv,"V_");
11169: strcat(fileresv,fileresu);
1.126 brouard 11170: if((ficresvij=fopen(fileresv,"w"))==NULL) {
11171: printf("Problem with variance resultfile: %s\n", fileresv);exit(0);
11172: fprintf(ficlog,"Problem with variance resultfile: %s\n", fileresv);exit(0);
11173: }
1.227 brouard 11174: printf(" Computing Variance-covariance of State-specific Expectancies: file '%s' ... ", fileresv);fflush(stdout);
11175: fprintf(ficlog," Computing Variance-covariance of State-specific Expectancies: file '%s' ... ", fileresv);fflush(ficlog);
1.126 brouard 11176:
1.145 brouard 11177: /*for(cptcov=1,k=0;cptcov<=i1;cptcov++){
11178: for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){*/
11179:
1.235 brouard 11180: i1=pow(2,cptcoveff); /* Number of combination of dummy covariates */
11181: if (cptcovn < 1){i1=1;}
11182:
11183: for(nres=1; nres <= nresult; nres++) /* For each resultline */
11184: for(k=1; k<=i1;k++){ /* For any combination of dummy covariates, fixed and varying */
11185: if(TKresult[nres]!= k)
11186: continue;
1.242 brouard 11187: printf("\n#****** Result for:");
11188: fprintf(ficrest,"\n#****** Result for:");
11189: fprintf(ficlog,"\n#****** Result for:");
1.227 brouard 11190: for(j=1;j<=cptcoveff;j++){
11191: printf("V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
11192: fprintf(ficrest,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
11193: fprintf(ficlog,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
11194: }
1.235 brouard 11195: for (j=1; j<= nsq; j++){ /* For each selected (single) quantitative value */
11196: printf(" V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
11197: fprintf(ficrest," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
11198: fprintf(ficlog," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
11199: }
1.208 brouard 11200: fprintf(ficrest,"******\n");
1.227 brouard 11201: fprintf(ficlog,"******\n");
11202: printf("******\n");
1.208 brouard 11203:
11204: fprintf(ficresstdeij,"\n#****** ");
11205: fprintf(ficrescveij,"\n#****** ");
1.225 brouard 11206: for(j=1;j<=cptcoveff;j++) {
1.227 brouard 11207: fprintf(ficresstdeij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
11208: fprintf(ficrescveij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
1.208 brouard 11209: }
1.235 brouard 11210: for (j=1; j<= nsq; j++){ /* For each selected (single) quantitative value */
11211: fprintf(ficresstdeij," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
11212: fprintf(ficrescveij," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
11213: }
1.208 brouard 11214: fprintf(ficresstdeij,"******\n");
11215: fprintf(ficrescveij,"******\n");
11216:
11217: fprintf(ficresvij,"\n#****** ");
1.238 brouard 11218: /* pstamp(ficresvij); */
1.225 brouard 11219: for(j=1;j<=cptcoveff;j++)
1.227 brouard 11220: fprintf(ficresvij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
1.235 brouard 11221: for (j=1; j<= nsq; j++){ /* For each selected (single) quantitative value */
11222: fprintf(ficresvij," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
11223: }
1.208 brouard 11224: fprintf(ficresvij,"******\n");
11225:
11226: eij=ma3x(1,nlstate,1,nlstate,(int) bage, (int) fage);
11227: oldm=oldms;savm=savms;
1.235 brouard 11228: printf(" cvevsij ");
11229: fprintf(ficlog, " cvevsij ");
11230: cvevsij(eij, p, nlstate, stepm, (int) bage, (int)fage, oldm, savm, k, estepm, delti, matcov, strstart, nres);
1.208 brouard 11231: printf(" end cvevsij \n ");
11232: fprintf(ficlog, " end cvevsij \n ");
11233:
11234: /*
11235: */
11236: /* goto endfree; */
11237:
11238: vareij=ma3x(1,nlstate,1,nlstate,(int) bage, (int) fage);
11239: pstamp(ficrest);
11240:
11241:
11242: for(vpopbased=0; vpopbased <= popbased; vpopbased++){ /* Done for vpopbased=0 and vpopbased=1 if popbased==1*/
1.227 brouard 11243: oldm=oldms;savm=savms; /* ZZ Segmentation fault */
11244: cptcod= 0; /* To be deleted */
11245: printf("varevsij vpopbased=%d \n",vpopbased);
11246: fprintf(ficlog, "varevsij vpopbased=%d \n",vpopbased);
1.235 brouard 11247: 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 11248: 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 ");
11249: if(vpopbased==1)
11250: 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);
11251: else
11252: fprintf(ficrest,"the age specific period (stable) prevalences in each health state \n");
11253: fprintf(ficrest,"# Age popbased mobilav e.. (std) ");
11254: for (i=1;i<=nlstate;i++) fprintf(ficrest,"e.%d (std) ",i);
11255: fprintf(ficrest,"\n");
11256: /* printf("Which p?\n"); for(i=1;i<=npar;i++)printf("p[i=%d]=%lf,",i,p[i]);printf("\n"); */
11257: epj=vector(1,nlstate+1);
11258: printf("Computing age specific period (stable) prevalences in each health state \n");
11259: fprintf(ficlog,"Computing age specific period (stable) prevalences in each health state \n");
11260: for(age=bage; age <=fage ;age++){
1.235 brouard 11261: prevalim(prlim, nlstate, p, age, oldm, savm, ftolpl, &ncvyear, k, nres); /*ZZ Is it the correct prevalim */
1.227 brouard 11262: if (vpopbased==1) {
11263: if(mobilav ==0){
11264: for(i=1; i<=nlstate;i++)
11265: prlim[i][i]=probs[(int)age][i][k];
11266: }else{ /* mobilav */
11267: for(i=1; i<=nlstate;i++)
11268: prlim[i][i]=mobaverage[(int)age][i][k];
11269: }
11270: }
1.219 brouard 11271:
1.227 brouard 11272: fprintf(ficrest," %4.0f %d %d",age, vpopbased, mobilav);
11273: /* fprintf(ficrest," %4.0f %d %d %d %d",age, vpopbased, mobilav,Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]); */ /* to be done */
11274: /* printf(" age %4.0f ",age); */
11275: for(j=1, epj[nlstate+1]=0.;j <=nlstate;j++){
11276: for(i=1, epj[j]=0.;i <=nlstate;i++) {
11277: epj[j] += prlim[i][i]*eij[i][j][(int)age];
11278: /*ZZZ printf("%lf %lf ", prlim[i][i] ,eij[i][j][(int)age]);*/
11279: /* printf("%lf %lf ", prlim[i][i] ,eij[i][j][(int)age]); */
11280: }
11281: epj[nlstate+1] +=epj[j];
11282: }
11283: /* printf(" age %4.0f \n",age); */
1.219 brouard 11284:
1.227 brouard 11285: for(i=1, vepp=0.;i <=nlstate;i++)
11286: for(j=1;j <=nlstate;j++)
11287: vepp += vareij[i][j][(int)age];
11288: fprintf(ficrest," %7.3f (%7.3f)", epj[nlstate+1],sqrt(vepp));
11289: for(j=1;j <=nlstate;j++){
11290: fprintf(ficrest," %7.3f (%7.3f)", epj[j],sqrt(vareij[j][j][(int)age]));
11291: }
11292: fprintf(ficrest,"\n");
11293: }
1.208 brouard 11294: } /* End vpopbased */
11295: free_ma3x(eij,1,nlstate,1,nlstate,(int) bage, (int)fage);
11296: free_ma3x(vareij,1,nlstate,1,nlstate,(int) bage, (int)fage);
11297: free_vector(epj,1,nlstate+1);
1.235 brouard 11298: printf("done selection\n");fflush(stdout);
11299: fprintf(ficlog,"done selection\n");fflush(ficlog);
1.208 brouard 11300:
1.145 brouard 11301: /*}*/
1.235 brouard 11302: } /* End k selection */
1.227 brouard 11303:
11304: printf("done State-specific expectancies\n");fflush(stdout);
11305: fprintf(ficlog,"done State-specific expectancies\n");fflush(ficlog);
11306:
1.126 brouard 11307: /*------- Variance of period (stable) prevalence------*/
1.227 brouard 11308:
1.201 brouard 11309: strcpy(fileresvpl,"VPL_");
11310: strcat(fileresvpl,fileresu);
1.126 brouard 11311: if((ficresvpl=fopen(fileresvpl,"w"))==NULL) {
11312: printf("Problem with variance of period (stable) prevalence resultfile: %s\n", fileresvpl);
11313: exit(0);
11314: }
1.208 brouard 11315: printf("Computing Variance-covariance of period (stable) prevalence: file '%s' ...", fileresvpl);fflush(stdout);
11316: fprintf(ficlog, "Computing Variance-covariance of period (stable) prevalence: file '%s' ...", fileresvpl);fflush(ficlog);
1.227 brouard 11317:
1.145 brouard 11318: /*for(cptcov=1,k=0;cptcov<=i1;cptcov++){
11319: for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){*/
1.227 brouard 11320:
1.235 brouard 11321: i1=pow(2,cptcoveff);
11322: if (cptcovn < 1){i1=1;}
11323:
11324: for(nres=1; nres <= nresult; nres++) /* For each resultline */
11325: for(k=1; k<=i1;k++){
11326: if(TKresult[nres]!= k)
11327: continue;
1.227 brouard 11328: fprintf(ficresvpl,"\n#****** ");
11329: printf("\n#****** ");
11330: fprintf(ficlog,"\n#****** ");
11331: for(j=1;j<=cptcoveff;j++) {
11332: fprintf(ficresvpl,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
11333: fprintf(ficlog,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
11334: printf("V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
11335: }
1.235 brouard 11336: for (j=1; j<= nsq; j++){ /* For each selected (single) quantitative value */
11337: printf(" V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
11338: fprintf(ficresvpl," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
11339: fprintf(ficlog," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
11340: }
1.227 brouard 11341: fprintf(ficresvpl,"******\n");
11342: printf("******\n");
11343: fprintf(ficlog,"******\n");
11344:
11345: varpl=matrix(1,nlstate,(int) bage, (int) fage);
11346: oldm=oldms;savm=savms;
1.235 brouard 11347: varprevlim(fileres, varpl, matcov, p, delti, nlstate, stepm, (int) bage, (int) fage, oldm, savm, prlim, ftolpl, &ncvyear, k, strstart, nres);
1.227 brouard 11348: free_matrix(varpl,1,nlstate,(int) bage, (int)fage);
1.145 brouard 11349: /*}*/
1.126 brouard 11350: }
1.227 brouard 11351:
1.126 brouard 11352: fclose(ficresvpl);
1.208 brouard 11353: printf("done variance-covariance of period prevalence\n");fflush(stdout);
11354: fprintf(ficlog,"done variance-covariance of period prevalence\n");fflush(ficlog);
1.227 brouard 11355:
11356: free_vector(weight,1,n);
11357: free_imatrix(Tvard,1,NCOVMAX,1,2);
11358: free_imatrix(s,1,maxwav+1,1,n);
11359: free_matrix(anint,1,maxwav,1,n);
11360: free_matrix(mint,1,maxwav,1,n);
11361: free_ivector(cod,1,n);
11362: free_ivector(tab,1,NCOVMAX);
11363: fclose(ficresstdeij);
11364: fclose(ficrescveij);
11365: fclose(ficresvij);
11366: fclose(ficrest);
11367: fclose(ficpar);
11368:
11369:
1.126 brouard 11370: /*---------- End : free ----------------*/
1.219 brouard 11371: if (mobilav!=0 ||mobilavproj !=0)
11372: 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 11373: free_ma3x(probs,1,AGESUP,1,nlstate+ndeath, 1,ncovcombmax);
1.220 brouard 11374: free_matrix(prlim,1,nlstate,1,nlstate); /*here or after loop ? */
11375: free_matrix(pmmij,1,nlstate+ndeath,1,nlstate+ndeath);
1.126 brouard 11376: } /* mle==-3 arrives here for freeing */
1.227 brouard 11377: /* endfree:*/
11378: free_matrix(oldms, 1,nlstate+ndeath,1,nlstate+ndeath);
11379: free_matrix(newms, 1,nlstate+ndeath,1,nlstate+ndeath);
11380: free_matrix(savms, 1,nlstate+ndeath,1,nlstate+ndeath);
11381: free_ma3x(cotqvar,1,maxwav,1,nqtv,1,n);
1.233 brouard 11382: free_ma3x(cotvar,1,maxwav,1,ntv+nqtv,1,n);
1.227 brouard 11383: free_matrix(coqvar,1,maxwav,1,n);
11384: free_matrix(covar,0,NCOVMAX,1,n);
11385: free_matrix(matcov,1,npar,1,npar);
11386: free_matrix(hess,1,npar,1,npar);
11387: /*free_vector(delti,1,npar);*/
11388: free_ma3x(delti3,1,nlstate,1, nlstate+ndeath-1,1,ncovmodel);
11389: free_matrix(agev,1,maxwav,1,imx);
11390: free_ma3x(param,1,nlstate,1, nlstate+ndeath-1,1,ncovmodel);
11391:
11392: free_ivector(ncodemax,1,NCOVMAX);
11393: free_ivector(ncodemaxwundef,1,NCOVMAX);
11394: free_ivector(Dummy,-1,NCOVMAX);
11395: free_ivector(Fixed,-1,NCOVMAX);
1.238 brouard 11396: free_ivector(DummyV,1,NCOVMAX);
11397: free_ivector(FixedV,1,NCOVMAX);
1.227 brouard 11398: free_ivector(Typevar,-1,NCOVMAX);
11399: free_ivector(Tvar,1,NCOVMAX);
1.234 brouard 11400: free_ivector(TvarsQ,1,NCOVMAX);
11401: free_ivector(TvarsQind,1,NCOVMAX);
11402: free_ivector(TvarsD,1,NCOVMAX);
11403: free_ivector(TvarsDind,1,NCOVMAX);
1.231 brouard 11404: free_ivector(TvarFD,1,NCOVMAX);
11405: free_ivector(TvarFDind,1,NCOVMAX);
1.232 brouard 11406: free_ivector(TvarF,1,NCOVMAX);
11407: free_ivector(TvarFind,1,NCOVMAX);
11408: free_ivector(TvarV,1,NCOVMAX);
11409: free_ivector(TvarVind,1,NCOVMAX);
11410: free_ivector(TvarA,1,NCOVMAX);
11411: free_ivector(TvarAind,1,NCOVMAX);
1.231 brouard 11412: free_ivector(TvarFQ,1,NCOVMAX);
11413: free_ivector(TvarFQind,1,NCOVMAX);
11414: free_ivector(TvarVD,1,NCOVMAX);
11415: free_ivector(TvarVDind,1,NCOVMAX);
11416: free_ivector(TvarVQ,1,NCOVMAX);
11417: free_ivector(TvarVQind,1,NCOVMAX);
1.230 brouard 11418: free_ivector(Tvarsel,1,NCOVMAX);
11419: free_vector(Tvalsel,1,NCOVMAX);
1.227 brouard 11420: free_ivector(Tposprod,1,NCOVMAX);
11421: free_ivector(Tprod,1,NCOVMAX);
11422: free_ivector(Tvaraff,1,NCOVMAX);
11423: free_ivector(invalidvarcomb,1,ncovcombmax);
11424: free_ivector(Tage,1,NCOVMAX);
11425: free_ivector(Tmodelind,1,NCOVMAX);
1.228 brouard 11426: free_ivector(TmodelInvind,1,NCOVMAX);
11427: free_ivector(TmodelInvQind,1,NCOVMAX);
1.227 brouard 11428:
11429: free_imatrix(nbcode,0,NCOVMAX,0,NCOVMAX);
11430: /* free_imatrix(codtab,1,100,1,10); */
1.126 brouard 11431: fflush(fichtm);
11432: fflush(ficgp);
11433:
1.227 brouard 11434:
1.126 brouard 11435: if((nberr >0) || (nbwarn>0)){
1.216 brouard 11436: printf("End of Imach with %d errors and/or %d warnings. Please look at the log file for details.\n",nberr,nbwarn);
11437: 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 11438: }else{
11439: printf("End of Imach\n");
11440: fprintf(ficlog,"End of Imach\n");
11441: }
11442: printf("See log file on %s\n",filelog);
11443: /* gettimeofday(&end_time, (struct timezone*)0);*/ /* after time */
1.157 brouard 11444: /*(void) gettimeofday(&end_time,&tzp);*/
11445: rend_time = time(NULL);
11446: end_time = *localtime(&rend_time);
11447: /* tml = *localtime(&end_time.tm_sec); */
11448: strcpy(strtend,asctime(&end_time));
1.126 brouard 11449: printf("Local time at start %s\nLocal time at end %s",strstart, strtend);
11450: fprintf(ficlog,"Local time at start %s\nLocal time at end %s\n",strstart, strtend);
1.157 brouard 11451: printf("Total time used %s\n", asc_diff_time(rend_time -rstart_time,tmpout));
1.227 brouard 11452:
1.157 brouard 11453: printf("Total time was %.0lf Sec.\n", difftime(rend_time,rstart_time));
11454: fprintf(ficlog,"Total time used %s\n", asc_diff_time(rend_time -rstart_time,tmpout));
11455: fprintf(ficlog,"Total time was %.0lf Sec.\n", difftime(rend_time,rstart_time));
1.126 brouard 11456: /* printf("Total time was %d uSec.\n", total_usecs);*/
11457: /* if(fileappend(fichtm,optionfilehtm)){ */
11458: fprintf(fichtm,"<br>Local time at start %s<br>Local time at end %s<br>\n</body></html>",strstart, strtend);
11459: fclose(fichtm);
11460: fprintf(fichtmcov,"<br>Local time at start %s<br>Local time at end %s<br>\n</body></html>",strstart, strtend);
11461: fclose(fichtmcov);
11462: fclose(ficgp);
11463: fclose(ficlog);
11464: /*------ End -----------*/
1.227 brouard 11465:
11466:
11467: printf("Before Current directory %s!\n",pathcd);
1.184 brouard 11468: #ifdef WIN32
1.227 brouard 11469: if (_chdir(pathcd) != 0)
11470: printf("Can't move to directory %s!\n",path);
11471: if(_getcwd(pathcd,MAXLINE) > 0)
1.184 brouard 11472: #else
1.227 brouard 11473: if(chdir(pathcd) != 0)
11474: printf("Can't move to directory %s!\n", path);
11475: if (getcwd(pathcd, MAXLINE) > 0)
1.184 brouard 11476: #endif
1.126 brouard 11477: printf("Current directory %s!\n",pathcd);
11478: /*strcat(plotcmd,CHARSEPARATOR);*/
11479: sprintf(plotcmd,"gnuplot");
1.157 brouard 11480: #ifdef _WIN32
1.126 brouard 11481: sprintf(plotcmd,"\"%sgnuplot.exe\"",pathimach);
11482: #endif
11483: if(!stat(plotcmd,&info)){
1.158 brouard 11484: printf("Error or gnuplot program not found: '%s'\n",plotcmd);fflush(stdout);
1.126 brouard 11485: if(!stat(getenv("GNUPLOTBIN"),&info)){
1.158 brouard 11486: printf("Error or gnuplot program not found: '%s' Environment GNUPLOTBIN not set.\n",plotcmd);fflush(stdout);
1.126 brouard 11487: }else
11488: strcpy(pplotcmd,plotcmd);
1.157 brouard 11489: #ifdef __unix
1.126 brouard 11490: strcpy(plotcmd,GNUPLOTPROGRAM);
11491: if(!stat(plotcmd,&info)){
1.158 brouard 11492: printf("Error gnuplot program not found: '%s'\n",plotcmd);fflush(stdout);
1.126 brouard 11493: }else
11494: strcpy(pplotcmd,plotcmd);
11495: #endif
11496: }else
11497: strcpy(pplotcmd,plotcmd);
11498:
11499: sprintf(plotcmd,"%s %s",pplotcmd, optionfilegnuplot);
1.158 brouard 11500: printf("Starting graphs with: '%s'\n",plotcmd);fflush(stdout);
1.227 brouard 11501:
1.126 brouard 11502: if((outcmd=system(plotcmd)) != 0){
1.158 brouard 11503: printf("gnuplot command might not be in your path: '%s', err=%d\n", plotcmd, outcmd);
1.154 brouard 11504: printf("\n Trying if gnuplot resides on the same directory that IMaCh\n");
1.152 brouard 11505: sprintf(plotcmd,"%sgnuplot %s", pathimach, optionfilegnuplot);
1.150 brouard 11506: if((outcmd=system(plotcmd)) != 0)
1.153 brouard 11507: printf("\n Still a problem with gnuplot command %s, err=%d\n", plotcmd, outcmd);
1.126 brouard 11508: }
1.158 brouard 11509: printf(" Successful, please wait...");
1.126 brouard 11510: while (z[0] != 'q') {
11511: /* chdir(path); */
1.154 brouard 11512: printf("\nType e to edit results with your browser, g to graph again and q for exit: ");
1.126 brouard 11513: scanf("%s",z);
11514: /* if (z[0] == 'c') system("./imach"); */
11515: if (z[0] == 'e') {
1.158 brouard 11516: #ifdef __APPLE__
1.152 brouard 11517: sprintf(pplotcmd, "open %s", optionfilehtm);
1.157 brouard 11518: #elif __linux
11519: sprintf(pplotcmd, "xdg-open %s", optionfilehtm);
1.153 brouard 11520: #else
1.152 brouard 11521: sprintf(pplotcmd, "%s", optionfilehtm);
1.153 brouard 11522: #endif
11523: printf("Starting browser with: %s",pplotcmd);fflush(stdout);
11524: system(pplotcmd);
1.126 brouard 11525: }
11526: else if (z[0] == 'g') system(plotcmd);
11527: else if (z[0] == 'q') exit(0);
11528: }
1.227 brouard 11529: end:
1.126 brouard 11530: while (z[0] != 'q') {
1.195 brouard 11531: printf("\nType q for exiting: "); fflush(stdout);
1.126 brouard 11532: scanf("%s",z);
11533: }
11534: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>