Annotation of imach/src/imach.c, revision 1.234
1.234 ! brouard 1: /* $Id: imach.c,v 1.233 2016/08/23 07:40:50 brouard Exp $
1.126 brouard 2: $State: Exp $
1.163 brouard 3: $Log: imach.c,v $
1.234 ! brouard 4: Revision 1.233 2016/08/23 07:40:50 brouard
! 5: Summary: not working
! 6:
1.233 brouard 7: Revision 1.232 2016/08/22 14:20:21 brouard
8: Summary: not working
9:
1.232 brouard 10: Revision 1.231 2016/08/22 07:17:15 brouard
11: Summary: not working
12:
1.231 brouard 13: Revision 1.230 2016/08/22 06:55:53 brouard
14: Summary: Not working
15:
1.230 brouard 16: Revision 1.229 2016/07/23 09:45:53 brouard
17: Summary: Completing for func too
18:
1.229 brouard 19: Revision 1.228 2016/07/22 17:45:30 brouard
20: Summary: Fixing some arrays, still debugging
21:
1.227 brouard 22: Revision 1.226 2016/07/12 18:42:34 brouard
23: Summary: temp
24:
1.226 brouard 25: Revision 1.225 2016/07/12 08:40:03 brouard
26: Summary: saving but not running
27:
1.225 brouard 28: Revision 1.224 2016/07/01 13:16:01 brouard
29: Summary: Fixes
30:
1.224 brouard 31: Revision 1.223 2016/02/19 09:23:35 brouard
32: Summary: temporary
33:
1.223 brouard 34: Revision 1.222 2016/02/17 08:14:50 brouard
35: Summary: Probably last 0.98 stable version 0.98r6
36:
1.222 brouard 37: Revision 1.221 2016/02/15 23:35:36 brouard
38: Summary: minor bug
39:
1.220 brouard 40: Revision 1.219 2016/02/15 00:48:12 brouard
41: *** empty log message ***
42:
1.219 brouard 43: Revision 1.218 2016/02/12 11:29:23 brouard
44: Summary: 0.99 Back projections
45:
1.218 brouard 46: Revision 1.217 2015/12/23 17:18:31 brouard
47: Summary: Experimental backcast
48:
1.217 brouard 49: Revision 1.216 2015/12/18 17:32:11 brouard
50: Summary: 0.98r4 Warning and status=-2
51:
52: Version 0.98r4 is now:
53: - displaying an error when status is -1, date of interview unknown and date of death known;
54: - permitting a status -2 when the vital status is unknown at a known date of right truncation.
55: Older changes concerning s=-2, dating from 2005 have been supersed.
56:
1.216 brouard 57: Revision 1.215 2015/12/16 08:52:24 brouard
58: Summary: 0.98r4 working
59:
1.215 brouard 60: Revision 1.214 2015/12/16 06:57:54 brouard
61: Summary: temporary not working
62:
1.214 brouard 63: Revision 1.213 2015/12/11 18:22:17 brouard
64: Summary: 0.98r4
65:
1.213 brouard 66: Revision 1.212 2015/11/21 12:47:24 brouard
67: Summary: minor typo
68:
1.212 brouard 69: Revision 1.211 2015/11/21 12:41:11 brouard
70: Summary: 0.98r3 with some graph of projected cross-sectional
71:
72: Author: Nicolas Brouard
73:
1.211 brouard 74: Revision 1.210 2015/11/18 17:41:20 brouard
75: Summary: Start working on projected prevalences
76:
1.210 brouard 77: Revision 1.209 2015/11/17 22:12:03 brouard
78: Summary: Adding ftolpl parameter
79: Author: N Brouard
80:
81: We had difficulties to get smoothed confidence intervals. It was due
82: to the period prevalence which wasn't computed accurately. The inner
83: parameter ftolpl is now an outer parameter of the .imach parameter
84: file after estepm. If ftolpl is small 1.e-4 and estepm too,
85: computation are long.
86:
1.209 brouard 87: Revision 1.208 2015/11/17 14:31:57 brouard
88: Summary: temporary
89:
1.208 brouard 90: Revision 1.207 2015/10/27 17:36:57 brouard
91: *** empty log message ***
92:
1.207 brouard 93: Revision 1.206 2015/10/24 07:14:11 brouard
94: *** empty log message ***
95:
1.206 brouard 96: Revision 1.205 2015/10/23 15:50:53 brouard
97: Summary: 0.98r3 some clarification for graphs on likelihood contributions
98:
1.205 brouard 99: Revision 1.204 2015/10/01 16:20:26 brouard
100: Summary: Some new graphs of contribution to likelihood
101:
1.204 brouard 102: Revision 1.203 2015/09/30 17:45:14 brouard
103: Summary: looking at better estimation of the hessian
104:
105: Also a better criteria for convergence to the period prevalence And
106: therefore adding the number of years needed to converge. (The
107: prevalence in any alive state shold sum to one
108:
1.203 brouard 109: Revision 1.202 2015/09/22 19:45:16 brouard
110: Summary: Adding some overall graph on contribution to likelihood. Might change
111:
1.202 brouard 112: Revision 1.201 2015/09/15 17:34:58 brouard
113: Summary: 0.98r0
114:
115: - Some new graphs like suvival functions
116: - Some bugs fixed like model=1+age+V2.
117:
1.201 brouard 118: Revision 1.200 2015/09/09 16:53:55 brouard
119: Summary: Big bug thanks to Flavia
120:
121: Even model=1+age+V2. did not work anymore
122:
1.200 brouard 123: Revision 1.199 2015/09/07 14:09:23 brouard
124: Summary: 0.98q6 changing default small png format for graph to vectorized svg.
125:
1.199 brouard 126: Revision 1.198 2015/09/03 07:14:39 brouard
127: Summary: 0.98q5 Flavia
128:
1.198 brouard 129: Revision 1.197 2015/09/01 18:24:39 brouard
130: *** empty log message ***
131:
1.197 brouard 132: Revision 1.196 2015/08/18 23:17:52 brouard
133: Summary: 0.98q5
134:
1.196 brouard 135: Revision 1.195 2015/08/18 16:28:39 brouard
136: Summary: Adding a hack for testing purpose
137:
138: After reading the title, ftol and model lines, if the comment line has
139: a q, starting with #q, the answer at the end of the run is quit. It
140: permits to run test files in batch with ctest. The former workaround was
141: $ echo q | imach foo.imach
142:
1.195 brouard 143: Revision 1.194 2015/08/18 13:32:00 brouard
144: Summary: Adding error when the covariance matrix doesn't contain the exact number of lines required by the model line.
145:
1.194 brouard 146: Revision 1.193 2015/08/04 07:17:42 brouard
147: Summary: 0.98q4
148:
1.193 brouard 149: Revision 1.192 2015/07/16 16:49:02 brouard
150: Summary: Fixing some outputs
151:
1.192 brouard 152: Revision 1.191 2015/07/14 10:00:33 brouard
153: Summary: Some fixes
154:
1.191 brouard 155: Revision 1.190 2015/05/05 08:51:13 brouard
156: Summary: Adding digits in output parameters (7 digits instead of 6)
157:
158: Fix 1+age+.
159:
1.190 brouard 160: Revision 1.189 2015/04/30 14:45:16 brouard
161: Summary: 0.98q2
162:
1.189 brouard 163: Revision 1.188 2015/04/30 08:27:53 brouard
164: *** empty log message ***
165:
1.188 brouard 166: Revision 1.187 2015/04/29 09:11:15 brouard
167: *** empty log message ***
168:
1.187 brouard 169: Revision 1.186 2015/04/23 12:01:52 brouard
170: Summary: V1*age is working now, version 0.98q1
171:
172: Some codes had been disabled in order to simplify and Vn*age was
173: working in the optimization phase, ie, giving correct MLE parameters,
174: but, as usual, outputs were not correct and program core dumped.
175:
1.186 brouard 176: Revision 1.185 2015/03/11 13:26:42 brouard
177: Summary: Inclusion of compile and links command line for Intel Compiler
178:
1.185 brouard 179: Revision 1.184 2015/03/11 11:52:39 brouard
180: Summary: Back from Windows 8. Intel Compiler
181:
1.184 brouard 182: Revision 1.183 2015/03/10 20:34:32 brouard
183: Summary: 0.98q0, trying with directest, mnbrak fixed
184:
185: We use directest instead of original Powell test; probably no
186: incidence on the results, but better justifications;
187: We fixed Numerical Recipes mnbrak routine which was wrong and gave
188: wrong results.
189:
1.183 brouard 190: Revision 1.182 2015/02/12 08:19:57 brouard
191: Summary: Trying to keep directest which seems simpler and more general
192: Author: Nicolas Brouard
193:
1.182 brouard 194: Revision 1.181 2015/02/11 23:22:24 brouard
195: Summary: Comments on Powell added
196:
197: Author:
198:
1.181 brouard 199: Revision 1.180 2015/02/11 17:33:45 brouard
200: Summary: Finishing move from main to function (hpijx and prevalence_limit)
201:
1.180 brouard 202: Revision 1.179 2015/01/04 09:57:06 brouard
203: Summary: back to OS/X
204:
1.179 brouard 205: Revision 1.178 2015/01/04 09:35:48 brouard
206: *** empty log message ***
207:
1.178 brouard 208: Revision 1.177 2015/01/03 18:40:56 brouard
209: Summary: Still testing ilc32 on OSX
210:
1.177 brouard 211: Revision 1.176 2015/01/03 16:45:04 brouard
212: *** empty log message ***
213:
1.176 brouard 214: Revision 1.175 2015/01/03 16:33:42 brouard
215: *** empty log message ***
216:
1.175 brouard 217: Revision 1.174 2015/01/03 16:15:49 brouard
218: Summary: Still in cross-compilation
219:
1.174 brouard 220: Revision 1.173 2015/01/03 12:06:26 brouard
221: Summary: trying to detect cross-compilation
222:
1.173 brouard 223: Revision 1.172 2014/12/27 12:07:47 brouard
224: Summary: Back from Visual Studio and Intel, options for compiling for Windows XP
225:
1.172 brouard 226: Revision 1.171 2014/12/23 13:26:59 brouard
227: Summary: Back from Visual C
228:
229: Still problem with utsname.h on Windows
230:
1.171 brouard 231: Revision 1.170 2014/12/23 11:17:12 brouard
232: Summary: Cleaning some \%% back to %%
233:
234: The escape was mandatory for a specific compiler (which one?), but too many warnings.
235:
1.170 brouard 236: Revision 1.169 2014/12/22 23:08:31 brouard
237: Summary: 0.98p
238:
239: Outputs some informations on compiler used, OS etc. Testing on different platforms.
240:
1.169 brouard 241: Revision 1.168 2014/12/22 15:17:42 brouard
1.170 brouard 242: Summary: update
1.169 brouard 243:
1.168 brouard 244: Revision 1.167 2014/12/22 13:50:56 brouard
245: Summary: Testing uname and compiler version and if compiled 32 or 64
246:
247: Testing on Linux 64
248:
1.167 brouard 249: Revision 1.166 2014/12/22 11:40:47 brouard
250: *** empty log message ***
251:
1.166 brouard 252: Revision 1.165 2014/12/16 11:20:36 brouard
253: Summary: After compiling on Visual C
254:
255: * imach.c (Module): Merging 1.61 to 1.162
256:
1.165 brouard 257: Revision 1.164 2014/12/16 10:52:11 brouard
258: Summary: Merging with Visual C after suppressing some warnings for unused variables. Also fixing Saito's bug 0.98Xn
259:
260: * imach.c (Module): Merging 1.61 to 1.162
261:
1.164 brouard 262: Revision 1.163 2014/12/16 10:30:11 brouard
263: * imach.c (Module): Merging 1.61 to 1.162
264:
1.163 brouard 265: Revision 1.162 2014/09/25 11:43:39 brouard
266: Summary: temporary backup 0.99!
267:
1.162 brouard 268: Revision 1.1 2014/09/16 11:06:58 brouard
269: Summary: With some code (wrong) for nlopt
270:
271: Author:
272:
273: Revision 1.161 2014/09/15 20:41:41 brouard
274: Summary: Problem with macro SQR on Intel compiler
275:
1.161 brouard 276: Revision 1.160 2014/09/02 09:24:05 brouard
277: *** empty log message ***
278:
1.160 brouard 279: Revision 1.159 2014/09/01 10:34:10 brouard
280: Summary: WIN32
281: Author: Brouard
282:
1.159 brouard 283: Revision 1.158 2014/08/27 17:11:51 brouard
284: *** empty log message ***
285:
1.158 brouard 286: Revision 1.157 2014/08/27 16:26:55 brouard
287: Summary: Preparing windows Visual studio version
288: Author: Brouard
289:
290: In order to compile on Visual studio, time.h is now correct and time_t
291: and tm struct should be used. difftime should be used but sometimes I
292: just make the differences in raw time format (time(&now).
293: Trying to suppress #ifdef LINUX
294: Add xdg-open for __linux in order to open default browser.
295:
1.157 brouard 296: Revision 1.156 2014/08/25 20:10:10 brouard
297: *** empty log message ***
298:
1.156 brouard 299: Revision 1.155 2014/08/25 18:32:34 brouard
300: Summary: New compile, minor changes
301: Author: Brouard
302:
1.155 brouard 303: Revision 1.154 2014/06/20 17:32:08 brouard
304: Summary: Outputs now all graphs of convergence to period prevalence
305:
1.154 brouard 306: Revision 1.153 2014/06/20 16:45:46 brouard
307: Summary: If 3 live state, convergence to period prevalence on same graph
308: Author: Brouard
309:
1.153 brouard 310: Revision 1.152 2014/06/18 17:54:09 brouard
311: Summary: open browser, use gnuplot on same dir than imach if not found in the path
312:
1.152 brouard 313: Revision 1.151 2014/06/18 16:43:30 brouard
314: *** empty log message ***
315:
1.151 brouard 316: Revision 1.150 2014/06/18 16:42:35 brouard
317: Summary: If gnuplot is not in the path try on same directory than imach binary (OSX)
318: Author: brouard
319:
1.150 brouard 320: Revision 1.149 2014/06/18 15:51:14 brouard
321: Summary: Some fixes in parameter files errors
322: Author: Nicolas Brouard
323:
1.149 brouard 324: Revision 1.148 2014/06/17 17:38:48 brouard
325: Summary: Nothing new
326: Author: Brouard
327:
328: Just a new packaging for OS/X version 0.98nS
329:
1.148 brouard 330: Revision 1.147 2014/06/16 10:33:11 brouard
331: *** empty log message ***
332:
1.147 brouard 333: Revision 1.146 2014/06/16 10:20:28 brouard
334: Summary: Merge
335: Author: Brouard
336:
337: Merge, before building revised version.
338:
1.146 brouard 339: Revision 1.145 2014/06/10 21:23:15 brouard
340: Summary: Debugging with valgrind
341: Author: Nicolas Brouard
342:
343: Lot of changes in order to output the results with some covariates
344: After the Edimburgh REVES conference 2014, it seems mandatory to
345: improve the code.
346: No more memory valgrind error but a lot has to be done in order to
347: continue the work of splitting the code into subroutines.
348: Also, decodemodel has been improved. Tricode is still not
349: optimal. nbcode should be improved. Documentation has been added in
350: the source code.
351:
1.144 brouard 352: Revision 1.143 2014/01/26 09:45:38 brouard
353: Summary: Version 0.98nR (to be improved, but gives same optimization results as 0.98k. Nice, promising
354:
355: * imach.c (Module): Trying to merge old staffs together while being at Tokyo. Not tested...
356: (Module): Version 0.98nR Running ok, but output format still only works for three covariates.
357:
1.143 brouard 358: Revision 1.142 2014/01/26 03:57:36 brouard
359: Summary: gnuplot changed plot w l 1 has to be changed to plot w l lt 2
360:
361: * imach.c (Module): Trying to merge old staffs together while being at Tokyo. Not tested...
362:
1.142 brouard 363: Revision 1.141 2014/01/26 02:42:01 brouard
364: * imach.c (Module): Trying to merge old staffs together while being at Tokyo. Not tested...
365:
1.141 brouard 366: Revision 1.140 2011/09/02 10:37:54 brouard
367: Summary: times.h is ok with mingw32 now.
368:
1.140 brouard 369: Revision 1.139 2010/06/14 07:50:17 brouard
370: After the theft of my laptop, I probably lost some lines of codes which were not uploaded to the CVS tree.
371: I remember having already fixed agemin agemax which are pointers now but not cvs saved.
372:
1.139 brouard 373: Revision 1.138 2010/04/30 18:19:40 brouard
374: *** empty log message ***
375:
1.138 brouard 376: Revision 1.137 2010/04/29 18:11:38 brouard
377: (Module): Checking covariates for more complex models
378: than V1+V2. A lot of change to be done. Unstable.
379:
1.137 brouard 380: Revision 1.136 2010/04/26 20:30:53 brouard
381: (Module): merging some libgsl code. Fixing computation
382: of likelione (using inter/intrapolation if mle = 0) in order to
383: get same likelihood as if mle=1.
384: Some cleaning of code and comments added.
385:
1.136 brouard 386: Revision 1.135 2009/10/29 15:33:14 brouard
387: (Module): Now imach stops if date of birth, at least year of birth, is not given. Some cleaning of the code.
388:
1.135 brouard 389: Revision 1.134 2009/10/29 13:18:53 brouard
390: (Module): Now imach stops if date of birth, at least year of birth, is not given. Some cleaning of the code.
391:
1.134 brouard 392: Revision 1.133 2009/07/06 10:21:25 brouard
393: just nforces
394:
1.133 brouard 395: Revision 1.132 2009/07/06 08:22:05 brouard
396: Many tings
397:
1.132 brouard 398: Revision 1.131 2009/06/20 16:22:47 brouard
399: Some dimensions resccaled
400:
1.131 brouard 401: Revision 1.130 2009/05/26 06:44:34 brouard
402: (Module): Max Covariate is now set to 20 instead of 8. A
403: lot of cleaning with variables initialized to 0. Trying to make
404: V2+V3*age+V1+V4 strb=V3*age+V1+V4 working better.
405:
1.130 brouard 406: Revision 1.129 2007/08/31 13:49:27 lievre
407: Modification of the way of exiting when the covariate is not binary in order to see on the window the error message before exiting
408:
1.129 lievre 409: Revision 1.128 2006/06/30 13:02:05 brouard
410: (Module): Clarifications on computing e.j
411:
1.128 brouard 412: Revision 1.127 2006/04/28 18:11:50 brouard
413: (Module): Yes the sum of survivors was wrong since
414: imach-114 because nhstepm was no more computed in the age
415: loop. Now we define nhstepma in the age loop.
416: (Module): In order to speed up (in case of numerous covariates) we
417: compute health expectancies (without variances) in a first step
418: and then all the health expectancies with variances or standard
419: deviation (needs data from the Hessian matrices) which slows the
420: computation.
421: In the future we should be able to stop the program is only health
422: expectancies and graph are needed without standard deviations.
423:
1.127 brouard 424: Revision 1.126 2006/04/28 17:23:28 brouard
425: (Module): Yes the sum of survivors was wrong since
426: imach-114 because nhstepm was no more computed in the age
427: loop. Now we define nhstepma in the age loop.
428: Version 0.98h
429:
1.126 brouard 430: Revision 1.125 2006/04/04 15:20:31 lievre
431: Errors in calculation of health expectancies. Age was not initialized.
432: Forecasting file added.
433:
434: Revision 1.124 2006/03/22 17:13:53 lievre
435: Parameters are printed with %lf instead of %f (more numbers after the comma).
436: The log-likelihood is printed in the log file
437:
438: Revision 1.123 2006/03/20 10:52:43 brouard
439: * imach.c (Module): <title> changed, corresponds to .htm file
440: name. <head> headers where missing.
441:
442: * imach.c (Module): Weights can have a decimal point as for
443: English (a comma might work with a correct LC_NUMERIC environment,
444: otherwise the weight is truncated).
445: Modification of warning when the covariates values are not 0 or
446: 1.
447: Version 0.98g
448:
449: Revision 1.122 2006/03/20 09:45:41 brouard
450: (Module): Weights can have a decimal point as for
451: English (a comma might work with a correct LC_NUMERIC environment,
452: otherwise the weight is truncated).
453: Modification of warning when the covariates values are not 0 or
454: 1.
455: Version 0.98g
456:
457: Revision 1.121 2006/03/16 17:45:01 lievre
458: * imach.c (Module): Comments concerning covariates added
459:
460: * imach.c (Module): refinements in the computation of lli if
461: status=-2 in order to have more reliable computation if stepm is
462: not 1 month. Version 0.98f
463:
464: Revision 1.120 2006/03/16 15:10:38 lievre
465: (Module): refinements in the computation of lli if
466: status=-2 in order to have more reliable computation if stepm is
467: not 1 month. Version 0.98f
468:
469: Revision 1.119 2006/03/15 17:42:26 brouard
470: (Module): Bug if status = -2, the loglikelihood was
471: computed as likelihood omitting the logarithm. Version O.98e
472:
473: Revision 1.118 2006/03/14 18:20:07 brouard
474: (Module): varevsij Comments added explaining the second
475: table of variances if popbased=1 .
476: (Module): Covariances of eij, ekl added, graphs fixed, new html link.
477: (Module): Function pstamp added
478: (Module): Version 0.98d
479:
480: Revision 1.117 2006/03/14 17:16:22 brouard
481: (Module): varevsij Comments added explaining the second
482: table of variances if popbased=1 .
483: (Module): Covariances of eij, ekl added, graphs fixed, new html link.
484: (Module): Function pstamp added
485: (Module): Version 0.98d
486:
487: Revision 1.116 2006/03/06 10:29:27 brouard
488: (Module): Variance-covariance wrong links and
489: varian-covariance of ej. is needed (Saito).
490:
491: Revision 1.115 2006/02/27 12:17:45 brouard
492: (Module): One freematrix added in mlikeli! 0.98c
493:
494: Revision 1.114 2006/02/26 12:57:58 brouard
495: (Module): Some improvements in processing parameter
496: filename with strsep.
497:
498: Revision 1.113 2006/02/24 14:20:24 brouard
499: (Module): Memory leaks checks with valgrind and:
500: datafile was not closed, some imatrix were not freed and on matrix
501: allocation too.
502:
503: Revision 1.112 2006/01/30 09:55:26 brouard
504: (Module): Back to gnuplot.exe instead of wgnuplot.exe
505:
506: Revision 1.111 2006/01/25 20:38:18 brouard
507: (Module): Lots of cleaning and bugs added (Gompertz)
508: (Module): Comments can be added in data file. Missing date values
509: can be a simple dot '.'.
510:
511: Revision 1.110 2006/01/25 00:51:50 brouard
512: (Module): Lots of cleaning and bugs added (Gompertz)
513:
514: Revision 1.109 2006/01/24 19:37:15 brouard
515: (Module): Comments (lines starting with a #) are allowed in data.
516:
517: Revision 1.108 2006/01/19 18:05:42 lievre
518: Gnuplot problem appeared...
519: To be fixed
520:
521: Revision 1.107 2006/01/19 16:20:37 brouard
522: Test existence of gnuplot in imach path
523:
524: Revision 1.106 2006/01/19 13:24:36 brouard
525: Some cleaning and links added in html output
526:
527: Revision 1.105 2006/01/05 20:23:19 lievre
528: *** empty log message ***
529:
530: Revision 1.104 2005/09/30 16:11:43 lievre
531: (Module): sump fixed, loop imx fixed, and simplifications.
532: (Module): If the status is missing at the last wave but we know
533: that the person is alive, then we can code his/her status as -2
534: (instead of missing=-1 in earlier versions) and his/her
535: contributions to the likelihood is 1 - Prob of dying from last
536: health status (= 1-p13= p11+p12 in the easiest case of somebody in
537: the healthy state at last known wave). Version is 0.98
538:
539: Revision 1.103 2005/09/30 15:54:49 lievre
540: (Module): sump fixed, loop imx fixed, and simplifications.
541:
542: Revision 1.102 2004/09/15 17:31:30 brouard
543: Add the possibility to read data file including tab characters.
544:
545: Revision 1.101 2004/09/15 10:38:38 brouard
546: Fix on curr_time
547:
548: Revision 1.100 2004/07/12 18:29:06 brouard
549: Add version for Mac OS X. Just define UNIX in Makefile
550:
551: Revision 1.99 2004/06/05 08:57:40 brouard
552: *** empty log message ***
553:
554: Revision 1.98 2004/05/16 15:05:56 brouard
555: New version 0.97 . First attempt to estimate force of mortality
556: directly from the data i.e. without the need of knowing the health
557: state at each age, but using a Gompertz model: log u =a + b*age .
558: This is the basic analysis of mortality and should be done before any
559: other analysis, in order to test if the mortality estimated from the
560: cross-longitudinal survey is different from the mortality estimated
561: from other sources like vital statistic data.
562:
563: The same imach parameter file can be used but the option for mle should be -3.
564:
1.133 brouard 565: Agnès, who wrote this part of the code, tried to keep most of the
1.126 brouard 566: former routines in order to include the new code within the former code.
567:
568: The output is very simple: only an estimate of the intercept and of
569: the slope with 95% confident intervals.
570:
571: Current limitations:
572: A) Even if you enter covariates, i.e. with the
573: model= V1+V2 equation for example, the programm does only estimate a unique global model without covariates.
574: B) There is no computation of Life Expectancy nor Life Table.
575:
576: Revision 1.97 2004/02/20 13:25:42 lievre
577: Version 0.96d. Population forecasting command line is (temporarily)
578: suppressed.
579:
580: Revision 1.96 2003/07/15 15:38:55 brouard
581: * imach.c (Repository): Errors in subdirf, 2, 3 while printing tmpout is
582: rewritten within the same printf. Workaround: many printfs.
583:
584: Revision 1.95 2003/07/08 07:54:34 brouard
585: * imach.c (Repository):
586: (Repository): Using imachwizard code to output a more meaningful covariance
587: matrix (cov(a12,c31) instead of numbers.
588:
589: Revision 1.94 2003/06/27 13:00:02 brouard
590: Just cleaning
591:
592: Revision 1.93 2003/06/25 16:33:55 brouard
593: (Module): On windows (cygwin) function asctime_r doesn't
594: exist so I changed back to asctime which exists.
595: (Module): Version 0.96b
596:
597: Revision 1.92 2003/06/25 16:30:45 brouard
598: (Module): On windows (cygwin) function asctime_r doesn't
599: exist so I changed back to asctime which exists.
600:
601: Revision 1.91 2003/06/25 15:30:29 brouard
602: * imach.c (Repository): Duplicated warning errors corrected.
603: (Repository): Elapsed time after each iteration is now output. It
604: helps to forecast when convergence will be reached. Elapsed time
605: is stamped in powell. We created a new html file for the graphs
606: concerning matrix of covariance. It has extension -cov.htm.
607:
608: Revision 1.90 2003/06/24 12:34:15 brouard
609: (Module): Some bugs corrected for windows. Also, when
610: mle=-1 a template is output in file "or"mypar.txt with the design
611: of the covariance matrix to be input.
612:
613: Revision 1.89 2003/06/24 12:30:52 brouard
614: (Module): Some bugs corrected for windows. Also, when
615: mle=-1 a template is output in file "or"mypar.txt with the design
616: of the covariance matrix to be input.
617:
618: Revision 1.88 2003/06/23 17:54:56 brouard
619: * 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.
620:
621: Revision 1.87 2003/06/18 12:26:01 brouard
622: Version 0.96
623:
624: Revision 1.86 2003/06/17 20:04:08 brouard
625: (Module): Change position of html and gnuplot routines and added
626: routine fileappend.
627:
628: Revision 1.85 2003/06/17 13:12:43 brouard
629: * imach.c (Repository): Check when date of death was earlier that
630: current date of interview. It may happen when the death was just
631: prior to the death. In this case, dh was negative and likelihood
632: was wrong (infinity). We still send an "Error" but patch by
633: assuming that the date of death was just one stepm after the
634: interview.
635: (Repository): Because some people have very long ID (first column)
636: we changed int to long in num[] and we added a new lvector for
637: memory allocation. But we also truncated to 8 characters (left
638: truncation)
639: (Repository): No more line truncation errors.
640:
641: Revision 1.84 2003/06/13 21:44:43 brouard
642: * imach.c (Repository): Replace "freqsummary" at a correct
643: place. It differs from routine "prevalence" which may be called
644: many times. Probs is memory consuming and must be used with
645: parcimony.
646: Version 0.95a3 (should output exactly the same maximization than 0.8a2)
647:
648: Revision 1.83 2003/06/10 13:39:11 lievre
649: *** empty log message ***
650:
651: Revision 1.82 2003/06/05 15:57:20 brouard
652: Add log in imach.c and fullversion number is now printed.
653:
654: */
655: /*
656: Interpolated Markov Chain
657:
658: Short summary of the programme:
659:
1.227 brouard 660: This program computes Healthy Life Expectancies or State-specific
661: (if states aren't health statuses) Expectancies from
662: cross-longitudinal data. Cross-longitudinal data consist in:
663:
664: -1- a first survey ("cross") where individuals from different ages
665: are interviewed on their health status or degree of disability (in
666: the case of a health survey which is our main interest)
667:
668: -2- at least a second wave of interviews ("longitudinal") which
669: measure each change (if any) in individual health status. Health
670: expectancies are computed from the time spent in each health state
671: according to a model. More health states you consider, more time is
672: necessary to reach the Maximum Likelihood of the parameters involved
673: in the model. The simplest model is the multinomial logistic model
674: where pij is the probability to be observed in state j at the second
675: wave conditional to be observed in state i at the first
676: wave. Therefore the model is: log(pij/pii)= aij + bij*age+ cij*sex +
677: etc , where 'age' is age and 'sex' is a covariate. If you want to
678: have a more complex model than "constant and age", you should modify
679: the program where the markup *Covariates have to be included here
680: again* invites you to do it. More covariates you add, slower the
1.126 brouard 681: convergence.
682:
683: The advantage of this computer programme, compared to a simple
684: multinomial logistic model, is clear when the delay between waves is not
685: identical for each individual. Also, if a individual missed an
686: intermediate interview, the information is lost, but taken into
687: account using an interpolation or extrapolation.
688:
689: hPijx is the probability to be observed in state i at age x+h
690: conditional to the observed state i at age x. The delay 'h' can be
691: split into an exact number (nh*stepm) of unobserved intermediate
692: states. This elementary transition (by month, quarter,
693: semester or year) is modelled as a multinomial logistic. The hPx
694: matrix is simply the matrix product of nh*stepm elementary matrices
695: and the contribution of each individual to the likelihood is simply
696: hPijx.
697:
698: Also this programme outputs the covariance matrix of the parameters but also
1.218 brouard 699: of the life expectancies. It also computes the period (stable) prevalence.
700:
701: Back prevalence and projections:
1.227 brouard 702:
703: - back_prevalence_limit(double *p, double **bprlim, double ageminpar,
704: double agemaxpar, double ftolpl, int *ncvyearp, double
705: dateprev1,double dateprev2, int firstpass, int lastpass, int
706: mobilavproj)
707:
708: Computes the back prevalence limit for any combination of
709: covariate values k at any age between ageminpar and agemaxpar and
710: returns it in **bprlim. In the loops,
711:
712: - **bprevalim(**bprlim, ***mobaverage, nlstate, *p, age, **oldm,
713: **savm, **dnewm, **doldm, **dsavm, ftolpl, ncvyearp, k);
714:
715: - hBijx Back Probability to be in state i at age x-h being in j at x
1.218 brouard 716: Computes for any combination of covariates k and any age between bage and fage
717: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
718: oldm=oldms;savm=savms;
1.227 brouard 719:
720: - hbxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k);
1.218 brouard 721: Computes the transition matrix starting at age 'age' over
722: 'nhstepm*hstepm*stepm' months (i.e. until
723: age (in years) age+nhstepm*hstepm*stepm/12) by multiplying
1.227 brouard 724: nhstepm*hstepm matrices.
725:
726: Returns p3mat[i][j][h] after calling
727: p3mat[i][j][h]=matprod2(newm,
728: bmij(pmmij,cov,ncovmodel,x,nlstate,prevacurrent, dnewm, doldm,
729: dsavm,ij),\ 1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath,
730: oldm);
1.226 brouard 731:
732: Important routines
733:
734: - func (or funcone), computes logit (pij) distinguishing
735: o fixed variables (single or product dummies or quantitative);
736: o varying variables by:
737: (1) wave (single, product dummies, quantitative),
738: (2) by age (can be month) age (done), age*age (done), age*Vn where Vn can be:
739: % fixed dummy (treated) or quantitative (not done because time-consuming);
740: % varying dummy (not done) or quantitative (not done);
741: - Tricode which tests the modality of dummy variables (in order to warn with wrong or empty modalities)
742: and returns the number of efficient covariates cptcoveff and modalities nbcode[Tvar[k]][1]= 0 and nbcode[Tvar[k]][2]= 1 usually.
743: - printinghtml which outputs results like life expectancy in and from a state for a combination of modalities of dummy variables
744: o There are 2*cptcoveff combinations of (0,1) for cptcoveff variables. Outputting only combinations with people, éliminating 1 1 if
745: race White (0 0), Black vs White (1 0), Hispanic (0 1) and 1 1 being meaningless.
1.218 brouard 746:
1.226 brouard 747:
748:
1.133 brouard 749: Authors: Nicolas Brouard (brouard@ined.fr) and Agnès Lièvre (lievre@ined.fr).
750: Institut national d'études démographiques, Paris.
1.126 brouard 751: This software have been partly granted by Euro-REVES, a concerted action
752: from the European Union.
753: It is copyrighted identically to a GNU software product, ie programme and
754: software can be distributed freely for non commercial use. Latest version
755: can be accessed at http://euroreves.ined.fr/imach .
756:
757: Help to debug: LD_PRELOAD=/usr/local/lib/libnjamd.so ./imach foo.imach
758: or better on gdb : set env LD_PRELOAD=/usr/local/lib/libnjamd.so
759:
760: **********************************************************************/
761: /*
762: main
763: read parameterfile
764: read datafile
765: concatwav
766: freqsummary
767: if (mle >= 1)
768: mlikeli
769: print results files
770: if mle==1
771: computes hessian
772: read end of parameter file: agemin, agemax, bage, fage, estepm
773: begin-prev-date,...
774: open gnuplot file
775: open html file
1.145 brouard 776: period (stable) prevalence | pl_nom 1-1 2-2 etc by covariate
777: for age prevalim() | #****** V1=0 V2=1 V3=1 V4=0 ******
778: | 65 1 0 2 1 3 1 4 0 0.96326 0.03674
779: freexexit2 possible for memory heap.
780:
781: h Pij x | pij_nom ficrestpij
782: # Cov Agex agex+h hpijx with i,j= 1-1 1-2 1-3 2-1 2-2 2-3
783: 1 85 85 1.00000 0.00000 0.00000 0.00000 1.00000 0.00000
784: 1 85 86 0.68299 0.22291 0.09410 0.71093 0.00000 0.28907
785:
786: 1 65 99 0.00364 0.00322 0.99314 0.00350 0.00310 0.99340
787: 1 65 100 0.00214 0.00204 0.99581 0.00206 0.00196 0.99597
788: variance of p one-step probabilities varprob | prob_nom ficresprob #One-step probabilities and stand. devi in ()
789: Standard deviation of one-step probabilities | probcor_nom ficresprobcor #One-step probabilities and correlation matrix
790: Matrix of variance covariance of one-step probabilities | probcov_nom ficresprobcov #One-step probabilities and covariance matrix
791:
1.126 brouard 792: forecasting if prevfcast==1 prevforecast call prevalence()
793: health expectancies
794: Variance-covariance of DFLE
795: prevalence()
796: movingaverage()
797: varevsij()
798: if popbased==1 varevsij(,popbased)
799: total life expectancies
800: Variance of period (stable) prevalence
801: end
802: */
803:
1.187 brouard 804: /* #define DEBUG */
805: /* #define DEBUGBRENT */
1.203 brouard 806: /* #define DEBUGLINMIN */
807: /* #define DEBUGHESS */
808: #define DEBUGHESSIJ
1.224 brouard 809: /* #define LINMINORIGINAL /\* Don't use loop on scale in linmin (accepting nan) *\/ */
1.165 brouard 810: #define POWELL /* Instead of NLOPT */
1.224 brouard 811: #define POWELLNOF3INFF1TEST /* Skip test */
1.186 brouard 812: /* #define POWELLORIGINAL /\* Don't use Directest to decide new direction but original Powell test *\/ */
813: /* #define MNBRAKORIGINAL /\* Don't use mnbrak fix *\/ */
1.126 brouard 814:
815: #include <math.h>
816: #include <stdio.h>
817: #include <stdlib.h>
818: #include <string.h>
1.226 brouard 819: #include <ctype.h>
1.159 brouard 820:
821: #ifdef _WIN32
822: #include <io.h>
1.172 brouard 823: #include <windows.h>
824: #include <tchar.h>
1.159 brouard 825: #else
1.126 brouard 826: #include <unistd.h>
1.159 brouard 827: #endif
1.126 brouard 828:
829: #include <limits.h>
830: #include <sys/types.h>
1.171 brouard 831:
832: #if defined(__GNUC__)
833: #include <sys/utsname.h> /* Doesn't work on Windows */
834: #endif
835:
1.126 brouard 836: #include <sys/stat.h>
837: #include <errno.h>
1.159 brouard 838: /* extern int errno; */
1.126 brouard 839:
1.157 brouard 840: /* #ifdef LINUX */
841: /* #include <time.h> */
842: /* #include "timeval.h" */
843: /* #else */
844: /* #include <sys/time.h> */
845: /* #endif */
846:
1.126 brouard 847: #include <time.h>
848:
1.136 brouard 849: #ifdef GSL
850: #include <gsl/gsl_errno.h>
851: #include <gsl/gsl_multimin.h>
852: #endif
853:
1.167 brouard 854:
1.162 brouard 855: #ifdef NLOPT
856: #include <nlopt.h>
857: typedef struct {
858: double (* function)(double [] );
859: } myfunc_data ;
860: #endif
861:
1.126 brouard 862: /* #include <libintl.h> */
863: /* #define _(String) gettext (String) */
864:
1.141 brouard 865: #define MAXLINE 1024 /* Was 256. Overflow with 312 with 2 states and 4 covariates. Should be ok */
1.126 brouard 866:
867: #define GNUPLOTPROGRAM "gnuplot"
868: /*#define GNUPLOTPROGRAM "..\\gp37mgw\\wgnuplot"*/
869: #define FILENAMELENGTH 132
870:
871: #define GLOCK_ERROR_NOPATH -1 /* empty path */
872: #define GLOCK_ERROR_GETCWD -2 /* cannot get cwd */
873:
1.144 brouard 874: #define MAXPARM 128 /**< Maximum number of parameters for the optimization */
875: #define NPARMAX 64 /**< (nlstate+ndeath-1)*nlstate*ncovmodel */
1.126 brouard 876:
877: #define NINTERVMAX 8
1.144 brouard 878: #define NLSTATEMAX 8 /**< Maximum number of live states (for func) */
879: #define NDEATHMAX 8 /**< Maximum number of dead states (for func) */
880: #define NCOVMAX 20 /**< Maximum number of covariates, including generated covariates V1*V2 */
1.197 brouard 881: #define codtabm(h,k) (1 & (h-1) >> (k-1))+1
1.211 brouard 882: /*#define decodtabm(h,k,cptcoveff)= (h <= (1<<cptcoveff)?(((h-1) >> (k-1)) & 1) +1 : -1)*/
883: #define decodtabm(h,k,cptcoveff) (((h-1) >> (k-1)) & 1) +1
1.126 brouard 884: #define MAXN 20000
1.144 brouard 885: #define YEARM 12. /**< Number of months per year */
1.218 brouard 886: /* #define AGESUP 130 */
887: #define AGESUP 150
888: #define AGEMARGE 25 /* Marge for agemin and agemax for(iage=agemin-AGEMARGE; iage <= agemax+3+AGEMARGE; iage++) */
1.126 brouard 889: #define AGEBASE 40
1.194 brouard 890: #define AGEOVERFLOW 1.e20
1.164 brouard 891: #define AGEGOMP 10 /**< Minimal age for Gompertz adjustment */
1.157 brouard 892: #ifdef _WIN32
893: #define DIRSEPARATOR '\\'
894: #define CHARSEPARATOR "\\"
895: #define ODIRSEPARATOR '/'
896: #else
1.126 brouard 897: #define DIRSEPARATOR '/'
898: #define CHARSEPARATOR "/"
899: #define ODIRSEPARATOR '\\'
900: #endif
901:
1.234 ! brouard 902: /* $Id: imach.c,v 1.233 2016/08/23 07:40:50 brouard Exp $ */
1.126 brouard 903: /* $State: Exp $ */
1.196 brouard 904: #include "version.h"
905: char version[]=__IMACH_VERSION__;
1.224 brouard 906: 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.234 ! brouard 907: char fullversion[]="$Revision: 1.233 $ $Date: 2016/08/23 07:40:50 $";
1.126 brouard 908: char strstart[80];
909: char optionfilext[10], optionfilefiname[FILENAMELENGTH];
1.130 brouard 910: int erreur=0, nberr=0, nbwarn=0; /* Error number, number of errors number of warnings */
1.187 brouard 911: int nagesqr=0, nforce=0; /* nagesqr=1 if model is including age*age, number of forces */
1.145 brouard 912: /* Number of covariates model=V2+V1+ V3*age+V2*V4 */
913: int cptcovn=0; /**< cptcovn number of covariates added in the model (excepting constant and age and age*product) */
914: int cptcovt=0; /**< cptcovt number of covariates added in the model (excepting constant and age) */
1.225 brouard 915: int cptcovs=0; /**< cptcovs number of simple covariates in the model V2+V1 =2 */
916: int cptcovsnq=0; /**< cptcovsnq number of simple covariates in the model but non quantitative V2+V1 =2 */
1.145 brouard 917: int cptcovage=0; /**< Number of covariates with age: V3*age only =1 */
918: int cptcovprodnoage=0; /**< Number of covariate products without age */
919: int cptcoveff=0; /* Total number of covariates to vary for printing results */
1.233 brouard 920: int ncovf=0; /* Total number of effective fixed covariates (dummy or quantitative) in the model */
921: int ncovv=0; /* Total number of effective (wave) varying covariates (dummy or quantitative) in the model */
1.232 brouard 922: int ncova=0; /* Total number of effective (wave and stepm) varying with age covariates (dummy of quantitative) in the model */
1.234 ! brouard 923: int nsd=0; /**< Total number of single dummy variables (output) */
! 924: int nsq=0; /**< Total number of single quantitative variables (output) */
1.232 brouard 925: int ncoveff=0; /* Total number of effective fixed dummy covariates in the model */
1.225 brouard 926: int nqfveff=0; /**< nqfveff Number of Quantitative Fixed Variables Effective */
1.224 brouard 927: int ntveff=0; /**< ntveff number of effective time varying variables */
928: int nqtveff=0; /**< ntqveff number of effective time varying quantitative variables */
1.145 brouard 929: int cptcov=0; /* Working variable */
1.218 brouard 930: int ncovcombmax=NCOVMAX; /* Maximum calculated number of covariate combination = pow(2, cptcoveff) */
1.126 brouard 931: int npar=NPARMAX;
932: int nlstate=2; /* Number of live states */
933: int ndeath=1; /* Number of dead states */
1.130 brouard 934: int ncovmodel=0, ncovcol=0; /* Total number of covariables including constant a12*1 +b12*x ncovmodel=2 */
1.223 brouard 935: int nqv=0, ntv=0, nqtv=0; /* Total number of quantitative variables, time variable (dummy), quantitative and time variable */
1.126 brouard 936: int popbased=0;
937:
938: int *wav; /* Number of waves for this individuual 0 is possible */
1.130 brouard 939: int maxwav=0; /* Maxim number of waves */
940: int jmin=0, jmax=0; /* min, max spacing between 2 waves */
941: int ijmin=0, ijmax=0; /* Individuals having jmin and jmax */
942: int gipmx=0, gsw=0; /* Global variables on the number of contributions
1.126 brouard 943: to the likelihood and the sum of weights (done by funcone)*/
1.130 brouard 944: int mle=1, weightopt=0;
1.126 brouard 945: int **mw; /* mw[mi][i] is number of the mi wave for this individual */
946: int **dh; /* dh[mi][i] is number of steps between mi,mi+1 for this individual */
947: int **bh; /* bh[mi][i] is the bias (+ or -) for this individual if the delay between
948: * wave mi and wave mi+1 is not an exact multiple of stepm. */
1.162 brouard 949: int countcallfunc=0; /* Count the number of calls to func */
1.230 brouard 950: int selected(int kvar); /* Is covariate kvar selected for printing results */
951:
1.130 brouard 952: double jmean=1; /* Mean space between 2 waves */
1.145 brouard 953: double **matprod2(); /* test */
1.126 brouard 954: double **oldm, **newm, **savm; /* Working pointers to matrices */
955: double **oldms, **newms, **savms; /* Fixed working pointers to matrices */
1.218 brouard 956: double **ddnewms, **ddoldms, **ddsavms; /* for freeing later */
957:
1.136 brouard 958: /*FILE *fic ; */ /* Used in readdata only */
1.217 brouard 959: FILE *ficpar, *ficparo,*ficres, *ficresp, *ficresphtm, *ficresphtmfr, *ficrespl, *ficresplb,*ficrespij, *ficrespijb, *ficrest,*ficresf, *ficresfb,*ficrespop;
1.126 brouard 960: FILE *ficlog, *ficrespow;
1.130 brouard 961: int globpr=0; /* Global variable for printing or not */
1.126 brouard 962: double fretone; /* Only one call to likelihood */
1.130 brouard 963: long ipmx=0; /* Number of contributions */
1.126 brouard 964: double sw; /* Sum of weights */
965: char filerespow[FILENAMELENGTH];
966: char fileresilk[FILENAMELENGTH]; /* File of individual contributions to the likelihood */
967: FILE *ficresilk;
968: FILE *ficgp,*ficresprob,*ficpop, *ficresprobcov, *ficresprobcor;
969: FILE *ficresprobmorprev;
970: FILE *fichtm, *fichtmcov; /* Html File */
971: FILE *ficreseij;
972: char filerese[FILENAMELENGTH];
973: FILE *ficresstdeij;
974: char fileresstde[FILENAMELENGTH];
975: FILE *ficrescveij;
976: char filerescve[FILENAMELENGTH];
977: FILE *ficresvij;
978: char fileresv[FILENAMELENGTH];
979: FILE *ficresvpl;
980: char fileresvpl[FILENAMELENGTH];
981: char title[MAXLINE];
1.234 ! brouard 982: char model[MAXLINE]; /**< The model line */
1.217 brouard 983: char optionfile[FILENAMELENGTH], datafile[FILENAMELENGTH], filerespl[FILENAMELENGTH], fileresplb[FILENAMELENGTH];
1.126 brouard 984: char plotcmd[FILENAMELENGTH], pplotcmd[FILENAMELENGTH];
985: char tmpout[FILENAMELENGTH], tmpout2[FILENAMELENGTH];
986: char command[FILENAMELENGTH];
987: int outcmd=0;
988:
1.217 brouard 989: char fileres[FILENAMELENGTH], filerespij[FILENAMELENGTH], filerespijb[FILENAMELENGTH], filereso[FILENAMELENGTH], rfileres[FILENAMELENGTH];
1.202 brouard 990: char fileresu[FILENAMELENGTH]; /* fileres without r in front */
1.126 brouard 991: char filelog[FILENAMELENGTH]; /* Log file */
992: char filerest[FILENAMELENGTH];
993: char fileregp[FILENAMELENGTH];
994: char popfile[FILENAMELENGTH];
995:
996: char optionfilegnuplot[FILENAMELENGTH], optionfilehtm[FILENAMELENGTH], optionfilehtmcov[FILENAMELENGTH] ;
997:
1.157 brouard 998: /* struct timeval start_time, end_time, curr_time, last_time, forecast_time; */
999: /* struct timezone tzp; */
1000: /* extern int gettimeofday(); */
1001: struct tm tml, *gmtime(), *localtime();
1002:
1003: extern time_t time();
1004:
1005: struct tm start_time, end_time, curr_time, last_time, forecast_time;
1006: time_t rstart_time, rend_time, rcurr_time, rlast_time, rforecast_time; /* raw time */
1007: struct tm tm;
1008:
1.126 brouard 1009: char strcurr[80], strfor[80];
1010:
1011: char *endptr;
1012: long lval;
1013: double dval;
1014:
1015: #define NR_END 1
1016: #define FREE_ARG char*
1017: #define FTOL 1.0e-10
1018:
1019: #define NRANSI
1020: #define ITMAX 200
1021:
1022: #define TOL 2.0e-4
1023:
1024: #define CGOLD 0.3819660
1025: #define ZEPS 1.0e-10
1026: #define SHFT(a,b,c,d) (a)=(b);(b)=(c);(c)=(d);
1027:
1028: #define GOLD 1.618034
1029: #define GLIMIT 100.0
1030: #define TINY 1.0e-20
1031:
1032: static double maxarg1,maxarg2;
1033: #define FMAX(a,b) (maxarg1=(a),maxarg2=(b),(maxarg1)>(maxarg2)? (maxarg1):(maxarg2))
1034: #define FMIN(a,b) (maxarg1=(a),maxarg2=(b),(maxarg1)<(maxarg2)? (maxarg1):(maxarg2))
1035:
1036: #define SIGN(a,b) ((b)>0.0 ? fabs(a) : -fabs(a))
1037: #define rint(a) floor(a+0.5)
1.166 brouard 1038: /* http://www.thphys.uni-heidelberg.de/~robbers/cmbeasy/doc/html/myutils_8h-source.html */
1.183 brouard 1039: #define mytinydouble 1.0e-16
1.166 brouard 1040: /* #define DEQUAL(a,b) (fabs((a)-(b))<mytinydouble) */
1041: /* http://www.thphys.uni-heidelberg.de/~robbers/cmbeasy/doc/html/mynrutils_8h-source.html */
1042: /* static double dsqrarg; */
1043: /* #define DSQR(a) (DEQUAL((dsqrarg=(a)),0.0) ? 0.0 : dsqrarg*dsqrarg) */
1.126 brouard 1044: static double sqrarg;
1045: #define SQR(a) ((sqrarg=(a)) == 0.0 ? 0.0 :sqrarg*sqrarg)
1046: #define SWAP(a,b) {temp=(a);(a)=(b);(b)=temp;}
1047: int agegomp= AGEGOMP;
1048:
1049: int imx;
1050: int stepm=1;
1051: /* Stepm, step in month: minimum step interpolation*/
1052:
1053: int estepm;
1054: /* Estepm, step in month to interpolate survival function in order to approximate Life Expectancy*/
1055:
1056: int m,nb;
1057: long *num;
1.197 brouard 1058: int firstpass=0, lastpass=4,*cod, *cens;
1.192 brouard 1059: int *ncodemax; /* ncodemax[j]= Number of modalities of the j th
1060: covariate for which somebody answered excluding
1061: undefined. Usually 2: 0 and 1. */
1062: int *ncodemaxwundef; /* ncodemax[j]= Number of modalities of the j th
1063: covariate for which somebody answered including
1064: undefined. Usually 3: -1, 0 and 1. */
1.126 brouard 1065: double **agev,*moisnais, *annais, *moisdc, *andc,**mint, **anint;
1.218 brouard 1066: double **pmmij, ***probs; /* Global pointer */
1.219 brouard 1067: double ***mobaverage, ***mobaverages; /* New global variable */
1.126 brouard 1068: double *ageexmed,*agecens;
1069: double dateintmean=0;
1070:
1071: double *weight;
1072: int **s; /* Status */
1.141 brouard 1073: double *agedc;
1.145 brouard 1074: double **covar; /**< covar[j,i], value of jth covariate for individual i,
1.141 brouard 1075: * covar=matrix(0,NCOVMAX,1,n);
1.187 brouard 1076: * cov[Tage[kk]+2]=covar[Tvar[Tage[kk]]][i]*age; */
1.225 brouard 1077: double **coqvar; /* Fixed quantitative covariate iqv */
1078: double ***cotvar; /* Time varying covariate itv */
1079: double ***cotqvar; /* Time varying quantitative covariate itqv */
1.141 brouard 1080: double idx;
1081: int **nbcode, *Tvar; /**< model=V2 => Tvar[1]= 2 */
1.234 ! brouard 1082: /* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
! 1083: /*k 1 2 3 4 5 6 7 8 9 */
! 1084: /*Tvar[k]= 5 4 3 6 5 2 7 1 1 */
! 1085: /* Tndvar[k] 1 2 3 4 5 */
! 1086: /*TDvar 4 3 6 7 1 */ /* For outputs only; combination of dummies fixed or varying */
! 1087: /* Tns[k] 1 2 2 4 5 */ /* Number of single cova */
! 1088: /* TvarsD[k] 1 2 3 */ /* Number of single dummy cova */
! 1089: /* TvarsDind 2 3 9 */ /* position K of single dummy cova */
! 1090: /* TvarsQ[k] 1 2 */ /* Number of single quantitative cova */
! 1091: /* TvarsQind 1 6 */ /* position K of single quantitative cova */
! 1092: /* Tprod[i]=k 4 7 */
! 1093: /* Tage[i]=k 5 8 */
! 1094: /* */
! 1095: /* Type */
! 1096: /* V 1 2 3 4 5 */
! 1097: /* F F V V V */
! 1098: /* D Q D D Q */
! 1099: /* */
! 1100: int *TvarsD;
! 1101: int *TvarsDind;
! 1102: int *TvarsQ;
! 1103: int *TvarsQind;
! 1104:
! 1105: /* 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 1106: 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 */
1107: 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 */
1108: 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 */
1109: int *TvarVind; /**< TvarVind[1]=1, TvarVind[2]=2 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
1110: 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 */
1111: 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 1112: int *TvarFD; /**< TvarFD[1]=V1 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
1113: int *TvarFDind; /* TvarFDind[1]=9 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
1114: int *TvarFQ; /* TvarFQ[1]=V2 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ /* Only simple fixed quantitative variable */
1115: int *TvarFQind; /* TvarFQind[1]=6 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ /* Only simple fixed quantitative variable */
1116: int *TvarVD; /* TvarVD[1]=V5 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ /* Only simple fixed quantitative variable */
1117: int *TvarVDind; /* TvarVDind[1]=1 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ /* Only simple fixed quantitative variable */
1118: 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 */
1119: 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 */
1120:
1.230 brouard 1121: int *Tvarsel; /**< Selected covariates for output */
1122: double *Tvalsel; /**< Selected modality value of covariate for output */
1.226 brouard 1123: int *Typevar; /**< 0 for simple covariate (dummy, quantitative, fixed or varying), 1 for age product, 2 for product */
1.227 brouard 1124: int *Fixed; /** Fixed[k] 0=fixed, 1 varying, 2 fixed with age product, 3 varying with age product */
1125: 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.197 brouard 1126: int *Tage;
1.227 brouard 1127: int anyvaryingduminmodel=0; /**< Any varying dummy in Model=1 yes, 0 no, to avoid a loop on waves in freq */
1.228 brouard 1128: 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 1129: 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*/
1130: 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 1131: int *Ndum; /** Freq of modality (tricode */
1.200 brouard 1132: /* int **codtab;*/ /**< codtab=imatrix(1,100,1,10); */
1.227 brouard 1133: int **Tvard;
1134: int *Tprod;/**< Gives the k position of the k1 product */
1135: int *Tposprod; /**< Gives the k1 product from the k position */
1136: /* Tprod[k1=1]=3(=V1*V4) for V2+V1+V1*V4+age*V3
1137: if V2+V1+V1*V4+age*V3+V3*V2 TProd[k1=2]=5 (V3*V2)
1138: Tposprod[k]=k1 , Tposprod[3]=1, Tposprod[5]=2
1139: */
1140: int cptcovprod, *Tvaraff, *invalidvarcomb;
1.126 brouard 1141: double *lsurv, *lpop, *tpop;
1142:
1.231 brouard 1143: #define FD 1; /* Fixed dummy covariate */
1144: #define FQ 2; /* Fixed quantitative covariate */
1145: #define FP 3; /* Fixed product covariate */
1146: #define FPDD 7; /* Fixed product dummy*dummy covariate */
1147: #define FPDQ 8; /* Fixed product dummy*quantitative covariate */
1148: #define FPQQ 9; /* Fixed product quantitative*quantitative covariate */
1149: #define VD 10; /* Varying dummy covariate */
1150: #define VQ 11; /* Varying quantitative covariate */
1151: #define VP 12; /* Varying product covariate */
1152: #define VPDD 13; /* Varying product dummy*dummy covariate */
1153: #define VPDQ 14; /* Varying product dummy*quantitative covariate */
1154: #define VPQQ 15; /* Varying product quantitative*quantitative covariate */
1155: #define APFD 16; /* Age product * fixed dummy covariate */
1156: #define APFQ 17; /* Age product * fixed quantitative covariate */
1157: #define APVD 18; /* Age product * varying dummy covariate */
1158: #define APVQ 19; /* Age product * varying quantitative covariate */
1159:
1160: #define FTYPE 1; /* Fixed covariate */
1161: #define VTYPE 2; /* Varying covariate (loop in wave) */
1162: #define ATYPE 2; /* Age product covariate (loop in dh within wave)*/
1163:
1164: struct kmodel{
1165: int maintype; /* main type */
1166: int subtype; /* subtype */
1167: };
1168: struct kmodel modell[NCOVMAX];
1169:
1.143 brouard 1170: double ftol=FTOL; /**< Tolerance for computing Max Likelihood */
1171: double ftolhess; /**< Tolerance for computing hessian */
1.126 brouard 1172:
1173: /**************** split *************************/
1174: static int split( char *path, char *dirc, char *name, char *ext, char *finame )
1175: {
1176: /* From a file name with (full) path (either Unix or Windows) we extract the directory (dirc)
1177: the name of the file (name), its extension only (ext) and its first part of the name (finame)
1178: */
1179: char *ss; /* pointer */
1.186 brouard 1180: int l1=0, l2=0; /* length counters */
1.126 brouard 1181:
1182: l1 = strlen(path ); /* length of path */
1183: if ( l1 == 0 ) return( GLOCK_ERROR_NOPATH );
1184: ss= strrchr( path, DIRSEPARATOR ); /* find last / */
1185: if ( ss == NULL ) { /* no directory, so determine current directory */
1186: strcpy( name, path ); /* we got the fullname name because no directory */
1187: /*if(strrchr(path, ODIRSEPARATOR )==NULL)
1188: printf("Warning you should use %s as a separator\n",DIRSEPARATOR);*/
1189: /* get current working directory */
1190: /* extern char* getcwd ( char *buf , int len);*/
1.184 brouard 1191: #ifdef WIN32
1192: if (_getcwd( dirc, FILENAME_MAX ) == NULL ) {
1193: #else
1194: if (getcwd(dirc, FILENAME_MAX) == NULL) {
1195: #endif
1.126 brouard 1196: return( GLOCK_ERROR_GETCWD );
1197: }
1198: /* got dirc from getcwd*/
1199: printf(" DIRC = %s \n",dirc);
1.205 brouard 1200: } else { /* strip directory from path */
1.126 brouard 1201: ss++; /* after this, the filename */
1202: l2 = strlen( ss ); /* length of filename */
1203: if ( l2 == 0 ) return( GLOCK_ERROR_NOPATH );
1204: strcpy( name, ss ); /* save file name */
1205: strncpy( dirc, path, l1 - l2 ); /* now the directory */
1.186 brouard 1206: dirc[l1-l2] = '\0'; /* add zero */
1.126 brouard 1207: printf(" DIRC2 = %s \n",dirc);
1208: }
1209: /* We add a separator at the end of dirc if not exists */
1210: l1 = strlen( dirc ); /* length of directory */
1211: if( dirc[l1-1] != DIRSEPARATOR ){
1212: dirc[l1] = DIRSEPARATOR;
1213: dirc[l1+1] = 0;
1214: printf(" DIRC3 = %s \n",dirc);
1215: }
1216: ss = strrchr( name, '.' ); /* find last / */
1217: if (ss >0){
1218: ss++;
1219: strcpy(ext,ss); /* save extension */
1220: l1= strlen( name);
1221: l2= strlen(ss)+1;
1222: strncpy( finame, name, l1-l2);
1223: finame[l1-l2]= 0;
1224: }
1225:
1226: return( 0 ); /* we're done */
1227: }
1228:
1229:
1230: /******************************************/
1231:
1232: void replace_back_to_slash(char *s, char*t)
1233: {
1234: int i;
1235: int lg=0;
1236: i=0;
1237: lg=strlen(t);
1238: for(i=0; i<= lg; i++) {
1239: (s[i] = t[i]);
1240: if (t[i]== '\\') s[i]='/';
1241: }
1242: }
1243:
1.132 brouard 1244: char *trimbb(char *out, char *in)
1.137 brouard 1245: { /* Trim multiple blanks in line but keeps first blanks if line starts with blanks */
1.132 brouard 1246: char *s;
1247: s=out;
1248: while (*in != '\0'){
1.137 brouard 1249: while( *in == ' ' && *(in+1) == ' '){ /* && *(in+1) != '\0'){*/
1.132 brouard 1250: in++;
1251: }
1252: *out++ = *in++;
1253: }
1254: *out='\0';
1255: return s;
1256: }
1257:
1.187 brouard 1258: /* char *substrchaine(char *out, char *in, char *chain) */
1259: /* { */
1260: /* /\* Substract chain 'chain' from 'in', return and output 'out' *\/ */
1261: /* char *s, *t; */
1262: /* t=in;s=out; */
1263: /* while ((*in != *chain) && (*in != '\0')){ */
1264: /* *out++ = *in++; */
1265: /* } */
1266:
1267: /* /\* *in matches *chain *\/ */
1268: /* while ((*in++ == *chain++) && (*in != '\0')){ */
1269: /* printf("*in = %c, *out= %c *chain= %c \n", *in, *out, *chain); */
1270: /* } */
1271: /* in--; chain--; */
1272: /* while ( (*in != '\0')){ */
1273: /* printf("Bef *in = %c, *out= %c *chain= %c \n", *in, *out, *chain); */
1274: /* *out++ = *in++; */
1275: /* printf("Aft *in = %c, *out= %c *chain= %c \n", *in, *out, *chain); */
1276: /* } */
1277: /* *out='\0'; */
1278: /* out=s; */
1279: /* return out; */
1280: /* } */
1281: char *substrchaine(char *out, char *in, char *chain)
1282: {
1283: /* Substract chain 'chain' from 'in', return and output 'out' */
1284: /* in="V1+V1*age+age*age+V2", chain="age*age" */
1285:
1286: char *strloc;
1287:
1288: strcpy (out, in);
1289: strloc = strstr(out, chain); /* strloc points to out at age*age+V2 */
1290: printf("Bef strloc=%s chain=%s out=%s \n", strloc, chain, out);
1291: if(strloc != NULL){
1292: /* will affect out */ /* strloc+strlenc(chain)=+V2 */ /* Will also work in Unicode */
1293: memmove(strloc,strloc+strlen(chain), strlen(strloc+strlen(chain))+1);
1294: /* strcpy (strloc, strloc +strlen(chain));*/
1295: }
1296: printf("Aft strloc=%s chain=%s in=%s out=%s \n", strloc, chain, in, out);
1297: return out;
1298: }
1299:
1300:
1.145 brouard 1301: char *cutl(char *blocc, char *alocc, char *in, char occ)
1302: {
1.187 brouard 1303: /* cuts string in into blocc and alocc where blocc ends before FIRST occurence of char 'occ'
1.145 brouard 1304: and alocc starts after first occurence of char 'occ' : ex cutv(blocc,alocc,"abcdef2ghi2j",'2')
1.187 brouard 1305: gives blocc="abcdef" and alocc="ghi2j".
1.145 brouard 1306: If occ is not found blocc is null and alocc is equal to in. Returns blocc
1307: */
1.160 brouard 1308: char *s, *t;
1.145 brouard 1309: t=in;s=in;
1310: while ((*in != occ) && (*in != '\0')){
1311: *alocc++ = *in++;
1312: }
1313: if( *in == occ){
1314: *(alocc)='\0';
1315: s=++in;
1316: }
1317:
1318: if (s == t) {/* occ not found */
1319: *(alocc-(in-s))='\0';
1320: in=s;
1321: }
1322: while ( *in != '\0'){
1323: *blocc++ = *in++;
1324: }
1325:
1326: *blocc='\0';
1327: return t;
1328: }
1.137 brouard 1329: char *cutv(char *blocc, char *alocc, char *in, char occ)
1330: {
1.187 brouard 1331: /* cuts string in into blocc and alocc where blocc ends before LAST occurence of char 'occ'
1.137 brouard 1332: and alocc starts after last occurence of char 'occ' : ex cutv(blocc,alocc,"abcdef2ghi2j",'2')
1333: gives blocc="abcdef2ghi" and alocc="j".
1334: If occ is not found blocc is null and alocc is equal to in. Returns alocc
1335: */
1336: char *s, *t;
1337: t=in;s=in;
1338: while (*in != '\0'){
1339: while( *in == occ){
1340: *blocc++ = *in++;
1341: s=in;
1342: }
1343: *blocc++ = *in++;
1344: }
1345: if (s == t) /* occ not found */
1346: *(blocc-(in-s))='\0';
1347: else
1348: *(blocc-(in-s)-1)='\0';
1349: in=s;
1350: while ( *in != '\0'){
1351: *alocc++ = *in++;
1352: }
1353:
1354: *alocc='\0';
1355: return s;
1356: }
1357:
1.126 brouard 1358: int nbocc(char *s, char occ)
1359: {
1360: int i,j=0;
1361: int lg=20;
1362: i=0;
1363: lg=strlen(s);
1364: for(i=0; i<= lg; i++) {
1.234 ! brouard 1365: if (s[i] == occ ) j++;
1.126 brouard 1366: }
1367: return j;
1368: }
1369:
1.137 brouard 1370: /* void cutv(char *u,char *v, char*t, char occ) */
1371: /* { */
1372: /* /\* cuts string t into u and v where u ends before last occurence of char 'occ' */
1373: /* and v starts after last occurence of char 'occ' : ex cutv(u,v,"abcdef2ghi2j",'2') */
1374: /* gives u="abcdef2ghi" and v="j" *\/ */
1375: /* int i,lg,j,p=0; */
1376: /* i=0; */
1377: /* lg=strlen(t); */
1378: /* for(j=0; j<=lg-1; j++) { */
1379: /* if((t[j]!= occ) && (t[j+1]== occ)) p=j+1; */
1380: /* } */
1.126 brouard 1381:
1.137 brouard 1382: /* for(j=0; j<p; j++) { */
1383: /* (u[j] = t[j]); */
1384: /* } */
1385: /* u[p]='\0'; */
1.126 brouard 1386:
1.137 brouard 1387: /* for(j=0; j<= lg; j++) { */
1388: /* if (j>=(p+1))(v[j-p-1] = t[j]); */
1389: /* } */
1390: /* } */
1.126 brouard 1391:
1.160 brouard 1392: #ifdef _WIN32
1393: char * strsep(char **pp, const char *delim)
1394: {
1395: char *p, *q;
1396:
1397: if ((p = *pp) == NULL)
1398: return 0;
1399: if ((q = strpbrk (p, delim)) != NULL)
1400: {
1401: *pp = q + 1;
1402: *q = '\0';
1403: }
1404: else
1405: *pp = 0;
1406: return p;
1407: }
1408: #endif
1409:
1.126 brouard 1410: /********************** nrerror ********************/
1411:
1412: void nrerror(char error_text[])
1413: {
1414: fprintf(stderr,"ERREUR ...\n");
1415: fprintf(stderr,"%s\n",error_text);
1416: exit(EXIT_FAILURE);
1417: }
1418: /*********************** vector *******************/
1419: double *vector(int nl, int nh)
1420: {
1421: double *v;
1422: v=(double *) malloc((size_t)((nh-nl+1+NR_END)*sizeof(double)));
1423: if (!v) nrerror("allocation failure in vector");
1424: return v-nl+NR_END;
1425: }
1426:
1427: /************************ free vector ******************/
1428: void free_vector(double*v, int nl, int nh)
1429: {
1430: free((FREE_ARG)(v+nl-NR_END));
1431: }
1432:
1433: /************************ivector *******************************/
1434: int *ivector(long nl,long nh)
1435: {
1436: int *v;
1437: v=(int *) malloc((size_t)((nh-nl+1+NR_END)*sizeof(int)));
1438: if (!v) nrerror("allocation failure in ivector");
1439: return v-nl+NR_END;
1440: }
1441:
1442: /******************free ivector **************************/
1443: void free_ivector(int *v, long nl, long nh)
1444: {
1445: free((FREE_ARG)(v+nl-NR_END));
1446: }
1447:
1448: /************************lvector *******************************/
1449: long *lvector(long nl,long nh)
1450: {
1451: long *v;
1452: v=(long *) malloc((size_t)((nh-nl+1+NR_END)*sizeof(long)));
1453: if (!v) nrerror("allocation failure in ivector");
1454: return v-nl+NR_END;
1455: }
1456:
1457: /******************free lvector **************************/
1458: void free_lvector(long *v, long nl, long nh)
1459: {
1460: free((FREE_ARG)(v+nl-NR_END));
1461: }
1462:
1463: /******************* imatrix *******************************/
1464: int **imatrix(long nrl, long nrh, long ncl, long nch)
1465: /* allocate a int matrix with subscript range m[nrl..nrh][ncl..nch] */
1466: {
1467: long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
1468: int **m;
1469:
1470: /* allocate pointers to rows */
1471: m=(int **) malloc((size_t)((nrow+NR_END)*sizeof(int*)));
1472: if (!m) nrerror("allocation failure 1 in matrix()");
1473: m += NR_END;
1474: m -= nrl;
1475:
1476:
1477: /* allocate rows and set pointers to them */
1478: m[nrl]=(int *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(int)));
1479: if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
1480: m[nrl] += NR_END;
1481: m[nrl] -= ncl;
1482:
1483: for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
1484:
1485: /* return pointer to array of pointers to rows */
1486: return m;
1487: }
1488:
1489: /****************** free_imatrix *************************/
1490: void free_imatrix(m,nrl,nrh,ncl,nch)
1491: int **m;
1492: long nch,ncl,nrh,nrl;
1493: /* free an int matrix allocated by imatrix() */
1494: {
1495: free((FREE_ARG) (m[nrl]+ncl-NR_END));
1496: free((FREE_ARG) (m+nrl-NR_END));
1497: }
1498:
1499: /******************* matrix *******************************/
1500: double **matrix(long nrl, long nrh, long ncl, long nch)
1501: {
1502: long i, nrow=nrh-nrl+1, ncol=nch-ncl+1;
1503: double **m;
1504:
1505: m=(double **) malloc((size_t)((nrow+NR_END)*sizeof(double*)));
1506: if (!m) nrerror("allocation failure 1 in matrix()");
1507: m += NR_END;
1508: m -= nrl;
1509:
1510: m[nrl]=(double *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(double)));
1511: if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
1512: m[nrl] += NR_END;
1513: m[nrl] -= ncl;
1514:
1515: for (i=nrl+1; i<=nrh; i++) m[i]=m[i-1]+ncol;
1516: return m;
1.145 brouard 1517: /* print *(*(m+1)+70) or print m[1][70]; print m+1 or print &(m[1]) or &(m[1][0])
1518: m[i] = address of ith row of the table. &(m[i]) is its value which is another adress
1519: that of m[i][0]. In order to get the value p m[i][0] but it is unitialized.
1.126 brouard 1520: */
1521: }
1522:
1523: /*************************free matrix ************************/
1524: void free_matrix(double **m, long nrl, long nrh, long ncl, long nch)
1525: {
1526: free((FREE_ARG)(m[nrl]+ncl-NR_END));
1527: free((FREE_ARG)(m+nrl-NR_END));
1528: }
1529:
1530: /******************* ma3x *******************************/
1531: double ***ma3x(long nrl, long nrh, long ncl, long nch, long nll, long nlh)
1532: {
1533: long i, j, nrow=nrh-nrl+1, ncol=nch-ncl+1, nlay=nlh-nll+1;
1534: double ***m;
1535:
1536: m=(double ***) malloc((size_t)((nrow+NR_END)*sizeof(double*)));
1537: if (!m) nrerror("allocation failure 1 in matrix()");
1538: m += NR_END;
1539: m -= nrl;
1540:
1541: m[nrl]=(double **) malloc((size_t)((nrow*ncol+NR_END)*sizeof(double)));
1542: if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
1543: m[nrl] += NR_END;
1544: m[nrl] -= ncl;
1545:
1546: for (i=nrl+1; i<=nrh; i++) m[i]=m[i-1]+ncol;
1547:
1548: m[nrl][ncl]=(double *) malloc((size_t)((nrow*ncol*nlay+NR_END)*sizeof(double)));
1549: if (!m[nrl][ncl]) nrerror("allocation failure 3 in matrix()");
1550: m[nrl][ncl] += NR_END;
1551: m[nrl][ncl] -= nll;
1552: for (j=ncl+1; j<=nch; j++)
1553: m[nrl][j]=m[nrl][j-1]+nlay;
1554:
1555: for (i=nrl+1; i<=nrh; i++) {
1556: m[i][ncl]=m[i-1l][ncl]+ncol*nlay;
1557: for (j=ncl+1; j<=nch; j++)
1558: m[i][j]=m[i][j-1]+nlay;
1559: }
1560: return m;
1561: /* gdb: p *(m+1) <=> p m[1] and p (m+1) <=> p (m+1) <=> p &(m[1])
1562: &(m[i][j][k]) <=> *((*(m+i) + j)+k)
1563: */
1564: }
1565:
1566: /*************************free ma3x ************************/
1567: void free_ma3x(double ***m, long nrl, long nrh, long ncl, long nch,long nll, long nlh)
1568: {
1569: free((FREE_ARG)(m[nrl][ncl]+ nll-NR_END));
1570: free((FREE_ARG)(m[nrl]+ncl-NR_END));
1571: free((FREE_ARG)(m+nrl-NR_END));
1572: }
1573:
1574: /*************** function subdirf ***********/
1575: char *subdirf(char fileres[])
1576: {
1577: /* Caution optionfilefiname is hidden */
1578: strcpy(tmpout,optionfilefiname);
1579: strcat(tmpout,"/"); /* Add to the right */
1580: strcat(tmpout,fileres);
1581: return tmpout;
1582: }
1583:
1584: /*************** function subdirf2 ***********/
1585: char *subdirf2(char fileres[], char *preop)
1586: {
1587:
1588: /* Caution optionfilefiname is hidden */
1589: strcpy(tmpout,optionfilefiname);
1590: strcat(tmpout,"/");
1591: strcat(tmpout,preop);
1592: strcat(tmpout,fileres);
1593: return tmpout;
1594: }
1595:
1596: /*************** function subdirf3 ***********/
1597: char *subdirf3(char fileres[], char *preop, char *preop2)
1598: {
1599:
1600: /* Caution optionfilefiname is hidden */
1601: strcpy(tmpout,optionfilefiname);
1602: strcat(tmpout,"/");
1603: strcat(tmpout,preop);
1604: strcat(tmpout,preop2);
1605: strcat(tmpout,fileres);
1606: return tmpout;
1607: }
1.213 brouard 1608:
1609: /*************** function subdirfext ***********/
1610: char *subdirfext(char fileres[], char *preop, char *postop)
1611: {
1612:
1613: strcpy(tmpout,preop);
1614: strcat(tmpout,fileres);
1615: strcat(tmpout,postop);
1616: return tmpout;
1617: }
1.126 brouard 1618:
1.213 brouard 1619: /*************** function subdirfext3 ***********/
1620: char *subdirfext3(char fileres[], char *preop, char *postop)
1621: {
1622:
1623: /* Caution optionfilefiname is hidden */
1624: strcpy(tmpout,optionfilefiname);
1625: strcat(tmpout,"/");
1626: strcat(tmpout,preop);
1627: strcat(tmpout,fileres);
1628: strcat(tmpout,postop);
1629: return tmpout;
1630: }
1631:
1.162 brouard 1632: char *asc_diff_time(long time_sec, char ascdiff[])
1633: {
1634: long sec_left, days, hours, minutes;
1635: days = (time_sec) / (60*60*24);
1636: sec_left = (time_sec) % (60*60*24);
1637: hours = (sec_left) / (60*60) ;
1638: sec_left = (sec_left) %(60*60);
1639: minutes = (sec_left) /60;
1640: sec_left = (sec_left) % (60);
1641: sprintf(ascdiff,"%ld day(s) %ld hour(s) %ld minute(s) %ld second(s)",days, hours, minutes, sec_left);
1642: return ascdiff;
1643: }
1644:
1.126 brouard 1645: /***************** f1dim *************************/
1646: extern int ncom;
1647: extern double *pcom,*xicom;
1648: extern double (*nrfunc)(double []);
1649:
1650: double f1dim(double x)
1651: {
1652: int j;
1653: double f;
1654: double *xt;
1655:
1656: xt=vector(1,ncom);
1657: for (j=1;j<=ncom;j++) xt[j]=pcom[j]+x*xicom[j];
1658: f=(*nrfunc)(xt);
1659: free_vector(xt,1,ncom);
1660: return f;
1661: }
1662:
1663: /*****************brent *************************/
1664: double brent(double ax, double bx, double cx, double (*f)(double), double tol, double *xmin)
1.187 brouard 1665: {
1666: /* Given a function f, and given a bracketing triplet of abscissas ax, bx, cx (such that bx is
1667: * between ax and cx, and f(bx) is less than both f(ax) and f(cx) ), this routine isolates
1668: * the minimum to a fractional precision of about tol using Brent’s method. The abscissa of
1669: * the minimum is returned as xmin, and the minimum function value is returned as brent , the
1670: * returned function value.
1671: */
1.126 brouard 1672: int iter;
1673: double a,b,d,etemp;
1.159 brouard 1674: double fu=0,fv,fw,fx;
1.164 brouard 1675: double ftemp=0.;
1.126 brouard 1676: double p,q,r,tol1,tol2,u,v,w,x,xm;
1677: double e=0.0;
1678:
1679: a=(ax < cx ? ax : cx);
1680: b=(ax > cx ? ax : cx);
1681: x=w=v=bx;
1682: fw=fv=fx=(*f)(x);
1683: for (iter=1;iter<=ITMAX;iter++) {
1684: xm=0.5*(a+b);
1685: tol2=2.0*(tol1=tol*fabs(x)+ZEPS);
1686: /* if (2.0*fabs(fp-(*fret)) <= ftol*(fabs(fp)+fabs(*fret)))*/
1687: printf(".");fflush(stdout);
1688: fprintf(ficlog,".");fflush(ficlog);
1.162 brouard 1689: #ifdef DEBUGBRENT
1.126 brouard 1690: 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);
1691: 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);
1692: /* if ((fabs(x-xm) <= (tol2-0.5*(b-a)))||(2.0*fabs(fu-ftemp) <= ftol*1.e-2*(fabs(fu)+fabs(ftemp)))) { */
1693: #endif
1694: if (fabs(x-xm) <= (tol2-0.5*(b-a))){
1695: *xmin=x;
1696: return fx;
1697: }
1698: ftemp=fu;
1699: if (fabs(e) > tol1) {
1700: r=(x-w)*(fx-fv);
1701: q=(x-v)*(fx-fw);
1702: p=(x-v)*q-(x-w)*r;
1703: q=2.0*(q-r);
1704: if (q > 0.0) p = -p;
1705: q=fabs(q);
1706: etemp=e;
1707: e=d;
1708: if (fabs(p) >= fabs(0.5*q*etemp) || p <= q*(a-x) || p >= q*(b-x))
1.224 brouard 1709: d=CGOLD*(e=(x >= xm ? a-x : b-x));
1.126 brouard 1710: else {
1.224 brouard 1711: d=p/q;
1712: u=x+d;
1713: if (u-a < tol2 || b-u < tol2)
1714: d=SIGN(tol1,xm-x);
1.126 brouard 1715: }
1716: } else {
1717: d=CGOLD*(e=(x >= xm ? a-x : b-x));
1718: }
1719: u=(fabs(d) >= tol1 ? x+d : x+SIGN(tol1,d));
1720: fu=(*f)(u);
1721: if (fu <= fx) {
1722: if (u >= x) a=x; else b=x;
1723: SHFT(v,w,x,u)
1.183 brouard 1724: SHFT(fv,fw,fx,fu)
1725: } else {
1726: if (u < x) a=u; else b=u;
1727: if (fu <= fw || w == x) {
1.224 brouard 1728: v=w;
1729: w=u;
1730: fv=fw;
1731: fw=fu;
1.183 brouard 1732: } else if (fu <= fv || v == x || v == w) {
1.224 brouard 1733: v=u;
1734: fv=fu;
1.183 brouard 1735: }
1736: }
1.126 brouard 1737: }
1738: nrerror("Too many iterations in brent");
1739: *xmin=x;
1740: return fx;
1741: }
1742:
1743: /****************** mnbrak ***********************/
1744:
1745: void mnbrak(double *ax, double *bx, double *cx, double *fa, double *fb, double *fc,
1746: double (*func)(double))
1.183 brouard 1747: { /* Given a function func , and given distinct initial points ax and bx , this routine searches in
1748: the downhill direction (defined by the function as evaluated at the initial points) and returns
1749: new points ax , bx , cx that bracket a minimum of the function. Also returned are the function
1750: values at the three points, fa, fb , and fc such that fa > fb and fb < fc.
1751: */
1.126 brouard 1752: double ulim,u,r,q, dum;
1753: double fu;
1.187 brouard 1754:
1755: double scale=10.;
1756: int iterscale=0;
1757:
1758: *fa=(*func)(*ax); /* xta[j]=pcom[j]+(*ax)*xicom[j]; fa=f(xta[j])*/
1759: *fb=(*func)(*bx); /* xtb[j]=pcom[j]+(*bx)*xicom[j]; fb=f(xtb[j]) */
1760:
1761:
1762: /* while(*fb != *fb){ /\* *ax should be ok, reducing distance to *ax *\/ */
1763: /* printf("Warning mnbrak *fb = %lf, *bx=%lf *ax=%lf *fa==%lf iter=%d\n",*fb, *bx, *ax, *fa, iterscale++); */
1764: /* *bx = *ax - (*ax - *bx)/scale; */
1765: /* *fb=(*func)(*bx); /\* xtb[j]=pcom[j]+(*bx)*xicom[j]; fb=f(xtb[j]) *\/ */
1766: /* } */
1767:
1.126 brouard 1768: if (*fb > *fa) {
1769: SHFT(dum,*ax,*bx,dum)
1.183 brouard 1770: SHFT(dum,*fb,*fa,dum)
1771: }
1.126 brouard 1772: *cx=(*bx)+GOLD*(*bx-*ax);
1773: *fc=(*func)(*cx);
1.183 brouard 1774: #ifdef DEBUG
1.224 brouard 1775: printf("mnbrak0 a=%lf *fa=%lf, b=%lf *fb=%lf, c=%lf *fc=%lf\n",*ax,*fa,*bx,*fb,*cx, *fc);
1776: 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 1777: #endif
1.224 brouard 1778: 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 1779: r=(*bx-*ax)*(*fb-*fc);
1.224 brouard 1780: q=(*bx-*cx)*(*fb-*fa); /* What if fa=inf */
1.126 brouard 1781: u=(*bx)-((*bx-*cx)*q-(*bx-*ax)*r)/
1.183 brouard 1782: (2.0*SIGN(FMAX(fabs(q-r),TINY),q-r)); /* Minimum abscissa of a parabolic estimated from (a,fa), (b,fb) and (c,fc). */
1783: ulim=(*bx)+GLIMIT*(*cx-*bx); /* Maximum abscissa where function should be evaluated */
1784: if ((*bx-u)*(u-*cx) > 0.0) { /* if u_p is between b and c */
1.126 brouard 1785: fu=(*func)(u);
1.163 brouard 1786: #ifdef DEBUG
1787: /* f(x)=A(x-u)**2+f(u) */
1788: double A, fparabu;
1789: A= (*fb - *fa)/(*bx-*ax)/(*bx+*ax-2*u);
1790: fparabu= *fa - A*(*ax-u)*(*ax-u);
1.224 brouard 1791: 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);
1792: 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 1793: /* And thus,it can be that fu > *fc even if fparabu < *fc */
1794: /* mnbrak (*ax=7.666299858533, *fa=299039.693133272231), (*bx=8.595447774979, *fb=298976.598289369489),
1795: (*cx=10.098840694817, *fc=298946.631474258087), (*u=9.852501168332, fu=298948.773013752128, fparabu=298945.434711494134) */
1796: /* In that case, there is no bracket in the output! Routine is wrong with many consequences.*/
1.163 brouard 1797: #endif
1.184 brouard 1798: #ifdef MNBRAKORIGINAL
1.183 brouard 1799: #else
1.191 brouard 1800: /* if (fu > *fc) { */
1801: /* #ifdef DEBUG */
1802: /* printf("mnbrak4 fu > fc \n"); */
1803: /* fprintf(ficlog, "mnbrak4 fu > fc\n"); */
1804: /* #endif */
1805: /* /\* 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 *\\/ *\/ */
1806: /* /\* SHFT(*fa,*fc,fu,*fc) /\\* (b, u, c) is a bracket while test fb > fc will be fu > fc will exit *\\/ *\/ */
1807: /* dum=u; /\* Shifting c and u *\/ */
1808: /* u = *cx; */
1809: /* *cx = dum; */
1810: /* dum = fu; */
1811: /* fu = *fc; */
1812: /* *fc =dum; */
1813: /* } else { /\* end *\/ */
1814: /* #ifdef DEBUG */
1815: /* printf("mnbrak3 fu < fc \n"); */
1816: /* fprintf(ficlog, "mnbrak3 fu < fc\n"); */
1817: /* #endif */
1818: /* dum=u; /\* Shifting c and u *\/ */
1819: /* u = *cx; */
1820: /* *cx = dum; */
1821: /* dum = fu; */
1822: /* fu = *fc; */
1823: /* *fc =dum; */
1824: /* } */
1.224 brouard 1825: #ifdef DEBUGMNBRAK
1826: double A, fparabu;
1827: A= (*fb - *fa)/(*bx-*ax)/(*bx+*ax-2*u);
1828: fparabu= *fa - A*(*ax-u)*(*ax-u);
1829: 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);
1830: 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 1831: #endif
1.191 brouard 1832: dum=u; /* Shifting c and u */
1833: u = *cx;
1834: *cx = dum;
1835: dum = fu;
1836: fu = *fc;
1837: *fc =dum;
1.183 brouard 1838: #endif
1.162 brouard 1839: } else if ((*cx-u)*(u-ulim) > 0.0) { /* u is after c but before ulim */
1.183 brouard 1840: #ifdef DEBUG
1.224 brouard 1841: printf("\nmnbrak2 u=%lf after c=%lf but before ulim\n",u,*cx);
1842: fprintf(ficlog,"\nmnbrak2 u=%lf after c=%lf but before ulim\n",u,*cx);
1.183 brouard 1843: #endif
1.126 brouard 1844: fu=(*func)(u);
1845: if (fu < *fc) {
1.183 brouard 1846: #ifdef DEBUG
1.224 brouard 1847: printf("\nmnbrak2 u=%lf after c=%lf but before ulim=%lf AND fu=%lf < %lf=fc\n",u,*cx,ulim,fu, *fc);
1848: fprintf(ficlog,"\nmnbrak2 u=%lf after c=%lf but before ulim=%lf AND fu=%lf < %lf=fc\n",u,*cx,ulim,fu, *fc);
1849: #endif
1850: SHFT(*bx,*cx,u,*cx+GOLD*(*cx-*bx))
1851: SHFT(*fb,*fc,fu,(*func)(u))
1852: #ifdef DEBUG
1853: printf("\nmnbrak2 shift GOLD c=%lf",*cx+GOLD*(*cx-*bx));
1.183 brouard 1854: #endif
1855: }
1.162 brouard 1856: } else if ((u-ulim)*(ulim-*cx) >= 0.0) { /* u outside ulim (verifying that ulim is beyond c) */
1.183 brouard 1857: #ifdef DEBUG
1.224 brouard 1858: printf("\nmnbrak2 u=%lf outside ulim=%lf (verifying that ulim is beyond c=%lf)\n",u,ulim,*cx);
1859: fprintf(ficlog,"\nmnbrak2 u=%lf outside ulim=%lf (verifying that ulim is beyond c=%lf)\n",u,ulim,*cx);
1.183 brouard 1860: #endif
1.126 brouard 1861: u=ulim;
1862: fu=(*func)(u);
1.183 brouard 1863: } else { /* u could be left to b (if r > q parabola has a maximum) */
1864: #ifdef DEBUG
1.224 brouard 1865: printf("\nmnbrak2 u=%lf could be left to b=%lf (if r=%lf > q=%lf parabola has a maximum)\n",u,*bx,r,q);
1866: 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 1867: #endif
1.126 brouard 1868: u=(*cx)+GOLD*(*cx-*bx);
1869: fu=(*func)(u);
1.224 brouard 1870: #ifdef DEBUG
1871: printf("\nmnbrak2 new u=%lf fu=%lf shifted gold left from c=%lf and b=%lf \n",u,fu,*cx,*bx);
1872: fprintf(ficlog,"\nmnbrak2 new u=%lf fu=%lf shifted gold left from c=%lf and b=%lf \n",u,fu,*cx,*bx);
1873: #endif
1.183 brouard 1874: } /* end tests */
1.126 brouard 1875: SHFT(*ax,*bx,*cx,u)
1.183 brouard 1876: SHFT(*fa,*fb,*fc,fu)
1877: #ifdef DEBUG
1.224 brouard 1878: printf("\nmnbrak2 shift (*ax=%.12f, *fa=%.12lf), (*bx=%.12f, *fb=%.12lf), (*cx=%.12f, *fc=%.12lf)\n",*ax,*fa,*bx,*fb,*cx,*fc);
1879: 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 1880: #endif
1881: } /* 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 1882: }
1883:
1884: /*************** linmin ************************/
1.162 brouard 1885: /* Given an n -dimensional point p[1..n] and an n -dimensional direction xi[1..n] , moves and
1886: resets p to where the function func(p) takes on a minimum along the direction xi from p ,
1887: and replaces xi by the actual vector displacement that p was moved. Also returns as fret
1888: the value of func at the returned location p . This is actually all accomplished by calling the
1889: routines mnbrak and brent .*/
1.126 brouard 1890: int ncom;
1891: double *pcom,*xicom;
1892: double (*nrfunc)(double []);
1893:
1.224 brouard 1894: #ifdef LINMINORIGINAL
1.126 brouard 1895: void linmin(double p[], double xi[], int n, double *fret,double (*func)(double []))
1.224 brouard 1896: #else
1897: void linmin(double p[], double xi[], int n, double *fret,double (*func)(double []), int *flat)
1898: #endif
1.126 brouard 1899: {
1900: double brent(double ax, double bx, double cx,
1901: double (*f)(double), double tol, double *xmin);
1902: double f1dim(double x);
1903: void mnbrak(double *ax, double *bx, double *cx, double *fa, double *fb,
1904: double *fc, double (*func)(double));
1905: int j;
1906: double xx,xmin,bx,ax;
1907: double fx,fb,fa;
1.187 brouard 1908:
1.203 brouard 1909: #ifdef LINMINORIGINAL
1910: #else
1911: double scale=10., axs, xxs; /* Scale added for infinity */
1912: #endif
1913:
1.126 brouard 1914: ncom=n;
1915: pcom=vector(1,n);
1916: xicom=vector(1,n);
1917: nrfunc=func;
1918: for (j=1;j<=n;j++) {
1919: pcom[j]=p[j];
1.202 brouard 1920: xicom[j]=xi[j]; /* Former scale xi[j] of currrent direction i */
1.126 brouard 1921: }
1.187 brouard 1922:
1.203 brouard 1923: #ifdef LINMINORIGINAL
1924: xx=1.;
1925: #else
1926: axs=0.0;
1927: xxs=1.;
1928: do{
1929: xx= xxs;
1930: #endif
1.187 brouard 1931: ax=0.;
1932: mnbrak(&ax,&xx,&bx,&fa,&fx,&fb,f1dim); /* Outputs: xtx[j]=pcom[j]+(*xx)*xicom[j]; fx=f(xtx[j]) */
1933: /* brackets with inputs ax=0 and xx=1, but points, pcom=p, and directions values, xicom=xi, are sent via f1dim(x) */
1934: /* 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)) */
1935: /* Outputs: fa=f(p(j)) and fx=f(p(j) + xxs * xi(j) ) and f(bx)= f(p(j)+ bx* xi(j)) */
1936: /* Given input ax=axs and xx=xxs, xx might be too far from ax to get a finite f(xx) */
1937: /* Searches on line, outputs (ax, xx, bx) such that fx < min(fa and fb) */
1938: /* 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 1939: #ifdef LINMINORIGINAL
1940: #else
1941: if (fx != fx){
1.224 brouard 1942: xxs=xxs/scale; /* Trying a smaller xx, closer to initial ax=0 */
1943: printf("|");
1944: fprintf(ficlog,"|");
1.203 brouard 1945: #ifdef DEBUGLINMIN
1.224 brouard 1946: 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 1947: #endif
1948: }
1.224 brouard 1949: }while(fx != fx && xxs > 1.e-5);
1.203 brouard 1950: #endif
1951:
1.191 brouard 1952: #ifdef DEBUGLINMIN
1953: 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 1954: 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 1955: #endif
1.224 brouard 1956: #ifdef LINMINORIGINAL
1957: #else
1958: if(fb == fx){ /* Flat function in the direction */
1959: xmin=xx;
1960: *flat=1;
1961: }else{
1962: *flat=0;
1963: #endif
1964: /*Flat mnbrak2 shift (*ax=0.000000000000, *fa=51626.272983130431), (*bx=-1.618034000000, *fb=51590.149499362531), (*cx=-4.236068025156, *fc=51590.149499362531) */
1.187 brouard 1965: *fret=brent(ax,xx,bx,f1dim,TOL,&xmin); /* Giving a bracketting triplet (ax, xx, bx), find a minimum, xmin, according to f1dim, *fret(xmin),*/
1966: /* fa = f(p[j] + ax * xi[j]), fx = f(p[j] + xx * xi[j]), fb = f(p[j] + bx * xi[j]) */
1967: /* fmin = f(p[j] + xmin * xi[j]) */
1968: /* P+lambda n in that direction (lambdamin), with TOL between abscisses */
1969: /* f1dim(xmin): for (j=1;j<=ncom;j++) xt[j]=pcom[j]+xmin*xicom[j]; */
1.126 brouard 1970: #ifdef DEBUG
1.224 brouard 1971: 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);
1972: 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);
1973: #endif
1974: #ifdef LINMINORIGINAL
1975: #else
1976: }
1.126 brouard 1977: #endif
1.191 brouard 1978: #ifdef DEBUGLINMIN
1979: printf("linmin end ");
1.202 brouard 1980: fprintf(ficlog,"linmin end ");
1.191 brouard 1981: #endif
1.126 brouard 1982: for (j=1;j<=n;j++) {
1.203 brouard 1983: #ifdef LINMINORIGINAL
1984: xi[j] *= xmin;
1985: #else
1986: #ifdef DEBUGLINMIN
1987: if(xxs <1.0)
1988: printf(" before xi[%d]=%12.8f", j,xi[j]);
1989: #endif
1990: 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) */
1991: #ifdef DEBUGLINMIN
1992: if(xxs <1.0)
1993: 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 );
1994: #endif
1995: #endif
1.187 brouard 1996: p[j] += xi[j]; /* Parameters values are updated accordingly */
1.126 brouard 1997: }
1.191 brouard 1998: #ifdef DEBUGLINMIN
1.203 brouard 1999: printf("\n");
1.191 brouard 2000: printf("Comparing last *frec(xmin=%12.8f)=%12.8f from Brent and frec(0.)=%12.8f \n", xmin, *fret, (*func)(p));
1.202 brouard 2001: 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 2002: for (j=1;j<=n;j++) {
1.202 brouard 2003: printf(" xi[%d]= %14.10f p[%d]= %12.7f",j,xi[j],j,p[j]);
2004: fprintf(ficlog," xi[%d]= %14.10f p[%d]= %12.7f",j,xi[j],j,p[j]);
2005: if(j % ncovmodel == 0){
1.191 brouard 2006: printf("\n");
1.202 brouard 2007: fprintf(ficlog,"\n");
2008: }
1.191 brouard 2009: }
1.203 brouard 2010: #else
1.191 brouard 2011: #endif
1.126 brouard 2012: free_vector(xicom,1,n);
2013: free_vector(pcom,1,n);
2014: }
2015:
2016:
2017: /*************** powell ************************/
1.162 brouard 2018: /*
2019: Minimization of a function func of n variables. Input consists of an initial starting point
2020: p[1..n] ; an initial matrix xi[1..n][1..n] , whose columns contain the initial set of di-
2021: rections (usually the n unit vectors); and ftol , the fractional tolerance in the function value
2022: such that failure to decrease by more than this amount on one iteration signals doneness. On
2023: output, p is set to the best point found, xi is the then-current direction set, fret is the returned
2024: function value at p , and iter is the number of iterations taken. The routine linmin is used.
2025: */
1.224 brouard 2026: #ifdef LINMINORIGINAL
2027: #else
2028: int *flatdir; /* Function is vanishing in that direction */
1.225 brouard 2029: int flat=0, flatd=0; /* Function is vanishing in that direction */
1.224 brouard 2030: #endif
1.126 brouard 2031: void powell(double p[], double **xi, int n, double ftol, int *iter, double *fret,
2032: double (*func)(double []))
2033: {
1.224 brouard 2034: #ifdef LINMINORIGINAL
2035: void linmin(double p[], double xi[], int n, double *fret,
1.126 brouard 2036: double (*func)(double []));
1.224 brouard 2037: #else
2038: void linmin(double p[], double xi[], int n, double *fret,
2039: double (*func)(double []),int *flat);
2040: #endif
1.126 brouard 2041: int i,ibig,j;
2042: double del,t,*pt,*ptt,*xit;
1.181 brouard 2043: double directest;
1.126 brouard 2044: double fp,fptt;
2045: double *xits;
2046: int niterf, itmp;
1.224 brouard 2047: #ifdef LINMINORIGINAL
2048: #else
2049:
2050: flatdir=ivector(1,n);
2051: for (j=1;j<=n;j++) flatdir[j]=0;
2052: #endif
1.126 brouard 2053:
2054: pt=vector(1,n);
2055: ptt=vector(1,n);
2056: xit=vector(1,n);
2057: xits=vector(1,n);
2058: *fret=(*func)(p);
2059: for (j=1;j<=n;j++) pt[j]=p[j];
1.202 brouard 2060: rcurr_time = time(NULL);
1.126 brouard 2061: for (*iter=1;;++(*iter)) {
1.187 brouard 2062: fp=(*fret); /* From former iteration or initial value */
1.126 brouard 2063: ibig=0;
2064: del=0.0;
1.157 brouard 2065: rlast_time=rcurr_time;
2066: /* (void) gettimeofday(&curr_time,&tzp); */
2067: rcurr_time = time(NULL);
2068: curr_time = *localtime(&rcurr_time);
2069: printf("\nPowell iter=%d -2*LL=%.12f %ld sec. %ld sec.",*iter,*fret, rcurr_time-rlast_time, rcurr_time-rstart_time);fflush(stdout);
2070: fprintf(ficlog,"\nPowell iter=%d -2*LL=%.12f %ld sec. %ld sec.",*iter,*fret,rcurr_time-rlast_time, rcurr_time-rstart_time); fflush(ficlog);
2071: /* fprintf(ficrespow,"%d %.12f %ld",*iter,*fret,curr_time.tm_sec-start_time.tm_sec); */
1.192 brouard 2072: for (i=1;i<=n;i++) {
1.126 brouard 2073: printf(" %d %.12f",i, p[i]);
2074: fprintf(ficlog," %d %.12lf",i, p[i]);
2075: fprintf(ficrespow," %.12lf", p[i]);
2076: }
2077: printf("\n");
2078: fprintf(ficlog,"\n");
2079: fprintf(ficrespow,"\n");fflush(ficrespow);
2080: if(*iter <=3){
1.157 brouard 2081: tml = *localtime(&rcurr_time);
2082: strcpy(strcurr,asctime(&tml));
2083: rforecast_time=rcurr_time;
1.126 brouard 2084: itmp = strlen(strcurr);
2085: if(strcurr[itmp-1]=='\n') /* Windows outputs with a new line */
1.224 brouard 2086: strcurr[itmp-1]='\0';
1.162 brouard 2087: printf("\nConsidering the time needed for the last iteration #%d: %ld seconds,\n",*iter,rcurr_time-rlast_time);
1.157 brouard 2088: fprintf(ficlog,"\nConsidering the time needed for this last iteration #%d: %ld seconds,\n",*iter,rcurr_time-rlast_time);
1.126 brouard 2089: for(niterf=10;niterf<=30;niterf+=10){
1.224 brouard 2090: rforecast_time=rcurr_time+(niterf-*iter)*(rcurr_time-rlast_time);
2091: forecast_time = *localtime(&rforecast_time);
2092: strcpy(strfor,asctime(&forecast_time));
2093: itmp = strlen(strfor);
2094: if(strfor[itmp-1]=='\n')
2095: strfor[itmp-1]='\0';
2096: 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);
2097: 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 2098: }
2099: }
1.187 brouard 2100: for (i=1;i<=n;i++) { /* For each direction i */
2101: for (j=1;j<=n;j++) xit[j]=xi[j][i]; /* Directions stored from previous iteration with previous scales */
1.126 brouard 2102: fptt=(*fret);
2103: #ifdef DEBUG
1.203 brouard 2104: printf("fret=%lf, %lf, %lf \n", *fret, *fret, *fret);
2105: fprintf(ficlog, "fret=%lf, %lf, %lf \n", *fret, *fret, *fret);
1.126 brouard 2106: #endif
1.203 brouard 2107: printf("%d",i);fflush(stdout); /* print direction (parameter) i */
1.126 brouard 2108: fprintf(ficlog,"%d",i);fflush(ficlog);
1.224 brouard 2109: #ifdef LINMINORIGINAL
1.188 brouard 2110: linmin(p,xit,n,fret,func); /* Point p[n]. xit[n] has been loaded for direction i as input.*/
1.224 brouard 2111: #else
2112: linmin(p,xit,n,fret,func,&flat); /* Point p[n]. xit[n] has been loaded for direction i as input.*/
2113: flatdir[i]=flat; /* Function is vanishing in that direction i */
2114: #endif
2115: /* Outputs are fret(new point p) p is updated and xit rescaled */
1.188 brouard 2116: if (fabs(fptt-(*fret)) > del) { /* We are keeping the max gain on each of the n directions */
1.224 brouard 2117: /* because that direction will be replaced unless the gain del is small */
2118: /* in comparison with the 'probable' gain, mu^2, with the last average direction. */
2119: /* Unless the n directions are conjugate some gain in the determinant may be obtained */
2120: /* with the new direction. */
2121: del=fabs(fptt-(*fret));
2122: ibig=i;
1.126 brouard 2123: }
2124: #ifdef DEBUG
2125: printf("%d %.12e",i,(*fret));
2126: fprintf(ficlog,"%d %.12e",i,(*fret));
2127: for (j=1;j<=n;j++) {
1.224 brouard 2128: xits[j]=FMAX(fabs(p[j]-pt[j]),1.e-5);
2129: printf(" x(%d)=%.12e",j,xit[j]);
2130: fprintf(ficlog," x(%d)=%.12e",j,xit[j]);
1.126 brouard 2131: }
2132: for(j=1;j<=n;j++) {
1.225 brouard 2133: printf(" p(%d)=%.12e",j,p[j]);
2134: fprintf(ficlog," p(%d)=%.12e",j,p[j]);
1.126 brouard 2135: }
2136: printf("\n");
2137: fprintf(ficlog,"\n");
2138: #endif
1.187 brouard 2139: } /* end loop on each direction i */
2140: /* Convergence test will use last linmin estimation (fret) and compare former iteration (fp) */
1.188 brouard 2141: /* But p and xit have been updated at the end of linmin, *fret corresponds to new p, xit */
1.187 brouard 2142: /* New value of last point Pn is not computed, P(n-1) */
1.224 brouard 2143: for(j=1;j<=n;j++) {
1.225 brouard 2144: if(flatdir[j] >0){
2145: printf(" p(%d)=%lf flat=%d ",j,p[j],flatdir[j]);
2146: fprintf(ficlog," p(%d)=%lf flat=%d ",j,p[j],flatdir[j]);
2147: }
2148: /* printf("\n"); */
2149: /* fprintf(ficlog,"\n"); */
2150: }
1.182 brouard 2151: if (2.0*fabs(fp-(*fret)) <= ftol*(fabs(fp)+fabs(*fret))) { /* Did we reach enough precision? */
1.188 brouard 2152: /* We could compare with a chi^2. chisquare(0.95,ddl=1)=3.84 */
2153: /* By adding age*age in a model, the new -2LL should be lower and the difference follows a */
2154: /* a chisquare statistics with 1 degree. To be significant at the 95% level, it should have */
2155: /* decreased of more than 3.84 */
2156: /* By adding age*age and V1*age the gain (-2LL) should be more than 5.99 (ddl=2) */
2157: /* By using V1+V2+V3, the gain should be 7.82, compared with basic 1+age. */
2158: /* By adding 10 parameters more the gain should be 18.31 */
1.224 brouard 2159:
1.188 brouard 2160: /* Starting the program with initial values given by a former maximization will simply change */
2161: /* the scales of the directions and the directions, because the are reset to canonical directions */
2162: /* Thus the first calls to linmin will give new points and better maximizations until fp-(*fret) is */
2163: /* under the tolerance value. If the tolerance is very small 1.e-9, it could last long. */
1.126 brouard 2164: #ifdef DEBUG
2165: int k[2],l;
2166: k[0]=1;
2167: k[1]=-1;
2168: printf("Max: %.12e",(*func)(p));
2169: fprintf(ficlog,"Max: %.12e",(*func)(p));
2170: for (j=1;j<=n;j++) {
2171: printf(" %.12e",p[j]);
2172: fprintf(ficlog," %.12e",p[j]);
2173: }
2174: printf("\n");
2175: fprintf(ficlog,"\n");
2176: for(l=0;l<=1;l++) {
2177: for (j=1;j<=n;j++) {
2178: ptt[j]=p[j]+(p[j]-pt[j])*k[l];
2179: printf("l=%d j=%d ptt=%.12e, xits=%.12e, p=%.12e, xit=%.12e", l,j,ptt[j],xits[j],p[j],xit[j]);
2180: fprintf(ficlog,"l=%d j=%d ptt=%.12e, xits=%.12e, p=%.12e, xit=%.12e", l,j,ptt[j],xits[j],p[j],xit[j]);
2181: }
2182: printf("func(ptt)=%.12e, deriv=%.12e\n",(*func)(ptt),(ptt[j]-p[j])/((*func)(ptt)-(*func)(p)));
2183: fprintf(ficlog,"func(ptt)=%.12e, deriv=%.12e\n",(*func)(ptt),(ptt[j]-p[j])/((*func)(ptt)-(*func)(p)));
2184: }
2185: #endif
2186:
1.224 brouard 2187: #ifdef LINMINORIGINAL
2188: #else
2189: free_ivector(flatdir,1,n);
2190: #endif
1.126 brouard 2191: free_vector(xit,1,n);
2192: free_vector(xits,1,n);
2193: free_vector(ptt,1,n);
2194: free_vector(pt,1,n);
2195: return;
1.192 brouard 2196: } /* enough precision */
1.126 brouard 2197: if (*iter == ITMAX) nrerror("powell exceeding maximum iterations.");
1.181 brouard 2198: for (j=1;j<=n;j++) { /* Computes the extrapolated point P_0 + 2 (P_n-P_0) */
1.126 brouard 2199: ptt[j]=2.0*p[j]-pt[j];
2200: xit[j]=p[j]-pt[j];
2201: pt[j]=p[j];
2202: }
1.181 brouard 2203: fptt=(*func)(ptt); /* f_3 */
1.224 brouard 2204: #ifdef NODIRECTIONCHANGEDUNTILNITER /* No change in drections until some iterations are done */
2205: if (*iter <=4) {
1.225 brouard 2206: #else
2207: #endif
1.224 brouard 2208: #ifdef POWELLNOF3INFF1TEST /* skips test F3 <F1 */
1.192 brouard 2209: #else
1.161 brouard 2210: if (fptt < fp) { /* If extrapolated point is better, decide if we keep that new direction or not */
1.192 brouard 2211: #endif
1.162 brouard 2212: /* (x1 f1=fp), (x2 f2=*fret), (x3 f3=fptt), (xm fm) */
1.161 brouard 2213: /* From x1 (P0) distance of x2 is at h and x3 is 2h */
1.162 brouard 2214: /* Let f"(x2) be the 2nd derivative equal everywhere. */
2215: /* Then the parabolic through (x1,f1), (x2,f2) and (x3,f3) */
2216: /* will reach at f3 = fm + h^2/2 f"m ; f" = (f1 -2f2 +f3 ) / h**2 */
1.224 brouard 2217: /* Conditional for using this new direction is that mu^2 = (f1-2f2+f3)^2 /2 < del or directest <0 */
2218: /* also lamda^2=(f1-f2)^2/mu² is a parasite solution of powell */
2219: /* For powell, inclusion of this average direction is only if t(del)<0 or del inbetween mu^2 and lambda^2 */
1.161 brouard 2220: /* t=2.0*(fp-2.0*(*fret)+fptt)*SQR(fp-(*fret)-del)-del*SQR(fp-fptt); */
1.224 brouard 2221: /* Even if f3 <f1, directest can be negative and t >0 */
2222: /* mu² and del² are equal when f3=f1 */
2223: /* f3 < f1 : mu² < del <= lambda^2 both test are equivalent */
2224: /* f3 < f1 : mu² < lambda^2 < del then directtest is negative and powell t is positive */
2225: /* f3 > f1 : lambda² < mu^2 < del then t is negative and directest >0 */
2226: /* f3 > f1 : lambda² < del < mu^2 then t is positive and directest >0 */
1.183 brouard 2227: #ifdef NRCORIGINAL
2228: t=2.0*(fp-2.0*(*fret)+fptt)*SQR(fp-(*fret)-del)- del*SQR(fp-fptt); /* Original Numerical Recipes in C*/
2229: #else
2230: 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 2231: t= t- del*SQR(fp-fptt);
1.183 brouard 2232: #endif
1.202 brouard 2233: directest = fp-2.0*(*fret)+fptt - 2.0 * del; /* If delta was big enough we change it for a new direction */
1.161 brouard 2234: #ifdef DEBUG
1.181 brouard 2235: 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);
2236: 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 2237: printf("t3= %.12lf, t4= %.12lf, t3*= %.12lf, t4*= %.12lf\n",SQR(fp-(*fret)-del),SQR(fp-fptt),
2238: (fp-(*fret)-del)*(fp-(*fret)-del),(fp-fptt)*(fp-fptt));
2239: fprintf(ficlog,"t3= %.12lf, t4= %.12lf, t3*= %.12lf, t4*= %.12lf\n",SQR(fp-(*fret)-del),SQR(fp-fptt),
2240: (fp-(*fret)-del)*(fp-(*fret)-del),(fp-fptt)*(fp-fptt));
2241: 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);
2242: 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);
2243: #endif
1.183 brouard 2244: #ifdef POWELLORIGINAL
2245: if (t < 0.0) { /* Then we use it for new direction */
2246: #else
1.182 brouard 2247: if (directest*t < 0.0) { /* Contradiction between both tests */
1.224 brouard 2248: 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 2249: 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 2250: 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 2251: fprintf(ficlog,"f1-2f2+f3= %.12lf, f1-f2-del= %.12lf, f1-f3= %.12lf\n",fp-2.0*(*fret)+fptt, fp -(*fret) -del, fp-fptt);
2252: }
1.181 brouard 2253: if (directest < 0.0) { /* Then we use it for new direction */
2254: #endif
1.191 brouard 2255: #ifdef DEBUGLINMIN
1.234 ! brouard 2256: printf("Before linmin in direction P%d-P0\n",n);
! 2257: for (j=1;j<=n;j++) {
! 2258: printf(" Before xit[%d]= %12.7f p[%d]= %12.7f",j,xit[j],j,p[j]);
! 2259: fprintf(ficlog," Before xit[%d]= %12.7f p[%d]= %12.7f",j,xit[j],j,p[j]);
! 2260: if(j % ncovmodel == 0){
! 2261: printf("\n");
! 2262: fprintf(ficlog,"\n");
! 2263: }
! 2264: }
1.224 brouard 2265: #endif
2266: #ifdef LINMINORIGINAL
1.234 ! brouard 2267: linmin(p,xit,n,fret,func); /* computes minimum on the extrapolated direction: changes p and rescales xit.*/
1.224 brouard 2268: #else
1.234 ! brouard 2269: linmin(p,xit,n,fret,func,&flat); /* computes minimum on the extrapolated direction: changes p and rescales xit.*/
! 2270: flatdir[i]=flat; /* Function is vanishing in that direction i */
1.191 brouard 2271: #endif
1.234 ! brouard 2272:
1.191 brouard 2273: #ifdef DEBUGLINMIN
1.234 ! brouard 2274: for (j=1;j<=n;j++) {
! 2275: printf("After xit[%d]= %12.7f p[%d]= %12.7f",j,xit[j],j,p[j]);
! 2276: fprintf(ficlog,"After xit[%d]= %12.7f p[%d]= %12.7f",j,xit[j],j,p[j]);
! 2277: if(j % ncovmodel == 0){
! 2278: printf("\n");
! 2279: fprintf(ficlog,"\n");
! 2280: }
! 2281: }
1.224 brouard 2282: #endif
1.234 ! brouard 2283: for (j=1;j<=n;j++) {
! 2284: xi[j][ibig]=xi[j][n]; /* Replace direction with biggest decrease by last direction n */
! 2285: xi[j][n]=xit[j]; /* and this nth direction by the by the average p_0 p_n */
! 2286: }
1.224 brouard 2287: #ifdef LINMINORIGINAL
2288: #else
1.234 ! brouard 2289: for (j=1, flatd=0;j<=n;j++) {
! 2290: if(flatdir[j]>0)
! 2291: flatd++;
! 2292: }
! 2293: if(flatd >0){
! 2294: printf("%d flat directions\n",flatd);
! 2295: fprintf(ficlog,"%d flat directions\n",flatd);
! 2296: for (j=1;j<=n;j++) {
! 2297: if(flatdir[j]>0){
! 2298: printf("%d ",j);
! 2299: fprintf(ficlog,"%d ",j);
! 2300: }
! 2301: }
! 2302: printf("\n");
! 2303: fprintf(ficlog,"\n");
! 2304: }
1.191 brouard 2305: #endif
1.234 ! brouard 2306: printf("Gaining to use new average direction of P0 P%d instead of biggest increase direction %d :\n",n,ibig);
! 2307: fprintf(ficlog,"Gaining to use new average direction of P0 P%d instead of biggest increase direction %d :\n",n,ibig);
! 2308:
1.126 brouard 2309: #ifdef DEBUG
1.234 ! brouard 2310: printf("Direction changed last moved %d in place of ibig=%d, new last is the average:\n",n,ibig);
! 2311: fprintf(ficlog,"Direction changed last moved %d in place of ibig=%d, new last is the average:\n",n,ibig);
! 2312: for(j=1;j<=n;j++){
! 2313: printf(" %lf",xit[j]);
! 2314: fprintf(ficlog," %lf",xit[j]);
! 2315: }
! 2316: printf("\n");
! 2317: fprintf(ficlog,"\n");
1.126 brouard 2318: #endif
1.192 brouard 2319: } /* end of t or directest negative */
1.224 brouard 2320: #ifdef POWELLNOF3INFF1TEST
1.192 brouard 2321: #else
1.234 ! brouard 2322: } /* end if (fptt < fp) */
1.192 brouard 2323: #endif
1.225 brouard 2324: #ifdef NODIRECTIONCHANGEDUNTILNITER /* No change in drections until some iterations are done */
1.234 ! brouard 2325: } /*NODIRECTIONCHANGEDUNTILNITER No change in drections until some iterations are done */
1.225 brouard 2326: #else
1.224 brouard 2327: #endif
1.234 ! brouard 2328: } /* loop iteration */
1.126 brouard 2329: }
1.234 ! brouard 2330:
1.126 brouard 2331: /**** Prevalence limit (stable or period prevalence) ****************/
1.234 ! brouard 2332:
1.203 brouard 2333: double **prevalim(double **prlim, int nlstate, double x[], double age, double **oldm, double **savm, double ftolpl, int *ncvyear, int ij)
1.234 ! brouard 2334: {
! 2335: /* Computes the prevalence limit in each live state at age x and for covariate combiation ij by left multiplying the unit
! 2336: matrix by transitions matrix until convergence is reached with precision ftolpl */
1.206 brouard 2337: /* Wx= Wx-1 Px-1= Wx-2 Px-2 Px-1 = Wx-n Px-n ... Px-2 Px-1 I */
2338: /* Wx is row vector: population in state 1, population in state 2, population dead */
2339: /* or prevalence in state 1, prevalence in state 2, 0 */
2340: /* newm is the matrix after multiplications, its rows are identical at a factor */
2341: /* Initial matrix pimij */
2342: /* {0.85204250825084937, 0.13044499163996345, 0.017512500109187184, */
2343: /* 0.090851990222114765, 0.88271245433047185, 0.026435555447413338, */
2344: /* 0, 0 , 1} */
2345: /*
2346: * and after some iteration: */
2347: /* {0.45504275246439968, 0.42731458730878791, 0.11764266022681241, */
2348: /* 0.45201005341706885, 0.42865420071559901, 0.11933574586733192, */
2349: /* 0, 0 , 1} */
2350: /* And prevalence by suppressing the deaths are close to identical rows in prlim: */
2351: /* {0.51571254859325999, 0.4842874514067399, */
2352: /* 0.51326036147820708, 0.48673963852179264} */
2353: /* If we start from prlim again, prlim tends to a constant matrix */
1.234 ! brouard 2354:
1.126 brouard 2355: int i, ii,j,k;
1.209 brouard 2356: double *min, *max, *meandiff, maxmax,sumnew=0.;
1.145 brouard 2357: /* double **matprod2(); */ /* test */
1.218 brouard 2358: double **out, cov[NCOVMAX+1], **pmij(); /* **pmmij is a global variable feeded with oldms etc */
1.126 brouard 2359: double **newm;
1.209 brouard 2360: double agefin, delaymax=200. ; /* 100 Max number of years to converge */
1.203 brouard 2361: int ncvloop=0;
1.169 brouard 2362:
1.209 brouard 2363: min=vector(1,nlstate);
2364: max=vector(1,nlstate);
2365: meandiff=vector(1,nlstate);
2366:
1.218 brouard 2367: /* Starting with matrix unity */
1.126 brouard 2368: for (ii=1;ii<=nlstate+ndeath;ii++)
2369: for (j=1;j<=nlstate+ndeath;j++){
2370: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
2371: }
1.169 brouard 2372:
2373: cov[1]=1.;
2374:
2375: /* Even if hstepm = 1, at least one multiplication by the unit matrix */
1.202 brouard 2376: /* Start at agefin= age, computes the matrix of passage and loops decreasing agefin until convergence is reached */
1.126 brouard 2377: for(agefin=age-stepm/YEARM; agefin>=age-delaymax; agefin=agefin-stepm/YEARM){
1.202 brouard 2378: ncvloop++;
1.126 brouard 2379: newm=savm;
2380: /* Covariates have to be included here again */
1.138 brouard 2381: cov[2]=agefin;
1.187 brouard 2382: if(nagesqr==1)
2383: cov[3]= agefin*agefin;;
1.234 ! brouard 2384: for (k=1; k<=nsd;k++) { /* For single dummy covariates only */
! 2385: /* Here comes the value of the covariate 'ij' after renumbering k with single dummy covariates */
! 2386: cov[2+nagesqr+TvarsDind[k]]=nbcode[TvarsD[k]][codtabm(ij,k)];
! 2387: printf("prevalim ij=%d k=%d TvarsD[%d]=%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));
! 2388: }
! 2389: for (k=1; k<=nsq;k++) { /* For single varying covariates only */
! 2390: /* Here comes the value of quantitative after renumbering k with single quantitative covariates */
! 2391: /* cov[2+nagesqr+TvarsQind[k]]=qselvar[k]; */
! 2392: printf("prevalim ij=%d k=%d TvarsQind[%d]=%d \n",ij,k,k,TvarsQind[k]);
1.138 brouard 2393: }
1.186 brouard 2394: /*wrong? for (k=1; k<=cptcovage;k++) cov[2+Tage[k]]=cov[2+Tage[k]]*cov[2]; */
1.200 brouard 2395: /* for (k=1; k<=cptcovage;k++) cov[2+nagesqr+Tage[k]]=nbcode[Tvar[k]][codtabm(ij,Tvar[k])]*cov[2]; */
1.234 ! brouard 2396: for (k=1; k<=cptcovage;k++){
! 2397: if(Dummy[Tvar[Tage[k]]]){
! 2398: cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,k)]*cov[2];
! 2399: } else{
! 2400: ;
! 2401: /* cov[2+nagesqr+Tage[k]]=qselvar[k]; */
! 2402: }
! 2403: printf("prevalim Age ij=%d k=%d Tage[%d]=%d \n",ij,k,k,Tage[k]);
! 2404: }
! 2405: for (k=1; k<=cptcovprod;k++){ /* */
! 2406: printf("prevalim Prod ij=%d k=%d Tprod[%d]=%d Tvard[%d][1]=%d, Tvard[%d][2]=%d\n",ij,k,k,Tprod[k], k,Tvard[k][1], k,Tvard[k][2]);
1.200 brouard 2407: cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)] * nbcode[Tvard[k][2]][codtabm(ij,k)];
1.234 ! brouard 2408: }
1.138 brouard 2409: /*printf("ij=%d cptcovprod=%d tvar=%d ", ij, cptcovprod, Tvar[1]);*/
2410: /*printf("ij=%d cov[3]=%lf cov[4]=%lf \n",ij, cov[3],cov[4]);*/
2411: /*printf("ij=%d cov[3]=%lf \n",ij, cov[3]);*/
1.145 brouard 2412: /* savm=pmij(pmmij,cov,ncovmodel,x,nlstate); */
2413: /* out=matprod2(newm, pmij(pmmij,cov,ncovmodel,x,nlstate),1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm); /\* Bug Valgrind *\/ */
1.218 brouard 2414: /* age and covariate values of ij are in 'cov' */
1.142 brouard 2415: out=matprod2(newm, pmij(pmmij,cov,ncovmodel,x,nlstate),1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm); /* Bug Valgrind */
1.138 brouard 2416:
1.126 brouard 2417: savm=oldm;
2418: oldm=newm;
1.209 brouard 2419:
2420: for(j=1; j<=nlstate; j++){
2421: max[j]=0.;
2422: min[j]=1.;
2423: }
2424: for(i=1;i<=nlstate;i++){
2425: sumnew=0;
2426: for(k=1; k<=ndeath; k++) sumnew+=newm[i][nlstate+k];
2427: for(j=1; j<=nlstate; j++){
2428: prlim[i][j]= newm[i][j]/(1-sumnew);
2429: max[j]=FMAX(max[j],prlim[i][j]);
2430: min[j]=FMIN(min[j],prlim[i][j]);
2431: }
2432: }
2433:
1.126 brouard 2434: maxmax=0.;
1.209 brouard 2435: for(j=1; j<=nlstate; j++){
2436: meandiff[j]=(max[j]-min[j])/(max[j]+min[j])*2.; /* mean difference for each column */
2437: maxmax=FMAX(maxmax,meandiff[j]);
2438: /* 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 2439: } /* j loop */
1.203 brouard 2440: *ncvyear= (int)age- (int)agefin;
1.208 brouard 2441: /* 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 2442: if(maxmax < ftolpl){
1.209 brouard 2443: /* printf("maxmax=%lf ncvloop=%ld, age=%d, agefin=%d ncvyear=%d \n", maxmax, ncvloop, (int)age, (int)agefin, *ncvyear); */
2444: free_vector(min,1,nlstate);
2445: free_vector(max,1,nlstate);
2446: free_vector(meandiff,1,nlstate);
1.126 brouard 2447: return prlim;
2448: }
1.169 brouard 2449: } /* age loop */
1.208 brouard 2450: /* After some age loop it doesn't converge */
1.209 brouard 2451: 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 2452: 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 2453: /* 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); */
2454: free_vector(min,1,nlstate);
2455: free_vector(max,1,nlstate);
2456: free_vector(meandiff,1,nlstate);
1.208 brouard 2457:
1.169 brouard 2458: return prlim; /* should not reach here */
1.126 brouard 2459: }
2460:
1.217 brouard 2461:
2462: /**** Back Prevalence limit (stable or period prevalence) ****************/
2463:
1.218 brouard 2464: /* 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) */
2465: /* 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) */
2466: double **bprevalim(double **bprlim, double ***prevacurrent, int nlstate, double x[], double age, double ftolpl, int *ncvyear, int ij)
1.217 brouard 2467: {
1.218 brouard 2468: /* Computes the prevalence limit in each live state at age x and covariate ij by left multiplying the unit
1.217 brouard 2469: matrix by transitions matrix until convergence is reached with precision ftolpl */
2470: /* Wx= Wx-1 Px-1= Wx-2 Px-2 Px-1 = Wx-n Px-n ... Px-2 Px-1 I */
2471: /* Wx is row vector: population in state 1, population in state 2, population dead */
2472: /* or prevalence in state 1, prevalence in state 2, 0 */
2473: /* newm is the matrix after multiplications, its rows are identical at a factor */
2474: /* Initial matrix pimij */
2475: /* {0.85204250825084937, 0.13044499163996345, 0.017512500109187184, */
2476: /* 0.090851990222114765, 0.88271245433047185, 0.026435555447413338, */
2477: /* 0, 0 , 1} */
2478: /*
2479: * and after some iteration: */
2480: /* {0.45504275246439968, 0.42731458730878791, 0.11764266022681241, */
2481: /* 0.45201005341706885, 0.42865420071559901, 0.11933574586733192, */
2482: /* 0, 0 , 1} */
2483: /* And prevalence by suppressing the deaths are close to identical rows in prlim: */
2484: /* {0.51571254859325999, 0.4842874514067399, */
2485: /* 0.51326036147820708, 0.48673963852179264} */
2486: /* If we start from prlim again, prlim tends to a constant matrix */
2487:
2488: int i, ii,j,k;
2489: double *min, *max, *meandiff, maxmax,sumnew=0.;
2490: /* double **matprod2(); */ /* test */
2491: double **out, cov[NCOVMAX+1], **bmij();
2492: double **newm;
1.218 brouard 2493: double **dnewm, **doldm, **dsavm; /* for use */
2494: double **oldm, **savm; /* for use */
2495:
1.217 brouard 2496: double agefin, delaymax=200. ; /* 100 Max number of years to converge */
2497: int ncvloop=0;
2498:
2499: min=vector(1,nlstate);
2500: max=vector(1,nlstate);
2501: meandiff=vector(1,nlstate);
2502:
1.218 brouard 2503: dnewm=ddnewms; doldm=ddoldms; dsavm=ddsavms;
2504: oldm=oldms; savm=savms;
2505:
2506: /* Starting with matrix unity */
2507: for (ii=1;ii<=nlstate+ndeath;ii++)
2508: for (j=1;j<=nlstate+ndeath;j++){
1.217 brouard 2509: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
2510: }
2511:
2512: cov[1]=1.;
2513:
2514: /* Even if hstepm = 1, at least one multiplication by the unit matrix */
2515: /* Start at agefin= age, computes the matrix of passage and loops decreasing agefin until convergence is reached */
1.218 brouard 2516: /* for(agefin=age+stepm/YEARM; agefin<=age+delaymax; agefin=agefin+stepm/YEARM){ /\* A changer en age *\/ */
2517: for(agefin=age; agefin<AGESUP; agefin=agefin+stepm/YEARM){ /* A changer en age */
1.217 brouard 2518: ncvloop++;
1.218 brouard 2519: newm=savm; /* oldm should be kept from previous iteration or unity at start */
2520: /* newm points to the allocated table savm passed by the function it can be written, savm could be reallocated */
1.217 brouard 2521: /* Covariates have to be included here again */
2522: cov[2]=agefin;
2523: if(nagesqr==1)
2524: cov[3]= agefin*agefin;;
2525: for (k=1; k<=cptcovn;k++) {
2526: /* cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,Tvar[k])]; */
2527: cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,k)];
2528: /* 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])]); */
2529: }
2530: /*wrong? for (k=1; k<=cptcovage;k++) cov[2+Tage[k]]=cov[2+Tage[k]]*cov[2]; */
2531: /* for (k=1; k<=cptcovage;k++) cov[2+nagesqr+Tage[k]]=nbcode[Tvar[k]][codtabm(ij,Tvar[k])]*cov[2]; */
2532: for (k=1; k<=cptcovage;k++) cov[2+nagesqr+Tage[k]]=nbcode[Tvar[k]][codtabm(ij,k)]*cov[2];
2533: for (k=1; k<=cptcovprod;k++) /* Useless */
2534: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,Tvard[k][1])] * nbcode[Tvard[k][2]][codtabm(ij,Tvard[k][2])]; */
2535: cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)] * nbcode[Tvard[k][2]][codtabm(ij,k)];
2536:
2537: /*printf("ij=%d cptcovprod=%d tvar=%d ", ij, cptcovprod, Tvar[1]);*/
2538: /*printf("ij=%d cov[3]=%lf cov[4]=%lf \n",ij, cov[3],cov[4]);*/
2539: /*printf("ij=%d cov[3]=%lf \n",ij, cov[3]);*/
2540: /* savm=pmij(pmmij,cov,ncovmodel,x,nlstate); */
2541: /* out=matprod2(newm, pmij(pmmij,cov,ncovmodel,x,nlstate),1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm); /\* Bug Valgrind *\/ */
1.218 brouard 2542: /* ij should be linked to the correct index of cov */
2543: /* age and covariate values ij are in 'cov', but we need to pass
2544: * ij for the observed prevalence at age and status and covariate
2545: * number: prevacurrent[(int)agefin][ii][ij]
2546: */
2547: /* 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 *\/ */
2548: /* 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 *\/ */
2549: 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 2550: savm=oldm;
2551: oldm=newm;
2552: for(j=1; j<=nlstate; j++){
2553: max[j]=0.;
2554: min[j]=1.;
2555: }
2556: for(j=1; j<=nlstate; j++){
2557: for(i=1;i<=nlstate;i++){
1.234 ! brouard 2558: /* bprlim[i][j]= newm[i][j]/(1-sumnew); */
! 2559: bprlim[i][j]= newm[i][j];
! 2560: max[i]=FMAX(max[i],bprlim[i][j]); /* Max in line */
! 2561: min[i]=FMIN(min[i],bprlim[i][j]);
1.217 brouard 2562: }
2563: }
1.218 brouard 2564:
1.217 brouard 2565: maxmax=0.;
2566: for(i=1; i<=nlstate; i++){
2567: meandiff[i]=(max[i]-min[i])/(max[i]+min[i])*2.; /* mean difference for each column */
2568: maxmax=FMAX(maxmax,meandiff[i]);
2569: /* 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); */
2570: } /* j loop */
2571: *ncvyear= -( (int)age- (int)agefin);
1.218 brouard 2572: /* printf("Back maxmax=%lf ncvloop=%d, age=%d, agefin=%d ncvyear=%d \n", maxmax, ncvloop, (int)age, (int)agefin, *ncvyear);*/
1.217 brouard 2573: if(maxmax < ftolpl){
1.220 brouard 2574: /* printf("OK Back maxmax=%lf ncvloop=%d, age=%d, agefin=%d ncvyear=%d \n", maxmax, ncvloop, (int)age, (int)agefin, *ncvyear); */
1.217 brouard 2575: free_vector(min,1,nlstate);
2576: free_vector(max,1,nlstate);
2577: free_vector(meandiff,1,nlstate);
2578: return bprlim;
2579: }
2580: } /* age loop */
2581: /* After some age loop it doesn't converge */
2582: 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\
2583: 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);
2584: /* 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); */
2585: free_vector(min,1,nlstate);
2586: free_vector(max,1,nlstate);
2587: free_vector(meandiff,1,nlstate);
2588:
2589: return bprlim; /* should not reach here */
2590: }
2591:
1.126 brouard 2592: /*************** transition probabilities ***************/
2593:
2594: double **pmij(double **ps, double *cov, int ncovmodel, double *x, int nlstate )
2595: {
1.138 brouard 2596: /* According to parameters values stored in x and the covariate's values stored in cov,
2597: computes the probability to be observed in state j being in state i by appying the
2598: model to the ncovmodel covariates (including constant and age).
2599: lnpijopii=ln(pij/pii)= aij+bij*age+cij*v1+dij*v2+... = sum_nc=1^ncovmodel xij(nc)*cov[nc]
2600: and, according on how parameters are entered, the position of the coefficient xij(nc) of the
2601: ncth covariate in the global vector x is given by the formula:
2602: j<i nc+((i-1)*(nlstate+ndeath-1)+j-1)*ncovmodel
2603: j>=i nc + ((i-1)*(nlstate+ndeath-1)+(j-2))*ncovmodel
2604: Computes ln(pij/pii) (lnpijopii), deduces pij/pii by exponentiation,
2605: sums on j different of i to get 1-pii/pii, deduces pii, and then all pij.
2606: Outputs ps[i][j] the probability to be observed in j being in j according to
2607: the values of the covariates cov[nc] and corresponding parameter values x[nc+shiftij]
2608: */
2609: double s1, lnpijopii;
1.126 brouard 2610: /*double t34;*/
1.164 brouard 2611: int i,j, nc, ii, jj;
1.126 brouard 2612:
1.223 brouard 2613: for(i=1; i<= nlstate; i++){
2614: for(j=1; j<i;j++){
2615: for (nc=1, lnpijopii=0.;nc <=ncovmodel; nc++){
2616: /*lnpijopii += param[i][j][nc]*cov[nc];*/
2617: lnpijopii += x[nc+((i-1)*(nlstate+ndeath-1)+j-1)*ncovmodel]*cov[nc];
2618: /* printf("Int j<i s1=%.17e, lnpijopii=%.17e\n",s1,lnpijopii); */
2619: }
2620: ps[i][j]=lnpijopii; /* In fact ln(pij/pii) */
2621: /* printf("s1=%.17e, lnpijopii=%.17e\n",s1,lnpijopii); */
2622: }
2623: for(j=i+1; j<=nlstate+ndeath;j++){
2624: for (nc=1, lnpijopii=0.;nc <=ncovmodel; nc++){
2625: /*lnpijopii += x[(i-1)*nlstate*ncovmodel+(j-2)*ncovmodel+nc+(i-1)*(ndeath-1)*ncovmodel]*cov[nc];*/
2626: lnpijopii += x[nc + ((i-1)*(nlstate+ndeath-1)+(j-2))*ncovmodel]*cov[nc];
2627: /* printf("Int j>i s1=%.17e, lnpijopii=%.17e %lx %lx\n",s1,lnpijopii,s1,lnpijopii); */
2628: }
2629: ps[i][j]=lnpijopii; /* In fact ln(pij/pii) */
2630: }
2631: }
1.218 brouard 2632:
1.223 brouard 2633: for(i=1; i<= nlstate; i++){
2634: s1=0;
2635: for(j=1; j<i; j++){
2636: s1+=exp(ps[i][j]); /* In fact sums pij/pii */
2637: /*printf("debug1 %d %d ps=%lf exp(ps)=%lf s1+=%lf\n",i,j,ps[i][j],exp(ps[i][j]),s1); */
2638: }
2639: for(j=i+1; j<=nlstate+ndeath; j++){
2640: s1+=exp(ps[i][j]); /* In fact sums pij/pii */
2641: /*printf("debug2 %d %d ps=%lf exp(ps)=%lf s1+=%lf\n",i,j,ps[i][j],exp(ps[i][j]),s1); */
2642: }
2643: /* s1= sum_{j<>i} pij/pii=(1-pii)/pii and thus pii is known from s1 */
2644: ps[i][i]=1./(s1+1.);
2645: /* Computing other pijs */
2646: for(j=1; j<i; j++)
2647: ps[i][j]= exp(ps[i][j])*ps[i][i];
2648: for(j=i+1; j<=nlstate+ndeath; j++)
2649: ps[i][j]= exp(ps[i][j])*ps[i][i];
2650: /* ps[i][nlstate+1]=1.-s1- ps[i][i];*/ /* Sum should be 1 */
2651: } /* end i */
1.218 brouard 2652:
1.223 brouard 2653: for(ii=nlstate+1; ii<= nlstate+ndeath; ii++){
2654: for(jj=1; jj<= nlstate+ndeath; jj++){
2655: ps[ii][jj]=0;
2656: ps[ii][ii]=1;
2657: }
2658: }
1.218 brouard 2659:
2660:
1.223 brouard 2661: /* for(ii=1; ii<= nlstate+ndeath; ii++){ */
2662: /* for(jj=1; jj<= nlstate+ndeath; jj++){ */
2663: /* printf(" pmij ps[%d][%d]=%lf ",ii,jj,ps[ii][jj]); */
2664: /* } */
2665: /* printf("\n "); */
2666: /* } */
2667: /* printf("\n ");printf("%lf ",cov[2]);*/
2668: /*
2669: for(i=1; i<= npar; i++) printf("%f ",x[i]);
1.218 brouard 2670: goto end;*/
1.223 brouard 2671: return ps;
1.126 brouard 2672: }
2673:
1.218 brouard 2674: /*************** backward transition probabilities ***************/
2675:
2676: /* 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 ) */
2677: /* double **bmij(double **ps, double *cov, int ncovmodel, double *x, int nlstate, double ***prevacurrent, double ***dnewm, double **doldm, double **dsavm, int ij ) */
2678: double **bmij(double **ps, double *cov, int ncovmodel, double *x, int nlstate, double ***prevacurrent, int ij )
2679: {
1.222 brouard 2680: /* Computes the backward probability at age agefin and covariate ij
2681: * and returns in **ps as well as **bmij.
2682: */
1.218 brouard 2683: int i, ii, j,k;
1.222 brouard 2684:
2685: double **out, **pmij();
2686: double sumnew=0.;
1.218 brouard 2687: double agefin;
1.222 brouard 2688:
2689: double **dnewm, **dsavm, **doldm;
2690: double **bbmij;
2691:
1.218 brouard 2692: doldm=ddoldms; /* global pointers */
1.222 brouard 2693: dnewm=ddnewms;
2694: dsavm=ddsavms;
2695:
2696: agefin=cov[2];
2697: /* bmij *//* age is cov[2], ij is included in cov, but we need for
2698: the observed prevalence (with this covariate ij) */
2699: dsavm=pmij(pmmij,cov,ncovmodel,x,nlstate);
2700: /* We do have the matrix Px in savm and we need pij */
2701: for (j=1;j<=nlstate+ndeath;j++){
2702: sumnew=0.; /* w1 p11 + w2 p21 only on live states */
2703: for (ii=1;ii<=nlstate;ii++){
2704: sumnew+=dsavm[ii][j]*prevacurrent[(int)agefin][ii][ij];
2705: } /* sumnew is (N11+N21)/N..= N.1/N.. = sum on i of w_i pij */
2706: for (ii=1;ii<=nlstate+ndeath;ii++){
2707: if(sumnew >= 1.e-10){
2708: /* if(agefin >= agemaxpar && agefin <= agemaxpar+stepm/YEARM){ */
2709: /* doldm[ii][j]=(ii==j ? 1./sumnew : 0.0); */
2710: /* }else if(agefin >= agemaxpar+stepm/YEARM){ */
2711: /* doldm[ii][j]=(ii==j ? 1./sumnew : 0.0); */
2712: /* }else */
2713: doldm[ii][j]=(ii==j ? 1./sumnew : 0.0);
2714: }else{
2715: 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);
2716: }
2717: } /*End ii */
2718: } /* End j, At the end doldm is diag[1/(w_1p1i+w_2 p2i)] */
2719: /* left Product of this diag matrix by dsavm=Px (newm=dsavm*doldm) */
2720: bbmij=matprod2(dnewm, dsavm,1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, doldm); /* Bug Valgrind */
2721: /* dsavm=doldm; /\* dsavm is now diag [1/(w_1p1i+w_2 p2i)] but can be overwritten*\/ */
2722: /* doldm=dnewm; /\* doldm is now Px * diag [1/(w_1p1i+w_2 p2i)] *\/ */
2723: /* dnewm=dsavm; /\* doldm is now Px * diag [1/(w_1p1i+w_2 p2i)] *\/ */
2724: /* left Product of this matrix by diag matrix of prevalences (savm) */
2725: for (j=1;j<=nlstate+ndeath;j++){
2726: for (ii=1;ii<=nlstate+ndeath;ii++){
2727: dsavm[ii][j]=(ii==j ? prevacurrent[(int)agefin][ii][ij] : 0.0);
2728: }
2729: } /* End j, At the end oldm is diag[1/(w_1p1i+w_2 p2i)] */
2730: ps=matprod2(doldm, dsavm,1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, dnewm); /* Bug Valgrind */
2731: /* newm or out is now diag[w_i] * Px * diag [1/(w_1p1i+w_2 p2i)] */
2732: /* end bmij */
2733: return ps;
1.218 brouard 2734: }
1.217 brouard 2735: /*************** transition probabilities ***************/
2736:
1.218 brouard 2737: double **bpmij(double **ps, double *cov, int ncovmodel, double *x, int nlstate )
1.217 brouard 2738: {
2739: /* According to parameters values stored in x and the covariate's values stored in cov,
2740: computes the probability to be observed in state j being in state i by appying the
2741: model to the ncovmodel covariates (including constant and age).
2742: lnpijopii=ln(pij/pii)= aij+bij*age+cij*v1+dij*v2+... = sum_nc=1^ncovmodel xij(nc)*cov[nc]
2743: and, according on how parameters are entered, the position of the coefficient xij(nc) of the
2744: ncth covariate in the global vector x is given by the formula:
2745: j<i nc+((i-1)*(nlstate+ndeath-1)+j-1)*ncovmodel
2746: j>=i nc + ((i-1)*(nlstate+ndeath-1)+(j-2))*ncovmodel
2747: Computes ln(pij/pii) (lnpijopii), deduces pij/pii by exponentiation,
2748: sums on j different of i to get 1-pii/pii, deduces pii, and then all pij.
2749: Outputs ps[i][j] the probability to be observed in j being in j according to
2750: the values of the covariates cov[nc] and corresponding parameter values x[nc+shiftij]
2751: */
2752: double s1, lnpijopii;
2753: /*double t34;*/
2754: int i,j, nc, ii, jj;
2755:
1.234 ! brouard 2756: for(i=1; i<= nlstate; i++){
! 2757: for(j=1; j<i;j++){
! 2758: for (nc=1, lnpijopii=0.;nc <=ncovmodel; nc++){
! 2759: /*lnpijopii += param[i][j][nc]*cov[nc];*/
! 2760: lnpijopii += x[nc+((i-1)*(nlstate+ndeath-1)+j-1)*ncovmodel]*cov[nc];
! 2761: /* printf("Int j<i s1=%.17e, lnpijopii=%.17e\n",s1,lnpijopii); */
! 2762: }
! 2763: ps[i][j]=lnpijopii; /* In fact ln(pij/pii) */
! 2764: /* printf("s1=%.17e, lnpijopii=%.17e\n",s1,lnpijopii); */
! 2765: }
! 2766: for(j=i+1; j<=nlstate+ndeath;j++){
! 2767: for (nc=1, lnpijopii=0.;nc <=ncovmodel; nc++){
! 2768: /*lnpijopii += x[(i-1)*nlstate*ncovmodel+(j-2)*ncovmodel+nc+(i-1)*(ndeath-1)*ncovmodel]*cov[nc];*/
! 2769: lnpijopii += x[nc + ((i-1)*(nlstate+ndeath-1)+(j-2))*ncovmodel]*cov[nc];
! 2770: /* printf("Int j>i s1=%.17e, lnpijopii=%.17e %lx %lx\n",s1,lnpijopii,s1,lnpijopii); */
! 2771: }
! 2772: ps[i][j]=lnpijopii; /* In fact ln(pij/pii) */
! 2773: }
! 2774: }
! 2775:
! 2776: for(i=1; i<= nlstate; i++){
! 2777: s1=0;
! 2778: for(j=1; j<i; j++){
! 2779: s1+=exp(ps[i][j]); /* In fact sums pij/pii */
! 2780: /*printf("debug1 %d %d ps=%lf exp(ps)=%lf s1+=%lf\n",i,j,ps[i][j],exp(ps[i][j]),s1); */
! 2781: }
! 2782: for(j=i+1; j<=nlstate+ndeath; j++){
! 2783: s1+=exp(ps[i][j]); /* In fact sums pij/pii */
! 2784: /*printf("debug2 %d %d ps=%lf exp(ps)=%lf s1+=%lf\n",i,j,ps[i][j],exp(ps[i][j]),s1); */
! 2785: }
! 2786: /* s1= sum_{j<>i} pij/pii=(1-pii)/pii and thus pii is known from s1 */
! 2787: ps[i][i]=1./(s1+1.);
! 2788: /* Computing other pijs */
! 2789: for(j=1; j<i; j++)
! 2790: ps[i][j]= exp(ps[i][j])*ps[i][i];
! 2791: for(j=i+1; j<=nlstate+ndeath; j++)
! 2792: ps[i][j]= exp(ps[i][j])*ps[i][i];
! 2793: /* ps[i][nlstate+1]=1.-s1- ps[i][i];*/ /* Sum should be 1 */
! 2794: } /* end i */
! 2795:
! 2796: for(ii=nlstate+1; ii<= nlstate+ndeath; ii++){
! 2797: for(jj=1; jj<= nlstate+ndeath; jj++){
! 2798: ps[ii][jj]=0;
! 2799: ps[ii][ii]=1;
! 2800: }
! 2801: }
! 2802: /* Added for backcast */ /* Transposed matrix too */
! 2803: for(jj=1; jj<= nlstate+ndeath; jj++){
! 2804: s1=0.;
! 2805: for(ii=1; ii<= nlstate+ndeath; ii++){
! 2806: s1+=ps[ii][jj];
! 2807: }
! 2808: for(ii=1; ii<= nlstate; ii++){
! 2809: ps[ii][jj]=ps[ii][jj]/s1;
! 2810: }
! 2811: }
! 2812: /* Transposition */
! 2813: for(jj=1; jj<= nlstate+ndeath; jj++){
! 2814: for(ii=jj; ii<= nlstate+ndeath; ii++){
! 2815: s1=ps[ii][jj];
! 2816: ps[ii][jj]=ps[jj][ii];
! 2817: ps[jj][ii]=s1;
! 2818: }
! 2819: }
! 2820: /* for(ii=1; ii<= nlstate+ndeath; ii++){ */
! 2821: /* for(jj=1; jj<= nlstate+ndeath; jj++){ */
! 2822: /* printf(" pmij ps[%d][%d]=%lf ",ii,jj,ps[ii][jj]); */
! 2823: /* } */
! 2824: /* printf("\n "); */
! 2825: /* } */
! 2826: /* printf("\n ");printf("%lf ",cov[2]);*/
! 2827: /*
! 2828: for(i=1; i<= npar; i++) printf("%f ",x[i]);
! 2829: goto end;*/
! 2830: return ps;
1.217 brouard 2831: }
2832:
2833:
1.126 brouard 2834: /**************** Product of 2 matrices ******************/
2835:
1.145 brouard 2836: double **matprod2(double **out, double **in,int nrl, int nrh, int ncl, int nch, int ncolol, int ncoloh, double **b)
1.126 brouard 2837: {
2838: /* Computes the matrix product of in(1,nrh-nrl+1)(1,nch-ncl+1) times
2839: b(1,nch-ncl+1)(1,ncoloh-ncolol+1) into out(...) */
2840: /* in, b, out are matrice of pointers which should have been initialized
2841: before: only the contents of out is modified. The function returns
2842: a pointer to pointers identical to out */
1.145 brouard 2843: int i, j, k;
1.126 brouard 2844: for(i=nrl; i<= nrh; i++)
1.145 brouard 2845: for(k=ncolol; k<=ncoloh; k++){
2846: out[i][k]=0.;
2847: for(j=ncl; j<=nch; j++)
2848: out[i][k] +=in[i][j]*b[j][k];
2849: }
1.126 brouard 2850: return out;
2851: }
2852:
2853:
2854: /************* Higher Matrix Product ***************/
2855:
2856: double ***hpxij(double ***po, int nhstepm, double age, int hstepm, double *x, int nlstate, int stepm, double **oldm, double **savm, int ij )
2857: {
1.218 brouard 2858: /* Computes the transition matrix starting at age 'age' and combination of covariate values corresponding to ij over
1.126 brouard 2859: 'nhstepm*hstepm*stepm' months (i.e. until
2860: age (in years) age+nhstepm*hstepm*stepm/12) by multiplying
2861: nhstepm*hstepm matrices.
2862: Output is stored in matrix po[i][j][h] for h every 'hstepm' step
2863: (typically every 2 years instead of every month which is too big
2864: for the memory).
2865: Model is determined by parameters x and covariates have to be
2866: included manually here.
2867:
2868: */
2869:
2870: int i, j, d, h, k;
1.131 brouard 2871: double **out, cov[NCOVMAX+1];
1.126 brouard 2872: double **newm;
1.187 brouard 2873: double agexact;
1.214 brouard 2874: double agebegin, ageend;
1.126 brouard 2875:
2876: /* Hstepm could be zero and should return the unit matrix */
2877: for (i=1;i<=nlstate+ndeath;i++)
2878: for (j=1;j<=nlstate+ndeath;j++){
2879: oldm[i][j]=(i==j ? 1.0 : 0.0);
2880: po[i][j][0]=(i==j ? 1.0 : 0.0);
2881: }
2882: /* Even if hstepm = 1, at least one multiplication by the unit matrix */
2883: for(h=1; h <=nhstepm; h++){
2884: for(d=1; d <=hstepm; d++){
2885: newm=savm;
2886: /* Covariates have to be included here again */
2887: cov[1]=1.;
1.214 brouard 2888: agexact=age+((h-1)*hstepm + (d-1))*stepm/YEARM; /* age just before transition */
1.187 brouard 2889: cov[2]=agexact;
2890: if(nagesqr==1)
1.227 brouard 2891: cov[3]= agexact*agexact;
1.131 brouard 2892: for (k=1; k<=cptcovn;k++)
1.227 brouard 2893: cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,k)];
2894: /* cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,Tvar[k])]; */
1.186 brouard 2895: for (k=1; k<=cptcovage;k++) /* Should start at cptcovn+1 */
1.227 brouard 2896: /* cov[2+Tage[k]]=cov[2+Tage[k]]*cov[2]; */
2897: cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,k)]*cov[2];
2898: /* cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,Tvar[Tage[k]])]*cov[2]; */
1.145 brouard 2899: for (k=1; k<=cptcovprod;k++) /* Useless because included in cptcovn */
1.227 brouard 2900: cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)]*nbcode[Tvard[k][2]][codtabm(ij,k)];
2901: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,Tvard[k][1])]*nbcode[Tvard[k][2]][codtabm(ij,Tvard[k][2])]; */
2902:
2903:
1.126 brouard 2904: /*printf("hxi cptcov=%d cptcode=%d\n",cptcov,cptcode);*/
2905: /*printf("h=%d d=%d age=%f cov=%f\n",h,d,age,cov[2]);*/
1.218 brouard 2906: /* right multiplication of oldm by the current matrix */
1.126 brouard 2907: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath,
2908: pmij(pmmij,cov,ncovmodel,x,nlstate));
1.217 brouard 2909: /* if((int)age == 70){ */
2910: /* printf(" Forward hpxij age=%d agexact=%f d=%d nhstepm=%d hstepm=%d\n", (int) age, agexact, d, nhstepm, hstepm); */
2911: /* for(i=1; i<=nlstate+ndeath; i++) { */
2912: /* printf("%d pmmij ",i); */
2913: /* for(j=1;j<=nlstate+ndeath;j++) { */
2914: /* printf("%f ",pmmij[i][j]); */
2915: /* } */
2916: /* printf(" oldm "); */
2917: /* for(j=1;j<=nlstate+ndeath;j++) { */
2918: /* printf("%f ",oldm[i][j]); */
2919: /* } */
2920: /* printf("\n"); */
2921: /* } */
2922: /* } */
1.126 brouard 2923: savm=oldm;
2924: oldm=newm;
2925: }
2926: for(i=1; i<=nlstate+ndeath; i++)
2927: for(j=1;j<=nlstate+ndeath;j++) {
1.218 brouard 2928: po[i][j][h]=newm[i][j];
2929: /*if(h==nhstepm) printf("po[%d][%d][%d]=%f ",i,j,h,po[i][j][h]);*/
1.126 brouard 2930: }
1.128 brouard 2931: /*printf("h=%d ",h);*/
1.126 brouard 2932: } /* end h */
1.218 brouard 2933: /* printf("\n H=%d \n",h); */
1.126 brouard 2934: return po;
2935: }
2936:
1.217 brouard 2937: /************* Higher Back Matrix Product ***************/
1.218 brouard 2938: /* 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 2939: double ***hbxij(double ***po, int nhstepm, double age, int hstepm, double *x, double ***prevacurrent, int nlstate, int stepm, int ij )
1.217 brouard 2940: {
1.218 brouard 2941: /* Computes the transition matrix starting at age 'age' over
1.217 brouard 2942: 'nhstepm*hstepm*stepm' months (i.e. until
1.218 brouard 2943: age (in years) age+nhstepm*hstepm*stepm/12) by multiplying
2944: nhstepm*hstepm matrices.
2945: Output is stored in matrix po[i][j][h] for h every 'hstepm' step
2946: (typically every 2 years instead of every month which is too big
1.217 brouard 2947: for the memory).
1.218 brouard 2948: Model is determined by parameters x and covariates have to be
2949: included manually here.
1.217 brouard 2950:
1.222 brouard 2951: */
1.217 brouard 2952:
2953: int i, j, d, h, k;
2954: double **out, cov[NCOVMAX+1];
2955: double **newm;
2956: double agexact;
2957: double agebegin, ageend;
1.222 brouard 2958: double **oldm, **savm;
1.217 brouard 2959:
1.222 brouard 2960: oldm=oldms;savm=savms;
1.217 brouard 2961: /* Hstepm could be zero and should return the unit matrix */
2962: for (i=1;i<=nlstate+ndeath;i++)
2963: for (j=1;j<=nlstate+ndeath;j++){
2964: oldm[i][j]=(i==j ? 1.0 : 0.0);
2965: po[i][j][0]=(i==j ? 1.0 : 0.0);
2966: }
2967: /* Even if hstepm = 1, at least one multiplication by the unit matrix */
2968: for(h=1; h <=nhstepm; h++){
2969: for(d=1; d <=hstepm; d++){
2970: newm=savm;
2971: /* Covariates have to be included here again */
2972: cov[1]=1.;
2973: agexact=age-((h-1)*hstepm + (d-1))*stepm/YEARM; /* age just before transition */
2974: /* agexact=age+((h-1)*hstepm + (d-1))*stepm/YEARM; /\* age just before transition *\/ */
2975: cov[2]=agexact;
2976: if(nagesqr==1)
1.222 brouard 2977: cov[3]= agexact*agexact;
1.218 brouard 2978: for (k=1; k<=cptcovn;k++)
1.222 brouard 2979: cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,k)];
2980: /* cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,Tvar[k])]; */
1.217 brouard 2981: for (k=1; k<=cptcovage;k++) /* Should start at cptcovn+1 */
1.222 brouard 2982: /* cov[2+Tage[k]]=cov[2+Tage[k]]*cov[2]; */
2983: cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,k)]*cov[2];
2984: /* cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,Tvar[Tage[k]])]*cov[2]; */
1.217 brouard 2985: for (k=1; k<=cptcovprod;k++) /* Useless because included in cptcovn */
1.222 brouard 2986: cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)]*nbcode[Tvard[k][2]][codtabm(ij,k)];
2987: /* 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 2988:
2989:
1.217 brouard 2990: /*printf("hxi cptcov=%d cptcode=%d\n",cptcov,cptcode);*/
2991: /*printf("h=%d d=%d age=%f cov=%f\n",h,d,age,cov[2]);*/
1.218 brouard 2992: /* Careful transposed matrix */
1.222 brouard 2993: /* age is in cov[2] */
1.218 brouard 2994: /* out=matprod2(newm, bmij(pmmij,cov,ncovmodel,x,nlstate,prevacurrent, dnewm, doldm, dsavm,ij),\ */
1.222 brouard 2995: /* 1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm); */
1.218 brouard 2996: out=matprod2(newm, bmij(pmmij,cov,ncovmodel,x,nlstate,prevacurrent,ij),\
1.222 brouard 2997: 1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm);
1.217 brouard 2998: /* if((int)age == 70){ */
2999: /* printf(" Backward hbxij age=%d agexact=%f d=%d nhstepm=%d hstepm=%d\n", (int) age, agexact, d, nhstepm, hstepm); */
3000: /* for(i=1; i<=nlstate+ndeath; i++) { */
3001: /* printf("%d pmmij ",i); */
3002: /* for(j=1;j<=nlstate+ndeath;j++) { */
3003: /* printf("%f ",pmmij[i][j]); */
3004: /* } */
3005: /* printf(" oldm "); */
3006: /* for(j=1;j<=nlstate+ndeath;j++) { */
3007: /* printf("%f ",oldm[i][j]); */
3008: /* } */
3009: /* printf("\n"); */
3010: /* } */
3011: /* } */
3012: savm=oldm;
3013: oldm=newm;
3014: }
3015: for(i=1; i<=nlstate+ndeath; i++)
3016: for(j=1;j<=nlstate+ndeath;j++) {
1.222 brouard 3017: po[i][j][h]=newm[i][j];
3018: /*if(h==nhstepm) printf("po[%d][%d][%d]=%f ",i,j,h,po[i][j][h]);*/
1.217 brouard 3019: }
3020: /*printf("h=%d ",h);*/
3021: } /* end h */
1.222 brouard 3022: /* printf("\n H=%d \n",h); */
1.217 brouard 3023: return po;
3024: }
3025:
3026:
1.162 brouard 3027: #ifdef NLOPT
3028: double myfunc(unsigned n, const double *p1, double *grad, void *pd){
3029: double fret;
3030: double *xt;
3031: int j;
3032: myfunc_data *d2 = (myfunc_data *) pd;
3033: /* xt = (p1-1); */
3034: xt=vector(1,n);
3035: for (j=1;j<=n;j++) xt[j]=p1[j-1]; /* xt[1]=p1[0] */
3036:
3037: fret=(d2->function)(xt); /* p xt[1]@8 is fine */
3038: /* fret=(*func)(xt); /\* p xt[1]@8 is fine *\/ */
3039: printf("Function = %.12lf ",fret);
3040: for (j=1;j<=n;j++) printf(" %d %.8lf", j, xt[j]);
3041: printf("\n");
3042: free_vector(xt,1,n);
3043: return fret;
3044: }
3045: #endif
1.126 brouard 3046:
3047: /*************** log-likelihood *************/
3048: double func( double *x)
3049: {
1.226 brouard 3050: int i, ii, j, k, mi, d, kk;
3051: int ioffset=0;
3052: double l, ll[NLSTATEMAX+1], cov[NCOVMAX+1];
3053: double **out;
3054: double lli; /* Individual log likelihood */
3055: int s1, s2;
1.228 brouard 3056: 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 3057: double bbh, survp;
3058: long ipmx;
3059: double agexact;
3060: /*extern weight */
3061: /* We are differentiating ll according to initial status */
3062: /* for (i=1;i<=npar;i++) printf("%f ", x[i]);*/
3063: /*for(i=1;i<imx;i++)
3064: printf(" %d\n",s[4][i]);
3065: */
1.162 brouard 3066:
1.226 brouard 3067: ++countcallfunc;
1.162 brouard 3068:
1.226 brouard 3069: cov[1]=1.;
1.126 brouard 3070:
1.226 brouard 3071: for(k=1; k<=nlstate; k++) ll[k]=0.;
1.224 brouard 3072: ioffset=0;
1.226 brouard 3073: if(mle==1){
3074: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
3075: /* Computes the values of the ncovmodel covariates of the model
3076: depending if the covariates are fixed or varying (age dependent) and stores them in cov[]
3077: Then computes with function pmij which return a matrix p[i][j] giving the elementary probability
3078: to be observed in j being in i according to the model.
3079: */
3080: ioffset=2+nagesqr+cptcovage;
1.233 brouard 3081: /* Fixed */
1.234 ! brouard 3082: for (k=1; k<=ncovf;k++){ /* Simple and product fixed covariates without age* products */
! 3083: 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)*/
! 3084: }
1.226 brouard 3085: /* In model V2+V1*V4+age*V3+V3*V2 Tvar[1] is V2, Tvar[2=V1*V4]
3086: is 6, Tvar[3=age*V3] should not be computed because of age Tvar[4=V3*V2]
3087: has been calculated etc */
3088: /* For an individual i, wav[i] gives the number of effective waves */
3089: /* We compute the contribution to Likelihood of each effective transition
3090: mw[mi][i] is real wave of the mi th effectve wave */
3091: /* Then statuses are computed at each begin and end of an effective wave s1=s[ mw[mi][i] ][i];
3092: s2=s[mw[mi+1][i]][i];
3093: And the iv th varying covariate is the cotvar[mw[mi+1][i]][iv][i]
3094: But if the variable is not in the model TTvar[iv] is the real variable effective in the model:
3095: meaning that decodemodel should be used cotvar[mw[mi+1][i]][TTvar[iv]][i]
3096: */
3097: for(mi=1; mi<= wav[i]-1; mi++){
1.234 ! brouard 3098: for(k=1; k <= ncovv ; k++){ /* Varying covariates (single and product but no age )*/
! 3099: cov[ioffset+TvarVind[k]]=cotvar[mw[mi][i]][Tvar[TvarVind[k]]][i];
! 3100: }
! 3101: for (ii=1;ii<=nlstate+ndeath;ii++)
! 3102: for (j=1;j<=nlstate+ndeath;j++){
! 3103: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
! 3104: savm[ii][j]=(ii==j ? 1.0 : 0.0);
! 3105: }
! 3106: for(d=0; d<dh[mi][i]; d++){
! 3107: newm=savm;
! 3108: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM;
! 3109: cov[2]=agexact;
! 3110: if(nagesqr==1)
! 3111: cov[3]= agexact*agexact; /* Should be changed here */
! 3112: for (kk=1; kk<=cptcovage;kk++) {
! 3113: cov[Tage[kk]+2+nagesqr]=covar[Tvar[Tage[kk]]][i]*agexact; /* Tage[kk] gives the data-covariate associated with age */
! 3114: }
! 3115: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
! 3116: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
! 3117: savm=oldm;
! 3118: oldm=newm;
! 3119: } /* end mult */
! 3120:
! 3121: /*lli=log(out[s[mw[mi][i]][i]][s[mw[mi+1][i]][i]]);*/ /* Original formula */
! 3122: /* But now since version 0.9 we anticipate for bias at large stepm.
! 3123: * If stepm is larger than one month (smallest stepm) and if the exact delay
! 3124: * (in months) between two waves is not a multiple of stepm, we rounded to
! 3125: * the nearest (and in case of equal distance, to the lowest) interval but now
! 3126: * we keep into memory the bias bh[mi][i] and also the previous matrix product
! 3127: * (i.e to dh[mi][i]-1) saved in 'savm'. Then we inter(extra)polate the
! 3128: * probability in order to take into account the bias as a fraction of the way
1.231 brouard 3129: * from savm to out if bh is negative or even beyond if bh is positive. bh varies
3130: * -stepm/2 to stepm/2 .
3131: * For stepm=1 the results are the same as for previous versions of Imach.
3132: * For stepm > 1 the results are less biased than in previous versions.
3133: */
1.234 ! brouard 3134: s1=s[mw[mi][i]][i];
! 3135: s2=s[mw[mi+1][i]][i];
! 3136: bbh=(double)bh[mi][i]/(double)stepm;
! 3137: /* bias bh is positive if real duration
! 3138: * is higher than the multiple of stepm and negative otherwise.
! 3139: */
! 3140: /* lli= (savm[s1][s2]>1.e-8 ?(1.+bbh)*log(out[s1][s2])- bbh*log(savm[s1][s2]):log((1.+bbh)*out[s1][s2]));*/
! 3141: if( s2 > nlstate){
! 3142: /* i.e. if s2 is a death state and if the date of death is known
! 3143: then the contribution to the likelihood is the probability to
! 3144: die between last step unit time and current step unit time,
! 3145: which is also equal to probability to die before dh
! 3146: minus probability to die before dh-stepm .
! 3147: In version up to 0.92 likelihood was computed
! 3148: as if date of death was unknown. Death was treated as any other
! 3149: health state: the date of the interview describes the actual state
! 3150: and not the date of a change in health state. The former idea was
! 3151: to consider that at each interview the state was recorded
! 3152: (healthy, disable or death) and IMaCh was corrected; but when we
! 3153: introduced the exact date of death then we should have modified
! 3154: the contribution of an exact death to the likelihood. This new
! 3155: contribution is smaller and very dependent of the step unit
! 3156: stepm. It is no more the probability to die between last interview
! 3157: and month of death but the probability to survive from last
! 3158: interview up to one month before death multiplied by the
! 3159: probability to die within a month. Thanks to Chris
! 3160: Jackson for correcting this bug. Former versions increased
! 3161: mortality artificially. The bad side is that we add another loop
! 3162: which slows down the processing. The difference can be up to 10%
! 3163: lower mortality.
! 3164: */
! 3165: /* If, at the beginning of the maximization mostly, the
! 3166: cumulative probability or probability to be dead is
! 3167: constant (ie = 1) over time d, the difference is equal to
! 3168: 0. out[s1][3] = savm[s1][3]: probability, being at state
! 3169: s1 at precedent wave, to be dead a month before current
! 3170: wave is equal to probability, being at state s1 at
! 3171: precedent wave, to be dead at mont of the current
! 3172: wave. Then the observed probability (that this person died)
! 3173: is null according to current estimated parameter. In fact,
! 3174: it should be very low but not zero otherwise the log go to
! 3175: infinity.
! 3176: */
1.183 brouard 3177: /* #ifdef INFINITYORIGINAL */
3178: /* lli=log(out[s1][s2] - savm[s1][s2]); */
3179: /* #else */
3180: /* if ((out[s1][s2] - savm[s1][s2]) < mytinydouble) */
3181: /* lli=log(mytinydouble); */
3182: /* else */
3183: /* lli=log(out[s1][s2] - savm[s1][s2]); */
3184: /* #endif */
1.226 brouard 3185: lli=log(out[s1][s2] - savm[s1][s2]);
1.216 brouard 3186:
1.226 brouard 3187: } else if ( s2==-1 ) { /* alive */
3188: for (j=1,survp=0. ; j<=nlstate; j++)
3189: survp += (1.+bbh)*out[s1][j]- bbh*savm[s1][j];
3190: /*survp += out[s1][j]; */
3191: lli= log(survp);
3192: }
3193: else if (s2==-4) {
3194: for (j=3,survp=0. ; j<=nlstate; j++)
3195: survp += (1.+bbh)*out[s1][j]- bbh*savm[s1][j];
3196: lli= log(survp);
3197: }
3198: else if (s2==-5) {
3199: for (j=1,survp=0. ; j<=2; j++)
3200: survp += (1.+bbh)*out[s1][j]- bbh*savm[s1][j];
3201: lli= log(survp);
3202: }
3203: else{
3204: lli= log((1.+bbh)*out[s1][s2]- bbh*savm[s1][s2]); /* linear interpolation */
3205: /* 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 */
3206: }
3207: /*lli=(1.+bbh)*log(out[s1][s2])- bbh*log(savm[s1][s2]);*/
3208: /*if(lli ==000.0)*/
3209: /*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); */
3210: ipmx +=1;
3211: sw += weight[i];
3212: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
3213: /* if (lli < log(mytinydouble)){ */
3214: /* 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); */
3215: /* 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]); */
3216: /* } */
3217: } /* end of wave */
3218: } /* end of individual */
3219: } else if(mle==2){
3220: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
3221: for (k=1; k<=cptcovn;k++) cov[2+nagesqr+k]=covar[Tvar[k]][i];
3222: for(mi=1; mi<= wav[i]-1; mi++){
3223: for (ii=1;ii<=nlstate+ndeath;ii++)
3224: for (j=1;j<=nlstate+ndeath;j++){
3225: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
3226: savm[ii][j]=(ii==j ? 1.0 : 0.0);
3227: }
3228: for(d=0; d<=dh[mi][i]; d++){
3229: newm=savm;
3230: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM;
3231: cov[2]=agexact;
3232: if(nagesqr==1)
3233: cov[3]= agexact*agexact;
3234: for (kk=1; kk<=cptcovage;kk++) {
3235: cov[Tage[kk]+2+nagesqr]=covar[Tvar[Tage[kk]]][i]*agexact;
3236: }
3237: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
3238: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
3239: savm=oldm;
3240: oldm=newm;
3241: } /* end mult */
3242:
3243: s1=s[mw[mi][i]][i];
3244: s2=s[mw[mi+1][i]][i];
3245: bbh=(double)bh[mi][i]/(double)stepm;
3246: 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 */
3247: ipmx +=1;
3248: sw += weight[i];
3249: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
3250: } /* end of wave */
3251: } /* end of individual */
3252: } else if(mle==3){ /* exponential inter-extrapolation */
3253: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
3254: for (k=1; k<=cptcovn;k++) cov[2+nagesqr+k]=covar[Tvar[k]][i];
3255: for(mi=1; mi<= wav[i]-1; mi++){
3256: for (ii=1;ii<=nlstate+ndeath;ii++)
3257: for (j=1;j<=nlstate+ndeath;j++){
3258: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
3259: savm[ii][j]=(ii==j ? 1.0 : 0.0);
3260: }
3261: for(d=0; d<dh[mi][i]; d++){
3262: newm=savm;
3263: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM;
3264: cov[2]=agexact;
3265: if(nagesqr==1)
3266: cov[3]= agexact*agexact;
3267: for (kk=1; kk<=cptcovage;kk++) {
3268: cov[Tage[kk]+2+nagesqr]=covar[Tvar[Tage[kk]]][i]*agexact;
3269: }
3270: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
3271: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
3272: savm=oldm;
3273: oldm=newm;
3274: } /* end mult */
3275:
3276: s1=s[mw[mi][i]][i];
3277: s2=s[mw[mi+1][i]][i];
3278: bbh=(double)bh[mi][i]/(double)stepm;
3279: 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 */
3280: ipmx +=1;
3281: sw += weight[i];
3282: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
3283: } /* end of wave */
3284: } /* end of individual */
3285: }else if (mle==4){ /* ml=4 no inter-extrapolation */
3286: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
3287: for (k=1; k<=cptcovn;k++) cov[2+nagesqr+k]=covar[Tvar[k]][i];
3288: for(mi=1; mi<= wav[i]-1; mi++){
3289: for (ii=1;ii<=nlstate+ndeath;ii++)
3290: for (j=1;j<=nlstate+ndeath;j++){
3291: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
3292: savm[ii][j]=(ii==j ? 1.0 : 0.0);
3293: }
3294: for(d=0; d<dh[mi][i]; d++){
3295: newm=savm;
3296: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM;
3297: cov[2]=agexact;
3298: if(nagesqr==1)
3299: cov[3]= agexact*agexact;
3300: for (kk=1; kk<=cptcovage;kk++) {
3301: cov[Tage[kk]+2+nagesqr]=covar[Tvar[Tage[kk]]][i]*agexact;
3302: }
1.126 brouard 3303:
1.226 brouard 3304: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
3305: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
3306: savm=oldm;
3307: oldm=newm;
3308: } /* end mult */
3309:
3310: s1=s[mw[mi][i]][i];
3311: s2=s[mw[mi+1][i]][i];
3312: if( s2 > nlstate){
3313: lli=log(out[s1][s2] - savm[s1][s2]);
3314: } else if ( s2==-1 ) { /* alive */
3315: for (j=1,survp=0. ; j<=nlstate; j++)
3316: survp += out[s1][j];
3317: lli= log(survp);
3318: }else{
3319: lli=log(out[s[mw[mi][i]][i]][s[mw[mi+1][i]][i]]); /* Original formula */
3320: }
3321: ipmx +=1;
3322: sw += weight[i];
3323: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
1.126 brouard 3324: /* 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 3325: } /* end of wave */
3326: } /* end of individual */
3327: }else{ /* ml=5 no inter-extrapolation no jackson =0.8a */
3328: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
3329: for (k=1; k<=cptcovn;k++) cov[2+nagesqr+k]=covar[Tvar[k]][i];
3330: for(mi=1; mi<= wav[i]-1; mi++){
3331: for (ii=1;ii<=nlstate+ndeath;ii++)
3332: for (j=1;j<=nlstate+ndeath;j++){
3333: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
3334: savm[ii][j]=(ii==j ? 1.0 : 0.0);
3335: }
3336: for(d=0; d<dh[mi][i]; d++){
3337: newm=savm;
3338: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM;
3339: cov[2]=agexact;
3340: if(nagesqr==1)
3341: cov[3]= agexact*agexact;
3342: for (kk=1; kk<=cptcovage;kk++) {
3343: cov[Tage[kk]+2+nagesqr]=covar[Tvar[Tage[kk]]][i]*agexact;
3344: }
1.126 brouard 3345:
1.226 brouard 3346: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
3347: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
3348: savm=oldm;
3349: oldm=newm;
3350: } /* end mult */
3351:
3352: s1=s[mw[mi][i]][i];
3353: s2=s[mw[mi+1][i]][i];
3354: lli=log(out[s[mw[mi][i]][i]][s[mw[mi+1][i]][i]]); /* Original formula */
3355: ipmx +=1;
3356: sw += weight[i];
3357: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
3358: /*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]);*/
3359: } /* end of wave */
3360: } /* end of individual */
3361: } /* End of if */
3362: for(k=1,l=0.; k<=nlstate; k++) l += ll[k];
3363: /* printf("l1=%f l2=%f ",ll[1],ll[2]); */
3364: l= l*ipmx/sw; /* To get the same order of magnitude as if weight=1 for every body */
3365: return -l;
1.126 brouard 3366: }
3367:
3368: /*************** log-likelihood *************/
3369: double funcone( double *x)
3370: {
1.228 brouard 3371: /* Same as func but slower because of a lot of printf and if */
1.126 brouard 3372: int i, ii, j, k, mi, d, kk;
1.228 brouard 3373: int ioffset=0;
1.131 brouard 3374: double l, ll[NLSTATEMAX+1], cov[NCOVMAX+1];
1.126 brouard 3375: double **out;
3376: double lli; /* Individual log likelihood */
3377: double llt;
3378: int s1, s2;
1.228 brouard 3379: int iv=0, iqv=0, itv=0, iqtv=0 ; /* Index of varying covariate, fixed quantitative cov, time varying covariate, quantitative time varying covariate */
3380:
1.126 brouard 3381: double bbh, survp;
1.187 brouard 3382: double agexact;
1.214 brouard 3383: double agebegin, ageend;
1.126 brouard 3384: /*extern weight */
3385: /* We are differentiating ll according to initial status */
3386: /* for (i=1;i<=npar;i++) printf("%f ", x[i]);*/
3387: /*for(i=1;i<imx;i++)
3388: printf(" %d\n",s[4][i]);
3389: */
3390: cov[1]=1.;
3391:
3392: for(k=1; k<=nlstate; k++) ll[k]=0.;
1.224 brouard 3393: ioffset=0;
3394: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
1.225 brouard 3395: ioffset=2+nagesqr+cptcovage;
1.232 brouard 3396: /* Fixed */
1.224 brouard 3397: /* for (k=1; k<=cptcovn;k++) cov[2+nagesqr+k]=covar[Tvar[k]][i]; */
1.232 brouard 3398: /* for (k=1; k<=ncoveff;k++){ /\* Simple and product fixed Dummy covariates without age* products *\/ */
3399: for (k=1; k<=ncovf;k++){ /* Simple and product fixed covariates without age* products */
3400: 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)*/
3401: /* cov[ioffset+TvarFind[1]]=covar[Tvar[TvarFind[1]]][i]; */
3402: /* cov[2+6]=covar[Tvar[6]][i]; */
3403: /* cov[2+6]=covar[2][i]; V2 */
3404: /* cov[TvarFind[2]]=covar[Tvar[TvarFind[2]]][i]; */
3405: /* cov[2+7]=covar[Tvar[7]][i]; */
3406: /* cov[2+7]=covar[7][i]; V7=V1*V2 */
3407: /* cov[TvarFind[3]]=covar[Tvar[TvarFind[3]]][i]; */
3408: /* cov[2+9]=covar[Tvar[9]][i]; */
3409: /* cov[2+9]=covar[1][i]; V1 */
1.225 brouard 3410: }
1.232 brouard 3411: /* for (k=1; k<=nqfveff;k++){ /\* Simple and product fixed Quantitative covariates without age* products *\/ */
3412: /* 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?)*\/ */
3413: /* } */
1.231 brouard 3414: /* for(iqv=1; iqv <= nqfveff; iqv++){ /\* Quantitative fixed covariates *\/ */
3415: /* cov[++ioffset]=coqvar[Tvar[iqv]][i]; /\* Only V2 k=6 and V1*V2 7 *\/ */
3416: /* } */
1.225 brouard 3417:
1.233 brouard 3418:
3419: for(mi=1; mi<= wav[i]-1; mi++){ /* Varying with waves */
1.232 brouard 3420: /* Wave varying (but not age varying) */
3421: for(k=1; k <= ncovv ; k++){ /* Varying covariates (single and product but no age )*/
1.233 brouard 3422: cov[ioffset+TvarVind[k]]=cotvar[mw[mi][i]][Tvar[TvarVind[k]]][i];
1.232 brouard 3423: }
3424: /* for(itv=1; itv <= ntveff; itv++){ /\* Varying dummy covariates (single??)*\/ */
1.231 brouard 3425: /* iv= Tvar[Tmodelind[ioffset-2-nagesqr-cptcovage+itv]]-ncovcol-nqv; /\* Counting the # varying covariate from 1 to ntveff *\/ */
3426: /* cov[ioffset+iv]=cotvar[mw[mi][i]][iv][i]; */
1.232 brouard 3427: /* k=ioffset-2-nagesqr-cptcovage+itv; /\* position in simple model *\/ */
3428: /* cov[ioffset+itv]=cotvar[mw[mi][i]][TmodelInvind[itv]][i]; */
1.231 brouard 3429: /* 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 3430: /* for(iqtv=1; iqtv <= nqtveff; iqtv++){ /\* Varying quantitatives covariates *\/ */
3431: /* iv=TmodelInvQind[iqtv]; /\* Counting the # varying covariate from 1 to ntveff *\/ */
3432: /* /\* 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]); *\/ */
3433: /* cov[ioffset+ntveff+iqtv]=cotqvar[mw[mi][i]][TmodelInvQind[iqtv]][i]; */
3434: /* } */
1.126 brouard 3435: for (ii=1;ii<=nlstate+ndeath;ii++)
1.231 brouard 3436: for (j=1;j<=nlstate+ndeath;j++){
3437: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
3438: savm[ii][j]=(ii==j ? 1.0 : 0.0);
3439: }
1.214 brouard 3440:
3441: agebegin=agev[mw[mi][i]][i]; /* Age at beginning of effective wave */
3442: ageend=agev[mw[mi][i]][i] + (dh[mi][i])*stepm/YEARM; /* Age at end of effective wave and at the end of transition */
3443: for(d=0; d<dh[mi][i]; d++){ /* Delay between two effective waves */
1.231 brouard 3444: /*dh[m][i] or dh[mw[mi][i]][i] is the delay between two effective waves m=mw[mi][i]
3445: and mw[mi+1][i]. dh depends on stepm.*/
3446: newm=savm;
3447: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM;
3448: cov[2]=agexact;
3449: if(nagesqr==1)
3450: cov[3]= agexact*agexact;
3451: for (kk=1; kk<=cptcovage;kk++) {
3452: cov[Tage[kk]+2+nagesqr]=covar[Tvar[Tage[kk]]][i]*agexact;
3453: }
3454: /* printf("i=%d,mi=%d,d=%d,mw[mi][i]=%d\n",i, mi,d,mw[mi][i]); */
3455: /* savm=pmij(pmmij,cov,ncovmodel,x,nlstate); */
3456: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
3457: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
3458: /* out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath, */
3459: /* 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate)); */
3460: savm=oldm;
3461: oldm=newm;
1.126 brouard 3462: } /* end mult */
3463:
3464: s1=s[mw[mi][i]][i];
3465: s2=s[mw[mi+1][i]][i];
1.217 brouard 3466: /* if(s2==-1){ */
3467: /* printf(" s1=%d, s2=%d i=%d \n", s1, s2, i); */
3468: /* /\* exit(1); *\/ */
3469: /* } */
1.126 brouard 3470: bbh=(double)bh[mi][i]/(double)stepm;
3471: /* bias is positive if real duration
3472: * is higher than the multiple of stepm and negative otherwise.
3473: */
3474: if( s2 > nlstate && (mle <5) ){ /* Jackson */
1.232 brouard 3475: lli=log(out[s1][s2] - savm[s1][s2]);
1.216 brouard 3476: } else if ( s2==-1 ) { /* alive */
1.232 brouard 3477: for (j=1,survp=0. ; j<=nlstate; j++)
3478: survp += (1.+bbh)*out[s1][j]- bbh*savm[s1][j];
3479: lli= log(survp);
1.126 brouard 3480: }else if (mle==1){
1.232 brouard 3481: lli= log((1.+bbh)*out[s1][s2]- bbh*savm[s1][s2]); /* linear interpolation */
1.126 brouard 3482: } else if(mle==2){
1.232 brouard 3483: 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 3484: } else if(mle==3){ /* exponential inter-extrapolation */
1.232 brouard 3485: 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 3486: } else if (mle==4){ /* mle=4 no inter-extrapolation */
1.232 brouard 3487: lli=log(out[s1][s2]); /* Original formula */
1.136 brouard 3488: } else{ /* mle=0 back to 1 */
1.232 brouard 3489: lli= log((1.+bbh)*out[s1][s2]- bbh*savm[s1][s2]); /* linear interpolation */
3490: /*lli=log(out[s1][s2]); */ /* Original formula */
1.126 brouard 3491: } /* End of if */
3492: ipmx +=1;
3493: sw += weight[i];
3494: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
1.132 brouard 3495: /*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 3496: if(globpr){
1.232 brouard 3497: fprintf(ficresilk,"%9ld %6.1f %6.1f %6d %2d %2d %2d %2d %3d %15.6f %8.4f %8.3f\
1.126 brouard 3498: %11.6f %11.6f %11.6f ", \
1.232 brouard 3499: num[i], agebegin, ageend, i,s1,s2,mi,mw[mi][i],dh[mi][i],exp(lli),weight[i],weight[i]*gipmx/gsw,
3500: 2*weight[i]*lli,out[s1][s2],savm[s1][s2]);
3501: for(k=1,llt=0.,l=0.; k<=nlstate; k++){
3502: llt +=ll[k]*gipmx/gsw;
3503: fprintf(ficresilk," %10.6f",-ll[k]*gipmx/gsw);
3504: }
3505: fprintf(ficresilk," %10.6f\n", -llt);
1.126 brouard 3506: }
1.232 brouard 3507: } /* end of wave */
3508: } /* end of individual */
3509: for(k=1,l=0.; k<=nlstate; k++) l += ll[k];
3510: /* printf("l1=%f l2=%f ",ll[1],ll[2]); */
3511: l= l*ipmx/sw; /* To get the same order of magnitude as if weight=1 for every body */
3512: if(globpr==0){ /* First time we count the contributions and weights */
3513: gipmx=ipmx;
3514: gsw=sw;
3515: }
3516: return -l;
1.126 brouard 3517: }
3518:
3519:
3520: /*************** function likelione ***********/
3521: void likelione(FILE *ficres,double p[], int npar, int nlstate, int *globpri, long *ipmx, double *sw, double *fretone, double (*funcone)(double []))
3522: {
3523: /* This routine should help understanding what is done with
3524: the selection of individuals/waves and
3525: to check the exact contribution to the likelihood.
3526: Plotting could be done.
3527: */
3528: int k;
3529:
3530: if(*globpri !=0){ /* Just counts and sums, no printings */
1.201 brouard 3531: strcpy(fileresilk,"ILK_");
1.202 brouard 3532: strcat(fileresilk,fileresu);
1.126 brouard 3533: if((ficresilk=fopen(fileresilk,"w"))==NULL) {
3534: printf("Problem with resultfile: %s\n", fileresilk);
3535: fprintf(ficlog,"Problem with resultfile: %s\n", fileresilk);
3536: }
1.214 brouard 3537: 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");
3538: fprintf(ficresilk, "#num_i ageb agend i s1 s2 mi mw dh likeli weight %%weight 2wlli out sav ");
1.126 brouard 3539: /* i,s1,s2,mi,mw[mi][i],dh[mi][i],exp(lli),weight[i],2*weight[i]*lli,out[s1][s2],savm[s1][s2]); */
3540: for(k=1; k<=nlstate; k++)
3541: fprintf(ficresilk," -2*gipw/gsw*weight*ll[%d]++",k);
3542: fprintf(ficresilk," -2*gipw/gsw*weight*ll(total)\n");
3543: }
3544:
3545: *fretone=(*funcone)(p);
3546: if(*globpri !=0){
3547: fclose(ficresilk);
1.205 brouard 3548: if (mle ==0)
3549: fprintf(fichtm,"\n<br>File of contributions to the likelihood computed with initial parameters and mle = %d.",mle);
3550: else if(mle >=1)
3551: fprintf(fichtm,"\n<br>File of contributions to the likelihood computed with optimized parameters mle = %d.",mle);
3552: 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 3553:
1.208 brouard 3554:
3555: for (k=1; k<= nlstate ; k++) {
1.211 brouard 3556: 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 3557: <img src=\"%s-p%dj.png\">",k,k,subdirf2(optionfilefiname,"ILK_"),k,subdirf2(optionfilefiname,"ILK_"),k,subdirf2(optionfilefiname,"ILK_"),k);
3558: }
1.207 brouard 3559: 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 3560: <img src=\"%s-ori.png\">",subdirf2(optionfilefiname,"ILK_"),subdirf2(optionfilefiname,"ILK_"),subdirf2(optionfilefiname,"ILK_"));
1.207 brouard 3561: fprintf(fichtm,"<br>- and by state of destination <a href=\"%s-dest.png\">%s-dest.png</a><br> \
1.204 brouard 3562: <img src=\"%s-dest.png\">",subdirf2(optionfilefiname,"ILK_"),subdirf2(optionfilefiname,"ILK_"),subdirf2(optionfilefiname,"ILK_"));
1.207 brouard 3563: fflush(fichtm);
1.205 brouard 3564: }
1.126 brouard 3565: return;
3566: }
3567:
3568:
3569: /*********** Maximum Likelihood Estimation ***************/
3570:
3571: void mlikeli(FILE *ficres,double p[], int npar, int ncovmodel, int nlstate, double ftol, double (*func)(double []))
3572: {
1.165 brouard 3573: int i,j, iter=0;
1.126 brouard 3574: double **xi;
3575: double fret;
3576: double fretone; /* Only one call to likelihood */
3577: /* char filerespow[FILENAMELENGTH];*/
1.162 brouard 3578:
3579: #ifdef NLOPT
3580: int creturn;
3581: nlopt_opt opt;
3582: /* double lb[9] = { -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL }; /\* lower bounds *\/ */
3583: double *lb;
3584: double minf; /* the minimum objective value, upon return */
3585: double * p1; /* Shifted parameters from 0 instead of 1 */
3586: myfunc_data dinst, *d = &dinst;
3587: #endif
3588:
3589:
1.126 brouard 3590: xi=matrix(1,npar,1,npar);
3591: for (i=1;i<=npar;i++)
3592: for (j=1;j<=npar;j++)
3593: xi[i][j]=(i==j ? 1.0 : 0.0);
3594: printf("Powell\n"); fprintf(ficlog,"Powell\n");
1.201 brouard 3595: strcpy(filerespow,"POW_");
1.126 brouard 3596: strcat(filerespow,fileres);
3597: if((ficrespow=fopen(filerespow,"w"))==NULL) {
3598: printf("Problem with resultfile: %s\n", filerespow);
3599: fprintf(ficlog,"Problem with resultfile: %s\n", filerespow);
3600: }
3601: fprintf(ficrespow,"# Powell\n# iter -2*LL");
3602: for (i=1;i<=nlstate;i++)
3603: for(j=1;j<=nlstate+ndeath;j++)
3604: if(j!=i)fprintf(ficrespow," p%1d%1d",i,j);
3605: fprintf(ficrespow,"\n");
1.162 brouard 3606: #ifdef POWELL
1.126 brouard 3607: powell(p,xi,npar,ftol,&iter,&fret,func);
1.162 brouard 3608: #endif
1.126 brouard 3609:
1.162 brouard 3610: #ifdef NLOPT
3611: #ifdef NEWUOA
3612: opt = nlopt_create(NLOPT_LN_NEWUOA,npar);
3613: #else
3614: opt = nlopt_create(NLOPT_LN_BOBYQA,npar);
3615: #endif
3616: lb=vector(0,npar-1);
3617: for (i=0;i<npar;i++) lb[i]= -HUGE_VAL;
3618: nlopt_set_lower_bounds(opt, lb);
3619: nlopt_set_initial_step1(opt, 0.1);
3620:
3621: p1= (p+1); /* p *(p+1)@8 and p *(p1)@8 are equal p1[0]=p[1] */
3622: d->function = func;
3623: printf(" Func %.12lf \n",myfunc(npar,p1,NULL,d));
3624: nlopt_set_min_objective(opt, myfunc, d);
3625: nlopt_set_xtol_rel(opt, ftol);
3626: if ((creturn=nlopt_optimize(opt, p1, &minf)) < 0) {
3627: printf("nlopt failed! %d\n",creturn);
3628: }
3629: else {
3630: printf("found minimum after %d evaluations (NLOPT=%d)\n", countcallfunc ,NLOPT);
3631: printf("found minimum at f(%g,%g) = %0.10g\n", p[0], p[1], minf);
3632: iter=1; /* not equal */
3633: }
3634: nlopt_destroy(opt);
3635: #endif
1.126 brouard 3636: free_matrix(xi,1,npar,1,npar);
3637: fclose(ficrespow);
1.203 brouard 3638: printf("\n#Number of iterations & function calls = %d & %d, -2 Log likelihood = %.12f\n",iter, countcallfunc,func(p));
3639: fprintf(ficlog,"\n#Number of iterations & function calls = %d & %d, -2 Log likelihood = %.12f\n",iter, countcallfunc,func(p));
1.180 brouard 3640: fprintf(ficres,"#Number of iterations & function calls = %d & %d, -2 Log likelihood = %.12f\n",iter, countcallfunc,func(p));
1.126 brouard 3641:
3642: }
3643:
3644: /**** Computes Hessian and covariance matrix ***/
1.203 brouard 3645: void hesscov(double **matcov, double **hess, double p[], int npar, double delti[], double ftolhess, double (*func)(double []))
1.126 brouard 3646: {
3647: double **a,**y,*x,pd;
1.203 brouard 3648: /* double **hess; */
1.164 brouard 3649: int i, j;
1.126 brouard 3650: int *indx;
3651:
3652: double hessii(double p[], double delta, int theta, double delti[],double (*func)(double []),int npar);
1.203 brouard 3653: double hessij(double p[], double **hess, double delti[], int i, int j,double (*func)(double []),int npar);
1.126 brouard 3654: void lubksb(double **a, int npar, int *indx, double b[]) ;
3655: void ludcmp(double **a, int npar, int *indx, double *d) ;
3656: double gompertz(double p[]);
1.203 brouard 3657: /* hess=matrix(1,npar,1,npar); */
1.126 brouard 3658:
3659: printf("\nCalculation of the hessian matrix. Wait...\n");
3660: fprintf(ficlog,"\nCalculation of the hessian matrix. Wait...\n");
3661: for (i=1;i<=npar;i++){
1.203 brouard 3662: printf("%d-",i);fflush(stdout);
3663: fprintf(ficlog,"%d-",i);fflush(ficlog);
1.126 brouard 3664:
3665: hess[i][i]=hessii(p,ftolhess,i,delti,func,npar);
3666:
3667: /* printf(" %f ",p[i]);
3668: printf(" %lf %lf %lf",hess[i][i],ftolhess,delti[i]);*/
3669: }
3670:
3671: for (i=1;i<=npar;i++) {
3672: for (j=1;j<=npar;j++) {
3673: if (j>i) {
1.203 brouard 3674: printf(".%d-%d",i,j);fflush(stdout);
3675: fprintf(ficlog,".%d-%d",i,j);fflush(ficlog);
3676: hess[i][j]=hessij(p,hess, delti,i,j,func,npar);
1.126 brouard 3677:
3678: hess[j][i]=hess[i][j];
3679: /*printf(" %lf ",hess[i][j]);*/
3680: }
3681: }
3682: }
3683: printf("\n");
3684: fprintf(ficlog,"\n");
3685:
3686: printf("\nInverting the hessian to get the covariance matrix. Wait...\n");
3687: fprintf(ficlog,"\nInverting the hessian to get the covariance matrix. Wait...\n");
3688:
3689: a=matrix(1,npar,1,npar);
3690: y=matrix(1,npar,1,npar);
3691: x=vector(1,npar);
3692: indx=ivector(1,npar);
3693: for (i=1;i<=npar;i++)
3694: for (j=1;j<=npar;j++) a[i][j]=hess[i][j];
3695: ludcmp(a,npar,indx,&pd);
3696:
3697: for (j=1;j<=npar;j++) {
3698: for (i=1;i<=npar;i++) x[i]=0;
3699: x[j]=1;
3700: lubksb(a,npar,indx,x);
3701: for (i=1;i<=npar;i++){
3702: matcov[i][j]=x[i];
3703: }
3704: }
3705:
3706: printf("\n#Hessian matrix#\n");
3707: fprintf(ficlog,"\n#Hessian matrix#\n");
3708: for (i=1;i<=npar;i++) {
3709: for (j=1;j<=npar;j++) {
1.203 brouard 3710: printf("%.6e ",hess[i][j]);
3711: fprintf(ficlog,"%.6e ",hess[i][j]);
1.126 brouard 3712: }
3713: printf("\n");
3714: fprintf(ficlog,"\n");
3715: }
3716:
1.203 brouard 3717: /* printf("\n#Covariance matrix#\n"); */
3718: /* fprintf(ficlog,"\n#Covariance matrix#\n"); */
3719: /* for (i=1;i<=npar;i++) { */
3720: /* for (j=1;j<=npar;j++) { */
3721: /* printf("%.6e ",matcov[i][j]); */
3722: /* fprintf(ficlog,"%.6e ",matcov[i][j]); */
3723: /* } */
3724: /* printf("\n"); */
3725: /* fprintf(ficlog,"\n"); */
3726: /* } */
3727:
1.126 brouard 3728: /* Recompute Inverse */
1.203 brouard 3729: /* for (i=1;i<=npar;i++) */
3730: /* for (j=1;j<=npar;j++) a[i][j]=matcov[i][j]; */
3731: /* ludcmp(a,npar,indx,&pd); */
3732:
3733: /* printf("\n#Hessian matrix recomputed#\n"); */
3734:
3735: /* for (j=1;j<=npar;j++) { */
3736: /* for (i=1;i<=npar;i++) x[i]=0; */
3737: /* x[j]=1; */
3738: /* lubksb(a,npar,indx,x); */
3739: /* for (i=1;i<=npar;i++){ */
3740: /* y[i][j]=x[i]; */
3741: /* printf("%.3e ",y[i][j]); */
3742: /* fprintf(ficlog,"%.3e ",y[i][j]); */
3743: /* } */
3744: /* printf("\n"); */
3745: /* fprintf(ficlog,"\n"); */
3746: /* } */
3747:
3748: /* Verifying the inverse matrix */
3749: #ifdef DEBUGHESS
3750: y=matprod2(y,hess,1,npar,1,npar,1,npar,matcov);
1.126 brouard 3751:
1.203 brouard 3752: printf("\n#Verification: multiplying the matrix of covariance by the Hessian matrix, should be unity:#\n");
3753: fprintf(ficlog,"\n#Verification: multiplying the matrix of covariance by the Hessian matrix. Should be unity:#\n");
1.126 brouard 3754:
3755: for (j=1;j<=npar;j++) {
3756: for (i=1;i<=npar;i++){
1.203 brouard 3757: printf("%.2f ",y[i][j]);
3758: fprintf(ficlog,"%.2f ",y[i][j]);
1.126 brouard 3759: }
3760: printf("\n");
3761: fprintf(ficlog,"\n");
3762: }
1.203 brouard 3763: #endif
1.126 brouard 3764:
3765: free_matrix(a,1,npar,1,npar);
3766: free_matrix(y,1,npar,1,npar);
3767: free_vector(x,1,npar);
3768: free_ivector(indx,1,npar);
1.203 brouard 3769: /* free_matrix(hess,1,npar,1,npar); */
1.126 brouard 3770:
3771:
3772: }
3773:
3774: /*************** hessian matrix ****************/
3775: double hessii(double x[], double delta, int theta, double delti[], double (*func)(double []), int npar)
1.203 brouard 3776: { /* Around values of x, computes the function func and returns the scales delti and hessian */
1.126 brouard 3777: int i;
3778: int l=1, lmax=20;
1.203 brouard 3779: double k1,k2, res, fx;
1.132 brouard 3780: double p2[MAXPARM+1]; /* identical to x */
1.126 brouard 3781: double delt=0.0001, delts, nkhi=10.,nkhif=1., khi=1.e-4;
3782: int k=0,kmax=10;
3783: double l1;
3784:
3785: fx=func(x);
3786: for (i=1;i<=npar;i++) p2[i]=x[i];
1.145 brouard 3787: for(l=0 ; l <=lmax; l++){ /* Enlarging the zone around the Maximum */
1.126 brouard 3788: l1=pow(10,l);
3789: delts=delt;
3790: for(k=1 ; k <kmax; k=k+1){
3791: delt = delta*(l1*k);
3792: p2[theta]=x[theta] +delt;
1.145 brouard 3793: k1=func(p2)-fx; /* Might be negative if too close to the theoretical maximum */
1.126 brouard 3794: p2[theta]=x[theta]-delt;
3795: k2=func(p2)-fx;
3796: /*res= (k1-2.0*fx+k2)/delt/delt; */
1.203 brouard 3797: res= (k1+k2)/delt/delt/2.; /* Divided by 2 because L and not 2*L */
1.126 brouard 3798:
1.203 brouard 3799: #ifdef DEBUGHESSII
1.126 brouard 3800: 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);
3801: 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);
3802: #endif
3803: /*if(fabs(k1-2.0*fx+k2) <1.e-13){ */
3804: if((k1 <khi/nkhi/2.) || (k2 <khi/nkhi/2.)){
3805: k=kmax;
3806: }
3807: else if((k1 >khi/nkhif) || (k2 >khi/nkhif)){ /* Keeps lastvalue before 3.84/2 KHI2 5% 1d.f. */
1.164 brouard 3808: k=kmax; l=lmax*10;
1.126 brouard 3809: }
3810: else if((k1 >khi/nkhi) || (k2 >khi/nkhi)){
3811: delts=delt;
3812: }
1.203 brouard 3813: } /* End loop k */
1.126 brouard 3814: }
3815: delti[theta]=delts;
3816: return res;
3817:
3818: }
3819:
1.203 brouard 3820: double hessij( double x[], double **hess, double delti[], int thetai,int thetaj,double (*func)(double []),int npar)
1.126 brouard 3821: {
3822: int i;
1.164 brouard 3823: int l=1, lmax=20;
1.126 brouard 3824: double k1,k2,k3,k4,res,fx;
1.132 brouard 3825: double p2[MAXPARM+1];
1.203 brouard 3826: int k, kmax=1;
3827: double v1, v2, cv12, lc1, lc2;
1.208 brouard 3828:
3829: int firstime=0;
1.203 brouard 3830:
1.126 brouard 3831: fx=func(x);
1.203 brouard 3832: for (k=1; k<=kmax; k=k+10) {
1.126 brouard 3833: for (i=1;i<=npar;i++) p2[i]=x[i];
1.203 brouard 3834: p2[thetai]=x[thetai]+delti[thetai]*k;
3835: p2[thetaj]=x[thetaj]+delti[thetaj]*k;
1.126 brouard 3836: k1=func(p2)-fx;
3837:
1.203 brouard 3838: p2[thetai]=x[thetai]+delti[thetai]*k;
3839: p2[thetaj]=x[thetaj]-delti[thetaj]*k;
1.126 brouard 3840: k2=func(p2)-fx;
3841:
1.203 brouard 3842: p2[thetai]=x[thetai]-delti[thetai]*k;
3843: p2[thetaj]=x[thetaj]+delti[thetaj]*k;
1.126 brouard 3844: k3=func(p2)-fx;
3845:
1.203 brouard 3846: p2[thetai]=x[thetai]-delti[thetai]*k;
3847: p2[thetaj]=x[thetaj]-delti[thetaj]*k;
1.126 brouard 3848: k4=func(p2)-fx;
1.203 brouard 3849: res=(k1-k2-k3+k4)/4.0/delti[thetai]/k/delti[thetaj]/k/2.; /* Because of L not 2*L */
3850: if(k1*k2*k3*k4 <0.){
1.208 brouard 3851: firstime=1;
1.203 brouard 3852: kmax=kmax+10;
1.208 brouard 3853: }
3854: if(kmax >=10 || firstime ==1){
1.218 brouard 3855: 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);
3856: 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 3857: 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);
3858: 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);
3859: }
3860: #ifdef DEBUGHESSIJ
3861: v1=hess[thetai][thetai];
3862: v2=hess[thetaj][thetaj];
3863: cv12=res;
3864: /* Computing eigen value of Hessian matrix */
3865: lc1=((v1+v2)+sqrt((v1+v2)*(v1+v2) - 4*(v1*v2-cv12*cv12)))/2.;
3866: lc2=((v1+v2)-sqrt((v1+v2)*(v1+v2) - 4*(v1*v2-cv12*cv12)))/2.;
3867: if ((lc2 <0) || (lc1 <0) ){
3868: printf("Warning: sub Hessian matrix '%d%d' does not have positive eigen values \n",thetai,thetaj);
3869: fprintf(ficlog, "Warning: sub Hessian matrix '%d%d' does not have positive eigen values \n",thetai,thetaj);
3870: 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);
3871: 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);
3872: }
1.126 brouard 3873: #endif
3874: }
3875: return res;
3876: }
3877:
1.203 brouard 3878: /* Not done yet: Was supposed to fix if not exactly at the maximum */
3879: /* double hessij( double x[], double delti[], int thetai,int thetaj,double (*func)(double []),int npar) */
3880: /* { */
3881: /* int i; */
3882: /* int l=1, lmax=20; */
3883: /* double k1,k2,k3,k4,res,fx; */
3884: /* double p2[MAXPARM+1]; */
3885: /* double delt=0.0001, delts, nkhi=10.,nkhif=1., khi=1.e-4; */
3886: /* int k=0,kmax=10; */
3887: /* double l1; */
3888:
3889: /* fx=func(x); */
3890: /* for(l=0 ; l <=lmax; l++){ /\* Enlarging the zone around the Maximum *\/ */
3891: /* l1=pow(10,l); */
3892: /* delts=delt; */
3893: /* for(k=1 ; k <kmax; k=k+1){ */
3894: /* delt = delti*(l1*k); */
3895: /* for (i=1;i<=npar;i++) p2[i]=x[i]; */
3896: /* p2[thetai]=x[thetai]+delti[thetai]/k; */
3897: /* p2[thetaj]=x[thetaj]+delti[thetaj]/k; */
3898: /* k1=func(p2)-fx; */
3899:
3900: /* p2[thetai]=x[thetai]+delti[thetai]/k; */
3901: /* p2[thetaj]=x[thetaj]-delti[thetaj]/k; */
3902: /* k2=func(p2)-fx; */
3903:
3904: /* p2[thetai]=x[thetai]-delti[thetai]/k; */
3905: /* p2[thetaj]=x[thetaj]+delti[thetaj]/k; */
3906: /* k3=func(p2)-fx; */
3907:
3908: /* p2[thetai]=x[thetai]-delti[thetai]/k; */
3909: /* p2[thetaj]=x[thetaj]-delti[thetaj]/k; */
3910: /* k4=func(p2)-fx; */
3911: /* res=(k1-k2-k3+k4)/4.0/delti[thetai]*k/delti[thetaj]*k/2.; /\* Because of L not 2*L *\/ */
3912: /* #ifdef DEBUGHESSIJ */
3913: /* 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); */
3914: /* 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); */
3915: /* #endif */
3916: /* if((k1 <khi/nkhi/2.) || (k2 <khi/nkhi/2.)|| (k4 <khi/nkhi/2.)|| (k4 <khi/nkhi/2.)){ */
3917: /* k=kmax; */
3918: /* } */
3919: /* else if((k1 >khi/nkhif) || (k2 >khi/nkhif) || (k4 >khi/nkhif) || (k4 >khi/nkhif)){ /\* Keeps lastvalue before 3.84/2 KHI2 5% 1d.f. *\/ */
3920: /* k=kmax; l=lmax*10; */
3921: /* } */
3922: /* else if((k1 >khi/nkhi) || (k2 >khi/nkhi)){ */
3923: /* delts=delt; */
3924: /* } */
3925: /* } /\* End loop k *\/ */
3926: /* } */
3927: /* delti[theta]=delts; */
3928: /* return res; */
3929: /* } */
3930:
3931:
1.126 brouard 3932: /************** Inverse of matrix **************/
3933: void ludcmp(double **a, int n, int *indx, double *d)
3934: {
3935: int i,imax,j,k;
3936: double big,dum,sum,temp;
3937: double *vv;
3938:
3939: vv=vector(1,n);
3940: *d=1.0;
3941: for (i=1;i<=n;i++) {
3942: big=0.0;
3943: for (j=1;j<=n;j++)
3944: if ((temp=fabs(a[i][j])) > big) big=temp;
3945: if (big == 0.0) nrerror("Singular matrix in routine ludcmp");
3946: vv[i]=1.0/big;
3947: }
3948: for (j=1;j<=n;j++) {
3949: for (i=1;i<j;i++) {
3950: sum=a[i][j];
3951: for (k=1;k<i;k++) sum -= a[i][k]*a[k][j];
3952: a[i][j]=sum;
3953: }
3954: big=0.0;
3955: for (i=j;i<=n;i++) {
3956: sum=a[i][j];
3957: for (k=1;k<j;k++)
3958: sum -= a[i][k]*a[k][j];
3959: a[i][j]=sum;
3960: if ( (dum=vv[i]*fabs(sum)) >= big) {
3961: big=dum;
3962: imax=i;
3963: }
3964: }
3965: if (j != imax) {
3966: for (k=1;k<=n;k++) {
3967: dum=a[imax][k];
3968: a[imax][k]=a[j][k];
3969: a[j][k]=dum;
3970: }
3971: *d = -(*d);
3972: vv[imax]=vv[j];
3973: }
3974: indx[j]=imax;
3975: if (a[j][j] == 0.0) a[j][j]=TINY;
3976: if (j != n) {
3977: dum=1.0/(a[j][j]);
3978: for (i=j+1;i<=n;i++) a[i][j] *= dum;
3979: }
3980: }
3981: free_vector(vv,1,n); /* Doesn't work */
3982: ;
3983: }
3984:
3985: void lubksb(double **a, int n, int *indx, double b[])
3986: {
3987: int i,ii=0,ip,j;
3988: double sum;
3989:
3990: for (i=1;i<=n;i++) {
3991: ip=indx[i];
3992: sum=b[ip];
3993: b[ip]=b[i];
3994: if (ii)
3995: for (j=ii;j<=i-1;j++) sum -= a[i][j]*b[j];
3996: else if (sum) ii=i;
3997: b[i]=sum;
3998: }
3999: for (i=n;i>=1;i--) {
4000: sum=b[i];
4001: for (j=i+1;j<=n;j++) sum -= a[i][j]*b[j];
4002: b[i]=sum/a[i][i];
4003: }
4004: }
4005:
4006: void pstamp(FILE *fichier)
4007: {
1.196 brouard 4008: fprintf(fichier,"# %s.%s\n#IMaCh version %s, %s\n#%s\n# %s", optionfilefiname,optionfilext,version,copyright, fullversion, strstart);
1.126 brouard 4009: }
4010:
4011: /************ Frequencies ********************/
1.226 brouard 4012: void freqsummary(char fileres[], int iagemin, int iagemax, int **s, double **agev, int nlstate, int imx, \
4013: int *Tvaraff, int *invalidvarcomb, int **nbcode, int *ncodemax,double **mint,double **anint, char strstart[], \
4014: int firstpass, int lastpass, int stepm, int weightopt, char model[])
4015: { /* Some frequencies */
4016:
1.227 brouard 4017: int i, m, jk, j1, bool, z1,j, k, iv;
1.226 brouard 4018: int iind=0, iage=0;
4019: int mi; /* Effective wave */
4020: int first;
4021: double ***freq; /* Frequencies */
4022: double *meanq;
4023: double **meanqt;
4024: double *pp, **prop, *posprop, *pospropt;
4025: double pos=0., posproptt=0., pospropta=0., k2, dateintsum=0,k2cpt=0;
4026: char fileresp[FILENAMELENGTH], fileresphtm[FILENAMELENGTH], fileresphtmfr[FILENAMELENGTH];
4027: double agebegin, ageend;
4028:
4029: pp=vector(1,nlstate);
4030: prop=matrix(1,nlstate,iagemin-AGEMARGE,iagemax+3+AGEMARGE);
4031: posprop=vector(1,nlstate); /* Counting the number of transition starting from a live state per age */
4032: pospropt=vector(1,nlstate); /* Counting the number of transition starting from a live state */
4033: /* prop=matrix(1,nlstate,iagemin,iagemax+3); */
4034: meanq=vector(1,nqfveff); /* Number of Quantitative Fixed Variables Effective */
4035: meanqt=matrix(1,lastpass,1,nqtveff);
4036: strcpy(fileresp,"P_");
4037: strcat(fileresp,fileresu);
4038: /*strcat(fileresphtm,fileresu);*/
4039: if((ficresp=fopen(fileresp,"w"))==NULL) {
4040: printf("Problem with prevalence resultfile: %s\n", fileresp);
4041: fprintf(ficlog,"Problem with prevalence resultfile: %s\n", fileresp);
4042: exit(0);
4043: }
1.214 brouard 4044:
1.226 brouard 4045: strcpy(fileresphtm,subdirfext(optionfilefiname,"PHTM_",".htm"));
4046: if((ficresphtm=fopen(fileresphtm,"w"))==NULL) {
4047: printf("Problem with prevalence HTM resultfile '%s' with errno='%s'\n",fileresphtm,strerror(errno));
4048: fprintf(ficlog,"Problem with prevalence HTM resultfile '%s' with errno='%s'\n",fileresphtm,strerror(errno));
4049: fflush(ficlog);
4050: exit(70);
4051: }
4052: else{
4053: fprintf(ficresphtm,"<html><head>\n<title>IMaCh PHTM_ %s</title></head>\n <body><font size=\"2\">%s <br> %s</font> \
1.214 brouard 4054: <hr size=\"2\" color=\"#EC5E5E\"> \n\
4055: Title=%s <br>Datafile=%s Firstpass=%d Lastpass=%d Stepm=%d Weight=%d Model=1+age+%s<br>\n",\
1.226 brouard 4056: fileresphtm,version,fullversion,title,datafile,firstpass,lastpass,stepm, weightopt, model);
4057: }
4058: fprintf(ficresphtm,"Current page is file <a href=\"%s\">%s</a><br>\n\n<h4>Frequencies and prevalence by age at begin of transition</h4>\n",fileresphtm, fileresphtm);
1.214 brouard 4059:
1.226 brouard 4060: strcpy(fileresphtmfr,subdirfext(optionfilefiname,"PHTMFR_",".htm"));
4061: if((ficresphtmfr=fopen(fileresphtmfr,"w"))==NULL) {
4062: printf("Problem with frequency table HTM resultfile '%s' with errno='%s'\n",fileresphtmfr,strerror(errno));
4063: fprintf(ficlog,"Problem with frequency table HTM resultfile '%s' with errno='%s'\n",fileresphtmfr,strerror(errno));
4064: fflush(ficlog);
4065: exit(70);
4066: }
4067: else{
4068: fprintf(ficresphtmfr,"<html><head>\n<title>IMaCh PHTM_Frequency table %s</title></head>\n <body><font size=\"2\">%s <br> %s</font> \
1.214 brouard 4069: <hr size=\"2\" color=\"#EC5E5E\"> \n\
4070: Title=%s <br>Datafile=%s Firstpass=%d Lastpass=%d Stepm=%d Weight=%d Model=1+age+%s<br>\n",\
1.226 brouard 4071: fileresphtmfr,version,fullversion,title,datafile,firstpass,lastpass,stepm, weightopt, model);
4072: }
4073: fprintf(ficresphtmfr,"Current page is file <a href=\"%s\">%s</a><br>\n\n<h4>Frequencies of all effective transitions by age at begin of transition </h4>Unknown status is -1<br/>\n",fileresphtmfr, fileresphtmfr);
1.214 brouard 4074:
1.226 brouard 4075: freq= ma3x(-5,nlstate+ndeath,-5,nlstate+ndeath,iagemin-AGEMARGE,iagemax+3+AGEMARGE);
4076: j1=0;
1.126 brouard 4077:
1.227 brouard 4078: /* j=ncoveff; /\* Only fixed dummy covariates *\/ */
4079: j=cptcoveff; /* Only dummy covariates of the model */
1.226 brouard 4080: if (cptcovn<1) {j=1;ncodemax[1]=1;}
1.220 brouard 4081:
1.226 brouard 4082: first=1;
1.220 brouard 4083:
1.226 brouard 4084: /* Detects if a combination j1 is empty: for a multinomial variable like 3 education levels:
4085: reference=low_education V1=0,V2=0
4086: med_educ V1=1 V2=0,
4087: high_educ V1=0 V2=1
4088: Then V1=1 and V2=1 is a noisy combination that we want to exclude for the list 2**cptcoveff
4089: */
1.126 brouard 4090:
1.227 brouard 4091: 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 4092: posproptt=0.;
4093: /*printf("cptcoveff=%d Tvaraff=%d", cptcoveff,Tvaraff[1]);
4094: scanf("%d", i);*/
4095: for (i=-5; i<=nlstate+ndeath; i++)
4096: for (jk=-5; jk<=nlstate+ndeath; jk++)
1.231 brouard 4097: for(m=iagemin; m <= iagemax+3; m++)
4098: freq[i][jk][m]=0;
4099:
1.226 brouard 4100: for (i=1; i<=nlstate; i++) {
4101: for(m=iagemin; m <= iagemax+3; m++)
1.231 brouard 4102: prop[i][m]=0;
1.226 brouard 4103: posprop[i]=0;
4104: pospropt[i]=0;
4105: }
1.227 brouard 4106: /* for (z1=1; z1<= nqfveff; z1++) { */
4107: /* meanq[z1]+=0.; */
4108: /* for(m=1;m<=lastpass;m++){ */
4109: /* meanqt[m][z1]=0.; */
4110: /* } */
4111: /* } */
1.231 brouard 4112:
1.226 brouard 4113: dateintsum=0;
4114: k2cpt=0;
1.227 brouard 4115: /* For that combination of covariate j1, we count and print the frequencies in one pass */
1.226 brouard 4116: for (iind=1; iind<=imx; iind++) { /* For each individual iind */
4117: bool=1;
1.227 brouard 4118: if(anyvaryingduminmodel==0){ /* If All fixed covariates */
1.234 ! brouard 4119: if (cptcoveff >0) { /* Filter is here: Must be looked at for model=V1+V2+V3+V4 */
1.227 brouard 4120: /* for (z1=1; z1<= nqfveff; z1++) { */
4121: /* meanq[z1]+=coqvar[Tvar[z1]][iind]; /\* Computes mean of quantitative with selected filter *\/ */
4122: /* } */
1.234 ! brouard 4123: for (z1=1; z1<=cptcoveff; z1++) {
! 4124: /* if(Tvaraff[z1] ==-20){ */
! 4125: /* /\* sumnew+=cotvar[mw[mi][iind]][z1][iind]; *\/ */
! 4126: /* }else if(Tvaraff[z1] ==-10){ */
! 4127: /* /\* sumnew+=coqvar[z1][iind]; *\/ */
! 4128: /* }else */
! 4129: if (covar[Tvaraff[z1]][iind]!= nbcode[Tvaraff[z1]][codtabm(j1,z1)]){
! 4130: /* Tests if this individual iind responded to j1 (V4=1 V3=0) */
! 4131: bool=0;
! 4132: /* 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",
! 4133: bool,i,z1, z1, Tvaraff[z1],i,covar[Tvaraff[z1]][i],j1,z1,codtabm(j1,z1),
! 4134: j1,z1,nbcode[Tvaraff[z1]][codtabm(j1,z1)],j1);*/
! 4135: /* For j1=7 in V1+V2+V3+V4 = 0 1 1 0 and codtabm(7,3)=1 and nbcde[3][?]=1*/
! 4136: } /* Onlyf fixed */
! 4137: } /* end z1 */
! 4138: } /* cptcovn > 0 */
1.227 brouard 4139: } /* end any */
4140: if (bool==1){ /* We selected an individual iind satisfying combination j1 or all fixed */
1.234 ! brouard 4141: /* for(m=firstpass; m<=lastpass; m++){ */
! 4142: for(mi=1; mi<wav[iind];mi++){ /* For that wave */
! 4143: m=mw[mi][iind];
! 4144: if(anyvaryingduminmodel==1){ /* Some are varying covariates */
! 4145: for (z1=1; z1<=cptcoveff; z1++) {
! 4146: if( Fixed[Tmodelind[z1]]==1){
! 4147: iv= Tvar[Tmodelind[z1]]-ncovcol-nqv;
! 4148: if (cotvar[m][iv][iind]!= nbcode[Tvaraff[z1]][codtabm(j1,z1)]) /* iv=1 to ntv, right modality */
! 4149: bool=0;
! 4150: }else if( Fixed[Tmodelind[z1]]== 0) { /* fixed */
! 4151: if (covar[Tvaraff[z1]][iind]!= nbcode[Tvaraff[z1]][codtabm(j1,z1)]) {
! 4152: bool=0;
! 4153: }
! 4154: }
! 4155: }
! 4156: }/* Some are varying covariates, we tried to speed up if all fixed covariates in the model, avoiding waves loop */
! 4157: /* bool =0 we keep that guy which corresponds to the combination of dummy values */
! 4158: if(bool==1){
! 4159: /* dh[m][iind] or dh[mw[mi][iind]][iind] is the delay between two effective (mi) waves m=mw[mi][iind]
! 4160: and mw[mi+1][iind]. dh depends on stepm. */
! 4161: agebegin=agev[m][iind]; /* Age at beginning of wave before transition*/
! 4162: ageend=agev[m][iind]+(dh[m][iind])*stepm/YEARM; /* Age at end of wave and transition */
! 4163: if(m >=firstpass && m <=lastpass){
! 4164: k2=anint[m][iind]+(mint[m][iind]/12.);
! 4165: /*if ((k2>=dateprev1) && (k2<=dateprev2)) {*/
! 4166: if(agev[m][iind]==0) agev[m][iind]=iagemax+1; /* All ages equal to 0 are in iagemax+1 */
! 4167: if(agev[m][iind]==1) agev[m][iind]=iagemax+2; /* All ages equal to 1 are in iagemax+2 */
! 4168: if (s[m][iind]>0 && s[m][iind]<=nlstate) /* If status at wave m is known and a live state */
! 4169: prop[s[m][iind]][(int)agev[m][iind]] += weight[iind]; /* At age of beginning of transition, where status is known */
! 4170: if (m<lastpass) {
! 4171: /* if(s[m][iind]==4 && s[m+1][iind]==4) */
! 4172: /* 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]); */
! 4173: if(s[m][iind]==-1)
! 4174: 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.));
! 4175: freq[s[m][iind]][s[m+1][iind]][(int)agev[m][iind]] += weight[iind]; /* At age of beginning of transition, where status is known */
! 4176: /* freq[s[m][iind]][s[m+1][iind]][(int)((agebegin+ageend)/2.)] += weight[iind]; */
! 4177: 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 */
! 4178: }
! 4179: } /* end if between passes */
! 4180: if ((agev[m][iind]>1) && (agev[m][iind]< (iagemax+3)) && (anint[m][iind]!=9999) && (mint[m][iind]!=99)) {
! 4181: dateintsum=dateintsum+k2;
! 4182: k2cpt++;
! 4183: /* printf("iind=%ld dateintmean = %lf dateintsum=%lf k2cpt=%lf k2=%lf\n",iind, dateintsum/k2cpt, dateintsum,k2cpt, k2); */
! 4184: }
! 4185: } /* end bool 2 */
! 4186: } /* end m */
1.226 brouard 4187: } /* end bool */
4188: } /* end iind = 1 to imx */
4189: /* prop[s][age] is feeded for any initial and valid live state as well as
4190: freq[s1][s2][age] at single age of beginning the transition, for a combination j1 */
1.231 brouard 4191:
4192:
1.226 brouard 4193: /* fprintf(ficresp, "#Count between %.lf/%.lf/%.lf and %.lf/%.lf/%.lf\n",jprev1, mprev1,anprev1,jprev2, mprev2,anprev2);*/
4194: pstamp(ficresp);
1.227 brouard 4195: /* if (ncoveff>0) { */
4196: if (cptcoveff>0) {
1.226 brouard 4197: fprintf(ficresp, "\n#********** Variable ");
4198: fprintf(ficresphtm, "\n<br/><br/><h3>********** Variable ");
4199: fprintf(ficresphtmfr, "\n<br/><br/><h3>********** Variable ");
1.227 brouard 4200: for (z1=1; z1<=cptcoveff; z1++){
1.234 ! brouard 4201: fprintf(ficresp, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
! 4202: fprintf(ficresphtm, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
! 4203: fprintf(ficresphtmfr, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
1.226 brouard 4204: }
4205: fprintf(ficresp, "**********\n#");
4206: fprintf(ficresphtm, "**********</h3>\n");
4207: fprintf(ficresphtmfr, "**********</h3>\n");
4208: fprintf(ficlog, "\n#********** Variable ");
1.227 brouard 4209: for (z1=1; z1<=cptcoveff; z1++) fprintf(ficlog, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
1.226 brouard 4210: fprintf(ficlog, "**********\n");
4211: }
4212: fprintf(ficresphtm,"<table style=\"text-align:center; border: 1px solid\">");
4213: for(i=1; i<=nlstate;i++) {
4214: fprintf(ficresp, " Age Prev(%d) N(%d) N",i,i);
4215: fprintf(ficresphtm, "<th>Age</th><th>Prev(%d)</th><th>N(%d)</th><th>N</th>",i,i);
4216: }
4217: fprintf(ficresp, "\n");
4218: fprintf(ficresphtm, "\n");
1.231 brouard 4219:
1.226 brouard 4220: /* Header of frequency table by age */
4221: fprintf(ficresphtmfr,"<table style=\"text-align:center; border: 1px solid\">");
4222: fprintf(ficresphtmfr,"<th>Age</th> ");
4223: for(jk=-1; jk <=nlstate+ndeath; jk++){
4224: for(m=-1; m <=nlstate+ndeath; m++){
1.234 ! brouard 4225: if(jk!=0 && m!=0)
! 4226: fprintf(ficresphtmfr,"<th>%d%d</th> ",jk,m);
1.226 brouard 4227: }
4228: }
4229: fprintf(ficresphtmfr, "\n");
1.231 brouard 4230:
1.226 brouard 4231: /* For each age */
4232: for(iage=iagemin; iage <= iagemax+3; iage++){
4233: fprintf(ficresphtm,"<tr>");
4234: if(iage==iagemax+1){
1.231 brouard 4235: fprintf(ficlog,"1");
4236: fprintf(ficresphtmfr,"<tr><th>0</th> ");
1.226 brouard 4237: }else if(iage==iagemax+2){
1.231 brouard 4238: fprintf(ficlog,"0");
4239: fprintf(ficresphtmfr,"<tr><th>Unknown</th> ");
1.226 brouard 4240: }else if(iage==iagemax+3){
1.231 brouard 4241: fprintf(ficlog,"Total");
4242: fprintf(ficresphtmfr,"<tr><th>Total</th> ");
1.226 brouard 4243: }else{
1.231 brouard 4244: if(first==1){
4245: first=0;
4246: printf("See log file for details...\n");
4247: }
4248: fprintf(ficresphtmfr,"<tr><th>%d</th> ",iage);
4249: fprintf(ficlog,"Age %d", iage);
1.226 brouard 4250: }
4251: for(jk=1; jk <=nlstate ; jk++){
1.231 brouard 4252: for(m=-1, pp[jk]=0; m <=nlstate+ndeath ; m++)
4253: pp[jk] += freq[jk][m][iage];
1.226 brouard 4254: }
4255: for(jk=1; jk <=nlstate ; jk++){
1.231 brouard 4256: for(m=-1, pos=0; m <=0 ; m++)
4257: pos += freq[jk][m][iage];
4258: if(pp[jk]>=1.e-10){
4259: if(first==1){
4260: printf(" %d.=%.0f loss[%d]=%.1f%%",jk,pp[jk],jk,100*pos/pp[jk]);
4261: }
4262: fprintf(ficlog," %d.=%.0f loss[%d]=%.1f%%",jk,pp[jk],jk,100*pos/pp[jk]);
4263: }else{
4264: if(first==1)
4265: printf(" %d.=%.0f loss[%d]=NaNQ%%",jk,pp[jk],jk);
4266: fprintf(ficlog," %d.=%.0f loss[%d]=NaNQ%%",jk,pp[jk],jk);
4267: }
1.226 brouard 4268: }
1.231 brouard 4269:
1.226 brouard 4270: for(jk=1; jk <=nlstate ; jk++){
1.231 brouard 4271: /* posprop[jk]=0; */
4272: for(m=0, pp[jk]=0; m <=nlstate+ndeath; m++)/* Summing on all ages */
4273: pp[jk] += freq[jk][m][iage];
1.226 brouard 4274: } /* pp[jk] is the total number of transitions starting from state jk and any ending status until this age */
1.231 brouard 4275:
1.226 brouard 4276: for(jk=1,pos=0, pospropta=0.; jk <=nlstate ; jk++){
1.231 brouard 4277: pos += pp[jk]; /* pos is the total number of transitions until this age */
4278: posprop[jk] += prop[jk][iage]; /* prop is the number of transitions from a live state
4279: from jk at age iage prop[s[m][iind]][(int)agev[m][iind]] += weight[iind] */
4280: pospropta += prop[jk][iage]; /* prop is the number of transitions from a live state
4281: from jk at age iage prop[s[m][iind]][(int)agev[m][iind]] += weight[iind] */
1.226 brouard 4282: }
4283: for(jk=1; jk <=nlstate ; jk++){
1.231 brouard 4284: if(pos>=1.e-5){
4285: if(first==1)
4286: printf(" %d.=%.0f prev[%d]=%.1f%%",jk,pp[jk],jk,100*pp[jk]/pos);
4287: fprintf(ficlog," %d.=%.0f prev[%d]=%.1f%%",jk,pp[jk],jk,100*pp[jk]/pos);
4288: }else{
4289: if(first==1)
4290: printf(" %d.=%.0f prev[%d]=NaNQ%%",jk,pp[jk],jk);
4291: fprintf(ficlog," %d.=%.0f prev[%d]=NaNQ%%",jk,pp[jk],jk);
4292: }
4293: if( iage <= iagemax){
4294: if(pos>=1.e-5){
4295: fprintf(ficresp," %d %.5f %.0f %.0f",iage,prop[jk][iage]/pospropta, prop[jk][iage],pospropta);
4296: fprintf(ficresphtm,"<th>%d</th><td>%.5f</td><td>%.0f</td><td>%.0f</td>",iage,prop[jk][iage]/pospropta, prop[jk][iage],pospropta);
4297: /*probs[iage][jk][j1]= pp[jk]/pos;*/
4298: /*printf("\niage=%d jk=%d j1=%d %.5f %.0f %.0f %f",iage,jk,j1,pp[jk]/pos, pp[jk],pos,probs[iage][jk][j1]);*/
4299: }
4300: else{
4301: fprintf(ficresp," %d NaNq %.0f %.0f",iage,prop[jk][iage],pospropta);
4302: fprintf(ficresphtm,"<th>%d</th><td>NaNq</td><td>%.0f</td><td>%.0f</td>",iage, prop[jk][iage],pospropta);
4303: }
4304: }
4305: pospropt[jk] +=posprop[jk];
1.226 brouard 4306: } /* end loop jk */
4307: /* pospropt=0.; */
4308: for(jk=-1; jk <=nlstate+ndeath; jk++){
1.231 brouard 4309: for(m=-1; m <=nlstate+ndeath; m++){
4310: if(freq[jk][m][iage] !=0 ) { /* minimizing output */
4311: if(first==1){
4312: printf(" %d%d=%.0f",jk,m,freq[jk][m][iage]);
4313: }
4314: fprintf(ficlog," %d%d=%.0f",jk,m,freq[jk][m][iage]);
4315: }
4316: if(jk!=0 && m!=0)
4317: fprintf(ficresphtmfr,"<td>%.0f</td> ",freq[jk][m][iage]);
4318: }
1.226 brouard 4319: } /* end loop jk */
4320: posproptt=0.;
4321: for(jk=1; jk <=nlstate; jk++){
1.231 brouard 4322: posproptt += pospropt[jk];
1.226 brouard 4323: }
4324: fprintf(ficresphtmfr,"</tr>\n ");
4325: if(iage <= iagemax){
1.231 brouard 4326: fprintf(ficresp,"\n");
4327: fprintf(ficresphtm,"</tr>\n");
1.226 brouard 4328: }
4329: if(first==1)
1.231 brouard 4330: printf("Others in log...\n");
1.226 brouard 4331: fprintf(ficlog,"\n");
4332: } /* end loop age iage */
4333: fprintf(ficresphtm,"<tr><th>Tot</th>");
4334: for(jk=1; jk <=nlstate ; jk++){
4335: if(posproptt < 1.e-5){
1.231 brouard 4336: fprintf(ficresphtm,"<td>Nanq</td><td>%.0f</td><td>%.0f</td>",pospropt[jk],posproptt);
1.226 brouard 4337: }else{
1.231 brouard 4338: fprintf(ficresphtm,"<td>%.5f</td><td>%.0f</td><td>%.0f</td>",pospropt[jk]/posproptt,pospropt[jk],posproptt);
1.226 brouard 4339: }
4340: }
4341: fprintf(ficresphtm,"</tr>\n");
4342: fprintf(ficresphtm,"</table>\n");
4343: fprintf(ficresphtmfr,"</table>\n");
4344: if(posproptt < 1.e-5){
4345: fprintf(ficresphtm,"\n <p><b> This combination (%d) is not valid and no result will be produced</b></p>",j1);
4346: fprintf(ficresphtmfr,"\n <p><b> This combination (%d) is not valid and no result will be produced</b></p>",j1);
4347: fprintf(ficres,"\n This combination (%d) is not valid and no result will be produced\n\n",j1);
4348: invalidvarcomb[j1]=1;
4349: }else{
4350: fprintf(ficresphtm,"\n <p> This combination (%d) is valid and result will be produced.</p>",j1);
4351: invalidvarcomb[j1]=0;
4352: }
4353: fprintf(ficresphtmfr,"</table>\n");
4354: } /* end selected combination of covariate j1 */
4355: dateintmean=dateintsum/k2cpt;
1.231 brouard 4356:
1.226 brouard 4357: fclose(ficresp);
4358: fclose(ficresphtm);
4359: fclose(ficresphtmfr);
4360: free_vector(meanq,1,nqfveff);
4361: free_matrix(meanqt,1,lastpass,1,nqtveff);
4362: free_ma3x(freq,-5,nlstate+ndeath,-5,nlstate+ndeath, iagemin-AGEMARGE, iagemax+3+AGEMARGE);
4363: free_vector(pospropt,1,nlstate);
4364: free_vector(posprop,1,nlstate);
4365: free_matrix(prop,1,nlstate,iagemin-AGEMARGE, iagemax+3+AGEMARGE);
4366: free_vector(pp,1,nlstate);
4367: /* End of freqsummary */
4368: }
1.126 brouard 4369:
4370: /************ Prevalence ********************/
1.227 brouard 4371: 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)
4372: {
4373: /* Compute observed prevalence between dateprev1 and dateprev2 by counting the number of people
4374: in each health status at the date of interview (if between dateprev1 and dateprev2).
4375: We still use firstpass and lastpass as another selection.
4376: */
1.126 brouard 4377:
1.227 brouard 4378: int i, m, jk, j1, bool, z1,j, iv;
4379: int mi; /* Effective wave */
4380: int iage;
4381: double agebegin, ageend;
4382:
4383: double **prop;
4384: double posprop;
4385: double y2; /* in fractional years */
4386: int iagemin, iagemax;
4387: int first; /** to stop verbosity which is redirected to log file */
4388:
4389: iagemin= (int) agemin;
4390: iagemax= (int) agemax;
4391: /*pp=vector(1,nlstate);*/
4392: prop=matrix(1,nlstate,iagemin-AGEMARGE,iagemax+3+AGEMARGE);
4393: /* freq=ma3x(-1,nlstate+ndeath,-1,nlstate+ndeath,iagemin,iagemax+3);*/
4394: j1=0;
1.222 brouard 4395:
1.227 brouard 4396: /*j=cptcoveff;*/
4397: if (cptcovn<1) {j=1;ncodemax[1]=1;}
1.222 brouard 4398:
1.227 brouard 4399: first=1;
4400: for(j1=1; j1<= (int) pow(2,cptcoveff);j1++){ /* For each combination of covariate */
4401: for (i=1; i<=nlstate; i++)
4402: for(iage=iagemin-AGEMARGE; iage <= iagemax+3+AGEMARGE; iage++)
4403: prop[i][iage]=0.0;
4404: printf("Prevalence combination of varying and fixed dummies %d\n",j1);
4405: /* fprintf(ficlog," V%d=%d ",Tvaraff[j1],nbcode[Tvaraff[j1]][codtabm(k,j1)]); */
4406: fprintf(ficlog,"Prevalence combination of varying and fixed dummies %d\n",j1);
4407:
4408: for (i=1; i<=imx; i++) { /* Each individual */
4409: bool=1;
4410: /* for(m=firstpass; m<=lastpass; m++){/\* Other selection (we can limit to certain interviews*\/ */
4411: for(mi=1; mi<wav[i];mi++){ /* For this wave too look where individual can be counted V4=0 V3=0 */
4412: m=mw[mi][i];
4413: /* Tmodelind[z1]=k is the position of the varying covariate in the model, but which # within 1 to ntv? */
4414: /* Tvar[Tmodelind[z1]] is the n of Vn; n-ncovcol-nqv is the first time varying covariate or iv */
4415: for (z1=1; z1<=cptcoveff; z1++){
4416: if( Fixed[Tmodelind[z1]]==1){
4417: iv= Tvar[Tmodelind[z1]]-ncovcol-nqv;
4418: if (cotvar[m][iv][i]!= nbcode[Tvaraff[z1]][codtabm(j1,z1)]) /* iv=1 to ntv, right modality */
4419: bool=0;
4420: }else if( Fixed[Tmodelind[z1]]== 0) /* fixed */
4421: if (covar[Tvaraff[z1]][i]!= nbcode[Tvaraff[z1]][codtabm(j1,z1)]) {
4422: bool=0;
4423: }
4424: }
4425: if(bool==1){ /* Otherwise we skip that wave/person */
4426: agebegin=agev[m][i]; /* Age at beginning of wave before transition*/
4427: /* ageend=agev[m][i]+(dh[m][i])*stepm/YEARM; /\* Age at end of wave and transition *\/ */
4428: if(m >=firstpass && m <=lastpass){
4429: y2=anint[m][i]+(mint[m][i]/12.); /* Fractional date in year */
4430: if ((y2>=dateprev1) && (y2<=dateprev2)) { /* Here is the main selection (fractional years) */
4431: if(agev[m][i]==0) agev[m][i]=iagemax+1;
4432: if(agev[m][i]==1) agev[m][i]=iagemax+2;
4433: if((int)agev[m][i] <iagemin-AGEMARGE || (int)agev[m][i] >iagemax+3+AGEMARGE){
4434: 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);
4435: exit(1);
4436: }
4437: if (s[m][i]>0 && s[m][i]<=nlstate) {
4438: /*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]]);*/
4439: prop[s[m][i]][(int)agev[m][i]] += weight[i];/* At age of beginning of transition, where status is known */
4440: prop[s[m][i]][iagemax+3] += weight[i];
4441: } /* end valid statuses */
4442: } /* end selection of dates */
4443: } /* end selection of waves */
4444: } /* end bool */
4445: } /* end wave */
4446: } /* end individual */
4447: for(i=iagemin; i <= iagemax+3; i++){
4448: for(jk=1,posprop=0; jk <=nlstate ; jk++) {
4449: posprop += prop[jk][i];
4450: }
4451:
4452: for(jk=1; jk <=nlstate ; jk++){
4453: if( i <= iagemax){
4454: if(posprop>=1.e-5){
4455: probs[i][jk][j1]= prop[jk][i]/posprop;
4456: } else{
4457: if(first==1){
4458: first=0;
4459: 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]);
4460: }
4461: }
4462: }
4463: }/* end jk */
4464: }/* end i */
1.222 brouard 4465: /*} *//* end i1 */
1.227 brouard 4466: } /* end j1 */
1.222 brouard 4467:
1.227 brouard 4468: /* free_ma3x(freq,-1,nlstate+ndeath,-1,nlstate+ndeath, iagemin, iagemax+3);*/
4469: /*free_vector(pp,1,nlstate);*/
4470: free_matrix(prop,1,nlstate, iagemin-AGEMARGE,iagemax+3+AGEMARGE);
4471: } /* End of prevalence */
1.126 brouard 4472:
4473: /************* Waves Concatenation ***************/
4474:
4475: 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)
4476: {
4477: /* Concatenates waves: wav[i] is the number of effective (useful waves) of individual i.
4478: Death is a valid wave (if date is known).
4479: mw[mi][i] is the mi (mi=1 to wav[i]) effective wave of individual i
4480: dh[m][i] or dh[mw[mi][i]][i] is the delay between two effective waves m=mw[mi][i]
4481: and mw[mi+1][i]. dh depends on stepm.
1.227 brouard 4482: */
1.126 brouard 4483:
1.224 brouard 4484: int i=0, mi=0, m=0, mli=0;
1.126 brouard 4485: /* int j, k=0,jk, ju, jl,jmin=1e+5, jmax=-1;
4486: double sum=0., jmean=0.;*/
1.224 brouard 4487: int first=0, firstwo=0, firsthree=0, firstfour=0, firstfiv=0;
1.126 brouard 4488: int j, k=0,jk, ju, jl;
4489: double sum=0.;
4490: first=0;
1.214 brouard 4491: firstwo=0;
1.217 brouard 4492: firsthree=0;
1.218 brouard 4493: firstfour=0;
1.164 brouard 4494: jmin=100000;
1.126 brouard 4495: jmax=-1;
4496: jmean=0.;
1.224 brouard 4497:
4498: /* Treating live states */
1.214 brouard 4499: for(i=1; i<=imx; i++){ /* For simple cases and if state is death */
1.224 brouard 4500: mi=0; /* First valid wave */
1.227 brouard 4501: mli=0; /* Last valid wave */
1.126 brouard 4502: m=firstpass;
1.214 brouard 4503: while(s[m][i] <= nlstate){ /* a live state */
1.227 brouard 4504: 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 */
4505: mli=m-1;/* mw[++mi][i]=m-1; */
4506: }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 */
4507: mw[++mi][i]=m;
4508: mli=m;
1.224 brouard 4509: } /* else might be a useless wave -1 and mi is not incremented and mw[mi] not updated */
4510: if(m < lastpass){ /* m < lastpass, standard case */
1.227 brouard 4511: m++; /* mi gives the "effective" current wave, m the current wave, go to next wave by incrementing m */
1.216 brouard 4512: }
1.227 brouard 4513: else{ /* m >= lastpass, eventual special issue with warning */
1.224 brouard 4514: #ifdef UNKNOWNSTATUSNOTCONTRIBUTING
1.227 brouard 4515: break;
1.224 brouard 4516: #else
1.227 brouard 4517: if(s[m][i]==-1 && (int) andc[i] == 9999 && (int)anint[m][i] != 9999){
4518: if(firsthree == 0){
4519: 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);
4520: firsthree=1;
4521: }
4522: 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);
4523: mw[++mi][i]=m;
4524: mli=m;
4525: }
4526: if(s[m][i]==-2){ /* Vital status is really unknown */
4527: nbwarn++;
4528: if((int)anint[m][i] == 9999){ /* Has the vital status really been verified? */
4529: 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);
4530: 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);
4531: }
4532: break;
4533: }
4534: break;
1.224 brouard 4535: #endif
1.227 brouard 4536: }/* End m >= lastpass */
1.126 brouard 4537: }/* end while */
1.224 brouard 4538:
1.227 brouard 4539: /* 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 4540: /* After last pass */
1.224 brouard 4541: /* Treating death states */
1.214 brouard 4542: if (s[m][i] > nlstate){ /* In a death state */
1.227 brouard 4543: /* if( mint[m][i]==mdc[m][i] && anint[m][i]==andc[m][i]){ /\* same date of death and date of interview *\/ */
4544: /* } */
1.126 brouard 4545: mi++; /* Death is another wave */
4546: /* if(mi==0) never been interviewed correctly before death */
1.227 brouard 4547: /* Only death is a correct wave */
1.126 brouard 4548: mw[mi][i]=m;
1.224 brouard 4549: }
4550: #ifndef DISPATCHINGKNOWNDEATHAFTERLASTWAVE
1.227 brouard 4551: 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 4552: /* m++; */
4553: /* mi++; */
4554: /* s[m][i]=nlstate+1; /\* We are setting the status to the last of non live state *\/ */
4555: /* mw[mi][i]=m; */
1.218 brouard 4556: if ((int)anint[m][i]!= 9999) { /* date of last interview is known */
1.227 brouard 4557: 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 */
4558: nbwarn++;
4559: if(firstfiv==0){
4560: 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 );
4561: firstfiv=1;
4562: }else{
4563: 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 );
4564: }
4565: }else{ /* Death occured afer last wave potential bias */
4566: nberr++;
4567: if(firstwo==0){
4568: 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 );
4569: firstwo=1;
4570: }
4571: 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 );
4572: }
1.218 brouard 4573: }else{ /* end date of interview is known */
1.227 brouard 4574: /* death is known but not confirmed by death status at any wave */
4575: if(firstfour==0){
4576: 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 );
4577: firstfour=1;
4578: }
4579: 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 4580: }
1.224 brouard 4581: } /* end if date of death is known */
4582: #endif
4583: wav[i]=mi; /* mi should be the last effective wave (or mli) */
4584: /* wav[i]=mw[mi][i]; */
1.126 brouard 4585: if(mi==0){
4586: nbwarn++;
4587: if(first==0){
1.227 brouard 4588: printf("Warning! No valid information for individual %ld line=%d (skipped) and may be others, see log file\n",num[i],i);
4589: first=1;
1.126 brouard 4590: }
4591: if(first==1){
1.227 brouard 4592: fprintf(ficlog,"Warning! No valid information for individual %ld line=%d (skipped)\n",num[i],i);
1.126 brouard 4593: }
4594: } /* end mi==0 */
4595: } /* End individuals */
1.214 brouard 4596: /* wav and mw are no more changed */
1.223 brouard 4597:
1.214 brouard 4598:
1.126 brouard 4599: for(i=1; i<=imx; i++){
4600: for(mi=1; mi<wav[i];mi++){
4601: if (stepm <=0)
1.227 brouard 4602: dh[mi][i]=1;
1.126 brouard 4603: else{
1.227 brouard 4604: if (s[mw[mi+1][i]][i] > nlstate) { /* A death */
4605: if (agedc[i] < 2*AGESUP) {
4606: j= rint(agedc[i]*12-agev[mw[mi][i]][i]*12);
4607: if(j==0) j=1; /* Survives at least one month after exam */
4608: else if(j<0){
4609: nberr++;
4610: 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]);
4611: j=1; /* Temporary Dangerous patch */
4612: 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);
4613: 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]);
4614: 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);
4615: }
4616: k=k+1;
4617: if (j >= jmax){
4618: jmax=j;
4619: ijmax=i;
4620: }
4621: if (j <= jmin){
4622: jmin=j;
4623: ijmin=i;
4624: }
4625: sum=sum+j;
4626: /*if (j<0) printf("j=%d num=%d \n",j,i);*/
4627: /* printf("%d %d %d %d\n", s[mw[mi][i]][i] ,s[mw[mi+1][i]][i],j,i);*/
4628: }
4629: }
4630: else{
4631: j= rint( (agev[mw[mi+1][i]][i]*12 - agev[mw[mi][i]][i]*12));
1.126 brouard 4632: /* 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 4633:
1.227 brouard 4634: k=k+1;
4635: if (j >= jmax) {
4636: jmax=j;
4637: ijmax=i;
4638: }
4639: else if (j <= jmin){
4640: jmin=j;
4641: ijmin=i;
4642: }
4643: /* if (j<10) printf("j=%d jmin=%d num=%d ",j,jmin,i); */
4644: /*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]);*/
4645: if(j<0){
4646: nberr++;
4647: 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]);
4648: 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]);
4649: }
4650: sum=sum+j;
4651: }
4652: jk= j/stepm;
4653: jl= j -jk*stepm;
4654: ju= j -(jk+1)*stepm;
4655: if(mle <=1){ /* only if we use a the linear-interpoloation pseudo-likelihood */
4656: if(jl==0){
4657: dh[mi][i]=jk;
4658: bh[mi][i]=0;
4659: }else{ /* We want a negative bias in order to only have interpolation ie
4660: * to avoid the price of an extra matrix product in likelihood */
4661: dh[mi][i]=jk+1;
4662: bh[mi][i]=ju;
4663: }
4664: }else{
4665: if(jl <= -ju){
4666: dh[mi][i]=jk;
4667: bh[mi][i]=jl; /* bias is positive if real duration
4668: * is higher than the multiple of stepm and negative otherwise.
4669: */
4670: }
4671: else{
4672: dh[mi][i]=jk+1;
4673: bh[mi][i]=ju;
4674: }
4675: if(dh[mi][i]==0){
4676: dh[mi][i]=1; /* At least one step */
4677: bh[mi][i]=ju; /* At least one step */
4678: /* 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);*/
4679: }
4680: } /* end if mle */
1.126 brouard 4681: }
4682: } /* end wave */
4683: }
4684: jmean=sum/k;
4685: 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 4686: 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 4687: }
1.126 brouard 4688:
4689: /*********** Tricode ****************************/
1.220 brouard 4690: void tricode(int *cptcov, int *Tvar, int **nbcode, int imx, int *Ndum)
1.126 brouard 4691: {
1.144 brouard 4692: /**< Uses cptcovn+2*cptcovprod as the number of covariates */
4693: /* Tvar[i]=atoi(stre); find 'n' in Vn and stores in Tvar. If model=V2+V1 Tvar[1]=2 and Tvar[2]=1
1.169 brouard 4694: * Boring subroutine which should only output nbcode[Tvar[j]][k]
1.224 brouard 4695: * Tvar[5] in V2+V1+V3*age+V2*V4 is 4 (V4) even it is a time varying or quantitative variable
4696: * nbcode[Tvar[5]][1]= nbcode[4][1]=0, nbcode[4][2]=1 (usually);
1.144 brouard 4697: */
1.130 brouard 4698:
1.145 brouard 4699: int ij=1, k=0, j=0, i=0, maxncov=NCOVMAX;
1.136 brouard 4700: int modmaxcovj=0; /* Modality max of covariates j */
1.145 brouard 4701: int cptcode=0; /* Modality max of covariates j */
4702: int modmincovj=0; /* Modality min of covariates j */
4703:
4704:
1.220 brouard 4705: /* cptcoveff=0; */
1.224 brouard 4706: /* *cptcov=0; */
1.126 brouard 4707:
1.144 brouard 4708: for (k=1; k <= maxncov; k++) ncodemax[k]=0; /* Horrible constant again replaced by NCOVMAX */
1.126 brouard 4709:
1.224 brouard 4710: /* Loop on covariates without age and products and no quantitative variable */
4711: /* for (j=1; j<=(cptcovs); j++) { /\* From model V1 + V2*age+ V3 + V3*V4 keeps V1 + V3 = 2 only *\/ */
1.227 brouard 4712: for (k=1; k<=cptcovt; k++) { /* From model V1 + V2*age + V3 + V3*V4 keeps V1 + V3 = 2 only */
4713: for (j=-1; (j < maxncov); j++) Ndum[j]=0;
4714: if(Dummy[k]==0 && Typevar[k] !=1){ /* Dummy covariate and not age product */
4715: switch(Fixed[k]) {
4716: case 0: /* Testing on fixed dummy covariate, simple or product of fixed */
1.231 brouard 4717: 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*/
4718: ij=(int)(covar[Tvar[k]][i]);
4719: /* ij=0 or 1 or -1. Value of the covariate Tvar[j] for individual i
4720: * If product of Vn*Vm, still boolean *:
4721: * If it was coded 1, 2, 3, 4 should be splitted into 3 boolean variables
4722: * 1 => 0 0 0, 2 => 0 0 1, 3 => 0 1 1, 4=1 0 0 */
4723: /* Finds for covariate j, n=Tvar[j] of Vn . ij is the
4724: modality of the nth covariate of individual i. */
4725: if (ij > modmaxcovj)
4726: modmaxcovj=ij;
4727: else if (ij < modmincovj)
4728: modmincovj=ij;
4729: if ((ij < -1) && (ij > NCOVMAX)){
4730: printf( "Error: minimal is less than -1 or maximal is bigger than %d. Exiting. \n", NCOVMAX );
4731: exit(1);
4732: }else
4733: Ndum[ij]++; /*counts and stores the occurence of this modality 0, 1, -1*/
4734: /* If coded 1, 2, 3 , counts the number of 1 Ndum[1], number of 2, Ndum[2], etc */
4735: /*printf("i=%d ij=%d Ndum[ij]=%d imx=%d",i,ij,Ndum[ij],imx);*/
4736: /* getting the maximum value of the modality of the covariate
4737: (should be 0 or 1 now) Tvar[j]. If V=sex and male is coded 0 and
4738: female ies 1, then modmaxcovj=1.
4739: */
4740: } /* end for loop on individuals i */
4741: printf(" Minimal and maximal values of %d th covariate V%d: min=%d max=%d \n", k, Tvar[k], modmincovj, modmaxcovj);
4742: fprintf(ficlog," Minimal and maximal values of %d th covariate V%d: min=%d max=%d \n", k, Tvar[k], modmincovj, modmaxcovj);
4743: cptcode=modmaxcovj;
4744: /* Ndum[0] = frequency of 0 for model-covariate j, Ndum[1] frequency of 1 etc. */
4745: /*for (i=0; i<=cptcode; i++) {*/
4746: for (j=modmincovj; j<=modmaxcovj; j++) { /* j=-1 ? 0 and 1*//* For each value j of the modality of model-cov k */
4747: printf("Frequencies of covariates %d ie V%d with value %d: %d\n", k, Tvar[k], j, Ndum[j]);
4748: fprintf(ficlog, "Frequencies of covariates %d ie V%d with value %d: %d\n", k, Tvar[k], j, Ndum[j]);
4749: if( Ndum[j] != 0 ){ /* Counts if nobody answered modality j ie empty modality, we skip it and reorder */
4750: if( j != -1){
4751: ncodemax[k]++; /* ncodemax[k]= Number of modalities of the k th
4752: covariate for which somebody answered excluding
4753: undefined. Usually 2: 0 and 1. */
4754: }
4755: ncodemaxwundef[k]++; /* ncodemax[j]= Number of modalities of the k th
4756: covariate for which somebody answered including
4757: undefined. Usually 3: -1, 0 and 1. */
4758: } /* In fact ncodemax[k]=2 (dichotom. variables only) but it could be more for
4759: * historical reasons: 3 if coded 1, 2, 3 and 4 and Ndum[2]=0 */
4760: } /* Ndum[-1] number of undefined modalities */
4761:
4762: /* j is a covariate, n=Tvar[j] of Vn; Fills nbcode */
4763: /* For covariate j, modalities could be 1, 2, 3, 4, 5, 6, 7. */
4764: /* If Ndum[1]=0, Ndum[2]=0, Ndum[3]= 635, Ndum[4]=0, Ndum[5]=0, Ndum[6]=27, Ndum[7]=125; */
4765: /* modmincovj=3; modmaxcovj = 7; */
4766: /* There are only 3 modalities non empty 3, 6, 7 (or 2 if 27 is too few) : ncodemax[j]=3; */
4767: /* which will be coded 0, 1, 2 which in binary on 2=3-1 digits are 0=00 1=01, 2=10; */
4768: /* defining two dummy variables: variables V1_1 and V1_2.*/
4769: /* nbcode[Tvar[j]][ij]=k; */
4770: /* nbcode[Tvar[j]][1]=0; */
4771: /* nbcode[Tvar[j]][2]=1; */
4772: /* nbcode[Tvar[j]][3]=2; */
4773: /* To be continued (not working yet). */
4774: ij=0; /* ij is similar to i but can jump over null modalities */
4775: 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*/
4776: if (Ndum[i] == 0) { /* If nobody responded to this modality k */
4777: break;
4778: }
4779: ij++;
4780: 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*/
4781: cptcode = ij; /* New max modality for covar j */
4782: } /* end of loop on modality i=-1 to 1 or more */
4783: break;
1.227 brouard 4784: case 1: /* Testing on varying covariate, could be simple and
4785: * should look at waves or product of fixed *
4786: * varying. No time to test -1, assuming 0 and 1 only */
1.231 brouard 4787: ij=0;
4788: for(i=0; i<=1;i++){
4789: nbcode[Tvar[k]][++ij]=i;
4790: }
4791: break;
1.227 brouard 4792: default:
1.231 brouard 4793: break;
1.227 brouard 4794: } /* end switch */
4795: } /* end dummy test */
1.225 brouard 4796:
1.192 brouard 4797: /* for (k=0; k<= cptcode; k++) { /\* k=-1 ? k=0 to 1 *\//\* Could be 1 to 4 *\//\* cptcode=modmaxcovj *\/ */
4798: /* /\*recode from 0 *\/ */
4799: /* k is a modality. If we have model=V1+V1*sex */
4800: /* then: nbcode[1][1]=0 ; nbcode[1][2]=1; nbcode[2][1]=0 ; nbcode[2][2]=1; */
4801: /* But if some modality were not used, it is recoded from 0 to a newer modmaxcovj=cptcode *\/ */
4802: /* } */
4803: /* /\* cptcode = ij; *\/ /\* New max modality for covar j *\/ */
4804: /* if (ij > ncodemax[j]) { */
4805: /* printf( " Error ij=%d > ncodemax[%d]=%d\n", ij, j, ncodemax[j]); */
4806: /* fprintf(ficlog, " Error ij=%d > ncodemax[%d]=%d\n", ij, j, ncodemax[j]); */
4807: /* break; */
4808: /* } */
4809: /* } /\* end of loop on modality k *\/ */
1.137 brouard 4810: } /* end of loop on model-covariate j. nbcode[Tvarj][1]=0 and nbcode[Tvarj][2]=1 sets the value of covariate j*/
4811:
1.225 brouard 4812: for (k=-1; k< maxncov; k++) Ndum[k]=0;
1.227 brouard 4813: /* Look at fixed dummy (single or product) covariates to check empty modalities */
1.187 brouard 4814: for (i=1; i<=ncovmodel-2-nagesqr; i++) { /* -2, cste and age and eventually age*age */
1.225 brouard 4815: /* Listing of all covariables in statement model to see if some covariates appear twice. For example, V1 appears twice in V1+V1*V2.*/
1.227 brouard 4816: 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 */
4817: 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 */
4818: /* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1, {2, 1, 1, 1, 2, 1, 1, 0, 0} */
1.225 brouard 4819: } /* V4+V3+V5, Ndum[1]@5={0, 0, 1, 1, 1} */
4820:
4821: ij=0;
1.227 brouard 4822: /* for (i=0; i<= maxncov-1; i++) { /\* modmaxcovj is unknown here. Only Ndum[2(V2),3(age*V3), 5(V3*V2) 6(V1*V4) *\/ */
4823: for (k=1; k<= cptcovt; k++) { /* modmaxcovj is unknown here. Only Ndum[2(V2),3(age*V3), 5(V3*V2) 6(V1*V4) */
1.225 brouard 4824: /*printf("Ndum[%d]=%d\n",i, Ndum[i]);*/
1.227 brouard 4825: /* if((Ndum[i]!=0) && (i<=ncovcol)){ /\* Tvar[i] <= ncovmodel ? *\/ */
4826: if(Ndum[Tvar[k]]!=0 && Dummy[k] == 0 && Typevar[k]==0){ /* Only Dummy and non empty in the model */
4827: /* If product not in single variable we don't print results */
1.225 brouard 4828: /*printf("diff Ndum[%d]=%d\n",i, Ndum[i]);*/
1.230 brouard 4829: ++ij;/* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1, */
4830: 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*/
4831: Tmodelind[ij]=k; /* Tmodelind: index in model of dummies Tmodelind[1]=2 V4: pos=2; V3: pos=3, V1=9 {2, 3, 9, ?, ?,} */
1.231 brouard 4832: TmodelInvind[ij]=Tvar[k]- ncovcol-nqv; /* Inverse TmodelInvind[2=V4]=2 second dummy varying cov (V4)4-1-1 {0, 2, 1, } TmodelInvind[3]=1 */
1.227 brouard 4833: if(Fixed[k]!=0)
4834: anyvaryingduminmodel=1;
1.231 brouard 4835: /* }else if((Ndum[i]!=0) && (i<=ncovcol+nqv)){ */
4836: /* Tvaraff[++ij]=-10; /\* Dont'n know how to treat quantitative variables yet *\/ */
4837: /* }else if((Ndum[i]!=0) && (i<=ncovcol+nqv+ntv)){ */
4838: /* Tvaraff[++ij]=i; /\*For printing (unclear) *\/ */
4839: /* }else if((Ndum[i]!=0) && (i<=ncovcol+nqv+ntv+nqtv)){ */
4840: /* Tvaraff[++ij]=-20; /\* Dont'n know how to treat quantitative variables yet *\/ */
1.227 brouard 4841: }
1.225 brouard 4842: } /* Tvaraff[1]@5 {3, 4, -20, 0, 0} Very strange */
4843: /* ij--; */
4844: /* cptcoveff=ij; /\*Number of total covariates*\/ */
4845: *cptcov=ij; /*Number of total real effective covariates: effective
1.231 brouard 4846: * because they can be excluded from the model and real
4847: * if in the model but excluded because missing values, but how to get k from ij?*/
1.227 brouard 4848: for(j=ij+1; j<= cptcovt; j++){
4849: Tvaraff[j]=0;
4850: Tmodelind[j]=0;
4851: }
1.228 brouard 4852: for(j=ntveff+1; j<= cptcovt; j++){
4853: TmodelInvind[j]=0;
4854: }
1.227 brouard 4855: /* To be sorted */
4856: ;
1.126 brouard 4857: }
4858:
1.145 brouard 4859:
1.126 brouard 4860: /*********** Health Expectancies ****************/
4861:
1.127 brouard 4862: void evsij(double ***eij, double x[], int nlstate, int stepm, int bage, int fage, double **oldm, double **savm, int cij, int estepm,char strstart[] )
1.126 brouard 4863:
4864: {
4865: /* Health expectancies, no variances */
1.164 brouard 4866: int i, j, nhstepm, hstepm, h, nstepm;
1.126 brouard 4867: int nhstepma, nstepma; /* Decreasing with age */
4868: double age, agelim, hf;
4869: double ***p3mat;
4870: double eip;
4871:
4872: pstamp(ficreseij);
4873: fprintf(ficreseij,"# (a) Life expectancies by health status at initial age and (b) health expectancies by health status at initial age\n");
4874: fprintf(ficreseij,"# Age");
4875: for(i=1; i<=nlstate;i++){
4876: for(j=1; j<=nlstate;j++){
4877: fprintf(ficreseij," e%1d%1d ",i,j);
4878: }
4879: fprintf(ficreseij," e%1d. ",i);
4880: }
4881: fprintf(ficreseij,"\n");
4882:
4883:
4884: if(estepm < stepm){
4885: printf ("Problem %d lower than %d\n",estepm, stepm);
4886: }
4887: else hstepm=estepm;
4888: /* We compute the life expectancy from trapezoids spaced every estepm months
4889: * This is mainly to measure the difference between two models: for example
4890: * if stepm=24 months pijx are given only every 2 years and by summing them
4891: * we are calculating an estimate of the Life Expectancy assuming a linear
4892: * progression in between and thus overestimating or underestimating according
4893: * to the curvature of the survival function. If, for the same date, we
4894: * estimate the model with stepm=1 month, we can keep estepm to 24 months
4895: * to compare the new estimate of Life expectancy with the same linear
4896: * hypothesis. A more precise result, taking into account a more precise
4897: * curvature will be obtained if estepm is as small as stepm. */
4898:
4899: /* For example we decided to compute the life expectancy with the smallest unit */
4900: /* hstepm beeing the number of stepms, if hstepm=1 the length of hstepm is stepm.
4901: nhstepm is the number of hstepm from age to agelim
4902: nstepm is the number of stepm from age to agelin.
4903: Look at hpijx to understand the reason of that which relies in memory size
4904: and note for a fixed period like estepm months */
4905: /* We decided (b) to get a life expectancy respecting the most precise curvature of the
4906: survival function given by stepm (the optimization length). Unfortunately it
4907: means that if the survival funtion is printed only each two years of age and if
4908: you sum them up and add 1 year (area under the trapezoids) you won't get the same
4909: results. So we changed our mind and took the option of the best precision.
4910: */
4911: hstepm=hstepm/stepm; /* Typically in stepm units, if stepm=6 & estepm=24 , = 24/6 months = 4 */
4912:
4913: agelim=AGESUP;
4914: /* If stepm=6 months */
4915: /* Computed by stepm unit matrices, product of hstepm matrices, stored
4916: in an array of nhstepm length: nhstepm=10, hstepm=4, stepm=6 months */
4917:
4918: /* nhstepm age range expressed in number of stepm */
4919: nstepm=(int) rint((agelim-bage)*YEARM/stepm); /* Biggest nstepm */
4920: /* Typically if 20 years nstepm = 20*12/6=40 stepm */
4921: /* if (stepm >= YEARM) hstepm=1;*/
4922: nhstepm = nstepm/hstepm;/* Expressed in hstepm, typically nhstepm=40/4=10 */
4923: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
4924:
4925: for (age=bage; age<=fage; age ++){
4926: nstepma=(int) rint((agelim-bage)*YEARM/stepm); /* Biggest nstepm */
4927: /* Typically if 20 years nstepm = 20*12/6=40 stepm */
4928: /* if (stepm >= YEARM) hstepm=1;*/
4929: nhstepma = nstepma/hstepm;/* Expressed in hstepm, typically nhstepma=40/4=10 */
4930:
4931: /* If stepm=6 months */
4932: /* Computed by stepm unit matrices, product of hstepma matrices, stored
4933: in an array of nhstepma length: nhstepma=10, hstepm=4, stepm=6 months */
4934:
4935: hpxij(p3mat,nhstepma,age,hstepm,x,nlstate,stepm,oldm, savm, cij);
4936:
4937: hf=hstepm*stepm/YEARM; /* Duration of hstepm expressed in year unit. */
4938:
4939: printf("%d|",(int)age);fflush(stdout);
4940: fprintf(ficlog,"%d|",(int)age);fflush(ficlog);
4941:
4942: /* Computing expectancies */
4943: for(i=1; i<=nlstate;i++)
4944: for(j=1; j<=nlstate;j++)
4945: for (h=0, eij[i][j][(int)age]=0; h<=nhstepm-1; h++){
4946: eij[i][j][(int)age] += (p3mat[i][j][h]+p3mat[i][j][h+1])/2.0*hf;
4947:
4948: /* 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]);*/
4949:
4950: }
4951:
4952: fprintf(ficreseij,"%3.0f",age );
4953: for(i=1; i<=nlstate;i++){
4954: eip=0;
4955: for(j=1; j<=nlstate;j++){
4956: eip +=eij[i][j][(int)age];
4957: fprintf(ficreseij,"%9.4f", eij[i][j][(int)age] );
4958: }
4959: fprintf(ficreseij,"%9.4f", eip );
4960: }
4961: fprintf(ficreseij,"\n");
4962:
4963: }
4964: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
4965: printf("\n");
4966: fprintf(ficlog,"\n");
4967:
4968: }
4969:
1.127 brouard 4970: void cvevsij(double ***eij, double x[], int nlstate, int stepm, int bage, int fage, double **oldm, double **savm, int cij, int estepm,double delti[],double **matcov,char strstart[] )
1.126 brouard 4971:
4972: {
4973: /* Covariances of health expectancies eij and of total life expectancies according
1.222 brouard 4974: to initial status i, ei. .
1.126 brouard 4975: */
4976: int i, j, nhstepm, hstepm, h, nstepm, k, cptj, cptj2, i2, j2, ij, ji;
4977: int nhstepma, nstepma; /* Decreasing with age */
4978: double age, agelim, hf;
4979: double ***p3matp, ***p3matm, ***varhe;
4980: double **dnewm,**doldm;
4981: double *xp, *xm;
4982: double **gp, **gm;
4983: double ***gradg, ***trgradg;
4984: int theta;
4985:
4986: double eip, vip;
4987:
4988: varhe=ma3x(1,nlstate*nlstate,1,nlstate*nlstate,(int) bage, (int) fage);
4989: xp=vector(1,npar);
4990: xm=vector(1,npar);
4991: dnewm=matrix(1,nlstate*nlstate,1,npar);
4992: doldm=matrix(1,nlstate*nlstate,1,nlstate*nlstate);
4993:
4994: pstamp(ficresstdeij);
4995: fprintf(ficresstdeij,"# Health expectancies with standard errors\n");
4996: fprintf(ficresstdeij,"# Age");
4997: for(i=1; i<=nlstate;i++){
4998: for(j=1; j<=nlstate;j++)
4999: fprintf(ficresstdeij," e%1d%1d (SE)",i,j);
5000: fprintf(ficresstdeij," e%1d. ",i);
5001: }
5002: fprintf(ficresstdeij,"\n");
5003:
5004: pstamp(ficrescveij);
5005: fprintf(ficrescveij,"# Subdiagonal matrix of covariances of health expectancies by age: cov(eij,ekl)\n");
5006: fprintf(ficrescveij,"# Age");
5007: for(i=1; i<=nlstate;i++)
5008: for(j=1; j<=nlstate;j++){
5009: cptj= (j-1)*nlstate+i;
5010: for(i2=1; i2<=nlstate;i2++)
5011: for(j2=1; j2<=nlstate;j2++){
5012: cptj2= (j2-1)*nlstate+i2;
5013: if(cptj2 <= cptj)
5014: fprintf(ficrescveij," %1d%1d,%1d%1d",i,j,i2,j2);
5015: }
5016: }
5017: fprintf(ficrescveij,"\n");
5018:
5019: if(estepm < stepm){
5020: printf ("Problem %d lower than %d\n",estepm, stepm);
5021: }
5022: else hstepm=estepm;
5023: /* We compute the life expectancy from trapezoids spaced every estepm months
5024: * This is mainly to measure the difference between two models: for example
5025: * if stepm=24 months pijx are given only every 2 years and by summing them
5026: * we are calculating an estimate of the Life Expectancy assuming a linear
5027: * progression in between and thus overestimating or underestimating according
5028: * to the curvature of the survival function. If, for the same date, we
5029: * estimate the model with stepm=1 month, we can keep estepm to 24 months
5030: * to compare the new estimate of Life expectancy with the same linear
5031: * hypothesis. A more precise result, taking into account a more precise
5032: * curvature will be obtained if estepm is as small as stepm. */
5033:
5034: /* For example we decided to compute the life expectancy with the smallest unit */
5035: /* hstepm beeing the number of stepms, if hstepm=1 the length of hstepm is stepm.
5036: nhstepm is the number of hstepm from age to agelim
5037: nstepm is the number of stepm from age to agelin.
5038: Look at hpijx to understand the reason of that which relies in memory size
5039: and note for a fixed period like estepm months */
5040: /* We decided (b) to get a life expectancy respecting the most precise curvature of the
5041: survival function given by stepm (the optimization length). Unfortunately it
5042: means that if the survival funtion is printed only each two years of age and if
5043: you sum them up and add 1 year (area under the trapezoids) you won't get the same
5044: results. So we changed our mind and took the option of the best precision.
5045: */
5046: hstepm=hstepm/stepm; /* Typically in stepm units, if stepm=6 & estepm=24 , = 24/6 months = 4 */
5047:
5048: /* If stepm=6 months */
5049: /* nhstepm age range expressed in number of stepm */
5050: agelim=AGESUP;
5051: nstepm=(int) rint((agelim-bage)*YEARM/stepm);
5052: /* Typically if 20 years nstepm = 20*12/6=40 stepm */
5053: /* if (stepm >= YEARM) hstepm=1;*/
5054: nhstepm = nstepm/hstepm;/* Expressed in hstepm, typically nhstepm=40/4=10 */
5055:
5056: p3matp=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
5057: p3matm=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
5058: gradg=ma3x(0,nhstepm,1,npar,1,nlstate*nlstate);
5059: trgradg =ma3x(0,nhstepm,1,nlstate*nlstate,1,npar);
5060: gp=matrix(0,nhstepm,1,nlstate*nlstate);
5061: gm=matrix(0,nhstepm,1,nlstate*nlstate);
5062:
5063: for (age=bage; age<=fage; age ++){
5064: nstepma=(int) rint((agelim-bage)*YEARM/stepm); /* Biggest nstepm */
5065: /* Typically if 20 years nstepm = 20*12/6=40 stepm */
5066: /* if (stepm >= YEARM) hstepm=1;*/
5067: nhstepma = nstepma/hstepm;/* Expressed in hstepm, typically nhstepma=40/4=10 */
1.218 brouard 5068:
1.126 brouard 5069: /* If stepm=6 months */
5070: /* Computed by stepm unit matrices, product of hstepma matrices, stored
5071: in an array of nhstepma length: nhstepma=10, hstepm=4, stepm=6 months */
5072:
5073: hf=hstepm*stepm/YEARM; /* Duration of hstepm expressed in year unit. */
1.218 brouard 5074:
1.126 brouard 5075: /* Computing Variances of health expectancies */
5076: /* Gradient is computed with plus gp and minus gm. Code is duplicated in order to
5077: decrease memory allocation */
5078: for(theta=1; theta <=npar; theta++){
5079: for(i=1; i<=npar; i++){
1.222 brouard 5080: xp[i] = x[i] + (i==theta ?delti[theta]:0);
5081: xm[i] = x[i] - (i==theta ?delti[theta]:0);
1.126 brouard 5082: }
5083: hpxij(p3matp,nhstepm,age,hstepm,xp,nlstate,stepm,oldm,savm, cij);
5084: hpxij(p3matm,nhstepm,age,hstepm,xm,nlstate,stepm,oldm,savm, cij);
1.218 brouard 5085:
1.126 brouard 5086: for(j=1; j<= nlstate; j++){
1.222 brouard 5087: for(i=1; i<=nlstate; i++){
5088: for(h=0; h<=nhstepm-1; h++){
5089: gp[h][(j-1)*nlstate + i] = (p3matp[i][j][h]+p3matp[i][j][h+1])/2.;
5090: gm[h][(j-1)*nlstate + i] = (p3matm[i][j][h]+p3matm[i][j][h+1])/2.;
5091: }
5092: }
1.126 brouard 5093: }
1.218 brouard 5094:
1.126 brouard 5095: for(ij=1; ij<= nlstate*nlstate; ij++)
1.222 brouard 5096: for(h=0; h<=nhstepm-1; h++){
5097: gradg[h][theta][ij]= (gp[h][ij]-gm[h][ij])/2./delti[theta];
5098: }
1.126 brouard 5099: }/* End theta */
5100:
5101:
5102: for(h=0; h<=nhstepm-1; h++)
5103: for(j=1; j<=nlstate*nlstate;j++)
1.222 brouard 5104: for(theta=1; theta <=npar; theta++)
5105: trgradg[h][j][theta]=gradg[h][theta][j];
1.126 brouard 5106:
1.218 brouard 5107:
1.222 brouard 5108: for(ij=1;ij<=nlstate*nlstate;ij++)
1.126 brouard 5109: for(ji=1;ji<=nlstate*nlstate;ji++)
1.222 brouard 5110: varhe[ij][ji][(int)age] =0.;
1.218 brouard 5111:
1.222 brouard 5112: printf("%d|",(int)age);fflush(stdout);
5113: fprintf(ficlog,"%d|",(int)age);fflush(ficlog);
5114: for(h=0;h<=nhstepm-1;h++){
1.126 brouard 5115: for(k=0;k<=nhstepm-1;k++){
1.222 brouard 5116: matprod2(dnewm,trgradg[h],1,nlstate*nlstate,1,npar,1,npar,matcov);
5117: matprod2(doldm,dnewm,1,nlstate*nlstate,1,npar,1,nlstate*nlstate,gradg[k]);
5118: for(ij=1;ij<=nlstate*nlstate;ij++)
5119: for(ji=1;ji<=nlstate*nlstate;ji++)
5120: varhe[ij][ji][(int)age] += doldm[ij][ji]*hf*hf;
1.126 brouard 5121: }
5122: }
1.218 brouard 5123:
1.126 brouard 5124: /* Computing expectancies */
5125: hpxij(p3matm,nhstepm,age,hstepm,x,nlstate,stepm,oldm, savm, cij);
5126: for(i=1; i<=nlstate;i++)
5127: for(j=1; j<=nlstate;j++)
1.222 brouard 5128: for (h=0, eij[i][j][(int)age]=0; h<=nhstepm-1; h++){
5129: eij[i][j][(int)age] += (p3matm[i][j][h]+p3matm[i][j][h+1])/2.0*hf;
1.218 brouard 5130:
1.222 brouard 5131: /* 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 5132:
1.222 brouard 5133: }
1.218 brouard 5134:
1.126 brouard 5135: fprintf(ficresstdeij,"%3.0f",age );
5136: for(i=1; i<=nlstate;i++){
5137: eip=0.;
5138: vip=0.;
5139: for(j=1; j<=nlstate;j++){
1.222 brouard 5140: eip += eij[i][j][(int)age];
5141: for(k=1; k<=nlstate;k++) /* Sum on j and k of cov(eij,eik) */
5142: vip += varhe[(j-1)*nlstate+i][(k-1)*nlstate+i][(int)age];
5143: 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 5144: }
5145: fprintf(ficresstdeij," %9.4f (%.4f)", eip, sqrt(vip));
5146: }
5147: fprintf(ficresstdeij,"\n");
1.218 brouard 5148:
1.126 brouard 5149: fprintf(ficrescveij,"%3.0f",age );
5150: for(i=1; i<=nlstate;i++)
5151: for(j=1; j<=nlstate;j++){
1.222 brouard 5152: cptj= (j-1)*nlstate+i;
5153: for(i2=1; i2<=nlstate;i2++)
5154: for(j2=1; j2<=nlstate;j2++){
5155: cptj2= (j2-1)*nlstate+i2;
5156: if(cptj2 <= cptj)
5157: fprintf(ficrescveij," %.4f", varhe[cptj][cptj2][(int)age]);
5158: }
1.126 brouard 5159: }
5160: fprintf(ficrescveij,"\n");
1.218 brouard 5161:
1.126 brouard 5162: }
5163: free_matrix(gm,0,nhstepm,1,nlstate*nlstate);
5164: free_matrix(gp,0,nhstepm,1,nlstate*nlstate);
5165: free_ma3x(gradg,0,nhstepm,1,npar,1,nlstate*nlstate);
5166: free_ma3x(trgradg,0,nhstepm,1,nlstate*nlstate,1,npar);
5167: free_ma3x(p3matm,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
5168: free_ma3x(p3matp,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
5169: printf("\n");
5170: fprintf(ficlog,"\n");
1.218 brouard 5171:
1.126 brouard 5172: free_vector(xm,1,npar);
5173: free_vector(xp,1,npar);
5174: free_matrix(dnewm,1,nlstate*nlstate,1,npar);
5175: free_matrix(doldm,1,nlstate*nlstate,1,nlstate*nlstate);
5176: free_ma3x(varhe,1,nlstate*nlstate,1,nlstate*nlstate,(int) bage, (int)fage);
5177: }
1.218 brouard 5178:
1.126 brouard 5179: /************ Variance ******************/
1.209 brouard 5180: void varevsij(char optionfilefiname[], double ***vareij, double **matcov, double x[], double delti[], int nlstate, int stepm, double bage, double fage, double **oldm, double **savm, double **prlim, double ftolpl, int *ncvyearp, int ij, int estepm, int cptcov, int cptcod, int popbased, int mobilav, char strstart[])
1.218 brouard 5181: {
5182: /* Variance of health expectancies */
5183: /* double **prevalim(double **prlim, int nlstate, double *xp, double age, double **oldm, double ** savm,double ftolpl);*/
5184: /* double **newm;*/
5185: /* int movingaverage(double ***probs, double bage,double fage, double ***mobaverage, int mobilav)*/
5186:
5187: /* int movingaverage(); */
5188: double **dnewm,**doldm;
5189: double **dnewmp,**doldmp;
5190: int i, j, nhstepm, hstepm, h, nstepm ;
5191: int k;
5192: double *xp;
5193: double **gp, **gm; /* for var eij */
5194: double ***gradg, ***trgradg; /*for var eij */
5195: double **gradgp, **trgradgp; /* for var p point j */
5196: double *gpp, *gmp; /* for var p point j */
5197: double **varppt; /* for var p point j nlstate to nlstate+ndeath */
5198: double ***p3mat;
5199: double age,agelim, hf;
5200: /* double ***mobaverage; */
5201: int theta;
5202: char digit[4];
5203: char digitp[25];
5204:
5205: char fileresprobmorprev[FILENAMELENGTH];
5206:
5207: if(popbased==1){
5208: if(mobilav!=0)
5209: strcpy(digitp,"-POPULBASED-MOBILAV_");
5210: else strcpy(digitp,"-POPULBASED-NOMOBIL_");
5211: }
5212: else
5213: strcpy(digitp,"-STABLBASED_");
1.126 brouard 5214:
1.218 brouard 5215: /* if (mobilav!=0) { */
5216: /* mobaverage= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
5217: /* if (movingaverage(probs, bage, fage, mobaverage,mobilav)!=0){ */
5218: /* fprintf(ficlog," Error in movingaverage mobilav=%d\n",mobilav); */
5219: /* printf(" Error in movingaverage mobilav=%d\n",mobilav); */
5220: /* } */
5221: /* } */
5222:
5223: strcpy(fileresprobmorprev,"PRMORPREV-");
5224: sprintf(digit,"%-d",ij);
5225: /*printf("DIGIT=%s, ij=%d ijr=%-d|\n",digit, ij,ij);*/
5226: strcat(fileresprobmorprev,digit); /* Tvar to be done */
5227: strcat(fileresprobmorprev,digitp); /* Popbased or not, mobilav or not */
5228: strcat(fileresprobmorprev,fileresu);
5229: if((ficresprobmorprev=fopen(fileresprobmorprev,"w"))==NULL) {
5230: printf("Problem with resultfile: %s\n", fileresprobmorprev);
5231: fprintf(ficlog,"Problem with resultfile: %s\n", fileresprobmorprev);
5232: }
5233: printf("Computing total mortality p.j=w1*p1j+w2*p2j+..: result on file '%s' \n",fileresprobmorprev);
5234: fprintf(ficlog,"Computing total mortality p.j=w1*p1j+w2*p2j+..: result on file '%s' \n",fileresprobmorprev);
5235: pstamp(ficresprobmorprev);
5236: 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);
5237: fprintf(ficresprobmorprev,"# Age cov=%-d",ij);
5238: for(j=nlstate+1; j<=(nlstate+ndeath);j++){
5239: fprintf(ficresprobmorprev," p.%-d SE",j);
5240: for(i=1; i<=nlstate;i++)
5241: fprintf(ficresprobmorprev," w%1d p%-d%-d",i,i,j);
5242: }
5243: fprintf(ficresprobmorprev,"\n");
5244:
5245: fprintf(ficgp,"\n# Routine varevsij");
5246: fprintf(ficgp,"\nunset title \n");
5247: /* fprintf(fichtm, "#Local time at start: %s", strstart);*/
5248: 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");
5249: fprintf(fichtm,"\n<br>%s <br>\n",digitp);
5250: /* } */
5251: varppt = matrix(nlstate+1,nlstate+ndeath,nlstate+1,nlstate+ndeath);
5252: pstamp(ficresvij);
5253: fprintf(ficresvij,"# Variance and covariance of health expectancies e.j \n# (weighted average of eij where weights are ");
5254: if(popbased==1)
5255: 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);
5256: else
5257: fprintf(ficresvij,"the age specific period (stable) prevalences in each health state \n");
5258: fprintf(ficresvij,"# Age");
5259: for(i=1; i<=nlstate;i++)
5260: for(j=1; j<=nlstate;j++)
5261: fprintf(ficresvij," Cov(e.%1d, e.%1d)",i,j);
5262: fprintf(ficresvij,"\n");
5263:
5264: xp=vector(1,npar);
5265: dnewm=matrix(1,nlstate,1,npar);
5266: doldm=matrix(1,nlstate,1,nlstate);
5267: dnewmp= matrix(nlstate+1,nlstate+ndeath,1,npar);
5268: doldmp= matrix(nlstate+1,nlstate+ndeath,nlstate+1,nlstate+ndeath);
5269:
5270: gradgp=matrix(1,npar,nlstate+1,nlstate+ndeath);
5271: gpp=vector(nlstate+1,nlstate+ndeath);
5272: gmp=vector(nlstate+1,nlstate+ndeath);
5273: trgradgp =matrix(nlstate+1,nlstate+ndeath,1,npar); /* mu or p point j*/
1.126 brouard 5274:
1.218 brouard 5275: if(estepm < stepm){
5276: printf ("Problem %d lower than %d\n",estepm, stepm);
5277: }
5278: else hstepm=estepm;
5279: /* For example we decided to compute the life expectancy with the smallest unit */
5280: /* hstepm beeing the number of stepms, if hstepm=1 the length of hstepm is stepm.
5281: nhstepm is the number of hstepm from age to agelim
5282: nstepm is the number of stepm from age to agelim.
5283: Look at function hpijx to understand why because of memory size limitations,
5284: we decided (b) to get a life expectancy respecting the most precise curvature of the
5285: survival function given by stepm (the optimization length). Unfortunately it
5286: means that if the survival funtion is printed every two years of age and if
5287: you sum them up and add 1 year (area under the trapezoids) you won't get the same
5288: results. So we changed our mind and took the option of the best precision.
5289: */
5290: hstepm=hstepm/stepm; /* Typically in stepm units, if stepm=6 & estepm=24 , = 24/6 months = 4 */
5291: agelim = AGESUP;
5292: for (age=bage; age<=fage; age ++){ /* If stepm=6 months */
5293: nstepm=(int) rint((agelim-age)*YEARM/stepm); /* Typically 20 years = 20*12/6=40 */
5294: nhstepm = nstepm/hstepm;/* Expressed in hstepm, typically nhstepm=40/4=10 */
5295: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
5296: gradg=ma3x(0,nhstepm,1,npar,1,nlstate);
5297: gp=matrix(0,nhstepm,1,nlstate);
5298: gm=matrix(0,nhstepm,1,nlstate);
5299:
5300:
5301: for(theta=1; theta <=npar; theta++){
5302: for(i=1; i<=npar; i++){ /* Computes gradient x + delta*/
5303: xp[i] = x[i] + (i==theta ?delti[theta]:0);
5304: }
5305:
5306: prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp,ij);
5307:
5308: if (popbased==1) {
5309: if(mobilav ==0){
5310: for(i=1; i<=nlstate;i++)
5311: prlim[i][i]=probs[(int)age][i][ij];
5312: }else{ /* mobilav */
5313: for(i=1; i<=nlstate;i++)
5314: prlim[i][i]=mobaverage[(int)age][i][ij];
5315: }
5316: }
5317:
5318: hpxij(p3mat,nhstepm,age,hstepm,xp,nlstate,stepm,oldm,savm, ij); /* Returns p3mat[i][j][h] for h=1 to nhstepm */
5319: for(j=1; j<= nlstate; j++){
5320: for(h=0; h<=nhstepm; h++){
5321: for(i=1, gp[h][j]=0.;i<=nlstate;i++)
5322: gp[h][j] += prlim[i][i]*p3mat[i][j][h];
5323: }
5324: }
5325: /* Next for computing probability of death (h=1 means
5326: computed over hstepm matrices product = hstepm*stepm months)
5327: as a weighted average of prlim.
5328: */
5329: for(j=nlstate+1;j<=nlstate+ndeath;j++){
5330: for(i=1,gpp[j]=0.; i<= nlstate; i++)
5331: gpp[j] += prlim[i][i]*p3mat[i][j][1];
5332: }
5333: /* end probability of death */
5334:
5335: for(i=1; i<=npar; i++) /* Computes gradient x - delta */
5336: xp[i] = x[i] - (i==theta ?delti[theta]:0);
5337:
5338: prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp, ij);
5339:
5340: if (popbased==1) {
5341: if(mobilav ==0){
5342: for(i=1; i<=nlstate;i++)
5343: prlim[i][i]=probs[(int)age][i][ij];
5344: }else{ /* mobilav */
5345: for(i=1; i<=nlstate;i++)
5346: prlim[i][i]=mobaverage[(int)age][i][ij];
5347: }
5348: }
5349:
5350: hpxij(p3mat,nhstepm,age,hstepm,xp,nlstate,stepm,oldm,savm, ij);
5351:
5352: for(j=1; j<= nlstate; j++){ /* Sum of wi * eij = e.j */
5353: for(h=0; h<=nhstepm; h++){
5354: for(i=1, gm[h][j]=0.;i<=nlstate;i++)
5355: gm[h][j] += prlim[i][i]*p3mat[i][j][h];
5356: }
5357: }
5358: /* This for computing probability of death (h=1 means
5359: computed over hstepm matrices product = hstepm*stepm months)
5360: as a weighted average of prlim.
5361: */
5362: for(j=nlstate+1;j<=nlstate+ndeath;j++){
5363: for(i=1,gmp[j]=0.; i<= nlstate; i++)
5364: gmp[j] += prlim[i][i]*p3mat[i][j][1];
5365: }
5366: /* end probability of death */
5367:
5368: for(j=1; j<= nlstate; j++) /* vareij */
5369: for(h=0; h<=nhstepm; h++){
5370: gradg[h][theta][j]= (gp[h][j]-gm[h][j])/2./delti[theta];
5371: }
5372:
5373: for(j=nlstate+1; j<= nlstate+ndeath; j++){ /* var mu */
5374: gradgp[theta][j]= (gpp[j]-gmp[j])/2./delti[theta];
5375: }
5376:
5377: } /* End theta */
5378:
5379: trgradg =ma3x(0,nhstepm,1,nlstate,1,npar); /* veij */
5380:
5381: for(h=0; h<=nhstepm; h++) /* veij */
5382: for(j=1; j<=nlstate;j++)
5383: for(theta=1; theta <=npar; theta++)
5384: trgradg[h][j][theta]=gradg[h][theta][j];
5385:
5386: for(j=nlstate+1; j<=nlstate+ndeath;j++) /* mu */
5387: for(theta=1; theta <=npar; theta++)
5388: trgradgp[j][theta]=gradgp[theta][j];
5389:
5390:
5391: hf=hstepm*stepm/YEARM; /* Duration of hstepm expressed in year unit. */
5392: for(i=1;i<=nlstate;i++)
5393: for(j=1;j<=nlstate;j++)
5394: vareij[i][j][(int)age] =0.;
5395:
5396: for(h=0;h<=nhstepm;h++){
5397: for(k=0;k<=nhstepm;k++){
5398: matprod2(dnewm,trgradg[h],1,nlstate,1,npar,1,npar,matcov);
5399: matprod2(doldm,dnewm,1,nlstate,1,npar,1,nlstate,gradg[k]);
5400: for(i=1;i<=nlstate;i++)
5401: for(j=1;j<=nlstate;j++)
5402: vareij[i][j][(int)age] += doldm[i][j]*hf*hf;
5403: }
5404: }
5405:
5406: /* pptj */
5407: matprod2(dnewmp,trgradgp,nlstate+1,nlstate+ndeath,1,npar,1,npar,matcov);
5408: matprod2(doldmp,dnewmp,nlstate+1,nlstate+ndeath,1,npar,nlstate+1,nlstate+ndeath,gradgp);
5409: for(j=nlstate+1;j<=nlstate+ndeath;j++)
5410: for(i=nlstate+1;i<=nlstate+ndeath;i++)
5411: varppt[j][i]=doldmp[j][i];
5412: /* end ppptj */
5413: /* x centered again */
5414:
5415: prevalim(prlim,nlstate,x,age,oldm,savm,ftolpl,ncvyearp,ij);
5416:
5417: if (popbased==1) {
5418: if(mobilav ==0){
5419: for(i=1; i<=nlstate;i++)
5420: prlim[i][i]=probs[(int)age][i][ij];
5421: }else{ /* mobilav */
5422: for(i=1; i<=nlstate;i++)
5423: prlim[i][i]=mobaverage[(int)age][i][ij];
5424: }
5425: }
5426:
5427: /* This for computing probability of death (h=1 means
5428: computed over hstepm (estepm) matrices product = hstepm*stepm months)
5429: as a weighted average of prlim.
5430: */
5431: hpxij(p3mat,nhstepm,age,hstepm,x,nlstate,stepm,oldm,savm, ij);
5432: for(j=nlstate+1;j<=nlstate+ndeath;j++){
5433: for(i=1,gmp[j]=0.;i<= nlstate; i++)
5434: gmp[j] += prlim[i][i]*p3mat[i][j][1];
5435: }
5436: /* end probability of death */
5437:
5438: fprintf(ficresprobmorprev,"%3d %d ",(int) age, ij);
5439: for(j=nlstate+1; j<=(nlstate+ndeath);j++){
5440: fprintf(ficresprobmorprev," %11.3e %11.3e",gmp[j], sqrt(varppt[j][j]));
5441: for(i=1; i<=nlstate;i++){
5442: fprintf(ficresprobmorprev," %11.3e %11.3e ",prlim[i][i],p3mat[i][j][1]);
5443: }
5444: }
5445: fprintf(ficresprobmorprev,"\n");
5446:
5447: fprintf(ficresvij,"%.0f ",age );
5448: for(i=1; i<=nlstate;i++)
5449: for(j=1; j<=nlstate;j++){
5450: fprintf(ficresvij," %.4f", vareij[i][j][(int)age]);
5451: }
5452: fprintf(ficresvij,"\n");
5453: free_matrix(gp,0,nhstepm,1,nlstate);
5454: free_matrix(gm,0,nhstepm,1,nlstate);
5455: free_ma3x(gradg,0,nhstepm,1,npar,1,nlstate);
5456: free_ma3x(trgradg,0,nhstepm,1,nlstate,1,npar);
5457: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
5458: } /* End age */
5459: free_vector(gpp,nlstate+1,nlstate+ndeath);
5460: free_vector(gmp,nlstate+1,nlstate+ndeath);
5461: free_matrix(gradgp,1,npar,nlstate+1,nlstate+ndeath);
5462: free_matrix(trgradgp,nlstate+1,nlstate+ndeath,1,npar); /* mu or p point j*/
5463: /* fprintf(ficgp,"\nunset parametric;unset label; set ter png small size 320, 240"); */
5464: fprintf(ficgp,"\nunset parametric;unset label; set ter svg size 640, 480");
5465: /* for(j=nlstate+1; j<= nlstate+ndeath; j++){ *//* Only the first actually */
5466: fprintf(ficgp,"\n set log y; unset log x;set xlabel \"Age\"; set ylabel \"Force of mortality (year-1)\";");
5467: fprintf(ficgp,"\nset out \"%s%s.svg\";",subdirf3(optionfilefiname,"VARMUPTJGR-",digitp),digit);
5468: /* fprintf(ficgp,"\n plot \"%s\" u 1:($3*%6.3f) not w l 1 ",fileresprobmorprev,YEARM/estepm); */
5469: /* fprintf(ficgp,"\n replot \"%s\" u 1:(($3+1.96*$4)*%6.3f) t \"95\%% interval\" w l 2 ",fileresprobmorprev,YEARM/estepm); */
5470: /* fprintf(ficgp,"\n replot \"%s\" u 1:(($3-1.96*$4)*%6.3f) not w l 2 ",fileresprobmorprev,YEARM/estepm); */
5471: fprintf(ficgp,"\n plot \"%s\" u 1:($3) not w l lt 1 ",subdirf(fileresprobmorprev));
5472: fprintf(ficgp,"\n replot \"%s\" u 1:(($3+1.96*$4)) t \"95%% interval\" w l lt 2 ",subdirf(fileresprobmorprev));
5473: fprintf(ficgp,"\n replot \"%s\" u 1:(($3-1.96*$4)) not w l lt 2 ",subdirf(fileresprobmorprev));
5474: fprintf(fichtm,"\n<br> File (multiple files are possible if covariates are present): <A href=\"%s\">%s</a>\n",subdirf(fileresprobmorprev),subdirf(fileresprobmorprev));
5475: 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);
5476: /* 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 5477: */
1.218 brouard 5478: /* fprintf(ficgp,"\nset out \"varmuptjgr%s%s%s.svg\";replot;",digitp,optionfilefiname,digit); */
5479: fprintf(ficgp,"\nset out;\nset out \"%s%s.svg\";replot;set out;\n",subdirf3(optionfilefiname,"VARMUPTJGR-",digitp),digit);
1.126 brouard 5480:
1.218 brouard 5481: free_vector(xp,1,npar);
5482: free_matrix(doldm,1,nlstate,1,nlstate);
5483: free_matrix(dnewm,1,nlstate,1,npar);
5484: free_matrix(doldmp,nlstate+1,nlstate+ndeath,nlstate+1,nlstate+ndeath);
5485: free_matrix(dnewmp,nlstate+1,nlstate+ndeath,1,npar);
5486: free_matrix(varppt,nlstate+1,nlstate+ndeath,nlstate+1,nlstate+ndeath);
5487: /* if (mobilav!=0) free_ma3x(mobaverage,1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
5488: fclose(ficresprobmorprev);
5489: fflush(ficgp);
5490: fflush(fichtm);
5491: } /* end varevsij */
1.126 brouard 5492:
5493: /************ Variance of prevlim ******************/
1.209 brouard 5494: void varprevlim(char fileres[], double **varpl, double **matcov, double x[], double delti[], int nlstate, int stepm, double bage, double fage, double **oldm, double **savm, double **prlim, double ftolpl, int *ncvyearp, int ij, char strstart[])
1.126 brouard 5495: {
1.205 brouard 5496: /* Variance of prevalence limit for each state ij using current parameters x[] and estimates of neighbourhood give by delti*/
1.126 brouard 5497: /* double **prevalim(double **prlim, int nlstate, double *xp, double age, double **oldm, double **savm,double ftolpl);*/
1.164 brouard 5498:
1.126 brouard 5499: double **dnewm,**doldm;
5500: int i, j, nhstepm, hstepm;
5501: double *xp;
5502: double *gp, *gm;
5503: double **gradg, **trgradg;
1.208 brouard 5504: double **mgm, **mgp;
1.126 brouard 5505: double age,agelim;
5506: int theta;
5507:
5508: pstamp(ficresvpl);
5509: fprintf(ficresvpl,"# Standard deviation of period (stable) prevalences \n");
5510: fprintf(ficresvpl,"# Age");
5511: for(i=1; i<=nlstate;i++)
5512: fprintf(ficresvpl," %1d-%1d",i,i);
5513: fprintf(ficresvpl,"\n");
5514:
5515: xp=vector(1,npar);
5516: dnewm=matrix(1,nlstate,1,npar);
5517: doldm=matrix(1,nlstate,1,nlstate);
5518:
5519: hstepm=1*YEARM; /* Every year of age */
5520: hstepm=hstepm/stepm; /* Typically in stepm units, if j= 2 years, = 2/6 months = 4 */
5521: agelim = AGESUP;
5522: for (age=bage; age<=fage; age ++){ /* If stepm=6 months */
5523: nhstepm=(int) rint((agelim-age)*YEARM/stepm); /* Typically 20 years = 20*12/6=40 */
5524: if (stepm >= YEARM) hstepm=1;
5525: nhstepm = nhstepm/hstepm; /* Typically 40/4=10 */
5526: gradg=matrix(1,npar,1,nlstate);
1.208 brouard 5527: mgp=matrix(1,npar,1,nlstate);
5528: mgm=matrix(1,npar,1,nlstate);
1.126 brouard 5529: gp=vector(1,nlstate);
5530: gm=vector(1,nlstate);
5531:
5532: for(theta=1; theta <=npar; theta++){
5533: for(i=1; i<=npar; i++){ /* Computes gradient */
5534: xp[i] = x[i] + (i==theta ?delti[theta]:0);
5535: }
1.209 brouard 5536: if((int)age==79 ||(int)age== 80 ||(int)age== 81 )
5537: prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp,ij);
5538: else
5539: prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp,ij);
1.208 brouard 5540: for(i=1;i<=nlstate;i++){
1.126 brouard 5541: gp[i] = prlim[i][i];
1.208 brouard 5542: mgp[theta][i] = prlim[i][i];
5543: }
1.126 brouard 5544: for(i=1; i<=npar; i++) /* Computes gradient */
5545: xp[i] = x[i] - (i==theta ?delti[theta]:0);
1.209 brouard 5546: if((int)age==79 ||(int)age== 80 ||(int)age== 81 )
5547: prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp,ij);
5548: else
5549: prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp,ij);
1.208 brouard 5550: for(i=1;i<=nlstate;i++){
1.126 brouard 5551: gm[i] = prlim[i][i];
1.208 brouard 5552: mgm[theta][i] = prlim[i][i];
5553: }
1.126 brouard 5554: for(i=1;i<=nlstate;i++)
5555: gradg[theta][i]= (gp[i]-gm[i])/2./delti[theta];
1.209 brouard 5556: /* gradg[theta][2]= -gradg[theta][1]; */ /* For testing if nlstate=2 */
1.126 brouard 5557: } /* End theta */
5558:
5559: trgradg =matrix(1,nlstate,1,npar);
5560:
5561: for(j=1; j<=nlstate;j++)
5562: for(theta=1; theta <=npar; theta++)
5563: trgradg[j][theta]=gradg[theta][j];
1.209 brouard 5564: /* if((int)age==79 ||(int)age== 80 ||(int)age== 81 ){ */
5565: /* printf("\nmgm mgp %d ",(int)age); */
5566: /* for(j=1; j<=nlstate;j++){ */
5567: /* printf(" %d ",j); */
5568: /* for(theta=1; theta <=npar; theta++) */
5569: /* printf(" %d %lf %lf",theta,mgm[theta][j],mgp[theta][j]); */
5570: /* printf("\n "); */
5571: /* } */
5572: /* } */
5573: /* if((int)age==79 ||(int)age== 80 ||(int)age== 81 ){ */
5574: /* printf("\n gradg %d ",(int)age); */
5575: /* for(j=1; j<=nlstate;j++){ */
5576: /* printf("%d ",j); */
5577: /* for(theta=1; theta <=npar; theta++) */
5578: /* printf("%d %lf ",theta,gradg[theta][j]); */
5579: /* printf("\n "); */
5580: /* } */
5581: /* } */
1.126 brouard 5582:
5583: for(i=1;i<=nlstate;i++)
5584: varpl[i][(int)age] =0.;
1.209 brouard 5585: if((int)age==79 ||(int)age== 80 ||(int)age== 81){
1.205 brouard 5586: matprod2(dnewm,trgradg,1,nlstate,1,npar,1,npar,matcov);
5587: matprod2(doldm,dnewm,1,nlstate,1,npar,1,nlstate,gradg);
5588: }else{
1.126 brouard 5589: matprod2(dnewm,trgradg,1,nlstate,1,npar,1,npar,matcov);
5590: matprod2(doldm,dnewm,1,nlstate,1,npar,1,nlstate,gradg);
1.205 brouard 5591: }
1.126 brouard 5592: for(i=1;i<=nlstate;i++)
5593: varpl[i][(int)age] = doldm[i][i]; /* Covariances are useless */
5594:
5595: fprintf(ficresvpl,"%.0f ",age );
5596: for(i=1; i<=nlstate;i++)
5597: fprintf(ficresvpl," %.5f (%.5f)",prlim[i][i],sqrt(varpl[i][(int)age]));
5598: fprintf(ficresvpl,"\n");
5599: free_vector(gp,1,nlstate);
5600: free_vector(gm,1,nlstate);
1.208 brouard 5601: free_matrix(mgm,1,npar,1,nlstate);
5602: free_matrix(mgp,1,npar,1,nlstate);
1.126 brouard 5603: free_matrix(gradg,1,npar,1,nlstate);
5604: free_matrix(trgradg,1,nlstate,1,npar);
5605: } /* End age */
5606:
5607: free_vector(xp,1,npar);
5608: free_matrix(doldm,1,nlstate,1,npar);
5609: free_matrix(dnewm,1,nlstate,1,nlstate);
5610:
5611: }
5612:
5613: /************ Variance of one-step probabilities ******************/
5614: 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 5615: {
5616: int i, j=0, k1, l1, tj;
5617: int k2, l2, j1, z1;
5618: int k=0, l;
5619: int first=1, first1, first2;
5620: double cv12, mu1, mu2, lc1, lc2, v12, v21, v11, v22,v1,v2, c12, tnalp;
5621: double **dnewm,**doldm;
5622: double *xp;
5623: double *gp, *gm;
5624: double **gradg, **trgradg;
5625: double **mu;
5626: double age, cov[NCOVMAX+1];
5627: double std=2.0; /* Number of standard deviation wide of confidence ellipsoids */
5628: int theta;
5629: char fileresprob[FILENAMELENGTH];
5630: char fileresprobcov[FILENAMELENGTH];
5631: char fileresprobcor[FILENAMELENGTH];
5632: double ***varpij;
5633:
5634: strcpy(fileresprob,"PROB_");
5635: strcat(fileresprob,fileres);
5636: if((ficresprob=fopen(fileresprob,"w"))==NULL) {
5637: printf("Problem with resultfile: %s\n", fileresprob);
5638: fprintf(ficlog,"Problem with resultfile: %s\n", fileresprob);
5639: }
5640: strcpy(fileresprobcov,"PROBCOV_");
5641: strcat(fileresprobcov,fileresu);
5642: if((ficresprobcov=fopen(fileresprobcov,"w"))==NULL) {
5643: printf("Problem with resultfile: %s\n", fileresprobcov);
5644: fprintf(ficlog,"Problem with resultfile: %s\n", fileresprobcov);
5645: }
5646: strcpy(fileresprobcor,"PROBCOR_");
5647: strcat(fileresprobcor,fileresu);
5648: if((ficresprobcor=fopen(fileresprobcor,"w"))==NULL) {
5649: printf("Problem with resultfile: %s\n", fileresprobcor);
5650: fprintf(ficlog,"Problem with resultfile: %s\n", fileresprobcor);
5651: }
5652: printf("Computing standard deviation of one-step probabilities: result on file '%s' \n",fileresprob);
5653: fprintf(ficlog,"Computing standard deviation of one-step probabilities: result on file '%s' \n",fileresprob);
5654: printf("Computing matrix of variance covariance of one-step probabilities: result on file '%s' \n",fileresprobcov);
5655: fprintf(ficlog,"Computing matrix of variance covariance of one-step probabilities: result on file '%s' \n",fileresprobcov);
5656: printf("and correlation matrix of one-step probabilities: result on file '%s' \n",fileresprobcor);
5657: fprintf(ficlog,"and correlation matrix of one-step probabilities: result on file '%s' \n",fileresprobcor);
5658: pstamp(ficresprob);
5659: fprintf(ficresprob,"#One-step probabilities and stand. devi in ()\n");
5660: fprintf(ficresprob,"# Age");
5661: pstamp(ficresprobcov);
5662: fprintf(ficresprobcov,"#One-step probabilities and covariance matrix\n");
5663: fprintf(ficresprobcov,"# Age");
5664: pstamp(ficresprobcor);
5665: fprintf(ficresprobcor,"#One-step probabilities and correlation matrix\n");
5666: fprintf(ficresprobcor,"# Age");
1.126 brouard 5667:
5668:
1.222 brouard 5669: for(i=1; i<=nlstate;i++)
5670: for(j=1; j<=(nlstate+ndeath);j++){
5671: fprintf(ficresprob," p%1d-%1d (SE)",i,j);
5672: fprintf(ficresprobcov," p%1d-%1d ",i,j);
5673: fprintf(ficresprobcor," p%1d-%1d ",i,j);
5674: }
5675: /* fprintf(ficresprob,"\n");
5676: fprintf(ficresprobcov,"\n");
5677: fprintf(ficresprobcor,"\n");
5678: */
5679: xp=vector(1,npar);
5680: dnewm=matrix(1,(nlstate)*(nlstate+ndeath),1,npar);
5681: doldm=matrix(1,(nlstate)*(nlstate+ndeath),1,(nlstate)*(nlstate+ndeath));
5682: mu=matrix(1,(nlstate)*(nlstate+ndeath), (int) bage, (int)fage);
5683: varpij=ma3x(1,nlstate*(nlstate+ndeath),1,nlstate*(nlstate+ndeath),(int) bage, (int) fage);
5684: first=1;
5685: fprintf(ficgp,"\n# Routine varprob");
5686: fprintf(fichtm,"\n<li><h4> Computing and drawing one step probabilities with their confidence intervals</h4></li>\n");
5687: fprintf(fichtm,"\n");
5688:
5689: 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);
5690: 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);
5691: fprintf(fichtmcov,"\nEllipsoids of confidence centered on point (p<inf>ij</inf>, p<inf>kl</inf>) are estimated \
1.126 brouard 5692: and drawn. It helps understanding how is the covariance between two incidences.\
5693: They are expressed in year<sup>-1</sup> in order to be less dependent of stepm.<br>\n");
1.222 brouard 5694: 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 5695: It can be understood this way: if pij and pkl where uncorrelated the (2x2) matrix of covariance \
5696: would have been (1/(var pij), 0 , 0, 1/(var pkl)), and the confidence interval would be 2 \
5697: standard deviations wide on each axis. <br>\
5698: Now, if both incidences are correlated (usual case) we diagonalised the inverse of the covariance matrix\
5699: and made the appropriate rotation to look at the uncorrelated principal directions.<br>\
5700: To be simple, these graphs help to understand the significativity of each parameter in relation to a second other one.<br> \n");
5701:
1.222 brouard 5702: cov[1]=1;
5703: /* tj=cptcoveff; */
1.225 brouard 5704: tj = (int) pow(2,cptcoveff);
1.222 brouard 5705: if (cptcovn<1) {tj=1;ncodemax[1]=1;}
5706: j1=0;
1.224 brouard 5707: for(j1=1; j1<=tj;j1++){ /* For each valid combination of covariates or only once*/
1.222 brouard 5708: if (cptcovn>0) {
5709: fprintf(ficresprob, "\n#********** Variable ");
1.225 brouard 5710: for (z1=1; z1<=cptcoveff; z1++) fprintf(ficresprob, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
1.222 brouard 5711: fprintf(ficresprob, "**********\n#\n");
5712: fprintf(ficresprobcov, "\n#********** Variable ");
1.225 brouard 5713: for (z1=1; z1<=cptcoveff; z1++) fprintf(ficresprobcov, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
1.222 brouard 5714: fprintf(ficresprobcov, "**********\n#\n");
1.220 brouard 5715:
1.222 brouard 5716: fprintf(ficgp, "\n#********** Variable ");
1.225 brouard 5717: for (z1=1; z1<=cptcoveff; z1++) fprintf(ficgp, " V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
1.222 brouard 5718: fprintf(ficgp, "**********\n#\n");
1.220 brouard 5719:
5720:
1.222 brouard 5721: fprintf(fichtmcov, "\n<hr size=\"2\" color=\"#EC5E5E\">********** Variable ");
1.225 brouard 5722: for (z1=1; z1<=cptcoveff; z1++) fprintf(fichtm, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
1.222 brouard 5723: fprintf(fichtmcov, "**********\n<hr size=\"2\" color=\"#EC5E5E\">");
1.220 brouard 5724:
1.222 brouard 5725: fprintf(ficresprobcor, "\n#********** Variable ");
1.225 brouard 5726: for (z1=1; z1<=cptcoveff; z1++) fprintf(ficresprobcor, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
1.222 brouard 5727: fprintf(ficresprobcor, "**********\n#");
5728: if(invalidvarcomb[j1]){
5729: fprintf(ficgp,"\n#Combination (%d) ignored because no cases \n",j1);
5730: fprintf(fichtmcov,"\n<h3>Combination (%d) ignored because no cases </h3>\n",j1);
5731: continue;
5732: }
5733: }
5734: gradg=matrix(1,npar,1,(nlstate)*(nlstate+ndeath));
5735: trgradg=matrix(1,(nlstate)*(nlstate+ndeath),1,npar);
5736: gp=vector(1,(nlstate)*(nlstate+ndeath));
5737: gm=vector(1,(nlstate)*(nlstate+ndeath));
5738: for (age=bage; age<=fage; age ++){
5739: cov[2]=age;
5740: if(nagesqr==1)
5741: cov[3]= age*age;
5742: for (k=1; k<=cptcovn;k++) {
5743: cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(j1,k)];
5744: /*cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(j1,Tvar[k])];*//* j1 1 2 3 4
5745: * 1 1 1 1 1
5746: * 2 2 1 1 1
5747: * 3 1 2 1 1
5748: */
5749: /* nbcode[1][1]=0 nbcode[1][2]=1;*/
5750: }
5751: /* for (k=1; k<=cptcovage;k++) cov[2+Tage[k]]=cov[2+Tage[k]]*cov[2]; */
5752: for (k=1; k<=cptcovage;k++) cov[2+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,k)]*cov[2];
5753: for (k=1; k<=cptcovprod;k++)
5754: cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)]*nbcode[Tvard[k][2]][codtabm(ij,k)];
1.220 brouard 5755:
5756:
1.222 brouard 5757: for(theta=1; theta <=npar; theta++){
5758: for(i=1; i<=npar; i++)
5759: xp[i] = x[i] + (i==theta ?delti[theta]:(double)0);
1.220 brouard 5760:
1.222 brouard 5761: pmij(pmmij,cov,ncovmodel,xp,nlstate);
1.220 brouard 5762:
1.222 brouard 5763: k=0;
5764: for(i=1; i<= (nlstate); i++){
5765: for(j=1; j<=(nlstate+ndeath);j++){
5766: k=k+1;
5767: gp[k]=pmmij[i][j];
5768: }
5769: }
1.220 brouard 5770:
1.222 brouard 5771: for(i=1; i<=npar; i++)
5772: xp[i] = x[i] - (i==theta ?delti[theta]:(double)0);
1.220 brouard 5773:
1.222 brouard 5774: pmij(pmmij,cov,ncovmodel,xp,nlstate);
5775: k=0;
5776: for(i=1; i<=(nlstate); i++){
5777: for(j=1; j<=(nlstate+ndeath);j++){
5778: k=k+1;
5779: gm[k]=pmmij[i][j];
5780: }
5781: }
1.220 brouard 5782:
1.222 brouard 5783: for(i=1; i<= (nlstate)*(nlstate+ndeath); i++)
5784: gradg[theta][i]=(gp[i]-gm[i])/(double)2./delti[theta];
5785: }
1.126 brouard 5786:
1.222 brouard 5787: for(j=1; j<=(nlstate)*(nlstate+ndeath);j++)
5788: for(theta=1; theta <=npar; theta++)
5789: trgradg[j][theta]=gradg[theta][j];
1.220 brouard 5790:
1.222 brouard 5791: matprod2(dnewm,trgradg,1,(nlstate)*(nlstate+ndeath),1,npar,1,npar,matcov);
5792: matprod2(doldm,dnewm,1,(nlstate)*(nlstate+ndeath),1,npar,1,(nlstate)*(nlstate+ndeath),gradg);
1.220 brouard 5793:
1.222 brouard 5794: pmij(pmmij,cov,ncovmodel,x,nlstate);
1.220 brouard 5795:
1.222 brouard 5796: k=0;
5797: for(i=1; i<=(nlstate); i++){
5798: for(j=1; j<=(nlstate+ndeath);j++){
5799: k=k+1;
5800: mu[k][(int) age]=pmmij[i][j];
5801: }
5802: }
5803: for(i=1;i<=(nlstate)*(nlstate+ndeath);i++)
5804: for(j=1;j<=(nlstate)*(nlstate+ndeath);j++)
5805: varpij[i][j][(int)age] = doldm[i][j];
1.220 brouard 5806:
1.222 brouard 5807: /*printf("\n%d ",(int)age);
5808: for (i=1; i<=(nlstate)*(nlstate+ndeath);i++){
5809: printf("%e [%e ;%e] ",gm[i],gm[i]-2*sqrt(doldm[i][i]),gm[i]+2*sqrt(doldm[i][i]));
5810: fprintf(ficlog,"%e [%e ;%e] ",gm[i],gm[i]-2*sqrt(doldm[i][i]),gm[i]+2*sqrt(doldm[i][i]));
5811: }*/
1.220 brouard 5812:
1.222 brouard 5813: fprintf(ficresprob,"\n%d ",(int)age);
5814: fprintf(ficresprobcov,"\n%d ",(int)age);
5815: fprintf(ficresprobcor,"\n%d ",(int)age);
1.220 brouard 5816:
1.222 brouard 5817: for (i=1; i<=(nlstate)*(nlstate+ndeath);i++)
5818: fprintf(ficresprob,"%11.3e (%11.3e) ",mu[i][(int) age],sqrt(varpij[i][i][(int)age]));
5819: for (i=1; i<=(nlstate)*(nlstate+ndeath);i++){
5820: fprintf(ficresprobcov,"%11.3e ",mu[i][(int) age]);
5821: fprintf(ficresprobcor,"%11.3e ",mu[i][(int) age]);
5822: }
5823: i=0;
5824: for (k=1; k<=(nlstate);k++){
5825: for (l=1; l<=(nlstate+ndeath);l++){
5826: i++;
5827: fprintf(ficresprobcov,"\n%d %d-%d",(int)age,k,l);
5828: fprintf(ficresprobcor,"\n%d %d-%d",(int)age,k,l);
5829: for (j=1; j<=i;j++){
5830: /* printf(" k=%d l=%d i=%d j=%d\n",k,l,i,j);fflush(stdout); */
5831: fprintf(ficresprobcov," %11.3e",varpij[i][j][(int)age]);
5832: fprintf(ficresprobcor," %11.3e",varpij[i][j][(int) age]/sqrt(varpij[i][i][(int) age])/sqrt(varpij[j][j][(int)age]));
5833: }
5834: }
5835: }/* end of loop for state */
5836: } /* end of loop for age */
5837: free_vector(gp,1,(nlstate+ndeath)*(nlstate+ndeath));
5838: free_vector(gm,1,(nlstate+ndeath)*(nlstate+ndeath));
5839: free_matrix(trgradg,1,(nlstate+ndeath)*(nlstate+ndeath),1,npar);
5840: free_matrix(gradg,1,(nlstate+ndeath)*(nlstate+ndeath),1,npar);
5841:
5842: /* Confidence intervalle of pij */
5843: /*
5844: fprintf(ficgp,"\nunset parametric;unset label");
5845: fprintf(ficgp,"\nset log y;unset log x; set xlabel \"Age\";set ylabel \"probability (year-1)\"");
5846: fprintf(ficgp,"\nset ter png small\nset size 0.65,0.65");
5847: 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);
5848: fprintf(fichtm,"\n<br><img src=\"pijgr%s.png\"> ",optionfilefiname);
5849: fprintf(ficgp,"\nset out \"pijgr%s.png\"",optionfilefiname);
5850: fprintf(ficgp,"\nplot \"%s\" every :::%d::%d u 1:2 \"\%%lf",k1,k2,xfilevarprob);
5851: */
5852:
5853: /* Drawing ellipsoids of confidence of two variables p(k1-l1,k2-l2)*/
5854: first1=1;first2=2;
5855: for (k2=1; k2<=(nlstate);k2++){
5856: for (l2=1; l2<=(nlstate+ndeath);l2++){
5857: if(l2==k2) continue;
5858: j=(k2-1)*(nlstate+ndeath)+l2;
5859: for (k1=1; k1<=(nlstate);k1++){
5860: for (l1=1; l1<=(nlstate+ndeath);l1++){
5861: if(l1==k1) continue;
5862: i=(k1-1)*(nlstate+ndeath)+l1;
5863: if(i<=j) continue;
5864: for (age=bage; age<=fage; age ++){
5865: if ((int)age %5==0){
5866: v1=varpij[i][i][(int)age]/stepm*YEARM/stepm*YEARM;
5867: v2=varpij[j][j][(int)age]/stepm*YEARM/stepm*YEARM;
5868: cv12=varpij[i][j][(int)age]/stepm*YEARM/stepm*YEARM;
5869: mu1=mu[i][(int) age]/stepm*YEARM ;
5870: mu2=mu[j][(int) age]/stepm*YEARM;
5871: c12=cv12/sqrt(v1*v2);
5872: /* Computing eigen value of matrix of covariance */
5873: lc1=((v1+v2)+sqrt((v1+v2)*(v1+v2) - 4*(v1*v2-cv12*cv12)))/2.;
5874: lc2=((v1+v2)-sqrt((v1+v2)*(v1+v2) - 4*(v1*v2-cv12*cv12)))/2.;
5875: if ((lc2 <0) || (lc1 <0) ){
5876: if(first2==1){
5877: first1=0;
5878: 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);
5879: }
5880: 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);
5881: /* lc1=fabs(lc1); */ /* If we want to have them positive */
5882: /* lc2=fabs(lc2); */
5883: }
1.220 brouard 5884:
1.222 brouard 5885: /* Eigen vectors */
5886: v11=(1./sqrt(1+(v1-lc1)*(v1-lc1)/cv12/cv12));
5887: /*v21=sqrt(1.-v11*v11); *//* error */
5888: v21=(lc1-v1)/cv12*v11;
5889: v12=-v21;
5890: v22=v11;
5891: tnalp=v21/v11;
5892: if(first1==1){
5893: first1=0;
5894: 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);
5895: }
5896: 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);
5897: /*printf(fignu*/
5898: /* mu1+ v11*lc1*cost + v12*lc2*sin(t) */
5899: /* mu2+ v21*lc1*cost + v22*lc2*sin(t) */
5900: if(first==1){
5901: first=0;
5902: fprintf(ficgp,"\n# Ellipsoids of confidence\n#\n");
5903: fprintf(ficgp,"\nset parametric;unset label");
5904: 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);
5905: fprintf(ficgp,"\nset ter svg size 640, 480");
5906: fprintf(fichtmcov,"\n<br>Ellipsoids of confidence cov(p%1d%1d,p%1d%1d) expressed in year<sup>-1</sup>\
1.220 brouard 5907: :<a href=\"%s_%d%1d%1d-%1d%1d.svg\"> \
1.201 brouard 5908: %s_%d%1d%1d-%1d%1d.svg</A>, ",k1,l1,k2,l2,\
1.222 brouard 5909: subdirf2(optionfilefiname,"VARPIJGR_"), j1,k1,l1,k2,l2, \
5910: subdirf2(optionfilefiname,"VARPIJGR_"), j1,k1,l1,k2,l2);
5911: fprintf(fichtmcov,"\n<br><img src=\"%s_%d%1d%1d-%1d%1d.svg\"> ",subdirf2(optionfilefiname,"VARPIJGR_"), j1,k1,l1,k2,l2);
5912: fprintf(fichtmcov,"\n<br> Correlation at age %d (%.3f),",(int) age, c12);
5913: fprintf(ficgp,"\nset out \"%s_%d%1d%1d-%1d%1d.svg\"",subdirf2(optionfilefiname,"VARPIJGR_"), j1,k1,l1,k2,l2);
5914: fprintf(ficgp,"\nset label \"%d\" at %11.3e,%11.3e center",(int) age, mu1,mu2);
5915: fprintf(ficgp,"\n# Age %d, p%1d%1d - p%1d%1d",(int) age, k1,l1,k2,l2);
5916: 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", \
5917: mu1,std,v11,sqrt(lc1),v12,sqrt(lc2), \
5918: mu2,std,v21,sqrt(lc1),v22,sqrt(lc2));
5919: }else{
5920: first=0;
5921: fprintf(fichtmcov," %d (%.3f),",(int) age, c12);
5922: fprintf(ficgp,"\n# Age %d, p%1d%1d - p%1d%1d",(int) age, k1,l1,k2,l2);
5923: fprintf(ficgp,"\nset label \"%d\" at %11.3e,%11.3e center",(int) age, mu1,mu2);
5924: 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", \
5925: mu1,std,v11,sqrt(lc1),v12,sqrt(lc2), \
5926: mu2,std,v21,sqrt(lc1),v22,sqrt(lc2));
5927: }/* if first */
5928: } /* age mod 5 */
5929: } /* end loop age */
5930: fprintf(ficgp,"\nset out;\nset out \"%s_%d%1d%1d-%1d%1d.svg\";replot;set out;",subdirf2(optionfilefiname,"VARPIJGR_"), j1,k1,l1,k2,l2);
5931: first=1;
5932: } /*l12 */
5933: } /* k12 */
5934: } /*l1 */
5935: }/* k1 */
5936: } /* loop on combination of covariates j1 */
5937: free_ma3x(varpij,1,nlstate,1,nlstate+ndeath,(int) bage, (int)fage);
5938: free_matrix(mu,1,(nlstate+ndeath)*(nlstate+ndeath),(int) bage, (int)fage);
5939: free_matrix(doldm,1,(nlstate)*(nlstate+ndeath),1,(nlstate)*(nlstate+ndeath));
5940: free_matrix(dnewm,1,(nlstate)*(nlstate+ndeath),1,npar);
5941: free_vector(xp,1,npar);
5942: fclose(ficresprob);
5943: fclose(ficresprobcov);
5944: fclose(ficresprobcor);
5945: fflush(ficgp);
5946: fflush(fichtmcov);
5947: }
1.126 brouard 5948:
5949:
5950: /******************* Printing html file ***********/
1.201 brouard 5951: void printinghtml(char fileresu[], char title[], char datafile[], int firstpass, \
1.126 brouard 5952: int lastpass, int stepm, int weightopt, char model[],\
5953: int imx,int jmin, int jmax, double jmeanint,char rfileres[],\
1.217 brouard 5954: int popforecast, int prevfcast, int backcast, int estepm , \
1.213 brouard 5955: double jprev1, double mprev1,double anprev1, double dateprev1, \
5956: double jprev2, double mprev2,double anprev2, double dateprev2){
1.126 brouard 5957: int jj1, k1, i1, cpt;
5958:
5959: fprintf(fichtm,"<ul><li><a href='#firstorder'>Result files (first order: no variance)</a>\n \
5960: <li><a href='#secondorder'>Result files (second order (variance)</a>\n \
5961: </ul>");
1.214 brouard 5962: fprintf(fichtm,"<ul><li><h4><a name='firstorder'>Result files (first order: no variance)</a></h4>\n");
5963: 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",
5964: jprev1, mprev1,anprev1,jprev2, mprev2,anprev2,subdirfext3(optionfilefiname,"PHTMFR_",".htm"),subdirfext3(optionfilefiname,"PHTMFR_",".htm"));
5965: 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 5966: jprev1, mprev1,anprev1,jprev2, mprev2,anprev2,subdirfext3(optionfilefiname,"PHTM_",".htm"),subdirfext3(optionfilefiname,"PHTM_",".htm"));
5967: fprintf(fichtm,", <a href=\"%s\">%s</a> (text file) <br>\n",subdirf2(fileresu,"P_"),subdirf2(fileresu,"P_"));
1.126 brouard 5968: fprintf(fichtm,"\
5969: - Estimated transition probabilities over %d (stepm) months: <a href=\"%s\">%s</a><br>\n ",
1.201 brouard 5970: stepm,subdirf2(fileresu,"PIJ_"),subdirf2(fileresu,"PIJ_"));
1.126 brouard 5971: fprintf(fichtm,"\
1.217 brouard 5972: - Estimated back transition probabilities over %d (stepm) months: <a href=\"%s\">%s</a><br>\n ",
5973: stepm,subdirf2(fileresu,"PIJB_"),subdirf2(fileresu,"PIJB_"));
5974: fprintf(fichtm,"\
1.126 brouard 5975: - Period (stable) prevalence in each health state: <a href=\"%s\">%s</a> <br>\n",
1.201 brouard 5976: subdirf2(fileresu,"PL_"),subdirf2(fileresu,"PL_"));
1.126 brouard 5977: fprintf(fichtm,"\
1.217 brouard 5978: - Period (stable) back prevalence in each health state: <a href=\"%s\">%s</a> <br>\n",
5979: subdirf2(fileresu,"PLB_"),subdirf2(fileresu,"PLB_"));
5980: fprintf(fichtm,"\
1.211 brouard 5981: - (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 5982: <a href=\"%s\">%s</a> <br>\n",
1.201 brouard 5983: estepm,subdirf2(fileresu,"E_"),subdirf2(fileresu,"E_"));
1.211 brouard 5984: if(prevfcast==1){
5985: fprintf(fichtm,"\
5986: - Prevalence projections by age and states: \
1.201 brouard 5987: <a href=\"%s\">%s</a> <br>\n</li>", subdirf2(fileresu,"F_"),subdirf2(fileresu,"F_"));
1.211 brouard 5988: }
1.126 brouard 5989:
1.222 brouard 5990: fprintf(fichtm," \n<ul><li><b>Graphs</b></li><p>");
1.126 brouard 5991:
1.225 brouard 5992: m=pow(2,cptcoveff);
1.222 brouard 5993: if (cptcovn < 1) {m=1;ncodemax[1]=1;}
1.126 brouard 5994:
1.222 brouard 5995: jj1=0;
5996: for(k1=1; k1<=m;k1++){
1.220 brouard 5997:
1.222 brouard 5998: /* for(i1=1; i1<=ncodemax[k1];i1++){ */
5999: jj1++;
6000: if (cptcovn > 0) {
6001: fprintf(fichtm,"<hr size=\"2\" color=\"#EC5E5E\">************ Results for covariates");
1.225 brouard 6002: for (cpt=1; cpt<=cptcoveff;cpt++){
1.222 brouard 6003: fprintf(fichtm," V%d=%d ",Tvaraff[cpt],nbcode[Tvaraff[cpt]][codtabm(jj1,cpt)]);
6004: printf(" V%d=%d ",Tvaraff[cpt],nbcode[Tvaraff[cpt]][codtabm(jj1,cpt)]);fflush(stdout);
6005: }
1.230 brouard 6006: /* if(nqfveff+nqtveff 0) */ /* Test to be done */
1.222 brouard 6007: fprintf(fichtm," ************\n<hr size=\"2\" color=\"#EC5E5E\">");
6008: if(invalidvarcomb[k1]){
6009: fprintf(fichtm,"\n<h3>Combination (%d) ignored because no cases </h3>\n",k1);
6010: printf("\nCombination (%d) ignored because no cases \n",k1);
6011: continue;
6012: }
6013: }
6014: /* aij, bij */
6015: fprintf(fichtm,"<br>- Logit model (yours is: 1+age+%s), for example: logit(pij)=log(pij/pii)= aij+ bij age + V1 age + etc. as a function of age: <a href=\"%s_%d-1.svg\">%s_%d-1.svg</a><br> \
1.211 brouard 6016: <img src=\"%s_%d-1.svg\">",model,subdirf2(optionfilefiname,"PE_"),jj1,subdirf2(optionfilefiname,"PE_"),jj1,subdirf2(optionfilefiname,"PE_"),jj1);
1.222 brouard 6017: /* Pij */
6018: fprintf(fichtm,"<br>\n- P<sub>ij</sub> or conditional probabilities to be observed in state j being in state i, %d (stepm) months before: <a href=\"%s_%d-2.svg\">%s_%d-2.svg</a><br> \
1.201 brouard 6019: <img src=\"%s_%d-2.svg\">",stepm,subdirf2(optionfilefiname,"PE_"),jj1,subdirf2(optionfilefiname,"PE_"),jj1,subdirf2(optionfilefiname,"PE_"),jj1);
1.222 brouard 6020: /* Quasi-incidences */
6021: 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 6022: before but expressed in per year i.e. quasi incidences if stepm is small and probabilities too, \
1.211 brouard 6023: incidence (rates) are the limit when h tends to zero of the ratio of the probability <sub>h</sub>P<sub>ij</sub> \
6024: divided by h: <sub>h</sub>P<sub>ij</sub>/h : <a href=\"%s_%d-3.svg\">%s_%d-3.svg</a><br> \
1.201 brouard 6025: <img src=\"%s_%d-3.svg\">",stepm,subdirf2(optionfilefiname,"PE_"),jj1,subdirf2(optionfilefiname,"PE_"),jj1,subdirf2(optionfilefiname,"PE_"),jj1);
1.222 brouard 6026: /* Survival functions (period) in state j */
6027: for(cpt=1; cpt<=nlstate;cpt++){
6028: fprintf(fichtm,"<br>\n- Survival functions in state %d. Or probability to survive in state %d being in state (1 to %d) at different ages. <a href=\"%s%d_%d.svg\">%s%d_%d.svg</a><br> \
1.201 brouard 6029: <img src=\"%s_%d-%d.svg\">", cpt, cpt, nlstate, subdirf2(optionfilefiname,"LIJ_"),cpt,jj1,subdirf2(optionfilefiname,"LIJ_"),cpt,jj1,subdirf2(optionfilefiname,"LIJ_"),cpt,jj1);
1.222 brouard 6030: }
6031: /* State specific survival functions (period) */
6032: for(cpt=1; cpt<=nlstate;cpt++){
6033: fprintf(fichtm,"<br>\n- Survival functions from state %d in each live state and total.\
1.220 brouard 6034: Or probability to survive in various states (1 to %d) being in state %d at different ages. \
1.201 brouard 6035: <a href=\"%s%d_%d.svg\">%s%d_%d.svg</a><br> <img src=\"%s_%d-%d.svg\">", cpt, nlstate, cpt, subdirf2(optionfilefiname,"LIJT_"),cpt,jj1,subdirf2(optionfilefiname,"LIJT_"),cpt,jj1,subdirf2(optionfilefiname,"LIJT_"),cpt,jj1);
1.222 brouard 6036: }
6037: /* Period (stable) prevalence in each health state */
6038: for(cpt=1; cpt<=nlstate;cpt++){
6039: fprintf(fichtm,"<br>\n- Convergence to period (stable) prevalence in state %d. Or probability to be in state %d being in state (1 to %d) at different ages. <a href=\"%s_%d-%d.svg\">%s_%d-%d.svg</a><br> \
1.201 brouard 6040: <img src=\"%s_%d-%d.svg\">", cpt, cpt, nlstate, subdirf2(optionfilefiname,"P_"),cpt,jj1,subdirf2(optionfilefiname,"P_"),cpt,jj1,subdirf2(optionfilefiname,"P_"),cpt,jj1);
1.222 brouard 6041: }
6042: if(backcast==1){
6043: /* Period (stable) back prevalence in each health state */
6044: for(cpt=1; cpt<=nlstate;cpt++){
6045: fprintf(fichtm,"<br>\n- Convergence to period (stable) back prevalence in state %d. Or probability to be in state %d being in state (1 to %d) at different ages. <a href=\"%s_%d-%d.svg\">%s_%d-%d.svg</a><br> \
1.217 brouard 6046: <img src=\"%s_%d-%d.svg\">", cpt, cpt, nlstate, subdirf2(optionfilefiname,"PB_"),cpt,jj1,subdirf2(optionfilefiname,"PB_"),cpt,jj1,subdirf2(optionfilefiname,"PB_"),cpt,jj1);
1.222 brouard 6047: }
1.217 brouard 6048: }
1.222 brouard 6049: if(prevfcast==1){
6050: /* Projection of prevalence up to period (stable) prevalence in each health state */
6051: for(cpt=1; cpt<=nlstate;cpt++){
6052: fprintf(fichtm,"<br>\n- Projection of cross-sectional prevalence (estimated with cases observed from %.1f to %.1f) up to period (stable) prevalence in state %d. Or probability to be in state %d being in state (1 to %d) at different ages. <a href=\"%s%d_%d.svg\">%s%d_%d.svg</a><br> \
1.213 brouard 6053: <img src=\"%s_%d-%d.svg\">", dateprev1, dateprev2, cpt, cpt, nlstate, subdirf2(optionfilefiname,"PROJ_"),cpt,jj1,subdirf2(optionfilefiname,"PROJ_"),cpt,jj1,subdirf2(optionfilefiname,"PROJ_"),cpt,jj1);
1.222 brouard 6054: }
6055: }
1.220 brouard 6056:
1.222 brouard 6057: for(cpt=1; cpt<=nlstate;cpt++) {
6058: fprintf(fichtm,"\n<br>- Life expectancy by health state (%d) at initial age and its decomposition into health expectancies in each alive state (1 to %d) (or area under each survival functions): <a href=\"%s_%d%d.svg\">%s_%d%d.svg</a> <br> \
1.201 brouard 6059: <img src=\"%s_%d%d.svg\">",cpt,nlstate,subdirf2(optionfilefiname,"EXP_"),cpt,jj1,subdirf2(optionfilefiname,"EXP_"),cpt,jj1,subdirf2(optionfilefiname,"EXP_"),cpt,jj1);
1.222 brouard 6060: }
6061: /* } /\* end i1 *\/ */
6062: }/* End k1 */
6063: fprintf(fichtm,"</ul>");
1.126 brouard 6064:
1.222 brouard 6065: fprintf(fichtm,"\
1.126 brouard 6066: \n<br><li><h4> <a name='secondorder'>Result files (second order: variances)</a></h4>\n\
1.193 brouard 6067: - Parameter file with estimated parameters and covariance matrix: <a href=\"%s\">%s</a> <br> \
1.203 brouard 6068: - 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 6069: But because parameters are usually highly correlated (a higher incidence of disability \
6070: and a higher incidence of recovery can give very close observed transition) it might \
6071: be very useful to look not only at linear confidence intervals estimated from the \
6072: variances but at the covariance matrix. And instead of looking at the estimated coefficients \
6073: (parameters) of the logistic regression, it might be more meaningful to visualize the \
6074: covariance matrix of the one-step probabilities. \
6075: See page 'Matrix of variance-covariance of one-step probabilities' below. \n", rfileres,rfileres);
1.126 brouard 6076:
1.222 brouard 6077: fprintf(fichtm," - Standard deviation of one-step probabilities: <a href=\"%s\">%s</a> <br>\n",
6078: subdirf2(fileresu,"PROB_"),subdirf2(fileresu,"PROB_"));
6079: fprintf(fichtm,"\
1.126 brouard 6080: - Variance-covariance of one-step probabilities: <a href=\"%s\">%s</a> <br>\n",
1.222 brouard 6081: subdirf2(fileresu,"PROBCOV_"),subdirf2(fileresu,"PROBCOV_"));
1.126 brouard 6082:
1.222 brouard 6083: fprintf(fichtm,"\
1.126 brouard 6084: - Correlation matrix of one-step probabilities: <a href=\"%s\">%s</a> <br>\n",
1.222 brouard 6085: subdirf2(fileresu,"PROBCOR_"),subdirf2(fileresu,"PROBCOR_"));
6086: fprintf(fichtm,"\
1.126 brouard 6087: - 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): \
6088: <a href=\"%s\">%s</a> <br>\n</li>",
1.201 brouard 6089: estepm,subdirf2(fileresu,"CVE_"),subdirf2(fileresu,"CVE_"));
1.222 brouard 6090: fprintf(fichtm,"\
1.126 brouard 6091: - (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): \
6092: <a href=\"%s\">%s</a> <br>\n</li>",
1.201 brouard 6093: estepm,subdirf2(fileresu,"STDE_"),subdirf2(fileresu,"STDE_"));
1.222 brouard 6094: fprintf(fichtm,"\
1.128 brouard 6095: - 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 6096: estepm, subdirf2(fileresu,"V_"),subdirf2(fileresu,"V_"));
6097: fprintf(fichtm,"\
1.128 brouard 6098: - 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 6099: estepm, subdirf2(fileresu,"T_"),subdirf2(fileresu,"T_"));
6100: fprintf(fichtm,"\
1.126 brouard 6101: - Standard deviation of period (stable) prevalences: <a href=\"%s\">%s</a> <br>\n",\
1.222 brouard 6102: subdirf2(fileresu,"VPL_"),subdirf2(fileresu,"VPL_"));
1.126 brouard 6103:
6104: /* if(popforecast==1) fprintf(fichtm,"\n */
6105: /* - Prevalences forecasting: <a href=\"f%s\">f%s</a> <br>\n */
6106: /* - Population forecasting (if popforecast=1): <a href=\"pop%s\">pop%s</a> <br>\n */
6107: /* <br>",fileres,fileres,fileres,fileres); */
6108: /* else */
6109: /* 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 6110: fflush(fichtm);
6111: fprintf(fichtm," <ul><li><b>Graphs</b></li><p>");
1.126 brouard 6112:
1.225 brouard 6113: m=pow(2,cptcoveff);
1.222 brouard 6114: if (cptcovn < 1) {m=1;ncodemax[1]=1;}
1.126 brouard 6115:
1.222 brouard 6116: jj1=0;
6117: for(k1=1; k1<=m;k1++){
6118: /* for(i1=1; i1<=ncodemax[k1];i1++){ */
6119: jj1++;
1.126 brouard 6120: if (cptcovn > 0) {
6121: fprintf(fichtm,"<hr size=\"2\" color=\"#EC5E5E\">************ Results for covariates");
1.225 brouard 6122: for (cpt=1; cpt<=cptcoveff;cpt++) /**< cptcoveff number of variables */
1.222 brouard 6123: fprintf(fichtm," V%d=%d ",Tvaraff[cpt],nbcode[Tvaraff[cpt]][codtabm(jj1,cpt)]);
1.126 brouard 6124: fprintf(fichtm," ************\n<hr size=\"2\" color=\"#EC5E5E\">");
1.220 brouard 6125:
1.222 brouard 6126: if(invalidvarcomb[k1]){
6127: fprintf(fichtm,"\n<h4>Combination (%d) ignored because no cases </h4>\n",k1);
6128: continue;
6129: }
1.126 brouard 6130: }
6131: for(cpt=1; cpt<=nlstate;cpt++) {
1.218 brouard 6132: fprintf(fichtm,"\n<br>- Observed (cross-sectional) and period (incidence based) \
6133: prevalence (with 95%% confidence interval) in state (%d): <a href=\"%s_%d-%d.svg\"> %s_%d-%d.svg</a>\n <br>\
1.205 brouard 6134: <img src=\"%s_%d-%d.svg\">",cpt,subdirf2(optionfilefiname,"V_"),cpt,jj1,subdirf2(optionfilefiname,"V_"),cpt,jj1,subdirf2(optionfilefiname,"V_"),cpt,jj1);
1.126 brouard 6135: }
6136: fprintf(fichtm,"\n<br>- Total life expectancy by age and \
1.128 brouard 6137: health expectancies in states (1) and (2). If popbased=1 the smooth (due to the model) \
6138: true period expectancies (those weighted with period prevalences are also\
6139: drawn in addition to the population based expectancies computed using\
1.218 brouard 6140: observed and cahotic prevalences: <a href=\"%s_%d.svg\">%s_%d.svg</a>\n<br>\
1.205 brouard 6141: <img src=\"%s_%d.svg\">",subdirf2(optionfilefiname,"E_"),jj1,subdirf2(optionfilefiname,"E_"),jj1,subdirf2(optionfilefiname,"E_"),jj1);
1.222 brouard 6142: /* } /\* end i1 *\/ */
6143: }/* End k1 */
6144: fprintf(fichtm,"</ul>");
6145: fflush(fichtm);
1.126 brouard 6146: }
6147:
6148: /******************* Gnuplot file **************/
1.223 brouard 6149: void printinggnuplot(char fileresu[], char optionfilefiname[], double ageminpar, double agemaxpar, double fage , int prevfcast, int backcast, char pathc[], double p[]){
1.126 brouard 6150:
6151: char dirfileres[132],optfileres[132];
1.223 brouard 6152: char gplotcondition[132];
1.164 brouard 6153: int cpt=0,k1=0,i=0,k=0,j=0,jk=0,k2=0,k3=0,ij=0,l=0;
1.211 brouard 6154: int lv=0, vlv=0, kl=0;
1.130 brouard 6155: int ng=0;
1.201 brouard 6156: int vpopbased;
1.223 brouard 6157: int ioffset; /* variable offset for columns */
1.219 brouard 6158:
1.126 brouard 6159: /* if((ficgp=fopen(optionfilegnuplot,"a"))==NULL) { */
6160: /* printf("Problem with file %s",optionfilegnuplot); */
6161: /* fprintf(ficlog,"Problem with file %s",optionfilegnuplot); */
6162: /* } */
6163:
6164: /*#ifdef windows */
6165: fprintf(ficgp,"cd \"%s\" \n",pathc);
1.223 brouard 6166: /*#endif */
1.225 brouard 6167: m=pow(2,cptcoveff);
1.126 brouard 6168:
1.202 brouard 6169: /* Contribution to likelihood */
6170: /* Plot the probability implied in the likelihood */
1.223 brouard 6171: fprintf(ficgp,"\n# Contributions to the Likelihood, mle >=1. For mle=4 no interpolation, pure matrix products.\n#\n");
6172: fprintf(ficgp,"\n set log y; unset log x;set xlabel \"Age\"; set ylabel \"Likelihood (-2Log(L))\";");
6173: /* fprintf(ficgp,"\nset ter svg size 640, 480"); */ /* Too big for svg */
6174: fprintf(ficgp,"\nset ter pngcairo size 640, 480");
1.204 brouard 6175: /* nice for mle=4 plot by number of matrix products.
1.202 brouard 6176: replot "rrtest1/toto.txt" u 2:($4 == 1 && $5==2 ? $9 : 1/0):5 t "p12" with point lc 1 */
6177: /* replot exp(p1+p2*x)/(1+exp(p1+p2*x)+exp(p3+p4*x)+exp(p5+p6*x)) t "p12(x)" */
1.223 brouard 6178: /* fprintf(ficgp,"\nset out \"%s.svg\";",subdirf2(optionfilefiname,"ILK_")); */
6179: fprintf(ficgp,"\nset out \"%s-dest.png\";",subdirf2(optionfilefiname,"ILK_"));
6180: 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));
6181: fprintf(ficgp,"\nset out \"%s-ori.png\";",subdirf2(optionfilefiname,"ILK_"));
6182: 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));
6183: for (i=1; i<= nlstate ; i ++) {
6184: fprintf(ficgp,"\nset out \"%s-p%dj.png\";set ylabel \"Probability for each individual/wave\";",subdirf2(optionfilefiname,"ILK_"),i);
6185: fprintf(ficgp,"unset log;\n# plot weighted, mean weight should have point size of 0.5\n plot \"%s\"",subdirf(fileresilk));
6186: 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);
6187: for (j=2; j<= nlstate+ndeath ; j ++) {
6188: 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);
6189: }
6190: fprintf(ficgp,";\nset out; unset ylabel;\n");
6191: }
6192: /* 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 */
6193: /* fprintf(ficgp,"\nset log y;plot \"%s\" u 2:(-$11):3 t \"All sample, all transitions\" with dots lc variable",subdirf(fileresilk)); */
6194: /* fprintf(ficgp,"\nreplot \"%s\" u 2:($3 <= 3 ? -$11 : 1/0):3 t \"First 3 individuals\" with line lc variable", subdirf(fileresilk)); */
6195: fprintf(ficgp,"\nset out;unset log\n");
6196: /* fprintf(ficgp,"\nset out \"%s.svg\"; replot; set out; # bug gnuplot",subdirf2(optionfilefiname,"ILK_")); */
1.202 brouard 6197:
1.126 brouard 6198: strcpy(dirfileres,optionfilefiname);
6199: strcpy(optfileres,"vpl");
1.223 brouard 6200: /* 1eme*/
1.211 brouard 6201: for (cpt=1; cpt<= nlstate ; cpt ++) { /* For each live state */
1.230 brouard 6202: for (k1=1; k1<= m && selected(k1) ; k1 ++) { /* For each valid combination of covariate */
1.211 brouard 6203: /* plot [100000000000000000000:-100000000000000000000] "mysbiaspar/vplrmysbiaspar.txt to check */
6204: fprintf(ficgp,"\n# 1st: Period (stable) prevalence with CI: 'VPL_' files ");
1.225 brouard 6205: for (k=1; k<=cptcoveff; k++){ /* For each covariate k get corresponding value lv for combination k1 */
6206: lv= decodtabm(k1,k,cptcoveff); /* Should be the value of the covariate corresponding to k1 combination */
1.223 brouard 6207: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
6208: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
6209: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
6210: vlv= nbcode[Tvaraff[k]][lv]; /* vlv is the value of the covariate lv, 0 or 1 */
6211: /* For each combination of covariate k1 (V1=1, V3=0), we printed the current covariate k and its value vlv */
6212: fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv);
1.211 brouard 6213: }
6214: fprintf(ficgp,"\n#\n");
1.223 brouard 6215: if(invalidvarcomb[k1]){
6216: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
6217: continue;
6218: }
1.211 brouard 6219:
1.223 brouard 6220: fprintf(ficgp,"\nset out \"%s_%d-%d.svg\" \n",subdirf2(optionfilefiname,"V_"),cpt,k1);
6221: fprintf(ficgp,"\n#set out \"V_%s_%d-%d.svg\" \n",optionfilefiname,cpt,k1);
6222: fprintf(ficgp,"set xlabel \"Age\" \n\
1.219 brouard 6223: set ylabel \"Probability\" \n \
6224: set ter svg size 640, 480\n \
1.201 brouard 6225: plot [%.f:%.f] \"%s\" every :::%d::%d u 1:2 \"%%lf",ageminpar,fage,subdirf2(fileresu,"VPL_"),k1-1,k1-1);
1.219 brouard 6226:
1.223 brouard 6227: for (i=1; i<= nlstate ; i ++) {
6228: if (i==cpt) fprintf(ficgp," %%lf (%%lf)");
6229: else fprintf(ficgp," %%*lf (%%*lf)");
6230: }
6231: fprintf(ficgp,"\" t\"Period (stable) prevalence\" w l lt 0,\"%s\" every :::%d::%d u 1:($2+1.96*$3) \"%%lf",subdirf2(fileresu,"VPL_"),k1-1,k1-1);
6232: for (i=1; i<= nlstate ; i ++) {
6233: if (i==cpt) fprintf(ficgp," %%lf (%%lf)");
6234: else fprintf(ficgp," %%*lf (%%*lf)");
6235: }
6236: fprintf(ficgp,"\" t\"95%% CI\" w l lt 1,\"%s\" every :::%d::%d u 1:($2-1.96*$3) \"%%lf",subdirf2(fileresu,"VPL_"),k1-1,k1-1);
6237: for (i=1; i<= nlstate ; i ++) {
6238: if (i==cpt) fprintf(ficgp," %%lf (%%lf)");
6239: else fprintf(ficgp," %%*lf (%%*lf)");
6240: }
6241: 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));
6242: if(backcast==1){ /* We need to get the corresponding values of the covariates involved in this combination k1 */
6243: /* 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); */
6244: fprintf(ficgp,",\"%s\" u 1:((",subdirf2(fileresu,"PLB_")); /* Age is in 1 */
1.225 brouard 6245: if(cptcoveff ==0){
1.223 brouard 6246: fprintf(ficgp,"$%d)) t 'Backward prevalence in state %d' with line ", 2+(cpt-1), cpt );
6247: }else{
6248: kl=0;
1.225 brouard 6249: for (k=1; k<=cptcoveff; k++){ /* For each combination of covariate */
6250: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate value corresponding to k1 combination and kth covariate */
1.223 brouard 6251: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
6252: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
6253: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
6254: vlv= nbcode[Tvaraff[k]][lv];
6255: kl++;
6256: /* 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 *\/ */
6257: /*6+(cpt-1)*(nlstate+1)+1+(i-1)+(nlstate+1)*nlstate; 6+(1-1)*(2+1)+1+(1-1) +(2+1)*2=13 */
6258: /*6+1+(i-1)+(nlstate+1)*nlstate; 6+1+(1-1) +(2+1)*2=13 */
6259: /* '' u 6:(($1==1 && $2==0 && $3==2 && $4==0)? $9/(1.-$15) : 1/0):($5==2000? 3:2) t 'p.1' with line lc variable*/
1.225 brouard 6260: if(k==cptcoveff){
1.227 brouard 6261: 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], \
6262: 4+(cpt-1), cpt ); /* 4 or 6 ?*/
1.223 brouard 6263: }else{
6264: fprintf(ficgp,"$%d==%d && $%d==%d && ",kl+1, Tvaraff[k],kl+1+1,nbcode[Tvaraff[k]][lv]);
6265: kl++;
6266: }
6267: } /* end covariate */
6268: } /* end if no covariate */
6269: } /* end if backcast */
6270: fprintf(ficgp,"\nset out \n");
1.201 brouard 6271: } /* k1 */
6272: } /* cpt */
1.126 brouard 6273: /*2 eme*/
6274: for (k1=1; k1<= m ; k1 ++) {
1.220 brouard 6275:
1.223 brouard 6276: fprintf(ficgp,"\n# 2nd: Total life expectancy with CI: 't' files ");
1.225 brouard 6277: for (k=1; k<=cptcoveff; k++){ /* For each covariate and each value */
6278: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate number corresponding to k1 combination */
1.223 brouard 6279: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
6280: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
6281: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
6282: vlv= nbcode[Tvaraff[k]][lv];
6283: fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv);
6284: }
6285: fprintf(ficgp,"\n#\n");
6286: if(invalidvarcomb[k1]){
6287: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
6288: continue;
6289: }
1.219 brouard 6290:
1.223 brouard 6291: fprintf(ficgp,"\nset out \"%s_%d.svg\" \n",subdirf2(optionfilefiname,"E_"),k1);
6292: for(vpopbased=0; vpopbased <= popbased; vpopbased++){ /* Done for vpopbased=0 and vpopbased=1 if popbased==1*/
6293: if(vpopbased==0)
6294: fprintf(ficgp,"set ylabel \"Years\" \nset ter svg size 640, 480\nplot [%.f:%.f] ",ageminpar,fage);
6295: else
6296: fprintf(ficgp,"\nreplot ");
6297: for (i=1; i<= nlstate+1 ; i ++) {
6298: k=2*i;
6299: 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);
6300: for (j=1; j<= nlstate+1 ; j ++) {
6301: if (j==i) fprintf(ficgp," %%lf (%%lf)");
6302: else fprintf(ficgp," %%*lf (%%*lf)");
6303: }
6304: if (i== 1) fprintf(ficgp,"\" t\"TLE\" w l lt %d, \\\n",i);
6305: else fprintf(ficgp,"\" t\"LE in state (%d)\" w l lt %d, \\\n",i-1,i+1);
6306: 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);
6307: for (j=1; j<= nlstate+1 ; j ++) {
6308: if (j==i) fprintf(ficgp," %%lf (%%lf)");
6309: else fprintf(ficgp," %%*lf (%%*lf)");
6310: }
6311: fprintf(ficgp,"\" t\"\" w l lt 0,");
6312: 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);
6313: for (j=1; j<= nlstate+1 ; j ++) {
6314: if (j==i) fprintf(ficgp," %%lf (%%lf)");
6315: else fprintf(ficgp," %%*lf (%%*lf)");
6316: }
6317: if (i== (nlstate+1)) fprintf(ficgp,"\" t\"\" w l lt 0");
6318: else fprintf(ficgp,"\" t\"\" w l lt 0,\\\n");
6319: } /* state */
6320: } /* vpopbased */
6321: fprintf(ficgp,"\nset out;set out \"%s_%d.svg\"; replot; set out; \n",subdirf2(optionfilefiname,"E_"),k1); /* Buggy gnuplot */
1.201 brouard 6322: } /* k1 */
1.219 brouard 6323:
6324:
1.126 brouard 6325: /*3eme*/
6326: for (k1=1; k1<= m ; k1 ++) {
1.220 brouard 6327:
1.126 brouard 6328: for (cpt=1; cpt<= nlstate ; cpt ++) {
1.211 brouard 6329: fprintf(ficgp,"\n# 3d: Life expectancy with EXP_ files: cov=%d state=%d",k1, cpt);
1.225 brouard 6330: for (k=1; k<=cptcoveff; k++){ /* For each covariate and each value */
6331: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate number corresponding to k1 combination */
1.223 brouard 6332: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
6333: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
6334: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
6335: vlv= nbcode[Tvaraff[k]][lv];
6336: fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv);
1.211 brouard 6337: }
6338: fprintf(ficgp,"\n#\n");
1.223 brouard 6339: if(invalidvarcomb[k1]){
6340: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
6341: continue;
6342: }
1.219 brouard 6343:
1.126 brouard 6344: /* k=2+nlstate*(2*cpt-2); */
6345: k=2+(nlstate+1)*(cpt-1);
1.201 brouard 6346: fprintf(ficgp,"\nset out \"%s_%d%d.svg\" \n",subdirf2(optionfilefiname,"EXP_"),cpt,k1);
1.199 brouard 6347: fprintf(ficgp,"set ter svg size 640, 480\n\
1.201 brouard 6348: plot [%.f:%.f] \"%s\" every :::%d::%d u 1:%d t \"e%d1\" w l",ageminpar,fage,subdirf2(fileresu,"E_"),k1-1,k1-1,k,cpt);
1.126 brouard 6349: /*fprintf(ficgp,",\"e%s\" every :::%d::%d u 1:($%d-2*$%d) \"\%%lf ",fileres,k1-1,k1-1,k,k+1);
1.223 brouard 6350: for (i=1; i<= nlstate*2 ; i ++) fprintf(ficgp,"\%%lf (\%%lf) ");
6351: fprintf(ficgp,"\" t \"e%d1\" w l",cpt);
6352: fprintf(ficgp,",\"e%s\" every :::%d::%d u 1:($%d+2*$%d) \"\%%lf ",fileres,k1-1,k1-1,k,k+1);
6353: for (i=1; i<= nlstate*2 ; i ++) fprintf(ficgp,"\%%lf (\%%lf) ");
6354: fprintf(ficgp,"\" t \"e%d1\" w l",cpt);
1.219 brouard 6355:
1.126 brouard 6356: */
6357: for (i=1; i< nlstate ; i ++) {
1.223 brouard 6358: 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);
6359: /* 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 6360:
1.126 brouard 6361: }
1.201 brouard 6362: fprintf(ficgp," ,\"%s\" every :::%d::%d u 1:%d t \"e%d.\" w l",subdirf2(fileresu,"E_"),k1-1,k1-1,k+nlstate,cpt);
1.126 brouard 6363: }
6364: }
6365:
1.223 brouard 6366: /* 4eme */
1.201 brouard 6367: /* Survival functions (period) from state i in state j by initial state i */
6368: for (k1=1; k1<= m ; k1 ++) { /* For each multivariate if any */
1.220 brouard 6369:
1.201 brouard 6370: for (cpt=1; cpt<=nlstate ; cpt ++) { /* For each life state */
1.211 brouard 6371: fprintf(ficgp,"\n#\n#\n# Survival functions in state j : 'LIJ_' files, cov=%d state=%d",k1, cpt);
1.225 brouard 6372: for (k=1; k<=cptcoveff; k++){ /* For each covariate and each value */
6373: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate number corresponding to k1 combination */
1.223 brouard 6374: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
6375: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
6376: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
6377: vlv= nbcode[Tvaraff[k]][lv];
6378: fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv);
1.211 brouard 6379: }
6380: fprintf(ficgp,"\n#\n");
1.223 brouard 6381: if(invalidvarcomb[k1]){
6382: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
6383: continue;
6384: }
1.220 brouard 6385:
1.201 brouard 6386: fprintf(ficgp,"\nset out \"%s_%d-%d.svg\" \n",subdirf2(optionfilefiname,"LIJ_"),cpt,k1);
6387: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability to be alive\" \n\
1.220 brouard 6388: set ter svg size 640, 480\n \
6389: unset log y\n \
1.201 brouard 6390: plot [%.f:%.f] ", ageminpar, agemaxpar);
1.211 brouard 6391: k=3;
1.201 brouard 6392: for (i=1; i<= nlstate ; i ++){
1.223 brouard 6393: if(i==1){
6394: fprintf(ficgp,"\"%s\"",subdirf2(fileresu,"PIJ_"));
6395: }else{
6396: fprintf(ficgp,", '' ");
6397: }
6398: l=(nlstate+ndeath)*(i-1)+1;
6399: fprintf(ficgp," u ($1==%d ? ($3):1/0):($%d/($%d",k1,k+l+(cpt-1),k+l);
6400: for (j=2; j<= nlstate+ndeath ; j ++)
6401: fprintf(ficgp,"+$%d",k+l+j-1);
6402: fprintf(ficgp,")) t \"l(%d,%d)\" w l",i,cpt);
1.201 brouard 6403: } /* nlstate */
6404: fprintf(ficgp,"\nset out\n");
6405: } /* end cpt state*/
6406: } /* end covariate */
1.220 brouard 6407:
6408: /* 5eme */
1.201 brouard 6409: /* Survival functions (period) from state i in state j by final state j */
1.202 brouard 6410: for (k1=1; k1<= m ; k1 ++) { /* For each covariate if any */
1.201 brouard 6411: for (cpt=1; cpt<=nlstate ; cpt ++) { /* For each inital state */
1.223 brouard 6412:
1.201 brouard 6413: fprintf(ficgp,"\n#\n#\n# Survival functions in state j and all livestates from state i by final state j: 'lij' files, cov=%d state=%d",k1, cpt);
1.225 brouard 6414: for (k=1; k<=cptcoveff; k++){ /* For each covariate and each value */
1.227 brouard 6415: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate number corresponding to k1 combination */
6416: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
6417: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
6418: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
6419: vlv= nbcode[Tvaraff[k]][lv];
6420: fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv);
1.211 brouard 6421: }
6422: fprintf(ficgp,"\n#\n");
1.223 brouard 6423: if(invalidvarcomb[k1]){
1.227 brouard 6424: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
6425: continue;
1.223 brouard 6426: }
1.227 brouard 6427:
1.201 brouard 6428: fprintf(ficgp,"\nset out \"%s_%d-%d.svg\" \n",subdirf2(optionfilefiname,"LIJT_"),cpt,k1);
6429: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability to be alive\" \n\
1.227 brouard 6430: set ter svg size 640, 480\n \
6431: unset log y\n \
1.201 brouard 6432: plot [%.f:%.f] ", ageminpar, agemaxpar);
1.211 brouard 6433: k=3;
1.201 brouard 6434: for (j=1; j<= nlstate ; j ++){ /* Lived in state j */
1.227 brouard 6435: if(j==1)
6436: fprintf(ficgp,"\"%s\"",subdirf2(fileresu,"PIJ_"));
6437: else
6438: fprintf(ficgp,", '' ");
6439: l=(nlstate+ndeath)*(cpt-1) +j;
6440: fprintf(ficgp," u (($1==%d && (floor($2)%%5 == 0)) ? ($3):1/0):($%d",k1,k+l);
6441: /* for (i=2; i<= nlstate+ndeath ; i ++) */
6442: /* fprintf(ficgp,"+$%d",k+l+i-1); */
6443: fprintf(ficgp,") t \"l(%d,%d)\" w l",cpt,j);
1.201 brouard 6444: } /* nlstate */
6445: fprintf(ficgp,", '' ");
6446: fprintf(ficgp," u (($1==%d && (floor($2)%%5 == 0)) ? ($3):1/0):(",k1);
6447: for (j=1; j<= nlstate ; j ++){ /* Lived in state j */
1.227 brouard 6448: l=(nlstate+ndeath)*(cpt-1) +j;
6449: if(j < nlstate)
6450: fprintf(ficgp,"$%d +",k+l);
6451: else
6452: fprintf(ficgp,"$%d) t\"l(%d,.)\" w l",k+l,cpt);
1.201 brouard 6453: }
6454: fprintf(ficgp,"\nset out\n");
6455: } /* end cpt state*/
6456: } /* end covariate */
1.227 brouard 6457:
1.220 brouard 6458: /* 6eme */
1.202 brouard 6459: /* CV preval stable (period) for each covariate */
1.211 brouard 6460: for (k1=1; k1<= m ; k1 ++) { /* For each covariate combination (1 to m=2**k), if any covariate is present */
1.153 brouard 6461: for (cpt=1; cpt<=nlstate ; cpt ++) { /* For each life state */
1.227 brouard 6462:
1.211 brouard 6463: fprintf(ficgp,"\n#\n#\n#CV preval stable (period): 'pij' files, covariatecombination#=%d state=%d",k1, cpt);
1.225 brouard 6464: for (k=1; k<=cptcoveff; k++){ /* For each covariate and each value */
1.227 brouard 6465: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate number corresponding to k1 combination */
6466: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
6467: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
6468: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
6469: vlv= nbcode[Tvaraff[k]][lv];
6470: fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv);
1.211 brouard 6471: }
6472: fprintf(ficgp,"\n#\n");
1.223 brouard 6473: if(invalidvarcomb[k1]){
1.227 brouard 6474: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
6475: continue;
1.223 brouard 6476: }
1.227 brouard 6477:
1.201 brouard 6478: fprintf(ficgp,"\nset out \"%s_%d-%d.svg\" \n",subdirf2(optionfilefiname,"P_"),cpt,k1);
1.126 brouard 6479: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability\" \n\
1.227 brouard 6480: set ter svg size 640, 480\n \
6481: unset log y\n \
1.153 brouard 6482: plot [%.f:%.f] ", ageminpar, agemaxpar);
1.211 brouard 6483: k=3; /* Offset */
1.153 brouard 6484: for (i=1; i<= nlstate ; i ++){
1.227 brouard 6485: if(i==1)
6486: fprintf(ficgp,"\"%s\"",subdirf2(fileresu,"PIJ_"));
6487: else
6488: fprintf(ficgp,", '' ");
6489: l=(nlstate+ndeath)*(i-1)+1;
6490: fprintf(ficgp," u ($1==%d ? ($3):1/0):($%d/($%d",k1,k+l+(cpt-1),k+l);
6491: for (j=2; j<= nlstate ; j ++)
6492: fprintf(ficgp,"+$%d",k+l+j-1);
6493: fprintf(ficgp,")) t \"prev(%d,%d)\" w l",i,cpt);
1.153 brouard 6494: } /* nlstate */
1.201 brouard 6495: fprintf(ficgp,"\nset out\n");
1.153 brouard 6496: } /* end cpt state*/
6497: } /* end covariate */
1.227 brouard 6498:
6499:
1.220 brouard 6500: /* 7eme */
1.218 brouard 6501: if(backcast == 1){
1.217 brouard 6502: /* CV back preval stable (period) for each covariate */
1.218 brouard 6503: for (k1=1; k1<= m ; k1 ++) { /* For each covariate combination (1 to m=2**k), if any covariate is present */
6504: for (cpt=1; cpt<=nlstate ; cpt ++) { /* For each life state */
1.227 brouard 6505: fprintf(ficgp,"\n#\n#\n#CV Back preval stable (period): 'pij' files, covariatecombination#=%d state=%d",k1, cpt);
6506: for (k=1; k<=cptcoveff; k++){ /* For each covariate and each value */
6507: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate number corresponding to k1 combination */
6508: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
6509: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
1.223 brouard 6510: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
1.227 brouard 6511: vlv= nbcode[Tvaraff[k]][lv];
6512: fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv);
6513: }
6514: fprintf(ficgp,"\n#\n");
6515: if(invalidvarcomb[k1]){
6516: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
6517: continue;
6518: }
6519:
6520: fprintf(ficgp,"\nset out \"%s_%d-%d.svg\" \n",subdirf2(optionfilefiname,"PB_"),cpt,k1);
6521: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability\" \n\
6522: set ter svg size 640, 480\n \
6523: unset log y\n \
1.218 brouard 6524: plot [%.f:%.f] ", ageminpar, agemaxpar);
1.227 brouard 6525: k=3; /* Offset */
6526: for (i=1; i<= nlstate ; i ++){
6527: if(i==1)
6528: fprintf(ficgp,"\"%s\"",subdirf2(fileresu,"PIJB_"));
6529: else
6530: fprintf(ficgp,", '' ");
6531: /* l=(nlstate+ndeath)*(i-1)+1; */
6532: l=(nlstate+ndeath)*(cpt-1)+1;
6533: /* fprintf(ficgp," u ($1==%d ? ($3):1/0):($%d/($%d",k1,k+l+(cpt-1),k+l); /\* a vérifier *\/ */
6534: /* fprintf(ficgp," u ($1==%d ? ($3):1/0):($%d/($%d",k1,k+l+(cpt-1),k+l+(cpt-1)+i-1); /\* a vérifier *\/ */
6535: fprintf(ficgp," u ($1==%d ? ($3):1/0):($%d",k1,k+l+(cpt-1)+i-1); /* a vérifier */
6536: /* for (j=2; j<= nlstate ; j ++) */
6537: /* fprintf(ficgp,"+$%d",k+l+j-1); */
6538: /* /\* fprintf(ficgp,"+$%d",k+l+j-1); *\/ */
6539: fprintf(ficgp,") t \"bprev(%d,%d)\" w l",i,cpt);
6540: } /* nlstate */
6541: fprintf(ficgp,"\nset out\n");
1.218 brouard 6542: } /* end cpt state*/
6543: } /* end covariate */
6544: } /* End if backcast */
6545:
1.223 brouard 6546: /* 8eme */
1.218 brouard 6547: if(prevfcast==1){
6548: /* Projection from cross-sectional to stable (period) for each covariate */
6549:
6550: for (k1=1; k1<= m ; k1 ++) { /* For each covariate combination (1 to m=2**k), if any covariate is present */
1.211 brouard 6551: for (cpt=1; cpt<=nlstate ; cpt ++) { /* For each life state */
1.227 brouard 6552: fprintf(ficgp,"\n#\n#\n#Projection of prevalence to stable (period): 'PROJ_' files, covariatecombination#=%d state=%d",k1, cpt);
6553: for (k=1; k<=cptcoveff; k++){ /* For each correspondig covariate value */
6554: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate value corresponding to k1 combination and kth covariate */
6555: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
6556: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
6557: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
6558: vlv= nbcode[Tvaraff[k]][lv];
6559: fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv);
6560: }
6561: fprintf(ficgp,"\n#\n");
6562: if(invalidvarcomb[k1]){
6563: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
6564: continue;
6565: }
6566:
6567: fprintf(ficgp,"# hpijx=probability over h years, hp.jx is weighted by observed prev\n ");
6568: fprintf(ficgp,"\nset out \"%s_%d-%d.svg\" \n",subdirf2(optionfilefiname,"PROJ_"),cpt,k1);
6569: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Prevalence\" \n\
6570: set ter svg size 640, 480\n \
6571: unset log y\n \
1.219 brouard 6572: plot [%.f:%.f] ", ageminpar, agemaxpar);
1.227 brouard 6573: for (i=1; i<= nlstate+1 ; i ++){ /* nlstate +1 p11 p21 p.1 */
6574: /*# V1 = 1 V2 = 0 yearproj age p11 p21 p.1 p12 p22 p.2 p13 p23 p.3*/
6575: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
6576: /*# yearproj age p11 p21 p.1 p12 p22 p.2 p13 p23 p.3*/
6577: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
6578: if(i==1){
6579: fprintf(ficgp,"\"%s\"",subdirf2(fileresu,"F_"));
6580: }else{
6581: fprintf(ficgp,",\\\n '' ");
6582: }
6583: if(cptcoveff ==0){ /* No covariate */
6584: ioffset=2; /* Age is in 2 */
6585: /*# yearproj age p11 p21 p31 p.1 p12 p22 p32 p.2 p13 p23 p33 p.3 p14 p24 p34 p.4*/
6586: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 */
6587: /*# V1 = 1 yearproj age p11 p21 p31 p.1 p12 p22 p32 p.2 p13 p23 p33 p.3 p14 p24 p34 p.4*/
6588: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 */
6589: fprintf(ficgp," u %d:(", ioffset);
6590: if(i==nlstate+1)
6591: fprintf(ficgp," $%d/(1.-$%d)) t 'pw.%d' with line ", \
6592: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset+1+(i-1)+(nlstate+1)*nlstate,cpt );
6593: else
6594: fprintf(ficgp," $%d/(1.-$%d)) t 'p%d%d' with line ", \
6595: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset+1+(i-1)+(nlstate+1)*nlstate,i,cpt );
6596: }else{ /* more than 2 covariates */
6597: if(cptcoveff ==1){
6598: ioffset=4; /* Age is in 4 */
6599: }else{
6600: ioffset=6; /* Age is in 6 */
6601: /*# V1 = 1 V2 = 0 yearproj age p11 p21 p.1 p12 p22 p.2 p13 p23 p.3*/
6602: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
6603: }
6604: fprintf(ficgp," u %d:(",ioffset);
6605: kl=0;
6606: strcpy(gplotcondition,"(");
6607: for (k=1; k<=cptcoveff; k++){ /* For each covariate writing the chain of conditions */
6608: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate value corresponding to combination k1 and covariate k */
6609: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
6610: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
6611: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
6612: vlv= nbcode[Tvaraff[k]][lv]; /* Value of the modality of Tvaraff[k] */
6613: kl++;
6614: sprintf(gplotcondition+strlen(gplotcondition),"$%d==%d && $%d==%d " ,kl,Tvaraff[k], kl+1, nbcode[Tvaraff[k]][lv]);
6615: kl++;
6616: if(k <cptcoveff && cptcoveff>1)
6617: sprintf(gplotcondition+strlen(gplotcondition)," && ");
6618: }
6619: strcpy(gplotcondition+strlen(gplotcondition),")");
6620: /* 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 *\/ */
6621: /*6+(cpt-1)*(nlstate+1)+1+(i-1)+(nlstate+1)*nlstate; 6+(1-1)*(2+1)+1+(1-1) +(2+1)*2=13 */
6622: /*6+1+(i-1)+(nlstate+1)*nlstate; 6+1+(1-1) +(2+1)*2=13 */
6623: /* '' 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*/
6624: if(i==nlstate+1){
6625: fprintf(ficgp,"%s ? $%d/(1.-$%d) : 1/0) t 'p.%d' with line ", gplotcondition, \
6626: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset+1+(i-1)+(nlstate+1)*nlstate,cpt );
6627: }else{
6628: fprintf(ficgp,"%s ? $%d/(1.-$%d) : 1/0) t 'p%d%d' with line ", gplotcondition, \
6629: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset +1+(i-1)+(nlstate+1)*nlstate,i,cpt );
6630: }
6631: } /* end if covariate */
6632: } /* nlstate */
6633: fprintf(ficgp,"\nset out\n");
1.223 brouard 6634: } /* end cpt state*/
6635: } /* end covariate */
6636: } /* End if prevfcast */
1.227 brouard 6637:
6638:
1.223 brouard 6639: /* proba elementaires */
6640: fprintf(ficgp,"\n##############\n#MLE estimated parameters\n#############\n");
1.126 brouard 6641: for(i=1,jk=1; i <=nlstate; i++){
1.187 brouard 6642: fprintf(ficgp,"# initial state %d\n",i);
1.126 brouard 6643: for(k=1; k <=(nlstate+ndeath); k++){
6644: if (k != i) {
1.227 brouard 6645: fprintf(ficgp,"# current state %d\n",k);
6646: for(j=1; j <=ncovmodel; j++){
6647: fprintf(ficgp,"p%d=%f; ",jk,p[jk]);
6648: jk++;
6649: }
6650: fprintf(ficgp,"\n");
1.126 brouard 6651: }
6652: }
1.223 brouard 6653: }
1.187 brouard 6654: fprintf(ficgp,"##############\n#\n");
1.227 brouard 6655:
1.145 brouard 6656: /*goto avoid;*/
1.200 brouard 6657: fprintf(ficgp,"\n##############\n#Graphics of probabilities or incidences\n#############\n");
1.187 brouard 6658: fprintf(ficgp,"# logi(p12/p11)=a12+b12*age+c12age*age+d12*V1+e12*V1*age\n");
6659: fprintf(ficgp,"# logi(p12/p11)=p1 +p2*age +p3*age*age+ p4*V1+ p5*V1*age\n");
6660: fprintf(ficgp,"# logi(p13/p11)=a13+b13*age+c13age*age+d13*V1+e13*V1*age\n");
6661: fprintf(ficgp,"# logi(p13/p11)=p6 +p7*age +p8*age*age+ p9*V1+ p10*V1*age\n");
6662: fprintf(ficgp,"# p12+p13+p14+p11=1=p11(1+exp(a12+b12*age+c12age*age+d12*V1+e12*V1*age)\n");
6663: fprintf(ficgp,"# +exp(a13+b13*age+c13age*age+d13*V1+e13*V1*age)+...)\n");
6664: fprintf(ficgp,"# p11=1/(1+exp(a12+b12*age+c12age*age+d12*V1+e12*V1*age)\n");
6665: fprintf(ficgp,"# +exp(a13+b13*age+c13age*age+d13*V1+e13*V1*age)+...)\n");
6666: fprintf(ficgp,"# p12=exp(a12+b12*age+c12age*age+d12*V1+e12*V1*age)/\n");
6667: fprintf(ficgp,"# (1+exp(a12+b12*age+c12age*age+d12*V1+e12*V1*age)\n");
6668: fprintf(ficgp,"# +exp(a13+b13*age+c13age*age+d13*V1+e13*V1*age))\n");
6669: fprintf(ficgp,"# +exp(a14+b14*age+c14age*age+d14*V1+e14*V1*age)+...)\n");
6670: fprintf(ficgp,"#\n");
1.223 brouard 6671: for(ng=1; ng<=3;ng++){ /* Number of graphics: first is logit, 2nd is probabilities, third is incidences per year*/
6672: fprintf(ficgp,"# ng=%d\n",ng);
1.225 brouard 6673: fprintf(ficgp,"# jk=1 to 2^%d=%d\n",cptcoveff,m);
1.223 brouard 6674: for(jk=1; jk <=m; jk++) {
6675: fprintf(ficgp,"# jk=%d\n",jk);
6676: fprintf(ficgp,"\nset out \"%s_%d-%d.svg\" ",subdirf2(optionfilefiname,"PE_"),jk,ng);
6677: fprintf(ficgp,"\nset ter svg size 640, 480 ");
6678: if (ng==1){
6679: fprintf(ficgp,"\nset ylabel \"Value of the logit of the model\"\n"); /* exp(a12+b12*x) could be nice */
6680: fprintf(ficgp,"\nunset log y");
6681: }else if (ng==2){
6682: fprintf(ficgp,"\nset ylabel \"Probability\"\n");
6683: fprintf(ficgp,"\nset log y");
6684: }else if (ng==3){
6685: fprintf(ficgp,"\nset ylabel \"Quasi-incidence per year\"\n");
6686: fprintf(ficgp,"\nset log y");
6687: }else
6688: fprintf(ficgp,"\nunset title ");
6689: fprintf(ficgp,"\nplot [%.f:%.f] ",ageminpar,agemaxpar);
6690: i=1;
6691: for(k2=1; k2<=nlstate; k2++) {
6692: k3=i;
6693: for(k=1; k<=(nlstate+ndeath); k++) {
6694: if (k != k2){
6695: switch( ng) {
6696: case 1:
6697: if(nagesqr==0)
6698: fprintf(ficgp," p%d+p%d*x",i,i+1);
6699: else /* nagesqr =1 */
6700: fprintf(ficgp," p%d+p%d*x+p%d*x*x",i,i+1,i+1+nagesqr);
6701: break;
6702: case 2: /* ng=2 */
6703: if(nagesqr==0)
6704: fprintf(ficgp," exp(p%d+p%d*x",i,i+1);
6705: else /* nagesqr =1 */
6706: fprintf(ficgp," exp(p%d+p%d*x+p%d*x*x",i,i+1,i+1+nagesqr);
6707: break;
6708: case 3:
6709: if(nagesqr==0)
6710: fprintf(ficgp," %f*exp(p%d+p%d*x",YEARM/stepm,i,i+1);
6711: else /* nagesqr =1 */
6712: fprintf(ficgp," %f*exp(p%d+p%d*x+p%d*x*x",YEARM/stepm,i,i+1,i+1+nagesqr);
6713: break;
6714: }
6715: ij=1;/* To be checked else nbcode[0][0] wrong */
6716: for(j=3; j <=ncovmodel-nagesqr; j++) {
6717: /* printf("Tage[%d]=%d, j=%d\n", ij, Tage[ij], j); */
6718: if(ij <=cptcovage) { /* Bug valgrind */
6719: if((j-2)==Tage[ij]) { /* Bug valgrind */
6720: fprintf(ficgp,"+p%d*%d*x",i+j+nagesqr-1,nbcode[Tvar[j-2]][codtabm(jk,j-2)]);
6721: /* fprintf(ficgp,"+p%d*%d*x",i+j+nagesqr-1,nbcode[Tvar[j-2]][codtabm(jk,Tvar[j-2])]); */
6722: ij++;
6723: }
6724: }
6725: else
1.227 brouard 6726: fprintf(ficgp,"+p%d*%d",i+j+nagesqr-1,nbcode[Tvar[j-2]][codtabm(jk,j-2)]); /* Valgrind bug nbcode */
1.223 brouard 6727: }
6728: }else{
6729: i=i-ncovmodel;
6730: if(ng !=1 ) /* For logit formula of log p11 is more difficult to get */
6731: fprintf(ficgp," (1.");
6732: }
1.227 brouard 6733:
1.223 brouard 6734: if(ng != 1){
6735: fprintf(ficgp,")/(1");
1.227 brouard 6736:
1.223 brouard 6737: for(k1=1; k1 <=nlstate; k1++){
6738: if(nagesqr==0)
6739: fprintf(ficgp,"+exp(p%d+p%d*x",k3+(k1-1)*ncovmodel,k3+(k1-1)*ncovmodel+1);
6740: else /* nagesqr =1 */
6741: 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 6742:
1.223 brouard 6743: ij=1;
6744: for(j=3; j <=ncovmodel-nagesqr; j++){
6745: if(ij <=cptcovage) { /* Bug valgrind */
6746: if((j-2)==Tage[ij]) { /* Bug valgrind */
6747: fprintf(ficgp,"+p%d*%d*x",k3+(k1-1)*ncovmodel+1+j-2+nagesqr,nbcode[Tvar[j-2]][codtabm(jk,j-2)]);
6748: /* fprintf(ficgp,"+p%d*%d*x",k3+(k1-1)*ncovmodel+1+j-2+nagesqr,nbcode[Tvar[j-2]][codtabm(jk,Tvar[j-2])]); */
6749: ij++;
6750: }
6751: }
6752: else
1.225 brouard 6753: 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 6754: }
6755: fprintf(ficgp,")");
6756: }
6757: fprintf(ficgp,")");
6758: if(ng ==2)
6759: fprintf(ficgp," t \"p%d%d\" ", k2,k);
6760: else /* ng= 3 */
6761: fprintf(ficgp," t \"i%d%d\" ", k2,k);
6762: }else{ /* end ng <> 1 */
6763: if( k !=k2) /* logit p11 is hard to draw */
6764: fprintf(ficgp," t \"logit(p%d%d)\" ", k2,k);
6765: }
6766: if ((k+k2)!= (nlstate*2+ndeath) && ng != 1)
6767: fprintf(ficgp,",");
6768: if (ng == 1 && k!=k2 && (k+k2)!= (nlstate*2+ndeath))
6769: fprintf(ficgp,",");
6770: i=i+ncovmodel;
6771: } /* end k */
6772: } /* end k2 */
6773: fprintf(ficgp,"\n set out\n");
6774: } /* end jk */
6775: } /* end ng */
6776: /* avoid: */
6777: fflush(ficgp);
1.126 brouard 6778: } /* end gnuplot */
6779:
6780:
6781: /*************** Moving average **************/
1.219 brouard 6782: /* int movingaverage(double ***probs, double bage, double fage, double ***mobaverage, int mobilav, double bageout, double fageout){ */
1.222 brouard 6783: int movingaverage(double ***probs, double bage, double fage, double ***mobaverage, int mobilav){
1.218 brouard 6784:
1.222 brouard 6785: int i, cpt, cptcod;
6786: int modcovmax =1;
6787: int mobilavrange, mob;
6788: int iage=0;
6789:
6790: double sum=0.;
6791: double age;
6792: double *sumnewp, *sumnewm;
6793: double *agemingood, *agemaxgood; /* Currently identical for all covariates */
6794:
6795:
1.225 brouard 6796: /* modcovmax=2*cptcoveff;/\* Max number of modalities. We suppose */
1.222 brouard 6797: /* a covariate has 2 modalities, should be equal to ncovcombmax *\/ */
6798:
6799: sumnewp = vector(1,ncovcombmax);
6800: sumnewm = vector(1,ncovcombmax);
6801: agemingood = vector(1,ncovcombmax);
6802: agemaxgood = vector(1,ncovcombmax);
6803:
6804: for (cptcod=1;cptcod<=ncovcombmax;cptcod++){
6805: sumnewm[cptcod]=0.;
6806: sumnewp[cptcod]=0.;
6807: agemingood[cptcod]=0;
6808: agemaxgood[cptcod]=0;
6809: }
6810: if (cptcovn<1) ncovcombmax=1; /* At least 1 pass */
6811:
6812: if(mobilav==1||mobilav ==3 ||mobilav==5 ||mobilav== 7){
6813: if(mobilav==1) mobilavrange=5; /* default */
6814: else mobilavrange=mobilav;
6815: for (age=bage; age<=fage; age++)
6816: for (i=1; i<=nlstate;i++)
6817: for (cptcod=1;cptcod<=ncovcombmax;cptcod++)
6818: mobaverage[(int)age][i][cptcod]=probs[(int)age][i][cptcod];
6819: /* We keep the original values on the extreme ages bage, fage and for
6820: fage+1 and bage-1 we use a 3 terms moving average; for fage+2 bage+2
6821: we use a 5 terms etc. until the borders are no more concerned.
6822: */
6823: for (mob=3;mob <=mobilavrange;mob=mob+2){
6824: for (age=bage+(mob-1)/2; age<=fage-(mob-1)/2; age++){
6825: for (i=1; i<=nlstate;i++){
6826: for (cptcod=1;cptcod<=ncovcombmax;cptcod++){
6827: mobaverage[(int)age][i][cptcod] =probs[(int)age][i][cptcod];
6828: for (cpt=1;cpt<=(mob-1)/2;cpt++){
6829: mobaverage[(int)age][i][cptcod] +=probs[(int)age-cpt][i][cptcod];
6830: mobaverage[(int)age][i][cptcod] +=probs[(int)age+cpt][i][cptcod];
6831: }
6832: mobaverage[(int)age][i][cptcod]=mobaverage[(int)age][i][cptcod]/mob;
6833: }
6834: }
6835: }/* end age */
6836: }/* end mob */
6837: }else
6838: return -1;
6839: for (cptcod=1;cptcod<=ncovcombmax;cptcod++){
6840: /* for (age=bage+(mob-1)/2; age<=fage-(mob-1)/2; age++){ */
6841: if(invalidvarcomb[cptcod]){
6842: printf("\nCombination (%d) ignored because no cases \n",cptcod);
6843: continue;
6844: }
1.219 brouard 6845:
1.222 brouard 6846: agemingood[cptcod]=fage-(mob-1)/2;
6847: for (age=fage-(mob-1)/2; age>=bage; age--){/* From oldest to youngest, finding the youngest wrong */
6848: sumnewm[cptcod]=0.;
6849: for (i=1; i<=nlstate;i++){
6850: sumnewm[cptcod]+=mobaverage[(int)age][i][cptcod];
6851: }
6852: if(fabs(sumnewm[cptcod] - 1.) <= 1.e-3) { /* good */
6853: agemingood[cptcod]=age;
6854: }else{ /* bad */
6855: for (i=1; i<=nlstate;i++){
6856: mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemingood[cptcod]][i][cptcod];
6857: } /* i */
6858: } /* end bad */
6859: }/* age */
6860: sum=0.;
6861: for (i=1; i<=nlstate;i++){
6862: sum+=mobaverage[(int)agemingood[cptcod]][i][cptcod];
6863: }
6864: if(fabs(sum - 1.) > 1.e-3) { /* bad */
6865: 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);
6866: /* for (i=1; i<=nlstate;i++){ */
6867: /* mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemingood[cptcod]][i][cptcod]; */
6868: /* } /\* i *\/ */
6869: } /* end bad */
6870: /* else{ /\* We found some ages summing to one, we will smooth the oldest *\/ */
6871: /* From youngest, finding the oldest wrong */
6872: agemaxgood[cptcod]=bage+(mob-1)/2;
6873: for (age=bage+(mob-1)/2; age<=fage; age++){
6874: sumnewm[cptcod]=0.;
6875: for (i=1; i<=nlstate;i++){
6876: sumnewm[cptcod]+=mobaverage[(int)age][i][cptcod];
6877: }
6878: if(fabs(sumnewm[cptcod] - 1.) <= 1.e-3) { /* good */
6879: agemaxgood[cptcod]=age;
6880: }else{ /* bad */
6881: for (i=1; i<=nlstate;i++){
6882: mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemaxgood[cptcod]][i][cptcod];
6883: } /* i */
6884: } /* end bad */
6885: }/* age */
6886: sum=0.;
6887: for (i=1; i<=nlstate;i++){
6888: sum+=mobaverage[(int)agemaxgood[cptcod]][i][cptcod];
6889: }
6890: if(fabs(sum - 1.) > 1.e-3) { /* bad */
6891: 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);
6892: /* for (i=1; i<=nlstate;i++){ */
6893: /* mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemingood[cptcod]][i][cptcod]; */
6894: /* } /\* i *\/ */
6895: } /* end bad */
6896:
6897: for (age=bage; age<=fage; age++){
6898: printf("%d %d ", cptcod, (int)age);
6899: sumnewp[cptcod]=0.;
6900: sumnewm[cptcod]=0.;
6901: for (i=1; i<=nlstate;i++){
6902: sumnewp[cptcod]+=probs[(int)age][i][cptcod];
6903: sumnewm[cptcod]+=mobaverage[(int)age][i][cptcod];
6904: /* printf("%.4f %.4f ",probs[(int)age][i][cptcod], mobaverage[(int)age][i][cptcod]); */
6905: }
6906: /* printf("%.4f %.4f \n",sumnewp[cptcod], sumnewm[cptcod]); */
6907: }
6908: /* printf("\n"); */
6909: /* } */
6910: /* brutal averaging */
6911: for (i=1; i<=nlstate;i++){
6912: for (age=1; age<=bage; age++){
6913: mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemingood[cptcod]][i][cptcod];
6914: /* printf("age=%d i=%d cptcod=%d mobaverage=%.4f \n",(int)age,i, cptcod, mobaverage[(int)age][i][cptcod]); */
6915: }
6916: for (age=fage; age<=AGESUP; age++){
6917: mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemaxgood[cptcod]][i][cptcod];
6918: /* printf("age=%d i=%d cptcod=%d mobaverage=%.4f \n",(int)age,i, cptcod, mobaverage[(int)age][i][cptcod]); */
6919: }
6920: } /* end i status */
6921: for (i=nlstate+1; i<=nlstate+ndeath;i++){
6922: for (age=1; age<=AGESUP; age++){
6923: /*printf("i=%d, age=%d, cptcod=%d\n",i, (int)age, cptcod);*/
6924: mobaverage[(int)age][i][cptcod]=0.;
6925: }
6926: }
6927: }/* end cptcod */
6928: free_vector(sumnewm,1, ncovcombmax);
6929: free_vector(sumnewp,1, ncovcombmax);
6930: free_vector(agemaxgood,1, ncovcombmax);
6931: free_vector(agemingood,1, ncovcombmax);
6932: return 0;
6933: }/* End movingaverage */
1.218 brouard 6934:
1.126 brouard 6935:
6936: /************** Forecasting ******************/
1.225 brouard 6937: 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 6938: /* proj1, year, month, day of starting projection
6939: agemin, agemax range of age
6940: dateprev1 dateprev2 range of dates during which prevalence is computed
6941: anproj2 year of en of projection (same day and month as proj1).
6942: */
1.164 brouard 6943: int yearp, stepsize, hstepm, nhstepm, j, k, cptcod, i, h, i1;
1.126 brouard 6944: double agec; /* generic age */
6945: double agelim, ppij, yp,yp1,yp2,jprojmean,mprojmean,anprojmean;
6946: double *popeffectif,*popcount;
6947: double ***p3mat;
1.218 brouard 6948: /* double ***mobaverage; */
1.126 brouard 6949: char fileresf[FILENAMELENGTH];
6950:
6951: agelim=AGESUP;
1.211 brouard 6952: /* Compute observed prevalence between dateprev1 and dateprev2 by counting the number of people
6953: in each health status at the date of interview (if between dateprev1 and dateprev2).
6954: We still use firstpass and lastpass as another selection.
6955: */
1.214 brouard 6956: /* freqsummary(fileres, agemin, agemax, s, agev, nlstate, imx,Tvaraff,nbcode, ncodemax,mint,anint,strstart,\ */
6957: /* firstpass, lastpass, stepm, weightopt, model); */
1.126 brouard 6958:
1.201 brouard 6959: strcpy(fileresf,"F_");
6960: strcat(fileresf,fileresu);
1.126 brouard 6961: if((ficresf=fopen(fileresf,"w"))==NULL) {
6962: printf("Problem with forecast resultfile: %s\n", fileresf);
6963: fprintf(ficlog,"Problem with forecast resultfile: %s\n", fileresf);
6964: }
1.215 brouard 6965: printf("Computing forecasting: result on file '%s', please wait... \n", fileresf);
6966: fprintf(ficlog,"Computing forecasting: result on file '%s', please wait... \n", fileresf);
1.126 brouard 6967:
1.225 brouard 6968: if (cptcoveff==0) ncodemax[cptcoveff]=1;
1.126 brouard 6969:
6970:
6971: stepsize=(int) (stepm+YEARM-1)/YEARM;
6972: if (stepm<=12) stepsize=1;
6973: if(estepm < stepm){
6974: printf ("Problem %d lower than %d\n",estepm, stepm);
6975: }
6976: else hstepm=estepm;
6977:
6978: hstepm=hstepm/stepm;
6979: yp1=modf(dateintmean,&yp);/* extracts integral of datemean in yp and
6980: fractional in yp1 */
6981: anprojmean=yp;
6982: yp2=modf((yp1*12),&yp);
6983: mprojmean=yp;
6984: yp1=modf((yp2*30.5),&yp);
6985: jprojmean=yp;
6986: if(jprojmean==0) jprojmean=1;
6987: if(mprojmean==0) jprojmean=1;
6988:
1.227 brouard 6989: i1=pow(2,cptcoveff);
1.126 brouard 6990: if (cptcovn < 1){i1=1;}
6991:
6992: fprintf(ficresf,"# Mean day of interviews %.lf/%.lf/%.lf (%.2f) between %.2f and %.2f \n",jprojmean,mprojmean,anprojmean,dateintmean,dateprev1,dateprev2);
6993:
6994: fprintf(ficresf,"#****** Routine prevforecast **\n");
1.227 brouard 6995:
1.126 brouard 6996: /* if (h==(int)(YEARM*yearp)){ */
1.227 brouard 6997: for(k=1;k<=i1;k++){
6998: if(invalidvarcomb[k]){
6999: printf("\nCombination (%d) projection ignored because no cases \n",k);
7000: continue;
7001: }
7002: fprintf(ficresf,"\n#****** hpijx=probability over h years, hp.jx is weighted by observed prev \n#");
7003: for(j=1;j<=cptcoveff;j++) {
7004: fprintf(ficresf," V%d (=) %d",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
7005: }
7006: fprintf(ficresf," yearproj age");
7007: for(j=1; j<=nlstate+ndeath;j++){
7008: for(i=1; i<=nlstate;i++)
7009: fprintf(ficresf," p%d%d",i,j);
7010: fprintf(ficresf," wp.%d",j);
7011: }
7012: for (yearp=0; yearp<=(anproj2-anproj1);yearp +=stepsize) {
7013: fprintf(ficresf,"\n");
7014: fprintf(ficresf,"\n# Forecasting at date %.lf/%.lf/%.lf ",jproj1,mproj1,anproj1+yearp);
7015: for (agec=fage; agec>=(ageminpar-1); agec--){
7016: nhstepm=(int) rint((agelim-agec)*YEARM/stepm);
7017: nhstepm = nhstepm/hstepm;
7018: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
7019: oldm=oldms;savm=savms;
7020: hpxij(p3mat,nhstepm,agec,hstepm,p,nlstate,stepm,oldm,savm, k);
7021:
7022: for (h=0; h<=nhstepm; h++){
7023: if (h*hstepm/YEARM*stepm ==yearp) {
7024: fprintf(ficresf,"\n");
7025: for(j=1;j<=cptcoveff;j++)
7026: fprintf(ficresf,"%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
7027: fprintf(ficresf,"%.f %.f ",anproj1+yearp,agec+h*hstepm/YEARM*stepm);
7028: }
7029: for(j=1; j<=nlstate+ndeath;j++) {
7030: ppij=0.;
7031: for(i=1; i<=nlstate;i++) {
7032: if (mobilav==1)
7033: ppij=ppij+p3mat[i][j][h]*mobaverage[(int)agec][i][k];
7034: else {
7035: ppij=ppij+p3mat[i][j][h]*probs[(int)(agec)][i][k];
7036: }
7037: if (h*hstepm/YEARM*stepm== yearp) {
7038: fprintf(ficresf," %.3f", p3mat[i][j][h]);
7039: }
7040: } /* end i */
7041: if (h*hstepm/YEARM*stepm==yearp) {
7042: fprintf(ficresf," %.3f", ppij);
7043: }
7044: }/* end j */
7045: } /* end h */
7046: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
7047: } /* end agec */
7048: } /* end yearp */
7049: } /* end k */
1.219 brouard 7050:
1.126 brouard 7051: fclose(ficresf);
1.215 brouard 7052: printf("End of Computing forecasting \n");
7053: fprintf(ficlog,"End of Computing forecasting\n");
7054:
1.126 brouard 7055: }
7056:
1.218 brouard 7057: /* /\************** Back Forecasting ******************\/ */
1.225 brouard 7058: /* 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 7059: /* /\* back1, year, month, day of starting backection */
7060: /* agemin, agemax range of age */
7061: /* dateprev1 dateprev2 range of dates during which prevalence is computed */
7062: /* anback2 year of en of backection (same day and month as back1). */
7063: /* *\/ */
7064: /* int yearp, stepsize, hstepm, nhstepm, j, k, cptcod, i, h, i1; */
7065: /* double agec; /\* generic age *\/ */
7066: /* double agelim, ppij, yp,yp1,yp2,jprojmean,mprojmean,anprojmean; */
7067: /* double *popeffectif,*popcount; */
7068: /* double ***p3mat; */
7069: /* /\* double ***mobaverage; *\/ */
7070: /* char fileresfb[FILENAMELENGTH]; */
7071:
7072: /* agelim=AGESUP; */
7073: /* /\* Compute observed prevalence between dateprev1 and dateprev2 by counting the number of people */
7074: /* in each health status at the date of interview (if between dateprev1 and dateprev2). */
7075: /* We still use firstpass and lastpass as another selection. */
7076: /* *\/ */
7077: /* /\* freqsummary(fileres, agemin, agemax, s, agev, nlstate, imx,Tvaraff,nbcode, ncodemax,mint,anint,strstart,\ *\/ */
7078: /* /\* firstpass, lastpass, stepm, weightopt, model); *\/ */
7079: /* prevalence(probs, ageminpar, agemax, s, agev, nlstate, imx, Tvar, nbcode, ncodemax, mint, anint, dateprev1, dateprev2, firstpass, lastpass); */
7080:
7081: /* strcpy(fileresfb,"FB_"); */
7082: /* strcat(fileresfb,fileresu); */
7083: /* if((ficresfb=fopen(fileresfb,"w"))==NULL) { */
7084: /* printf("Problem with back forecast resultfile: %s\n", fileresfb); */
7085: /* fprintf(ficlog,"Problem with back forecast resultfile: %s\n", fileresfb); */
7086: /* } */
7087: /* printf("Computing back forecasting: result on file '%s', please wait... \n", fileresfb); */
7088: /* fprintf(ficlog,"Computing back forecasting: result on file '%s', please wait... \n", fileresfb); */
7089:
1.225 brouard 7090: /* if (cptcoveff==0) ncodemax[cptcoveff]=1; */
1.218 brouard 7091:
7092: /* /\* if (mobilav!=0) { *\/ */
7093: /* /\* mobaverage= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX); *\/ */
7094: /* /\* if (movingaverage(probs, ageminpar, fage, mobaverage,mobilav)!=0){ *\/ */
7095: /* /\* fprintf(ficlog," Error in movingaverage mobilav=%d\n",mobilav); *\/ */
7096: /* /\* printf(" Error in movingaverage mobilav=%d\n",mobilav); *\/ */
7097: /* /\* } *\/ */
7098: /* /\* } *\/ */
7099:
7100: /* stepsize=(int) (stepm+YEARM-1)/YEARM; */
7101: /* if (stepm<=12) stepsize=1; */
7102: /* if(estepm < stepm){ */
7103: /* printf ("Problem %d lower than %d\n",estepm, stepm); */
7104: /* } */
7105: /* else hstepm=estepm; */
7106:
7107: /* hstepm=hstepm/stepm; */
7108: /* yp1=modf(dateintmean,&yp);/\* extracts integral of datemean in yp and */
7109: /* fractional in yp1 *\/ */
7110: /* anprojmean=yp; */
7111: /* yp2=modf((yp1*12),&yp); */
7112: /* mprojmean=yp; */
7113: /* yp1=modf((yp2*30.5),&yp); */
7114: /* jprojmean=yp; */
7115: /* if(jprojmean==0) jprojmean=1; */
7116: /* if(mprojmean==0) jprojmean=1; */
7117:
1.225 brouard 7118: /* i1=cptcoveff; */
1.218 brouard 7119: /* if (cptcovn < 1){i1=1;} */
1.217 brouard 7120:
1.218 brouard 7121: /* fprintf(ficresfb,"# Mean day of interviews %.lf/%.lf/%.lf (%.2f) between %.2f and %.2f \n",jprojmean,mprojmean,anprojmean,dateintmean,dateprev1,dateprev2); */
1.217 brouard 7122:
1.218 brouard 7123: /* fprintf(ficresfb,"#****** Routine prevbackforecast **\n"); */
7124:
7125: /* /\* if (h==(int)(YEARM*yearp)){ *\/ */
7126: /* for(cptcov=1, k=0;cptcov<=i1;cptcov++){ */
1.225 brouard 7127: /* for(cptcod=1;cptcod<=ncodemax[cptcoveff];cptcod++){ */
1.218 brouard 7128: /* k=k+1; */
7129: /* fprintf(ficresfb,"\n#****** hbijx=probability over h years, hp.jx is weighted by observed prev \n#"); */
1.225 brouard 7130: /* for(j=1;j<=cptcoveff;j++) { */
1.218 brouard 7131: /* fprintf(ficresfb," V%d (=) %d",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]); */
7132: /* } */
7133: /* fprintf(ficresfb," yearbproj age"); */
7134: /* for(j=1; j<=nlstate+ndeath;j++){ */
7135: /* for(i=1; i<=nlstate;i++) */
7136: /* fprintf(ficresfb," p%d%d",i,j); */
7137: /* fprintf(ficresfb," p.%d",j); */
7138: /* } */
7139: /* for (yearp=0; yearp>=(anback2-anback1);yearp -=stepsize) { */
7140: /* /\* for (yearp=0; yearp<=(anproj2-anproj1);yearp +=stepsize) { *\/ */
7141: /* fprintf(ficresfb,"\n"); */
7142: /* fprintf(ficresfb,"\n# Back Forecasting at date %.lf/%.lf/%.lf ",jback1,mback1,anback1+yearp); */
7143: /* for (agec=fage; agec>=(ageminpar-1); agec--){ */
7144: /* nhstepm=(int) rint((agelim-agec)*YEARM/stepm); */
7145: /* nhstepm = nhstepm/hstepm; */
7146: /* p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); */
7147: /* oldm=oldms;savm=savms; */
7148: /* hbxij(p3mat,nhstepm,agec,hstepm,p,prevacurrent,nlstate,stepm,oldm,savm,oldm,savm, dnewm, doldm, dsavm, k); */
7149: /* for (h=0; h<=nhstepm; h++){ */
7150: /* if (h*hstepm/YEARM*stepm ==yearp) { */
7151: /* fprintf(ficresfb,"\n"); */
1.225 brouard 7152: /* for(j=1;j<=cptcoveff;j++) */
1.218 brouard 7153: /* fprintf(ficresfb,"%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]); */
7154: /* fprintf(ficresfb,"%.f %.f ",anback1+yearp,agec+h*hstepm/YEARM*stepm); */
7155: /* } */
7156: /* for(j=1; j<=nlstate+ndeath;j++) { */
7157: /* ppij=0.; */
7158: /* for(i=1; i<=nlstate;i++) { */
7159: /* if (mobilav==1) */
7160: /* ppij=ppij+p3mat[i][j][h]*mobaverage[(int)agec][i][cptcod]; */
7161: /* else { */
7162: /* ppij=ppij+p3mat[i][j][h]*probs[(int)(agec)][i][cptcod]; */
7163: /* } */
7164: /* if (h*hstepm/YEARM*stepm== yearp) { */
7165: /* fprintf(ficresfb," %.3f", p3mat[i][j][h]); */
7166: /* } */
7167: /* } /\* end i *\/ */
7168: /* if (h*hstepm/YEARM*stepm==yearp) { */
7169: /* fprintf(ficresfb," %.3f", ppij); */
7170: /* } */
7171: /* }/\* end j *\/ */
7172: /* } /\* end h *\/ */
7173: /* free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); */
7174: /* } /\* end agec *\/ */
7175: /* } /\* end yearp *\/ */
7176: /* } /\* end cptcod *\/ */
7177: /* } /\* end cptcov *\/ */
7178:
7179: /* /\* if (mobilav!=0) free_ma3x(mobaverage,1, AGESUP,1,NCOVMAX, 1,NCOVMAX); *\/ */
7180:
7181: /* fclose(ficresfb); */
7182: /* printf("End of Computing Back forecasting \n"); */
7183: /* fprintf(ficlog,"End of Computing Back forecasting\n"); */
1.217 brouard 7184:
1.218 brouard 7185: /* } */
1.217 brouard 7186:
1.126 brouard 7187: /************** Forecasting *****not tested NB*************/
1.227 brouard 7188: /* 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 7189:
1.227 brouard 7190: /* int cpt, stepsize, hstepm, nhstepm, j,k,c, cptcod, i,h; */
7191: /* int *popage; */
7192: /* double calagedatem, agelim, kk1, kk2; */
7193: /* double *popeffectif,*popcount; */
7194: /* double ***p3mat,***tabpop,***tabpopprev; */
7195: /* /\* double ***mobaverage; *\/ */
7196: /* char filerespop[FILENAMELENGTH]; */
1.126 brouard 7197:
1.227 brouard 7198: /* tabpop= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
7199: /* tabpopprev= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
7200: /* agelim=AGESUP; */
7201: /* calagedatem=(anpyram+mpyram/12.+jpyram/365.-dateintmean)*YEARM; */
1.126 brouard 7202:
1.227 brouard 7203: /* prevalence(probs, ageminpar, agemax, s, agev, nlstate, imx, Tvar, nbcode, ncodemax, mint, anint, dateprev1, dateprev2, firstpass, lastpass); */
1.126 brouard 7204:
7205:
1.227 brouard 7206: /* strcpy(filerespop,"POP_"); */
7207: /* strcat(filerespop,fileresu); */
7208: /* if((ficrespop=fopen(filerespop,"w"))==NULL) { */
7209: /* printf("Problem with forecast resultfile: %s\n", filerespop); */
7210: /* fprintf(ficlog,"Problem with forecast resultfile: %s\n", filerespop); */
7211: /* } */
7212: /* printf("Computing forecasting: result on file '%s' \n", filerespop); */
7213: /* fprintf(ficlog,"Computing forecasting: result on file '%s' \n", filerespop); */
1.126 brouard 7214:
1.227 brouard 7215: /* if (cptcoveff==0) ncodemax[cptcoveff]=1; */
1.126 brouard 7216:
1.227 brouard 7217: /* /\* if (mobilav!=0) { *\/ */
7218: /* /\* mobaverage= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX); *\/ */
7219: /* /\* if (movingaverage(probs, ageminpar, fage, mobaverage,mobilav)!=0){ *\/ */
7220: /* /\* fprintf(ficlog," Error in movingaverage mobilav=%d\n",mobilav); *\/ */
7221: /* /\* printf(" Error in movingaverage mobilav=%d\n",mobilav); *\/ */
7222: /* /\* } *\/ */
7223: /* /\* } *\/ */
1.126 brouard 7224:
1.227 brouard 7225: /* stepsize=(int) (stepm+YEARM-1)/YEARM; */
7226: /* if (stepm<=12) stepsize=1; */
1.126 brouard 7227:
1.227 brouard 7228: /* agelim=AGESUP; */
1.126 brouard 7229:
1.227 brouard 7230: /* hstepm=1; */
7231: /* hstepm=hstepm/stepm; */
1.218 brouard 7232:
1.227 brouard 7233: /* if (popforecast==1) { */
7234: /* if((ficpop=fopen(popfile,"r"))==NULL) { */
7235: /* printf("Problem with population file : %s\n",popfile);exit(0); */
7236: /* fprintf(ficlog,"Problem with population file : %s\n",popfile);exit(0); */
7237: /* } */
7238: /* popage=ivector(0,AGESUP); */
7239: /* popeffectif=vector(0,AGESUP); */
7240: /* popcount=vector(0,AGESUP); */
1.126 brouard 7241:
1.227 brouard 7242: /* i=1; */
7243: /* while ((c=fscanf(ficpop,"%d %lf\n",&popage[i],&popcount[i])) != EOF) i=i+1; */
1.218 brouard 7244:
1.227 brouard 7245: /* imx=i; */
7246: /* for (i=1; i<imx;i++) popeffectif[popage[i]]=popcount[i]; */
7247: /* } */
1.218 brouard 7248:
1.227 brouard 7249: /* for(cptcov=1,k=0;cptcov<=i2;cptcov++){ */
7250: /* for(cptcod=1;cptcod<=ncodemax[cptcoveff];cptcod++){ */
7251: /* k=k+1; */
7252: /* fprintf(ficrespop,"\n#******"); */
7253: /* for(j=1;j<=cptcoveff;j++) { */
7254: /* fprintf(ficrespop," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]); */
7255: /* } */
7256: /* fprintf(ficrespop,"******\n"); */
7257: /* fprintf(ficrespop,"# Age"); */
7258: /* for(j=1; j<=nlstate+ndeath;j++) fprintf(ficrespop," P.%d",j); */
7259: /* if (popforecast==1) fprintf(ficrespop," [Population]"); */
1.126 brouard 7260:
1.227 brouard 7261: /* for (cpt=0; cpt<=0;cpt++) { */
7262: /* fprintf(ficrespop,"\n\n# Forecasting at date %.lf/%.lf/%.lf ",jpyram,mpyram,anpyram+cpt); */
1.126 brouard 7263:
1.227 brouard 7264: /* for (agedeb=(fage-((int)calagedatem %12/12.)); agedeb>=(ageminpar-((int)calagedatem %12)/12.); agedeb--){ */
7265: /* nhstepm=(int) rint((agelim-agedeb)*YEARM/stepm); */
7266: /* nhstepm = nhstepm/hstepm; */
1.126 brouard 7267:
1.227 brouard 7268: /* p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); */
7269: /* oldm=oldms;savm=savms; */
7270: /* hpxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k); */
1.218 brouard 7271:
1.227 brouard 7272: /* for (h=0; h<=nhstepm; h++){ */
7273: /* if (h==(int) (calagedatem+YEARM*cpt)) { */
7274: /* fprintf(ficrespop,"\n %3.f ",agedeb+h*hstepm/YEARM*stepm); */
7275: /* } */
7276: /* for(j=1; j<=nlstate+ndeath;j++) { */
7277: /* kk1=0.;kk2=0; */
7278: /* for(i=1; i<=nlstate;i++) { */
7279: /* if (mobilav==1) */
7280: /* kk1=kk1+p3mat[i][j][h]*mobaverage[(int)agedeb+1][i][cptcod]; */
7281: /* else { */
7282: /* kk1=kk1+p3mat[i][j][h]*probs[(int)(agedeb+1)][i][cptcod]; */
7283: /* } */
7284: /* } */
7285: /* if (h==(int)(calagedatem+12*cpt)){ */
7286: /* tabpop[(int)(agedeb)][j][cptcod]=kk1; */
7287: /* /\*fprintf(ficrespop," %.3f", kk1); */
7288: /* if (popforecast==1) fprintf(ficrespop," [%.f]", kk1*popeffectif[(int)agedeb+1]);*\/ */
7289: /* } */
7290: /* } */
7291: /* for(i=1; i<=nlstate;i++){ */
7292: /* kk1=0.; */
7293: /* for(j=1; j<=nlstate;j++){ */
7294: /* kk1= kk1+tabpop[(int)(agedeb)][j][cptcod]; */
7295: /* } */
7296: /* tabpopprev[(int)(agedeb)][i][cptcod]=tabpop[(int)(agedeb)][i][cptcod]/kk1*popeffectif[(int)(agedeb+(calagedatem+12*cpt)*hstepm/YEARM*stepm-1)]; */
7297: /* } */
1.218 brouard 7298:
1.227 brouard 7299: /* if (h==(int)(calagedatem+12*cpt)) */
7300: /* for(j=1; j<=nlstate;j++) */
7301: /* fprintf(ficrespop," %15.2f",tabpopprev[(int)(agedeb+1)][j][cptcod]); */
7302: /* } */
7303: /* free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); */
7304: /* } */
7305: /* } */
1.218 brouard 7306:
1.227 brouard 7307: /* /\******\/ */
1.218 brouard 7308:
1.227 brouard 7309: /* for (cpt=1; cpt<=(anpyram1-anpyram);cpt++) { */
7310: /* fprintf(ficrespop,"\n\n# Forecasting at date %.lf/%.lf/%.lf ",jpyram,mpyram,anpyram+cpt); */
7311: /* for (agedeb=(fage-((int)calagedatem %12/12.)); agedeb>=(ageminpar-((int)calagedatem %12)/12.); agedeb--){ */
7312: /* nhstepm=(int) rint((agelim-agedeb)*YEARM/stepm); */
7313: /* nhstepm = nhstepm/hstepm; */
1.126 brouard 7314:
1.227 brouard 7315: /* p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); */
7316: /* oldm=oldms;savm=savms; */
7317: /* hpxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k); */
7318: /* for (h=0; h<=nhstepm; h++){ */
7319: /* if (h==(int) (calagedatem+YEARM*cpt)) { */
7320: /* fprintf(ficresf,"\n %3.f ",agedeb+h*hstepm/YEARM*stepm); */
7321: /* } */
7322: /* for(j=1; j<=nlstate+ndeath;j++) { */
7323: /* kk1=0.;kk2=0; */
7324: /* for(i=1; i<=nlstate;i++) { */
7325: /* kk1=kk1+p3mat[i][j][h]*tabpopprev[(int)agedeb+1][i][cptcod]; */
7326: /* } */
7327: /* if (h==(int)(calagedatem+12*cpt)) fprintf(ficresf," %15.2f", kk1); */
7328: /* } */
7329: /* } */
7330: /* free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); */
7331: /* } */
7332: /* } */
7333: /* } */
7334: /* } */
1.218 brouard 7335:
1.227 brouard 7336: /* /\* if (mobilav!=0) free_ma3x(mobaverage,1, AGESUP,1,NCOVMAX, 1,NCOVMAX); *\/ */
1.218 brouard 7337:
1.227 brouard 7338: /* if (popforecast==1) { */
7339: /* free_ivector(popage,0,AGESUP); */
7340: /* free_vector(popeffectif,0,AGESUP); */
7341: /* free_vector(popcount,0,AGESUP); */
7342: /* } */
7343: /* free_ma3x(tabpop,1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
7344: /* free_ma3x(tabpopprev,1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
7345: /* fclose(ficrespop); */
7346: /* } /\* End of popforecast *\/ */
1.218 brouard 7347:
1.126 brouard 7348: int fileappend(FILE *fichier, char *optionfich)
7349: {
7350: if((fichier=fopen(optionfich,"a"))==NULL) {
7351: printf("Problem with file: %s\n", optionfich);
7352: fprintf(ficlog,"Problem with file: %s\n", optionfich);
7353: return (0);
7354: }
7355: fflush(fichier);
7356: return (1);
7357: }
7358:
7359:
7360: /**************** function prwizard **********************/
7361: void prwizard(int ncovmodel, int nlstate, int ndeath, char model[], FILE *ficparo)
7362: {
7363:
7364: /* Wizard to print covariance matrix template */
7365:
1.164 brouard 7366: char ca[32], cb[32];
7367: int i,j, k, li, lj, lk, ll, jj, npar, itimes;
1.126 brouard 7368: int numlinepar;
7369:
7370: printf("# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n");
7371: fprintf(ficparo,"# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n");
7372: for(i=1; i <=nlstate; i++){
7373: jj=0;
7374: for(j=1; j <=nlstate+ndeath; j++){
7375: if(j==i) continue;
7376: jj++;
7377: /*ca[0]= k+'a'-1;ca[1]='\0';*/
7378: printf("%1d%1d",i,j);
7379: fprintf(ficparo,"%1d%1d",i,j);
7380: for(k=1; k<=ncovmodel;k++){
7381: /* printf(" %lf",param[i][j][k]); */
7382: /* fprintf(ficparo," %lf",param[i][j][k]); */
7383: printf(" 0.");
7384: fprintf(ficparo," 0.");
7385: }
7386: printf("\n");
7387: fprintf(ficparo,"\n");
7388: }
7389: }
7390: printf("# Scales (for hessian or gradient estimation)\n");
7391: fprintf(ficparo,"# Scales (for hessian or gradient estimation)\n");
7392: npar= (nlstate+ndeath-1)*nlstate*ncovmodel; /* Number of parameters*/
7393: for(i=1; i <=nlstate; i++){
7394: jj=0;
7395: for(j=1; j <=nlstate+ndeath; j++){
7396: if(j==i) continue;
7397: jj++;
7398: fprintf(ficparo,"%1d%1d",i,j);
7399: printf("%1d%1d",i,j);
7400: fflush(stdout);
7401: for(k=1; k<=ncovmodel;k++){
7402: /* printf(" %le",delti3[i][j][k]); */
7403: /* fprintf(ficparo," %le",delti3[i][j][k]); */
7404: printf(" 0.");
7405: fprintf(ficparo," 0.");
7406: }
7407: numlinepar++;
7408: printf("\n");
7409: fprintf(ficparo,"\n");
7410: }
7411: }
7412: printf("# Covariance matrix\n");
7413: /* # 121 Var(a12)\n\ */
7414: /* # 122 Cov(b12,a12) Var(b12)\n\ */
7415: /* # 131 Cov(a13,a12) Cov(a13,b12, Var(a13)\n\ */
7416: /* # 132 Cov(b13,a12) Cov(b13,b12, Cov(b13,a13) Var(b13)\n\ */
7417: /* # 212 Cov(a21,a12) Cov(a21,b12, Cov(a21,a13) Cov(a21,b13) Var(a21)\n\ */
7418: /* # 212 Cov(b21,a12) Cov(b21,b12, Cov(b21,a13) Cov(b21,b13) Cov(b21,a21) Var(b21)\n\ */
7419: /* # 232 Cov(a23,a12) Cov(a23,b12, Cov(a23,a13) Cov(a23,b13) Cov(a23,a21) Cov(a23,b21) Var(a23)\n\ */
7420: /* # 232 Cov(b23,a12) Cov(b23,b12) ... Var (b23)\n" */
7421: fflush(stdout);
7422: fprintf(ficparo,"# Covariance matrix\n");
7423: /* # 121 Var(a12)\n\ */
7424: /* # 122 Cov(b12,a12) Var(b12)\n\ */
7425: /* # ...\n\ */
7426: /* # 232 Cov(b23,a12) Cov(b23,b12) ... Var (b23)\n" */
7427:
7428: for(itimes=1;itimes<=2;itimes++){
7429: jj=0;
7430: for(i=1; i <=nlstate; i++){
7431: for(j=1; j <=nlstate+ndeath; j++){
7432: if(j==i) continue;
7433: for(k=1; k<=ncovmodel;k++){
7434: jj++;
7435: ca[0]= k+'a'-1;ca[1]='\0';
7436: if(itimes==1){
7437: printf("#%1d%1d%d",i,j,k);
7438: fprintf(ficparo,"#%1d%1d%d",i,j,k);
7439: }else{
7440: printf("%1d%1d%d",i,j,k);
7441: fprintf(ficparo,"%1d%1d%d",i,j,k);
7442: /* printf(" %.5le",matcov[i][j]); */
7443: }
7444: ll=0;
7445: for(li=1;li <=nlstate; li++){
7446: for(lj=1;lj <=nlstate+ndeath; lj++){
7447: if(lj==li) continue;
7448: for(lk=1;lk<=ncovmodel;lk++){
7449: ll++;
7450: if(ll<=jj){
7451: cb[0]= lk +'a'-1;cb[1]='\0';
7452: if(ll<jj){
7453: if(itimes==1){
7454: printf(" Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj);
7455: fprintf(ficparo," Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj);
7456: }else{
7457: printf(" 0.");
7458: fprintf(ficparo," 0.");
7459: }
7460: }else{
7461: if(itimes==1){
7462: printf(" Var(%s%1d%1d)",ca,i,j);
7463: fprintf(ficparo," Var(%s%1d%1d)",ca,i,j);
7464: }else{
7465: printf(" 0.");
7466: fprintf(ficparo," 0.");
7467: }
7468: }
7469: }
7470: } /* end lk */
7471: } /* end lj */
7472: } /* end li */
7473: printf("\n");
7474: fprintf(ficparo,"\n");
7475: numlinepar++;
7476: } /* end k*/
7477: } /*end j */
7478: } /* end i */
7479: } /* end itimes */
7480:
7481: } /* end of prwizard */
7482: /******************* Gompertz Likelihood ******************************/
7483: double gompertz(double x[])
7484: {
7485: double A,B,L=0.0,sump=0.,num=0.;
7486: int i,n=0; /* n is the size of the sample */
7487:
1.220 brouard 7488: for (i=1;i<=imx ; i++) {
1.126 brouard 7489: sump=sump+weight[i];
7490: /* sump=sump+1;*/
7491: num=num+1;
7492: }
7493:
7494:
7495: /* for (i=0; i<=imx; i++)
7496: 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]);*/
7497:
7498: for (i=1;i<=imx ; i++)
7499: {
7500: if (cens[i] == 1 && wav[i]>1)
7501: A=-x[1]/(x[2])*(exp(x[2]*(agecens[i]-agegomp))-exp(x[2]*(ageexmed[i]-agegomp)));
7502:
7503: if (cens[i] == 0 && wav[i]>1)
7504: A=-x[1]/(x[2])*(exp(x[2]*(agedc[i]-agegomp))-exp(x[2]*(ageexmed[i]-agegomp)))
7505: +log(x[1]/YEARM)+x[2]*(agedc[i]-agegomp)+log(YEARM);
7506:
7507: /*if (wav[i] > 1 && agecens[i] > 15) {*/ /* ??? */
7508: if (wav[i] > 1 ) { /* ??? */
7509: L=L+A*weight[i];
7510: /* 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]);*/
7511: }
7512: }
7513:
7514: /*printf("x1=%2.9f x2=%2.9f x3=%2.9f L=%f\n",x[1],x[2],x[3],L);*/
7515:
7516: return -2*L*num/sump;
7517: }
7518:
1.136 brouard 7519: #ifdef GSL
7520: /******************* Gompertz_f Likelihood ******************************/
7521: double gompertz_f(const gsl_vector *v, void *params)
7522: {
7523: double A,B,LL=0.0,sump=0.,num=0.;
7524: double *x= (double *) v->data;
7525: int i,n=0; /* n is the size of the sample */
7526:
7527: for (i=0;i<=imx-1 ; i++) {
7528: sump=sump+weight[i];
7529: /* sump=sump+1;*/
7530: num=num+1;
7531: }
7532:
7533:
7534: /* for (i=0; i<=imx; i++)
7535: 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]);*/
7536: printf("x[0]=%lf x[1]=%lf\n",x[0],x[1]);
7537: for (i=1;i<=imx ; i++)
7538: {
7539: if (cens[i] == 1 && wav[i]>1)
7540: A=-x[0]/(x[1])*(exp(x[1]*(agecens[i]-agegomp))-exp(x[1]*(ageexmed[i]-agegomp)));
7541:
7542: if (cens[i] == 0 && wav[i]>1)
7543: A=-x[0]/(x[1])*(exp(x[1]*(agedc[i]-agegomp))-exp(x[1]*(ageexmed[i]-agegomp)))
7544: +log(x[0]/YEARM)+x[1]*(agedc[i]-agegomp)+log(YEARM);
7545:
7546: /*if (wav[i] > 1 && agecens[i] > 15) {*/ /* ??? */
7547: if (wav[i] > 1 ) { /* ??? */
7548: LL=LL+A*weight[i];
7549: /* 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]);*/
7550: }
7551: }
7552:
7553: /*printf("x1=%2.9f x2=%2.9f x3=%2.9f L=%f\n",x[1],x[2],x[3],L);*/
7554: printf("x[0]=%lf x[1]=%lf -2*LL*num/sump=%lf\n",x[0],x[1],-2*LL*num/sump);
7555:
7556: return -2*LL*num/sump;
7557: }
7558: #endif
7559:
1.126 brouard 7560: /******************* Printing html file ***********/
1.201 brouard 7561: void printinghtmlmort(char fileresu[], char title[], char datafile[], int firstpass, \
1.126 brouard 7562: int lastpass, int stepm, int weightopt, char model[],\
7563: int imx, double p[],double **matcov,double agemortsup){
7564: int i,k;
7565:
7566: fprintf(fichtm,"<ul><li><h4>Result files </h4>\n Force of mortality. Parameters of the Gompertz fit (with confidence interval in brackets):<br>");
7567: fprintf(fichtm," mu(age) =%lf*exp(%lf*(age-%d)) per year<br><br>",p[1],p[2],agegomp);
7568: for (i=1;i<=2;i++)
7569: 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 7570: fprintf(fichtm,"<br><br><img src=\"graphmort.svg\">");
1.126 brouard 7571: fprintf(fichtm,"</ul>");
7572:
7573: fprintf(fichtm,"<ul><li><h4>Life table</h4>\n <br>");
7574:
7575: 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>");
7576:
7577: for (k=agegomp;k<(agemortsup-2);k++)
7578: 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]);
7579:
7580:
7581: fflush(fichtm);
7582: }
7583:
7584: /******************* Gnuplot file **************/
1.201 brouard 7585: void printinggnuplotmort(char fileresu[], char optionfilefiname[], double ageminpar, double agemaxpar, double fage , char pathc[], double p[]){
1.126 brouard 7586:
7587: char dirfileres[132],optfileres[132];
1.164 brouard 7588:
1.126 brouard 7589: int ng;
7590:
7591:
7592: /*#ifdef windows */
7593: fprintf(ficgp,"cd \"%s\" \n",pathc);
7594: /*#endif */
7595:
7596:
7597: strcpy(dirfileres,optionfilefiname);
7598: strcpy(optfileres,"vpl");
1.199 brouard 7599: fprintf(ficgp,"set out \"graphmort.svg\"\n ");
1.126 brouard 7600: fprintf(ficgp,"set xlabel \"Age\"\n set ylabel \"Force of mortality (per year)\" \n ");
1.199 brouard 7601: fprintf(ficgp, "set ter svg size 640, 480\n set log y\n");
1.145 brouard 7602: /* fprintf(ficgp, "set size 0.65,0.65\n"); */
1.126 brouard 7603: fprintf(ficgp,"plot [%d:100] %lf*exp(%lf*(x-%d))",agegomp,p[1],p[2],agegomp);
7604:
7605: }
7606:
1.136 brouard 7607: int readdata(char datafile[], int firstobs, int lastobs, int *imax)
7608: {
1.126 brouard 7609:
1.136 brouard 7610: /*-------- data file ----------*/
7611: FILE *fic;
7612: char dummy[]=" ";
1.223 brouard 7613: int i=0, j=0, n=0, iv=0;
7614: int lstra;
1.136 brouard 7615: int linei, month, year,iout;
7616: char line[MAXLINE], linetmp[MAXLINE];
1.164 brouard 7617: char stra[MAXLINE], strb[MAXLINE];
1.136 brouard 7618: char *stratrunc;
1.223 brouard 7619:
1.126 brouard 7620:
7621:
1.136 brouard 7622: if((fic=fopen(datafile,"r"))==NULL) {
1.218 brouard 7623: printf("Problem while opening datafile: %s with errno='%s'\n", datafile,strerror(errno));fflush(stdout);
7624: fprintf(ficlog,"Problem while opening datafile: %s with errno='%s'\n", datafile,strerror(errno));fflush(ficlog);return 1;
1.136 brouard 7625: }
1.126 brouard 7626:
1.136 brouard 7627: i=1;
7628: linei=0;
7629: while ((fgets(line, MAXLINE, fic) != NULL) &&((i >= firstobs) && (i <=lastobs))) {
7630: linei=linei+1;
7631: for(j=strlen(line); j>=0;j--){ /* Untabifies line */
7632: if(line[j] == '\t')
7633: line[j] = ' ';
7634: }
7635: for(j=strlen(line)-1; (line[j]==' ')||(line[j]==10)||(line[j]==13);j--){
7636: ;
7637: };
7638: line[j+1]=0; /* Trims blanks at end of line */
7639: if(line[0]=='#'){
7640: fprintf(ficlog,"Comment line\n%s\n",line);
7641: printf("Comment line\n%s\n",line);
7642: continue;
7643: }
7644: trimbb(linetmp,line); /* Trims multiple blanks in line */
1.164 brouard 7645: strcpy(line, linetmp);
1.223 brouard 7646:
7647: /* Loops on waves */
7648: for (j=maxwav;j>=1;j--){
7649: for (iv=nqtv;iv>=1;iv--){ /* Loop on time varying quantitative variables */
1.232 brouard 7650: cutv(stra, strb, line, ' ');
7651: if(strb[0]=='.') { /* Missing value */
7652: lval=-1;
7653: cotqvar[j][iv][i]=-1; /* 0.0/0.0 */
7654: cotvar[j][ntv+iv][i]=-1; /* For performance reasons */
7655: if(isalpha(strb[1])) { /* .m or .d Really Missing value */
7656: 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);
7657: 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);
7658: return 1;
7659: }
7660: }else{
7661: errno=0;
7662: /* what_kind_of_number(strb); */
7663: dval=strtod(strb,&endptr);
7664: /* if( strb[0]=='\0' || (*endptr != '\0')){ */
7665: /* if(strb != endptr && *endptr == '\0') */
7666: /* dval=dlval; */
7667: /* if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN)) */
7668: if( strb[0]=='\0' || (*endptr != '\0')){
7669: 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);
7670: 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);
7671: return 1;
7672: }
7673: cotqvar[j][iv][i]=dval;
7674: cotvar[j][ntv+iv][i]=dval;
7675: }
7676: strcpy(line,stra);
1.223 brouard 7677: }/* end loop ntqv */
1.225 brouard 7678:
1.223 brouard 7679: for (iv=ntv;iv>=1;iv--){ /* Loop on time varying dummies */
1.232 brouard 7680: cutv(stra, strb, line, ' ');
7681: if(strb[0]=='.') { /* Missing value */
7682: lval=-1;
7683: }else{
7684: errno=0;
7685: lval=strtol(strb,&endptr,10);
7686: /* if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN))*/
7687: if( strb[0]=='\0' || (*endptr != '\0')){
7688: 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);
7689: 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);
7690: return 1;
7691: }
7692: }
7693: if(lval <-1 || lval >1){
7694: printf("Error reading data around '%ld' at line number %d for individual %d, '%s'\n \
1.223 brouard 7695: Should be a value of %d(nth) covariate (0 should be the value for the reference and 1\n \
7696: for the alternative. IMaCh does not build design variables automatically, do it yourself.\n \
1.232 brouard 7697: For example, for multinomial values like 1, 2 and 3,\n \
7698: build V1=0 V2=0 for the reference value (1),\n \
7699: V1=1 V2=0 for (2) \n \
1.223 brouard 7700: and V1=0 V2=1 for (3). V1=1 V2=1 should not exist and the corresponding\n \
1.232 brouard 7701: output of IMaCh is often meaningless.\n \
1.223 brouard 7702: Exiting.\n",lval,linei, i,line,j);
1.232 brouard 7703: fprintf(ficlog,"Error reading data around '%ld' at line number %d for individual %d, '%s'\n \
1.223 brouard 7704: Should be a value of %d(nth) covariate (0 should be the value for the reference and 1\n \
7705: for the alternative. IMaCh does not build design variables automatically, do it yourself.\n \
1.232 brouard 7706: For example, for multinomial values like 1, 2 and 3,\n \
7707: build V1=0 V2=0 for the reference value (1),\n \
7708: V1=1 V2=0 for (2) \n \
1.223 brouard 7709: and V1=0 V2=1 for (3). V1=1 V2=1 should not exist and the corresponding\n \
1.232 brouard 7710: output of IMaCh is often meaningless.\n \
1.223 brouard 7711: Exiting.\n",lval,linei, i,line,j);fflush(ficlog);
1.232 brouard 7712: return 1;
7713: }
7714: cotvar[j][iv][i]=(double)(lval);
7715: strcpy(line,stra);
1.223 brouard 7716: }/* end loop ntv */
1.225 brouard 7717:
1.223 brouard 7718: /* Statuses at wave */
1.137 brouard 7719: cutv(stra, strb, line, ' ');
1.223 brouard 7720: if(strb[0]=='.') { /* Missing value */
1.232 brouard 7721: lval=-1;
1.136 brouard 7722: }else{
1.232 brouard 7723: errno=0;
7724: lval=strtol(strb,&endptr,10);
7725: /* if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN))*/
7726: if( strb[0]=='\0' || (*endptr != '\0')){
7727: 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);
7728: 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);
7729: return 1;
7730: }
1.136 brouard 7731: }
1.225 brouard 7732:
1.136 brouard 7733: s[j][i]=lval;
1.225 brouard 7734:
1.223 brouard 7735: /* Date of Interview */
1.136 brouard 7736: strcpy(line,stra);
7737: cutv(stra, strb,line,' ');
1.169 brouard 7738: if( (iout=sscanf(strb,"%d/%d",&month, &year)) != 0){
1.136 brouard 7739: }
1.169 brouard 7740: else if( (iout=sscanf(strb,"%s.",dummy)) != 0){
1.225 brouard 7741: month=99;
7742: year=9999;
1.136 brouard 7743: }else{
1.225 brouard 7744: 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);
7745: 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);
7746: return 1;
1.136 brouard 7747: }
7748: anint[j][i]= (double) year;
7749: mint[j][i]= (double)month;
7750: strcpy(line,stra);
1.223 brouard 7751: } /* End loop on waves */
1.225 brouard 7752:
1.223 brouard 7753: /* Date of death */
1.136 brouard 7754: cutv(stra, strb,line,' ');
1.169 brouard 7755: if( (iout=sscanf(strb,"%d/%d",&month, &year)) != 0){
1.136 brouard 7756: }
1.169 brouard 7757: else if( (iout=sscanf(strb,"%s.",dummy)) != 0){
1.136 brouard 7758: month=99;
7759: year=9999;
7760: }else{
1.141 brouard 7761: 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 7762: 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);
7763: return 1;
1.136 brouard 7764: }
7765: andc[i]=(double) year;
7766: moisdc[i]=(double) month;
7767: strcpy(line,stra);
7768:
1.223 brouard 7769: /* Date of birth */
1.136 brouard 7770: cutv(stra, strb,line,' ');
1.169 brouard 7771: if( (iout=sscanf(strb,"%d/%d",&month, &year)) != 0){
1.136 brouard 7772: }
1.169 brouard 7773: else if( (iout=sscanf(strb,"%s.", dummy)) != 0){
1.136 brouard 7774: month=99;
7775: year=9999;
7776: }else{
1.141 brouard 7777: 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);
7778: 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 7779: return 1;
1.136 brouard 7780: }
7781: if (year==9999) {
1.141 brouard 7782: 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);
7783: 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 7784: return 1;
7785:
1.136 brouard 7786: }
7787: annais[i]=(double)(year);
7788: moisnais[i]=(double)(month);
7789: strcpy(line,stra);
1.225 brouard 7790:
1.223 brouard 7791: /* Sample weight */
1.136 brouard 7792: cutv(stra, strb,line,' ');
7793: errno=0;
7794: dval=strtod(strb,&endptr);
7795: if( strb[0]=='\0' || (*endptr != '\0')){
1.141 brouard 7796: printf("Error reading data around '%f' at line number %d, \"%s\" for individual %d\nShould be a weight. Exiting.\n",dval, i,line,linei);
7797: 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 7798: fflush(ficlog);
7799: return 1;
7800: }
7801: weight[i]=dval;
7802: strcpy(line,stra);
1.225 brouard 7803:
1.223 brouard 7804: for (iv=nqv;iv>=1;iv--){ /* Loop on fixed quantitative variables */
7805: cutv(stra, strb, line, ' ');
7806: if(strb[0]=='.') { /* Missing value */
1.225 brouard 7807: lval=-1;
1.223 brouard 7808: }else{
1.225 brouard 7809: errno=0;
7810: /* what_kind_of_number(strb); */
7811: dval=strtod(strb,&endptr);
7812: /* if(strb != endptr && *endptr == '\0') */
7813: /* dval=dlval; */
7814: /* if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN)) */
7815: if( strb[0]=='\0' || (*endptr != '\0')){
7816: 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);
7817: 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);
7818: return 1;
7819: }
7820: coqvar[iv][i]=dval;
1.226 brouard 7821: covar[ncovcol+iv][i]=dval; /* including qvar in standard covar for performance reasons */
1.223 brouard 7822: }
7823: strcpy(line,stra);
7824: }/* end loop nqv */
1.136 brouard 7825:
1.223 brouard 7826: /* Covariate values */
1.136 brouard 7827: for (j=ncovcol;j>=1;j--){
7828: cutv(stra, strb,line,' ');
1.223 brouard 7829: if(strb[0]=='.') { /* Missing covariate value */
1.225 brouard 7830: lval=-1;
1.136 brouard 7831: }else{
1.225 brouard 7832: errno=0;
7833: lval=strtol(strb,&endptr,10);
7834: if( strb[0]=='\0' || (*endptr != '\0')){
7835: 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);
7836: 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);
7837: return 1;
7838: }
1.136 brouard 7839: }
7840: if(lval <-1 || lval >1){
1.225 brouard 7841: printf("Error reading data around '%ld' at line number %d for individual %d, '%s'\n \
1.136 brouard 7842: Should be a value of %d(nth) covariate (0 should be the value for the reference and 1\n \
7843: for the alternative. IMaCh does not build design variables automatically, do it yourself.\n \
1.225 brouard 7844: For example, for multinomial values like 1, 2 and 3,\n \
7845: build V1=0 V2=0 for the reference value (1),\n \
7846: V1=1 V2=0 for (2) \n \
1.136 brouard 7847: and V1=0 V2=1 for (3). V1=1 V2=1 should not exist and the corresponding\n \
1.225 brouard 7848: output of IMaCh is often meaningless.\n \
1.136 brouard 7849: Exiting.\n",lval,linei, i,line,j);
1.225 brouard 7850: fprintf(ficlog,"Error reading data around '%ld' at line number %d for individual %d, '%s'\n \
1.136 brouard 7851: Should be a value of %d(nth) covariate (0 should be the value for the reference and 1\n \
7852: for the alternative. IMaCh does not build design variables automatically, do it yourself.\n \
1.225 brouard 7853: For example, for multinomial values like 1, 2 and 3,\n \
7854: build V1=0 V2=0 for the reference value (1),\n \
7855: V1=1 V2=0 for (2) \n \
1.136 brouard 7856: and V1=0 V2=1 for (3). V1=1 V2=1 should not exist and the corresponding\n \
1.225 brouard 7857: output of IMaCh is often meaningless.\n \
1.136 brouard 7858: Exiting.\n",lval,linei, i,line,j);fflush(ficlog);
1.225 brouard 7859: return 1;
1.136 brouard 7860: }
7861: covar[j][i]=(double)(lval);
7862: strcpy(line,stra);
7863: }
7864: lstra=strlen(stra);
1.225 brouard 7865:
1.136 brouard 7866: if(lstra > 9){ /* More than 2**32 or max of what printf can write with %ld */
7867: stratrunc = &(stra[lstra-9]);
7868: num[i]=atol(stratrunc);
7869: }
7870: else
7871: num[i]=atol(stra);
7872: /*if((s[2][i]==2) && (s[3][i]==-1)&&(s[4][i]==9)){
7873: 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;}*/
7874:
7875: i=i+1;
7876: } /* End loop reading data */
1.225 brouard 7877:
1.136 brouard 7878: *imax=i-1; /* Number of individuals */
7879: fclose(fic);
1.225 brouard 7880:
1.136 brouard 7881: return (0);
1.164 brouard 7882: /* endread: */
1.225 brouard 7883: printf("Exiting readdata: ");
7884: fclose(fic);
7885: return (1);
1.223 brouard 7886: }
1.126 brouard 7887:
1.234 ! brouard 7888: void removefirstspace(char **stri){/*, char stro[]) {*/
1.230 brouard 7889: char *p1 = *stri, *p2 = *stri;
1.234 ! brouard 7890: if (*p2 == ' ')
! 7891: p2++;
! 7892: /* while ((*p1++ = *p2++) !=0) */
! 7893: /* ; */
! 7894: /* do */
! 7895: /* while (*p2 == ' ') */
! 7896: /* p2++; */
! 7897: /* while (*p1++ == *p2++); */
! 7898: *stri=p2;
1.145 brouard 7899: }
7900:
1.230 brouard 7901: int decoderesult ( char resultline[])
7902: /**< This routine decode one result line and returns the combination # of dummy covariates only **/
7903: {
1.234 ! brouard 7904: int j=0, k=0, k1=0, k2=0, match=0;
1.230 brouard 7905: char resultsav[MAXLINE];
1.234 ! brouard 7906: int resultmodel[MAXLINE];
! 7907: int modelresult[MAXLINE];
1.230 brouard 7908: char stra[80], strb[80], strc[80], strd[80],stre[80];
7909:
1.234 ! brouard 7910: removefirstspace(&resultline);
1.233 brouard 7911: printf("decoderesult:%s\n",resultline);
1.230 brouard 7912:
7913: if (strstr(resultline,"v") !=0){
7914: printf("Error. 'v' must be in upper case 'V' result: %s ",resultline);
7915: fprintf(ficlog,"Error. 'v' must be in upper case result: %s ",resultline);fflush(ficlog);
7916: return 1;
7917: }
7918: trimbb(resultsav, resultline);
7919: if (strlen(resultsav) >1){
7920: j=nbocc(resultsav,'='); /**< j=Number of covariate values'=' */
7921: }
1.234 ! brouard 7922: if( j != cptcovs ){ /* Be careful if a variable is in a product but not single */
! 7923: 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);
! 7924: 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);
! 7925: }
! 7926: for(k=1; k<=j;k++){ /* Loop on any covariate of the result line */
! 7927: if(nbocc(resultsav,'=') >1){
! 7928: cutl(stra,strb,resultsav,' '); /* keeps in strb after the first ' '
! 7929: resultsav= V4=1 V5=25.1 V3=0 strb=V3=0 stra= V4=1 V5=25.1 */
! 7930: cutl(strc,strd,strb,'='); /* strb:V4=1 strc=1 strd=V4 */
! 7931: }else
! 7932: cutl(strc,strd,resultsav,'=');
1.230 brouard 7933: Tvalsel[k]=atof(strc); /* 1 */
1.234 ! brouard 7934:
1.230 brouard 7935: cutl(strc,stre,strd,'V'); /* strd='V4' strc=4 stre='V' */;
7936: Tvarsel[k]=atoi(strc);
7937: /* Typevarsel[k]=1; /\* 1 for age product *\/ */
7938: /* cptcovsel++; */
7939: if (nbocc(stra,'=') >0)
7940: strcpy(resultsav,stra); /* and analyzes it */
7941: }
1.234 ! brouard 7942: /* Checking if no missing or useless values in comparison of current model needs */
! 7943: for(k1=1; k1<= cptcovt ;k1++){ /* model line */
! 7944: if(Typevar[k1]==0){
! 7945: match=0;
! 7946: for(k2=1; k2 <=j;k2++){
! 7947: if(Tvar[k1]==Tvarsel[k2]) {
! 7948: modelresult[k2]=k1;
! 7949: match=1;
! 7950: break;
! 7951: }
! 7952: }
! 7953: if(match == 0){
! 7954: printf("Error in result line: %d value missing; result: %s, model=%s\n",k1, resultline, model);
! 7955: }
! 7956: }
! 7957: }
! 7958:
! 7959: for(k2=1; k2 <=j;k2++){ /* result line */
! 7960: match=0;
! 7961: for(k1=1; k1<= cptcovt ;k1++){ /* model line */
! 7962: if(Typevar[k1]==0){
! 7963: if(Tvar[k1]==Tvarsel[k2]) {
! 7964: resultmodel[k1]=k2;
! 7965: ++match;
! 7966: }
! 7967: }
! 7968: }
! 7969: if(match == 0){
! 7970: printf("Error in result line: %d value missing; result: %s, model=%s\n",k1, resultline, model);
! 7971: }else if(match > 1){
! 7972: printf("Error in result line: %d doubled; result: %s, model=%s\n",k2, resultline, model);
! 7973: }
! 7974: }
! 7975:
! 7976: /* We need to deduce which combination number is chosen and save quantitative values */
! 7977:
1.230 brouard 7978: return (0);
7979: }
7980: int selected( int kvar){ /* Selected combination of covariates */
7981: if(Tvarsel[kvar])
7982: return (0);
7983: else
7984: return(1);
7985: }
7986: int decodemodel( char model[], int lastobs)
7987: /**< This routine decodes the model and returns:
1.224 brouard 7988: * Model V1+V2+V3+V8+V7*V8+V5*V6+V8*age+V3*age+age*age
7989: * - nagesqr = 1 if age*age in the model, otherwise 0.
7990: * - cptcovt total number of covariates of the model nbocc(+)+1 = 8 excepting constant and age and age*age
7991: * - cptcovn or number of covariates k of the models excluding age*products =6 and age*age
7992: * - cptcovage number of covariates with age*products =2
7993: * - cptcovs number of simple covariates
7994: * - 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
7995: * which is a new column after the 9 (ncovcol) variables.
7996: * - if k is a product Vn*Vm covar[k][i] is filled with correct values for each individual
7997: * - Tprod[l] gives the kth covariates of the product Vn*Vm l=1 to cptcovprod-cptcovage
7998: * Tprod[1]@2 {5, 6}: position of first product V7*V8 is 5, and second V5*V6 is 6.
7999: * - Tvard[k] p Tvard[1][1]@4 {7, 8, 5, 6} for V7*V8 and V5*V6 .
8000: */
1.136 brouard 8001: {
1.145 brouard 8002: int i, j, k, ks;
1.227 brouard 8003: int j1, k1, k2, k3, k4;
1.136 brouard 8004: char modelsav[80];
1.145 brouard 8005: char stra[80], strb[80], strc[80], strd[80],stre[80];
1.187 brouard 8006: char *strpt;
1.136 brouard 8007:
1.145 brouard 8008: /*removespace(model);*/
1.136 brouard 8009: if (strlen(model) >1){ /* If there is at least 1 covariate */
1.145 brouard 8010: j=0, j1=0, k1=0, k2=-1, ks=0, cptcovn=0;
1.137 brouard 8011: if (strstr(model,"AGE") !=0){
1.192 brouard 8012: printf("Error. AGE must be in lower case 'age' model=1+age+%s. ",model);
8013: fprintf(ficlog,"Error. AGE must be in lower case model=1+age+%s. ",model);fflush(ficlog);
1.136 brouard 8014: return 1;
8015: }
1.141 brouard 8016: if (strstr(model,"v") !=0){
8017: printf("Error. 'v' must be in upper case 'V' model=%s ",model);
8018: fprintf(ficlog,"Error. 'v' must be in upper case model=%s ",model);fflush(ficlog);
8019: return 1;
8020: }
1.187 brouard 8021: strcpy(modelsav,model);
8022: if ((strpt=strstr(model,"age*age")) !=0){
8023: printf(" strpt=%s, model=%s\n",strpt, model);
8024: if(strpt != model){
1.234 ! brouard 8025: printf("Error in model: 'model=%s'; 'age*age' should in first place before other covariates\n \
1.192 brouard 8026: 'model=1+age+age*age+V1.' or 'model=1+age+age*age+V1+V1*age.', please swap as well as \n \
1.187 brouard 8027: corresponding column of parameters.\n",model);
1.234 ! brouard 8028: fprintf(ficlog,"Error in model: 'model=%s'; 'age*age' should in first place before other covariates\n \
1.192 brouard 8029: 'model=1+age+age*age+V1.' or 'model=1+age+age*age+V1+V1*age.', please swap as well as \n \
1.187 brouard 8030: corresponding column of parameters.\n",model); fflush(ficlog);
1.234 ! brouard 8031: return 1;
1.225 brouard 8032: }
1.187 brouard 8033: nagesqr=1;
8034: if (strstr(model,"+age*age") !=0)
1.234 ! brouard 8035: substrchaine(modelsav, model, "+age*age");
1.187 brouard 8036: else if (strstr(model,"age*age+") !=0)
1.234 ! brouard 8037: substrchaine(modelsav, model, "age*age+");
1.187 brouard 8038: else
1.234 ! brouard 8039: substrchaine(modelsav, model, "age*age");
1.187 brouard 8040: }else
8041: nagesqr=0;
8042: if (strlen(modelsav) >1){
8043: j=nbocc(modelsav,'+'); /**< j=Number of '+' */
8044: j1=nbocc(modelsav,'*'); /**< j1=Number of '*' */
1.224 brouard 8045: cptcovs=j+1-j1; /**< Number of simple covariates V1+V1*age+V3 +V3*V4+age*age=> V1 + V3 =5-3=2 */
1.187 brouard 8046: cptcovt= j+1; /* Number of total covariates in the model, not including
1.225 brouard 8047: * cst, age and age*age
8048: * V1+V1*age+ V3 + V3*V4+age*age=> 3+1=4*/
8049: /* including age products which are counted in cptcovage.
8050: * but the covariates which are products must be treated
8051: * separately: ncovn=4- 2=2 (V1+V3). */
1.187 brouard 8052: cptcovprod=j1; /**< Number of products V1*V2 +v3*age = 2 */
8053: cptcovprodnoage=0; /**< Number of covariate products without age: V3*V4 =1 */
1.225 brouard 8054:
8055:
1.187 brouard 8056: /* Design
8057: * V1 V2 V3 V4 V5 V6 V7 V8 V9 Weight
8058: * < ncovcol=8 >
8059: * Model V2 + V1 + V3*age + V3 + V5*V6 + V7*V8 + V8*age + V8
8060: * k= 1 2 3 4 5 6 7 8
8061: * cptcovn number of covariates (not including constant and age ) = # of + plus 1 = 7+1=8
8062: * covar[k,i], value of kth covariate if not including age for individual i:
1.224 brouard 8063: * covar[1][i]= (V1), covar[4][i]=(V4), covar[8][i]=(V8)
8064: * Tvar[k] # of the kth covariate: Tvar[1]=2 Tvar[2]=1 Tvar[4]=3 Tvar[8]=8
1.187 brouard 8065: * if multiplied by age: V3*age Tvar[3=V3*age]=3 (V3) Tvar[7]=8 and
8066: * Tage[++cptcovage]=k
8067: * if products, new covar are created after ncovcol with k1
8068: * Tvar[k]=ncovcol+k1; # of the kth covariate product: Tvar[5]=ncovcol+1=10 Tvar[6]=ncovcol+1=11
8069: * Tprod[k1]=k; Tprod[1]=5 Tprod[2]= 6; gives the position of the k1th product
8070: * 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
8071: * Tvar[cptcovn+k2]=Tvard[k1][1];Tvar[cptcovn+k2+1]=Tvard[k1][2];
8072: * Tvar[8+1]=5;Tvar[8+2]=6;Tvar[8+3]=7;Tvar[8+4]=8 inverted
8073: * V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11
8074: * < ncovcol=8 >
8075: * Model V2 + V1 + V3*age + V3 + V5*V6 + V7*V8 + V8*age + V8 d1 d1 d2 d2
8076: * k= 1 2 3 4 5 6 7 8 9 10 11 12
8077: * Tvar[k]= 2 1 3 3 10 11 8 8 5 6 7 8
8078: * p Tvar[1]@12={2, 1, 3, 3, 11, 10, 8, 8, 7, 8, 5, 6}
8079: * p Tprod[1]@2={ 6, 5}
8080: *p Tvard[1][1]@4= {7, 8, 5, 6}
8081: * covar[k][i]= V2 V1 ? V3 V5*V6? V7*V8? ? V8
8082: * cov[Tage[kk]+2]=covar[Tvar[Tage[kk]]][i]*cov[2];
8083: *How to reorganize?
8084: * Model V1 + V2 + V3 + V8 + V5*V6 + V7*V8 + V3*age + V8*age
8085: * Tvars {2, 1, 3, 3, 11, 10, 8, 8, 7, 8, 5, 6}
8086: * {2, 1, 4, 8, 5, 6, 3, 7}
8087: * Struct []
8088: */
1.225 brouard 8089:
1.187 brouard 8090: /* This loop fills the array Tvar from the string 'model'.*/
8091: /* j is the number of + signs in the model V1+V2+V3 j=2 i=3 to 1 */
8092: /* modelsav=V2+V1+V4+age*V3 strb=age*V3 stra=V2+V1+V4 */
8093: /* k=4 (age*V3) Tvar[k=4]= 3 (from V3) Tage[cptcovage=1]=4 */
8094: /* k=3 V4 Tvar[k=3]= 4 (from V4) */
8095: /* k=2 V1 Tvar[k=2]= 1 (from V1) */
8096: /* k=1 Tvar[1]=2 (from V2) */
8097: /* k=5 Tvar[5] */
8098: /* for (k=1; k<=cptcovn;k++) { */
1.198 brouard 8099: /* cov[2+k]=nbcode[Tvar[k]][codtabm(ij,Tvar[k])]; */
1.187 brouard 8100: /* } */
1.198 brouard 8101: /* for (k=1; k<=cptcovage;k++) cov[2+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,Tvar[Tage[k])]]*cov[2]; */
1.187 brouard 8102: /*
8103: * Treating invertedly V2+V1+V3*age+V2*V4 is as if written V2*V4 +V3*age + V1 + V2 */
1.227 brouard 8104: for(k=cptcovt; k>=1;k--){ /**< Number of covariates not including constant and age, neither age*age*/
8105: Tvar[k]=0; Tprod[k]=0; Tposprod[k]=0;
8106: }
1.187 brouard 8107: cptcovage=0;
8108: for(k=1; k<=cptcovt;k++){ /* Loop on total covariates of the model */
1.234 ! brouard 8109: cutl(stra,strb,modelsav,'+'); /* keeps in strb after the first '+'
1.225 brouard 8110: modelsav==V2+V1+V4+V3*age strb=V3*age stra=V2+V1+V4 */
1.234 ! brouard 8111: if (nbocc(modelsav,'+')==0) strcpy(strb,modelsav); /* and analyzes it */
! 8112: /* printf("i=%d a=%s b=%s sav=%s\n",i, stra,strb,modelsav);*/
! 8113: /*scanf("%d",i);*/
! 8114: if (strchr(strb,'*')) { /**< Model includes a product V2+V1+V4+V3*age strb=V3*age */
! 8115: cutl(strc,strd,strb,'*'); /**< strd*strc Vm*Vn: strb=V3*age(input) strc=age strd=V3 ; V3*V2 strc=V2, strd=V3 */
! 8116: if (strcmp(strc,"age")==0) { /**< Model includes age: Vn*age */
! 8117: /* covar is not filled and then is empty */
! 8118: cptcovprod--;
! 8119: cutl(stre,strb,strd,'V'); /* strd=V3(input): stre="3" */
! 8120: Tvar[k]=atoi(stre); /* V2+V1+V4+V3*age Tvar[4]=3 ; V1+V2*age Tvar[2]=2; V1+V1*age Tvar[2]=1 */
! 8121: Typevar[k]=1; /* 1 for age product */
! 8122: cptcovage++; /* Sums the number of covariates which include age as a product */
! 8123: Tage[cptcovage]=k; /* Tvar[4]=3, Tage[1] = 4 or V1+V1*age Tvar[2]=1, Tage[1]=2 */
! 8124: /*printf("stre=%s ", stre);*/
! 8125: } else if (strcmp(strd,"age")==0) { /* or age*Vn */
! 8126: cptcovprod--;
! 8127: cutl(stre,strb,strc,'V');
! 8128: Tvar[k]=atoi(stre);
! 8129: Typevar[k]=1; /* 1 for age product */
! 8130: cptcovage++;
! 8131: Tage[cptcovage]=k;
! 8132: } else { /* Age is not in the model product V2+V1+V1*V4+V3*age+V3*V2 strb=V3*V2*/
! 8133: /* loops on k1=1 (V3*V2) and k1=2 V4*V3 */
! 8134: cptcovn++;
! 8135: cptcovprodnoage++;k1++;
! 8136: cutl(stre,strb,strc,'V'); /* strc= Vn, stre is n; strb=V3*V2 stre=3 strc=*/
! 8137: Tvar[k]=ncovcol+nqv+ntv+nqtv+k1; /* For model-covariate k tells which data-covariate to use but
! 8138: because this model-covariate is a construction we invent a new column
! 8139: which is after existing variables ncovcol+nqv+ntv+nqtv + k1
! 8140: If already ncovcol=4 and model=V2+V1+V1*V4+age*V3+V3*V2
! 8141: Tvar[3=V1*V4]=4+1 Tvar[5=V3*V2]=4 + 2= 6, etc */
! 8142: Typevar[k]=2; /* 2 for double fixed dummy covariates */
! 8143: cutl(strc,strb,strd,'V'); /* strd was Vm, strc is m */
! 8144: Tprod[k1]=k; /* Tprod[1]=3(=V1*V4) for V2+V1+V1*V4+age*V3+V3*V2 */
! 8145: Tposprod[k]=k1; /* Tpsprod[3]=1, Tposprod[2]=5 */
! 8146: Tvard[k1][1] =atoi(strc); /* m 1 for V1*/
! 8147: Tvard[k1][2] =atoi(stre); /* n 4 for V4*/
! 8148: k2=k2+2; /* k2 is initialize to -1, We want to store the n and m in Vn*Vm at the end of Tvar */
! 8149: /* Tvar[cptcovt+k2]=Tvard[k1][1]; /\* Tvar[(cptcovt=4+k2=1)=5]= 1 (V1) *\/ */
! 8150: /* Tvar[cptcovt+k2+1]=Tvard[k1][2]; /\* Tvar[(cptcovt=4+(k2=1)+1)=6]= 4 (V4) *\/ */
1.225 brouard 8151: /*ncovcol=4 and model=V2+V1+V1*V4+age*V3+V3*V2, Tvar[3]=5, Tvar[4]=6, cptcovt=5 */
1.234 ! brouard 8152: /* 1 2 3 4 5 | Tvar[5+1)=1, Tvar[7]=2 */
! 8153: for (i=1; i<=lastobs;i++){
! 8154: /* Computes the new covariate which is a product of
! 8155: covar[n][i]* covar[m][i] and stores it at ncovol+k1 May not be defined */
! 8156: covar[ncovcol+k1][i]=covar[atoi(stre)][i]*covar[atoi(strc)][i];
! 8157: }
! 8158: } /* End age is not in the model */
! 8159: } /* End if model includes a product */
! 8160: else { /* no more sum */
! 8161: /*printf("d=%s c=%s b=%s\n", strd,strc,strb);*/
! 8162: /* scanf("%d",i);*/
! 8163: cutl(strd,strc,strb,'V');
! 8164: ks++; /**< Number of simple covariates dummy or quantitative, fixe or varying */
! 8165: cptcovn++; /** V4+V3+V5: V4 and V3 timevarying dummy covariates, V5 timevarying quantitative */
! 8166: Tvar[k]=atoi(strd);
! 8167: Typevar[k]=0; /* 0 for simple covariates */
! 8168: }
! 8169: strcpy(modelsav,stra); /* modelsav=V2+V1+V4 stra=V2+V1+V4 */
1.223 brouard 8170: /*printf("a=%s b=%s sav=%s\n", stra,strb,modelsav);
1.225 brouard 8171: scanf("%d",i);*/
1.187 brouard 8172: } /* end of loop + on total covariates */
8173: } /* end if strlen(modelsave == 0) age*age might exist */
8174: } /* end if strlen(model == 0) */
1.136 brouard 8175:
8176: /*The number n of Vn is stored in Tvar. cptcovage =number of age covariate. Tage gives the position of age. cptcovprod= number of products.
8177: If model=V1+V1*age then Tvar[1]=1 Tvar[2]=1 cptcovage=1 Tage[1]=2 cptcovprod=0*/
1.225 brouard 8178:
1.136 brouard 8179: /* printf("tvar1=%d tvar2=%d tvar3=%d cptcovage=%d Tage=%d",Tvar[1],Tvar[2],Tvar[3],cptcovage,Tage[1]);
1.225 brouard 8180: printf("cptcovprod=%d ", cptcovprod);
8181: fprintf(ficlog,"cptcovprod=%d ", cptcovprod);
8182: scanf("%d ",i);*/
8183:
8184:
1.230 brouard 8185: /* Until here, decodemodel knows only the grammar (simple, product, age*) of the model but not what kind
8186: of variable (dummy vs quantitative, fixed vs time varying) is behind. But we know the # of each. */
1.226 brouard 8187: /* ncovcol= 1, nqv=1 | ntv=2, nqtv= 1 = 5 possible variables data: 2 fixed 3, varying
8188: model= V5 + V4 +V3 + V4*V3 + V5*age + V2 + V1*V2 + V1*age + V5*age, V1 is not used saving its place
8189: k = 1 2 3 4 5 6 7 8 9
8190: Tvar[k]= 5 4 3 1+1+2+1+1=6 5 2 7 1 5
8191: Typevar[k]= 0 0 0 2 1 0 2 1 1
1.227 brouard 8192: Fixed[k] 1 1 1 1 3 0 0 or 2 2 3
8193: Dummy[k] 1 0 0 0 3 1 1 2 3
8194: Tmodelind[combination of covar]=k;
1.225 brouard 8195: */
8196: /* Dispatching between quantitative and time varying covariates */
1.226 brouard 8197: /* If Tvar[k] >ncovcol it is a product */
1.225 brouard 8198: /* 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 8199: /* Computing effective variables, ie used by the model, that is from the cptcovt variables */
1.227 brouard 8200: printf("Model=%s\n\
8201: Typevar: 0 for simple covariate (dummy, quantitative, fixed or varying), 1 for age product, 2 for product \n\
8202: Fixed[k] 0=fixed (product or simple), 1 varying, 2 fixed with age product, 3 varying with age product \n\
8203: 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);
8204: fprintf(ficlog,"Model=%s\n\
8205: Typevar: 0 for simple covariate (dummy, quantitative, fixed or varying), 1 for age product, 2 for product \n\
8206: Fixed[k] 0=fixed (product or simple), 1 varying, 2 fixed with age product, 3 varying with age product \n\
8207: 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);
8208:
1.234 ! brouard 8209: 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 */
! 8210: if (Tvar[k] <=ncovcol && Typevar[k]==0 ){ /* Simple fixed dummy (<=ncovcol) covariates */
1.227 brouard 8211: Fixed[k]= 0;
8212: Dummy[k]= 0;
1.225 brouard 8213: ncoveff++;
1.232 brouard 8214: ncovf++;
1.234 ! brouard 8215: nsd++;
! 8216: modell[k].maintype= FTYPE;
! 8217: TvarsD[nsd]=Tvar[k];
! 8218: TvarsDind[nsd]=k;
! 8219: TvarF[ncovf]=Tvar[k];
! 8220: TvarFind[ncovf]=k;
! 8221: TvarFD[ncoveff]=Tvar[k]; /* TvarFD[1]=V1 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
! 8222: TvarFDind[ncoveff]=k; /* TvarFDind[1]=9 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
! 8223: }else if( Tvar[k] <=ncovcol && Typevar[k]==2){ /* Product of fixed dummy (<=ncovcol) covariates */
! 8224: Fixed[k]= 0;
! 8225: Dummy[k]= 0;
! 8226: ncoveff++;
! 8227: ncovf++;
! 8228: modell[k].maintype= FTYPE;
! 8229: TvarF[ncovf]=Tvar[k];
! 8230: TvarFind[ncovf]=k;
1.230 brouard 8231: TvarFD[ncoveff]=Tvar[k]; /* TvarFD[1]=V1 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
1.231 brouard 8232: TvarFDind[ncoveff]=k; /* TvarFDind[1]=9 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
8233: }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 8234: Fixed[k]= 0;
8235: Dummy[k]= 1;
1.230 brouard 8236: nqfveff++;
1.234 ! brouard 8237: modell[k].maintype= FTYPE;
! 8238: modell[k].subtype= FQ;
! 8239: nsq++;
! 8240: TvarsQ[nsq]=Tvar[k];
! 8241: TvarsQind[nsq]=k;
1.232 brouard 8242: ncovf++;
1.234 ! brouard 8243: TvarF[ncovf]=Tvar[k];
! 8244: TvarFind[ncovf]=k;
1.231 brouard 8245: 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 8246: TvarFQind[nqfveff]=k; /* TvarFQind[1]=6 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ /* Only simple fixed quantitative variable */
1.234 ! brouard 8247: }else if( Tvar[k] <=ncovcol+nqv+ntv && Typevar[k]==0){/* Only simple time varying variables */
1.227 brouard 8248: Fixed[k]= 1;
8249: Dummy[k]= 0;
1.225 brouard 8250: ntveff++; /* Only simple time varying dummy variable */
1.234 ! brouard 8251: modell[k].maintype= VTYPE;
! 8252: modell[k].subtype= VD;
! 8253: nsd++;
! 8254: TvarsD[nsd]=Tvar[k];
! 8255: TvarsDind[nsd]=k;
! 8256: ncovv++; /* Only simple time varying variables */
! 8257: TvarV[ncovv]=Tvar[k];
! 8258: TvarVind[ncovv]=k;
1.231 brouard 8259: 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 */
8260: 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 8261: 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);
8262: printf("Quasi TmodelInvind[%d]=%d\n",k,Tvar[k]- ncovcol-nqv);
1.231 brouard 8263: }else if( Tvar[k] <=ncovcol+nqv+ntv+nqtv && Typevar[k]==0){ /* Only simple time varying quantitative variable V5*/
1.234 ! brouard 8264: Fixed[k]= 1;
! 8265: Dummy[k]= 1;
! 8266: nqtveff++;
! 8267: modell[k].maintype= VTYPE;
! 8268: modell[k].subtype= VQ;
! 8269: ncovv++; /* Only simple time varying variables */
! 8270: nsq++;
! 8271: TvarsQ[nsq]=Tvar[k];
! 8272: TvarsQind[nsq]=k;
! 8273: TvarV[ncovv]=Tvar[k];
! 8274: TvarVind[ncovv]=k;
1.231 brouard 8275: 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 */
8276: 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 8277: TmodelInvQind[nqtveff]=Tvar[k]- ncovcol-nqv-ntv;/* Only simple time varying quantitative variable */
! 8278: /* Tmodeliqind[k]=nqtveff;/\* Only simple time varying quantitative variable *\/ */
! 8279: 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 8280: printf("Quasi TmodelInvQind[%d]=%d\n",k,Tvar[k]- ncovcol-nqv-ntv);
1.227 brouard 8281: }else if (Typevar[k] == 1) { /* product with age */
1.234 ! brouard 8282: ncova++;
! 8283: TvarA[ncova]=Tvar[k];
! 8284: TvarAind[ncova]=k;
1.231 brouard 8285: if (Tvar[k] <=ncovcol ){ /* Product age with fixed dummy covariatee */
1.234 ! brouard 8286: Fixed[k]= 2;
! 8287: Dummy[k]= 2;
! 8288: modell[k].maintype= ATYPE;
! 8289: modell[k].subtype= APFD;
! 8290: /* ncoveff++; */
1.227 brouard 8291: }else if( Tvar[k] <=ncovcol+nqv) { /* Remind that product Vn*Vm are added in k*/
1.234 ! brouard 8292: Fixed[k]= 2;
! 8293: Dummy[k]= 3;
! 8294: modell[k].maintype= ATYPE;
! 8295: modell[k].subtype= APFQ; /* Product age * fixed quantitative */
! 8296: /* nqfveff++; /\* Only simple fixed quantitative variable *\/ */
1.227 brouard 8297: }else if( Tvar[k] <=ncovcol+nqv+ntv ){
1.234 ! brouard 8298: Fixed[k]= 3;
! 8299: Dummy[k]= 2;
! 8300: modell[k].maintype= ATYPE;
! 8301: modell[k].subtype= APVD; /* Product age * varying dummy */
! 8302: /* ntveff++; /\* Only simple time varying dummy variable *\/ */
1.227 brouard 8303: }else if( Tvar[k] <=ncovcol+nqv+ntv+nqtv){
1.234 ! brouard 8304: Fixed[k]= 3;
! 8305: Dummy[k]= 3;
! 8306: modell[k].maintype= ATYPE;
! 8307: modell[k].subtype= APVQ; /* Product age * varying quantitative */
! 8308: /* nqtveff++;/\* Only simple time varying quantitative variable *\/ */
1.227 brouard 8309: }
8310: }else if (Typevar[k] == 2) { /* product without age */
8311: k1=Tposprod[k];
8312: if(Tvard[k1][1] <=ncovcol){
1.234 ! brouard 8313: if(Tvard[k1][2] <=ncovcol){
! 8314: Fixed[k]= 1;
! 8315: Dummy[k]= 0;
! 8316: modell[k].maintype= FTYPE;
! 8317: modell[k].subtype= FPDD; /* Product fixed dummy * fixed dummy */
! 8318: ncovf++; /* Fixed variables without age */
! 8319: TvarF[ncovf]=Tvar[k];
! 8320: TvarFind[ncovf]=k;
! 8321: }else if(Tvard[k1][2] <=ncovcol+nqv){
! 8322: Fixed[k]= 0; /* or 2 ?*/
! 8323: Dummy[k]= 1;
! 8324: modell[k].maintype= FTYPE;
! 8325: modell[k].subtype= FPDQ; /* Product fixed dummy * fixed quantitative */
! 8326: ncovf++; /* Varying variables without age */
! 8327: TvarF[ncovf]=Tvar[k];
! 8328: TvarFind[ncovf]=k;
! 8329: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv){
! 8330: Fixed[k]= 1;
! 8331: Dummy[k]= 0;
! 8332: modell[k].maintype= VTYPE;
! 8333: modell[k].subtype= VPDD; /* Product fixed dummy * varying dummy */
! 8334: ncovv++; /* Varying variables without age */
! 8335: TvarV[ncovv]=Tvar[k];
! 8336: TvarVind[ncovv]=k;
! 8337: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv+nqtv){
! 8338: Fixed[k]= 1;
! 8339: Dummy[k]= 1;
! 8340: modell[k].maintype= VTYPE;
! 8341: modell[k].subtype= VPDQ; /* Product fixed dummy * varying quantitative */
! 8342: ncovv++; /* Varying variables without age */
! 8343: TvarV[ncovv]=Tvar[k];
! 8344: TvarVind[ncovv]=k;
! 8345: }
1.227 brouard 8346: }else if(Tvard[k1][1] <=ncovcol+nqv){
1.234 ! brouard 8347: if(Tvard[k1][2] <=ncovcol){
! 8348: Fixed[k]= 0; /* or 2 ?*/
! 8349: Dummy[k]= 1;
! 8350: modell[k].maintype= FTYPE;
! 8351: modell[k].subtype= FPDQ; /* Product fixed quantitative * fixed dummy */
! 8352: ncovf++; /* Fixed variables without age */
! 8353: TvarF[ncovf]=Tvar[k];
! 8354: TvarFind[ncovf]=k;
! 8355: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv){
! 8356: Fixed[k]= 1;
! 8357: Dummy[k]= 1;
! 8358: modell[k].maintype= VTYPE;
! 8359: modell[k].subtype= VPDQ; /* Product fixed quantitative * varying dummy */
! 8360: ncovv++; /* Varying variables without age */
! 8361: TvarV[ncovv]=Tvar[k];
! 8362: TvarVind[ncovv]=k;
! 8363: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv+nqtv){
! 8364: Fixed[k]= 1;
! 8365: Dummy[k]= 1;
! 8366: modell[k].maintype= VTYPE;
! 8367: modell[k].subtype= VPQQ; /* Product fixed quantitative * varying quantitative */
! 8368: ncovv++; /* Varying variables without age */
! 8369: TvarV[ncovv]=Tvar[k];
! 8370: TvarVind[ncovv]=k;
! 8371: ncovv++; /* Varying variables without age */
! 8372: TvarV[ncovv]=Tvar[k];
! 8373: TvarVind[ncovv]=k;
! 8374: }
1.227 brouard 8375: }else if(Tvard[k1][1] <=ncovcol+nqv+ntv){
1.234 ! brouard 8376: if(Tvard[k1][2] <=ncovcol){
! 8377: Fixed[k]= 1;
! 8378: Dummy[k]= 1;
! 8379: modell[k].maintype= VTYPE;
! 8380: modell[k].subtype= VPDD; /* Product time varying dummy * fixed dummy */
! 8381: ncovv++; /* Varying variables without age */
! 8382: TvarV[ncovv]=Tvar[k];
! 8383: TvarVind[ncovv]=k;
! 8384: }else if(Tvard[k1][2] <=ncovcol+nqv){
! 8385: Fixed[k]= 1;
! 8386: Dummy[k]= 1;
! 8387: modell[k].maintype= VTYPE;
! 8388: modell[k].subtype= VPDQ; /* Product time varying dummy * fixed quantitative */
! 8389: ncovv++; /* Varying variables without age */
! 8390: TvarV[ncovv]=Tvar[k];
! 8391: TvarVind[ncovv]=k;
! 8392: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv){
! 8393: Fixed[k]= 1;
! 8394: Dummy[k]= 0;
! 8395: modell[k].maintype= VTYPE;
! 8396: modell[k].subtype= VPDD; /* Product time varying dummy * time varying dummy */
! 8397: ncovv++; /* Varying variables without age */
! 8398: TvarV[ncovv]=Tvar[k];
! 8399: TvarVind[ncovv]=k;
! 8400: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv+nqtv){
! 8401: Fixed[k]= 1;
! 8402: Dummy[k]= 1;
! 8403: modell[k].maintype= VTYPE;
! 8404: modell[k].subtype= VPDQ; /* Product time varying dummy * time varying quantitative */
! 8405: ncovv++; /* Varying variables without age */
! 8406: TvarV[ncovv]=Tvar[k];
! 8407: TvarVind[ncovv]=k;
! 8408: }
1.227 brouard 8409: }else if(Tvard[k1][1] <=ncovcol+nqv+ntv+nqtv){
1.234 ! brouard 8410: if(Tvard[k1][2] <=ncovcol){
! 8411: Fixed[k]= 1;
! 8412: Dummy[k]= 1;
! 8413: modell[k].maintype= VTYPE;
! 8414: modell[k].subtype= VPDQ; /* Product time varying quantitative * fixed dummy */
! 8415: ncovv++; /* Varying variables without age */
! 8416: TvarV[ncovv]=Tvar[k];
! 8417: TvarVind[ncovv]=k;
! 8418: }else if(Tvard[k1][2] <=ncovcol+nqv){
! 8419: Fixed[k]= 1;
! 8420: Dummy[k]= 1;
! 8421: modell[k].maintype= VTYPE;
! 8422: modell[k].subtype= VPQQ; /* Product time varying quantitative * fixed quantitative */
! 8423: ncovv++; /* Varying variables without age */
! 8424: TvarV[ncovv]=Tvar[k];
! 8425: TvarVind[ncovv]=k;
! 8426: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv){
! 8427: Fixed[k]= 1;
! 8428: Dummy[k]= 1;
! 8429: modell[k].maintype= VTYPE;
! 8430: modell[k].subtype= VPDQ; /* Product time varying quantitative * time varying dummy */
! 8431: ncovv++; /* Varying variables without age */
! 8432: TvarV[ncovv]=Tvar[k];
! 8433: TvarVind[ncovv]=k;
! 8434: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv+nqtv){
! 8435: Fixed[k]= 1;
! 8436: Dummy[k]= 1;
! 8437: modell[k].maintype= VTYPE;
! 8438: modell[k].subtype= VPQQ; /* Product time varying quantitative * time varying quantitative */
! 8439: ncovv++; /* Varying variables without age */
! 8440: TvarV[ncovv]=Tvar[k];
! 8441: TvarVind[ncovv]=k;
! 8442: }
1.227 brouard 8443: }else{
1.234 ! brouard 8444: printf("Error unknown type of covariate: Tvard[%d][1]=%d,Tvard[%d][2]=%d\n",k1,Tvard[k1][1],k1,Tvard[k1][2]);
! 8445: fprintf(ficlog,"Error unknown type of covariate: Tvard[%d][1]=%d,Tvard[%d][2]=%d\n",k1,Tvard[k1][1],k1,Tvard[k1][2]);
1.226 brouard 8446: } /* end k1 */
1.225 brouard 8447: }else{
1.226 brouard 8448: printf("Error, current version can't treat for performance reasons, Tvar[%d]=%d, Typevar[%d]=%d\n", k, Tvar[k], k, Typevar[k]);
8449: 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 8450: }
1.227 brouard 8451: 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 8452: printf(" modell[%d].maintype=%d, modell[%d].subtype=%d\n",k,modell[k].maintype,k,modell[k].subtype);
1.227 brouard 8453: 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]);
8454: }
8455: /* Searching for doublons in the model */
8456: for(k1=1; k1<= cptcovt;k1++){
8457: for(k2=1; k2 <k1;k2++){
8458: if((Typevar[k1]==Typevar[k2]) && (Fixed[Tvar[k1]]==Fixed[Tvar[k2]]) && (Dummy[Tvar[k1]]==Dummy[Tvar[k2]] )){
1.234 ! brouard 8459: if((Typevar[k1] == 0 || Typevar[k1] == 1)){ /* Simple or age product */
! 8460: if(Tvar[k1]==Tvar[k2]){
! 8461: 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]]);
! 8462: 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);
! 8463: return(1);
! 8464: }
! 8465: }else if (Typevar[k1] ==2){
! 8466: k3=Tposprod[k1];
! 8467: k4=Tposprod[k2];
! 8468: 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])) ){
! 8469: 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]]);
! 8470: 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);
! 8471: return(1);
! 8472: }
! 8473: }
1.227 brouard 8474: }
8475: }
1.225 brouard 8476: }
8477: printf("ncoveff=%d, nqfveff=%d, ntveff=%d, nqtveff=%d, cptcovn=%d\n",ncoveff,nqfveff,ntveff,nqtveff,cptcovn);
8478: fprintf(ficlog,"ncoveff=%d, nqfveff=%d, ntveff=%d, nqtveff=%d, cptcovn=%d\n",ncoveff,nqfveff,ntveff,nqtveff,cptcovn);
1.234 ! brouard 8479: printf("ncovf=%d, ncovv=%d, ncova=%d, nsd=%d, nsq=%d\n",ncovf,ncovv,ncova,nsd,nsq);
! 8480: fprintf(ficlog,"ncovf=%d, ncovv=%d, ncova=%d, nsd=%d, nsq=%d\n",ncovf,ncovv,ncova,nsd, nsq);
1.137 brouard 8481: return (0); /* with covar[new additional covariate if product] and Tage if age */
1.164 brouard 8482: /*endread:*/
1.225 brouard 8483: printf("Exiting decodemodel: ");
8484: return (1);
1.136 brouard 8485: }
8486:
1.169 brouard 8487: int calandcheckages(int imx, int maxwav, double *agemin, double *agemax, int *nberr, int *nbwarn )
1.136 brouard 8488: {
8489: int i, m;
1.218 brouard 8490: int firstone=0;
8491:
1.136 brouard 8492: for (i=1; i<=imx; i++) {
8493: for(m=2; (m<= maxwav); m++) {
8494: if (((int)mint[m][i]== 99) && (s[m][i] <= nlstate)){
8495: anint[m][i]=9999;
1.216 brouard 8496: if (s[m][i] != -2) /* Keeping initial status of unknown vital status */
8497: s[m][i]=-1;
1.136 brouard 8498: }
8499: if((int)moisdc[i]==99 && (int)andc[i]==9999 && s[m][i]>nlstate){
1.169 brouard 8500: *nberr = *nberr + 1;
1.218 brouard 8501: if(firstone == 0){
8502: firstone=1;
8503: 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);
8504: }
8505: 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 8506: s[m][i]=-1;
8507: }
8508: if((int)moisdc[i]==99 && (int)andc[i]!=9999 && s[m][i]>nlstate){
1.169 brouard 8509: (*nberr)++;
1.136 brouard 8510: 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]);
8511: 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]);
8512: s[m][i]=-1; /* We prefer to skip it (and to skip it in version 0.8a1 too */
8513: }
8514: }
8515: }
8516:
8517: for (i=1; i<=imx; i++) {
8518: agedc[i]=(moisdc[i]/12.+andc[i])-(moisnais[i]/12.+annais[i]);
8519: for(m=firstpass; (m<= lastpass); m++){
1.214 brouard 8520: 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 8521: if (s[m][i] >= nlstate+1) {
1.169 brouard 8522: if(agedc[i]>0){
8523: if((int)moisdc[i]!=99 && (int)andc[i]!=9999){
1.136 brouard 8524: agev[m][i]=agedc[i];
1.214 brouard 8525: /*if(moisdc[i]==99 && andc[i]==9999) s[m][i]=-1;*/
1.169 brouard 8526: }else {
1.136 brouard 8527: if ((int)andc[i]!=9999){
8528: nbwarn++;
8529: printf("Warning negative age at death: %ld line:%d\n",num[i],i);
8530: fprintf(ficlog,"Warning negative age at death: %ld line:%d\n",num[i],i);
8531: agev[m][i]=-1;
8532: }
8533: }
1.169 brouard 8534: } /* agedc > 0 */
1.214 brouard 8535: } /* end if */
1.136 brouard 8536: else if(s[m][i] !=9){ /* Standard case, age in fractional
8537: years but with the precision of a month */
8538: agev[m][i]=(mint[m][i]/12.+1./24.+anint[m][i])-(moisnais[i]/12.+1./24.+annais[i]);
8539: if((int)mint[m][i]==99 || (int)anint[m][i]==9999)
8540: agev[m][i]=1;
8541: else if(agev[m][i] < *agemin){
8542: *agemin=agev[m][i];
8543: printf(" Min anint[%d][%d]=%.2f annais[%d]=%.2f, agemin=%.2f\n",m,i,anint[m][i], i,annais[i], *agemin);
8544: }
8545: else if(agev[m][i] >*agemax){
8546: *agemax=agev[m][i];
1.156 brouard 8547: /* printf(" Max anint[%d][%d]=%.0f annais[%d]=%.0f, agemax=%.2f\n",m,i,anint[m][i], i,annais[i], *agemax);*/
1.136 brouard 8548: }
8549: /*agev[m][i]=anint[m][i]-annais[i];*/
8550: /* agev[m][i] = age[i]+2*m;*/
1.214 brouard 8551: } /* en if 9*/
1.136 brouard 8552: else { /* =9 */
1.214 brouard 8553: /* printf("Debug num[%d]=%ld s[%d][%d]=%d\n",i,num[i], m,i, s[m][i]); */
1.136 brouard 8554: agev[m][i]=1;
8555: s[m][i]=-1;
8556: }
8557: }
1.214 brouard 8558: else if(s[m][i]==0) /*= 0 Unknown */
1.136 brouard 8559: agev[m][i]=1;
1.214 brouard 8560: else{
8561: printf("Warning, num[%d]=%ld, s[%d][%d]=%d\n", i, num[i], m, i,s[m][i]);
8562: fprintf(ficlog, "Warning, num[%d]=%ld, s[%d][%d]=%d\n", i, num[i], m, i,s[m][i]);
8563: agev[m][i]=0;
8564: }
8565: } /* End for lastpass */
8566: }
1.136 brouard 8567:
8568: for (i=1; i<=imx; i++) {
8569: for(m=firstpass; (m<=lastpass); m++){
8570: if (s[m][i] > (nlstate+ndeath)) {
1.169 brouard 8571: (*nberr)++;
1.136 brouard 8572: 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);
8573: 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);
8574: return 1;
8575: }
8576: }
8577: }
8578:
8579: /*for (i=1; i<=imx; i++){
8580: for (m=firstpass; (m<lastpass); m++){
8581: printf("%ld %d %.lf %d %d\n", num[i],(covar[1][i]),agev[m][i],s[m][i],s[m+1][i]);
8582: }
8583:
8584: }*/
8585:
8586:
1.139 brouard 8587: printf("Total number of individuals= %d, Agemin = %.2f, Agemax= %.2f\n\n", imx, *agemin, *agemax);
8588: fprintf(ficlog,"Total number of individuals= %d, Agemin = %.2f, Agemax= %.2f\n\n", imx, *agemin, *agemax);
1.136 brouard 8589:
8590: return (0);
1.164 brouard 8591: /* endread:*/
1.136 brouard 8592: printf("Exiting calandcheckages: ");
8593: return (1);
8594: }
8595:
1.172 brouard 8596: #if defined(_MSC_VER)
8597: /*printf("Visual C++ compiler: %s \n;", _MSC_FULL_VER);*/
8598: /*fprintf(ficlog, "Visual C++ compiler: %s \n;", _MSC_FULL_VER);*/
8599: //#include "stdafx.h"
8600: //#include <stdio.h>
8601: //#include <tchar.h>
8602: //#include <windows.h>
8603: //#include <iostream>
8604: typedef BOOL(WINAPI *LPFN_ISWOW64PROCESS) (HANDLE, PBOOL);
8605:
8606: LPFN_ISWOW64PROCESS fnIsWow64Process;
8607:
8608: BOOL IsWow64()
8609: {
8610: BOOL bIsWow64 = FALSE;
8611:
8612: //typedef BOOL (APIENTRY *LPFN_ISWOW64PROCESS)
8613: // (HANDLE, PBOOL);
8614:
8615: //LPFN_ISWOW64PROCESS fnIsWow64Process;
8616:
8617: HMODULE module = GetModuleHandle(_T("kernel32"));
8618: const char funcName[] = "IsWow64Process";
8619: fnIsWow64Process = (LPFN_ISWOW64PROCESS)
8620: GetProcAddress(module, funcName);
8621:
8622: if (NULL != fnIsWow64Process)
8623: {
8624: if (!fnIsWow64Process(GetCurrentProcess(),
8625: &bIsWow64))
8626: //throw std::exception("Unknown error");
8627: printf("Unknown error\n");
8628: }
8629: return bIsWow64 != FALSE;
8630: }
8631: #endif
1.177 brouard 8632:
1.191 brouard 8633: void syscompilerinfo(int logged)
1.167 brouard 8634: {
8635: /* #include "syscompilerinfo.h"*/
1.185 brouard 8636: /* command line Intel compiler 32bit windows, XP compatible:*/
8637: /* /GS /W3 /Gy
8638: /Zc:wchar_t /Zi /O2 /Fd"Release\vc120.pdb" /D "WIN32" /D "NDEBUG" /D
8639: "_CONSOLE" /D "_LIB" /D "_USING_V110_SDK71_" /D "_UNICODE" /D
8640: "UNICODE" /Qipo /Zc:forScope /Gd /Oi /MT /Fa"Release\" /EHsc /nologo
1.186 brouard 8641: /Fo"Release\" /Qprof-dir "Release\" /Fp"Release\IMaCh.pch"
8642: */
8643: /* 64 bits */
1.185 brouard 8644: /*
8645: /GS /W3 /Gy
8646: /Zc:wchar_t /Zi /O2 /Fd"x64\Release\vc120.pdb" /D "WIN32" /D "NDEBUG"
8647: /D "_CONSOLE" /D "_LIB" /D "_UNICODE" /D "UNICODE" /Qipo /Zc:forScope
8648: /Oi /MD /Fa"x64\Release\" /EHsc /nologo /Fo"x64\Release\" /Qprof-dir
8649: "x64\Release\" /Fp"x64\Release\IMaCh.pch" */
8650: /* Optimization are useless and O3 is slower than O2 */
8651: /*
8652: /GS /W3 /Gy /Zc:wchar_t /Zi /O3 /Fd"x64\Release\vc120.pdb" /D "WIN32"
8653: /D "NDEBUG" /D "_CONSOLE" /D "_LIB" /D "_UNICODE" /D "UNICODE" /Qipo
8654: /Zc:forScope /Oi /MD /Fa"x64\Release\" /EHsc /nologo /Qparallel
8655: /Fo"x64\Release\" /Qprof-dir "x64\Release\" /Fp"x64\Release\IMaCh.pch"
8656: */
1.186 brouard 8657: /* Link is */ /* /OUT:"visual studio
1.185 brouard 8658: 2013\Projects\IMaCh\Release\IMaCh.exe" /MANIFEST /NXCOMPAT
8659: /PDB:"visual studio
8660: 2013\Projects\IMaCh\Release\IMaCh.pdb" /DYNAMICBASE
8661: "kernel32.lib" "user32.lib" "gdi32.lib" "winspool.lib"
8662: "comdlg32.lib" "advapi32.lib" "shell32.lib" "ole32.lib"
8663: "oleaut32.lib" "uuid.lib" "odbc32.lib" "odbccp32.lib"
8664: /MACHINE:X86 /OPT:REF /SAFESEH /INCREMENTAL:NO
8665: /SUBSYSTEM:CONSOLE",5.01" /MANIFESTUAC:"level='asInvoker'
8666: uiAccess='false'"
8667: /ManifestFile:"Release\IMaCh.exe.intermediate.manifest" /OPT:ICF
8668: /NOLOGO /TLBID:1
8669: */
1.177 brouard 8670: #if defined __INTEL_COMPILER
1.178 brouard 8671: #if defined(__GNUC__)
8672: struct utsname sysInfo; /* For Intel on Linux and OS/X */
8673: #endif
1.177 brouard 8674: #elif defined(__GNUC__)
1.179 brouard 8675: #ifndef __APPLE__
1.174 brouard 8676: #include <gnu/libc-version.h> /* Only on gnu */
1.179 brouard 8677: #endif
1.177 brouard 8678: struct utsname sysInfo;
1.178 brouard 8679: int cross = CROSS;
8680: if (cross){
8681: printf("Cross-");
1.191 brouard 8682: if(logged) fprintf(ficlog, "Cross-");
1.178 brouard 8683: }
1.174 brouard 8684: #endif
8685:
1.171 brouard 8686: #include <stdint.h>
1.178 brouard 8687:
1.191 brouard 8688: printf("Compiled with:");if(logged)fprintf(ficlog,"Compiled with:");
1.169 brouard 8689: #if defined(__clang__)
1.191 brouard 8690: printf(" Clang/LLVM");if(logged)fprintf(ficlog," Clang/LLVM"); /* Clang/LLVM. ---------------------------------------------- */
1.169 brouard 8691: #endif
8692: #if defined(__ICC) || defined(__INTEL_COMPILER)
1.191 brouard 8693: printf(" Intel ICC/ICPC");if(logged)fprintf(ficlog," Intel ICC/ICPC");/* Intel ICC/ICPC. ------------------------------------------ */
1.169 brouard 8694: #endif
8695: #if defined(__GNUC__) || defined(__GNUG__)
1.191 brouard 8696: printf(" GNU GCC/G++");if(logged)fprintf(ficlog," GNU GCC/G++");/* GNU GCC/G++. --------------------------------------------- */
1.169 brouard 8697: #endif
8698: #if defined(__HP_cc) || defined(__HP_aCC)
1.191 brouard 8699: printf(" Hewlett-Packard C/aC++");if(logged)fprintf(fcilog," Hewlett-Packard C/aC++"); /* Hewlett-Packard C/aC++. ---------------------------------- */
1.169 brouard 8700: #endif
8701: #if defined(__IBMC__) || defined(__IBMCPP__)
1.191 brouard 8702: printf(" IBM XL C/C++"); if(logged) fprintf(ficlog," IBM XL C/C++");/* IBM XL C/C++. -------------------------------------------- */
1.169 brouard 8703: #endif
8704: #if defined(_MSC_VER)
1.191 brouard 8705: printf(" Microsoft Visual Studio");if(logged)fprintf(ficlog," Microsoft Visual Studio");/* Microsoft Visual Studio. --------------------------------- */
1.169 brouard 8706: #endif
8707: #if defined(__PGI)
1.191 brouard 8708: printf(" Portland Group PGCC/PGCPP");if(logged) fprintf(ficlog," Portland Group PGCC/PGCPP");/* Portland Group PGCC/PGCPP. ------------------------------- */
1.169 brouard 8709: #endif
8710: #if defined(__SUNPRO_C) || defined(__SUNPRO_CC)
1.191 brouard 8711: printf(" Oracle Solaris Studio");if(logged)fprintf(ficlog," Oracle Solaris Studio\n");/* Oracle Solaris Studio. ----------------------------------- */
1.167 brouard 8712: #endif
1.191 brouard 8713: printf(" for "); if (logged) fprintf(ficlog, " for ");
1.169 brouard 8714:
1.167 brouard 8715: // http://stackoverflow.com/questions/4605842/how-to-identify-platform-compiler-from-preprocessor-macros
8716: #ifdef _WIN32 // note the underscore: without it, it's not msdn official!
8717: // Windows (x64 and x86)
1.191 brouard 8718: printf("Windows (x64 and x86) ");if(logged) fprintf(ficlog,"Windows (x64 and x86) ");
1.167 brouard 8719: #elif __unix__ // all unices, not all compilers
8720: // Unix
1.191 brouard 8721: printf("Unix ");if(logged) fprintf(ficlog,"Unix ");
1.167 brouard 8722: #elif __linux__
8723: // linux
1.191 brouard 8724: printf("linux ");if(logged) fprintf(ficlog,"linux ");
1.167 brouard 8725: #elif __APPLE__
1.174 brouard 8726: // Mac OS, not sure if this is covered by __posix__ and/or __unix__ though..
1.191 brouard 8727: printf("Mac OS ");if(logged) fprintf(ficlog,"Mac OS ");
1.167 brouard 8728: #endif
8729:
8730: /* __MINGW32__ */
8731: /* __CYGWIN__ */
8732: /* __MINGW64__ */
8733: // http://msdn.microsoft.com/en-us/library/b0084kay.aspx
8734: /* _MSC_VER //the Visual C++ compiler is 17.00.51106.1, the _MSC_VER macro evaluates to 1700. Type cl /? */
8735: /* _MSC_FULL_VER //the Visual C++ compiler is 15.00.20706.01, the _MSC_FULL_VER macro evaluates to 150020706 */
8736: /* _WIN64 // Defined for applications for Win64. */
8737: /* _M_X64 // Defined for compilations that target x64 processors. */
8738: /* _DEBUG // Defined when you compile with /LDd, /MDd, and /MTd. */
1.171 brouard 8739:
1.167 brouard 8740: #if UINTPTR_MAX == 0xffffffff
1.191 brouard 8741: printf(" 32-bit"); if(logged) fprintf(ficlog," 32-bit");/* 32-bit */
1.167 brouard 8742: #elif UINTPTR_MAX == 0xffffffffffffffff
1.191 brouard 8743: printf(" 64-bit"); if(logged) fprintf(ficlog," 64-bit");/* 64-bit */
1.167 brouard 8744: #else
1.191 brouard 8745: printf(" wtf-bit"); if(logged) fprintf(ficlog," wtf-bit");/* wtf */
1.167 brouard 8746: #endif
8747:
1.169 brouard 8748: #if defined(__GNUC__)
8749: # if defined(__GNUC_PATCHLEVEL__)
8750: # define __GNUC_VERSION__ (__GNUC__ * 10000 \
8751: + __GNUC_MINOR__ * 100 \
8752: + __GNUC_PATCHLEVEL__)
8753: # else
8754: # define __GNUC_VERSION__ (__GNUC__ * 10000 \
8755: + __GNUC_MINOR__ * 100)
8756: # endif
1.174 brouard 8757: printf(" using GNU C version %d.\n", __GNUC_VERSION__);
1.191 brouard 8758: if(logged) fprintf(ficlog, " using GNU C version %d.\n", __GNUC_VERSION__);
1.176 brouard 8759:
8760: if (uname(&sysInfo) != -1) {
8761: printf("Running on: %s %s %s %s %s\n",sysInfo.sysname, sysInfo.nodename, sysInfo.release, sysInfo.version, sysInfo.machine);
1.191 brouard 8762: 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 8763: }
8764: else
8765: perror("uname() error");
1.179 brouard 8766: //#ifndef __INTEL_COMPILER
8767: #if !defined (__INTEL_COMPILER) && !defined(__APPLE__)
1.174 brouard 8768: printf("GNU libc version: %s\n", gnu_get_libc_version());
1.191 brouard 8769: if(logged) fprintf(ficlog,"GNU libc version: %s\n", gnu_get_libc_version());
1.177 brouard 8770: #endif
1.169 brouard 8771: #endif
1.172 brouard 8772:
8773: // void main()
8774: // {
1.169 brouard 8775: #if defined(_MSC_VER)
1.174 brouard 8776: if (IsWow64()){
1.191 brouard 8777: printf("\nThe program (probably compiled for 32bit) is running under WOW64 (64bit) emulation.\n");
8778: if (logged) fprintf(ficlog, "\nThe program (probably compiled for 32bit) is running under WOW64 (64bit) emulation.\n");
1.174 brouard 8779: }
8780: else{
1.191 brouard 8781: printf("\nThe program is not running under WOW64 (i.e probably on a 64bit Windows).\n");
8782: if (logged) fprintf(ficlog, "\nThe programm is not running under WOW64 (i.e probably on a 64bit Windows).\n");
1.174 brouard 8783: }
1.172 brouard 8784: // printf("\nPress Enter to continue...");
8785: // getchar();
8786: // }
8787:
1.169 brouard 8788: #endif
8789:
1.167 brouard 8790:
1.219 brouard 8791: }
1.136 brouard 8792:
1.219 brouard 8793: int prevalence_limit(double *p, double **prlim, double ageminpar, double agemaxpar, double ftolpl, int *ncvyearp){
1.180 brouard 8794: /*--------------- Prevalence limit (period or stable prevalence) --------------*/
8795: int i, j, k, i1 ;
1.202 brouard 8796: /* double ftolpl = 1.e-10; */
1.180 brouard 8797: double age, agebase, agelim;
1.203 brouard 8798: double tot;
1.180 brouard 8799:
1.202 brouard 8800: strcpy(filerespl,"PL_");
8801: strcat(filerespl,fileresu);
8802: if((ficrespl=fopen(filerespl,"w"))==NULL) {
8803: printf("Problem with period (stable) prevalence resultfile: %s\n", filerespl);return 1;
8804: fprintf(ficlog,"Problem with period (stable) prevalence resultfile: %s\n", filerespl);return 1;
8805: }
1.227 brouard 8806: printf("\nComputing period (stable) prevalence: result on file '%s' \n", filerespl);
8807: fprintf(ficlog,"\nComputing period (stable) prevalence: result on file '%s' \n", filerespl);
1.202 brouard 8808: pstamp(ficrespl);
1.203 brouard 8809: fprintf(ficrespl,"# Period (stable) prevalence. Precision given by ftolpl=%g \n", ftolpl);
1.202 brouard 8810: fprintf(ficrespl,"#Age ");
8811: for(i=1; i<=nlstate;i++) fprintf(ficrespl,"%d-%d ",i,i);
8812: fprintf(ficrespl,"\n");
1.180 brouard 8813:
1.219 brouard 8814: /* prlim=matrix(1,nlstate,1,nlstate);*/ /* back in main */
1.180 brouard 8815:
1.219 brouard 8816: agebase=ageminpar;
8817: agelim=agemaxpar;
1.180 brouard 8818:
1.227 brouard 8819: /* i1=pow(2,ncoveff); */
1.234 ! brouard 8820: i1=pow(2,cptcoveff); /* Number of combination of dummy covariates */
1.219 brouard 8821: if (cptcovn < 1){i1=1;}
1.180 brouard 8822:
1.220 brouard 8823: for(k=1; k<=i1;k++){
8824: /* for(cptcov=1,k=0;cptcov<=i1;cptcov++){ */
1.180 brouard 8825: /* for(cptcov=1,k=0;cptcov<=1;cptcov++){ */
1.219 brouard 8826: //for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){
1.220 brouard 8827: /* k=k+1; */
1.219 brouard 8828: /* to clean */
8829: //printf("cptcov=%d cptcod=%d codtab=%d\n",cptcov, cptcod,codtabm(cptcod,cptcov));
8830: fprintf(ficrespl,"#******");
8831: printf("#******");
8832: fprintf(ficlog,"#******");
1.227 brouard 8833: for(j=1;j<=cptcoveff ;j++) {/* all covariates */
8834: fprintf(ficrespl," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]); /* Here problem for varying dummy*/
1.219 brouard 8835: printf(" V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
8836: fprintf(ficlog," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
8837: }
8838: fprintf(ficrespl,"******\n");
8839: printf("******\n");
8840: fprintf(ficlog,"******\n");
1.227 brouard 8841: if(invalidvarcomb[k]){
8842: printf("\nCombination (%d) ignored because no case \n",k);
8843: fprintf(ficrespl,"#Combination (%d) ignored because no case \n",k);
8844: fprintf(ficlog,"\nCombination (%d) ignored because no case \n",k);
1.220 brouard 8845: continue;
1.227 brouard 8846: }
1.219 brouard 8847:
8848: fprintf(ficrespl,"#Age ");
1.227 brouard 8849: for(j=1;j<=cptcoveff;j++) {
1.219 brouard 8850: fprintf(ficrespl,"V%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
8851: }
8852: for(i=1; i<=nlstate;i++) fprintf(ficrespl," %d-%d ",i,i);
8853: fprintf(ficrespl,"Total Years_to_converge\n");
1.227 brouard 8854:
1.219 brouard 8855: for (age=agebase; age<=agelim; age++){
8856: /* for (age=agebase; age<=agebase; age++){ */
8857: prevalim(prlim, nlstate, p, age, oldm, savm, ftolpl, ncvyearp, k);
8858: fprintf(ficrespl,"%.0f ",age );
1.227 brouard 8859: for(j=1;j<=cptcoveff;j++)
8860: fprintf(ficrespl,"%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
1.219 brouard 8861: tot=0.;
8862: for(i=1; i<=nlstate;i++){
1.227 brouard 8863: tot += prlim[i][i];
8864: fprintf(ficrespl," %.5f", prlim[i][i]);
1.219 brouard 8865: }
8866: fprintf(ficrespl," %.3f %d\n", tot, *ncvyearp);
8867: } /* Age */
8868: /* was end of cptcod */
8869: } /* cptcov */
8870: return 0;
1.180 brouard 8871: }
8872:
1.218 brouard 8873: 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){
8874: /*--------------- Back Prevalence limit (period or stable prevalence) --------------*/
8875:
8876: /* Computes the back prevalence limit for any combination of covariate values
8877: * at any age between ageminpar and agemaxpar
8878: */
1.217 brouard 8879: int i, j, k, i1 ;
8880: /* double ftolpl = 1.e-10; */
8881: double age, agebase, agelim;
8882: double tot;
1.218 brouard 8883: /* double ***mobaverage; */
8884: /* double **dnewm, **doldm, **dsavm; /\* for use *\/ */
1.217 brouard 8885:
8886: strcpy(fileresplb,"PLB_");
8887: strcat(fileresplb,fileresu);
8888: if((ficresplb=fopen(fileresplb,"w"))==NULL) {
8889: printf("Problem with period (stable) back prevalence resultfile: %s\n", fileresplb);return 1;
8890: fprintf(ficlog,"Problem with period (stable) back prevalence resultfile: %s\n", fileresplb);return 1;
8891: }
8892: printf("Computing period (stable) back prevalence: result on file '%s' \n", fileresplb);
8893: fprintf(ficlog,"Computing period (stable) back prevalence: result on file '%s' \n", fileresplb);
8894: pstamp(ficresplb);
8895: fprintf(ficresplb,"# Period (stable) back prevalence. Precision given by ftolpl=%g \n", ftolpl);
8896: fprintf(ficresplb,"#Age ");
8897: for(i=1; i<=nlstate;i++) fprintf(ficresplb,"%d-%d ",i,i);
8898: fprintf(ficresplb,"\n");
8899:
1.218 brouard 8900:
8901: /* prlim=matrix(1,nlstate,1,nlstate);*/ /* back in main */
8902:
8903: agebase=ageminpar;
8904: agelim=agemaxpar;
8905:
8906:
1.227 brouard 8907: i1=pow(2,cptcoveff);
1.218 brouard 8908: if (cptcovn < 1){i1=1;}
1.227 brouard 8909:
8910: for(k=1; k<=i1;k++){
1.218 brouard 8911: //printf("cptcov=%d cptcod=%d codtab=%d\n",cptcov, cptcod,codtabm(cptcod,cptcov));
8912: fprintf(ficresplb,"#******");
8913: printf("#******");
8914: fprintf(ficlog,"#******");
1.227 brouard 8915: for(j=1;j<=cptcoveff ;j++) {/* all covariates */
1.218 brouard 8916: fprintf(ficresplb," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
8917: printf(" V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
8918: fprintf(ficlog," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
8919: }
8920: fprintf(ficresplb,"******\n");
8921: printf("******\n");
8922: fprintf(ficlog,"******\n");
1.227 brouard 8923: if(invalidvarcomb[k]){
8924: printf("\nCombination (%d) ignored because no cases \n",k);
8925: fprintf(ficresplb,"#Combination (%d) ignored because no cases \n",k);
8926: fprintf(ficlog,"\nCombination (%d) ignored because no cases \n",k);
8927: continue;
8928: }
1.218 brouard 8929:
8930: fprintf(ficresplb,"#Age ");
1.227 brouard 8931: for(j=1;j<=cptcoveff;j++) {
1.218 brouard 8932: fprintf(ficresplb,"V%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
8933: }
8934: for(i=1; i<=nlstate;i++) fprintf(ficresplb," %d-%d ",i,i);
8935: fprintf(ficresplb,"Total Years_to_converge\n");
8936:
8937:
8938: for (age=agebase; age<=agelim; age++){
8939: /* for (age=agebase; age<=agebase; age++){ */
8940: if(mobilavproj > 0){
8941: /* bprevalim(bprlim, mobaverage, nlstate, p, age, ageminpar, agemaxpar, oldm, savm, doldm, dsavm, ftolpl, ncvyearp, k); */
8942: /* bprevalim(bprlim, mobaverage, nlstate, p, age, oldm, savm, dnewm, doldm, dsavm, ftolpl, ncvyearp, k); */
1.227 brouard 8943: bprevalim(bprlim, mobaverage, nlstate, p, age, ftolpl, ncvyearp, k);
1.218 brouard 8944: }else if (mobilavproj == 0){
1.227 brouard 8945: 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);
8946: 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);
8947: exit(1);
1.218 brouard 8948: }else{
1.227 brouard 8949: /* bprevalim(bprlim, probs, nlstate, p, age, oldm, savm, dnewm, doldm, dsavm, ftolpl, ncvyearp, k); */
8950: bprevalim(bprlim, probs, nlstate, p, age, ftolpl, ncvyearp, k);
1.218 brouard 8951: }
8952: fprintf(ficresplb,"%.0f ",age );
1.227 brouard 8953: for(j=1;j<=cptcoveff;j++)
8954: fprintf(ficresplb,"%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
1.218 brouard 8955: tot=0.;
8956: for(i=1; i<=nlstate;i++){
1.227 brouard 8957: tot += bprlim[i][i];
8958: fprintf(ficresplb," %.5f", bprlim[i][i]);
1.218 brouard 8959: }
8960: fprintf(ficresplb," %.3f %d\n", tot, *ncvyearp);
8961: } /* Age */
8962: /* was end of cptcod */
8963: } /* cptcov */
8964:
8965: /* hBijx(p, bage, fage); */
8966: /* fclose(ficrespijb); */
8967:
8968: return 0;
1.217 brouard 8969: }
1.218 brouard 8970:
1.180 brouard 8971: int hPijx(double *p, int bage, int fage){
8972: /*------------- h Pij x at various ages ------------*/
8973:
8974: int stepsize;
8975: int agelim;
8976: int hstepm;
8977: int nhstepm;
8978: int h, i, i1, j, k;
8979:
8980: double agedeb;
8981: double ***p3mat;
8982:
1.201 brouard 8983: strcpy(filerespij,"PIJ_"); strcat(filerespij,fileresu);
1.180 brouard 8984: if((ficrespij=fopen(filerespij,"w"))==NULL) {
8985: printf("Problem with Pij resultfile: %s\n", filerespij); return 1;
8986: fprintf(ficlog,"Problem with Pij resultfile: %s\n", filerespij); return 1;
8987: }
8988: printf("Computing pij: result on file '%s' \n", filerespij);
8989: fprintf(ficlog,"Computing pij: result on file '%s' \n", filerespij);
8990:
8991: stepsize=(int) (stepm+YEARM-1)/YEARM;
8992: /*if (stepm<=24) stepsize=2;*/
8993:
8994: agelim=AGESUP;
8995: hstepm=stepsize*YEARM; /* Every year of age */
8996: hstepm=hstepm/stepm; /* Typically 2 years, = 2/6 months = 4 */
1.218 brouard 8997:
1.180 brouard 8998: /* hstepm=1; aff par mois*/
8999: pstamp(ficrespij);
9000: fprintf(ficrespij,"#****** h Pij x Probability to be in state j at age x+h being in i at x ");
1.227 brouard 9001: i1= pow(2,cptcoveff);
1.218 brouard 9002: /* for(cptcov=1,k=0;cptcov<=i1;cptcov++){ */
9003: /* /\*for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){*\/ */
9004: /* k=k+1; */
1.227 brouard 9005: for (k=1; k <= (int) pow(2,cptcoveff); k++){
1.183 brouard 9006: fprintf(ficrespij,"\n#****** ");
1.227 brouard 9007: for(j=1;j<=cptcoveff;j++)
1.198 brouard 9008: fprintf(ficrespij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
1.183 brouard 9009: fprintf(ficrespij,"******\n");
9010:
9011: for (agedeb=fage; agedeb>=bage; agedeb--){ /* If stepm=6 months */
9012: nhstepm=(int) rint((agelim-agedeb)*YEARM/stepm); /* Typically 20 years = 20*12/6=40 */
9013: nhstepm = nhstepm/hstepm; /* Typically 40/4=10 */
9014:
9015: /* nhstepm=nhstepm*YEARM; aff par mois*/
1.180 brouard 9016:
1.183 brouard 9017: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
9018: oldm=oldms;savm=savms;
9019: hpxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k);
9020: fprintf(ficrespij,"# Cov Agex agex+h hpijx with i,j=");
9021: for(i=1; i<=nlstate;i++)
9022: for(j=1; j<=nlstate+ndeath;j++)
9023: fprintf(ficrespij," %1d-%1d",i,j);
9024: fprintf(ficrespij,"\n");
9025: for (h=0; h<=nhstepm; h++){
9026: /*agedebphstep = agedeb + h*hstepm/YEARM*stepm;*/
9027: fprintf(ficrespij,"%d %3.f %3.f",k, agedeb, agedeb + h*hstepm/YEARM*stepm );
1.180 brouard 9028: for(i=1; i<=nlstate;i++)
9029: for(j=1; j<=nlstate+ndeath;j++)
1.183 brouard 9030: fprintf(ficrespij," %.5f", p3mat[i][j][h]);
1.180 brouard 9031: fprintf(ficrespij,"\n");
9032: }
1.183 brouard 9033: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
9034: fprintf(ficrespij,"\n");
9035: }
1.180 brouard 9036: /*}*/
9037: }
1.218 brouard 9038: return 0;
1.180 brouard 9039: }
1.218 brouard 9040:
9041: int hBijx(double *p, int bage, int fage, double ***prevacurrent){
1.217 brouard 9042: /*------------- h Bij x at various ages ------------*/
9043:
9044: int stepsize;
1.218 brouard 9045: /* int agelim; */
9046: int ageminl;
1.217 brouard 9047: int hstepm;
9048: int nhstepm;
9049: int h, i, i1, j, k;
1.218 brouard 9050:
1.217 brouard 9051: double agedeb;
9052: double ***p3mat;
1.218 brouard 9053:
9054: strcpy(filerespijb,"PIJB_"); strcat(filerespijb,fileresu);
9055: if((ficrespijb=fopen(filerespijb,"w"))==NULL) {
9056: printf("Problem with Pij back resultfile: %s\n", filerespijb); return 1;
9057: fprintf(ficlog,"Problem with Pij back resultfile: %s\n", filerespijb); return 1;
9058: }
9059: printf("Computing pij back: result on file '%s' \n", filerespijb);
9060: fprintf(ficlog,"Computing pij back: result on file '%s' \n", filerespijb);
9061:
9062: stepsize=(int) (stepm+YEARM-1)/YEARM;
9063: /*if (stepm<=24) stepsize=2;*/
1.217 brouard 9064:
1.218 brouard 9065: /* agelim=AGESUP; */
9066: ageminl=30;
9067: hstepm=stepsize*YEARM; /* Every year of age */
9068: hstepm=hstepm/stepm; /* Typically 2 years, = 2/6 months = 4 */
9069:
9070: /* hstepm=1; aff par mois*/
9071: pstamp(ficrespijb);
9072: fprintf(ficrespijb,"#****** h Pij x Back Probability to be in state i at age x-h being in j at x ");
1.227 brouard 9073: i1= pow(2,cptcoveff);
1.218 brouard 9074: /* for(cptcov=1,k=0;cptcov<=i1;cptcov++){ */
9075: /* /\*for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){*\/ */
9076: /* k=k+1; */
1.227 brouard 9077: for (k=1; k <= (int) pow(2,cptcoveff); k++){
1.218 brouard 9078: fprintf(ficrespijb,"\n#****** ");
1.227 brouard 9079: for(j=1;j<=cptcoveff;j++)
1.218 brouard 9080: fprintf(ficrespijb,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
9081: fprintf(ficrespijb,"******\n");
1.222 brouard 9082: if(invalidvarcomb[k]){
9083: fprintf(ficrespijb,"\n#Combination (%d) ignored because no cases \n",k);
9084: continue;
9085: }
1.218 brouard 9086:
9087: /* for (agedeb=fage; agedeb>=bage; agedeb--){ /\* If stepm=6 months *\/ */
9088: for (agedeb=bage; agedeb<=fage; agedeb++){ /* If stepm=6 months and estepm=24 (2 years) */
9089: /* nhstepm=(int) rint((agelim-agedeb)*YEARM/stepm); /\* Typically 20 years = 20*12/6=40 *\/ */
9090: nhstepm=(int) rint((agedeb-ageminl)*YEARM/stepm); /* Typically 20 years = 20*12/6=40 */
9091: nhstepm = nhstepm/hstepm; /* Typically 40/4=10, because estepm=24 stepm=6 => hstepm=24/6=4 */
9092:
9093: /* nhstepm=nhstepm*YEARM; aff par mois*/
9094:
9095: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
9096: /* oldm=oldms;savm=savms; */
9097: /* hbxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k); */
9098: hbxij(p3mat,nhstepm,agedeb,hstepm,p,prevacurrent,nlstate,stepm, k);
9099: /* hbxij(p3mat,nhstepm,agedeb,hstepm,p,prevacurrent,nlstate,stepm,oldm,savm, dnewm, doldm, dsavm, k); */
9100: fprintf(ficrespijb,"# Cov Agex agex-h hpijx with i,j=");
9101: for(i=1; i<=nlstate;i++)
9102: for(j=1; j<=nlstate+ndeath;j++)
9103: fprintf(ficrespijb," %1d-%1d",i,j);
9104: fprintf(ficrespijb,"\n");
9105: for (h=0; h<=nhstepm; h++){
9106: /*agedebphstep = agedeb + h*hstepm/YEARM*stepm;*/
9107: fprintf(ficrespijb,"%d %3.f %3.f",k, agedeb, agedeb - h*hstepm/YEARM*stepm );
9108: /* fprintf(ficrespijb,"%d %3.f %3.f",k, agedeb, agedeb + h*hstepm/YEARM*stepm ); */
1.217 brouard 9109: for(i=1; i<=nlstate;i++)
9110: for(j=1; j<=nlstate+ndeath;j++)
1.218 brouard 9111: fprintf(ficrespijb," %.5f", p3mat[i][j][h]);
1.217 brouard 9112: fprintf(ficrespijb,"\n");
9113: }
1.218 brouard 9114: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
9115: fprintf(ficrespijb,"\n");
1.217 brouard 9116: }
1.218 brouard 9117: /*}*/
9118: }
9119: return 0;
9120: } /* hBijx */
1.217 brouard 9121:
1.180 brouard 9122:
1.136 brouard 9123: /***********************************************/
9124: /**************** Main Program *****************/
9125: /***********************************************/
9126:
9127: int main(int argc, char *argv[])
9128: {
9129: #ifdef GSL
9130: const gsl_multimin_fminimizer_type *T;
9131: size_t iteri = 0, it;
9132: int rval = GSL_CONTINUE;
9133: int status = GSL_SUCCESS;
9134: double ssval;
9135: #endif
9136: int movingaverage(double ***probs, double bage,double fage, double ***mobaverage, int mobilav);
1.164 brouard 9137: int i,j, k, n=MAXN,iter=0,m,size=100, cptcod;
1.209 brouard 9138: int ncvyear=0; /* Number of years needed for the period prevalence to converge */
1.164 brouard 9139: int jj, ll, li, lj, lk;
1.136 brouard 9140: int numlinepar=0; /* Current linenumber of parameter file */
1.197 brouard 9141: int num_filled;
1.136 brouard 9142: int itimes;
9143: int NDIM=2;
9144: int vpopbased=0;
9145:
1.164 brouard 9146: char ca[32], cb[32];
1.136 brouard 9147: /* FILE *fichtm; *//* Html File */
9148: /* FILE *ficgp;*/ /*Gnuplot File */
9149: struct stat info;
1.191 brouard 9150: double agedeb=0.;
1.194 brouard 9151:
9152: double ageminpar=AGEOVERFLOW,agemin=AGEOVERFLOW, agemaxpar=-AGEOVERFLOW, agemax=-AGEOVERFLOW;
1.219 brouard 9153: double ageminout=-AGEOVERFLOW,agemaxout=AGEOVERFLOW; /* Smaller Age range redefined after movingaverage */
1.136 brouard 9154:
1.165 brouard 9155: double fret;
1.191 brouard 9156: double dum=0.; /* Dummy variable */
1.136 brouard 9157: double ***p3mat;
1.218 brouard 9158: /* double ***mobaverage; */
1.164 brouard 9159:
9160: char line[MAXLINE];
1.197 brouard 9161: char path[MAXLINE],pathc[MAXLINE],pathcd[MAXLINE],pathtot[MAXLINE];
9162:
1.234 ! brouard 9163: char modeltemp[MAXLINE];
1.230 brouard 9164: char resultline[MAXLINE];
9165:
1.136 brouard 9166: char pathr[MAXLINE], pathimach[MAXLINE];
1.164 brouard 9167: char *tok, *val; /* pathtot */
1.136 brouard 9168: int firstobs=1, lastobs=10;
1.195 brouard 9169: int c, h , cpt, c2;
1.191 brouard 9170: int jl=0;
9171: int i1, j1, jk, stepsize=0;
1.194 brouard 9172: int count=0;
9173:
1.164 brouard 9174: int *tab;
1.136 brouard 9175: int mobilavproj=0 , prevfcast=0 ; /* moving average of prev, If prevfcast=1 prevalence projection */
1.217 brouard 9176: int backcast=0;
1.136 brouard 9177: int mobilav=0,popforecast=0;
1.191 brouard 9178: int hstepm=0, nhstepm=0;
1.136 brouard 9179: int agemortsup;
9180: float sumlpop=0.;
9181: double jprev1=1, mprev1=1,anprev1=2000,jprev2=1, mprev2=1,anprev2=2000;
9182: double jpyram=1, mpyram=1,anpyram=2000,jpyram1=1, mpyram1=1,anpyram1=2000;
9183:
1.191 brouard 9184: double bage=0, fage=110., age, agelim=0., agebase=0.;
1.136 brouard 9185: double ftolpl=FTOL;
9186: double **prlim;
1.217 brouard 9187: double **bprlim;
1.136 brouard 9188: double ***param; /* Matrix of parameters */
9189: double *p;
9190: double **matcov; /* Matrix of covariance */
1.203 brouard 9191: double **hess; /* Hessian matrix */
1.136 brouard 9192: double ***delti3; /* Scale */
9193: double *delti; /* Scale */
9194: double ***eij, ***vareij;
9195: double **varpl; /* Variances of prevalence limits by age */
9196: double *epj, vepp;
1.164 brouard 9197:
1.136 brouard 9198: double dateprev1, dateprev2,jproj1=1,mproj1=1,anproj1=2000,jproj2=1,mproj2=1,anproj2=2000;
1.217 brouard 9199: double jback1=1,mback1=1,anback1=2000,jback2=1,mback2=1,anback2=2000;
9200:
1.136 brouard 9201: double **ximort;
1.145 brouard 9202: char *alph[]={"a","a","b","c","d","e"}, str[4]="1234";
1.136 brouard 9203: int *dcwave;
9204:
1.164 brouard 9205: char z[1]="c";
1.136 brouard 9206:
9207: /*char *strt;*/
9208: char strtend[80];
1.126 brouard 9209:
1.164 brouard 9210:
1.126 brouard 9211: /* setlocale (LC_ALL, ""); */
9212: /* bindtextdomain (PACKAGE, LOCALEDIR); */
9213: /* textdomain (PACKAGE); */
9214: /* setlocale (LC_CTYPE, ""); */
9215: /* setlocale (LC_MESSAGES, ""); */
9216:
9217: /* gettimeofday(&start_time, (struct timezone*)0); */ /* at first time */
1.157 brouard 9218: rstart_time = time(NULL);
9219: /* (void) gettimeofday(&start_time,&tzp);*/
9220: start_time = *localtime(&rstart_time);
1.126 brouard 9221: curr_time=start_time;
1.157 brouard 9222: /*tml = *localtime(&start_time.tm_sec);*/
9223: /* strcpy(strstart,asctime(&tml)); */
9224: strcpy(strstart,asctime(&start_time));
1.126 brouard 9225:
9226: /* printf("Localtime (at start)=%s",strstart); */
1.157 brouard 9227: /* tp.tm_sec = tp.tm_sec +86400; */
9228: /* tm = *localtime(&start_time.tm_sec); */
1.126 brouard 9229: /* tmg.tm_year=tmg.tm_year +dsign*dyear; */
9230: /* tmg.tm_mon=tmg.tm_mon +dsign*dmonth; */
9231: /* tmg.tm_hour=tmg.tm_hour + 1; */
1.157 brouard 9232: /* tp.tm_sec = mktime(&tmg); */
1.126 brouard 9233: /* strt=asctime(&tmg); */
9234: /* printf("Time(after) =%s",strstart); */
9235: /* (void) time (&time_value);
9236: * printf("time=%d,t-=%d\n",time_value,time_value-86400);
9237: * tm = *localtime(&time_value);
9238: * strstart=asctime(&tm);
9239: * printf("tim_value=%d,asctime=%s\n",time_value,strstart);
9240: */
9241:
9242: nberr=0; /* Number of errors and warnings */
9243: nbwarn=0;
1.184 brouard 9244: #ifdef WIN32
9245: _getcwd(pathcd, size);
9246: #else
1.126 brouard 9247: getcwd(pathcd, size);
1.184 brouard 9248: #endif
1.191 brouard 9249: syscompilerinfo(0);
1.196 brouard 9250: printf("\nIMaCh version %s, %s\n%s",version, copyright, fullversion);
1.126 brouard 9251: if(argc <=1){
9252: printf("\nEnter the parameter file name: ");
1.205 brouard 9253: if(!fgets(pathr,FILENAMELENGTH,stdin)){
9254: printf("ERROR Empty parameter file name\n");
9255: goto end;
9256: }
1.126 brouard 9257: i=strlen(pathr);
9258: if(pathr[i-1]=='\n')
9259: pathr[i-1]='\0';
1.156 brouard 9260: i=strlen(pathr);
1.205 brouard 9261: if(i >= 1 && pathr[i-1]==' ') {/* This may happen when dragging on oS/X! */
1.156 brouard 9262: pathr[i-1]='\0';
1.205 brouard 9263: }
9264: i=strlen(pathr);
9265: if( i==0 ){
9266: printf("ERROR Empty parameter file name\n");
9267: goto end;
9268: }
9269: for (tok = pathr; tok != NULL; ){
1.126 brouard 9270: printf("Pathr |%s|\n",pathr);
9271: while ((val = strsep(&tok, "\"" )) != NULL && *val == '\0');
9272: printf("val= |%s| pathr=%s\n",val,pathr);
9273: strcpy (pathtot, val);
9274: if(pathr[0] == '\0') break; /* Dirty */
9275: }
9276: }
9277: else{
9278: strcpy(pathtot,argv[1]);
9279: }
9280: /*if(getcwd(pathcd, MAXLINE)!= NULL)printf ("Error pathcd\n");*/
9281: /*cygwin_split_path(pathtot,path,optionfile);
9282: printf("pathtot=%s, path=%s, optionfile=%s\n",pathtot,path,optionfile);*/
9283: /* cutv(path,optionfile,pathtot,'\\');*/
9284:
9285: /* Split argv[0], imach program to get pathimach */
9286: printf("\nargv[0]=%s argv[1]=%s, \n",argv[0],argv[1]);
9287: split(argv[0],pathimach,optionfile,optionfilext,optionfilefiname);
9288: printf("\nargv[0]=%s pathimach=%s, \noptionfile=%s \noptionfilext=%s \noptionfilefiname=%s\n",argv[0],pathimach,optionfile,optionfilext,optionfilefiname);
9289: /* strcpy(pathimach,argv[0]); */
9290: /* Split argv[1]=pathtot, parameter file name to get path, optionfile, extension and name */
9291: split(pathtot,path,optionfile,optionfilext,optionfilefiname);
9292: printf("\npathtot=%s,\npath=%s,\noptionfile=%s \noptionfilext=%s \noptionfilefiname=%s\n",pathtot,path,optionfile,optionfilext,optionfilefiname);
1.184 brouard 9293: #ifdef WIN32
9294: _chdir(path); /* Can be a relative path */
9295: if(_getcwd(pathcd,MAXLINE) > 0) /* So pathcd is the full path */
9296: #else
1.126 brouard 9297: chdir(path); /* Can be a relative path */
1.184 brouard 9298: if (getcwd(pathcd, MAXLINE) > 0) /* So pathcd is the full path */
9299: #endif
9300: printf("Current directory %s!\n",pathcd);
1.126 brouard 9301: strcpy(command,"mkdir ");
9302: strcat(command,optionfilefiname);
9303: if((outcmd=system(command)) != 0){
1.169 brouard 9304: printf("Directory already exists (or can't create it) %s%s, err=%d\n",path,optionfilefiname,outcmd);
1.126 brouard 9305: /* fprintf(ficlog,"Problem creating directory %s%s\n",path,optionfilefiname); */
9306: /* fclose(ficlog); */
9307: /* exit(1); */
9308: }
9309: /* if((imk=mkdir(optionfilefiname))<0){ */
9310: /* perror("mkdir"); */
9311: /* } */
9312:
9313: /*-------- arguments in the command line --------*/
9314:
1.186 brouard 9315: /* Main Log file */
1.126 brouard 9316: strcat(filelog, optionfilefiname);
9317: strcat(filelog,".log"); /* */
9318: if((ficlog=fopen(filelog,"w"))==NULL) {
9319: printf("Problem with logfile %s\n",filelog);
9320: goto end;
9321: }
9322: fprintf(ficlog,"Log filename:%s\n",filelog);
1.197 brouard 9323: fprintf(ficlog,"Version %s %s",version,fullversion);
1.126 brouard 9324: fprintf(ficlog,"\nEnter the parameter file name: \n");
9325: fprintf(ficlog,"pathimach=%s\npathtot=%s\n\
9326: path=%s \n\
9327: optionfile=%s\n\
9328: optionfilext=%s\n\
1.156 brouard 9329: optionfilefiname='%s'\n",pathimach,pathtot,path,optionfile,optionfilext,optionfilefiname);
1.126 brouard 9330:
1.197 brouard 9331: syscompilerinfo(1);
1.167 brouard 9332:
1.126 brouard 9333: printf("Local time (at start):%s",strstart);
9334: fprintf(ficlog,"Local time (at start): %s",strstart);
9335: fflush(ficlog);
9336: /* (void) gettimeofday(&curr_time,&tzp); */
1.157 brouard 9337: /* printf("Elapsed time %d\n", asc_diff_time(curr_time.tm_sec-start_time.tm_sec,tmpout)); */
1.126 brouard 9338:
9339: /* */
9340: strcpy(fileres,"r");
9341: strcat(fileres, optionfilefiname);
1.201 brouard 9342: strcat(fileresu, optionfilefiname); /* Without r in front */
1.126 brouard 9343: strcat(fileres,".txt"); /* Other files have txt extension */
1.201 brouard 9344: strcat(fileresu,".txt"); /* Other files have txt extension */
1.126 brouard 9345:
1.186 brouard 9346: /* Main ---------arguments file --------*/
1.126 brouard 9347:
9348: if((ficpar=fopen(optionfile,"r"))==NULL) {
1.155 brouard 9349: printf("Problem with optionfile '%s' with errno='%s'\n",optionfile,strerror(errno));
9350: fprintf(ficlog,"Problem with optionfile '%s' with errno='%s'\n",optionfile,strerror(errno));
1.126 brouard 9351: fflush(ficlog);
1.149 brouard 9352: /* goto end; */
9353: exit(70);
1.126 brouard 9354: }
9355:
9356:
9357:
9358: strcpy(filereso,"o");
1.201 brouard 9359: strcat(filereso,fileresu);
1.126 brouard 9360: if((ficparo=fopen(filereso,"w"))==NULL) { /* opened on subdirectory */
9361: printf("Problem with Output resultfile: %s\n", filereso);
9362: fprintf(ficlog,"Problem with Output resultfile: %s\n", filereso);
9363: fflush(ficlog);
9364: goto end;
9365: }
9366:
9367: /* Reads comments: lines beginning with '#' */
9368: numlinepar=0;
1.197 brouard 9369:
9370: /* First parameter line */
9371: while(fgets(line, MAXLINE, ficpar)) {
9372: /* If line starts with a # it is a comment */
9373: if (line[0] == '#') {
9374: numlinepar++;
9375: fputs(line,stdout);
9376: fputs(line,ficparo);
9377: fputs(line,ficlog);
9378: continue;
9379: }else
9380: break;
9381: }
9382: if((num_filled=sscanf(line,"title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\n", \
9383: title, datafile, &lastobs, &firstpass,&lastpass)) !=EOF){
9384: if (num_filled != 5) {
9385: printf("Should be 5 parameters\n");
9386: }
1.126 brouard 9387: numlinepar++;
1.197 brouard 9388: printf("title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\n", title, datafile, lastobs, firstpass,lastpass);
9389: }
9390: /* Second parameter line */
9391: while(fgets(line, MAXLINE, ficpar)) {
9392: /* If line starts with a # it is a comment */
9393: if (line[0] == '#') {
9394: numlinepar++;
9395: fputs(line,stdout);
9396: fputs(line,ficparo);
9397: fputs(line,ficlog);
9398: continue;
9399: }else
9400: break;
9401: }
1.223 brouard 9402: 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", \
9403: &ftol, &stepm, &ncovcol, &nqv, &ntv, &nqtv, &nlstate, &ndeath, &maxwav, &mle, &weightopt)) !=EOF){
9404: if (num_filled != 11) {
9405: 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 9406: printf("but line=%s\n",line);
1.197 brouard 9407: }
1.223 brouard 9408: 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 9409: }
1.203 brouard 9410: /* ftolpl=6*ftol*1.e5; /\* 6.e-3 make convergences in less than 80 loops for the prevalence limit *\/ */
1.209 brouard 9411: /*ftolpl=6.e-4; *//* 6.e-3 make convergences in less than 80 loops for the prevalence limit */
1.197 brouard 9412: /* Third parameter line */
9413: while(fgets(line, MAXLINE, ficpar)) {
9414: /* If line starts with a # it is a comment */
9415: if (line[0] == '#') {
9416: numlinepar++;
9417: fputs(line,stdout);
9418: fputs(line,ficparo);
9419: fputs(line,ficlog);
9420: continue;
9421: }else
9422: break;
9423: }
1.201 brouard 9424: if((num_filled=sscanf(line,"model=1+age%[^.\n]", model)) !=EOF){
9425: if (num_filled == 0)
9426: model[0]='\0';
9427: else if (num_filled != 1){
1.197 brouard 9428: printf("ERROR %d: Model should be at minimum 'model=1+age.' %s\n",num_filled, line);
9429: fprintf(ficlog,"ERROR %d: Model should be at minimum 'model=1+age.' %s\n",num_filled, line);
9430: model[0]='\0';
9431: goto end;
9432: }
9433: else{
9434: if (model[0]=='+'){
9435: for(i=1; i<=strlen(model);i++)
9436: modeltemp[i-1]=model[i];
1.201 brouard 9437: strcpy(model,modeltemp);
1.197 brouard 9438: }
9439: }
1.199 brouard 9440: /* printf(" model=1+age%s modeltemp= %s, model=%s\n",model, modeltemp, model);fflush(stdout); */
1.203 brouard 9441: printf("model=1+age+%s\n",model);fflush(stdout);
1.197 brouard 9442: }
9443: /* 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); */
9444: /* numlinepar=numlinepar+3; /\* In general *\/ */
9445: /* 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 9446: 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);
9447: 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 9448: fflush(ficlog);
1.190 brouard 9449: /* if(model[0]=='#'|| model[0]== '\0'){ */
9450: if(model[0]=='#'){
1.187 brouard 9451: printf("Error in 'model' line: model should start with 'model=1+age+' and end with '.' \n \
9452: 'model=1+age+.' or 'model=1+age+V1.' or 'model=1+age+age*age+V1+V1*age.' or \n \
9453: 'model=1+age+V1+V2.' or 'model=1+age+V1+V2+V1*V2.' etc. \n"); \
9454: if(mle != -1){
9455: printf("Fix the model line and run imach with mle=-1 to get a correct template of the parameter file.\n");
9456: exit(1);
9457: }
9458: }
1.126 brouard 9459: while((c=getc(ficpar))=='#' && c!= EOF){
9460: ungetc(c,ficpar);
9461: fgets(line, MAXLINE, ficpar);
9462: numlinepar++;
1.195 brouard 9463: if(line[1]=='q'){ /* This #q will quit imach (the answer is q) */
9464: z[0]=line[1];
9465: }
9466: /* printf("****line [1] = %c \n",line[1]); */
1.141 brouard 9467: fputs(line, stdout);
9468: //puts(line);
1.126 brouard 9469: fputs(line,ficparo);
9470: fputs(line,ficlog);
9471: }
9472: ungetc(c,ficpar);
9473:
9474:
1.145 brouard 9475: covar=matrix(0,NCOVMAX,1,n); /**< used in readdata */
1.225 brouard 9476: coqvar=matrix(1,nqv,1,n); /**< Fixed quantitative covariate */
1.233 brouard 9477: cotvar=ma3x(1,maxwav,1,ntv+nqtv,1,n); /**< Time varying covariate (dummy and quantitative)*/
1.225 brouard 9478: cotqvar=ma3x(1,maxwav,1,nqtv,1,n); /**< Time varying quantitative covariate */
1.136 brouard 9479: cptcovn=0; /*Number of covariates, i.e. number of '+' in model statement plus one, indepently of n in Vn*/
9480: /* v1+v2+v3+v2*v4+v5*age makes cptcovn = 5
9481: v1+v2*age+v2*v3 makes cptcovn = 3
9482: */
9483: if (strlen(model)>1)
1.187 brouard 9484: 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 9485: else
1.187 brouard 9486: ncovmodel=2; /* Constant and age */
1.133 brouard 9487: nforce= (nlstate+ndeath-1)*nlstate; /* Number of forces ij from state i to j */
9488: npar= nforce*ncovmodel; /* Number of parameters like aij*/
1.131 brouard 9489: if(npar >MAXPARM || nlstate >NLSTATEMAX || ndeath >NDEATHMAX || ncovmodel>NCOVMAX){
9490: 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);
9491: 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);
9492: fflush(stdout);
9493: fclose (ficlog);
9494: goto end;
9495: }
1.126 brouard 9496: delti3= ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel);
9497: delti=delti3[1][1];
9498: /*delti=vector(1,npar); *//* Scale of each paramater (output from hesscov)*/
9499: if(mle==-1){ /* Print a wizard for help writing covariance matrix */
9500: prwizard(ncovmodel, nlstate, ndeath, model, ficparo);
1.191 brouard 9501: printf(" You chose mle=-1, look at file %s for a template of covariance matrix \n",filereso);
9502: fprintf(ficlog," You chose mle=-1, look at file %s for a template of covariance matrix \n",filereso);
1.126 brouard 9503: free_ma3x(delti3,1,nlstate,1, nlstate+ndeath-1,1,ncovmodel);
9504: fclose (ficparo);
9505: fclose (ficlog);
9506: goto end;
9507: exit(0);
1.220 brouard 9508: } else if(mle==-5) { /* Main Wizard */
1.126 brouard 9509: prwizard(ncovmodel, nlstate, ndeath, model, ficparo);
1.192 brouard 9510: printf(" You chose mle=-3, look at file %s for a template of covariance matrix \n",filereso);
9511: fprintf(ficlog," You chose mle=-3, look at file %s for a template of covariance matrix \n",filereso);
1.126 brouard 9512: param= ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel);
9513: matcov=matrix(1,npar,1,npar);
1.203 brouard 9514: hess=matrix(1,npar,1,npar);
1.220 brouard 9515: } else{ /* Begin of mle != -1 or -5 */
1.145 brouard 9516: /* Read guessed parameters */
1.126 brouard 9517: /* Reads comments: lines beginning with '#' */
9518: while((c=getc(ficpar))=='#' && c!= EOF){
9519: ungetc(c,ficpar);
9520: fgets(line, MAXLINE, ficpar);
9521: numlinepar++;
1.141 brouard 9522: fputs(line,stdout);
1.126 brouard 9523: fputs(line,ficparo);
9524: fputs(line,ficlog);
9525: }
9526: ungetc(c,ficpar);
9527:
9528: param= ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel);
9529: for(i=1; i <=nlstate; i++){
1.234 ! brouard 9530: j=0;
1.126 brouard 9531: for(jj=1; jj <=nlstate+ndeath; jj++){
1.234 ! brouard 9532: if(jj==i) continue;
! 9533: j++;
! 9534: fscanf(ficpar,"%1d%1d",&i1,&j1);
! 9535: if ((i1 != i) || (j1 != jj)){
! 9536: printf("Error in line parameters number %d, %1d%1d instead of %1d%1d \n \
1.126 brouard 9537: It might be a problem of design; if ncovcol and the model are correct\n \
9538: run imach with mle=-1 to get a correct template of the parameter file.\n",numlinepar, i,j, i1, j1);
1.234 ! brouard 9539: exit(1);
! 9540: }
! 9541: fprintf(ficparo,"%1d%1d",i1,j1);
! 9542: if(mle==1)
! 9543: printf("%1d%1d",i,jj);
! 9544: fprintf(ficlog,"%1d%1d",i,jj);
! 9545: for(k=1; k<=ncovmodel;k++){
! 9546: fscanf(ficpar," %lf",¶m[i][j][k]);
! 9547: if(mle==1){
! 9548: printf(" %lf",param[i][j][k]);
! 9549: fprintf(ficlog," %lf",param[i][j][k]);
! 9550: }
! 9551: else
! 9552: fprintf(ficlog," %lf",param[i][j][k]);
! 9553: fprintf(ficparo," %lf",param[i][j][k]);
! 9554: }
! 9555: fscanf(ficpar,"\n");
! 9556: numlinepar++;
! 9557: if(mle==1)
! 9558: printf("\n");
! 9559: fprintf(ficlog,"\n");
! 9560: fprintf(ficparo,"\n");
1.126 brouard 9561: }
9562: }
9563: fflush(ficlog);
1.234 ! brouard 9564:
1.145 brouard 9565: /* Reads scales values */
1.126 brouard 9566: p=param[1][1];
9567:
9568: /* Reads comments: lines beginning with '#' */
9569: while((c=getc(ficpar))=='#' && c!= EOF){
9570: ungetc(c,ficpar);
9571: fgets(line, MAXLINE, ficpar);
9572: numlinepar++;
1.141 brouard 9573: fputs(line,stdout);
1.126 brouard 9574: fputs(line,ficparo);
9575: fputs(line,ficlog);
9576: }
9577: ungetc(c,ficpar);
9578:
9579: for(i=1; i <=nlstate; i++){
9580: for(j=1; j <=nlstate+ndeath-1; j++){
1.234 ! brouard 9581: fscanf(ficpar,"%1d%1d",&i1,&j1);
! 9582: if ( (i1-i) * (j1-j) != 0){
! 9583: printf("Error in line parameters number %d, %1d%1d instead of %1d%1d \n",numlinepar, i,j, i1, j1);
! 9584: exit(1);
! 9585: }
! 9586: printf("%1d%1d",i,j);
! 9587: fprintf(ficparo,"%1d%1d",i1,j1);
! 9588: fprintf(ficlog,"%1d%1d",i1,j1);
! 9589: for(k=1; k<=ncovmodel;k++){
! 9590: fscanf(ficpar,"%le",&delti3[i][j][k]);
! 9591: printf(" %le",delti3[i][j][k]);
! 9592: fprintf(ficparo," %le",delti3[i][j][k]);
! 9593: fprintf(ficlog," %le",delti3[i][j][k]);
! 9594: }
! 9595: fscanf(ficpar,"\n");
! 9596: numlinepar++;
! 9597: printf("\n");
! 9598: fprintf(ficparo,"\n");
! 9599: fprintf(ficlog,"\n");
1.126 brouard 9600: }
9601: }
9602: fflush(ficlog);
1.234 ! brouard 9603:
1.145 brouard 9604: /* Reads covariance matrix */
1.126 brouard 9605: delti=delti3[1][1];
1.220 brouard 9606:
9607:
1.126 brouard 9608: /* 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 9609:
1.126 brouard 9610: /* Reads comments: lines beginning with '#' */
9611: while((c=getc(ficpar))=='#' && c!= EOF){
9612: ungetc(c,ficpar);
9613: fgets(line, MAXLINE, ficpar);
9614: numlinepar++;
1.141 brouard 9615: fputs(line,stdout);
1.126 brouard 9616: fputs(line,ficparo);
9617: fputs(line,ficlog);
9618: }
9619: ungetc(c,ficpar);
1.220 brouard 9620:
1.126 brouard 9621: matcov=matrix(1,npar,1,npar);
1.203 brouard 9622: hess=matrix(1,npar,1,npar);
1.131 brouard 9623: for(i=1; i <=npar; i++)
9624: for(j=1; j <=npar; j++) matcov[i][j]=0.;
1.220 brouard 9625:
1.194 brouard 9626: /* Scans npar lines */
1.126 brouard 9627: for(i=1; i <=npar; i++){
1.226 brouard 9628: count=fscanf(ficpar,"%1d%1d%d",&i1,&j1,&jk);
1.194 brouard 9629: if(count != 3){
1.226 brouard 9630: printf("Error! Error in parameter file %s at line %d after line starting with %1d%1d%1d\n\
1.194 brouard 9631: This is probably because your covariance matrix doesn't \n contain exactly %d lines corresponding to your model line '1+age+%s'.\n\
9632: Please run with mle=-1 to get a correct covariance matrix.\n",optionfile,numlinepar, i1,j1,jk, npar, model);
1.226 brouard 9633: fprintf(ficlog,"Error! Error in parameter file %s at line %d after line starting with %1d%1d%1d\n\
1.194 brouard 9634: This is probably because your covariance matrix doesn't \n contain exactly %d lines corresponding to your model line '1+age+%s'.\n\
9635: Please run with mle=-1 to get a correct covariance matrix.\n",optionfile,numlinepar, i1,j1,jk, npar, model);
1.226 brouard 9636: exit(1);
1.220 brouard 9637: }else{
1.226 brouard 9638: if(mle==1)
9639: printf("%1d%1d%d",i1,j1,jk);
9640: }
9641: fprintf(ficlog,"%1d%1d%d",i1,j1,jk);
9642: fprintf(ficparo,"%1d%1d%d",i1,j1,jk);
1.126 brouard 9643: for(j=1; j <=i; j++){
1.226 brouard 9644: fscanf(ficpar," %le",&matcov[i][j]);
9645: if(mle==1){
9646: printf(" %.5le",matcov[i][j]);
9647: }
9648: fprintf(ficlog," %.5le",matcov[i][j]);
9649: fprintf(ficparo," %.5le",matcov[i][j]);
1.126 brouard 9650: }
9651: fscanf(ficpar,"\n");
9652: numlinepar++;
9653: if(mle==1)
1.220 brouard 9654: printf("\n");
1.126 brouard 9655: fprintf(ficlog,"\n");
9656: fprintf(ficparo,"\n");
9657: }
1.194 brouard 9658: /* End of read covariance matrix npar lines */
1.126 brouard 9659: for(i=1; i <=npar; i++)
9660: for(j=i+1;j<=npar;j++)
1.226 brouard 9661: matcov[i][j]=matcov[j][i];
1.126 brouard 9662:
9663: if(mle==1)
9664: printf("\n");
9665: fprintf(ficlog,"\n");
9666:
9667: fflush(ficlog);
9668:
9669: /*-------- Rewriting parameter file ----------*/
9670: strcpy(rfileres,"r"); /* "Rparameterfile */
9671: strcat(rfileres,optionfilefiname); /* Parameter file first name*/
9672: strcat(rfileres,"."); /* */
9673: strcat(rfileres,optionfilext); /* Other files have txt extension */
9674: if((ficres =fopen(rfileres,"w"))==NULL) {
1.201 brouard 9675: printf("Problem writing new parameter file: %s\n", rfileres);goto end;
9676: fprintf(ficlog,"Problem writing new parameter file: %s\n", rfileres);goto end;
1.126 brouard 9677: }
9678: fprintf(ficres,"#%s\n",version);
9679: } /* End of mle != -3 */
1.218 brouard 9680:
1.186 brouard 9681: /* Main data
9682: */
1.126 brouard 9683: n= lastobs;
9684: num=lvector(1,n);
9685: moisnais=vector(1,n);
9686: annais=vector(1,n);
9687: moisdc=vector(1,n);
9688: andc=vector(1,n);
1.220 brouard 9689: weight=vector(1,n);
1.126 brouard 9690: agedc=vector(1,n);
9691: cod=ivector(1,n);
1.220 brouard 9692: for(i=1;i<=n;i++){
1.234 ! brouard 9693: num[i]=0;
! 9694: moisnais[i]=0;
! 9695: annais[i]=0;
! 9696: moisdc[i]=0;
! 9697: andc[i]=0;
! 9698: agedc[i]=0;
! 9699: cod[i]=0;
! 9700: weight[i]=1.0; /* Equal weights, 1 by default */
! 9701: }
1.126 brouard 9702: mint=matrix(1,maxwav,1,n);
9703: anint=matrix(1,maxwav,1,n);
1.131 brouard 9704: s=imatrix(1,maxwav+1,1,n); /* s[i][j] health state for wave i and individual j */
1.126 brouard 9705: tab=ivector(1,NCOVMAX);
1.144 brouard 9706: ncodemax=ivector(1,NCOVMAX); /* Number of code per covariate; if O and 1 only, 2**ncov; V1+V2+V3+V4=>16 */
1.192 brouard 9707: 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 9708:
1.136 brouard 9709: /* Reads data from file datafile */
9710: if (readdata(datafile, firstobs, lastobs, &imx)==1)
9711: goto end;
9712:
9713: /* Calculation of the number of parameters from char model */
1.234 ! brouard 9714: /* modelsav=V2+V1+V4+age*V3 strb=age*V3 stra=V2+V1+V4
1.137 brouard 9715: k=4 (age*V3) Tvar[k=4]= 3 (from V3) Tag[cptcovage=1]=4
9716: k=3 V4 Tvar[k=3]= 4 (from V4)
9717: k=2 V1 Tvar[k=2]= 1 (from V1)
9718: k=1 Tvar[1]=2 (from V2)
1.234 ! brouard 9719: */
! 9720:
! 9721: Tvar=ivector(1,NCOVMAX); /* Was 15 changed to NCOVMAX. */
! 9722: TvarsDind=ivector(1,NCOVMAX); /* */
! 9723: TvarsD=ivector(1,NCOVMAX); /* */
! 9724: TvarsQind=ivector(1,NCOVMAX); /* */
! 9725: TvarsQ=ivector(1,NCOVMAX); /* */
1.232 brouard 9726: TvarF=ivector(1,NCOVMAX); /* */
9727: TvarFind=ivector(1,NCOVMAX); /* */
9728: TvarV=ivector(1,NCOVMAX); /* */
9729: TvarVind=ivector(1,NCOVMAX); /* */
9730: TvarA=ivector(1,NCOVMAX); /* */
9731: TvarAind=ivector(1,NCOVMAX); /* */
1.231 brouard 9732: TvarFD=ivector(1,NCOVMAX); /* */
9733: TvarFDind=ivector(1,NCOVMAX); /* */
9734: TvarFQ=ivector(1,NCOVMAX); /* */
9735: TvarFQind=ivector(1,NCOVMAX); /* */
9736: TvarVD=ivector(1,NCOVMAX); /* */
9737: TvarVDind=ivector(1,NCOVMAX); /* */
9738: TvarVQ=ivector(1,NCOVMAX); /* */
9739: TvarVQind=ivector(1,NCOVMAX); /* */
9740:
1.230 brouard 9741: Tvalsel=vector(1,NCOVMAX); /* */
1.233 brouard 9742: Tvarsel=ivector(1,NCOVMAX); /* */
1.226 brouard 9743: Typevar=ivector(-1,NCOVMAX); /* -1 to 2 */
9744: Fixed=ivector(-1,NCOVMAX); /* -1 to 3 */
9745: Dummy=ivector(-1,NCOVMAX); /* -1 to 3 */
1.137 brouard 9746: /* V2+V1+V4+age*V3 is a model with 4 covariates (3 plus signs).
9747: For each model-covariate stores the data-covariate id. Tvar[1]=2, Tvar[2]=1, Tvar[3]=4,
9748: Tvar[4=age*V3] is 3 and 'age' is recorded in Tage.
9749: */
9750: /* For model-covariate k tells which data-covariate to use but
9751: because this model-covariate is a construction we invent a new column
9752: ncovcol + k1
9753: If already ncovcol=4 and model=V2+V1+V1*V4+age*V3
9754: Tvar[3=V1*V4]=4+1 etc */
1.227 brouard 9755: Tprod=ivector(1,NCOVMAX); /* Gives the k position of the k1 product */
9756: Tposprod=ivector(1,NCOVMAX); /* Gives the k1 product from the k position */
1.137 brouard 9757: /* Tprod[k1=1]=3(=V1*V4) for V2+V1+V1*V4+age*V3
9758: if V2+V1+V1*V4+age*V3+V3*V2 TProd[k1=2]=5 (V3*V2)
1.227 brouard 9759: Tposprod[k]=k1 , Tposprod[3]=1, Tposprod[5]=2
1.137 brouard 9760: */
1.145 brouard 9761: Tvaraff=ivector(1,NCOVMAX); /* Unclear */
9762: 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 9763: * For V3*V2 (in V2+V1+V1*V4+age*V3+V3*V2), V3*V2 position is 2nd.
9764: * Tvard[k1=2][1]=3 (V3) Tvard[k1=2][2]=2(V2) */
1.145 brouard 9765: Tage=ivector(1,NCOVMAX); /* Gives the covariate id of covariates associated with age: V2 + V1 + age*V4 + V3*age
1.137 brouard 9766: 4 covariates (3 plus signs)
9767: Tage[1=V3*age]= 4; Tage[2=age*V4] = 3
9768: */
1.230 brouard 9769: Tmodelind=ivector(1,NCOVMAX);/** gives the k model position of an
1.227 brouard 9770: * individual dummy, fixed or varying:
9771: * Tmodelind[Tvaraff[3]]=9,Tvaraff[1]@9={4,
9772: * 3, 1, 0, 0, 0, 0, 0, 0},
1.230 brouard 9773: * model=V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 ,
9774: * V1 df, V2 qf, V3 & V4 dv, V5 qv
9775: * Tmodelind[1]@9={9,0,3,2,}*/
9776: TmodelInvind=ivector(1,NCOVMAX); /* TmodelInvind=Tvar[k]- ncovcol-nqv={5-2-1=2,*/
9777: TmodelInvQind=ivector(1,NCOVMAX);/** gives the k model position of an
1.228 brouard 9778: * individual quantitative, fixed or varying:
9779: * Tmodelqind[1]=1,Tvaraff[1]@9={4,
9780: * 3, 1, 0, 0, 0, 0, 0, 0},
9781: * model=V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1*/
1.186 brouard 9782: /* Main decodemodel */
9783:
1.187 brouard 9784:
1.223 brouard 9785: if(decodemodel(model, lastobs) == 1) /* In order to get Tvar[k] V4+V3+V5 p Tvar[1]@3 = {4, 3, 5}*/
1.136 brouard 9786: goto end;
9787:
1.137 brouard 9788: if((double)(lastobs-imx)/(double)imx > 1.10){
9789: nbwarn++;
9790: 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);
9791: 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);
9792: }
1.136 brouard 9793: /* if(mle==1){*/
1.137 brouard 9794: if (weightopt != 1) { /* Maximisation without weights. We can have weights different from 1 but want no weight*/
9795: for(i=1;i<=imx;i++) weight[i]=1.0; /* changed to imx */
1.136 brouard 9796: }
9797:
9798: /*-calculation of age at interview from date of interview and age at death -*/
9799: agev=matrix(1,maxwav,1,imx);
9800:
9801: if(calandcheckages(imx, maxwav, &agemin, &agemax, &nberr, &nbwarn) == 1)
9802: goto end;
9803:
1.126 brouard 9804:
1.136 brouard 9805: agegomp=(int)agemin;
9806: free_vector(moisnais,1,n);
9807: free_vector(annais,1,n);
1.126 brouard 9808: /* free_matrix(mint,1,maxwav,1,n);
9809: free_matrix(anint,1,maxwav,1,n);*/
1.215 brouard 9810: /* free_vector(moisdc,1,n); */
9811: /* free_vector(andc,1,n); */
1.145 brouard 9812: /* */
9813:
1.126 brouard 9814: wav=ivector(1,imx);
1.214 brouard 9815: /* dh=imatrix(1,lastpass-firstpass+1,1,imx); */
9816: /* bh=imatrix(1,lastpass-firstpass+1,1,imx); */
9817: /* mw=imatrix(1,lastpass-firstpass+1,1,imx); */
9818: 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.*/
9819: bh=imatrix(1,lastpass-firstpass+2,1,imx);
9820: mw=imatrix(1,lastpass-firstpass+2,1,imx);
1.126 brouard 9821:
9822: /* Concatenates waves */
1.214 brouard 9823: /* Concatenates waves: wav[i] is the number of effective (useful waves) of individual i.
9824: Death is a valid wave (if date is known).
9825: mw[mi][i] is the number of (mi=1 to wav[i]) effective wave out of mi of individual i
9826: dh[m][i] or dh[mw[mi][i]][i] is the delay between two effective waves m=mw[mi][i]
9827: and mw[mi+1][i]. dh depends on stepm.
9828: */
9829:
1.126 brouard 9830: concatwav(wav, dh, bh, mw, s, agedc, agev, firstpass, lastpass, imx, nlstate, stepm);
1.145 brouard 9831: /* */
9832:
1.215 brouard 9833: free_vector(moisdc,1,n);
9834: free_vector(andc,1,n);
9835:
1.126 brouard 9836: /* Routine tricode is to calculate cptcoveff (real number of unique covariates) and to associate covariable number and modality */
9837: nbcode=imatrix(0,NCOVMAX,0,NCOVMAX);
9838: ncodemax[1]=1;
1.145 brouard 9839: Ndum =ivector(-1,NCOVMAX);
1.225 brouard 9840: cptcoveff=0;
1.220 brouard 9841: if (ncovmodel-nagesqr > 2 ){ /* That is if covariate other than cst, age and age*age */
9842: tricode(&cptcoveff,Tvar,nbcode,imx, Ndum); /**< Fills nbcode[Tvar[j]][l]; */
1.227 brouard 9843: }
9844:
9845: ncovcombmax=pow(2,cptcoveff);
9846: invalidvarcomb=ivector(1, ncovcombmax);
9847: for(i=1;i<ncovcombmax;i++)
9848: invalidvarcomb[i]=0;
9849:
1.211 brouard 9850: /* Nbcode gives the value of the lth modality (currently 1 to 2) of jth covariate, in
1.186 brouard 9851: V2+V1*age, there are 3 covariates Tvar[2]=1 (V1).*/
1.211 brouard 9852: /* 1 to ncodemax[j] which is the maximum value of this jth covariate */
1.227 brouard 9853:
1.200 brouard 9854: /* codtab=imatrix(1,100,1,10);*/ /* codtab[h,k]=( (h-1) - mod(k-1,2**(k-1) )/2**(k-1) */
1.198 brouard 9855: /*printf(" codtab[1,1],codtab[100,10]=%d,%d\n", codtab[1][1],codtabm(100,10));*/
1.186 brouard 9856: /* codtab gives the value 1 or 2 of the hth combination of k covariates (1 or 2).*/
1.211 brouard 9857: /* nbcode[Tvaraff[j]][codtabm(h,j)]) : if there are only 2 modalities for a covariate j,
9858: * codtabm(h,j) gives its value classified at position h and nbcode gives how it is coded
9859: * (currently 0 or 1) in the data.
9860: * In a loop on h=1 to 2**k, and a loop on j (=1 to k), we get the value of
9861: * corresponding modality (h,j).
9862: */
9863:
1.145 brouard 9864: h=0;
9865: /*if (cptcovn > 0) */
1.126 brouard 9866: m=pow(2,cptcoveff);
9867:
1.144 brouard 9868: /**< codtab(h,k) k = codtab[h,k]=( (h-1) - mod(k-1,2**(k-1) )/2**(k-1) + 1
1.211 brouard 9869: * For k=4 covariates, h goes from 1 to m=2**k
9870: * codtabm(h,k)= (1 & (h-1) >> (k-1)) + 1;
9871: * #define codtabm(h,k) (1 & (h-1) >> (k-1))+1
1.186 brouard 9872: * h\k 1 2 3 4
1.143 brouard 9873: *______________________________
9874: * 1 i=1 1 i=1 1 i=1 1 i=1 1
9875: * 2 2 1 1 1
9876: * 3 i=2 1 2 1 1
9877: * 4 2 2 1 1
9878: * 5 i=3 1 i=2 1 2 1
9879: * 6 2 1 2 1
9880: * 7 i=4 1 2 2 1
9881: * 8 2 2 2 1
1.197 brouard 9882: * 9 i=5 1 i=3 1 i=2 1 2
9883: * 10 2 1 1 2
9884: * 11 i=6 1 2 1 2
9885: * 12 2 2 1 2
9886: * 13 i=7 1 i=4 1 2 2
9887: * 14 2 1 2 2
9888: * 15 i=8 1 2 2 2
9889: * 16 2 2 2 2
1.143 brouard 9890: */
1.212 brouard 9891: /* How to do the opposite? From combination h (=1 to 2**k) how to get the value on the covariates? */
1.211 brouard 9892: /* from h=5 and m, we get then number of covariates k=log(m)/log(2)=4
9893: * and the value of each covariate?
9894: * V1=1, V2=1, V3=2, V4=1 ?
9895: * h-1=4 and 4 is 0100 or reverse 0010, and +1 is 1121 ok.
9896: * h=6, 6-1=5, 5 is 0101, 1010, 2121, V1=2nd, V2=1st, V3=2nd, V4=1st.
9897: * In order to get the real value in the data, we use nbcode
9898: * nbcode[Tvar[3][2nd]]=1 and nbcode[Tvar[4][1]]=0
9899: * We are keeping this crazy system in order to be able (in the future?)
9900: * to have more than 2 values (0 or 1) for a covariate.
9901: * #define codtabm(h,k) (1 & (h-1) >> (k-1))+1
9902: * h=6, k=2? h-1=5=0101, reverse 1010, +1=2121, k=2nd position: value is 1: codtabm(6,2)=1
9903: * bbbbbbbb
9904: * 76543210
9905: * h-1 00000101 (6-1=5)
1.219 brouard 9906: *(h-1)>>(k-1)= 00000010 >> (2-1) = 1 right shift
1.211 brouard 9907: * &
9908: * 1 00000001 (1)
1.219 brouard 9909: * 00000000 = 1 & ((h-1) >> (k-1))
9910: * +1= 00000001 =1
1.211 brouard 9911: *
9912: * h=14, k=3 => h'=h-1=13, k'=k-1=2
9913: * h' 1101 =2^3+2^2+0x2^1+2^0
9914: * >>k' 11
9915: * & 00000001
9916: * = 00000001
9917: * +1 = 00000010=2 = codtabm(14,3)
9918: * Reverse h=6 and m=16?
9919: * cptcoveff=log(16)/log(2)=4 covariate: 6-1=5=0101 reversed=1010 +1=2121 =>V1=2, V2=1, V3=2, V4=1.
9920: * for (j=1 to cptcoveff) Vj=decodtabm(j,h,cptcoveff)
9921: * decodtabm(h,j,cptcoveff)= (((h-1) >> (j-1)) & 1) +1
9922: * decodtabm(h,j,cptcoveff)= (h <= (1<<cptcoveff)?(((h-1) >> (j-1)) & 1) +1 : -1)
9923: * V3=decodtabm(14,3,2**4)=2
9924: * h'=13 1101 =2^3+2^2+0x2^1+2^0
9925: *(h-1) >> (j-1) 0011 =13 >> 2
9926: * &1 000000001
9927: * = 000000001
9928: * +1= 000000010 =2
9929: * 2211
9930: * V1=1+1, V2=0+1, V3=1+1, V4=1+1
9931: * V3=2
1.220 brouard 9932: * codtabm and decodtabm are identical
1.211 brouard 9933: */
9934:
1.145 brouard 9935:
9936: free_ivector(Ndum,-1,NCOVMAX);
9937:
9938:
1.126 brouard 9939:
1.186 brouard 9940: /* Initialisation of ----------- gnuplot -------------*/
1.126 brouard 9941: strcpy(optionfilegnuplot,optionfilefiname);
9942: if(mle==-3)
1.201 brouard 9943: strcat(optionfilegnuplot,"-MORT_");
1.126 brouard 9944: strcat(optionfilegnuplot,".gp");
9945:
9946: if((ficgp=fopen(optionfilegnuplot,"w"))==NULL) {
9947: printf("Problem with file %s",optionfilegnuplot);
9948: }
9949: else{
1.204 brouard 9950: fprintf(ficgp,"\n# IMaCh-%s\n", version);
1.126 brouard 9951: fprintf(ficgp,"# %s\n", optionfilegnuplot);
1.141 brouard 9952: //fprintf(ficgp,"set missing 'NaNq'\n");
9953: fprintf(ficgp,"set datafile missing 'NaNq'\n");
1.126 brouard 9954: }
9955: /* fclose(ficgp);*/
1.186 brouard 9956:
9957:
9958: /* Initialisation of --------- index.htm --------*/
1.126 brouard 9959:
9960: strcpy(optionfilehtm,optionfilefiname); /* Main html file */
9961: if(mle==-3)
1.201 brouard 9962: strcat(optionfilehtm,"-MORT_");
1.126 brouard 9963: strcat(optionfilehtm,".htm");
9964: if((fichtm=fopen(optionfilehtm,"w"))==NULL) {
1.131 brouard 9965: printf("Problem with %s \n",optionfilehtm);
9966: exit(0);
1.126 brouard 9967: }
9968:
9969: strcpy(optionfilehtmcov,optionfilefiname); /* Only for matrix of covariance */
9970: strcat(optionfilehtmcov,"-cov.htm");
9971: if((fichtmcov=fopen(optionfilehtmcov,"w"))==NULL) {
9972: printf("Problem with %s \n",optionfilehtmcov), exit(0);
9973: }
9974: else{
9975: fprintf(fichtmcov,"<html><head>\n<title>IMaCh Cov %s</title></head>\n <body><font size=\"2\">%s <br> %s</font> \
9976: <hr size=\"2\" color=\"#EC5E5E\"> \n\
1.204 brouard 9977: Title=%s <br>Datafile=%s Firstpass=%d Lastpass=%d Stepm=%d Weight=%d Model=1+age+%s<br>\n",\
1.126 brouard 9978: optionfilehtmcov,version,fullversion,title,datafile,firstpass,lastpass,stepm, weightopt, model);
9979: }
9980:
1.213 brouard 9981: 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 9982: <hr size=\"2\" color=\"#EC5E5E\"> \n\
9983: <font size=\"2\">IMaCh-%s <br> %s</font> \
1.126 brouard 9984: <hr size=\"2\" color=\"#EC5E5E\"> \n\
1.204 brouard 9985: Title=%s <br>Datafile=%s Firstpass=%d Lastpass=%d Stepm=%d Weight=%d Model=1+age+%s<br>\n\
1.126 brouard 9986: \n\
9987: <hr size=\"2\" color=\"#EC5E5E\">\
9988: <ul><li><h4>Parameter files</h4>\n\
9989: - Parameter file: <a href=\"%s.%s\">%s.%s</a><br>\n\
9990: - Copy of the parameter file: <a href=\"o%s\">o%s</a><br>\n\
9991: - Log file of the run: <a href=\"%s\">%s</a><br>\n\
9992: - Gnuplot file name: <a href=\"%s\">%s</a><br>\n\
9993: - Date and time at start: %s</ul>\n",\
9994: optionfilehtm,version,fullversion,title,datafile,firstpass,lastpass,stepm, weightopt, model,\
9995: optionfilefiname,optionfilext,optionfilefiname,optionfilext,\
9996: fileres,fileres,\
9997: filelog,filelog,optionfilegnuplot,optionfilegnuplot,strstart);
9998: fflush(fichtm);
9999:
10000: strcpy(pathr,path);
10001: strcat(pathr,optionfilefiname);
1.184 brouard 10002: #ifdef WIN32
10003: _chdir(optionfilefiname); /* Move to directory named optionfile */
10004: #else
1.126 brouard 10005: chdir(optionfilefiname); /* Move to directory named optionfile */
1.184 brouard 10006: #endif
10007:
1.126 brouard 10008:
1.220 brouard 10009: /* Calculates basic frequencies. Computes observed prevalence at single age
10010: and for any valid combination of covariates
1.126 brouard 10011: and prints on file fileres'p'. */
1.227 brouard 10012: freqsummary(fileres, agemin, agemax, s, agev, nlstate, imx, Tvaraff, invalidvarcomb, nbcode, ncodemax,mint,anint,strstart, \
10013: firstpass, lastpass, stepm, weightopt, model);
1.126 brouard 10014:
10015: fprintf(fichtm,"\n");
10016: fprintf(fichtm,"<br>Total number of observations=%d <br>\n\
10017: Youngest age at first (selected) pass %.2f, oldest age %.2f<br>\n\
10018: Interval (in months) between two waves: Min=%d Max=%d Mean=%.2lf<br>\n",\
10019: imx,agemin,agemax,jmin,jmax,jmean);
10020: pmmij= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
1.220 brouard 10021: oldms= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
10022: newms= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
10023: savms= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
10024: oldm=oldms; newm=newms; savm=savms; /* Keeps fixed addresses to free */
1.218 brouard 10025:
1.126 brouard 10026: /* For Powell, parameters are in a vector p[] starting at p[1]
10027: so we point p on param[1][1] so that p[1] maps on param[1][1][1] */
10028: p=param[1][1]; /* *(*(*(param +1)+1)+0) */
10029:
10030: globpr=0; /* To get the number ipmx of contributions and the sum of weights*/
1.186 brouard 10031: /* For mortality only */
1.126 brouard 10032: if (mle==-3){
1.136 brouard 10033: ximort=matrix(1,NDIM,1,NDIM);
1.220 brouard 10034: for(i=1;i<=NDIM;i++)
10035: for(j=1;j<=NDIM;j++)
10036: ximort[i][j]=0.;
1.186 brouard 10037: /* ximort=gsl_matrix_alloc(1,NDIM,1,NDIM); */
1.126 brouard 10038: cens=ivector(1,n);
10039: ageexmed=vector(1,n);
10040: agecens=vector(1,n);
10041: dcwave=ivector(1,n);
1.223 brouard 10042:
1.126 brouard 10043: for (i=1; i<=imx; i++){
10044: dcwave[i]=-1;
10045: for (m=firstpass; m<=lastpass; m++)
1.226 brouard 10046: if (s[m][i]>nlstate) {
10047: dcwave[i]=m;
10048: /* printf("i=%d j=%d s=%d dcwave=%d\n",i,j, s[j][i],dcwave[i]);*/
10049: break;
10050: }
1.126 brouard 10051: }
1.226 brouard 10052:
1.126 brouard 10053: for (i=1; i<=imx; i++) {
10054: if (wav[i]>0){
1.226 brouard 10055: ageexmed[i]=agev[mw[1][i]][i];
10056: j=wav[i];
10057: agecens[i]=1.;
10058:
10059: if (ageexmed[i]> 1 && wav[i] > 0){
10060: agecens[i]=agev[mw[j][i]][i];
10061: cens[i]= 1;
10062: }else if (ageexmed[i]< 1)
10063: cens[i]= -1;
10064: if (agedc[i]< AGESUP && agedc[i]>1 && dcwave[i]>firstpass && dcwave[i]<=lastpass)
10065: cens[i]=0 ;
1.126 brouard 10066: }
10067: else cens[i]=-1;
10068: }
10069:
10070: for (i=1;i<=NDIM;i++) {
10071: for (j=1;j<=NDIM;j++)
1.226 brouard 10072: ximort[i][j]=(i == j ? 1.0 : 0.0);
1.126 brouard 10073: }
10074:
1.145 brouard 10075: /*p[1]=0.0268; p[NDIM]=0.083;*/
1.126 brouard 10076: /*printf("%lf %lf", p[1], p[2]);*/
10077:
10078:
1.136 brouard 10079: #ifdef GSL
10080: printf("GSL optimization\n"); fprintf(ficlog,"Powell\n");
1.162 brouard 10081: #else
1.126 brouard 10082: printf("Powell\n"); fprintf(ficlog,"Powell\n");
1.136 brouard 10083: #endif
1.201 brouard 10084: strcpy(filerespow,"POW-MORT_");
10085: strcat(filerespow,fileresu);
1.126 brouard 10086: if((ficrespow=fopen(filerespow,"w"))==NULL) {
10087: printf("Problem with resultfile: %s\n", filerespow);
10088: fprintf(ficlog,"Problem with resultfile: %s\n", filerespow);
10089: }
1.136 brouard 10090: #ifdef GSL
10091: fprintf(ficrespow,"# GSL optimization\n# iter -2*LL");
1.162 brouard 10092: #else
1.126 brouard 10093: fprintf(ficrespow,"# Powell\n# iter -2*LL");
1.136 brouard 10094: #endif
1.126 brouard 10095: /* for (i=1;i<=nlstate;i++)
10096: for(j=1;j<=nlstate+ndeath;j++)
10097: if(j!=i)fprintf(ficrespow," p%1d%1d",i,j);
10098: */
10099: fprintf(ficrespow,"\n");
1.136 brouard 10100: #ifdef GSL
10101: /* gsl starts here */
10102: T = gsl_multimin_fminimizer_nmsimplex;
10103: gsl_multimin_fminimizer *sfm = NULL;
10104: gsl_vector *ss, *x;
10105: gsl_multimin_function minex_func;
10106:
10107: /* Initial vertex size vector */
10108: ss = gsl_vector_alloc (NDIM);
10109:
10110: if (ss == NULL){
10111: GSL_ERROR_VAL ("failed to allocate space for ss", GSL_ENOMEM, 0);
10112: }
10113: /* Set all step sizes to 1 */
10114: gsl_vector_set_all (ss, 0.001);
10115:
10116: /* Starting point */
1.126 brouard 10117:
1.136 brouard 10118: x = gsl_vector_alloc (NDIM);
10119:
10120: if (x == NULL){
10121: gsl_vector_free(ss);
10122: GSL_ERROR_VAL ("failed to allocate space for x", GSL_ENOMEM, 0);
10123: }
10124:
10125: /* Initialize method and iterate */
10126: /* p[1]=0.0268; p[NDIM]=0.083; */
1.186 brouard 10127: /* gsl_vector_set(x, 0, 0.0268); */
10128: /* gsl_vector_set(x, 1, 0.083); */
1.136 brouard 10129: gsl_vector_set(x, 0, p[1]);
10130: gsl_vector_set(x, 1, p[2]);
10131:
10132: minex_func.f = &gompertz_f;
10133: minex_func.n = NDIM;
10134: minex_func.params = (void *)&p; /* ??? */
10135:
10136: sfm = gsl_multimin_fminimizer_alloc (T, NDIM);
10137: gsl_multimin_fminimizer_set (sfm, &minex_func, x, ss);
10138:
10139: printf("Iterations beginning .....\n\n");
10140: printf("Iter. # Intercept Slope -Log Likelihood Simplex size\n");
10141:
10142: iteri=0;
10143: while (rval == GSL_CONTINUE){
10144: iteri++;
10145: status = gsl_multimin_fminimizer_iterate(sfm);
10146:
10147: if (status) printf("error: %s\n", gsl_strerror (status));
10148: fflush(0);
10149:
10150: if (status)
10151: break;
10152:
10153: rval = gsl_multimin_test_size (gsl_multimin_fminimizer_size (sfm), 1e-6);
10154: ssval = gsl_multimin_fminimizer_size (sfm);
10155:
10156: if (rval == GSL_SUCCESS)
10157: printf ("converged to a local maximum at\n");
10158:
10159: printf("%5d ", iteri);
10160: for (it = 0; it < NDIM; it++){
10161: printf ("%10.5f ", gsl_vector_get (sfm->x, it));
10162: }
10163: printf("f() = %-10.5f ssize = %.7f\n", sfm->fval, ssval);
10164: }
10165:
10166: printf("\n\n Please note: Program should be run many times with varying starting points to detemine global maximum\n\n");
10167:
10168: gsl_vector_free(x); /* initial values */
10169: gsl_vector_free(ss); /* inital step size */
10170: for (it=0; it<NDIM; it++){
10171: p[it+1]=gsl_vector_get(sfm->x,it);
10172: fprintf(ficrespow," %.12lf", p[it]);
10173: }
10174: gsl_multimin_fminimizer_free (sfm); /* p *(sfm.x.data) et p *(sfm.x.data+1) */
10175: #endif
10176: #ifdef POWELL
10177: powell(p,ximort,NDIM,ftol,&iter,&fret,gompertz);
10178: #endif
1.126 brouard 10179: fclose(ficrespow);
10180:
1.203 brouard 10181: hesscov(matcov, hess, p, NDIM, delti, 1e-4, gompertz);
1.126 brouard 10182:
10183: for(i=1; i <=NDIM; i++)
10184: for(j=i+1;j<=NDIM;j++)
1.220 brouard 10185: matcov[i][j]=matcov[j][i];
1.126 brouard 10186:
10187: printf("\nCovariance matrix\n ");
1.203 brouard 10188: fprintf(ficlog,"\nCovariance matrix\n ");
1.126 brouard 10189: for(i=1; i <=NDIM; i++) {
10190: for(j=1;j<=NDIM;j++){
1.220 brouard 10191: printf("%f ",matcov[i][j]);
10192: fprintf(ficlog,"%f ",matcov[i][j]);
1.126 brouard 10193: }
1.203 brouard 10194: printf("\n "); fprintf(ficlog,"\n ");
1.126 brouard 10195: }
10196:
10197: printf("iter=%d MLE=%f Eq=%lf*exp(%lf*(age-%d))\n",iter,-gompertz(p),p[1],p[2],agegomp);
1.193 brouard 10198: for (i=1;i<=NDIM;i++) {
1.126 brouard 10199: printf("%f [%f ; %f]\n",p[i],p[i]-2*sqrt(matcov[i][i]),p[i]+2*sqrt(matcov[i][i]));
1.193 brouard 10200: fprintf(ficlog,"%f [%f ; %f]\n",p[i],p[i]-2*sqrt(matcov[i][i]),p[i]+2*sqrt(matcov[i][i]));
10201: }
1.126 brouard 10202: lsurv=vector(1,AGESUP);
10203: lpop=vector(1,AGESUP);
10204: tpop=vector(1,AGESUP);
10205: lsurv[agegomp]=100000;
10206:
10207: for (k=agegomp;k<=AGESUP;k++) {
10208: agemortsup=k;
10209: if (p[1]*exp(p[2]*(k-agegomp))>1) break;
10210: }
10211:
10212: for (k=agegomp;k<agemortsup;k++)
10213: lsurv[k+1]=lsurv[k]-lsurv[k]*(p[1]*exp(p[2]*(k-agegomp)));
10214:
10215: for (k=agegomp;k<agemortsup;k++){
10216: lpop[k]=(lsurv[k]+lsurv[k+1])/2.;
10217: sumlpop=sumlpop+lpop[k];
10218: }
10219:
10220: tpop[agegomp]=sumlpop;
10221: for (k=agegomp;k<(agemortsup-3);k++){
10222: /* tpop[k+1]=2;*/
10223: tpop[k+1]=tpop[k]-lpop[k];
10224: }
10225:
10226:
10227: printf("\nAge lx qx dx Lx Tx e(x)\n");
10228: for (k=agegomp;k<(agemortsup-2);k++)
10229: 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]);
10230:
10231:
10232: replace_back_to_slash(pathc,pathcd); /* Even gnuplot wants a / */
1.220 brouard 10233: ageminpar=50;
10234: agemaxpar=100;
1.194 brouard 10235: if(ageminpar == AGEOVERFLOW ||agemaxpar == AGEOVERFLOW){
10236: printf("Warning! Error in gnuplot file with ageminpar %f or agemaxpar %f overflow\n\
10237: This is probably because your parameter file doesn't \n contain the exact number of lines (or columns) corresponding to your model line.\n\
10238: Please run with mle=-1 to get a correct covariance matrix.\n",ageminpar,agemaxpar);
10239: fprintf(ficlog,"Warning! Error in gnuplot file with ageminpar %f or agemaxpar %f overflow\n\
10240: This is probably because your parameter file doesn't \n contain the exact number of lines (or columns) corresponding to your model line.\n\
10241: Please run with mle=-1 to get a correct covariance matrix.\n",ageminpar,agemaxpar);
1.220 brouard 10242: }else{
10243: printf("Warning! ageminpar %f and agemaxpar %f have been fixed because for simplification until it is fixed...\n\n",ageminpar,agemaxpar);
10244: 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 10245: printinggnuplotmort(fileresu, optionfilefiname,ageminpar,agemaxpar,fage, pathc,p);
1.220 brouard 10246: }
1.201 brouard 10247: printinghtmlmort(fileresu,title,datafile, firstpass, lastpass, \
1.126 brouard 10248: stepm, weightopt,\
10249: model,imx,p,matcov,agemortsup);
10250:
10251: free_vector(lsurv,1,AGESUP);
10252: free_vector(lpop,1,AGESUP);
10253: free_vector(tpop,1,AGESUP);
1.220 brouard 10254: free_matrix(ximort,1,NDIM,1,NDIM);
1.136 brouard 10255: free_ivector(cens,1,n);
10256: free_vector(agecens,1,n);
10257: free_ivector(dcwave,1,n);
1.220 brouard 10258: #ifdef GSL
1.136 brouard 10259: #endif
1.186 brouard 10260: } /* Endof if mle==-3 mortality only */
1.205 brouard 10261: /* Standard */
10262: else{ /* For mle !=- 3, could be 0 or 1 or 4 etc. */
10263: globpr=0;/* Computes sum of likelihood for globpr=1 and funcone */
10264: /* Computes likelihood for initial parameters, uses funcone to compute gpimx and gsw */
1.132 brouard 10265: likelione(ficres, p, npar, nlstate, &globpr, &ipmx, &sw, &fretone, funcone); /* Prints the contributions to the likelihood */
1.126 brouard 10266: printf("First Likeli=%12.6f ipmx=%ld sw=%12.6f",fretone,ipmx,sw);
10267: for (k=1; k<=npar;k++)
10268: printf(" %d %8.5f",k,p[k]);
10269: printf("\n");
1.205 brouard 10270: if(mle>=1){ /* Could be 1 or 2, Real Maximization */
10271: /* mlikeli uses func not funcone */
10272: mlikeli(ficres,p, npar, ncovmodel, nlstate, ftol, func);
10273: }
10274: if(mle==0) {/* No optimization, will print the likelihoods for the datafile */
10275: globpr=0;/* Computes sum of likelihood for globpr=1 and funcone */
10276: /* Computes likelihood for initial parameters, uses funcone to compute gpimx and gsw */
10277: likelione(ficres, p, npar, nlstate, &globpr, &ipmx, &sw, &fretone, funcone); /* Prints the contributions to the likelihood */
10278: }
10279: globpr=1; /* again, to print the individual contributions using computed gpimx and gsw */
1.126 brouard 10280: likelione(ficres, p, npar, nlstate, &globpr, &ipmx, &sw, &fretone, funcone); /* Prints the contributions to the likelihood */
10281: printf("Second Likeli=%12.6f ipmx=%ld sw=%12.6f",fretone,ipmx,sw);
10282: for (k=1; k<=npar;k++)
10283: printf(" %d %8.5f",k,p[k]);
10284: printf("\n");
10285:
10286: /*--------- results files --------------*/
1.224 brouard 10287: 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 10288:
10289:
10290: fprintf(ficres,"# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n");
10291: printf("# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n");
10292: fprintf(ficlog,"# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n");
10293: for(i=1,jk=1; i <=nlstate; i++){
10294: for(k=1; k <=(nlstate+ndeath); k++){
1.225 brouard 10295: if (k != i) {
10296: printf("%d%d ",i,k);
10297: fprintf(ficlog,"%d%d ",i,k);
10298: fprintf(ficres,"%1d%1d ",i,k);
10299: for(j=1; j <=ncovmodel; j++){
10300: printf("%12.7f ",p[jk]);
10301: fprintf(ficlog,"%12.7f ",p[jk]);
10302: fprintf(ficres,"%12.7f ",p[jk]);
10303: jk++;
10304: }
10305: printf("\n");
10306: fprintf(ficlog,"\n");
10307: fprintf(ficres,"\n");
10308: }
1.126 brouard 10309: }
10310: }
1.203 brouard 10311: if(mle != 0){
10312: /* Computing hessian and covariance matrix only at a peak of the Likelihood, that is after optimization */
1.126 brouard 10313: ftolhess=ftol; /* Usually correct */
1.203 brouard 10314: hesscov(matcov, hess, p, npar, delti, ftolhess, func);
10315: 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");
10316: 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");
10317: for(i=1,jk=1; i <=nlstate; i++){
1.225 brouard 10318: for(k=1; k <=(nlstate+ndeath); k++){
10319: if (k != i) {
10320: printf("%d%d ",i,k);
10321: fprintf(ficlog,"%d%d ",i,k);
10322: for(j=1; j <=ncovmodel; j++){
10323: 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]));
10324: 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]));
10325: jk++;
10326: }
10327: printf("\n");
10328: fprintf(ficlog,"\n");
10329: }
10330: }
1.193 brouard 10331: }
1.203 brouard 10332: } /* end of hesscov and Wald tests */
1.225 brouard 10333:
1.203 brouard 10334: /* */
1.126 brouard 10335: fprintf(ficres,"# Scales (for hessian or gradient estimation)\n");
10336: printf("# Scales (for hessian or gradient estimation)\n");
10337: fprintf(ficlog,"# Scales (for hessian or gradient estimation)\n");
10338: for(i=1,jk=1; i <=nlstate; i++){
10339: for(j=1; j <=nlstate+ndeath; j++){
1.225 brouard 10340: if (j!=i) {
10341: fprintf(ficres,"%1d%1d",i,j);
10342: printf("%1d%1d",i,j);
10343: fprintf(ficlog,"%1d%1d",i,j);
10344: for(k=1; k<=ncovmodel;k++){
10345: printf(" %.5e",delti[jk]);
10346: fprintf(ficlog," %.5e",delti[jk]);
10347: fprintf(ficres," %.5e",delti[jk]);
10348: jk++;
10349: }
10350: printf("\n");
10351: fprintf(ficlog,"\n");
10352: fprintf(ficres,"\n");
10353: }
1.126 brouard 10354: }
10355: }
10356:
10357: 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 10358: if(mle >= 1) /* To big for the screen */
1.126 brouard 10359: 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");
10360: 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");
10361: /* # 121 Var(a12)\n\ */
10362: /* # 122 Cov(b12,a12) Var(b12)\n\ */
10363: /* # 131 Cov(a13,a12) Cov(a13,b12, Var(a13)\n\ */
10364: /* # 132 Cov(b13,a12) Cov(b13,b12, Cov(b13,a13) Var(b13)\n\ */
10365: /* # 212 Cov(a21,a12) Cov(a21,b12, Cov(a21,a13) Cov(a21,b13) Var(a21)\n\ */
10366: /* # 212 Cov(b21,a12) Cov(b21,b12, Cov(b21,a13) Cov(b21,b13) Cov(b21,a21) Var(b21)\n\ */
10367: /* # 232 Cov(a23,a12) Cov(a23,b12, Cov(a23,a13) Cov(a23,b13) Cov(a23,a21) Cov(a23,b21) Var(a23)\n\ */
10368: /* # 232 Cov(b23,a12) Cov(b23,b12) ... Var (b23)\n" */
10369:
10370:
10371: /* Just to have a covariance matrix which will be more understandable
10372: even is we still don't want to manage dictionary of variables
10373: */
10374: for(itimes=1;itimes<=2;itimes++){
10375: jj=0;
10376: for(i=1; i <=nlstate; i++){
1.225 brouard 10377: for(j=1; j <=nlstate+ndeath; j++){
10378: if(j==i) continue;
10379: for(k=1; k<=ncovmodel;k++){
10380: jj++;
10381: ca[0]= k+'a'-1;ca[1]='\0';
10382: if(itimes==1){
10383: if(mle>=1)
10384: printf("#%1d%1d%d",i,j,k);
10385: fprintf(ficlog,"#%1d%1d%d",i,j,k);
10386: fprintf(ficres,"#%1d%1d%d",i,j,k);
10387: }else{
10388: if(mle>=1)
10389: printf("%1d%1d%d",i,j,k);
10390: fprintf(ficlog,"%1d%1d%d",i,j,k);
10391: fprintf(ficres,"%1d%1d%d",i,j,k);
10392: }
10393: ll=0;
10394: for(li=1;li <=nlstate; li++){
10395: for(lj=1;lj <=nlstate+ndeath; lj++){
10396: if(lj==li) continue;
10397: for(lk=1;lk<=ncovmodel;lk++){
10398: ll++;
10399: if(ll<=jj){
10400: cb[0]= lk +'a'-1;cb[1]='\0';
10401: if(ll<jj){
10402: if(itimes==1){
10403: if(mle>=1)
10404: printf(" Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj);
10405: fprintf(ficlog," Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj);
10406: fprintf(ficres," Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj);
10407: }else{
10408: if(mle>=1)
10409: printf(" %.5e",matcov[jj][ll]);
10410: fprintf(ficlog," %.5e",matcov[jj][ll]);
10411: fprintf(ficres," %.5e",matcov[jj][ll]);
10412: }
10413: }else{
10414: if(itimes==1){
10415: if(mle>=1)
10416: printf(" Var(%s%1d%1d)",ca,i,j);
10417: fprintf(ficlog," Var(%s%1d%1d)",ca,i,j);
10418: fprintf(ficres," Var(%s%1d%1d)",ca,i,j);
10419: }else{
10420: if(mle>=1)
10421: printf(" %.7e",matcov[jj][ll]);
10422: fprintf(ficlog," %.7e",matcov[jj][ll]);
10423: fprintf(ficres," %.7e",matcov[jj][ll]);
10424: }
10425: }
10426: }
10427: } /* end lk */
10428: } /* end lj */
10429: } /* end li */
10430: if(mle>=1)
10431: printf("\n");
10432: fprintf(ficlog,"\n");
10433: fprintf(ficres,"\n");
10434: numlinepar++;
10435: } /* end k*/
10436: } /*end j */
1.126 brouard 10437: } /* end i */
10438: } /* end itimes */
10439:
10440: fflush(ficlog);
10441: fflush(ficres);
1.225 brouard 10442: while(fgets(line, MAXLINE, ficpar)) {
10443: /* If line starts with a # it is a comment */
10444: if (line[0] == '#') {
10445: numlinepar++;
10446: fputs(line,stdout);
10447: fputs(line,ficparo);
10448: fputs(line,ficlog);
10449: continue;
10450: }else
10451: break;
10452: }
10453:
1.209 brouard 10454: /* while((c=getc(ficpar))=='#' && c!= EOF){ */
10455: /* ungetc(c,ficpar); */
10456: /* fgets(line, MAXLINE, ficpar); */
10457: /* fputs(line,stdout); */
10458: /* fputs(line,ficparo); */
10459: /* } */
10460: /* ungetc(c,ficpar); */
1.126 brouard 10461:
10462: estepm=0;
1.209 brouard 10463: 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 10464:
10465: if (num_filled != 6) {
10466: 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);
10467: 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);
10468: goto end;
10469: }
10470: printf("agemin=%lf agemax=%lf bage=%lf fage=%lf estepm=%d ftolpl=%lf\n",ageminpar,agemaxpar, bage, fage, estepm, ftolpl);
10471: }
10472: /* ftolpl=6*ftol*1.e5; /\* 6.e-3 make convergences in less than 80 loops for the prevalence limit *\/ */
10473: /*ftolpl=6.e-4;*/ /* 6.e-3 make convergences in less than 80 loops for the prevalence limit */
10474:
1.209 brouard 10475: /* fscanf(ficpar,"agemin=%lf agemax=%lf bage=%lf fage=%lf estepm=%d ftolpl=%\n",&ageminpar,&agemaxpar, &bage, &fage, &estepm); */
1.126 brouard 10476: if (estepm==0 || estepm < stepm) estepm=stepm;
10477: if (fage <= 2) {
10478: bage = ageminpar;
10479: fage = agemaxpar;
10480: }
10481:
10482: fprintf(ficres,"# agemin agemax for life expectancy, bage fage (if mle==0 ie no data nor Max likelihood).\n");
1.211 brouard 10483: fprintf(ficres,"agemin=%.0f agemax=%.0f bage=%.0f fage=%.0f estepm=%d ftolpl=%e\n",ageminpar,agemaxpar,bage,fage, estepm, ftolpl);
10484: fprintf(ficparo,"agemin=%.0f agemax=%.0f bage=%.0f fage=%.0f estepm=%d, ftolpl=%e\n",ageminpar,agemaxpar,bage,fage, estepm, ftolpl);
1.220 brouard 10485:
1.186 brouard 10486: /* Other stuffs, more or less useful */
1.126 brouard 10487: while((c=getc(ficpar))=='#' && c!= EOF){
10488: ungetc(c,ficpar);
10489: fgets(line, MAXLINE, ficpar);
1.141 brouard 10490: fputs(line,stdout);
1.126 brouard 10491: fputs(line,ficparo);
10492: }
10493: ungetc(c,ficpar);
10494:
10495: 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);
10496: 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);
10497: 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);
10498: printf("begin-prev-date=%.lf/%.lf/%.lf end-prev-date=%.lf/%.lf/%.lf mov_average=%d\n",jprev1, mprev1,anprev1,jprev2, mprev2,anprev2,mobilav);
10499: 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);
10500:
10501: while((c=getc(ficpar))=='#' && c!= EOF){
10502: ungetc(c,ficpar);
10503: fgets(line, MAXLINE, ficpar);
1.141 brouard 10504: fputs(line,stdout);
1.126 brouard 10505: fputs(line,ficparo);
10506: }
10507: ungetc(c,ficpar);
10508:
10509:
10510: dateprev1=anprev1+(mprev1-1)/12.+(jprev1-1)/365.;
10511: dateprev2=anprev2+(mprev2-1)/12.+(jprev2-1)/365.;
10512:
10513: fscanf(ficpar,"pop_based=%d\n",&popbased);
1.193 brouard 10514: fprintf(ficlog,"pop_based=%d\n",popbased);
1.126 brouard 10515: fprintf(ficparo,"pop_based=%d\n",popbased);
10516: fprintf(ficres,"pop_based=%d\n",popbased);
10517:
10518: while((c=getc(ficpar))=='#' && c!= EOF){
10519: ungetc(c,ficpar);
10520: fgets(line, MAXLINE, ficpar);
1.141 brouard 10521: fputs(line,stdout);
1.126 brouard 10522: fputs(line,ficparo);
10523: }
10524: ungetc(c,ficpar);
10525:
10526: 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);
10527: 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);
10528: 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);
10529: 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);
10530: 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);
10531: /* day and month of proj2 are not used but only year anproj2.*/
10532:
1.217 brouard 10533: while((c=getc(ficpar))=='#' && c!= EOF){
10534: ungetc(c,ficpar);
10535: fgets(line, MAXLINE, ficpar);
10536: fputs(line,stdout);
10537: fputs(line,ficparo);
10538: }
10539: ungetc(c,ficpar);
10540:
10541: 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 10542: 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);
10543: 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);
10544: 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 10545: /* day and month of proj2 are not used but only year anproj2.*/
1.126 brouard 10546:
1.230 brouard 10547: /* Results */
10548: while(fgets(line, MAXLINE, ficpar)) {
10549: /* If line starts with a # it is a comment */
10550: if (line[0] == '#') {
10551: numlinepar++;
10552: fputs(line,stdout);
10553: fputs(line,ficparo);
10554: fputs(line,ficlog);
10555: continue;
10556: }else
10557: break;
10558: }
10559: while((num_filled=sscanf(line,"result:%[^\n]\n",resultline)) !=EOF){
10560: if (num_filled == 0)
10561: resultline[0]='\0';
10562: else if (num_filled != 1){
10563: printf("ERROR %d: result line should be at minimum 'result=' %s\n",num_filled, line);
10564: }
10565: printf("Result %d: result line should be at minimum 'line=' %s, result=%s\n",num_filled, line, resultline);
10566: decoderesult(resultline);
10567: while(fgets(line, MAXLINE, ficpar)) {
10568: /* If line starts with a # it is a comment */
10569: if (line[0] == '#') {
10570: numlinepar++;
10571: fputs(line,stdout);
10572: fputs(line,ficparo);
10573: fputs(line,ficlog);
10574: continue;
10575: }else
10576: break;
10577: }
10578: if (feof(ficpar))
10579: break;
10580: else{ /* Processess output results for this combination of covariate values */
10581: }
10582: }
10583:
10584:
1.126 brouard 10585:
1.230 brouard 10586: /* freqsummary(fileres, agemin, agemax, s, agev, nlstate, imx,Tvaraff,nbcode, ncodemax,mint,anint); */
1.145 brouard 10587: /* ,dateprev1,dateprev2,jprev1, mprev1,anprev1,jprev2, mprev2,anprev2); */
1.126 brouard 10588:
10589: replace_back_to_slash(pathc,pathcd); /* Even gnuplot wants a / */
1.194 brouard 10590: if(ageminpar == AGEOVERFLOW ||agemaxpar == -AGEOVERFLOW){
1.230 brouard 10591: printf("Warning! Error in gnuplot file with ageminpar %f or agemaxpar %f overflow\n\
1.194 brouard 10592: This is probably because your parameter file doesn't \n contain the exact number of lines (or columns) corresponding to your model line.\n\
10593: Please run with mle=-1 to get a correct covariance matrix.\n",ageminpar,agemaxpar);
1.230 brouard 10594: fprintf(ficlog,"Warning! Error in gnuplot file with ageminpar %f or agemaxpar %f overflow\n\
1.194 brouard 10595: This is probably because your parameter file doesn't \n contain the exact number of lines (or columns) corresponding to your model line.\n\
10596: Please run with mle=-1 to get a correct covariance matrix.\n",ageminpar,agemaxpar);
1.220 brouard 10597: }else{
1.218 brouard 10598: printinggnuplot(fileresu, optionfilefiname,ageminpar,agemaxpar,fage, prevfcast, backcast, pathc,p);
1.220 brouard 10599: }
10600: printinghtml(fileresu,title,datafile, firstpass, lastpass, stepm, weightopt, \
1.225 brouard 10601: model,imx,jmin,jmax,jmean,rfileres,popforecast,prevfcast,backcast, estepm, \
10602: jprev1,mprev1,anprev1,dateprev1,jprev2,mprev2,anprev2,dateprev2);
1.220 brouard 10603:
1.225 brouard 10604: /*------------ free_vector -------------*/
10605: /* chdir(path); */
1.220 brouard 10606:
1.215 brouard 10607: /* free_ivector(wav,1,imx); */ /* Moved after last prevalence call */
10608: /* free_imatrix(dh,1,lastpass-firstpass+2,1,imx); */
10609: /* free_imatrix(bh,1,lastpass-firstpass+2,1,imx); */
10610: /* free_imatrix(mw,1,lastpass-firstpass+2,1,imx); */
1.126 brouard 10611: free_lvector(num,1,n);
10612: free_vector(agedc,1,n);
10613: /*free_matrix(covar,0,NCOVMAX,1,n);*/
10614: /*free_matrix(covar,1,NCOVMAX,1,n);*/
10615: fclose(ficparo);
10616: fclose(ficres);
1.220 brouard 10617:
10618:
1.186 brouard 10619: /* Other results (useful)*/
1.220 brouard 10620:
10621:
1.126 brouard 10622: /*--------------- Prevalence limit (period or stable prevalence) --------------*/
1.180 brouard 10623: /*#include "prevlim.h"*/ /* Use ficrespl, ficlog */
10624: prlim=matrix(1,nlstate,1,nlstate);
1.209 brouard 10625: prevalence_limit(p, prlim, ageminpar, agemaxpar, ftolpl, &ncvyear);
1.126 brouard 10626: fclose(ficrespl);
10627:
10628: /*------------- h Pij x at various ages ------------*/
1.180 brouard 10629: /*#include "hpijx.h"*/
10630: hPijx(p, bage, fage);
1.145 brouard 10631: fclose(ficrespij);
1.227 brouard 10632:
1.220 brouard 10633: /* ncovcombmax= pow(2,cptcoveff); */
1.219 brouard 10634: /*-------------- Variance of one-step probabilities---*/
1.145 brouard 10635: k=1;
1.126 brouard 10636: varprob(optionfilefiname, matcov, p, delti, nlstate, bage, fage,k,Tvar,nbcode, ncodemax,strstart);
1.227 brouard 10637:
1.219 brouard 10638: /* Prevalence for each covariates in probs[age][status][cov] */
1.218 brouard 10639: probs= ma3x(1,AGESUP,1,nlstate+ndeath, 1,ncovcombmax);
1.126 brouard 10640: for(i=1;i<=AGESUP;i++)
1.219 brouard 10641: for(j=1;j<=nlstate+ndeath;j++) /* ndeath is useless but a necessity to be compared with mobaverages */
1.225 brouard 10642: for(k=1;k<=ncovcombmax;k++)
10643: probs[i][j][k]=0.;
1.219 brouard 10644: prevalence(probs, ageminpar, agemaxpar, s, agev, nlstate, imx, Tvar, nbcode, ncodemax, mint, anint, dateprev1, dateprev2, firstpass, lastpass);
10645: if (mobilav!=0 ||mobilavproj !=0 ) {
10646: mobaverages= ma3x(1, AGESUP,1,nlstate+ndeath, 1,ncovcombmax);
1.227 brouard 10647: for(i=1;i<=AGESUP;i++)
10648: for(j=1;j<=nlstate;j++)
10649: for(k=1;k<=ncovcombmax;k++)
10650: mobaverages[i][j][k]=0.;
1.219 brouard 10651: mobaverage=mobaverages;
10652: if (mobilav!=0) {
1.227 brouard 10653: if (movingaverage(probs, ageminpar, agemaxpar, mobaverage, mobilav)!=0){
10654: fprintf(ficlog," Error in movingaverage mobilav=%d\n",mobilav);
10655: printf(" Error in movingaverage mobilav=%d\n",mobilav);
10656: }
1.219 brouard 10657: }
10658: /* /\* Prevalence for each covariates in probs[age][status][cov] *\/ */
10659: /* prevalence(probs, ageminpar, agemaxpar, s, agev, nlstate, imx, Tvar, nbcode, ncodemax, mint, anint, dateprev1, dateprev2, firstpass, lastpass); */
10660: else if (mobilavproj !=0) {
1.227 brouard 10661: if (movingaverage(probs, ageminpar, agemaxpar, mobaverage, mobilavproj)!=0){
10662: fprintf(ficlog," Error in movingaverage mobilavproj=%d\n",mobilavproj);
10663: printf(" Error in movingaverage mobilavproj=%d\n",mobilavproj);
10664: }
1.219 brouard 10665: }
10666: }/* end if moving average */
1.227 brouard 10667:
1.126 brouard 10668: /*---------- Forecasting ------------------*/
10669: /*if((stepm == 1) && (strcmp(model,".")==0)){*/
10670: if(prevfcast==1){
10671: /* if(stepm ==1){*/
1.225 brouard 10672: prevforecast(fileresu, anproj1, mproj1, jproj1, agemin, agemax, dateprev1, dateprev2, mobilavproj, bage, fage, firstpass, lastpass, anproj2, p, cptcoveff);
1.126 brouard 10673: }
1.217 brouard 10674: if(backcast==1){
1.219 brouard 10675: ddnewms=matrix(1,nlstate+ndeath,1,nlstate+ndeath);
10676: ddoldms=matrix(1,nlstate+ndeath,1,nlstate+ndeath);
10677: ddsavms=matrix(1,nlstate+ndeath,1,nlstate+ndeath);
10678:
10679: /*--------------- Back Prevalence limit (period or stable prevalence) --------------*/
10680:
10681: bprlim=matrix(1,nlstate,1,nlstate);
10682: back_prevalence_limit(p, bprlim, ageminpar, agemaxpar, ftolpl, &ncvyear, dateprev1, dateprev2, firstpass, lastpass, mobilavproj);
10683: fclose(ficresplb);
10684:
1.222 brouard 10685: hBijx(p, bage, fage, mobaverage);
10686: fclose(ficrespijb);
1.219 brouard 10687: free_matrix(bprlim,1,nlstate,1,nlstate); /*here or after loop ? */
10688:
10689: /* prevbackforecast(fileresu, anback1, mback1, jback1, agemin, agemax, dateprev1, dateprev2, mobilavproj,
1.225 brouard 10690: bage, fage, firstpass, lastpass, anback2, p, cptcoveff); */
1.219 brouard 10691: free_matrix(ddnewms, 1, nlstate+ndeath, 1, nlstate+ndeath);
10692: free_matrix(ddsavms, 1, nlstate+ndeath, 1, nlstate+ndeath);
10693: free_matrix(ddoldms, 1, nlstate+ndeath, 1, nlstate+ndeath);
10694: }
1.217 brouard 10695:
1.186 brouard 10696:
10697: /* ------ Other prevalence ratios------------ */
1.126 brouard 10698:
1.215 brouard 10699: free_ivector(wav,1,imx);
10700: free_imatrix(dh,1,lastpass-firstpass+2,1,imx);
10701: free_imatrix(bh,1,lastpass-firstpass+2,1,imx);
10702: free_imatrix(mw,1,lastpass-firstpass+2,1,imx);
1.218 brouard 10703:
10704:
1.127 brouard 10705: /*---------- Health expectancies, no variances ------------*/
1.218 brouard 10706:
1.201 brouard 10707: strcpy(filerese,"E_");
10708: strcat(filerese,fileresu);
1.126 brouard 10709: if((ficreseij=fopen(filerese,"w"))==NULL) {
10710: printf("Problem with Health Exp. resultfile: %s\n", filerese); exit(0);
10711: fprintf(ficlog,"Problem with Health Exp. resultfile: %s\n", filerese); exit(0);
10712: }
1.208 brouard 10713: printf("Computing Health Expectancies: result on file '%s' ...", filerese);fflush(stdout);
10714: fprintf(ficlog,"Computing Health Expectancies: result on file '%s' ...", filerese);fflush(ficlog);
1.219 brouard 10715:
1.227 brouard 10716: for (k=1; k <= (int) pow(2,cptcoveff); k++){ /* For any combination of dummy covariates, fixed and varying */
1.219 brouard 10717: fprintf(ficreseij,"\n#****** ");
1.225 brouard 10718: for(j=1;j<=cptcoveff;j++) {
1.227 brouard 10719: fprintf(ficreseij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
1.219 brouard 10720: }
10721: fprintf(ficreseij,"******\n");
10722:
10723: eij=ma3x(1,nlstate,1,nlstate,(int) bage, (int) fage);
10724: oldm=oldms;savm=savms;
10725: evsij(eij, p, nlstate, stepm, (int) bage, (int)fage, oldm, savm, k, estepm, strstart);
1.127 brouard 10726:
1.219 brouard 10727: free_ma3x(eij,1,nlstate,1,nlstate,(int) bage, (int)fage);
1.127 brouard 10728: }
10729: fclose(ficreseij);
1.208 brouard 10730: printf("done evsij\n");fflush(stdout);
10731: fprintf(ficlog,"done evsij\n");fflush(ficlog);
1.218 brouard 10732:
1.227 brouard 10733: /*---------- State-specific expectancies and variances ------------*/
1.218 brouard 10734:
10735:
1.201 brouard 10736: strcpy(filerest,"T_");
10737: strcat(filerest,fileresu);
1.127 brouard 10738: if((ficrest=fopen(filerest,"w"))==NULL) {
10739: printf("Problem with total LE resultfile: %s\n", filerest);goto end;
10740: fprintf(ficlog,"Problem with total LE resultfile: %s\n", filerest);goto end;
10741: }
1.208 brouard 10742: printf("Computing Total Life expectancies with their standard errors: file '%s' ...\n", filerest); fflush(stdout);
10743: fprintf(ficlog,"Computing Total Life expectancies with their standard errors: file '%s' ...\n", filerest); fflush(ficlog);
1.218 brouard 10744:
1.126 brouard 10745:
1.201 brouard 10746: strcpy(fileresstde,"STDE_");
10747: strcat(fileresstde,fileresu);
1.126 brouard 10748: if((ficresstdeij=fopen(fileresstde,"w"))==NULL) {
1.227 brouard 10749: printf("Problem with State specific Exp. and std errors resultfile: %s\n", fileresstde); exit(0);
10750: fprintf(ficlog,"Problem with State specific Exp. and std errors resultfile: %s\n", fileresstde); exit(0);
1.126 brouard 10751: }
1.227 brouard 10752: printf(" Computing State-specific Expectancies and standard errors: result on file '%s' \n", fileresstde);
10753: fprintf(ficlog," Computing State-specific Expectancies and standard errors: result on file '%s' \n", fileresstde);
1.126 brouard 10754:
1.201 brouard 10755: strcpy(filerescve,"CVE_");
10756: strcat(filerescve,fileresu);
1.126 brouard 10757: if((ficrescveij=fopen(filerescve,"w"))==NULL) {
1.227 brouard 10758: printf("Problem with Covar. State-specific Exp. resultfile: %s\n", filerescve); exit(0);
10759: fprintf(ficlog,"Problem with Covar. State-specific Exp. resultfile: %s\n", filerescve); exit(0);
1.126 brouard 10760: }
1.227 brouard 10761: printf(" Computing Covar. of State-specific Expectancies: result on file '%s' \n", filerescve);
10762: fprintf(ficlog," Computing Covar. of State-specific Expectancies: result on file '%s' \n", filerescve);
1.126 brouard 10763:
1.201 brouard 10764: strcpy(fileresv,"V_");
10765: strcat(fileresv,fileresu);
1.126 brouard 10766: if((ficresvij=fopen(fileresv,"w"))==NULL) {
10767: printf("Problem with variance resultfile: %s\n", fileresv);exit(0);
10768: fprintf(ficlog,"Problem with variance resultfile: %s\n", fileresv);exit(0);
10769: }
1.227 brouard 10770: printf(" Computing Variance-covariance of State-specific Expectancies: file '%s' ... ", fileresv);fflush(stdout);
10771: fprintf(ficlog," Computing Variance-covariance of State-specific Expectancies: file '%s' ... ", fileresv);fflush(ficlog);
1.126 brouard 10772:
1.145 brouard 10773: /*for(cptcov=1,k=0;cptcov<=i1;cptcov++){
10774: for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){*/
10775:
1.225 brouard 10776: for (k=1; k <= (int) pow(2,cptcoveff); k++){
1.227 brouard 10777: printf("\n#****** ");
1.208 brouard 10778: fprintf(ficrest,"\n#****** ");
1.227 brouard 10779: fprintf(ficlog,"\n#****** ");
10780: for(j=1;j<=cptcoveff;j++){
10781: printf("V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
10782: fprintf(ficrest,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
10783: fprintf(ficlog,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
10784: }
1.208 brouard 10785: fprintf(ficrest,"******\n");
1.227 brouard 10786: fprintf(ficlog,"******\n");
10787: printf("******\n");
1.208 brouard 10788:
10789: fprintf(ficresstdeij,"\n#****** ");
10790: fprintf(ficrescveij,"\n#****** ");
1.225 brouard 10791: for(j=1;j<=cptcoveff;j++) {
1.227 brouard 10792: fprintf(ficresstdeij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
10793: fprintf(ficrescveij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
1.208 brouard 10794: }
10795: fprintf(ficresstdeij,"******\n");
10796: fprintf(ficrescveij,"******\n");
10797:
10798: fprintf(ficresvij,"\n#****** ");
1.225 brouard 10799: for(j=1;j<=cptcoveff;j++)
1.227 brouard 10800: fprintf(ficresvij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
1.208 brouard 10801: fprintf(ficresvij,"******\n");
10802:
10803: eij=ma3x(1,nlstate,1,nlstate,(int) bage, (int) fage);
10804: oldm=oldms;savm=savms;
1.227 brouard 10805: printf(" cvevsij combination#=%d, ",k);
10806: fprintf(ficlog, " cvevsij combination#=%d, ",k);
1.208 brouard 10807: cvevsij(eij, p, nlstate, stepm, (int) bage, (int)fage, oldm, savm, k, estepm, delti, matcov, strstart);
10808: printf(" end cvevsij \n ");
10809: fprintf(ficlog, " end cvevsij \n ");
10810:
10811: /*
10812: */
10813: /* goto endfree; */
10814:
10815: vareij=ma3x(1,nlstate,1,nlstate,(int) bage, (int) fage);
10816: pstamp(ficrest);
10817:
10818:
10819: for(vpopbased=0; vpopbased <= popbased; vpopbased++){ /* Done for vpopbased=0 and vpopbased=1 if popbased==1*/
1.227 brouard 10820: oldm=oldms;savm=savms; /* ZZ Segmentation fault */
10821: cptcod= 0; /* To be deleted */
10822: printf("varevsij vpopbased=%d \n",vpopbased);
10823: fprintf(ficlog, "varevsij vpopbased=%d \n",vpopbased);
10824: varevsij(optionfilefiname, vareij, matcov, p, delti, nlstate, stepm, (int) bage, (int) fage, oldm, savm, prlim, ftolpl, &ncvyear, k, estepm, cptcov,cptcod,vpopbased,mobilav, strstart); /* cptcod not initialized Intel */
10825: 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 ");
10826: if(vpopbased==1)
10827: 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);
10828: else
10829: fprintf(ficrest,"the age specific period (stable) prevalences in each health state \n");
10830: fprintf(ficrest,"# Age popbased mobilav e.. (std) ");
10831: for (i=1;i<=nlstate;i++) fprintf(ficrest,"e.%d (std) ",i);
10832: fprintf(ficrest,"\n");
10833: /* printf("Which p?\n"); for(i=1;i<=npar;i++)printf("p[i=%d]=%lf,",i,p[i]);printf("\n"); */
10834: epj=vector(1,nlstate+1);
10835: printf("Computing age specific period (stable) prevalences in each health state \n");
10836: fprintf(ficlog,"Computing age specific period (stable) prevalences in each health state \n");
10837: for(age=bage; age <=fage ;age++){
10838: prevalim(prlim, nlstate, p, age, oldm, savm, ftolpl, &ncvyear, k); /*ZZ Is it the correct prevalim */
10839: if (vpopbased==1) {
10840: if(mobilav ==0){
10841: for(i=1; i<=nlstate;i++)
10842: prlim[i][i]=probs[(int)age][i][k];
10843: }else{ /* mobilav */
10844: for(i=1; i<=nlstate;i++)
10845: prlim[i][i]=mobaverage[(int)age][i][k];
10846: }
10847: }
1.219 brouard 10848:
1.227 brouard 10849: fprintf(ficrest," %4.0f %d %d",age, vpopbased, mobilav);
10850: /* fprintf(ficrest," %4.0f %d %d %d %d",age, vpopbased, mobilav,Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]); */ /* to be done */
10851: /* printf(" age %4.0f ",age); */
10852: for(j=1, epj[nlstate+1]=0.;j <=nlstate;j++){
10853: for(i=1, epj[j]=0.;i <=nlstate;i++) {
10854: epj[j] += prlim[i][i]*eij[i][j][(int)age];
10855: /*ZZZ printf("%lf %lf ", prlim[i][i] ,eij[i][j][(int)age]);*/
10856: /* printf("%lf %lf ", prlim[i][i] ,eij[i][j][(int)age]); */
10857: }
10858: epj[nlstate+1] +=epj[j];
10859: }
10860: /* printf(" age %4.0f \n",age); */
1.219 brouard 10861:
1.227 brouard 10862: for(i=1, vepp=0.;i <=nlstate;i++)
10863: for(j=1;j <=nlstate;j++)
10864: vepp += vareij[i][j][(int)age];
10865: fprintf(ficrest," %7.3f (%7.3f)", epj[nlstate+1],sqrt(vepp));
10866: for(j=1;j <=nlstate;j++){
10867: fprintf(ficrest," %7.3f (%7.3f)", epj[j],sqrt(vareij[j][j][(int)age]));
10868: }
10869: fprintf(ficrest,"\n");
10870: }
1.208 brouard 10871: } /* End vpopbased */
10872: free_ma3x(eij,1,nlstate,1,nlstate,(int) bage, (int)fage);
10873: free_ma3x(vareij,1,nlstate,1,nlstate,(int) bage, (int)fage);
10874: free_vector(epj,1,nlstate+1);
10875: printf("done \n");fflush(stdout);
10876: fprintf(ficlog,"done\n");fflush(ficlog);
10877:
1.145 brouard 10878: /*}*/
1.208 brouard 10879: } /* End k */
1.227 brouard 10880:
10881: printf("done State-specific expectancies\n");fflush(stdout);
10882: fprintf(ficlog,"done State-specific expectancies\n");fflush(ficlog);
10883:
1.126 brouard 10884: /*------- Variance of period (stable) prevalence------*/
1.227 brouard 10885:
1.201 brouard 10886: strcpy(fileresvpl,"VPL_");
10887: strcat(fileresvpl,fileresu);
1.126 brouard 10888: if((ficresvpl=fopen(fileresvpl,"w"))==NULL) {
10889: printf("Problem with variance of period (stable) prevalence resultfile: %s\n", fileresvpl);
10890: exit(0);
10891: }
1.208 brouard 10892: printf("Computing Variance-covariance of period (stable) prevalence: file '%s' ...", fileresvpl);fflush(stdout);
10893: fprintf(ficlog, "Computing Variance-covariance of period (stable) prevalence: file '%s' ...", fileresvpl);fflush(ficlog);
1.227 brouard 10894:
1.145 brouard 10895: /*for(cptcov=1,k=0;cptcov<=i1;cptcov++){
10896: for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){*/
1.227 brouard 10897:
1.225 brouard 10898: for (k=1; k <= (int) pow(2,cptcoveff); k++){
1.227 brouard 10899: fprintf(ficresvpl,"\n#****** ");
10900: printf("\n#****** ");
10901: fprintf(ficlog,"\n#****** ");
10902: for(j=1;j<=cptcoveff;j++) {
10903: fprintf(ficresvpl,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
10904: fprintf(ficlog,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
10905: printf("V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
10906: }
10907: fprintf(ficresvpl,"******\n");
10908: printf("******\n");
10909: fprintf(ficlog,"******\n");
10910:
10911: varpl=matrix(1,nlstate,(int) bage, (int) fage);
10912: oldm=oldms;savm=savms;
10913: varprevlim(fileres, varpl, matcov, p, delti, nlstate, stepm, (int) bage, (int) fage, oldm, savm, prlim, ftolpl, &ncvyear, k, strstart);
10914: free_matrix(varpl,1,nlstate,(int) bage, (int)fage);
1.145 brouard 10915: /*}*/
1.126 brouard 10916: }
1.227 brouard 10917:
1.126 brouard 10918: fclose(ficresvpl);
1.208 brouard 10919: printf("done variance-covariance of period prevalence\n");fflush(stdout);
10920: fprintf(ficlog,"done variance-covariance of period prevalence\n");fflush(ficlog);
1.227 brouard 10921:
10922: free_vector(weight,1,n);
10923: free_imatrix(Tvard,1,NCOVMAX,1,2);
10924: free_imatrix(s,1,maxwav+1,1,n);
10925: free_matrix(anint,1,maxwav,1,n);
10926: free_matrix(mint,1,maxwav,1,n);
10927: free_ivector(cod,1,n);
10928: free_ivector(tab,1,NCOVMAX);
10929: fclose(ficresstdeij);
10930: fclose(ficrescveij);
10931: fclose(ficresvij);
10932: fclose(ficrest);
10933: fclose(ficpar);
10934:
10935:
1.126 brouard 10936: /*---------- End : free ----------------*/
1.219 brouard 10937: if (mobilav!=0 ||mobilavproj !=0)
10938: 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 10939: free_ma3x(probs,1,AGESUP,1,nlstate+ndeath, 1,ncovcombmax);
1.220 brouard 10940: free_matrix(prlim,1,nlstate,1,nlstate); /*here or after loop ? */
10941: free_matrix(pmmij,1,nlstate+ndeath,1,nlstate+ndeath);
1.126 brouard 10942: } /* mle==-3 arrives here for freeing */
1.227 brouard 10943: /* endfree:*/
10944: free_matrix(oldms, 1,nlstate+ndeath,1,nlstate+ndeath);
10945: free_matrix(newms, 1,nlstate+ndeath,1,nlstate+ndeath);
10946: free_matrix(savms, 1,nlstate+ndeath,1,nlstate+ndeath);
10947: free_ma3x(cotqvar,1,maxwav,1,nqtv,1,n);
1.233 brouard 10948: free_ma3x(cotvar,1,maxwav,1,ntv+nqtv,1,n);
1.227 brouard 10949: free_matrix(coqvar,1,maxwav,1,n);
10950: free_matrix(covar,0,NCOVMAX,1,n);
10951: free_matrix(matcov,1,npar,1,npar);
10952: free_matrix(hess,1,npar,1,npar);
10953: /*free_vector(delti,1,npar);*/
10954: free_ma3x(delti3,1,nlstate,1, nlstate+ndeath-1,1,ncovmodel);
10955: free_matrix(agev,1,maxwav,1,imx);
10956: free_ma3x(param,1,nlstate,1, nlstate+ndeath-1,1,ncovmodel);
10957:
10958: free_ivector(ncodemax,1,NCOVMAX);
10959: free_ivector(ncodemaxwundef,1,NCOVMAX);
10960: free_ivector(Dummy,-1,NCOVMAX);
10961: free_ivector(Fixed,-1,NCOVMAX);
10962: free_ivector(Typevar,-1,NCOVMAX);
10963: free_ivector(Tvar,1,NCOVMAX);
1.234 ! brouard 10964: free_ivector(TvarsQ,1,NCOVMAX);
! 10965: free_ivector(TvarsQind,1,NCOVMAX);
! 10966: free_ivector(TvarsD,1,NCOVMAX);
! 10967: free_ivector(TvarsDind,1,NCOVMAX);
1.231 brouard 10968: free_ivector(TvarFD,1,NCOVMAX);
10969: free_ivector(TvarFDind,1,NCOVMAX);
1.232 brouard 10970: free_ivector(TvarF,1,NCOVMAX);
10971: free_ivector(TvarFind,1,NCOVMAX);
10972: free_ivector(TvarV,1,NCOVMAX);
10973: free_ivector(TvarVind,1,NCOVMAX);
10974: free_ivector(TvarA,1,NCOVMAX);
10975: free_ivector(TvarAind,1,NCOVMAX);
1.231 brouard 10976: free_ivector(TvarFQ,1,NCOVMAX);
10977: free_ivector(TvarFQind,1,NCOVMAX);
10978: free_ivector(TvarVD,1,NCOVMAX);
10979: free_ivector(TvarVDind,1,NCOVMAX);
10980: free_ivector(TvarVQ,1,NCOVMAX);
10981: free_ivector(TvarVQind,1,NCOVMAX);
1.230 brouard 10982: free_ivector(Tvarsel,1,NCOVMAX);
10983: free_vector(Tvalsel,1,NCOVMAX);
1.227 brouard 10984: free_ivector(Tposprod,1,NCOVMAX);
10985: free_ivector(Tprod,1,NCOVMAX);
10986: free_ivector(Tvaraff,1,NCOVMAX);
10987: free_ivector(invalidvarcomb,1,ncovcombmax);
10988: free_ivector(Tage,1,NCOVMAX);
10989: free_ivector(Tmodelind,1,NCOVMAX);
1.228 brouard 10990: free_ivector(TmodelInvind,1,NCOVMAX);
10991: free_ivector(TmodelInvQind,1,NCOVMAX);
1.227 brouard 10992:
10993: free_imatrix(nbcode,0,NCOVMAX,0,NCOVMAX);
10994: /* free_imatrix(codtab,1,100,1,10); */
1.126 brouard 10995: fflush(fichtm);
10996: fflush(ficgp);
10997:
1.227 brouard 10998:
1.126 brouard 10999: if((nberr >0) || (nbwarn>0)){
1.216 brouard 11000: printf("End of Imach with %d errors and/or %d warnings. Please look at the log file for details.\n",nberr,nbwarn);
11001: 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 11002: }else{
11003: printf("End of Imach\n");
11004: fprintf(ficlog,"End of Imach\n");
11005: }
11006: printf("See log file on %s\n",filelog);
11007: /* gettimeofday(&end_time, (struct timezone*)0);*/ /* after time */
1.157 brouard 11008: /*(void) gettimeofday(&end_time,&tzp);*/
11009: rend_time = time(NULL);
11010: end_time = *localtime(&rend_time);
11011: /* tml = *localtime(&end_time.tm_sec); */
11012: strcpy(strtend,asctime(&end_time));
1.126 brouard 11013: printf("Local time at start %s\nLocal time at end %s",strstart, strtend);
11014: fprintf(ficlog,"Local time at start %s\nLocal time at end %s\n",strstart, strtend);
1.157 brouard 11015: printf("Total time used %s\n", asc_diff_time(rend_time -rstart_time,tmpout));
1.227 brouard 11016:
1.157 brouard 11017: printf("Total time was %.0lf Sec.\n", difftime(rend_time,rstart_time));
11018: fprintf(ficlog,"Total time used %s\n", asc_diff_time(rend_time -rstart_time,tmpout));
11019: fprintf(ficlog,"Total time was %.0lf Sec.\n", difftime(rend_time,rstart_time));
1.126 brouard 11020: /* printf("Total time was %d uSec.\n", total_usecs);*/
11021: /* if(fileappend(fichtm,optionfilehtm)){ */
11022: fprintf(fichtm,"<br>Local time at start %s<br>Local time at end %s<br>\n</body></html>",strstart, strtend);
11023: fclose(fichtm);
11024: fprintf(fichtmcov,"<br>Local time at start %s<br>Local time at end %s<br>\n</body></html>",strstart, strtend);
11025: fclose(fichtmcov);
11026: fclose(ficgp);
11027: fclose(ficlog);
11028: /*------ End -----------*/
1.227 brouard 11029:
11030:
11031: printf("Before Current directory %s!\n",pathcd);
1.184 brouard 11032: #ifdef WIN32
1.227 brouard 11033: if (_chdir(pathcd) != 0)
11034: printf("Can't move to directory %s!\n",path);
11035: if(_getcwd(pathcd,MAXLINE) > 0)
1.184 brouard 11036: #else
1.227 brouard 11037: if(chdir(pathcd) != 0)
11038: printf("Can't move to directory %s!\n", path);
11039: if (getcwd(pathcd, MAXLINE) > 0)
1.184 brouard 11040: #endif
1.126 brouard 11041: printf("Current directory %s!\n",pathcd);
11042: /*strcat(plotcmd,CHARSEPARATOR);*/
11043: sprintf(plotcmd,"gnuplot");
1.157 brouard 11044: #ifdef _WIN32
1.126 brouard 11045: sprintf(plotcmd,"\"%sgnuplot.exe\"",pathimach);
11046: #endif
11047: if(!stat(plotcmd,&info)){
1.158 brouard 11048: printf("Error or gnuplot program not found: '%s'\n",plotcmd);fflush(stdout);
1.126 brouard 11049: if(!stat(getenv("GNUPLOTBIN"),&info)){
1.158 brouard 11050: printf("Error or gnuplot program not found: '%s' Environment GNUPLOTBIN not set.\n",plotcmd);fflush(stdout);
1.126 brouard 11051: }else
11052: strcpy(pplotcmd,plotcmd);
1.157 brouard 11053: #ifdef __unix
1.126 brouard 11054: strcpy(plotcmd,GNUPLOTPROGRAM);
11055: if(!stat(plotcmd,&info)){
1.158 brouard 11056: printf("Error gnuplot program not found: '%s'\n",plotcmd);fflush(stdout);
1.126 brouard 11057: }else
11058: strcpy(pplotcmd,plotcmd);
11059: #endif
11060: }else
11061: strcpy(pplotcmd,plotcmd);
11062:
11063: sprintf(plotcmd,"%s %s",pplotcmd, optionfilegnuplot);
1.158 brouard 11064: printf("Starting graphs with: '%s'\n",plotcmd);fflush(stdout);
1.227 brouard 11065:
1.126 brouard 11066: if((outcmd=system(plotcmd)) != 0){
1.158 brouard 11067: printf("gnuplot command might not be in your path: '%s', err=%d\n", plotcmd, outcmd);
1.154 brouard 11068: printf("\n Trying if gnuplot resides on the same directory that IMaCh\n");
1.152 brouard 11069: sprintf(plotcmd,"%sgnuplot %s", pathimach, optionfilegnuplot);
1.150 brouard 11070: if((outcmd=system(plotcmd)) != 0)
1.153 brouard 11071: printf("\n Still a problem with gnuplot command %s, err=%d\n", plotcmd, outcmd);
1.126 brouard 11072: }
1.158 brouard 11073: printf(" Successful, please wait...");
1.126 brouard 11074: while (z[0] != 'q') {
11075: /* chdir(path); */
1.154 brouard 11076: printf("\nType e to edit results with your browser, g to graph again and q for exit: ");
1.126 brouard 11077: scanf("%s",z);
11078: /* if (z[0] == 'c') system("./imach"); */
11079: if (z[0] == 'e') {
1.158 brouard 11080: #ifdef __APPLE__
1.152 brouard 11081: sprintf(pplotcmd, "open %s", optionfilehtm);
1.157 brouard 11082: #elif __linux
11083: sprintf(pplotcmd, "xdg-open %s", optionfilehtm);
1.153 brouard 11084: #else
1.152 brouard 11085: sprintf(pplotcmd, "%s", optionfilehtm);
1.153 brouard 11086: #endif
11087: printf("Starting browser with: %s",pplotcmd);fflush(stdout);
11088: system(pplotcmd);
1.126 brouard 11089: }
11090: else if (z[0] == 'g') system(plotcmd);
11091: else if (z[0] == 'q') exit(0);
11092: }
1.227 brouard 11093: end:
1.126 brouard 11094: while (z[0] != 'q') {
1.195 brouard 11095: printf("\nType q for exiting: "); fflush(stdout);
1.126 brouard 11096: scanf("%s",z);
11097: }
11098: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>