File:
[Local Repository] /
imach /
src /
imach.c
Revision
1.338:
download - view:
text,
annotated -
select for diffs
Sun Sep 4 17:40:33 2022 UTC (23 months, 2 weeks ago) by
brouard
Branches:
MAIN
CVS tags:
HEAD
Summary: 0.99r36
* imach.c (Module): Now the easy runs i.e. without result or
model=1+age only did not work. The defautl combination should be 1
and not 0 because everything hasn't been tranformed yet.
1: /* $Id: imach.c,v 1.338 2022/09/04 17:40:33 brouard Exp $
2: $State: Exp $
3: $Log: imach.c,v $
4: Revision 1.338 2022/09/04 17:40:33 brouard
5: Summary: 0.99r36
6:
7: * imach.c (Module): Now the easy runs i.e. without result or
8: model=1+age only did not work. The defautl combination should be 1
9: and not 0 because everything hasn't been tranformed yet.
10:
11: Revision 1.337 2022/09/02 14:26:02 brouard
12: Summary: version 0.99r35
13:
14: * src/imach.c: Version 0.99r35 because it outputs same results with
15: 1+age+V1+V1*age for females and 1+age for females only
16: (education=1 noweight)
17:
18: Revision 1.336 2022/08/31 09:52:36 brouard
19: *** empty log message ***
20:
21: Revision 1.335 2022/08/31 08:23:16 brouard
22: Summary: improvements...
23:
24: Revision 1.334 2022/08/25 09:08:41 brouard
25: Summary: In progress for quantitative
26:
27: Revision 1.333 2022/08/21 09:10:30 brouard
28: * src/imach.c (Module): Version 0.99r33 A lot of changes in
29: reassigning covariates: my first idea was that people will always
30: use the first covariate V1 into the model but in fact they are
31: producing data with many covariates and can use an equation model
32: with some of the covariate; it means that in a model V2+V3 instead
33: of codtabm(k,Tvaraff[j]) which calculates for combination k, for
34: three covariates (V1, V2, V3) the value of Tvaraff[j], but in fact
35: the equation model is restricted to two variables only (V2, V3)
36: and the combination for V2 should be codtabm(k,1) instead of
37: (codtabm(k,2), and the code should be
38: codtabm(k,TnsdVar[Tvaraff[j]]. Many many changes have been
39: made. All of these should be simplified once a day like we did in
40: hpxij() for example by using precov[nres] which is computed in
41: decoderesult for each nres of each resultline. Loop should be done
42: on the equation model globally by distinguishing only product with
43: age (which are changing with age) and no more on type of
44: covariates, single dummies, single covariates.
45:
46: Revision 1.332 2022/08/21 09:06:25 brouard
47: Summary: Version 0.99r33
48:
49: * src/imach.c (Module): Version 0.99r33 A lot of changes in
50: reassigning covariates: my first idea was that people will always
51: use the first covariate V1 into the model but in fact they are
52: producing data with many covariates and can use an equation model
53: with some of the covariate; it means that in a model V2+V3 instead
54: of codtabm(k,Tvaraff[j]) which calculates for combination k, for
55: three covariates (V1, V2, V3) the value of Tvaraff[j], but in fact
56: the equation model is restricted to two variables only (V2, V3)
57: and the combination for V2 should be codtabm(k,1) instead of
58: (codtabm(k,2), and the code should be
59: codtabm(k,TnsdVar[Tvaraff[j]]. Many many changes have been
60: made. All of these should be simplified once a day like we did in
61: hpxij() for example by using precov[nres] which is computed in
62: decoderesult for each nres of each resultline. Loop should be done
63: on the equation model globally by distinguishing only product with
64: age (which are changing with age) and no more on type of
65: covariates, single dummies, single covariates.
66:
67: Revision 1.331 2022/08/07 05:40:09 brouard
68: *** empty log message ***
69:
70: Revision 1.330 2022/08/06 07:18:25 brouard
71: Summary: last 0.99r31
72:
73: * imach.c (Module): Version of imach using partly decoderesult to rebuild xpxij function
74:
75: Revision 1.329 2022/08/03 17:29:54 brouard
76: * imach.c (Module): Many errors in graphs fixed with Vn*age covariates.
77:
78: Revision 1.328 2022/07/27 17:40:48 brouard
79: Summary: valgrind bug fixed by initializing to zero DummyV as well as Tage
80:
81: Revision 1.327 2022/07/27 14:47:35 brouard
82: Summary: Still a problem for one-step probabilities in case of quantitative variables
83:
84: Revision 1.326 2022/07/26 17:33:55 brouard
85: Summary: some test with nres=1
86:
87: Revision 1.325 2022/07/25 14:27:23 brouard
88: Summary: r30
89:
90: * imach.c (Module): Error cptcovn instead of nsd in bmij (was
91: coredumped, revealed by Feiuno, thank you.
92:
93: Revision 1.324 2022/07/23 17:44:26 brouard
94: *** empty log message ***
95:
96: Revision 1.323 2022/07/22 12:30:08 brouard
97: * imach.c (Module): Output of Wald test in the htm file and not only in the log.
98:
99: Revision 1.322 2022/07/22 12:27:48 brouard
100: * imach.c (Module): Output of Wald test in the htm file and not only in the log.
101:
102: Revision 1.321 2022/07/22 12:04:24 brouard
103: Summary: r28
104:
105: * imach.c (Module): Output of Wald test in the htm file and not only in the log.
106:
107: Revision 1.320 2022/06/02 05:10:11 brouard
108: *** empty log message ***
109:
110: Revision 1.319 2022/06/02 04:45:11 brouard
111: * imach.c (Module): Adding the Wald tests from the log to the main
112: htm for better display of the maximum likelihood estimators.
113:
114: Revision 1.318 2022/05/24 08:10:59 brouard
115: * imach.c (Module): Some attempts to find a bug of wrong estimates
116: of confidencce intervals with product in the equation modelC
117:
118: Revision 1.317 2022/05/15 15:06:23 brouard
119: * imach.c (Module): Some minor improvements
120:
121: Revision 1.316 2022/05/11 15:11:31 brouard
122: Summary: r27
123:
124: Revision 1.315 2022/05/11 15:06:32 brouard
125: *** empty log message ***
126:
127: Revision 1.314 2022/04/13 17:43:09 brouard
128: * imach.c (Module): Adding link to text data files
129:
130: Revision 1.313 2022/04/11 15:57:42 brouard
131: * imach.c (Module): Error in rewriting the 'r' file with yearsfproj or yearsbproj fixed
132:
133: Revision 1.312 2022/04/05 21:24:39 brouard
134: *** empty log message ***
135:
136: Revision 1.311 2022/04/05 21:03:51 brouard
137: Summary: Fixed quantitative covariates
138:
139: Fixed covariates (dummy or quantitative)
140: with missing values have never been allowed but are ERRORS and
141: program quits. Standard deviations of fixed covariates were
142: wrongly computed. Mean and standard deviations of time varying
143: covariates are still not computed.
144:
145: Revision 1.310 2022/03/17 08:45:53 brouard
146: Summary: 99r25
147:
148: Improving detection of errors: result lines should be compatible with
149: the model.
150:
151: Revision 1.309 2021/05/20 12:39:14 brouard
152: Summary: Version 0.99r24
153:
154: Revision 1.308 2021/03/31 13:11:57 brouard
155: Summary: Version 0.99r23
156:
157:
158: * imach.c (Module): Still bugs in the result loop. Thank to Holly Benett
159:
160: Revision 1.307 2021/03/08 18:11:32 brouard
161: Summary: 0.99r22 fixed bug on result:
162:
163: Revision 1.306 2021/02/20 15:44:02 brouard
164: Summary: Version 0.99r21
165:
166: * imach.c (Module): Fix bug on quitting after result lines!
167: (Module): Version 0.99r21
168:
169: Revision 1.305 2021/02/20 15:28:30 brouard
170: * imach.c (Module): Fix bug on quitting after result lines!
171:
172: Revision 1.304 2021/02/12 11:34:20 brouard
173: * imach.c (Module): The use of a Windows BOM (huge) file is now an error
174:
175: Revision 1.303 2021/02/11 19:50:15 brouard
176: * (Module): imach.c Someone entered 'results:' instead of 'result:'. Now it is an error which is printed.
177:
178: Revision 1.302 2020/02/22 21:00:05 brouard
179: * (Module): imach.c Update mle=-3 (for computing Life expectancy
180: and life table from the data without any state)
181:
182: Revision 1.301 2019/06/04 13:51:20 brouard
183: Summary: Error in 'r'parameter file backcast yearsbproj instead of yearsfproj
184:
185: Revision 1.300 2019/05/22 19:09:45 brouard
186: Summary: version 0.99r19 of May 2019
187:
188: Revision 1.299 2019/05/22 18:37:08 brouard
189: Summary: Cleaned 0.99r19
190:
191: Revision 1.298 2019/05/22 18:19:56 brouard
192: *** empty log message ***
193:
194: Revision 1.297 2019/05/22 17:56:10 brouard
195: Summary: Fix bug by moving date2dmy and nhstepm which gaefin=-1
196:
197: Revision 1.296 2019/05/20 13:03:18 brouard
198: Summary: Projection syntax simplified
199:
200:
201: We can now start projections, forward or backward, from the mean date
202: of inteviews up to or down to a number of years of projection:
203: prevforecast=1 yearsfproj=15.3 mobil_average=0
204: or
205: prevforecast=1 starting-proj-date=1/1/2007 final-proj-date=12/31/2017 mobil_average=0
206: or
207: prevbackcast=1 yearsbproj=12.3 mobil_average=1
208: or
209: prevbackcast=1 starting-back-date=1/10/1999 final-back-date=1/1/1985 mobil_average=1
210:
211: Revision 1.295 2019/05/18 09:52:50 brouard
212: Summary: doxygen tex bug
213:
214: Revision 1.294 2019/05/16 14:54:33 brouard
215: Summary: There was some wrong lines added
216:
217: Revision 1.293 2019/05/09 15:17:34 brouard
218: *** empty log message ***
219:
220: Revision 1.292 2019/05/09 14:17:20 brouard
221: Summary: Some updates
222:
223: Revision 1.291 2019/05/09 13:44:18 brouard
224: Summary: Before ncovmax
225:
226: Revision 1.290 2019/05/09 13:39:37 brouard
227: Summary: 0.99r18 unlimited number of individuals
228:
229: The number n which was limited to 20,000 cases is now unlimited, from firstobs to lastobs. If the number is too for the virtual memory, probably an error will occur.
230:
231: Revision 1.289 2018/12/13 09:16:26 brouard
232: Summary: Bug for young ages (<-30) will be in r17
233:
234: Revision 1.288 2018/05/02 20:58:27 brouard
235: Summary: Some bugs fixed
236:
237: Revision 1.287 2018/05/01 17:57:25 brouard
238: Summary: Bug fixed by providing frequencies only for non missing covariates
239:
240: Revision 1.286 2018/04/27 14:27:04 brouard
241: Summary: some minor bugs
242:
243: Revision 1.285 2018/04/21 21:02:16 brouard
244: Summary: Some bugs fixed, valgrind tested
245:
246: Revision 1.284 2018/04/20 05:22:13 brouard
247: Summary: Computing mean and stdeviation of fixed quantitative variables
248:
249: Revision 1.283 2018/04/19 14:49:16 brouard
250: Summary: Some minor bugs fixed
251:
252: Revision 1.282 2018/02/27 22:50:02 brouard
253: *** empty log message ***
254:
255: Revision 1.281 2018/02/27 19:25:23 brouard
256: Summary: Adding second argument for quitting
257:
258: Revision 1.280 2018/02/21 07:58:13 brouard
259: Summary: 0.99r15
260:
261: New Makefile with recent VirtualBox 5.26. Bug in sqrt negatve in imach.c
262:
263: Revision 1.279 2017/07/20 13:35:01 brouard
264: Summary: temporary working
265:
266: Revision 1.278 2017/07/19 14:09:02 brouard
267: Summary: Bug for mobil_average=0 and prevforecast fixed(?)
268:
269: Revision 1.277 2017/07/17 08:53:49 brouard
270: Summary: BOM files can be read now
271:
272: Revision 1.276 2017/06/30 15:48:31 brouard
273: Summary: Graphs improvements
274:
275: Revision 1.275 2017/06/30 13:39:33 brouard
276: Summary: Saito's color
277:
278: Revision 1.274 2017/06/29 09:47:08 brouard
279: Summary: Version 0.99r14
280:
281: Revision 1.273 2017/06/27 11:06:02 brouard
282: Summary: More documentation on projections
283:
284: Revision 1.272 2017/06/27 10:22:40 brouard
285: Summary: Color of backprojection changed from 6 to 5(yellow)
286:
287: Revision 1.271 2017/06/27 10:17:50 brouard
288: Summary: Some bug with rint
289:
290: Revision 1.270 2017/05/24 05:45:29 brouard
291: *** empty log message ***
292:
293: Revision 1.269 2017/05/23 08:39:25 brouard
294: Summary: Code into subroutine, cleanings
295:
296: Revision 1.268 2017/05/18 20:09:32 brouard
297: Summary: backprojection and confidence intervals of backprevalence
298:
299: Revision 1.267 2017/05/13 10:25:05 brouard
300: Summary: temporary save for backprojection
301:
302: Revision 1.266 2017/05/13 07:26:12 brouard
303: Summary: Version 0.99r13 (improvements and bugs fixed)
304:
305: Revision 1.265 2017/04/26 16:22:11 brouard
306: Summary: imach 0.99r13 Some bugs fixed
307:
308: Revision 1.264 2017/04/26 06:01:29 brouard
309: Summary: Labels in graphs
310:
311: Revision 1.263 2017/04/24 15:23:15 brouard
312: Summary: to save
313:
314: Revision 1.262 2017/04/18 16:48:12 brouard
315: *** empty log message ***
316:
317: Revision 1.261 2017/04/05 10:14:09 brouard
318: Summary: Bug in E_ as well as in T_ fixed nres-1 vs k1-1
319:
320: Revision 1.260 2017/04/04 17:46:59 brouard
321: Summary: Gnuplot indexations fixed (humm)
322:
323: Revision 1.259 2017/04/04 13:01:16 brouard
324: Summary: Some errors to warnings only if date of death is unknown but status is death we could set to pi3
325:
326: Revision 1.258 2017/04/03 10:17:47 brouard
327: Summary: Version 0.99r12
328:
329: Some cleanings, conformed with updated documentation.
330:
331: Revision 1.257 2017/03/29 16:53:30 brouard
332: Summary: Temp
333:
334: Revision 1.256 2017/03/27 05:50:23 brouard
335: Summary: Temporary
336:
337: Revision 1.255 2017/03/08 16:02:28 brouard
338: Summary: IMaCh version 0.99r10 bugs in gnuplot fixed
339:
340: Revision 1.254 2017/03/08 07:13:00 brouard
341: Summary: Fixing data parameter line
342:
343: Revision 1.253 2016/12/15 11:59:41 brouard
344: Summary: 0.99 in progress
345:
346: Revision 1.252 2016/09/15 21:15:37 brouard
347: *** empty log message ***
348:
349: Revision 1.251 2016/09/15 15:01:13 brouard
350: Summary: not working
351:
352: Revision 1.250 2016/09/08 16:07:27 brouard
353: Summary: continue
354:
355: Revision 1.249 2016/09/07 17:14:18 brouard
356: Summary: Starting values from frequencies
357:
358: Revision 1.248 2016/09/07 14:10:18 brouard
359: *** empty log message ***
360:
361: Revision 1.247 2016/09/02 11:11:21 brouard
362: *** empty log message ***
363:
364: Revision 1.246 2016/09/02 08:49:22 brouard
365: *** empty log message ***
366:
367: Revision 1.245 2016/09/02 07:25:01 brouard
368: *** empty log message ***
369:
370: Revision 1.244 2016/09/02 07:17:34 brouard
371: *** empty log message ***
372:
373: Revision 1.243 2016/09/02 06:45:35 brouard
374: *** empty log message ***
375:
376: Revision 1.242 2016/08/30 15:01:20 brouard
377: Summary: Fixing a lots
378:
379: Revision 1.241 2016/08/29 17:17:25 brouard
380: Summary: gnuplot problem in Back projection to fix
381:
382: Revision 1.240 2016/08/29 07:53:18 brouard
383: Summary: Better
384:
385: Revision 1.239 2016/08/26 15:51:03 brouard
386: Summary: Improvement in Powell output in order to copy and paste
387:
388: Author:
389:
390: Revision 1.238 2016/08/26 14:23:35 brouard
391: Summary: Starting tests of 0.99
392:
393: Revision 1.237 2016/08/26 09:20:19 brouard
394: Summary: to valgrind
395:
396: Revision 1.236 2016/08/25 10:50:18 brouard
397: *** empty log message ***
398:
399: Revision 1.235 2016/08/25 06:59:23 brouard
400: *** empty log message ***
401:
402: Revision 1.234 2016/08/23 16:51:20 brouard
403: *** empty log message ***
404:
405: Revision 1.233 2016/08/23 07:40:50 brouard
406: Summary: not working
407:
408: Revision 1.232 2016/08/22 14:20:21 brouard
409: Summary: not working
410:
411: Revision 1.231 2016/08/22 07:17:15 brouard
412: Summary: not working
413:
414: Revision 1.230 2016/08/22 06:55:53 brouard
415: Summary: Not working
416:
417: Revision 1.229 2016/07/23 09:45:53 brouard
418: Summary: Completing for func too
419:
420: Revision 1.228 2016/07/22 17:45:30 brouard
421: Summary: Fixing some arrays, still debugging
422:
423: Revision 1.226 2016/07/12 18:42:34 brouard
424: Summary: temp
425:
426: Revision 1.225 2016/07/12 08:40:03 brouard
427: Summary: saving but not running
428:
429: Revision 1.224 2016/07/01 13:16:01 brouard
430: Summary: Fixes
431:
432: Revision 1.223 2016/02/19 09:23:35 brouard
433: Summary: temporary
434:
435: Revision 1.222 2016/02/17 08:14:50 brouard
436: Summary: Probably last 0.98 stable version 0.98r6
437:
438: Revision 1.221 2016/02/15 23:35:36 brouard
439: Summary: minor bug
440:
441: Revision 1.219 2016/02/15 00:48:12 brouard
442: *** empty log message ***
443:
444: Revision 1.218 2016/02/12 11:29:23 brouard
445: Summary: 0.99 Back projections
446:
447: Revision 1.217 2015/12/23 17:18:31 brouard
448: Summary: Experimental backcast
449:
450: Revision 1.216 2015/12/18 17:32:11 brouard
451: Summary: 0.98r4 Warning and status=-2
452:
453: Version 0.98r4 is now:
454: - displaying an error when status is -1, date of interview unknown and date of death known;
455: - permitting a status -2 when the vital status is unknown at a known date of right truncation.
456: Older changes concerning s=-2, dating from 2005 have been supersed.
457:
458: Revision 1.215 2015/12/16 08:52:24 brouard
459: Summary: 0.98r4 working
460:
461: Revision 1.214 2015/12/16 06:57:54 brouard
462: Summary: temporary not working
463:
464: Revision 1.213 2015/12/11 18:22:17 brouard
465: Summary: 0.98r4
466:
467: Revision 1.212 2015/11/21 12:47:24 brouard
468: Summary: minor typo
469:
470: Revision 1.211 2015/11/21 12:41:11 brouard
471: Summary: 0.98r3 with some graph of projected cross-sectional
472:
473: Author: Nicolas Brouard
474:
475: Revision 1.210 2015/11/18 17:41:20 brouard
476: Summary: Start working on projected prevalences Revision 1.209 2015/11/17 22:12:03 brouard
477: Summary: Adding ftolpl parameter
478: Author: N Brouard
479:
480: We had difficulties to get smoothed confidence intervals. It was due
481: to the period prevalence which wasn't computed accurately. The inner
482: parameter ftolpl is now an outer parameter of the .imach parameter
483: file after estepm. If ftolpl is small 1.e-4 and estepm too,
484: computation are long.
485:
486: Revision 1.208 2015/11/17 14:31:57 brouard
487: Summary: temporary
488:
489: Revision 1.207 2015/10/27 17:36:57 brouard
490: *** empty log message ***
491:
492: Revision 1.206 2015/10/24 07:14:11 brouard
493: *** empty log message ***
494:
495: Revision 1.205 2015/10/23 15:50:53 brouard
496: Summary: 0.98r3 some clarification for graphs on likelihood contributions
497:
498: Revision 1.204 2015/10/01 16:20:26 brouard
499: Summary: Some new graphs of contribution to likelihood
500:
501: Revision 1.203 2015/09/30 17:45:14 brouard
502: Summary: looking at better estimation of the hessian
503:
504: Also a better criteria for convergence to the period prevalence And
505: therefore adding the number of years needed to converge. (The
506: prevalence in any alive state shold sum to one
507:
508: Revision 1.202 2015/09/22 19:45:16 brouard
509: Summary: Adding some overall graph on contribution to likelihood. Might change
510:
511: Revision 1.201 2015/09/15 17:34:58 brouard
512: Summary: 0.98r0
513:
514: - Some new graphs like suvival functions
515: - Some bugs fixed like model=1+age+V2.
516:
517: Revision 1.200 2015/09/09 16:53:55 brouard
518: Summary: Big bug thanks to Flavia
519:
520: Even model=1+age+V2. did not work anymore
521:
522: Revision 1.199 2015/09/07 14:09:23 brouard
523: Summary: 0.98q6 changing default small png format for graph to vectorized svg.
524:
525: Revision 1.198 2015/09/03 07:14:39 brouard
526: Summary: 0.98q5 Flavia
527:
528: Revision 1.197 2015/09/01 18:24:39 brouard
529: *** empty log message ***
530:
531: Revision 1.196 2015/08/18 23:17:52 brouard
532: Summary: 0.98q5
533:
534: Revision 1.195 2015/08/18 16:28:39 brouard
535: Summary: Adding a hack for testing purpose
536:
537: After reading the title, ftol and model lines, if the comment line has
538: a q, starting with #q, the answer at the end of the run is quit. It
539: permits to run test files in batch with ctest. The former workaround was
540: $ echo q | imach foo.imach
541:
542: Revision 1.194 2015/08/18 13:32:00 brouard
543: Summary: Adding error when the covariance matrix doesn't contain the exact number of lines required by the model line.
544:
545: Revision 1.193 2015/08/04 07:17:42 brouard
546: Summary: 0.98q4
547:
548: Revision 1.192 2015/07/16 16:49:02 brouard
549: Summary: Fixing some outputs
550:
551: Revision 1.191 2015/07/14 10:00:33 brouard
552: Summary: Some fixes
553:
554: Revision 1.190 2015/05/05 08:51:13 brouard
555: Summary: Adding digits in output parameters (7 digits instead of 6)
556:
557: Fix 1+age+.
558:
559: Revision 1.189 2015/04/30 14:45:16 brouard
560: Summary: 0.98q2
561:
562: Revision 1.188 2015/04/30 08:27:53 brouard
563: *** empty log message ***
564:
565: Revision 1.187 2015/04/29 09:11:15 brouard
566: *** empty log message ***
567:
568: Revision 1.186 2015/04/23 12:01:52 brouard
569: Summary: V1*age is working now, version 0.98q1
570:
571: Some codes had been disabled in order to simplify and Vn*age was
572: working in the optimization phase, ie, giving correct MLE parameters,
573: but, as usual, outputs were not correct and program core dumped.
574:
575: Revision 1.185 2015/03/11 13:26:42 brouard
576: Summary: Inclusion of compile and links command line for Intel Compiler
577:
578: Revision 1.184 2015/03/11 11:52:39 brouard
579: Summary: Back from Windows 8. Intel Compiler
580:
581: Revision 1.183 2015/03/10 20:34:32 brouard
582: Summary: 0.98q0, trying with directest, mnbrak fixed
583:
584: We use directest instead of original Powell test; probably no
585: incidence on the results, but better justifications;
586: We fixed Numerical Recipes mnbrak routine which was wrong and gave
587: wrong results.
588:
589: Revision 1.182 2015/02/12 08:19:57 brouard
590: Summary: Trying to keep directest which seems simpler and more general
591: Author: Nicolas Brouard
592:
593: Revision 1.181 2015/02/11 23:22:24 brouard
594: Summary: Comments on Powell added
595:
596: Author:
597:
598: Revision 1.180 2015/02/11 17:33:45 brouard
599: Summary: Finishing move from main to function (hpijx and prevalence_limit)
600:
601: Revision 1.179 2015/01/04 09:57:06 brouard
602: Summary: back to OS/X
603:
604: Revision 1.178 2015/01/04 09:35:48 brouard
605: *** empty log message ***
606:
607: Revision 1.177 2015/01/03 18:40:56 brouard
608: Summary: Still testing ilc32 on OSX
609:
610: Revision 1.176 2015/01/03 16:45:04 brouard
611: *** empty log message ***
612:
613: Revision 1.175 2015/01/03 16:33:42 brouard
614: *** empty log message ***
615:
616: Revision 1.174 2015/01/03 16:15:49 brouard
617: Summary: Still in cross-compilation
618:
619: Revision 1.173 2015/01/03 12:06:26 brouard
620: Summary: trying to detect cross-compilation
621:
622: Revision 1.172 2014/12/27 12:07:47 brouard
623: Summary: Back from Visual Studio and Intel, options for compiling for Windows XP
624:
625: Revision 1.171 2014/12/23 13:26:59 brouard
626: Summary: Back from Visual C
627:
628: Still problem with utsname.h on Windows
629:
630: Revision 1.170 2014/12/23 11:17:12 brouard
631: Summary: Cleaning some \%% back to %%
632:
633: The escape was mandatory for a specific compiler (which one?), but too many warnings.
634:
635: Revision 1.169 2014/12/22 23:08:31 brouard
636: Summary: 0.98p
637:
638: Outputs some informations on compiler used, OS etc. Testing on different platforms.
639:
640: Revision 1.168 2014/12/22 15:17:42 brouard
641: Summary: update
642:
643: Revision 1.167 2014/12/22 13:50:56 brouard
644: Summary: Testing uname and compiler version and if compiled 32 or 64
645:
646: Testing on Linux 64
647:
648: Revision 1.166 2014/12/22 11:40:47 brouard
649: *** empty log message ***
650:
651: Revision 1.165 2014/12/16 11:20:36 brouard
652: Summary: After compiling on Visual C
653:
654: * imach.c (Module): Merging 1.61 to 1.162
655:
656: Revision 1.164 2014/12/16 10:52:11 brouard
657: Summary: Merging with Visual C after suppressing some warnings for unused variables. Also fixing Saito's bug 0.98Xn
658:
659: * imach.c (Module): Merging 1.61 to 1.162
660:
661: Revision 1.163 2014/12/16 10:30:11 brouard
662: * imach.c (Module): Merging 1.61 to 1.162
663:
664: Revision 1.162 2014/09/25 11:43:39 brouard
665: Summary: temporary backup 0.99!
666:
667: Revision 1.1 2014/09/16 11:06:58 brouard
668: Summary: With some code (wrong) for nlopt
669:
670: Author:
671:
672: Revision 1.161 2014/09/15 20:41:41 brouard
673: Summary: Problem with macro SQR on Intel compiler
674:
675: Revision 1.160 2014/09/02 09:24:05 brouard
676: *** empty log message ***
677:
678: Revision 1.159 2014/09/01 10:34:10 brouard
679: Summary: WIN32
680: Author: Brouard
681:
682: Revision 1.158 2014/08/27 17:11:51 brouard
683: *** empty log message ***
684:
685: Revision 1.157 2014/08/27 16:26:55 brouard
686: Summary: Preparing windows Visual studio version
687: Author: Brouard
688:
689: In order to compile on Visual studio, time.h is now correct and time_t
690: and tm struct should be used. difftime should be used but sometimes I
691: just make the differences in raw time format (time(&now).
692: Trying to suppress #ifdef LINUX
693: Add xdg-open for __linux in order to open default browser.
694:
695: Revision 1.156 2014/08/25 20:10:10 brouard
696: *** empty log message ***
697:
698: Revision 1.155 2014/08/25 18:32:34 brouard
699: Summary: New compile, minor changes
700: Author: Brouard
701:
702: Revision 1.154 2014/06/20 17:32:08 brouard
703: Summary: Outputs now all graphs of convergence to period prevalence
704:
705: Revision 1.153 2014/06/20 16:45:46 brouard
706: Summary: If 3 live state, convergence to period prevalence on same graph
707: Author: Brouard
708:
709: Revision 1.152 2014/06/18 17:54:09 brouard
710: Summary: open browser, use gnuplot on same dir than imach if not found in the path
711:
712: Revision 1.151 2014/06/18 16:43:30 brouard
713: *** empty log message ***
714:
715: Revision 1.150 2014/06/18 16:42:35 brouard
716: Summary: If gnuplot is not in the path try on same directory than imach binary (OSX)
717: Author: brouard
718:
719: Revision 1.149 2014/06/18 15:51:14 brouard
720: Summary: Some fixes in parameter files errors
721: Author: Nicolas Brouard
722:
723: Revision 1.148 2014/06/17 17:38:48 brouard
724: Summary: Nothing new
725: Author: Brouard
726:
727: Just a new packaging for OS/X version 0.98nS
728:
729: Revision 1.147 2014/06/16 10:33:11 brouard
730: *** empty log message ***
731:
732: Revision 1.146 2014/06/16 10:20:28 brouard
733: Summary: Merge
734: Author: Brouard
735:
736: Merge, before building revised version.
737:
738: Revision 1.145 2014/06/10 21:23:15 brouard
739: Summary: Debugging with valgrind
740: Author: Nicolas Brouard
741:
742: Lot of changes in order to output the results with some covariates
743: After the Edimburgh REVES conference 2014, it seems mandatory to
744: improve the code.
745: No more memory valgrind error but a lot has to be done in order to
746: continue the work of splitting the code into subroutines.
747: Also, decodemodel has been improved. Tricode is still not
748: optimal. nbcode should be improved. Documentation has been added in
749: the source code.
750:
751: Revision 1.143 2014/01/26 09:45:38 brouard
752: Summary: Version 0.98nR (to be improved, but gives same optimization results as 0.98k. Nice, promising
753:
754: * imach.c (Module): Trying to merge old staffs together while being at Tokyo. Not tested...
755: (Module): Version 0.98nR Running ok, but output format still only works for three covariates.
756:
757: Revision 1.142 2014/01/26 03:57:36 brouard
758: Summary: gnuplot changed plot w l 1 has to be changed to plot w l lt 2
759:
760: * imach.c (Module): Trying to merge old staffs together while being at Tokyo. Not tested...
761:
762: Revision 1.141 2014/01/26 02:42:01 brouard
763: * imach.c (Module): Trying to merge old staffs together while being at Tokyo. Not tested...
764:
765: Revision 1.140 2011/09/02 10:37:54 brouard
766: Summary: times.h is ok with mingw32 now.
767:
768: Revision 1.139 2010/06/14 07:50:17 brouard
769: After the theft of my laptop, I probably lost some lines of codes which were not uploaded to the CVS tree.
770: I remember having already fixed agemin agemax which are pointers now but not cvs saved.
771:
772: Revision 1.138 2010/04/30 18:19:40 brouard
773: *** empty log message ***
774:
775: Revision 1.137 2010/04/29 18:11:38 brouard
776: (Module): Checking covariates for more complex models
777: than V1+V2. A lot of change to be done. Unstable.
778:
779: Revision 1.136 2010/04/26 20:30:53 brouard
780: (Module): merging some libgsl code. Fixing computation
781: of likelione (using inter/intrapolation if mle = 0) in order to
782: get same likelihood as if mle=1.
783: Some cleaning of code and comments added.
784:
785: Revision 1.135 2009/10/29 15:33:14 brouard
786: (Module): Now imach stops if date of birth, at least year of birth, is not given. Some cleaning of the code.
787:
788: Revision 1.134 2009/10/29 13:18:53 brouard
789: (Module): Now imach stops if date of birth, at least year of birth, is not given. Some cleaning of the code.
790:
791: Revision 1.133 2009/07/06 10:21:25 brouard
792: just nforces
793:
794: Revision 1.132 2009/07/06 08:22:05 brouard
795: Many tings
796:
797: Revision 1.131 2009/06/20 16:22:47 brouard
798: Some dimensions resccaled
799:
800: Revision 1.130 2009/05/26 06:44:34 brouard
801: (Module): Max Covariate is now set to 20 instead of 8. A
802: lot of cleaning with variables initialized to 0. Trying to make
803: V2+V3*age+V1+V4 strb=V3*age+V1+V4 working better.
804:
805: Revision 1.129 2007/08/31 13:49:27 lievre
806: Modification of the way of exiting when the covariate is not binary in order to see on the window the error message before exiting
807:
808: Revision 1.128 2006/06/30 13:02:05 brouard
809: (Module): Clarifications on computing e.j
810:
811: Revision 1.127 2006/04/28 18:11:50 brouard
812: (Module): Yes the sum of survivors was wrong since
813: imach-114 because nhstepm was no more computed in the age
814: loop. Now we define nhstepma in the age loop.
815: (Module): In order to speed up (in case of numerous covariates) we
816: compute health expectancies (without variances) in a first step
817: and then all the health expectancies with variances or standard
818: deviation (needs data from the Hessian matrices) which slows the
819: computation.
820: In the future we should be able to stop the program is only health
821: expectancies and graph are needed without standard deviations.
822:
823: Revision 1.126 2006/04/28 17:23:28 brouard
824: (Module): Yes the sum of survivors was wrong since
825: imach-114 because nhstepm was no more computed in the age
826: loop. Now we define nhstepma in the age loop.
827: Version 0.98h
828:
829: Revision 1.125 2006/04/04 15:20:31 lievre
830: Errors in calculation of health expectancies. Age was not initialized.
831: Forecasting file added.
832:
833: Revision 1.124 2006/03/22 17:13:53 lievre
834: Parameters are printed with %lf instead of %f (more numbers after the comma).
835: The log-likelihood is printed in the log file
836:
837: Revision 1.123 2006/03/20 10:52:43 brouard
838: * imach.c (Module): <title> changed, corresponds to .htm file
839: name. <head> headers where missing.
840:
841: * imach.c (Module): Weights can have a decimal point as for
842: English (a comma might work with a correct LC_NUMERIC environment,
843: otherwise the weight is truncated).
844: Modification of warning when the covariates values are not 0 or
845: 1.
846: Version 0.98g
847:
848: Revision 1.122 2006/03/20 09:45:41 brouard
849: (Module): Weights can have a decimal point as for
850: English (a comma might work with a correct LC_NUMERIC environment,
851: otherwise the weight is truncated).
852: Modification of warning when the covariates values are not 0 or
853: 1.
854: Version 0.98g
855:
856: Revision 1.121 2006/03/16 17:45:01 lievre
857: * imach.c (Module): Comments concerning covariates added
858:
859: * imach.c (Module): refinements in the computation of lli if
860: status=-2 in order to have more reliable computation if stepm is
861: not 1 month. Version 0.98f
862:
863: Revision 1.120 2006/03/16 15:10:38 lievre
864: (Module): refinements in the computation of lli if
865: status=-2 in order to have more reliable computation if stepm is
866: not 1 month. Version 0.98f
867:
868: Revision 1.119 2006/03/15 17:42:26 brouard
869: (Module): Bug if status = -2, the loglikelihood was
870: computed as likelihood omitting the logarithm. Version O.98e
871:
872: Revision 1.118 2006/03/14 18:20:07 brouard
873: (Module): varevsij Comments added explaining the second
874: table of variances if popbased=1 .
875: (Module): Covariances of eij, ekl added, graphs fixed, new html link.
876: (Module): Function pstamp added
877: (Module): Version 0.98d
878:
879: Revision 1.117 2006/03/14 17:16:22 brouard
880: (Module): varevsij Comments added explaining the second
881: table of variances if popbased=1 .
882: (Module): Covariances of eij, ekl added, graphs fixed, new html link.
883: (Module): Function pstamp added
884: (Module): Version 0.98d
885:
886: Revision 1.116 2006/03/06 10:29:27 brouard
887: (Module): Variance-covariance wrong links and
888: varian-covariance of ej. is needed (Saito).
889:
890: Revision 1.115 2006/02/27 12:17:45 brouard
891: (Module): One freematrix added in mlikeli! 0.98c
892:
893: Revision 1.114 2006/02/26 12:57:58 brouard
894: (Module): Some improvements in processing parameter
895: filename with strsep.
896:
897: Revision 1.113 2006/02/24 14:20:24 brouard
898: (Module): Memory leaks checks with valgrind and:
899: datafile was not closed, some imatrix were not freed and on matrix
900: allocation too.
901:
902: Revision 1.112 2006/01/30 09:55:26 brouard
903: (Module): Back to gnuplot.exe instead of wgnuplot.exe
904:
905: Revision 1.111 2006/01/25 20:38:18 brouard
906: (Module): Lots of cleaning and bugs added (Gompertz)
907: (Module): Comments can be added in data file. Missing date values
908: can be a simple dot '.'.
909:
910: Revision 1.110 2006/01/25 00:51:50 brouard
911: (Module): Lots of cleaning and bugs added (Gompertz)
912:
913: Revision 1.109 2006/01/24 19:37:15 brouard
914: (Module): Comments (lines starting with a #) are allowed in data.
915:
916: Revision 1.108 2006/01/19 18:05:42 lievre
917: Gnuplot problem appeared...
918: To be fixed
919:
920: Revision 1.107 2006/01/19 16:20:37 brouard
921: Test existence of gnuplot in imach path
922:
923: Revision 1.106 2006/01/19 13:24:36 brouard
924: Some cleaning and links added in html output
925:
926: Revision 1.105 2006/01/05 20:23:19 lievre
927: *** empty log message ***
928:
929: Revision 1.104 2005/09/30 16:11:43 lievre
930: (Module): sump fixed, loop imx fixed, and simplifications.
931: (Module): If the status is missing at the last wave but we know
932: that the person is alive, then we can code his/her status as -2
933: (instead of missing=-1 in earlier versions) and his/her
934: contributions to the likelihood is 1 - Prob of dying from last
935: health status (= 1-p13= p11+p12 in the easiest case of somebody in
936: the healthy state at last known wave). Version is 0.98
937:
938: Revision 1.103 2005/09/30 15:54:49 lievre
939: (Module): sump fixed, loop imx fixed, and simplifications.
940:
941: Revision 1.102 2004/09/15 17:31:30 brouard
942: Add the possibility to read data file including tab characters.
943:
944: Revision 1.101 2004/09/15 10:38:38 brouard
945: Fix on curr_time
946:
947: Revision 1.100 2004/07/12 18:29:06 brouard
948: Add version for Mac OS X. Just define UNIX in Makefile
949:
950: Revision 1.99 2004/06/05 08:57:40 brouard
951: *** empty log message ***
952:
953: Revision 1.98 2004/05/16 15:05:56 brouard
954: New version 0.97 . First attempt to estimate force of mortality
955: directly from the data i.e. without the need of knowing the health
956: state at each age, but using a Gompertz model: log u =a + b*age .
957: This is the basic analysis of mortality and should be done before any
958: other analysis, in order to test if the mortality estimated from the
959: cross-longitudinal survey is different from the mortality estimated
960: from other sources like vital statistic data.
961:
962: The same imach parameter file can be used but the option for mle should be -3.
963:
964: Agnès, who wrote this part of the code, tried to keep most of the
965: former routines in order to include the new code within the former code.
966:
967: The output is very simple: only an estimate of the intercept and of
968: the slope with 95% confident intervals.
969:
970: Current limitations:
971: A) Even if you enter covariates, i.e. with the
972: model= V1+V2 equation for example, the programm does only estimate a unique global model without covariates.
973: B) There is no computation of Life Expectancy nor Life Table.
974:
975: Revision 1.97 2004/02/20 13:25:42 lievre
976: Version 0.96d. Population forecasting command line is (temporarily)
977: suppressed.
978:
979: Revision 1.96 2003/07/15 15:38:55 brouard
980: * imach.c (Repository): Errors in subdirf, 2, 3 while printing tmpout is
981: rewritten within the same printf. Workaround: many printfs.
982:
983: Revision 1.95 2003/07/08 07:54:34 brouard
984: * imach.c (Repository):
985: (Repository): Using imachwizard code to output a more meaningful covariance
986: matrix (cov(a12,c31) instead of numbers.
987:
988: Revision 1.94 2003/06/27 13:00:02 brouard
989: Just cleaning
990:
991: Revision 1.93 2003/06/25 16:33:55 brouard
992: (Module): On windows (cygwin) function asctime_r doesn't
993: exist so I changed back to asctime which exists.
994: (Module): Version 0.96b
995:
996: Revision 1.92 2003/06/25 16:30:45 brouard
997: (Module): On windows (cygwin) function asctime_r doesn't
998: exist so I changed back to asctime which exists.
999:
1000: Revision 1.91 2003/06/25 15:30:29 brouard
1001: * imach.c (Repository): Duplicated warning errors corrected.
1002: (Repository): Elapsed time after each iteration is now output. It
1003: helps to forecast when convergence will be reached. Elapsed time
1004: is stamped in powell. We created a new html file for the graphs
1005: concerning matrix of covariance. It has extension -cov.htm.
1006:
1007: Revision 1.90 2003/06/24 12:34:15 brouard
1008: (Module): Some bugs corrected for windows. Also, when
1009: mle=-1 a template is output in file "or"mypar.txt with the design
1010: of the covariance matrix to be input.
1011:
1012: Revision 1.89 2003/06/24 12:30:52 brouard
1013: (Module): Some bugs corrected for windows. Also, when
1014: mle=-1 a template is output in file "or"mypar.txt with the design
1015: of the covariance matrix to be input.
1016:
1017: Revision 1.88 2003/06/23 17:54:56 brouard
1018: * 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.
1019:
1020: Revision 1.87 2003/06/18 12:26:01 brouard
1021: Version 0.96
1022:
1023: Revision 1.86 2003/06/17 20:04:08 brouard
1024: (Module): Change position of html and gnuplot routines and added
1025: routine fileappend.
1026:
1027: Revision 1.85 2003/06/17 13:12:43 brouard
1028: * imach.c (Repository): Check when date of death was earlier that
1029: current date of interview. It may happen when the death was just
1030: prior to the death. In this case, dh was negative and likelihood
1031: was wrong (infinity). We still send an "Error" but patch by
1032: assuming that the date of death was just one stepm after the
1033: interview.
1034: (Repository): Because some people have very long ID (first column)
1035: we changed int to long in num[] and we added a new lvector for
1036: memory allocation. But we also truncated to 8 characters (left
1037: truncation)
1038: (Repository): No more line truncation errors.
1039:
1040: Revision 1.84 2003/06/13 21:44:43 brouard
1041: * imach.c (Repository): Replace "freqsummary" at a correct
1042: place. It differs from routine "prevalence" which may be called
1043: many times. Probs is memory consuming and must be used with
1044: parcimony.
1045: Version 0.95a3 (should output exactly the same maximization than 0.8a2)
1046:
1047: Revision 1.83 2003/06/10 13:39:11 lievre
1048: *** empty log message ***
1049:
1050: Revision 1.82 2003/06/05 15:57:20 brouard
1051: Add log in imach.c and fullversion number is now printed.
1052:
1053: */
1054: /*
1055: Interpolated Markov Chain
1056:
1057: Short summary of the programme:
1058:
1059: This program computes Healthy Life Expectancies or State-specific
1060: (if states aren't health statuses) Expectancies from
1061: cross-longitudinal data. Cross-longitudinal data consist in:
1062:
1063: -1- a first survey ("cross") where individuals from different ages
1064: are interviewed on their health status or degree of disability (in
1065: the case of a health survey which is our main interest)
1066:
1067: -2- at least a second wave of interviews ("longitudinal") which
1068: measure each change (if any) in individual health status. Health
1069: expectancies are computed from the time spent in each health state
1070: according to a model. More health states you consider, more time is
1071: necessary to reach the Maximum Likelihood of the parameters involved
1072: in the model. The simplest model is the multinomial logistic model
1073: where pij is the probability to be observed in state j at the second
1074: wave conditional to be observed in state i at the first
1075: wave. Therefore the model is: log(pij/pii)= aij + bij*age+ cij*sex +
1076: etc , where 'age' is age and 'sex' is a covariate. If you want to
1077: have a more complex model than "constant and age", you should modify
1078: the program where the markup *Covariates have to be included here
1079: again* invites you to do it. More covariates you add, slower the
1080: convergence.
1081:
1082: The advantage of this computer programme, compared to a simple
1083: multinomial logistic model, is clear when the delay between waves is not
1084: identical for each individual. Also, if a individual missed an
1085: intermediate interview, the information is lost, but taken into
1086: account using an interpolation or extrapolation.
1087:
1088: hPijx is the probability to be observed in state i at age x+h
1089: conditional to the observed state i at age x. The delay 'h' can be
1090: split into an exact number (nh*stepm) of unobserved intermediate
1091: states. This elementary transition (by month, quarter,
1092: semester or year) is modelled as a multinomial logistic. The hPx
1093: matrix is simply the matrix product of nh*stepm elementary matrices
1094: and the contribution of each individual to the likelihood is simply
1095: hPijx.
1096:
1097: Also this programme outputs the covariance matrix of the parameters but also
1098: of the life expectancies. It also computes the period (stable) prevalence.
1099:
1100: Back prevalence and projections:
1101:
1102: - back_prevalence_limit(double *p, double **bprlim, double ageminpar,
1103: double agemaxpar, double ftolpl, int *ncvyearp, double
1104: dateprev1,double dateprev2, int firstpass, int lastpass, int
1105: mobilavproj)
1106:
1107: Computes the back prevalence limit for any combination of
1108: covariate values k at any age between ageminpar and agemaxpar and
1109: returns it in **bprlim. In the loops,
1110:
1111: - **bprevalim(**bprlim, ***mobaverage, nlstate, *p, age, **oldm,
1112: **savm, **dnewm, **doldm, **dsavm, ftolpl, ncvyearp, k);
1113:
1114: - hBijx Back Probability to be in state i at age x-h being in j at x
1115: Computes for any combination of covariates k and any age between bage and fage
1116: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
1117: oldm=oldms;savm=savms;
1118:
1119: - hbxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k, nres);
1120: Computes the transition matrix starting at age 'age' over
1121: 'nhstepm*hstepm*stepm' months (i.e. until
1122: age (in years) age+nhstepm*hstepm*stepm/12) by multiplying
1123: nhstepm*hstepm matrices.
1124:
1125: Returns p3mat[i][j][h] after calling
1126: p3mat[i][j][h]=matprod2(newm,
1127: bmij(pmmij,cov,ncovmodel,x,nlstate,prevacurrent, dnewm, doldm,
1128: dsavm,ij),\ 1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath,
1129: oldm);
1130:
1131: Important routines
1132:
1133: - func (or funcone), computes logit (pij) distinguishing
1134: o fixed variables (single or product dummies or quantitative);
1135: o varying variables by:
1136: (1) wave (single, product dummies, quantitative),
1137: (2) by age (can be month) age (done), age*age (done), age*Vn where Vn can be:
1138: % fixed dummy (treated) or quantitative (not done because time-consuming);
1139: % varying dummy (not done) or quantitative (not done);
1140: - Tricode which tests the modality of dummy variables (in order to warn with wrong or empty modalities)
1141: and returns the number of efficient covariates cptcoveff and modalities nbcode[Tvar[k]][1]= 0 and nbcode[Tvar[k]][2]= 1 usually.
1142: - printinghtml which outputs results like life expectancy in and from a state for a combination of modalities of dummy variables
1143: o There are 2**cptcoveff combinations of (0,1) for cptcoveff variables. Outputting only combinations with people, éliminating 1 1 if
1144: race White (0 0), Black vs White (1 0), Hispanic (0 1) and 1 1 being meaningless.
1145:
1146:
1147:
1148: Authors: Nicolas Brouard (brouard@ined.fr) and Agnès Lièvre (lievre@ined.fr).
1149: Institut national d'études démographiques, Paris.
1150: This software have been partly granted by Euro-REVES, a concerted action
1151: from the European Union.
1152: It is copyrighted identically to a GNU software product, ie programme and
1153: software can be distributed freely for non commercial use. Latest version
1154: can be accessed at http://euroreves.ined.fr/imach .
1155:
1156: Help to debug: LD_PRELOAD=/usr/local/lib/libnjamd.so ./imach foo.imach
1157: or better on gdb : set env LD_PRELOAD=/usr/local/lib/libnjamd.so
1158:
1159: **********************************************************************/
1160: /*
1161: main
1162: read parameterfile
1163: read datafile
1164: concatwav
1165: freqsummary
1166: if (mle >= 1)
1167: mlikeli
1168: print results files
1169: if mle==1
1170: computes hessian
1171: read end of parameter file: agemin, agemax, bage, fage, estepm
1172: begin-prev-date,...
1173: open gnuplot file
1174: open html file
1175: period (stable) prevalence | pl_nom 1-1 2-2 etc by covariate
1176: for age prevalim() | #****** V1=0 V2=1 V3=1 V4=0 ******
1177: | 65 1 0 2 1 3 1 4 0 0.96326 0.03674
1178: freexexit2 possible for memory heap.
1179:
1180: h Pij x | pij_nom ficrestpij
1181: # Cov Agex agex+h hpijx with i,j= 1-1 1-2 1-3 2-1 2-2 2-3
1182: 1 85 85 1.00000 0.00000 0.00000 0.00000 1.00000 0.00000
1183: 1 85 86 0.68299 0.22291 0.09410 0.71093 0.00000 0.28907
1184:
1185: 1 65 99 0.00364 0.00322 0.99314 0.00350 0.00310 0.99340
1186: 1 65 100 0.00214 0.00204 0.99581 0.00206 0.00196 0.99597
1187: variance of p one-step probabilities varprob | prob_nom ficresprob #One-step probabilities and stand. devi in ()
1188: Standard deviation of one-step probabilities | probcor_nom ficresprobcor #One-step probabilities and correlation matrix
1189: Matrix of variance covariance of one-step probabilities | probcov_nom ficresprobcov #One-step probabilities and covariance matrix
1190:
1191: forecasting if prevfcast==1 prevforecast call prevalence()
1192: health expectancies
1193: Variance-covariance of DFLE
1194: prevalence()
1195: movingaverage()
1196: varevsij()
1197: if popbased==1 varevsij(,popbased)
1198: total life expectancies
1199: Variance of period (stable) prevalence
1200: end
1201: */
1202:
1203: /* #define DEBUG */
1204: /* #define DEBUGBRENT */
1205: /* #define DEBUGLINMIN */
1206: /* #define DEBUGHESS */
1207: #define DEBUGHESSIJ
1208: /* #define LINMINORIGINAL /\* Don't use loop on scale in linmin (accepting nan) *\/ */
1209: #define POWELL /* Instead of NLOPT */
1210: #define POWELLNOF3INFF1TEST /* Skip test */
1211: /* #define POWELLORIGINAL /\* Don't use Directest to decide new direction but original Powell test *\/ */
1212: /* #define MNBRAKORIGINAL /\* Don't use mnbrak fix *\/ */
1213: /* #define FLATSUP *//* Suppresses directions where likelihood is flat */
1214:
1215: #include <math.h>
1216: #include <stdio.h>
1217: #include <stdlib.h>
1218: #include <string.h>
1219: #include <ctype.h>
1220:
1221: #ifdef _WIN32
1222: #include <io.h>
1223: #include <windows.h>
1224: #include <tchar.h>
1225: #else
1226: #include <unistd.h>
1227: #endif
1228:
1229: #include <limits.h>
1230: #include <sys/types.h>
1231:
1232: #if defined(__GNUC__)
1233: #include <sys/utsname.h> /* Doesn't work on Windows */
1234: #endif
1235:
1236: #include <sys/stat.h>
1237: #include <errno.h>
1238: /* extern int errno; */
1239:
1240: /* #ifdef LINUX */
1241: /* #include <time.h> */
1242: /* #include "timeval.h" */
1243: /* #else */
1244: /* #include <sys/time.h> */
1245: /* #endif */
1246:
1247: #include <time.h>
1248:
1249: #ifdef GSL
1250: #include <gsl/gsl_errno.h>
1251: #include <gsl/gsl_multimin.h>
1252: #endif
1253:
1254:
1255: #ifdef NLOPT
1256: #include <nlopt.h>
1257: typedef struct {
1258: double (* function)(double [] );
1259: } myfunc_data ;
1260: #endif
1261:
1262: /* #include <libintl.h> */
1263: /* #define _(String) gettext (String) */
1264:
1265: #define MAXLINE 2048 /* Was 256 and 1024. Overflow with 312 with 2 states and 4 covariates. Should be ok */
1266:
1267: #define GNUPLOTPROGRAM "gnuplot"
1268: /*#define GNUPLOTPROGRAM "..\\gp37mgw\\wgnuplot"*/
1269: #define FILENAMELENGTH 256
1270:
1271: #define GLOCK_ERROR_NOPATH -1 /* empty path */
1272: #define GLOCK_ERROR_GETCWD -2 /* cannot get cwd */
1273:
1274: #define MAXPARM 128 /**< Maximum number of parameters for the optimization */
1275: #define NPARMAX 64 /**< (nlstate+ndeath-1)*nlstate*ncovmodel */
1276:
1277: #define NINTERVMAX 8
1278: #define NLSTATEMAX 8 /**< Maximum number of live states (for func) */
1279: #define NDEATHMAX 8 /**< Maximum number of dead states (for func) */
1280: #define NCOVMAX 30 /**< Maximum number of covariates used in the model, including generated covariates V1*V2 or V1*age */
1281: #define codtabm(h,k) (1 & (h-1) >> (k-1))+1
1282: /*#define decodtabm(h,k,cptcoveff)= (h <= (1<<cptcoveff)?(((h-1) >> (k-1)) & 1) +1 : -1)*/
1283: #define decodtabm(h,k,cptcoveff) (((h-1) >> (k-1)) & 1) +1
1284: /*#define MAXN 20000 */ /* Should by replaced by nobs, real number of observations and unlimited */
1285: #define YEARM 12. /**< Number of months per year */
1286: /* #define AGESUP 130 */
1287: /* #define AGESUP 150 */
1288: #define AGESUP 200
1289: #define AGEINF 0
1290: #define AGEMARGE 25 /* Marge for agemin and agemax for(iage=agemin-AGEMARGE; iage <= agemax+3+AGEMARGE; iage++) */
1291: #define AGEBASE 40
1292: #define AGEOVERFLOW 1.e20
1293: #define AGEGOMP 10 /**< Minimal age for Gompertz adjustment */
1294: #ifdef _WIN32
1295: #define DIRSEPARATOR '\\'
1296: #define CHARSEPARATOR "\\"
1297: #define ODIRSEPARATOR '/'
1298: #else
1299: #define DIRSEPARATOR '/'
1300: #define CHARSEPARATOR "/"
1301: #define ODIRSEPARATOR '\\'
1302: #endif
1303:
1304: /* $Id: imach.c,v 1.338 2022/09/04 17:40:33 brouard Exp $ */
1305: /* $State: Exp $ */
1306: #include "version.h"
1307: char version[]=__IMACH_VERSION__;
1308: char copyright[]="September 2022,INED-EUROREVES-Institut de longevite-Japan Society for the Promotion of Science (Grant-in-Aid for Scientific Research 25293121), Intel Software 2015-2020, Nihon University 2021-202, INED 2000-2022";
1309: char fullversion[]="$Revision: 1.338 $ $Date: 2022/09/04 17:40:33 $";
1310: char strstart[80];
1311: char optionfilext[10], optionfilefiname[FILENAMELENGTH];
1312: int erreur=0, nberr=0, nbwarn=0; /* Error number, number of errors number of warnings */
1313: int nagesqr=0, nforce=0; /* nagesqr=1 if model is including age*age, number of forces */
1314: /* Number of covariates model (1)=V2+V1+ V3*age+V2*V4 */
1315: /* Model(2) V1 + V2 + V3 + V8 + V7*V8 + V5*V6 + V8*age + V3*age + age*age */
1316: int cptcovn=0; /**< cptcovn decodemodel: number of covariates k of the models excluding age*products =6 and age*age but including products */
1317: int cptcovt=0; /**< cptcovt: total number of covariates of the model (2) nbocc(+)+1 = 8 excepting constant and age and age*age */
1318: int cptcovs=0; /**< cptcovs number of SIMPLE covariates in the model V2+V1 =2 (dummy or quantit or time varying) */
1319: int cptcovsnq=0; /**< cptcovsnq number of SIMPLE covariates in the model but non quantitative V2+V1 =2 */
1320: int cptcovage=0; /**< Number of covariates with age: V3*age only =1 */
1321: int cptcovprodnoage=0; /**< Number of covariate products without age */
1322: int cptcoveff=0; /* Total number of single dummy covariates (fixed or time varying) to vary for printing results (2**cptcoveff combinations of dummies)(computed in tricode as cptcov) */
1323: int ncovf=0; /* Total number of effective fixed covariates (dummy or quantitative) in the model */
1324: int ncovv=0; /* Total number of effective (wave) varying covariates (dummy or quantitative) in the model */
1325: int ncova=0; /* Total number of effective (wave and stepm) varying with age covariates (dummy of quantitative) in the model */
1326: int nsd=0; /**< Total number of single dummy variables (output) */
1327: int nsq=0; /**< Total number of single quantitative variables (output) */
1328: int ncoveff=0; /* Total number of effective fixed dummy covariates in the model */
1329: int nqfveff=0; /**< nqfveff Number of Quantitative Fixed Variables Effective */
1330: int ntveff=0; /**< ntveff number of effective time varying variables */
1331: int nqtveff=0; /**< ntqveff number of effective time varying quantitative variables */
1332: int cptcov=0; /* Working variable */
1333: int firstobs=1, lastobs=10; /* nobs = lastobs-firstobs+1 declared globally ;*/
1334: int nobs=10; /* Number of observations in the data lastobs-firstobs */
1335: int ncovcombmax=NCOVMAX; /* Maximum calculated number of covariate combination = pow(2, cptcoveff) */
1336: int npar=NPARMAX; /* Number of parameters (nlstate+ndeath-1)*nlstate*ncovmodel; */
1337: int nlstate=2; /* Number of live states */
1338: int ndeath=1; /* Number of dead states */
1339: int ncovmodel=0, ncovcol=0; /* Total number of covariables including constant a12*1 +b12*x ncovmodel=2 */
1340: int nqv=0, ntv=0, nqtv=0; /* Total number of quantitative variables, time variable (dummy), quantitative and time variable */
1341: int popbased=0;
1342:
1343: int *wav; /* Number of waves for this individuual 0 is possible */
1344: int maxwav=0; /* Maxim number of waves */
1345: int jmin=0, jmax=0; /* min, max spacing between 2 waves */
1346: int ijmin=0, ijmax=0; /* Individuals having jmin and jmax */
1347: int gipmx=0, gsw=0; /* Global variables on the number of contributions
1348: to the likelihood and the sum of weights (done by funcone)*/
1349: int mle=1, weightopt=0;
1350: int **mw; /* mw[mi][i] is number of the mi wave for this individual */
1351: int **dh; /* dh[mi][i] is number of steps between mi,mi+1 for this individual */
1352: int **bh; /* bh[mi][i] is the bias (+ or -) for this individual if the delay between
1353: * wave mi and wave mi+1 is not an exact multiple of stepm. */
1354: int countcallfunc=0; /* Count the number of calls to func */
1355: int selected(int kvar); /* Is covariate kvar selected for printing results */
1356:
1357: double jmean=1; /* Mean space between 2 waves */
1358: double **matprod2(); /* test */
1359: double **oldm, **newm, **savm; /* Working pointers to matrices */
1360: double **oldms, **newms, **savms; /* Fixed working pointers to matrices */
1361: double **ddnewms, **ddoldms, **ddsavms; /* for freeing later */
1362:
1363: /*FILE *fic ; */ /* Used in readdata only */
1364: FILE *ficpar, *ficparo,*ficres, *ficresp, *ficresphtm, *ficresphtmfr, *ficrespl, *ficresplb,*ficrespij, *ficrespijb, *ficrest,*ficresf, *ficresfb,*ficrespop;
1365: FILE *ficlog, *ficrespow;
1366: int globpr=0; /* Global variable for printing or not */
1367: double fretone; /* Only one call to likelihood */
1368: long ipmx=0; /* Number of contributions */
1369: double sw; /* Sum of weights */
1370: char filerespow[FILENAMELENGTH];
1371: char fileresilk[FILENAMELENGTH]; /* File of individual contributions to the likelihood */
1372: FILE *ficresilk;
1373: FILE *ficgp,*ficresprob,*ficpop, *ficresprobcov, *ficresprobcor;
1374: FILE *ficresprobmorprev;
1375: FILE *fichtm, *fichtmcov; /* Html File */
1376: FILE *ficreseij;
1377: char filerese[FILENAMELENGTH];
1378: FILE *ficresstdeij;
1379: char fileresstde[FILENAMELENGTH];
1380: FILE *ficrescveij;
1381: char filerescve[FILENAMELENGTH];
1382: FILE *ficresvij;
1383: char fileresv[FILENAMELENGTH];
1384:
1385: char title[MAXLINE];
1386: char model[MAXLINE]; /**< The model line */
1387: char optionfile[FILENAMELENGTH], datafile[FILENAMELENGTH], filerespl[FILENAMELENGTH], fileresplb[FILENAMELENGTH];
1388: char plotcmd[FILENAMELENGTH], pplotcmd[FILENAMELENGTH];
1389: char tmpout[FILENAMELENGTH], tmpout2[FILENAMELENGTH];
1390: char command[FILENAMELENGTH];
1391: int outcmd=0;
1392:
1393: char fileres[FILENAMELENGTH], filerespij[FILENAMELENGTH], filerespijb[FILENAMELENGTH], filereso[FILENAMELENGTH], rfileres[FILENAMELENGTH];
1394: char fileresu[FILENAMELENGTH]; /* fileres without r in front */
1395: char filelog[FILENAMELENGTH]; /* Log file */
1396: char filerest[FILENAMELENGTH];
1397: char fileregp[FILENAMELENGTH];
1398: char popfile[FILENAMELENGTH];
1399:
1400: char optionfilegnuplot[FILENAMELENGTH], optionfilehtm[FILENAMELENGTH], optionfilehtmcov[FILENAMELENGTH] ;
1401:
1402: /* struct timeval start_time, end_time, curr_time, last_time, forecast_time; */
1403: /* struct timezone tzp; */
1404: /* extern int gettimeofday(); */
1405: struct tm tml, *gmtime(), *localtime();
1406:
1407: extern time_t time();
1408:
1409: struct tm start_time, end_time, curr_time, last_time, forecast_time;
1410: time_t rstart_time, rend_time, rcurr_time, rlast_time, rforecast_time; /* raw time */
1411: struct tm tm;
1412:
1413: char strcurr[80], strfor[80];
1414:
1415: char *endptr;
1416: long lval;
1417: double dval;
1418:
1419: #define NR_END 1
1420: #define FREE_ARG char*
1421: #define FTOL 1.0e-10
1422:
1423: #define NRANSI
1424: #define ITMAX 200
1425: #define ITPOWMAX 20 /* This is now multiplied by the number of parameters */
1426:
1427: #define TOL 2.0e-4
1428:
1429: #define CGOLD 0.3819660
1430: #define ZEPS 1.0e-10
1431: #define SHFT(a,b,c,d) (a)=(b);(b)=(c);(c)=(d);
1432:
1433: #define GOLD 1.618034
1434: #define GLIMIT 100.0
1435: #define TINY 1.0e-20
1436:
1437: static double maxarg1,maxarg2;
1438: #define FMAX(a,b) (maxarg1=(a),maxarg2=(b),(maxarg1)>(maxarg2)? (maxarg1):(maxarg2))
1439: #define FMIN(a,b) (maxarg1=(a),maxarg2=(b),(maxarg1)<(maxarg2)? (maxarg1):(maxarg2))
1440:
1441: #define SIGN(a,b) ((b)>0.0 ? fabs(a) : -fabs(a))
1442: #define rint(a) floor(a+0.5)
1443: /* http://www.thphys.uni-heidelberg.de/~robbers/cmbeasy/doc/html/myutils_8h-source.html */
1444: #define mytinydouble 1.0e-16
1445: /* #define DEQUAL(a,b) (fabs((a)-(b))<mytinydouble) */
1446: /* http://www.thphys.uni-heidelberg.de/~robbers/cmbeasy/doc/html/mynrutils_8h-source.html */
1447: /* static double dsqrarg; */
1448: /* #define DSQR(a) (DEQUAL((dsqrarg=(a)),0.0) ? 0.0 : dsqrarg*dsqrarg) */
1449: static double sqrarg;
1450: #define SQR(a) ((sqrarg=(a)) == 0.0 ? 0.0 :sqrarg*sqrarg)
1451: #define SWAP(a,b) {temp=(a);(a)=(b);(b)=temp;}
1452: int agegomp= AGEGOMP;
1453:
1454: int imx;
1455: int stepm=1;
1456: /* Stepm, step in month: minimum step interpolation*/
1457:
1458: int estepm;
1459: /* Estepm, step in month to interpolate survival function in order to approximate Life Expectancy*/
1460:
1461: int m,nb;
1462: long *num;
1463: int firstpass=0, lastpass=4,*cod, *cens;
1464: int *ncodemax; /* ncodemax[j]= Number of modalities of the j th
1465: covariate for which somebody answered excluding
1466: undefined. Usually 2: 0 and 1. */
1467: int *ncodemaxwundef; /* ncodemax[j]= Number of modalities of the j th
1468: covariate for which somebody answered including
1469: undefined. Usually 3: -1, 0 and 1. */
1470: double **agev,*moisnais, *annais, *moisdc, *andc,**mint, **anint;
1471: double **pmmij, ***probs; /* Global pointer */
1472: double ***mobaverage, ***mobaverages; /* New global variable */
1473: double **precov; /* New global variable to store for each resultline, values of model covariates given by the resultlines (in order to speed up) */
1474: double *ageexmed,*agecens;
1475: double dateintmean=0;
1476: double anprojd, mprojd, jprojd; /* For eventual projections */
1477: double anprojf, mprojf, jprojf;
1478:
1479: double anbackd, mbackd, jbackd; /* For eventual backprojections */
1480: double anbackf, mbackf, jbackf;
1481: double jintmean,mintmean,aintmean;
1482: double *weight;
1483: int **s; /* Status */
1484: double *agedc;
1485: double **covar; /**< covar[j,i], value of jth covariate for individual i,
1486: * covar=matrix(0,NCOVMAX,1,n);
1487: * cov[Tage[kk]+2]=covar[Tvar[Tage[kk]]][i]*age; */
1488: double **coqvar; /* Fixed quantitative covariate nqv */
1489: double ***cotvar; /* Time varying covariate ntv */
1490: double ***cotqvar; /* Time varying quantitative covariate itqv */
1491: double idx;
1492: int **nbcode, *Tvar; /**< model=V2 => Tvar[1]= 2 */
1493: /* Some documentation */
1494: /* Design original data
1495: * V1 V2 V3 V4 V5 V6 V7 V8 Weight ddb ddth d1st s1 V9 V10 V11 V12 s2 V9 V10 V11 V12
1496: * < ncovcol=6 > nqv=2 (V7 V8) dv dv dv qtv dv dv dvv qtv
1497: * ntv=3 nqtv=1
1498: * cptcovn number of covariates (not including constant and age or age*age) = number of plus sign + 1 = 10+1=11
1499: * For time varying covariate, quanti or dummies
1500: * cotqvar[wav][iv(1 to nqtv)][i]= [1][12][i]=(V12) quanti
1501: * cotvar[wav][ntv+iv][i]= [3+(1 to nqtv)][i]=(V12) quanti
1502: * cotvar[wav][iv(1 to ntv)][i]= [1][1][i]=(V9) dummies at wav 1
1503: * cotvar[wav][iv(1 to ntv)][i]= [1][2][i]=(V10) dummies at wav 1
1504: * covar[Vk,i], value of the Vkth fixed covariate dummy or quanti for individual i:
1505: * covar[1][i]= (V1), covar[4][i]=(V4), covar[8][i]=(V8)
1506: * Model V2 + V1 + V3*age + V3 + V5*V6 + V7*V8 + V8*age + V8 + V9 + V9*age + V10
1507: * k= 1 2 3 4 5 6 7 8 9 10 11
1508: */
1509: /* According to the model, more columns can be added to covar by the product of covariates */
1510: /* ncovcol=1(Males=0 Females=1) nqv=1(raedyrs) ntv=2(withoutiadl=0 withiadl=1, witoutadl=0 withoutadl=1) nqtv=1(bmi) nlstate=3 ndeath=1
1511: # States 1=Coresidence, 2 Living alone, 3 Institution
1512: # V1=sex, V2=raedyrs Quant Fixed, State=livarnb4..livarnb11, V3=iadl4..iald11, V4=adlw4..adlw11, V5=r4bmi..r11bmi
1513: */
1514: /* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
1515: /* k 1 2 3 4 5 6 7 8 9 */
1516: /*Typevar[k]= 0 0 0 2 1 0 2 1 0 *//*0 for simple covariate (dummy, quantitative,*/
1517: /* fixed or varying), 1 for age product, 2 for*/
1518: /* product */
1519: /*Dummy[k]= 1 0 0 1 3 1 1 2 0 *//*Dummy[k] 0=dummy (0 1), 1 quantitative */
1520: /*(single or product without age), 2 dummy*/
1521: /* with age product, 3 quant with age product*/
1522: /*Tvar[k]= 5 4 3 6 5 2 7 1 1 */
1523: /* nsd 1 2 3 */ /* Counting single dummies covar fixed or tv */
1524: /*TnsdVar[Tvar] 1 2 3 */
1525: /*Tvaraff[nsd] 4 3 1 */ /* ID of single dummy cova fixed or timevary*/
1526: /*TvarsD[nsd] 4 3 1 */ /* ID of single dummy cova fixed or timevary*/
1527: /*TvarsDind[nsd] 2 3 9 */ /* position K of single dummy cova */
1528: /* nsq 1 2 */ /* Counting single quantit tv */
1529: /* TvarsQ[k] 5 2 */ /* Number of single quantitative cova */
1530: /* TvarsQind 1 6 */ /* position K of single quantitative cova */
1531: /* Tprod[i]=k 1 2 */ /* Position in model of the ith prod without age */
1532: /* cptcovage 1 2 */ /* Counting cov*age in the model equation */
1533: /* Tage[cptcovage]=k 5 8 */ /* Position in the model of ith cov*age */
1534: /* Tvard[1][1]@4={4,3,1,2} V4*V3 V1*V2 */ /* Position in model of the ith prod without age */
1535: /* Tvardk[4][1]=4;Tvardk[4][2]=3;Tvardk[7][1]=1;Tvardk[7][2]=2 */ /* Variables of a prod at position in the model equation*/
1536: /* TvarF TvarF[1]=Tvar[6]=2, TvarF[2]=Tvar[7]=7, TvarF[3]=Tvar[9]=1 ID of fixed covariates or product V2, V1*V2, V1 */
1537: /* TvarFind; TvarFind[1]=6, TvarFind[2]=7, TvarFind[3]=9 *//* Inverse V2(6) is first fixed (single or prod) */
1538: /* Type */
1539: /* V 1 2 3 4 5 */
1540: /* F F V V V */
1541: /* D Q D D Q */
1542: /* */
1543: int *TvarsD;
1544: int *TnsdVar;
1545: int *TvarsDind;
1546: int *TvarsQ;
1547: int *TvarsQind;
1548:
1549: #define MAXRESULTLINESPONE 10+1
1550: int nresult=0;
1551: int parameterline=0; /* # of the parameter (type) line */
1552: int TKresult[MAXRESULTLINESPONE]; /* TKresult[nres]=k for each resultline nres give the corresponding combination of dummies */
1553: int resultmodel[MAXRESULTLINESPONE][NCOVMAX];/* resultmodel[k1]=k3: k1th position in the model corresponds to the k3 position in the resultline */
1554: int modelresult[MAXRESULTLINESPONE][NCOVMAX];/* modelresult[k3]=k1: k1th position in the model corresponds to the k3 position in the resultline */
1555: int Tresult[MAXRESULTLINESPONE][NCOVMAX];/* Tresult[nres][result_position]= value of the dummy variable at the result_position in the nres resultline */
1556: int Tinvresult[MAXRESULTLINESPONE][NCOVMAX];/* Tinvresult[nres][Name of a dummy variable]= value of the variable in the result line */
1557: double TinvDoQresult[MAXRESULTLINESPONE][NCOVMAX];/* TinvDoQresult[nres][Name of a Dummy or Q variable]= value of the variable in the result line */
1558: int Tvresult[MAXRESULTLINESPONE][NCOVMAX]; /* Tvresult[nres][result_position]= name of the dummy variable at the result_position in the nres resultline */
1559: double Tqresult[MAXRESULTLINESPONE][NCOVMAX]; /* Tqresult[nres][result_position]= value of the variable at the result_position in the nres resultline */
1560: double Tqinvresult[MAXRESULTLINESPONE][NCOVMAX]; /* For quantitative variable , value (output) */
1561: int Tvqresult[MAXRESULTLINESPONE][NCOVMAX]; /* Tvqresult[nres][result_position]= id of the variable at the result_position in the nres resultline */
1562:
1563: /* ncovcol=1(Males=0 Females=1) nqv=1(raedyrs) ntv=2(withoutiadl=0 withiadl=1, witoutadl=0 withoutadl=1) nqtv=1(bmi) nlstate=3 ndeath=1
1564: # States 1=Coresidence, 2 Living alone, 3 Institution
1565: # V1=sex, V2=raedyrs Quant Fixed, State=livarnb4..livarnb11, V3=iadl4..iald11, V4=adlw4..adlw11, V5=r4bmi..r11bmi
1566: */
1567: /* 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 *\/ */
1568: 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 */
1569: 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 */
1570: 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 */
1571: int *TvarVind; /**< TvarVind[1]=1, TvarVind[2]=2 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
1572: 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 */
1573: int *TvarAind; /**< TvarindA[1]=5, TvarAind[2]=8 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
1574: int *TvarFD; /**< TvarFD[1]=V1 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
1575: int *TvarFDind; /* TvarFDind[1]=9 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
1576: int *TvarFQ; /* TvarFQ[1]=V2 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ /* Only simple fixed quantitative variable */
1577: int *TvarFQind; /* TvarFQind[1]=6 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ /* Only simple fixed quantitative variable */
1578: int *TvarVD; /* TvarVD[1]=V5 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ /* Only simple fixed quantitative variable */
1579: int *TvarVDind; /* TvarVDind[1]=1 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ /* Only simple fixed quantitative variable */
1580: 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 */
1581: 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 */
1582:
1583: int *Tvarsel; /**< Selected covariates for output */
1584: double *Tvalsel; /**< Selected modality value of covariate for output */
1585: int *Typevar; /**< 0 for simple covariate (dummy, quantitative, fixed or varying), 1 for age product, 2 for product */
1586: int *Fixed; /** Fixed[k] 0=fixed, 1 varying, 2 fixed with age product, 3 varying with age product */
1587: 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 */
1588: int *DummyV; /** Dummy[v] 0=dummy (0 1), 1 quantitative */
1589: int *FixedV; /** FixedV[v] 0 fixed, 1 varying */
1590: int *Tage;
1591: int anyvaryingduminmodel=0; /**< Any varying dummy in Model=1 yes, 0 no, to avoid a loop on waves in freq */
1592: 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*/
1593: 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*/
1594: 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 */
1595: int *Ndum; /** Freq of modality (tricode */
1596: /* int **codtab;*/ /**< codtab=imatrix(1,100,1,10); */
1597: int **Tvard;
1598: int **Tvardk;
1599: int *Tprod;/**< Gives the k position of the k1 product */
1600: /* Tprod[k1=1]=3(=V1*V4) for V2+V1+V1*V4+age*V3 */
1601: int *Tposprod; /**< Gives the k1 product from the k position */
1602: /* if V2+V1+V1*V4+age*V3+V3*V2 TProd[k1=2]=5 (V3*V2) */
1603: /* Tposprod[k]=k1 , Tposprod[3]=1, Tposprod[5(V3*V2)]=2 (2nd product without age) */
1604: int cptcovprod, *Tvaraff, *invalidvarcomb;
1605: double *lsurv, *lpop, *tpop;
1606:
1607: #define FD 1; /* Fixed dummy covariate */
1608: #define FQ 2; /* Fixed quantitative covariate */
1609: #define FP 3; /* Fixed product covariate */
1610: #define FPDD 7; /* Fixed product dummy*dummy covariate */
1611: #define FPDQ 8; /* Fixed product dummy*quantitative covariate */
1612: #define FPQQ 9; /* Fixed product quantitative*quantitative covariate */
1613: #define VD 10; /* Varying dummy covariate */
1614: #define VQ 11; /* Varying quantitative covariate */
1615: #define VP 12; /* Varying product covariate */
1616: #define VPDD 13; /* Varying product dummy*dummy covariate */
1617: #define VPDQ 14; /* Varying product dummy*quantitative covariate */
1618: #define VPQQ 15; /* Varying product quantitative*quantitative covariate */
1619: #define APFD 16; /* Age product * fixed dummy covariate */
1620: #define APFQ 17; /* Age product * fixed quantitative covariate */
1621: #define APVD 18; /* Age product * varying dummy covariate */
1622: #define APVQ 19; /* Age product * varying quantitative covariate */
1623:
1624: #define FTYPE 1; /* Fixed covariate */
1625: #define VTYPE 2; /* Varying covariate (loop in wave) */
1626: #define ATYPE 2; /* Age product covariate (loop in dh within wave)*/
1627:
1628: struct kmodel{
1629: int maintype; /* main type */
1630: int subtype; /* subtype */
1631: };
1632: struct kmodel modell[NCOVMAX];
1633:
1634: double ftol=FTOL; /**< Tolerance for computing Max Likelihood */
1635: double ftolhess; /**< Tolerance for computing hessian */
1636:
1637: /**************** split *************************/
1638: static int split( char *path, char *dirc, char *name, char *ext, char *finame )
1639: {
1640: /* From a file name with (full) path (either Unix or Windows) we extract the directory (dirc)
1641: the name of the file (name), its extension only (ext) and its first part of the name (finame)
1642: */
1643: char *ss; /* pointer */
1644: int l1=0, l2=0; /* length counters */
1645:
1646: l1 = strlen(path ); /* length of path */
1647: if ( l1 == 0 ) return( GLOCK_ERROR_NOPATH );
1648: ss= strrchr( path, DIRSEPARATOR ); /* find last / */
1649: if ( ss == NULL ) { /* no directory, so determine current directory */
1650: strcpy( name, path ); /* we got the fullname name because no directory */
1651: /*if(strrchr(path, ODIRSEPARATOR )==NULL)
1652: printf("Warning you should use %s as a separator\n",DIRSEPARATOR);*/
1653: /* get current working directory */
1654: /* extern char* getcwd ( char *buf , int len);*/
1655: #ifdef WIN32
1656: if (_getcwd( dirc, FILENAME_MAX ) == NULL ) {
1657: #else
1658: if (getcwd(dirc, FILENAME_MAX) == NULL) {
1659: #endif
1660: return( GLOCK_ERROR_GETCWD );
1661: }
1662: /* got dirc from getcwd*/
1663: printf(" DIRC = %s \n",dirc);
1664: } else { /* strip directory from path */
1665: ss++; /* after this, the filename */
1666: l2 = strlen( ss ); /* length of filename */
1667: if ( l2 == 0 ) return( GLOCK_ERROR_NOPATH );
1668: strcpy( name, ss ); /* save file name */
1669: strncpy( dirc, path, l1 - l2 ); /* now the directory */
1670: dirc[l1-l2] = '\0'; /* add zero */
1671: printf(" DIRC2 = %s \n",dirc);
1672: }
1673: /* We add a separator at the end of dirc if not exists */
1674: l1 = strlen( dirc ); /* length of directory */
1675: if( dirc[l1-1] != DIRSEPARATOR ){
1676: dirc[l1] = DIRSEPARATOR;
1677: dirc[l1+1] = 0;
1678: printf(" DIRC3 = %s \n",dirc);
1679: }
1680: ss = strrchr( name, '.' ); /* find last / */
1681: if (ss >0){
1682: ss++;
1683: strcpy(ext,ss); /* save extension */
1684: l1= strlen( name);
1685: l2= strlen(ss)+1;
1686: strncpy( finame, name, l1-l2);
1687: finame[l1-l2]= 0;
1688: }
1689:
1690: return( 0 ); /* we're done */
1691: }
1692:
1693:
1694: /******************************************/
1695:
1696: void replace_back_to_slash(char *s, char*t)
1697: {
1698: int i;
1699: int lg=0;
1700: i=0;
1701: lg=strlen(t);
1702: for(i=0; i<= lg; i++) {
1703: (s[i] = t[i]);
1704: if (t[i]== '\\') s[i]='/';
1705: }
1706: }
1707:
1708: char *trimbb(char *out, char *in)
1709: { /* Trim multiple blanks in line but keeps first blanks if line starts with blanks */
1710: char *s;
1711: s=out;
1712: while (*in != '\0'){
1713: while( *in == ' ' && *(in+1) == ' '){ /* && *(in+1) != '\0'){*/
1714: in++;
1715: }
1716: *out++ = *in++;
1717: }
1718: *out='\0';
1719: return s;
1720: }
1721:
1722: /* char *substrchaine(char *out, char *in, char *chain) */
1723: /* { */
1724: /* /\* Substract chain 'chain' from 'in', return and output 'out' *\/ */
1725: /* char *s, *t; */
1726: /* t=in;s=out; */
1727: /* while ((*in != *chain) && (*in != '\0')){ */
1728: /* *out++ = *in++; */
1729: /* } */
1730:
1731: /* /\* *in matches *chain *\/ */
1732: /* while ((*in++ == *chain++) && (*in != '\0')){ */
1733: /* printf("*in = %c, *out= %c *chain= %c \n", *in, *out, *chain); */
1734: /* } */
1735: /* in--; chain--; */
1736: /* while ( (*in != '\0')){ */
1737: /* printf("Bef *in = %c, *out= %c *chain= %c \n", *in, *out, *chain); */
1738: /* *out++ = *in++; */
1739: /* printf("Aft *in = %c, *out= %c *chain= %c \n", *in, *out, *chain); */
1740: /* } */
1741: /* *out='\0'; */
1742: /* out=s; */
1743: /* return out; */
1744: /* } */
1745: char *substrchaine(char *out, char *in, char *chain)
1746: {
1747: /* Substract chain 'chain' from 'in', return and output 'out' */
1748: /* in="V1+V1*age+age*age+V2", chain="age*age" */
1749:
1750: char *strloc;
1751:
1752: strcpy (out, in);
1753: strloc = strstr(out, chain); /* strloc points to out at age*age+V2 */
1754: printf("Bef strloc=%s chain=%s out=%s \n", strloc, chain, out);
1755: if(strloc != NULL){
1756: /* will affect out */ /* strloc+strlenc(chain)=+V2 */ /* Will also work in Unicode */
1757: memmove(strloc,strloc+strlen(chain), strlen(strloc+strlen(chain))+1);
1758: /* strcpy (strloc, strloc +strlen(chain));*/
1759: }
1760: printf("Aft strloc=%s chain=%s in=%s out=%s \n", strloc, chain, in, out);
1761: return out;
1762: }
1763:
1764:
1765: char *cutl(char *blocc, char *alocc, char *in, char occ)
1766: {
1767: /* cuts string in into blocc and alocc where blocc ends before FIRST occurence of char 'occ'
1768: and alocc starts after first occurence of char 'occ' : ex cutv(blocc,alocc,"abcdef2ghi2j",'2')
1769: gives alocc="abcdef" and blocc="ghi2j".
1770: If occ is not found blocc is null and alocc is equal to in. Returns blocc
1771: */
1772: char *s, *t;
1773: t=in;s=in;
1774: while ((*in != occ) && (*in != '\0')){
1775: *alocc++ = *in++;
1776: }
1777: if( *in == occ){
1778: *(alocc)='\0';
1779: s=++in;
1780: }
1781:
1782: if (s == t) {/* occ not found */
1783: *(alocc-(in-s))='\0';
1784: in=s;
1785: }
1786: while ( *in != '\0'){
1787: *blocc++ = *in++;
1788: }
1789:
1790: *blocc='\0';
1791: return t;
1792: }
1793: char *cutv(char *blocc, char *alocc, char *in, char occ)
1794: {
1795: /* cuts string in into blocc and alocc where blocc ends before LAST occurence of char 'occ'
1796: and alocc starts after last occurence of char 'occ' : ex cutv(blocc,alocc,"abcdef2ghi2j",'2')
1797: gives blocc="abcdef2ghi" and alocc="j".
1798: If occ is not found blocc is null and alocc is equal to in. Returns alocc
1799: */
1800: char *s, *t;
1801: t=in;s=in;
1802: while (*in != '\0'){
1803: while( *in == occ){
1804: *blocc++ = *in++;
1805: s=in;
1806: }
1807: *blocc++ = *in++;
1808: }
1809: if (s == t) /* occ not found */
1810: *(blocc-(in-s))='\0';
1811: else
1812: *(blocc-(in-s)-1)='\0';
1813: in=s;
1814: while ( *in != '\0'){
1815: *alocc++ = *in++;
1816: }
1817:
1818: *alocc='\0';
1819: return s;
1820: }
1821:
1822: int nbocc(char *s, char occ)
1823: {
1824: int i,j=0;
1825: int lg=20;
1826: i=0;
1827: lg=strlen(s);
1828: for(i=0; i<= lg; i++) {
1829: if (s[i] == occ ) j++;
1830: }
1831: return j;
1832: }
1833:
1834: /* void cutv(char *u,char *v, char*t, char occ) */
1835: /* { */
1836: /* /\* cuts string t into u and v where u ends before last occurence of char 'occ' */
1837: /* and v starts after last occurence of char 'occ' : ex cutv(u,v,"abcdef2ghi2j",'2') */
1838: /* gives u="abcdef2ghi" and v="j" *\/ */
1839: /* int i,lg,j,p=0; */
1840: /* i=0; */
1841: /* lg=strlen(t); */
1842: /* for(j=0; j<=lg-1; j++) { */
1843: /* if((t[j]!= occ) && (t[j+1]== occ)) p=j+1; */
1844: /* } */
1845:
1846: /* for(j=0; j<p; j++) { */
1847: /* (u[j] = t[j]); */
1848: /* } */
1849: /* u[p]='\0'; */
1850:
1851: /* for(j=0; j<= lg; j++) { */
1852: /* if (j>=(p+1))(v[j-p-1] = t[j]); */
1853: /* } */
1854: /* } */
1855:
1856: #ifdef _WIN32
1857: char * strsep(char **pp, const char *delim)
1858: {
1859: char *p, *q;
1860:
1861: if ((p = *pp) == NULL)
1862: return 0;
1863: if ((q = strpbrk (p, delim)) != NULL)
1864: {
1865: *pp = q + 1;
1866: *q = '\0';
1867: }
1868: else
1869: *pp = 0;
1870: return p;
1871: }
1872: #endif
1873:
1874: /********************** nrerror ********************/
1875:
1876: void nrerror(char error_text[])
1877: {
1878: fprintf(stderr,"ERREUR ...\n");
1879: fprintf(stderr,"%s\n",error_text);
1880: exit(EXIT_FAILURE);
1881: }
1882: /*********************** vector *******************/
1883: double *vector(int nl, int nh)
1884: {
1885: double *v;
1886: v=(double *) malloc((size_t)((nh-nl+1+NR_END)*sizeof(double)));
1887: if (!v) nrerror("allocation failure in vector");
1888: return v-nl+NR_END;
1889: }
1890:
1891: /************************ free vector ******************/
1892: void free_vector(double*v, int nl, int nh)
1893: {
1894: free((FREE_ARG)(v+nl-NR_END));
1895: }
1896:
1897: /************************ivector *******************************/
1898: int *ivector(long nl,long nh)
1899: {
1900: int *v;
1901: v=(int *) malloc((size_t)((nh-nl+1+NR_END)*sizeof(int)));
1902: if (!v) nrerror("allocation failure in ivector");
1903: return v-nl+NR_END;
1904: }
1905:
1906: /******************free ivector **************************/
1907: void free_ivector(int *v, long nl, long nh)
1908: {
1909: free((FREE_ARG)(v+nl-NR_END));
1910: }
1911:
1912: /************************lvector *******************************/
1913: long *lvector(long nl,long nh)
1914: {
1915: long *v;
1916: v=(long *) malloc((size_t)((nh-nl+1+NR_END)*sizeof(long)));
1917: if (!v) nrerror("allocation failure in ivector");
1918: return v-nl+NR_END;
1919: }
1920:
1921: /******************free lvector **************************/
1922: void free_lvector(long *v, long nl, long nh)
1923: {
1924: free((FREE_ARG)(v+nl-NR_END));
1925: }
1926:
1927: /******************* imatrix *******************************/
1928: int **imatrix(long nrl, long nrh, long ncl, long nch)
1929: /* allocate a int matrix with subscript range m[nrl..nrh][ncl..nch] */
1930: {
1931: long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
1932: int **m;
1933:
1934: /* allocate pointers to rows */
1935: m=(int **) malloc((size_t)((nrow+NR_END)*sizeof(int*)));
1936: if (!m) nrerror("allocation failure 1 in matrix()");
1937: m += NR_END;
1938: m -= nrl;
1939:
1940:
1941: /* allocate rows and set pointers to them */
1942: m[nrl]=(int *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(int)));
1943: if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
1944: m[nrl] += NR_END;
1945: m[nrl] -= ncl;
1946:
1947: for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
1948:
1949: /* return pointer to array of pointers to rows */
1950: return m;
1951: }
1952:
1953: /****************** free_imatrix *************************/
1954: void free_imatrix(m,nrl,nrh,ncl,nch)
1955: int **m;
1956: long nch,ncl,nrh,nrl;
1957: /* free an int matrix allocated by imatrix() */
1958: {
1959: free((FREE_ARG) (m[nrl]+ncl-NR_END));
1960: free((FREE_ARG) (m+nrl-NR_END));
1961: }
1962:
1963: /******************* matrix *******************************/
1964: double **matrix(long nrl, long nrh, long ncl, long nch)
1965: {
1966: long i, nrow=nrh-nrl+1, ncol=nch-ncl+1;
1967: double **m;
1968:
1969: m=(double **) malloc((size_t)((nrow+NR_END)*sizeof(double*)));
1970: if (!m) nrerror("allocation failure 1 in matrix()");
1971: m += NR_END;
1972: m -= nrl;
1973:
1974: m[nrl]=(double *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(double)));
1975: if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
1976: m[nrl] += NR_END;
1977: m[nrl] -= ncl;
1978:
1979: for (i=nrl+1; i<=nrh; i++) m[i]=m[i-1]+ncol;
1980: return m;
1981: /* print *(*(m+1)+70) or print m[1][70]; print m+1 or print &(m[1]) or &(m[1][0])
1982: m[i] = address of ith row of the table. &(m[i]) is its value which is another adress
1983: that of m[i][0]. In order to get the value p m[i][0] but it is unitialized.
1984: */
1985: }
1986:
1987: /*************************free matrix ************************/
1988: void free_matrix(double **m, long nrl, long nrh, long ncl, long nch)
1989: {
1990: free((FREE_ARG)(m[nrl]+ncl-NR_END));
1991: free((FREE_ARG)(m+nrl-NR_END));
1992: }
1993:
1994: /******************* ma3x *******************************/
1995: double ***ma3x(long nrl, long nrh, long ncl, long nch, long nll, long nlh)
1996: {
1997: long i, j, nrow=nrh-nrl+1, ncol=nch-ncl+1, nlay=nlh-nll+1;
1998: double ***m;
1999:
2000: m=(double ***) malloc((size_t)((nrow+NR_END)*sizeof(double*)));
2001: if (!m) nrerror("allocation failure 1 in matrix()");
2002: m += NR_END;
2003: m -= nrl;
2004:
2005: m[nrl]=(double **) malloc((size_t)((nrow*ncol+NR_END)*sizeof(double)));
2006: if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
2007: m[nrl] += NR_END;
2008: m[nrl] -= ncl;
2009:
2010: for (i=nrl+1; i<=nrh; i++) m[i]=m[i-1]+ncol;
2011:
2012: m[nrl][ncl]=(double *) malloc((size_t)((nrow*ncol*nlay+NR_END)*sizeof(double)));
2013: if (!m[nrl][ncl]) nrerror("allocation failure 3 in matrix()");
2014: m[nrl][ncl] += NR_END;
2015: m[nrl][ncl] -= nll;
2016: for (j=ncl+1; j<=nch; j++)
2017: m[nrl][j]=m[nrl][j-1]+nlay;
2018:
2019: for (i=nrl+1; i<=nrh; i++) {
2020: m[i][ncl]=m[i-1l][ncl]+ncol*nlay;
2021: for (j=ncl+1; j<=nch; j++)
2022: m[i][j]=m[i][j-1]+nlay;
2023: }
2024: return m;
2025: /* gdb: p *(m+1) <=> p m[1] and p (m+1) <=> p (m+1) <=> p &(m[1])
2026: &(m[i][j][k]) <=> *((*(m+i) + j)+k)
2027: */
2028: }
2029:
2030: /*************************free ma3x ************************/
2031: void free_ma3x(double ***m, long nrl, long nrh, long ncl, long nch,long nll, long nlh)
2032: {
2033: free((FREE_ARG)(m[nrl][ncl]+ nll-NR_END));
2034: free((FREE_ARG)(m[nrl]+ncl-NR_END));
2035: free((FREE_ARG)(m+nrl-NR_END));
2036: }
2037:
2038: /*************** function subdirf ***********/
2039: char *subdirf(char fileres[])
2040: {
2041: /* Caution optionfilefiname is hidden */
2042: strcpy(tmpout,optionfilefiname);
2043: strcat(tmpout,"/"); /* Add to the right */
2044: strcat(tmpout,fileres);
2045: return tmpout;
2046: }
2047:
2048: /*************** function subdirf2 ***********/
2049: char *subdirf2(char fileres[], char *preop)
2050: {
2051: /* Example subdirf2(optionfilefiname,"FB_") with optionfilefiname="texte", result="texte/FB_texte"
2052: Errors in subdirf, 2, 3 while printing tmpout is
2053: rewritten within the same printf. Workaround: many printfs */
2054: /* Caution optionfilefiname is hidden */
2055: strcpy(tmpout,optionfilefiname);
2056: strcat(tmpout,"/");
2057: strcat(tmpout,preop);
2058: strcat(tmpout,fileres);
2059: return tmpout;
2060: }
2061:
2062: /*************** function subdirf3 ***********/
2063: char *subdirf3(char fileres[], char *preop, char *preop2)
2064: {
2065:
2066: /* Caution optionfilefiname is hidden */
2067: strcpy(tmpout,optionfilefiname);
2068: strcat(tmpout,"/");
2069: strcat(tmpout,preop);
2070: strcat(tmpout,preop2);
2071: strcat(tmpout,fileres);
2072: return tmpout;
2073: }
2074:
2075: /*************** function subdirfext ***********/
2076: char *subdirfext(char fileres[], char *preop, char *postop)
2077: {
2078:
2079: strcpy(tmpout,preop);
2080: strcat(tmpout,fileres);
2081: strcat(tmpout,postop);
2082: return tmpout;
2083: }
2084:
2085: /*************** function subdirfext3 ***********/
2086: char *subdirfext3(char fileres[], char *preop, char *postop)
2087: {
2088:
2089: /* Caution optionfilefiname is hidden */
2090: strcpy(tmpout,optionfilefiname);
2091: strcat(tmpout,"/");
2092: strcat(tmpout,preop);
2093: strcat(tmpout,fileres);
2094: strcat(tmpout,postop);
2095: return tmpout;
2096: }
2097:
2098: char *asc_diff_time(long time_sec, char ascdiff[])
2099: {
2100: long sec_left, days, hours, minutes;
2101: days = (time_sec) / (60*60*24);
2102: sec_left = (time_sec) % (60*60*24);
2103: hours = (sec_left) / (60*60) ;
2104: sec_left = (sec_left) %(60*60);
2105: minutes = (sec_left) /60;
2106: sec_left = (sec_left) % (60);
2107: sprintf(ascdiff,"%ld day(s) %ld hour(s) %ld minute(s) %ld second(s)",days, hours, minutes, sec_left);
2108: return ascdiff;
2109: }
2110:
2111: /***************** f1dim *************************/
2112: extern int ncom;
2113: extern double *pcom,*xicom;
2114: extern double (*nrfunc)(double []);
2115:
2116: double f1dim(double x)
2117: {
2118: int j;
2119: double f;
2120: double *xt;
2121:
2122: xt=vector(1,ncom);
2123: for (j=1;j<=ncom;j++) xt[j]=pcom[j]+x*xicom[j];
2124: f=(*nrfunc)(xt);
2125: free_vector(xt,1,ncom);
2126: return f;
2127: }
2128:
2129: /*****************brent *************************/
2130: double brent(double ax, double bx, double cx, double (*f)(double), double tol, double *xmin)
2131: {
2132: /* Given a function f, and given a bracketing triplet of abscissas ax, bx, cx (such that bx is
2133: * between ax and cx, and f(bx) is less than both f(ax) and f(cx) ), this routine isolates
2134: * the minimum to a fractional precision of about tol using Brent’s method. The abscissa of
2135: * the minimum is returned as xmin, and the minimum function value is returned as brent , the
2136: * returned function value.
2137: */
2138: int iter;
2139: double a,b,d,etemp;
2140: double fu=0,fv,fw,fx;
2141: double ftemp=0.;
2142: double p,q,r,tol1,tol2,u,v,w,x,xm;
2143: double e=0.0;
2144:
2145: a=(ax < cx ? ax : cx);
2146: b=(ax > cx ? ax : cx);
2147: x=w=v=bx;
2148: fw=fv=fx=(*f)(x);
2149: for (iter=1;iter<=ITMAX;iter++) {
2150: xm=0.5*(a+b);
2151: tol2=2.0*(tol1=tol*fabs(x)+ZEPS);
2152: /* if (2.0*fabs(fp-(*fret)) <= ftol*(fabs(fp)+fabs(*fret)))*/
2153: printf(".");fflush(stdout);
2154: fprintf(ficlog,".");fflush(ficlog);
2155: #ifdef DEBUGBRENT
2156: 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);
2157: 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);
2158: /* if ((fabs(x-xm) <= (tol2-0.5*(b-a)))||(2.0*fabs(fu-ftemp) <= ftol*1.e-2*(fabs(fu)+fabs(ftemp)))) { */
2159: #endif
2160: if (fabs(x-xm) <= (tol2-0.5*(b-a))){
2161: *xmin=x;
2162: return fx;
2163: }
2164: ftemp=fu;
2165: if (fabs(e) > tol1) {
2166: r=(x-w)*(fx-fv);
2167: q=(x-v)*(fx-fw);
2168: p=(x-v)*q-(x-w)*r;
2169: q=2.0*(q-r);
2170: if (q > 0.0) p = -p;
2171: q=fabs(q);
2172: etemp=e;
2173: e=d;
2174: if (fabs(p) >= fabs(0.5*q*etemp) || p <= q*(a-x) || p >= q*(b-x))
2175: d=CGOLD*(e=(x >= xm ? a-x : b-x));
2176: else {
2177: d=p/q;
2178: u=x+d;
2179: if (u-a < tol2 || b-u < tol2)
2180: d=SIGN(tol1,xm-x);
2181: }
2182: } else {
2183: d=CGOLD*(e=(x >= xm ? a-x : b-x));
2184: }
2185: u=(fabs(d) >= tol1 ? x+d : x+SIGN(tol1,d));
2186: fu=(*f)(u);
2187: if (fu <= fx) {
2188: if (u >= x) a=x; else b=x;
2189: SHFT(v,w,x,u)
2190: SHFT(fv,fw,fx,fu)
2191: } else {
2192: if (u < x) a=u; else b=u;
2193: if (fu <= fw || w == x) {
2194: v=w;
2195: w=u;
2196: fv=fw;
2197: fw=fu;
2198: } else if (fu <= fv || v == x || v == w) {
2199: v=u;
2200: fv=fu;
2201: }
2202: }
2203: }
2204: nrerror("Too many iterations in brent");
2205: *xmin=x;
2206: return fx;
2207: }
2208:
2209: /****************** mnbrak ***********************/
2210:
2211: void mnbrak(double *ax, double *bx, double *cx, double *fa, double *fb, double *fc,
2212: double (*func)(double))
2213: { /* Given a function func , and given distinct initial points ax and bx , this routine searches in
2214: the downhill direction (defined by the function as evaluated at the initial points) and returns
2215: new points ax , bx , cx that bracket a minimum of the function. Also returned are the function
2216: values at the three points, fa, fb , and fc such that fa > fb and fb < fc.
2217: */
2218: double ulim,u,r,q, dum;
2219: double fu;
2220:
2221: double scale=10.;
2222: int iterscale=0;
2223:
2224: *fa=(*func)(*ax); /* xta[j]=pcom[j]+(*ax)*xicom[j]; fa=f(xta[j])*/
2225: *fb=(*func)(*bx); /* xtb[j]=pcom[j]+(*bx)*xicom[j]; fb=f(xtb[j]) */
2226:
2227:
2228: /* while(*fb != *fb){ /\* *ax should be ok, reducing distance to *ax *\/ */
2229: /* printf("Warning mnbrak *fb = %lf, *bx=%lf *ax=%lf *fa==%lf iter=%d\n",*fb, *bx, *ax, *fa, iterscale++); */
2230: /* *bx = *ax - (*ax - *bx)/scale; */
2231: /* *fb=(*func)(*bx); /\* xtb[j]=pcom[j]+(*bx)*xicom[j]; fb=f(xtb[j]) *\/ */
2232: /* } */
2233:
2234: if (*fb > *fa) {
2235: SHFT(dum,*ax,*bx,dum)
2236: SHFT(dum,*fb,*fa,dum)
2237: }
2238: *cx=(*bx)+GOLD*(*bx-*ax);
2239: *fc=(*func)(*cx);
2240: #ifdef DEBUG
2241: printf("mnbrak0 a=%lf *fa=%lf, b=%lf *fb=%lf, c=%lf *fc=%lf\n",*ax,*fa,*bx,*fb,*cx, *fc);
2242: fprintf(ficlog,"mnbrak0 a=%lf *fa=%lf, b=%lf *fb=%lf, c=%lf *fc=%lf\n",*ax,*fa,*bx,*fb,*cx, *fc);
2243: #endif
2244: while (*fb > *fc) { /* Declining a,b,c with fa> fb > fc. If fc=inf it exits and if flat fb=fc it exits too.*/
2245: r=(*bx-*ax)*(*fb-*fc);
2246: q=(*bx-*cx)*(*fb-*fa); /* What if fa=inf */
2247: u=(*bx)-((*bx-*cx)*q-(*bx-*ax)*r)/
2248: (2.0*SIGN(FMAX(fabs(q-r),TINY),q-r)); /* Minimum abscissa of a parabolic estimated from (a,fa), (b,fb) and (c,fc). */
2249: ulim=(*bx)+GLIMIT*(*cx-*bx); /* Maximum abscissa where function should be evaluated */
2250: if ((*bx-u)*(u-*cx) > 0.0) { /* if u_p is between b and c */
2251: fu=(*func)(u);
2252: #ifdef DEBUG
2253: /* f(x)=A(x-u)**2+f(u) */
2254: double A, fparabu;
2255: A= (*fb - *fa)/(*bx-*ax)/(*bx+*ax-2*u);
2256: fparabu= *fa - A*(*ax-u)*(*ax-u);
2257: 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);
2258: 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);
2259: /* And thus,it can be that fu > *fc even if fparabu < *fc */
2260: /* mnbrak (*ax=7.666299858533, *fa=299039.693133272231), (*bx=8.595447774979, *fb=298976.598289369489),
2261: (*cx=10.098840694817, *fc=298946.631474258087), (*u=9.852501168332, fu=298948.773013752128, fparabu=298945.434711494134) */
2262: /* In that case, there is no bracket in the output! Routine is wrong with many consequences.*/
2263: #endif
2264: #ifdef MNBRAKORIGINAL
2265: #else
2266: /* if (fu > *fc) { */
2267: /* #ifdef DEBUG */
2268: /* printf("mnbrak4 fu > fc \n"); */
2269: /* fprintf(ficlog, "mnbrak4 fu > fc\n"); */
2270: /* #endif */
2271: /* /\* 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 *\\/ *\/ */
2272: /* /\* SHFT(*fa,*fc,fu,*fc) /\\* (b, u, c) is a bracket while test fb > fc will be fu > fc will exit *\\/ *\/ */
2273: /* dum=u; /\* Shifting c and u *\/ */
2274: /* u = *cx; */
2275: /* *cx = dum; */
2276: /* dum = fu; */
2277: /* fu = *fc; */
2278: /* *fc =dum; */
2279: /* } else { /\* end *\/ */
2280: /* #ifdef DEBUG */
2281: /* printf("mnbrak3 fu < fc \n"); */
2282: /* fprintf(ficlog, "mnbrak3 fu < fc\n"); */
2283: /* #endif */
2284: /* dum=u; /\* Shifting c and u *\/ */
2285: /* u = *cx; */
2286: /* *cx = dum; */
2287: /* dum = fu; */
2288: /* fu = *fc; */
2289: /* *fc =dum; */
2290: /* } */
2291: #ifdef DEBUGMNBRAK
2292: double A, fparabu;
2293: A= (*fb - *fa)/(*bx-*ax)/(*bx+*ax-2*u);
2294: fparabu= *fa - A*(*ax-u)*(*ax-u);
2295: 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);
2296: 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);
2297: #endif
2298: dum=u; /* Shifting c and u */
2299: u = *cx;
2300: *cx = dum;
2301: dum = fu;
2302: fu = *fc;
2303: *fc =dum;
2304: #endif
2305: } else if ((*cx-u)*(u-ulim) > 0.0) { /* u is after c but before ulim */
2306: #ifdef DEBUG
2307: printf("\nmnbrak2 u=%lf after c=%lf but before ulim\n",u,*cx);
2308: fprintf(ficlog,"\nmnbrak2 u=%lf after c=%lf but before ulim\n",u,*cx);
2309: #endif
2310: fu=(*func)(u);
2311: if (fu < *fc) {
2312: #ifdef DEBUG
2313: printf("\nmnbrak2 u=%lf after c=%lf but before ulim=%lf AND fu=%lf < %lf=fc\n",u,*cx,ulim,fu, *fc);
2314: fprintf(ficlog,"\nmnbrak2 u=%lf after c=%lf but before ulim=%lf AND fu=%lf < %lf=fc\n",u,*cx,ulim,fu, *fc);
2315: #endif
2316: SHFT(*bx,*cx,u,*cx+GOLD*(*cx-*bx))
2317: SHFT(*fb,*fc,fu,(*func)(u))
2318: #ifdef DEBUG
2319: printf("\nmnbrak2 shift GOLD c=%lf",*cx+GOLD*(*cx-*bx));
2320: #endif
2321: }
2322: } else if ((u-ulim)*(ulim-*cx) >= 0.0) { /* u outside ulim (verifying that ulim is beyond c) */
2323: #ifdef DEBUG
2324: printf("\nmnbrak2 u=%lf outside ulim=%lf (verifying that ulim is beyond c=%lf)\n",u,ulim,*cx);
2325: fprintf(ficlog,"\nmnbrak2 u=%lf outside ulim=%lf (verifying that ulim is beyond c=%lf)\n",u,ulim,*cx);
2326: #endif
2327: u=ulim;
2328: fu=(*func)(u);
2329: } else { /* u could be left to b (if r > q parabola has a maximum) */
2330: #ifdef DEBUG
2331: printf("\nmnbrak2 u=%lf could be left to b=%lf (if r=%lf > q=%lf parabola has a maximum)\n",u,*bx,r,q);
2332: 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);
2333: #endif
2334: u=(*cx)+GOLD*(*cx-*bx);
2335: fu=(*func)(u);
2336: #ifdef DEBUG
2337: printf("\nmnbrak2 new u=%lf fu=%lf shifted gold left from c=%lf and b=%lf \n",u,fu,*cx,*bx);
2338: fprintf(ficlog,"\nmnbrak2 new u=%lf fu=%lf shifted gold left from c=%lf and b=%lf \n",u,fu,*cx,*bx);
2339: #endif
2340: } /* end tests */
2341: SHFT(*ax,*bx,*cx,u)
2342: SHFT(*fa,*fb,*fc,fu)
2343: #ifdef DEBUG
2344: printf("\nmnbrak2 shift (*ax=%.12f, *fa=%.12lf), (*bx=%.12f, *fb=%.12lf), (*cx=%.12f, *fc=%.12lf)\n",*ax,*fa,*bx,*fb,*cx,*fc);
2345: fprintf(ficlog, "\nmnbrak2 shift (*ax=%.12f, *fa=%.12lf), (*bx=%.12f, *fb=%.12lf), (*cx=%.12f, *fc=%.12lf)\n",*ax,*fa,*bx,*fb,*cx,*fc);
2346: #endif
2347: } /* end while; ie return (a, b, c, fa, fb, fc) such that a < b < c with f(a) > f(b) and fb < f(c) */
2348: }
2349:
2350: /*************** linmin ************************/
2351: /* Given an n -dimensional point p[1..n] and an n -dimensional direction xi[1..n] , moves and
2352: resets p to where the function func(p) takes on a minimum along the direction xi from p ,
2353: and replaces xi by the actual vector displacement that p was moved. Also returns as fret
2354: the value of func at the returned location p . This is actually all accomplished by calling the
2355: routines mnbrak and brent .*/
2356: int ncom;
2357: double *pcom,*xicom;
2358: double (*nrfunc)(double []);
2359:
2360: #ifdef LINMINORIGINAL
2361: void linmin(double p[], double xi[], int n, double *fret,double (*func)(double []))
2362: #else
2363: void linmin(double p[], double xi[], int n, double *fret,double (*func)(double []), int *flat)
2364: #endif
2365: {
2366: double brent(double ax, double bx, double cx,
2367: double (*f)(double), double tol, double *xmin);
2368: double f1dim(double x);
2369: void mnbrak(double *ax, double *bx, double *cx, double *fa, double *fb,
2370: double *fc, double (*func)(double));
2371: int j;
2372: double xx,xmin,bx,ax;
2373: double fx,fb,fa;
2374:
2375: #ifdef LINMINORIGINAL
2376: #else
2377: double scale=10., axs, xxs; /* Scale added for infinity */
2378: #endif
2379:
2380: ncom=n;
2381: pcom=vector(1,n);
2382: xicom=vector(1,n);
2383: nrfunc=func;
2384: for (j=1;j<=n;j++) {
2385: pcom[j]=p[j];
2386: xicom[j]=xi[j]; /* Former scale xi[j] of currrent direction i */
2387: }
2388:
2389: #ifdef LINMINORIGINAL
2390: xx=1.;
2391: #else
2392: axs=0.0;
2393: xxs=1.;
2394: do{
2395: xx= xxs;
2396: #endif
2397: ax=0.;
2398: mnbrak(&ax,&xx,&bx,&fa,&fx,&fb,f1dim); /* Outputs: xtx[j]=pcom[j]+(*xx)*xicom[j]; fx=f(xtx[j]) */
2399: /* brackets with inputs ax=0 and xx=1, but points, pcom=p, and directions values, xicom=xi, are sent via f1dim(x) */
2400: /* 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)) */
2401: /* Outputs: fa=f(p(j)) and fx=f(p(j) + xxs * xi(j) ) and f(bx)= f(p(j)+ bx* xi(j)) */
2402: /* Given input ax=axs and xx=xxs, xx might be too far from ax to get a finite f(xx) */
2403: /* Searches on line, outputs (ax, xx, bx) such that fx < min(fa and fb) */
2404: /* 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]]*/
2405: #ifdef LINMINORIGINAL
2406: #else
2407: if (fx != fx){
2408: xxs=xxs/scale; /* Trying a smaller xx, closer to initial ax=0 */
2409: printf("|");
2410: fprintf(ficlog,"|");
2411: #ifdef DEBUGLINMIN
2412: 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);
2413: #endif
2414: }
2415: }while(fx != fx && xxs > 1.e-5);
2416: #endif
2417:
2418: #ifdef DEBUGLINMIN
2419: 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);
2420: 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);
2421: #endif
2422: #ifdef LINMINORIGINAL
2423: #else
2424: if(fb == fx){ /* Flat function in the direction */
2425: xmin=xx;
2426: *flat=1;
2427: }else{
2428: *flat=0;
2429: #endif
2430: /*Flat mnbrak2 shift (*ax=0.000000000000, *fa=51626.272983130431), (*bx=-1.618034000000, *fb=51590.149499362531), (*cx=-4.236068025156, *fc=51590.149499362531) */
2431: *fret=brent(ax,xx,bx,f1dim,TOL,&xmin); /* Giving a bracketting triplet (ax, xx, bx), find a minimum, xmin, according to f1dim, *fret(xmin),*/
2432: /* fa = f(p[j] + ax * xi[j]), fx = f(p[j] + xx * xi[j]), fb = f(p[j] + bx * xi[j]) */
2433: /* fmin = f(p[j] + xmin * xi[j]) */
2434: /* P+lambda n in that direction (lambdamin), with TOL between abscisses */
2435: /* f1dim(xmin): for (j=1;j<=ncom;j++) xt[j]=pcom[j]+xmin*xicom[j]; */
2436: #ifdef DEBUG
2437: 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);
2438: 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);
2439: #endif
2440: #ifdef LINMINORIGINAL
2441: #else
2442: }
2443: #endif
2444: #ifdef DEBUGLINMIN
2445: printf("linmin end ");
2446: fprintf(ficlog,"linmin end ");
2447: #endif
2448: for (j=1;j<=n;j++) {
2449: #ifdef LINMINORIGINAL
2450: xi[j] *= xmin;
2451: #else
2452: #ifdef DEBUGLINMIN
2453: if(xxs <1.0)
2454: printf(" before xi[%d]=%12.8f", j,xi[j]);
2455: #endif
2456: 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) */
2457: #ifdef DEBUGLINMIN
2458: if(xxs <1.0)
2459: 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 );
2460: #endif
2461: #endif
2462: p[j] += xi[j]; /* Parameters values are updated accordingly */
2463: }
2464: #ifdef DEBUGLINMIN
2465: printf("\n");
2466: printf("Comparing last *frec(xmin=%12.8f)=%12.8f from Brent and frec(0.)=%12.8f \n", xmin, *fret, (*func)(p));
2467: fprintf(ficlog,"Comparing last *frec(xmin=%12.8f)=%12.8f from Brent and frec(0.)=%12.8f \n", xmin, *fret, (*func)(p));
2468: for (j=1;j<=n;j++) {
2469: printf(" xi[%d]= %14.10f p[%d]= %12.7f",j,xi[j],j,p[j]);
2470: fprintf(ficlog," xi[%d]= %14.10f p[%d]= %12.7f",j,xi[j],j,p[j]);
2471: if(j % ncovmodel == 0){
2472: printf("\n");
2473: fprintf(ficlog,"\n");
2474: }
2475: }
2476: #else
2477: #endif
2478: free_vector(xicom,1,n);
2479: free_vector(pcom,1,n);
2480: }
2481:
2482:
2483: /*************** powell ************************/
2484: /*
2485: Minimization of a function func of n variables. Input consists in an initial starting point
2486: p[1..n] ; an initial matrix xi[1..n][1..n] whose columns contain the initial set of di-
2487: rections (usually the n unit vectors); and ftol, the fractional tolerance in the function value
2488: such that failure to decrease by more than this amount in one iteration signals doneness. On
2489: output, p is set to the best point found, xi is the then-current direction set, fret is the returned
2490: function value at p , and iter is the number of iterations taken. The routine linmin is used.
2491: */
2492: #ifdef LINMINORIGINAL
2493: #else
2494: int *flatdir; /* Function is vanishing in that direction */
2495: int flat=0, flatd=0; /* Function is vanishing in that direction */
2496: #endif
2497: void powell(double p[], double **xi, int n, double ftol, int *iter, double *fret,
2498: double (*func)(double []))
2499: {
2500: #ifdef LINMINORIGINAL
2501: void linmin(double p[], double xi[], int n, double *fret,
2502: double (*func)(double []));
2503: #else
2504: void linmin(double p[], double xi[], int n, double *fret,
2505: double (*func)(double []),int *flat);
2506: #endif
2507: int i,ibig,j,jk,k;
2508: double del,t,*pt,*ptt,*xit;
2509: double directest;
2510: double fp,fptt;
2511: double *xits;
2512: int niterf, itmp;
2513:
2514: pt=vector(1,n);
2515: ptt=vector(1,n);
2516: xit=vector(1,n);
2517: xits=vector(1,n);
2518: *fret=(*func)(p);
2519: for (j=1;j<=n;j++) pt[j]=p[j];
2520: rcurr_time = time(NULL);
2521: fp=(*fret); /* Initialisation */
2522: for (*iter=1;;++(*iter)) {
2523: ibig=0;
2524: del=0.0;
2525: rlast_time=rcurr_time;
2526: /* (void) gettimeofday(&curr_time,&tzp); */
2527: rcurr_time = time(NULL);
2528: curr_time = *localtime(&rcurr_time);
2529: /* printf("\nPowell iter=%d -2*LL=%.12f gain=%.12f=%.3g %ld sec. %ld sec.",*iter,*fret, fp-*fret,fp-*fret, rcurr_time-rlast_time, rcurr_time-rstart_time);fflush(stdout); */
2530: /* fprintf(ficlog,"\nPowell iter=%d -2*LL=%.12f gain=%.12f=%.3g %ld sec. %ld sec.",*iter,*fret, fp-*fret,fp-*fret,rcurr_time-rlast_time, rcurr_time-rstart_time); fflush(ficlog); */
2531: printf("\nPowell iter=%d -2*LL=%.12f gain=%.3lg %ld sec. %ld sec.",*iter,*fret,fp-*fret, rcurr_time-rlast_time, rcurr_time-rstart_time);fflush(stdout);
2532: fprintf(ficlog,"\nPowell iter=%d -2*LL=%.12f gain=%.3lg %ld sec. %ld sec.",*iter,*fret,fp-*fret,rcurr_time-rlast_time, rcurr_time-rstart_time); fflush(ficlog);
2533: /* fprintf(ficrespow,"%d %.12f %ld",*iter,*fret,curr_time.tm_sec-start_time.tm_sec); */
2534: fp=(*fret); /* From former iteration or initial value */
2535: for (i=1;i<=n;i++) {
2536: fprintf(ficrespow," %.12lf", p[i]);
2537: }
2538: fprintf(ficrespow,"\n");fflush(ficrespow);
2539: printf("\n#model= 1 + age ");
2540: fprintf(ficlog,"\n#model= 1 + age ");
2541: if(nagesqr==1){
2542: printf(" + age*age ");
2543: fprintf(ficlog," + age*age ");
2544: }
2545: for(j=1;j <=ncovmodel-2;j++){
2546: if(Typevar[j]==0) {
2547: printf(" + V%d ",Tvar[j]);
2548: fprintf(ficlog," + V%d ",Tvar[j]);
2549: }else if(Typevar[j]==1) {
2550: printf(" + V%d*age ",Tvar[j]);
2551: fprintf(ficlog," + V%d*age ",Tvar[j]);
2552: }else if(Typevar[j]==2) {
2553: printf(" + V%d*V%d ",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
2554: fprintf(ficlog," + V%d*V%d ",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
2555: }
2556: }
2557: printf("\n");
2558: /* printf("12 47.0114589 0.0154322 33.2424412 0.3279905 2.3731903 */
2559: /* 13 -21.5392400 0.1118147 1.2680506 1.2973408 -1.0663662 */
2560: fprintf(ficlog,"\n");
2561: for(i=1,jk=1; i <=nlstate; i++){
2562: for(k=1; k <=(nlstate+ndeath); k++){
2563: if (k != i) {
2564: printf("%d%d ",i,k);
2565: fprintf(ficlog,"%d%d ",i,k);
2566: for(j=1; j <=ncovmodel; j++){
2567: printf("%12.7f ",p[jk]);
2568: fprintf(ficlog,"%12.7f ",p[jk]);
2569: jk++;
2570: }
2571: printf("\n");
2572: fprintf(ficlog,"\n");
2573: }
2574: }
2575: }
2576: if(*iter <=3 && *iter >1){
2577: tml = *localtime(&rcurr_time);
2578: strcpy(strcurr,asctime(&tml));
2579: rforecast_time=rcurr_time;
2580: itmp = strlen(strcurr);
2581: if(strcurr[itmp-1]=='\n') /* Windows outputs with a new line */
2582: strcurr[itmp-1]='\0';
2583: printf("\nConsidering the time needed for the last iteration #%d: %ld seconds,\n",*iter,rcurr_time-rlast_time);
2584: fprintf(ficlog,"\nConsidering the time needed for this last iteration #%d: %ld seconds,\n",*iter,rcurr_time-rlast_time);
2585: for(niterf=10;niterf<=30;niterf+=10){
2586: rforecast_time=rcurr_time+(niterf-*iter)*(rcurr_time-rlast_time);
2587: forecast_time = *localtime(&rforecast_time);
2588: strcpy(strfor,asctime(&forecast_time));
2589: itmp = strlen(strfor);
2590: if(strfor[itmp-1]=='\n')
2591: strfor[itmp-1]='\0';
2592: 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);
2593: 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);
2594: }
2595: }
2596: for (i=1;i<=n;i++) { /* For each direction i */
2597: for (j=1;j<=n;j++) xit[j]=xi[j][i]; /* Directions stored from previous iteration with previous scales */
2598: fptt=(*fret);
2599: #ifdef DEBUG
2600: printf("fret=%lf, %lf, %lf \n", *fret, *fret, *fret);
2601: fprintf(ficlog, "fret=%lf, %lf, %lf \n", *fret, *fret, *fret);
2602: #endif
2603: printf("%d",i);fflush(stdout); /* print direction (parameter) i */
2604: fprintf(ficlog,"%d",i);fflush(ficlog);
2605: #ifdef LINMINORIGINAL
2606: linmin(p,xit,n,fret,func); /* Point p[n]. xit[n] has been loaded for direction i as input.*/
2607: #else
2608: linmin(p,xit,n,fret,func,&flat); /* Point p[n]. xit[n] has been loaded for direction i as input.*/
2609: flatdir[i]=flat; /* Function is vanishing in that direction i */
2610: #endif
2611: /* Outputs are fret(new point p) p is updated and xit rescaled */
2612: if (fabs(fptt-(*fret)) > del) { /* We are keeping the max gain on each of the n directions */
2613: /* because that direction will be replaced unless the gain del is small */
2614: /* in comparison with the 'probable' gain, mu^2, with the last average direction. */
2615: /* Unless the n directions are conjugate some gain in the determinant may be obtained */
2616: /* with the new direction. */
2617: del=fabs(fptt-(*fret));
2618: ibig=i;
2619: }
2620: #ifdef DEBUG
2621: printf("%d %.12e",i,(*fret));
2622: fprintf(ficlog,"%d %.12e",i,(*fret));
2623: for (j=1;j<=n;j++) {
2624: xits[j]=FMAX(fabs(p[j]-pt[j]),1.e-5);
2625: printf(" x(%d)=%.12e",j,xit[j]);
2626: fprintf(ficlog," x(%d)=%.12e",j,xit[j]);
2627: }
2628: for(j=1;j<=n;j++) {
2629: printf(" p(%d)=%.12e",j,p[j]);
2630: fprintf(ficlog," p(%d)=%.12e",j,p[j]);
2631: }
2632: printf("\n");
2633: fprintf(ficlog,"\n");
2634: #endif
2635: } /* end loop on each direction i */
2636: /* Convergence test will use last linmin estimation (fret) and compare former iteration (fp) */
2637: /* But p and xit have been updated at the end of linmin, *fret corresponds to new p, xit */
2638: /* New value of last point Pn is not computed, P(n-1) */
2639: for(j=1;j<=n;j++) {
2640: if(flatdir[j] >0){
2641: printf(" p(%d)=%lf flat=%d ",j,p[j],flatdir[j]);
2642: fprintf(ficlog," p(%d)=%lf flat=%d ",j,p[j],flatdir[j]);
2643: }
2644: /* printf("\n"); */
2645: /* fprintf(ficlog,"\n"); */
2646: }
2647: /* if (2.0*fabs(fp-(*fret)) <= ftol*(fabs(fp)+fabs(*fret))) { /\* Did we reach enough precision? *\/ */
2648: if (2.0*fabs(fp-(*fret)) <= ftol) { /* Did we reach enough precision? */
2649: /* We could compare with a chi^2. chisquare(0.95,ddl=1)=3.84 */
2650: /* By adding age*age in a model, the new -2LL should be lower and the difference follows a */
2651: /* a chisquare statistics with 1 degree. To be significant at the 95% level, it should have */
2652: /* decreased of more than 3.84 */
2653: /* By adding age*age and V1*age the gain (-2LL) should be more than 5.99 (ddl=2) */
2654: /* By using V1+V2+V3, the gain should be 7.82, compared with basic 1+age. */
2655: /* By adding 10 parameters more the gain should be 18.31 */
2656:
2657: /* Starting the program with initial values given by a former maximization will simply change */
2658: /* the scales of the directions and the directions, because the are reset to canonical directions */
2659: /* Thus the first calls to linmin will give new points and better maximizations until fp-(*fret) is */
2660: /* under the tolerance value. If the tolerance is very small 1.e-9, it could last long. */
2661: #ifdef DEBUG
2662: int k[2],l;
2663: k[0]=1;
2664: k[1]=-1;
2665: printf("Max: %.12e",(*func)(p));
2666: fprintf(ficlog,"Max: %.12e",(*func)(p));
2667: for (j=1;j<=n;j++) {
2668: printf(" %.12e",p[j]);
2669: fprintf(ficlog," %.12e",p[j]);
2670: }
2671: printf("\n");
2672: fprintf(ficlog,"\n");
2673: for(l=0;l<=1;l++) {
2674: for (j=1;j<=n;j++) {
2675: ptt[j]=p[j]+(p[j]-pt[j])*k[l];
2676: printf("l=%d j=%d ptt=%.12e, xits=%.12e, p=%.12e, xit=%.12e", l,j,ptt[j],xits[j],p[j],xit[j]);
2677: fprintf(ficlog,"l=%d j=%d ptt=%.12e, xits=%.12e, p=%.12e, xit=%.12e", l,j,ptt[j],xits[j],p[j],xit[j]);
2678: }
2679: printf("func(ptt)=%.12e, deriv=%.12e\n",(*func)(ptt),(ptt[j]-p[j])/((*func)(ptt)-(*func)(p)));
2680: fprintf(ficlog,"func(ptt)=%.12e, deriv=%.12e\n",(*func)(ptt),(ptt[j]-p[j])/((*func)(ptt)-(*func)(p)));
2681: }
2682: #endif
2683:
2684: free_vector(xit,1,n);
2685: free_vector(xits,1,n);
2686: free_vector(ptt,1,n);
2687: free_vector(pt,1,n);
2688: return;
2689: } /* enough precision */
2690: if (*iter == ITMAX*n) nrerror("powell exceeding maximum iterations.");
2691: for (j=1;j<=n;j++) { /* Computes the extrapolated point P_0 + 2 (P_n-P_0) */
2692: ptt[j]=2.0*p[j]-pt[j];
2693: xit[j]=p[j]-pt[j];
2694: pt[j]=p[j];
2695: }
2696: fptt=(*func)(ptt); /* f_3 */
2697: #ifdef NODIRECTIONCHANGEDUNTILNITER /* No change in drections until some iterations are done */
2698: if (*iter <=4) {
2699: #else
2700: #endif
2701: #ifdef POWELLNOF3INFF1TEST /* skips test F3 <F1 */
2702: #else
2703: if (fptt < fp) { /* If extrapolated point is better, decide if we keep that new direction or not */
2704: #endif
2705: /* (x1 f1=fp), (x2 f2=*fret), (x3 f3=fptt), (xm fm) */
2706: /* From x1 (P0) distance of x2 is at h and x3 is 2h */
2707: /* Let f"(x2) be the 2nd derivative equal everywhere. */
2708: /* Then the parabolic through (x1,f1), (x2,f2) and (x3,f3) */
2709: /* will reach at f3 = fm + h^2/2 f"m ; f" = (f1 -2f2 +f3 ) / h**2 */
2710: /* Conditional for using this new direction is that mu^2 = (f1-2f2+f3)^2 /2 < del or directest <0 */
2711: /* also lamda^2=(f1-f2)^2/mu² is a parasite solution of powell */
2712: /* For powell, inclusion of this average direction is only if t(del)<0 or del inbetween mu^2 and lambda^2 */
2713: /* t=2.0*(fp-2.0*(*fret)+fptt)*SQR(fp-(*fret)-del)-del*SQR(fp-fptt); */
2714: /* Even if f3 <f1, directest can be negative and t >0 */
2715: /* mu² and del² are equal when f3=f1 */
2716: /* f3 < f1 : mu² < del <= lambda^2 both test are equivalent */
2717: /* f3 < f1 : mu² < lambda^2 < del then directtest is negative and powell t is positive */
2718: /* f3 > f1 : lambda² < mu^2 < del then t is negative and directest >0 */
2719: /* f3 > f1 : lambda² < del < mu^2 then t is positive and directest >0 */
2720: #ifdef NRCORIGINAL
2721: t=2.0*(fp-2.0*(*fret)+fptt)*SQR(fp-(*fret)-del)- del*SQR(fp-fptt); /* Original Numerical Recipes in C*/
2722: #else
2723: t=2.0*(fp-2.0*(*fret)+fptt)*SQR(fp-(*fret)-del); /* Intel compiler doesn't work on one line; bug reported */
2724: t= t- del*SQR(fp-fptt);
2725: #endif
2726: directest = fp-2.0*(*fret)+fptt - 2.0 * del; /* If delta was big enough we change it for a new direction */
2727: #ifdef DEBUG
2728: 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);
2729: 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);
2730: printf("t3= %.12lf, t4= %.12lf, t3*= %.12lf, t4*= %.12lf\n",SQR(fp-(*fret)-del),SQR(fp-fptt),
2731: (fp-(*fret)-del)*(fp-(*fret)-del),(fp-fptt)*(fp-fptt));
2732: fprintf(ficlog,"t3= %.12lf, t4= %.12lf, t3*= %.12lf, t4*= %.12lf\n",SQR(fp-(*fret)-del),SQR(fp-fptt),
2733: (fp-(*fret)-del)*(fp-(*fret)-del),(fp-fptt)*(fp-fptt));
2734: 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);
2735: 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);
2736: #endif
2737: #ifdef POWELLORIGINAL
2738: if (t < 0.0) { /* Then we use it for new direction */
2739: #else
2740: if (directest*t < 0.0) { /* Contradiction between both tests */
2741: 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);
2742: printf("f1-2f2+f3= %.12lf, f1-f2-del= %.12lf, f1-f3= %.12lf\n",fp-2.0*(*fret)+fptt, fp -(*fret) -del, fp-fptt);
2743: 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);
2744: fprintf(ficlog,"f1-2f2+f3= %.12lf, f1-f2-del= %.12lf, f1-f3= %.12lf\n",fp-2.0*(*fret)+fptt, fp -(*fret) -del, fp-fptt);
2745: }
2746: if (directest < 0.0) { /* Then we use it for new direction */
2747: #endif
2748: #ifdef DEBUGLINMIN
2749: printf("Before linmin in direction P%d-P0\n",n);
2750: for (j=1;j<=n;j++) {
2751: printf(" Before xit[%d]= %12.7f p[%d]= %12.7f",j,xit[j],j,p[j]);
2752: fprintf(ficlog," Before xit[%d]= %12.7f p[%d]= %12.7f",j,xit[j],j,p[j]);
2753: if(j % ncovmodel == 0){
2754: printf("\n");
2755: fprintf(ficlog,"\n");
2756: }
2757: }
2758: #endif
2759: #ifdef LINMINORIGINAL
2760: linmin(p,xit,n,fret,func); /* computes minimum on the extrapolated direction: changes p and rescales xit.*/
2761: #else
2762: linmin(p,xit,n,fret,func,&flat); /* computes minimum on the extrapolated direction: changes p and rescales xit.*/
2763: flatdir[i]=flat; /* Function is vanishing in that direction i */
2764: #endif
2765:
2766: #ifdef DEBUGLINMIN
2767: for (j=1;j<=n;j++) {
2768: printf("After xit[%d]= %12.7f p[%d]= %12.7f",j,xit[j],j,p[j]);
2769: fprintf(ficlog,"After xit[%d]= %12.7f p[%d]= %12.7f",j,xit[j],j,p[j]);
2770: if(j % ncovmodel == 0){
2771: printf("\n");
2772: fprintf(ficlog,"\n");
2773: }
2774: }
2775: #endif
2776: for (j=1;j<=n;j++) {
2777: xi[j][ibig]=xi[j][n]; /* Replace direction with biggest decrease by last direction n */
2778: xi[j][n]=xit[j]; /* and this nth direction by the by the average p_0 p_n */
2779: }
2780: #ifdef LINMINORIGINAL
2781: #else
2782: for (j=1, flatd=0;j<=n;j++) {
2783: if(flatdir[j]>0)
2784: flatd++;
2785: }
2786: if(flatd >0){
2787: printf("%d flat directions: ",flatd);
2788: fprintf(ficlog,"%d flat directions :",flatd);
2789: for (j=1;j<=n;j++) {
2790: if(flatdir[j]>0){
2791: printf("%d ",j);
2792: fprintf(ficlog,"%d ",j);
2793: }
2794: }
2795: printf("\n");
2796: fprintf(ficlog,"\n");
2797: #ifdef FLATSUP
2798: free_vector(xit,1,n);
2799: free_vector(xits,1,n);
2800: free_vector(ptt,1,n);
2801: free_vector(pt,1,n);
2802: return;
2803: #endif
2804: }
2805: #endif
2806: printf("Gaining to use new average direction of P0 P%d instead of biggest increase direction %d :\n",n,ibig);
2807: fprintf(ficlog,"Gaining to use new average direction of P0 P%d instead of biggest increase direction %d :\n",n,ibig);
2808:
2809: #ifdef DEBUG
2810: printf("Direction changed last moved %d in place of ibig=%d, new last is the average:\n",n,ibig);
2811: fprintf(ficlog,"Direction changed last moved %d in place of ibig=%d, new last is the average:\n",n,ibig);
2812: for(j=1;j<=n;j++){
2813: printf(" %lf",xit[j]);
2814: fprintf(ficlog," %lf",xit[j]);
2815: }
2816: printf("\n");
2817: fprintf(ficlog,"\n");
2818: #endif
2819: } /* end of t or directest negative */
2820: #ifdef POWELLNOF3INFF1TEST
2821: #else
2822: } /* end if (fptt < fp) */
2823: #endif
2824: #ifdef NODIRECTIONCHANGEDUNTILNITER /* No change in drections until some iterations are done */
2825: } /*NODIRECTIONCHANGEDUNTILNITER No change in drections until some iterations are done */
2826: #else
2827: #endif
2828: } /* loop iteration */
2829: }
2830:
2831: /**** Prevalence limit (stable or period prevalence) ****************/
2832:
2833: double **prevalim(double **prlim, int nlstate, double x[], double age, double **oldm, double **savm, double ftolpl, int *ncvyear, int ij, int nres)
2834: {
2835: /**< Computes the prevalence limit in each live state at age x and for covariate combination ij . Nicely done
2836: * (and selected quantitative values in nres)
2837: * by left multiplying the unit
2838: * matrix by transitions matrix until convergence is reached with precision ftolpl
2839: * Wx= Wx-1 Px-1= Wx-2 Px-2 Px-1 = Wx-n Px-n ... Px-2 Px-1 I
2840: * Wx is row vector: population in state 1, population in state 2, population dead
2841: * or prevalence in state 1, prevalence in state 2, 0
2842: * newm is the matrix after multiplications, its rows are identical at a factor.
2843: * Inputs are the parameter, age, a tolerance for the prevalence limit ftolpl.
2844: * Output is prlim.
2845: * Initial matrix pimij
2846: */
2847: /* {0.85204250825084937, 0.13044499163996345, 0.017512500109187184, */
2848: /* 0.090851990222114765, 0.88271245433047185, 0.026435555447413338, */
2849: /* 0, 0 , 1} */
2850: /*
2851: * and after some iteration: */
2852: /* {0.45504275246439968, 0.42731458730878791, 0.11764266022681241, */
2853: /* 0.45201005341706885, 0.42865420071559901, 0.11933574586733192, */
2854: /* 0, 0 , 1} */
2855: /* And prevalence by suppressing the deaths are close to identical rows in prlim: */
2856: /* {0.51571254859325999, 0.4842874514067399, */
2857: /* 0.51326036147820708, 0.48673963852179264} */
2858: /* If we start from prlim again, prlim tends to a constant matrix */
2859:
2860: int i, ii,j,k, k1;
2861: double *min, *max, *meandiff, maxmax,sumnew=0.;
2862: /* double **matprod2(); */ /* test */
2863: double **out, cov[NCOVMAX+1], **pmij(); /* **pmmij is a global variable feeded with oldms etc */
2864: double **newm;
2865: double agefin, delaymax=200. ; /* 100 Max number of years to converge */
2866: int ncvloop=0;
2867: int first=0;
2868:
2869: min=vector(1,nlstate);
2870: max=vector(1,nlstate);
2871: meandiff=vector(1,nlstate);
2872:
2873: /* Starting with matrix unity */
2874: for (ii=1;ii<=nlstate+ndeath;ii++)
2875: for (j=1;j<=nlstate+ndeath;j++){
2876: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
2877: }
2878:
2879: cov[1]=1.;
2880:
2881: /* Even if hstepm = 1, at least one multiplication by the unit matrix */
2882: /* Start at agefin= age, computes the matrix of passage and loops decreasing agefin until convergence is reached */
2883: for(agefin=age-stepm/YEARM; agefin>=age-delaymax; agefin=agefin-stepm/YEARM){
2884: ncvloop++;
2885: newm=savm;
2886: /* Covariates have to be included here again */
2887: cov[2]=agefin;
2888: if(nagesqr==1){
2889: cov[3]= agefin*agefin;
2890: }
2891: /* Model(2) V1 + V2 + V3 + V8 + V7*V8 + V5*V6 + V8*age + V3*age + age*age */
2892: /* total number of covariates of the model nbocc(+)+1 = 8 excepting constant and age and age*age */
2893: for(k1=1;k1<=cptcovt;k1++){ /* loop on model equation (including products) */
2894: if(Typevar[k1]==1){ /* A product with age */
2895: cov[2+nagesqr+k1]=precov[nres][k1]*cov[2];
2896: }else{
2897: cov[2+nagesqr+k1]=precov[nres][k1];
2898: }
2899: }/* End of loop on model equation */
2900:
2901: /* Start of old code (replaced by a loop on position in the model equation */
2902: /* for (k=1; k<=nsd;k++) { /\* For single dummy covariates only of the model *\/ */
2903: /* /\* Here comes the value of the covariate 'ij' after renumbering k with single dummy covariates *\/ */
2904: /* /\* cov[2+nagesqr+TvarsDind[k]]=nbcode[TvarsD[k]][codtabm(ij,TvarsD[k])]; *\/ */
2905: /* cov[2+nagesqr+TvarsDind[k]]=nbcode[TvarsD[k]][codtabm(ij,TnsdVar[TvarsD[k]])]; */
2906: /* /\* model = 1 +age + V1*V3 + age*V1 + V2 + V1 + age*V2 + V3 + V3*age + V1*V2 */
2907: /* * k 1 2 3 4 5 6 7 8 */
2908: /* *cov[] 1 2 3 4 5 6 7 8 9 10 */
2909: /* *TypeVar[k] 2 1 0 0 1 0 1 2 */
2910: /* *Dummy[k] 0 2 0 0 2 0 2 0 */
2911: /* *Tvar[k] 4 1 2 1 2 3 3 5 */
2912: /* *nsd=3 (1) (2) (3) */
2913: /* *TvarsD[nsd] [1]=2 1 3 */
2914: /* *TnsdVar [2]=2 [1]=1 [3]=3 */
2915: /* *TvarsDind[nsd](=k) [1]=3 [2]=4 [3]=6 */
2916: /* *Tage[] [1]=1 [2]=2 [3]=3 */
2917: /* *Tvard[] [1][1]=1 [2][1]=1 */
2918: /* * [1][2]=3 [2][2]=2 */
2919: /* *Tprod[](=k) [1]=1 [2]=8 */
2920: /* *TvarsDp(=Tvar) [1]=1 [2]=2 [3]=3 [4]=5 */
2921: /* *TvarD (=k) [1]=1 [2]=3 [3]=4 [3]=6 [4]=6 */
2922: /* *TvarsDpType */
2923: /* *si model= 1 + age + V3 + V2*age + V2 + V3*age */
2924: /* * nsd=1 (1) (2) */
2925: /* *TvarsD[nsd] 3 2 */
2926: /* *TnsdVar (3)=1 (2)=2 */
2927: /* *TvarsDind[nsd](=k) [1]=1 [2]=3 */
2928: /* *Tage[] [1]=2 [2]= 3 */
2929: /* *\/ */
2930: /* /\* cov[++k1]=nbcode[TvarsD[k]][codtabm(ij,k)]; *\/ */
2931: /* /\* printf("prevalim Dummy combi=%d k=%d TvarsD[%d]=V%d TvarsDind[%d]=%d nbcode=%d cov=%lf codtabm(%d,Tvar[%d])=%d \n",ij,k, k, TvarsD[k],k,TvarsDind[k],nbcode[TvarsD[k]][codtabm(ij,k)],cov[2+nagesqr+TvarsDind[k]], ij, k, codtabm(ij,k)); *\/ */
2932: /* } */
2933: /* for (k=1; k<=nsq;k++) { /\* For single quantitative varying covariates only of the model *\/ */
2934: /* /\* Here comes the value of quantitative after renumbering k with single quantitative covariates *\/ */
2935: /* /\* Tqresult[nres][result_position]= value of the variable at the result_position in the nres resultline *\/ */
2936: /* /\* cov[2+nagesqr+TvarsQind[k]]=Tqresult[nres][k]; *\/ */
2937: /* cov[2+nagesqr+TvarsQind[k]]=Tqresult[nres][resultmodel[nres][k1]] */
2938: /* /\* cov[++k1]=Tqresult[nres][k]; *\/ */
2939: /* /\* printf("prevalim Quantitative k=%d TvarsQind[%d]=%d, TvarsQ[%d]=V%d,Tqresult[%d][%d]=%f\n",k,k,TvarsQind[k],k,TvarsQ[k],nres,k,Tqresult[nres][k]); *\/ */
2940: /* } */
2941: /* for (k=1; k<=cptcovage;k++){ /\* For product with age *\/ */
2942: /* if(Dummy[Tage[k]]==2){ /\* dummy with age *\/ */
2943: /* cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,Tvar[Tage[k]])]*cov[2]; */
2944: /* /\* cov[++k1]=nbcode[Tvar[Tage[k]]][codtabm(ij,k)]*cov[2]; *\/ */
2945: /* } else if(Dummy[Tage[k]]==3){ /\* quantitative with age *\/ */
2946: /* cov[2+nagesqr+Tage[k]]=Tqresult[nres][k]; */
2947: /* /\* cov[++k1]=Tqresult[nres][k]; *\/ */
2948: /* } */
2949: /* /\* printf("prevalim Age combi=%d k=%d Tage[%d]=V%d Tqresult[%d][%d]=%f\n",ij,k,k,Tage[k],nres,k,Tqresult[nres][k]); *\/ */
2950: /* } */
2951: /* for (k=1; k<=cptcovprod;k++){ /\* For product without age *\/ */
2952: /* /\* printf("prevalim Prod ij=%d k=%d Tprod[%d]=%d Tvard[%d][1]=V%d, Tvard[%d][2]=V%d\n",ij,k,k,Tprod[k], k,Tvard[k][1], k,Tvard[k][2]); *\/ */
2953: /* if(Dummy[Tvard[k][1]]==0){ */
2954: /* if(Dummy[Tvard[k][2]]==0){ */
2955: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,Tvard[k][1])] * nbcode[Tvard[k][2]][codtabm(ij,Tvard[k][2])]; */
2956: /* /\* cov[++k1]=nbcode[Tvard[k][1]][codtabm(ij,k)] * nbcode[Tvard[k][2]][codtabm(ij,k)]; *\/ */
2957: /* }else{ */
2958: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,Tvard[k][1])] * Tqresult[nres][k]; */
2959: /* /\* cov[++k1]=nbcode[Tvard[k][1]][codtabm(ij,k)] * Tqresult[nres][k]; *\/ */
2960: /* } */
2961: /* }else{ */
2962: /* if(Dummy[Tvard[k][2]]==0){ */
2963: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][2]][codtabm(ij,Tvard[k][2])] * Tqinvresult[nres][Tvard[k][1]]; */
2964: /* /\* cov[++k1]=nbcode[Tvard[k][2]][codtabm(ij,k)] * Tqinvresult[nres][Tvard[k][1]]; *\/ */
2965: /* }else{ */
2966: /* cov[2+nagesqr+Tprod[k]]=Tqinvresult[nres][Tvard[k][1]]* Tqinvresult[nres][Tvard[k][2]]; */
2967: /* /\* cov[++k1]=Tqinvresult[nres][Tvard[k][1]]* Tqinvresult[nres][Tvard[k][2]]; *\/ */
2968: /* } */
2969: /* } */
2970: /* } /\* End product without age *\/ */
2971: /* ENd of old code */
2972: /*printf("ij=%d cptcovprod=%d tvar=%d ", ij, cptcovprod, Tvar[1]);*/
2973: /*printf("ij=%d cov[3]=%lf cov[4]=%lf \n",ij, cov[3],cov[4]);*/
2974: /*printf("ij=%d cov[3]=%lf \n",ij, cov[3]);*/
2975: /* savm=pmij(pmmij,cov,ncovmodel,x,nlstate); */
2976: /* out=matprod2(newm, pmij(pmmij,cov,ncovmodel,x,nlstate),1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm); /\* Bug Valgrind *\/ */
2977: /* age and covariate values of ij are in 'cov' */
2978: out=matprod2(newm, pmij(pmmij,cov,ncovmodel,x,nlstate),1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm); /* Bug Valgrind */
2979:
2980: savm=oldm;
2981: oldm=newm;
2982:
2983: for(j=1; j<=nlstate; j++){
2984: max[j]=0.;
2985: min[j]=1.;
2986: }
2987: for(i=1;i<=nlstate;i++){
2988: sumnew=0;
2989: for(k=1; k<=ndeath; k++) sumnew+=newm[i][nlstate+k];
2990: for(j=1; j<=nlstate; j++){
2991: prlim[i][j]= newm[i][j]/(1-sumnew);
2992: max[j]=FMAX(max[j],prlim[i][j]);
2993: min[j]=FMIN(min[j],prlim[i][j]);
2994: }
2995: }
2996:
2997: maxmax=0.;
2998: for(j=1; j<=nlstate; j++){
2999: meandiff[j]=(max[j]-min[j])/(max[j]+min[j])*2.; /* mean difference for each column */
3000: maxmax=FMAX(maxmax,meandiff[j]);
3001: /* 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); */
3002: } /* j loop */
3003: *ncvyear= (int)age- (int)agefin;
3004: /* printf("maxmax=%lf maxmin=%lf ncvloop=%d, age=%d, agefin=%d ncvyear=%d \n", maxmax, maxmin, ncvloop, (int)age, (int)agefin, *ncvyear); */
3005: if(maxmax < ftolpl){
3006: /* printf("maxmax=%lf ncvloop=%ld, age=%d, agefin=%d ncvyear=%d \n", maxmax, ncvloop, (int)age, (int)agefin, *ncvyear); */
3007: free_vector(min,1,nlstate);
3008: free_vector(max,1,nlstate);
3009: free_vector(meandiff,1,nlstate);
3010: return prlim;
3011: }
3012: } /* agefin loop */
3013: /* After some age loop it doesn't converge */
3014: if(!first){
3015: first=1;
3016: printf("Warning: the stable prevalence at age %d did not converge with the required precision (%g > ftolpl=%g) within %.d years and %d loops. Try to lower 'ftolpl'. Youngest age to start was %d=(%d-%d). Others in log file only...\n", (int)age, maxmax, ftolpl, *ncvyear, ncvloop, (int)(agefin+stepm/YEARM), (int)(age-stepm/YEARM), (int)delaymax);
3017: fprintf(ficlog, "Warning: the stable prevalence at age %d did not converge with the required precision (%g > ftolpl=%g) within %.d years and %d loops. Try to lower 'ftolpl'. Youngest age to start was %d=(%d-%d).\n", (int)age, maxmax, ftolpl, *ncvyear, ncvloop, (int)(agefin+stepm/YEARM), (int)(age-stepm/YEARM), (int)delaymax);
3018: }else if (first >=1 && first <10){
3019: fprintf(ficlog, "Warning: the stable prevalence at age %d did not converge with the required precision (%g > ftolpl=%g) within %.d years and %d loops. Try to lower 'ftolpl'. Youngest age to start was %d=(%d-%d).\n", (int)age, maxmax, ftolpl, *ncvyear, ncvloop, (int)(agefin+stepm/YEARM), (int)(age-stepm/YEARM), (int)delaymax);
3020: first++;
3021: }else if (first ==10){
3022: fprintf(ficlog, "Warning: the stable prevalence at age %d did not converge with the required precision (%g > ftolpl=%g) within %.d years and %d loops. Try to lower 'ftolpl'. Youngest age to start was %d=(%d-%d).\n", (int)age, maxmax, ftolpl, *ncvyear, ncvloop, (int)(agefin+stepm/YEARM), (int)(age-stepm/YEARM), (int)delaymax);
3023: printf("Warning: the stable prevalence dit not converge. This warning came too often, IMaCh will stop notifying, even in its log file. Look at the graphs to appreciate the non convergence.\n");
3024: fprintf(ficlog,"Warning: the stable prevalence no convergence; too many cases, giving up noticing, even in log file\n");
3025: first++;
3026: }
3027:
3028: /* 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); */
3029: free_vector(min,1,nlstate);
3030: free_vector(max,1,nlstate);
3031: free_vector(meandiff,1,nlstate);
3032:
3033: return prlim; /* should not reach here */
3034: }
3035:
3036:
3037: /**** Back Prevalence limit (stable or period prevalence) ****************/
3038:
3039: /* 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) */
3040: /* 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) */
3041: double **bprevalim(double **bprlim, double ***prevacurrent, int nlstate, double x[], double age, double ftolpl, int *ncvyear, int ij, int nres)
3042: {
3043: /* Computes the prevalence limit in each live state at age x and for covariate combination ij (<=2**cptcoveff) by left multiplying the unit
3044: matrix by transitions matrix until convergence is reached with precision ftolpl */
3045: /* Wx= Wx-1 Px-1= Wx-2 Px-2 Px-1 = Wx-n Px-n ... Px-2 Px-1 I */
3046: /* Wx is row vector: population in state 1, population in state 2, population dead */
3047: /* or prevalence in state 1, prevalence in state 2, 0 */
3048: /* newm is the matrix after multiplications, its rows are identical at a factor */
3049: /* Initial matrix pimij */
3050: /* {0.85204250825084937, 0.13044499163996345, 0.017512500109187184, */
3051: /* 0.090851990222114765, 0.88271245433047185, 0.026435555447413338, */
3052: /* 0, 0 , 1} */
3053: /*
3054: * and after some iteration: */
3055: /* {0.45504275246439968, 0.42731458730878791, 0.11764266022681241, */
3056: /* 0.45201005341706885, 0.42865420071559901, 0.11933574586733192, */
3057: /* 0, 0 , 1} */
3058: /* And prevalence by suppressing the deaths are close to identical rows in prlim: */
3059: /* {0.51571254859325999, 0.4842874514067399, */
3060: /* 0.51326036147820708, 0.48673963852179264} */
3061: /* If we start from prlim again, prlim tends to a constant matrix */
3062:
3063: int i, ii,j,k, k1;
3064: int first=0;
3065: double *min, *max, *meandiff, maxmax,sumnew=0.;
3066: /* double **matprod2(); */ /* test */
3067: double **out, cov[NCOVMAX+1], **bmij();
3068: double **newm;
3069: double **dnewm, **doldm, **dsavm; /* for use */
3070: double **oldm, **savm; /* for use */
3071:
3072: double agefin, delaymax=200. ; /* 100 Max number of years to converge */
3073: int ncvloop=0;
3074:
3075: min=vector(1,nlstate);
3076: max=vector(1,nlstate);
3077: meandiff=vector(1,nlstate);
3078:
3079: dnewm=ddnewms; doldm=ddoldms; dsavm=ddsavms;
3080: oldm=oldms; savm=savms;
3081:
3082: /* Starting with matrix unity */
3083: for (ii=1;ii<=nlstate+ndeath;ii++)
3084: for (j=1;j<=nlstate+ndeath;j++){
3085: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
3086: }
3087:
3088: cov[1]=1.;
3089:
3090: /* Even if hstepm = 1, at least one multiplication by the unit matrix */
3091: /* Start at agefin= age, computes the matrix of passage and loops decreasing agefin until convergence is reached */
3092: /* for(agefin=age+stepm/YEARM; agefin<=age+delaymax; agefin=agefin+stepm/YEARM){ /\* A changer en age *\/ */
3093: /* for(agefin=age; agefin<AGESUP; agefin=agefin+stepm/YEARM){ /\* A changer en age *\/ */
3094: for(agefin=age; agefin<FMIN(AGESUP,age+delaymax); agefin=agefin+stepm/YEARM){ /* A changer en age */
3095: ncvloop++;
3096: newm=savm; /* oldm should be kept from previous iteration or unity at start */
3097: /* newm points to the allocated table savm passed by the function it can be written, savm could be reallocated */
3098: /* Covariates have to be included here again */
3099: cov[2]=agefin;
3100: if(nagesqr==1){
3101: cov[3]= agefin*agefin;;
3102: }
3103: for(k1=1;k1<=cptcovt;k1++){ /* loop on model equation (including products) */
3104: if(Typevar[k1]==1){ /* A product with age */
3105: cov[2+nagesqr+k1]=precov[nres][k1]*cov[2];
3106: }else{
3107: cov[2+nagesqr+k1]=precov[nres][k1];
3108: }
3109: }/* End of loop on model equation */
3110:
3111: /* Old code */
3112:
3113: /* for (k=1; k<=nsd;k++) { /\* For single dummy covariates only *\/ */
3114: /* /\* Here comes the value of the covariate 'ij' after renumbering k with single dummy covariates *\/ */
3115: /* cov[2+nagesqr+TvarsDind[k]]=nbcode[TvarsD[k]][codtabm(ij,TvarsD[k])]; */
3116: /* /\* printf("bprevalim Dummy agefin=%.0f combi=%d k=%d TvarsD[%d]=V%d TvarsDind[%d]=%d nbcode=%d cov[%d]=%lf codtabm(%d,Tvar[%d])=%d \n",agefin,ij,k, k, TvarsD[k],k,TvarsDind[k],nbcode[TvarsD[k]][codtabm(ij,k)],2+nagesqr+TvarsDind[k],cov[2+nagesqr+TvarsDind[k]], ij, k, codtabm(ij,k)); *\/ */
3117: /* } */
3118: /* /\* for (k=1; k<=cptcovn;k++) { *\/ */
3119: /* /\* /\\* cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,Tvar[k])]; *\\/ *\/ */
3120: /* /\* cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,k)]; *\/ */
3121: /* /\* /\\* 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])]); *\\/ *\/ */
3122: /* /\* } *\/ */
3123: /* for (k=1; k<=nsq;k++) { /\* For single varying covariates only *\/ */
3124: /* /\* Here comes the value of quantitative after renumbering k with single quantitative covariates *\/ */
3125: /* cov[2+nagesqr+TvarsQind[k]]=Tqresult[nres][k]; */
3126: /* /\* printf("prevalim Quantitative k=%d TvarsQind[%d]=%d, TvarsQ[%d]=V%d,Tqresult[%d][%d]=%f\n",k,k,TvarsQind[k],k,TvarsQ[k],nres,k,Tqresult[nres][k]); *\/ */
3127: /* } */
3128: /* /\* for (k=1; k<=cptcovage;k++) cov[2+nagesqr+Tage[k]]=nbcode[Tvar[k]][codtabm(ij,k)]*cov[2]; *\/ */
3129: /* /\* for (k=1; k<=cptcovprod;k++) /\\* Useless *\\/ *\/ */
3130: /* /\* /\\* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,Tvard[k][1])] * nbcode[Tvard[k][2]][codtabm(ij,Tvard[k][2])]; *\\/ *\/ */
3131: /* /\* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)] * nbcode[Tvard[k][2]][codtabm(ij,k)]; *\/ */
3132: /* for (k=1; k<=cptcovage;k++){ /\* For product with age *\/ */
3133: /* /\* if(Dummy[Tvar[Tage[k]]]== 2){ /\\* dummy with age *\\/ ERROR ???*\/ */
3134: /* if(Dummy[Tage[k]]== 2){ /\* dummy with age *\/ */
3135: /* cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,Tvar[Tage[k]])]*cov[2]; */
3136: /* } else if(Dummy[Tage[k]]== 3){ /\* quantitative with age *\/ */
3137: /* cov[2+nagesqr+Tage[k]]=Tqresult[nres][k]; */
3138: /* } */
3139: /* /\* printf("prevalim Age combi=%d k=%d Tage[%d]=V%d Tqresult[%d][%d]=%f\n",ij,k,k,Tage[k],nres,k,Tqresult[nres][k]); *\/ */
3140: /* } */
3141: /* for (k=1; k<=cptcovprod;k++){ /\* For product without age *\/ */
3142: /* /\* printf("prevalim Prod ij=%d k=%d Tprod[%d]=%d Tvard[%d][1]=V%d, Tvard[%d][2]=V%d\n",ij,k,k,Tprod[k], k,Tvard[k][1], k,Tvard[k][2]); *\/ */
3143: /* if(Dummy[Tvard[k][1]]==0){ */
3144: /* if(Dummy[Tvard[k][2]]==0){ */
3145: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,Tvard[k][1])] * nbcode[Tvard[k][2]][codtabm(ij,Tvard[k][2])]; */
3146: /* }else{ */
3147: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,Tvard[k][1])] * Tqresult[nres][k]; */
3148: /* } */
3149: /* }else{ */
3150: /* if(Dummy[Tvard[k][2]]==0){ */
3151: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][2]][codtabm(ij,Tvard[k][2])] * Tqinvresult[nres][Tvard[k][1]]; */
3152: /* }else{ */
3153: /* cov[2+nagesqr+Tprod[k]]=Tqinvresult[nres][Tvard[k][1]]* Tqinvresult[nres][Tvard[k][2]]; */
3154: /* } */
3155: /* } */
3156: /* } */
3157:
3158: /*printf("ij=%d cptcovprod=%d tvar=%d ", ij, cptcovprod, Tvar[1]);*/
3159: /*printf("ij=%d cov[3]=%lf cov[4]=%lf \n",ij, cov[3],cov[4]);*/
3160: /*printf("ij=%d cov[3]=%lf \n",ij, cov[3]);*/
3161: /* savm=pmij(pmmij,cov,ncovmodel,x,nlstate); */
3162: /* out=matprod2(newm, pmij(pmmij,cov,ncovmodel,x,nlstate),1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm); /\* Bug Valgrind *\/ */
3163: /* ij should be linked to the correct index of cov */
3164: /* age and covariate values ij are in 'cov', but we need to pass
3165: * ij for the observed prevalence at age and status and covariate
3166: * number: prevacurrent[(int)agefin][ii][ij]
3167: */
3168: /* 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 *\/ */
3169: /* 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 *\/ */
3170: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, bmij(pmmij,cov,ncovmodel,x,nlstate,prevacurrent,ij)); /* Bug Valgrind */
3171: /* if((int)age == 86 || (int)age == 87){ */
3172: /* printf(" Backward prevalim age=%d agefin=%d \n", (int) age, (int) agefin); */
3173: /* for(i=1; i<=nlstate+ndeath; i++) { */
3174: /* printf("%d newm= ",i); */
3175: /* for(j=1;j<=nlstate+ndeath;j++) { */
3176: /* printf("%f ",newm[i][j]); */
3177: /* } */
3178: /* printf("oldm * "); */
3179: /* for(j=1;j<=nlstate+ndeath;j++) { */
3180: /* printf("%f ",oldm[i][j]); */
3181: /* } */
3182: /* printf(" bmmij "); */
3183: /* for(j=1;j<=nlstate+ndeath;j++) { */
3184: /* printf("%f ",pmmij[i][j]); */
3185: /* } */
3186: /* printf("\n"); */
3187: /* } */
3188: /* } */
3189: savm=oldm;
3190: oldm=newm;
3191:
3192: for(j=1; j<=nlstate; j++){
3193: max[j]=0.;
3194: min[j]=1.;
3195: }
3196: for(j=1; j<=nlstate; j++){
3197: for(i=1;i<=nlstate;i++){
3198: /* bprlim[i][j]= newm[i][j]/(1-sumnew); */
3199: bprlim[i][j]= newm[i][j];
3200: max[i]=FMAX(max[i],bprlim[i][j]); /* Max in line */
3201: min[i]=FMIN(min[i],bprlim[i][j]);
3202: }
3203: }
3204:
3205: maxmax=0.;
3206: for(i=1; i<=nlstate; i++){
3207: meandiff[i]=(max[i]-min[i])/(max[i]+min[i])*2.; /* mean difference for each column, could be nan! */
3208: maxmax=FMAX(maxmax,meandiff[i]);
3209: /* 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); */
3210: } /* i loop */
3211: *ncvyear= -( (int)age- (int)agefin);
3212: /* printf("Back maxmax=%lf ncvloop=%d, age=%d, agefin=%d ncvyear=%d \n", maxmax, ncvloop, (int)age, (int)agefin, *ncvyear); */
3213: if(maxmax < ftolpl){
3214: /* printf("OK Back maxmax=%lf ncvloop=%d, age=%d, agefin=%d ncvyear=%d \n", maxmax, ncvloop, (int)age, (int)agefin, *ncvyear); */
3215: free_vector(min,1,nlstate);
3216: free_vector(max,1,nlstate);
3217: free_vector(meandiff,1,nlstate);
3218: return bprlim;
3219: }
3220: } /* agefin loop */
3221: /* After some age loop it doesn't converge */
3222: if(!first){
3223: first=1;
3224: 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'. Others in log file only...\n\
3225: 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);
3226: }
3227: fprintf(ficlog,"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\
3228: 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);
3229: /* 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); */
3230: free_vector(min,1,nlstate);
3231: free_vector(max,1,nlstate);
3232: free_vector(meandiff,1,nlstate);
3233:
3234: return bprlim; /* should not reach here */
3235: }
3236:
3237: /*************** transition probabilities ***************/
3238:
3239: double **pmij(double **ps, double *cov, int ncovmodel, double *x, int nlstate )
3240: {
3241: /* According to parameters values stored in x and the covariate's values stored in cov,
3242: computes the probability to be observed in state j (after stepm years) being in state i by appying the
3243: model to the ncovmodel covariates (including constant and age).
3244: lnpijopii=ln(pij/pii)= aij+bij*age+cij*v1+dij*v2+... = sum_nc=1^ncovmodel xij(nc)*cov[nc]
3245: and, according on how parameters are entered, the position of the coefficient xij(nc) of the
3246: ncth covariate in the global vector x is given by the formula:
3247: j<i nc+((i-1)*(nlstate+ndeath-1)+j-1)*ncovmodel
3248: j>=i nc + ((i-1)*(nlstate+ndeath-1)+(j-2))*ncovmodel
3249: Computes ln(pij/pii) (lnpijopii), deduces pij/pii by exponentiation,
3250: sums on j different of i to get 1-pii/pii, deduces pii, and then all pij.
3251: Outputs ps[i][j] or probability to be observed in j being in i according to
3252: the values of the covariates cov[nc] and corresponding parameter values x[nc+shiftij]
3253: Sum on j ps[i][j] should equal to 1.
3254: */
3255: double s1, lnpijopii;
3256: /*double t34;*/
3257: int i,j, nc, ii, jj;
3258:
3259: for(i=1; i<= nlstate; i++){
3260: for(j=1; j<i;j++){
3261: for (nc=1, lnpijopii=0.;nc <=ncovmodel; nc++){
3262: /*lnpijopii += param[i][j][nc]*cov[nc];*/
3263: lnpijopii += x[nc+((i-1)*(nlstate+ndeath-1)+j-1)*ncovmodel]*cov[nc];
3264: /* printf("Int j<i s1=%.17e, lnpijopii=%.17e\n",s1,lnpijopii); */
3265: }
3266: ps[i][j]=lnpijopii; /* In fact ln(pij/pii) */
3267: /* printf("Debug pmij() i=%d j=%d nc=%d s1=%.17f, lnpijopii=%.17f\n",i,j,nc, s1,lnpijopii); */
3268: }
3269: for(j=i+1; j<=nlstate+ndeath;j++){
3270: for (nc=1, lnpijopii=0.;nc <=ncovmodel; nc++){
3271: /*lnpijopii += x[(i-1)*nlstate*ncovmodel+(j-2)*ncovmodel+nc+(i-1)*(ndeath-1)*ncovmodel]*cov[nc];*/
3272: lnpijopii += x[nc + ((i-1)*(nlstate+ndeath-1)+(j-2))*ncovmodel]*cov[nc];
3273: /* printf("Int j>i s1=%.17e, lnpijopii=%.17e %lx %lx\n",s1,lnpijopii,s1,lnpijopii); */
3274: }
3275: ps[i][j]=lnpijopii; /* In fact ln(pij/pii) */
3276: /* printf("Debug pmij() i=%d j=%d nc=%d s1=%.17f, lnpijopii=%.17f\n",i,j,nc, s1,lnpijopii); */
3277: }
3278: }
3279:
3280: for(i=1; i<= nlstate; i++){
3281: s1=0;
3282: for(j=1; j<i; j++){
3283: s1+=exp(ps[i][j]); /* In fact sums pij/pii */
3284: /* printf("debug1 %d %d ps=%lf exp(ps)=%lf s1+=%lf\n",i,j,ps[i][j],exp(ps[i][j]),s1); */
3285: }
3286: for(j=i+1; j<=nlstate+ndeath; j++){
3287: s1+=exp(ps[i][j]); /* In fact sums pij/pii */
3288: /* printf("debug2 %d %d ps=%lf exp(ps)=%lf s1+=%lf\n",i,j,ps[i][j],exp(ps[i][j]),s1); */
3289: }
3290: /* s1= sum_{j<>i} pij/pii=(1-pii)/pii and thus pii is known from s1 */
3291: ps[i][i]=1./(s1+1.);
3292: /* Computing other pijs */
3293: for(j=1; j<i; j++)
3294: ps[i][j]= exp(ps[i][j])*ps[i][i];/* Bug valgrind */
3295: for(j=i+1; j<=nlstate+ndeath; j++)
3296: ps[i][j]= exp(ps[i][j])*ps[i][i];
3297: /* ps[i][nlstate+1]=1.-s1- ps[i][i];*/ /* Sum should be 1 */
3298: } /* end i */
3299:
3300: for(ii=nlstate+1; ii<= nlstate+ndeath; ii++){
3301: for(jj=1; jj<= nlstate+ndeath; jj++){
3302: ps[ii][jj]=0;
3303: ps[ii][ii]=1;
3304: }
3305: }
3306:
3307:
3308: /* for(ii=1; ii<= nlstate+ndeath; ii++){ */
3309: /* for(jj=1; jj<= nlstate+ndeath; jj++){ */
3310: /* printf(" pmij ps[%d][%d]=%lf ",ii,jj,ps[ii][jj]); */
3311: /* } */
3312: /* printf("\n "); */
3313: /* } */
3314: /* printf("\n ");printf("%lf ",cov[2]);*/
3315: /*
3316: for(i=1; i<= npar; i++) printf("%f ",x[i]);
3317: goto end;*/
3318: return ps; /* Pointer is unchanged since its call */
3319: }
3320:
3321: /*************** backward transition probabilities ***************/
3322:
3323: /* 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 ) */
3324: /* double **bmij(double **ps, double *cov, int ncovmodel, double *x, int nlstate, double ***prevacurrent, double ***dnewm, double **doldm, double **dsavm, int ij ) */
3325: double **bmij(double **ps, double *cov, int ncovmodel, double *x, int nlstate, double ***prevacurrent, int ij )
3326: {
3327: /* Computes the backward probability at age agefin, cov[2], and covariate combination 'ij'. In fact cov is already filled and x too.
3328: * Call to pmij(cov and x), call to cross prevalence, sums and inverses, left multiply, and returns in **ps as well as **bmij.
3329: */
3330: int i, ii, j,k;
3331:
3332: double **out, **pmij();
3333: double sumnew=0.;
3334: double agefin;
3335: double k3=0.; /* constant of the w_x diagonal matrix (in order for B to sum to 1 even for death state) */
3336: double **dnewm, **dsavm, **doldm;
3337: double **bbmij;
3338:
3339: doldm=ddoldms; /* global pointers */
3340: dnewm=ddnewms;
3341: dsavm=ddsavms;
3342:
3343: /* Debug */
3344: /* printf("Bmij ij=%d, cov[2}=%f\n", ij, cov[2]); */
3345: agefin=cov[2];
3346: /* Bx = Diag(w_x) P_x Diag(Sum_i w^i_x p^ij_x */
3347: /* bmij *//* age is cov[2], ij is included in cov, but we need for
3348: the observed prevalence (with this covariate ij) at beginning of transition */
3349: /* dsavm=pmij(pmmij,cov,ncovmodel,x,nlstate); */
3350:
3351: /* P_x */
3352: pmmij=pmij(pmmij,cov,ncovmodel,x,nlstate); /*This is forward probability from agefin to agefin + stepm *//* Bug valgrind */
3353: /* outputs pmmij which is a stochastic matrix in row */
3354:
3355: /* Diag(w_x) */
3356: /* Rescaling the cross-sectional prevalence: Problem with prevacurrent which can be zero */
3357: sumnew=0.;
3358: /*for (ii=1;ii<=nlstate+ndeath;ii++){*/
3359: for (ii=1;ii<=nlstate;ii++){ /* Only on live states */
3360: /* printf(" agefin=%d, ii=%d, ij=%d, prev=%f\n",(int)agefin,ii, ij, prevacurrent[(int)agefin][ii][ij]); */
3361: sumnew+=prevacurrent[(int)agefin][ii][ij];
3362: }
3363: if(sumnew >0.01){ /* At least some value in the prevalence */
3364: for (ii=1;ii<=nlstate+ndeath;ii++){
3365: for (j=1;j<=nlstate+ndeath;j++)
3366: doldm[ii][j]=(ii==j ? prevacurrent[(int)agefin][ii][ij]/sumnew : 0.0);
3367: }
3368: }else{
3369: for (ii=1;ii<=nlstate+ndeath;ii++){
3370: for (j=1;j<=nlstate+ndeath;j++)
3371: doldm[ii][j]=(ii==j ? 1./nlstate : 0.0);
3372: }
3373: /* if(sumnew <0.9){ */
3374: /* printf("Problem internal bmij B: sum on i wi <0.9: j=%d, sum_i wi=%lf,agefin=%d\n",j,sumnew, (int)agefin); */
3375: /* } */
3376: }
3377: k3=0.0; /* We put the last diagonal to 0 */
3378: for (ii=nlstate+1;ii<=nlstate+ndeath;ii++){
3379: doldm[ii][ii]= k3;
3380: }
3381: /* End doldm, At the end doldm is diag[(w_i)] */
3382:
3383: /* Left product of this diag matrix by pmmij=Px (dnewm=dsavm*doldm): diag[(w_i)*Px */
3384: bbmij=matprod2(dnewm, doldm,1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, pmmij); /* was a Bug Valgrind */
3385:
3386: /* Diag(Sum_i w^i_x p^ij_x, should be the prevalence at age x+stepm */
3387: /* w1 p11 + w2 p21 only on live states N1./N..*N11/N1. + N2./N..*N21/N2.=(N11+N21)/N..=N.1/N.. */
3388: for (j=1;j<=nlstate+ndeath;j++){
3389: sumnew=0.;
3390: for (ii=1;ii<=nlstate;ii++){
3391: /* sumnew+=dsavm[ii][j]*prevacurrent[(int)agefin][ii][ij]; */
3392: sumnew+=pmmij[ii][j]*doldm[ii][ii]; /* Yes prevalence at beginning of transition */
3393: } /* sumnew is (N11+N21)/N..= N.1/N.. = sum on i of w_i pij */
3394: for (ii=1;ii<=nlstate+ndeath;ii++){
3395: /* if(agefin >= agemaxpar && agefin <= agemaxpar+stepm/YEARM){ */
3396: /* dsavm[ii][j]=(ii==j ? 1./sumnew : 0.0); */
3397: /* }else if(agefin >= agemaxpar+stepm/YEARM){ */
3398: /* dsavm[ii][j]=(ii==j ? 1./sumnew : 0.0); */
3399: /* }else */
3400: dsavm[ii][j]=(ii==j ? 1./sumnew : 0.0);
3401: } /*End ii */
3402: } /* End j, At the end dsavm is diag[1/(w_1p1i+w_2 p2i)] for ALL states even if the sum is only for live states */
3403:
3404: ps=matprod2(ps, dnewm,1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, dsavm); /* was a Bug Valgrind */
3405: /* ps is now diag[w_i] * Px * diag [1/(w_1p1i+w_2 p2i)] */
3406: /* end bmij */
3407: return ps; /*pointer is unchanged */
3408: }
3409: /*************** transition probabilities ***************/
3410:
3411: double **bpmij(double **ps, double *cov, int ncovmodel, double *x, int nlstate )
3412: {
3413: /* According to parameters values stored in x and the covariate's values stored in cov,
3414: computes the probability to be observed in state j being in state i by appying the
3415: model to the ncovmodel covariates (including constant and age).
3416: lnpijopii=ln(pij/pii)= aij+bij*age+cij*v1+dij*v2+... = sum_nc=1^ncovmodel xij(nc)*cov[nc]
3417: and, according on how parameters are entered, the position of the coefficient xij(nc) of the
3418: ncth covariate in the global vector x is given by the formula:
3419: j<i nc+((i-1)*(nlstate+ndeath-1)+j-1)*ncovmodel
3420: j>=i nc + ((i-1)*(nlstate+ndeath-1)+(j-2))*ncovmodel
3421: Computes ln(pij/pii) (lnpijopii), deduces pij/pii by exponentiation,
3422: sums on j different of i to get 1-pii/pii, deduces pii, and then all pij.
3423: Outputs ps[i][j] the probability to be observed in j being in j according to
3424: the values of the covariates cov[nc] and corresponding parameter values x[nc+shiftij]
3425: */
3426: double s1, lnpijopii;
3427: /*double t34;*/
3428: int i,j, nc, ii, jj;
3429:
3430: for(i=1; i<= nlstate; i++){
3431: for(j=1; j<i;j++){
3432: for (nc=1, lnpijopii=0.;nc <=ncovmodel; nc++){
3433: /*lnpijopii += param[i][j][nc]*cov[nc];*/
3434: lnpijopii += x[nc+((i-1)*(nlstate+ndeath-1)+j-1)*ncovmodel]*cov[nc];
3435: /* printf("Int j<i s1=%.17e, lnpijopii=%.17e\n",s1,lnpijopii); */
3436: }
3437: ps[i][j]=lnpijopii; /* In fact ln(pij/pii) */
3438: /* printf("s1=%.17e, lnpijopii=%.17e\n",s1,lnpijopii); */
3439: }
3440: for(j=i+1; j<=nlstate+ndeath;j++){
3441: for (nc=1, lnpijopii=0.;nc <=ncovmodel; nc++){
3442: /*lnpijopii += x[(i-1)*nlstate*ncovmodel+(j-2)*ncovmodel+nc+(i-1)*(ndeath-1)*ncovmodel]*cov[nc];*/
3443: lnpijopii += x[nc + ((i-1)*(nlstate+ndeath-1)+(j-2))*ncovmodel]*cov[nc];
3444: /* printf("Int j>i s1=%.17e, lnpijopii=%.17e %lx %lx\n",s1,lnpijopii,s1,lnpijopii); */
3445: }
3446: ps[i][j]=lnpijopii; /* In fact ln(pij/pii) */
3447: }
3448: }
3449:
3450: for(i=1; i<= nlstate; i++){
3451: s1=0;
3452: for(j=1; j<i; j++){
3453: s1+=exp(ps[i][j]); /* In fact sums pij/pii */
3454: /*printf("debug1 %d %d ps=%lf exp(ps)=%lf s1+=%lf\n",i,j,ps[i][j],exp(ps[i][j]),s1); */
3455: }
3456: for(j=i+1; j<=nlstate+ndeath; j++){
3457: s1+=exp(ps[i][j]); /* In fact sums pij/pii */
3458: /*printf("debug2 %d %d ps=%lf exp(ps)=%lf s1+=%lf\n",i,j,ps[i][j],exp(ps[i][j]),s1); */
3459: }
3460: /* s1= sum_{j<>i} pij/pii=(1-pii)/pii and thus pii is known from s1 */
3461: ps[i][i]=1./(s1+1.);
3462: /* Computing other pijs */
3463: for(j=1; j<i; j++)
3464: ps[i][j]= exp(ps[i][j])*ps[i][i];
3465: for(j=i+1; j<=nlstate+ndeath; j++)
3466: ps[i][j]= exp(ps[i][j])*ps[i][i];
3467: /* ps[i][nlstate+1]=1.-s1- ps[i][i];*/ /* Sum should be 1 */
3468: } /* end i */
3469:
3470: for(ii=nlstate+1; ii<= nlstate+ndeath; ii++){
3471: for(jj=1; jj<= nlstate+ndeath; jj++){
3472: ps[ii][jj]=0;
3473: ps[ii][ii]=1;
3474: }
3475: }
3476: /* Added for prevbcast */ /* Transposed matrix too */
3477: for(jj=1; jj<= nlstate+ndeath; jj++){
3478: s1=0.;
3479: for(ii=1; ii<= nlstate+ndeath; ii++){
3480: s1+=ps[ii][jj];
3481: }
3482: for(ii=1; ii<= nlstate; ii++){
3483: ps[ii][jj]=ps[ii][jj]/s1;
3484: }
3485: }
3486: /* Transposition */
3487: for(jj=1; jj<= nlstate+ndeath; jj++){
3488: for(ii=jj; ii<= nlstate+ndeath; ii++){
3489: s1=ps[ii][jj];
3490: ps[ii][jj]=ps[jj][ii];
3491: ps[jj][ii]=s1;
3492: }
3493: }
3494: /* for(ii=1; ii<= nlstate+ndeath; ii++){ */
3495: /* for(jj=1; jj<= nlstate+ndeath; jj++){ */
3496: /* printf(" pmij ps[%d][%d]=%lf ",ii,jj,ps[ii][jj]); */
3497: /* } */
3498: /* printf("\n "); */
3499: /* } */
3500: /* printf("\n ");printf("%lf ",cov[2]);*/
3501: /*
3502: for(i=1; i<= npar; i++) printf("%f ",x[i]);
3503: goto end;*/
3504: return ps;
3505: }
3506:
3507:
3508: /**************** Product of 2 matrices ******************/
3509:
3510: double **matprod2(double **out, double **in,int nrl, int nrh, int ncl, int nch, int ncolol, int ncoloh, double **b)
3511: {
3512: /* Computes the matrix product of in(1,nrh-nrl+1)(1,nch-ncl+1) times
3513: b(1,nch-ncl+1)(1,ncoloh-ncolol+1) into out(...) */
3514: /* in, b, out are matrice of pointers which should have been initialized
3515: before: only the contents of out is modified. The function returns
3516: a pointer to pointers identical to out */
3517: int i, j, k;
3518: for(i=nrl; i<= nrh; i++)
3519: for(k=ncolol; k<=ncoloh; k++){
3520: out[i][k]=0.;
3521: for(j=ncl; j<=nch; j++)
3522: out[i][k] +=in[i][j]*b[j][k];
3523: }
3524: return out;
3525: }
3526:
3527:
3528: /************* Higher Matrix Product ***************/
3529:
3530: double ***hpxij(double ***po, int nhstepm, double age, int hstepm, double *x, int nlstate, int stepm, double **oldm, double **savm, int ij, int nres )
3531: {
3532: /* Already optimized with precov.
3533: Computes the transition matrix starting at age 'age' and dummies values in each resultline (loop on ij to find the corresponding combination) to over
3534: 'nhstepm*hstepm*stepm' months (i.e. until
3535: age (in years) age+nhstepm*hstepm*stepm/12) by multiplying
3536: nhstepm*hstepm matrices.
3537: Output is stored in matrix po[i][j][h] for h every 'hstepm' step
3538: (typically every 2 years instead of every month which is too big
3539: for the memory).
3540: Model is determined by parameters x and covariates have to be
3541: included manually here.
3542:
3543: */
3544:
3545: int i, j, d, h, k, k1;
3546: double **out, cov[NCOVMAX+1];
3547: double **newm;
3548: double agexact;
3549: double agebegin, ageend;
3550:
3551: /* Hstepm could be zero and should return the unit matrix */
3552: for (i=1;i<=nlstate+ndeath;i++)
3553: for (j=1;j<=nlstate+ndeath;j++){
3554: oldm[i][j]=(i==j ? 1.0 : 0.0);
3555: po[i][j][0]=(i==j ? 1.0 : 0.0);
3556: }
3557: /* Even if hstepm = 1, at least one multiplication by the unit matrix */
3558: for(h=1; h <=nhstepm; h++){
3559: for(d=1; d <=hstepm; d++){
3560: newm=savm;
3561: /* Covariates have to be included here again */
3562: cov[1]=1.;
3563: agexact=age+((h-1)*hstepm + (d-1))*stepm/YEARM; /* age just before transition */
3564: cov[2]=agexact;
3565: if(nagesqr==1){
3566: cov[3]= agexact*agexact;
3567: }
3568: /* Model(2) V1 + V2 + V3 + V8 + V7*V8 + V5*V6 + V8*age + V3*age + age*age */
3569: /* total number of covariates of the model nbocc(+)+1 = 8 excepting constant and age and age*age */
3570: for(k1=1;k1<=cptcovt;k1++){ /* loop on model equation (including products) */
3571: if(Typevar[k1]==1){ /* A product with age */
3572: cov[2+nagesqr+k1]=precov[nres][k1]*cov[2];
3573: }else{
3574: cov[2+nagesqr+k1]=precov[nres][k1];
3575: }
3576: }/* End of loop on model equation */
3577: /* Old code */
3578: /* if( Dummy[k1]==0 && Typevar[k1]==0 ){ /\* Single dummy *\/ */
3579: /* /\* V(Tvarsel)=Tvalsel=Tresult[nres][pos](value); V(Tvresult[nres][pos] (variable): V(variable)=value) *\/ */
3580: /* /\* for (k=1; k<=nsd;k++) { /\\* For single dummy covariates only *\\/ *\/ */
3581: /* /\* /\\* Here comes the value of the covariate 'ij' after renumbering k with single dummy covariates *\\/ *\/ */
3582: /* /\* codtabm(ij,k) (1 & (ij-1) >> (k-1))+1 *\/ */
3583: /* /\* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 *\/ */
3584: /* /\* k 1 2 3 4 5 6 7 8 9 *\/ */
3585: /* /\*Tvar[k]= 5 4 3 6 5 2 7 1 1 *\/ */
3586: /* /\* nsd 1 2 3 *\/ /\* Counting single dummies covar fixed or tv *\/ */
3587: /* /\*TvarsD[nsd] 4 3 1 *\/ /\* ID of single dummy cova fixed or timevary*\/ */
3588: /* /\*TvarsDind[k] 2 3 9 *\/ /\* position K of single dummy cova *\/ */
3589: /* /\* cov[2+nagesqr+TvarsDind[k]]=nbcode[TvarsD[k]][codtabm(ij,k)];or [codtabm(ij,TnsdVar[TvarsD[k]] *\/ */
3590: /* cov[2+nagesqr+k1]=Tresult[nres][resultmodel[nres][k1]]; */
3591: /* /\* printf("hpxij Dummy combi=%d k=%d TvarsD[%d]=V%d TvarsDind[%d]=%d nbcode=%d cov=%lf codtabm(%d,TnsdVar[TvarsD[%d])=%d \n",ij,k, k, TvarsD[k],k,TvarsDind[k],nbcode[TvarsD[k]][codtabm(ij,TnsdVar[TvarsD[k]])],cov[2+nagesqr+TvarsDind[k]], ij, k, codtabm(ij,TnsdVar[TvarsD[k]])); *\/ */
3592: /* printf("hpxij Dummy combi=%d k1=%d Tvar[%d]=V%d cov[2+%d+%d]=%lf resultmodel[nres][%d]=%d nres/nresult=%d/%d \n",ij,k1,k1, Tvar[k1],nagesqr,k1,cov[2+nagesqr+k1],k1,resultmodel[nres][k1],nres,nresult); */
3593: /* printf("hpxij new Dummy precov[nres=%d][k1=%d]=%.4f\n", nres, k1, precov[nres][k1]); */
3594: /* }else if( Dummy[k1]==1 && Typevar[k1]==0 ){ /\* Single quantitative variables *\/ */
3595: /* /\* resultmodel[nres][k1]=k3: k1th position in the model correspond to the k3 position in the resultline *\/ */
3596: /* cov[2+nagesqr+k1]=Tqresult[nres][resultmodel[nres][k1]]; */
3597: /* /\* for (k=1; k<=nsq;k++) { /\\* For single varying covariates only *\\/ *\/ */
3598: /* /\* /\\* Here comes the value of quantitative after renumbering k with single quantitative covariates *\\/ *\/ */
3599: /* /\* cov[2+nagesqr+TvarsQind[k]]=Tqresult[nres][k]; *\/ */
3600: /* printf("hPxij Quantitative k1=%d resultmodel[nres][%d]=%d,Tqresult[%d][%d]=%f\n",k1,k1,resultmodel[nres][k1],nres,resultmodel[nres][k1],Tqresult[nres][resultmodel[nres][k1]]); */
3601: /* printf("hpxij new Quanti precov[nres=%d][k1=%d]=%.4f\n", nres, k1, precov[nres][k1]); */
3602: /* }else if( Dummy[k1]==2 ){ /\* For dummy with age product *\/ */
3603: /* /\* Tvar[k1] Variable in the age product age*V1 is 1 *\/ */
3604: /* /\* [Tinvresult[nres][V1] is its value in the resultline nres *\/ */
3605: /* cov[2+nagesqr+k1]=TinvDoQresult[nres][Tvar[k1]]*cov[2]; */
3606: /* printf("DhPxij Dummy with age k1=%d Tvar[%d]=%d TinvDoQresult[nres=%d][%d]=%.f age=%.2f,cov[2+%d+%d]=%.3f\n",k1,k1,Tvar[k1],nres,TinvDoQresult[nres][Tvar[k1]],cov[2],nagesqr,k1,cov[2+nagesqr+k1]); */
3607: /* printf("hpxij new Dummy with age product precov[nres=%d][k1=%d]=%.4f * age=%.2f\n", nres, k1, precov[nres][k1], cov[2]); */
3608:
3609: /* /\* cov[2+nagesqr+k1]=Tresult[nres][resultmodel[nres][k1]]; *\/ */
3610: /* /\* for (k=1; k<=cptcovage;k++){ /\\* For product with age V1+V1*age +V4 +age*V3 *\\/ *\/ */
3611: /* /\* 1+2 Tage[1]=2 TVar[2]=1 Dummy[2]=2, Tage[2]=4 TVar[4]=3 Dummy[4]=3 quant*\/ */
3612: /* /\* *\/ */
3613: /* /\* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 *\/ */
3614: /* /\* k 1 2 3 4 5 6 7 8 9 *\/ */
3615: /* /\*Tvar[k]= 5 4 3 6 5 2 7 1 1 *\/ */
3616: /* /\*cptcovage=2 1 2 *\/ */
3617: /* /\*Tage[k]= 5 8 *\/ */
3618: /* }else if( Dummy[k1]==3 ){ /\* For quant with age product *\/ */
3619: /* cov[2+nagesqr+k1]=Tresult[nres][resultmodel[nres][k1]]; */
3620: /* printf("QhPxij Quant with age k1=%d resultmodel[nres][%d]=%d,Tqresult[%d][%d]=%f\n",k1,k1,resultmodel[nres][k1],nres,resultmodel[nres][k1],Tqresult[nres][resultmodel[nres][k1]]); */
3621: /* printf("hpxij new Quanti with age product precov[nres=%d][k1=%d] * age=%.2f\n", nres, k1, precov[nres][k1], cov[2]); */
3622: /* /\* if(Dummy[Tage[k]]== 2){ /\\* dummy with age *\\/ *\/ */
3623: /* /\* /\\* if(Dummy[Tvar[Tage[k]]]== 2){ /\\\* dummy with age *\\\/ *\\/ *\/ */
3624: /* /\* /\\* cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,k)]*cov[2]; *\\/ *\/ */
3625: /* /\* /\\* cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,TnsdVar[TvarsD[Tvar[Tage[k]]]])]*cov[2]; *\\/ *\/ */
3626: /* /\* cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,TnsdVar[TvarsD[Tvar[Tage[k]]]])]*cov[2]; *\/ */
3627: /* /\* printf("hPxij Age combi=%d k=%d cptcovage=%d Tage[%d]=%d Tvar[Tage[%d]]=V%d nbcode[Tvar[Tage[k]]][codtabm(ij,TnsdVar[Tvar[Tage[k]]]])]=%d nres=%d\n",ij,k,cptcovage,k,Tage[k],k,Tvar[Tage[k]], nbcode[Tvar[Tage[k]]][codtabm(ij,TnsdVar[Tvar[Tage[k]]])],nres); *\/ */
3628: /* /\* } else if(Dummy[Tage[k]]== 3){ /\\* quantitative with age *\\/ *\/ */
3629: /* /\* cov[2+nagesqr+Tage[k]]=Tqresult[nres][k]; *\/ */
3630: /* /\* } *\/ */
3631: /* /\* printf("hPxij Age combi=%d k=%d Tage[%d]=V%d Tqresult[%d][%d]=%f\n",ij,k,k,Tage[k],nres,k,Tqresult[nres][k]); *\/ */
3632: /* }else if(Typevar[k1]==2 ){ /\* For product (not with age) *\/ */
3633: /* /\* for (k=1; k<=cptcovprod;k++){ /\\* For product without age *\\/ *\/ */
3634: /* /\* /\\* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 *\\/ *\/ */
3635: /* /\* /\\* k 1 2 3 4 5 6 7 8 9 *\\/ *\/ */
3636: /* /\* /\\*Tvar[k]= 5 4 3 6 5 2 7 1 1 *\\/ *\/ */
3637: /* /\* /\\*cptcovprod=1 1 2 *\\/ *\/ */
3638: /* /\* /\\*Tprod[]= 4 7 *\\/ *\/ */
3639: /* /\* /\\*Tvard[][1] 4 1 *\\/ *\/ */
3640: /* /\* /\\*Tvard[][2] 3 2 *\\/ *\/ */
3641:
3642: /* /\* printf("hPxij Prod ij=%d k=%d Tprod[%d]=%d Tvard[%d][1]=V%d, Tvard[%d][2]=V%d nbcode[Tvard[k][1]][codtabm(ij,Tvard[k][1])]=%d nbcode[Tvard[k][2]][codtabm(ij,Tvard[k][1])]=%d\n",ij,k,k,Tprod[k], k,Tvard[k][1], k,Tvard[k][2],nbcode[Tvard[k][1]][codtabm(ij,Tvard[k][1])],nbcode[Tvard[k][2]][codtabm(ij,Tvard[k][2])]); *\/ */
3643: /* /\* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)] * nbcode[Tvard[k][2]][codtabm(ij,k)]; *\/ */
3644: /* cov[2+nagesqr+k1]=TinvDoQresult[nres][Tvardk[k1][1]] * TinvDoQresult[nres][Tvardk[k1][2]]; */
3645: /* printf("hPxij Prod ij=%d k1=%d cov[2+%d+%d]=%.5f Tvard[%d][1]=V%d * Tvard[%d][2]=V%d ; TinvDoQresult[nres][Tvardk[k1][1]]=%.4f * TinvDoQresult[nres][Tvardk[k1][1]]=%.4f\n",ij,k1,nagesqr,k1,cov[2+nagesqr+k1],k1,Tvardk[k1][1], k1,Tvardk[k1][2], TinvDoQresult[nres][Tvardk[k1][1]], TinvDoQresult[nres][Tvardk[k1][2]]); */
3646: /* printf("hpxij new Product no age product precov[nres=%d][k1=%d]=%.4f\n", nres, k1, precov[nres][k1]); */
3647:
3648: /* /\* if(Dummy[Tvardk[k1][1]]==0){ *\/ */
3649: /* /\* if(Dummy[Tvardk[k1][2]]==0){ /\\* Product of dummies *\\/ *\/ */
3650: /* /\* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)] * nbcode[Tvard[k][2]][codtabm(ij,k)]; *\/ */
3651: /* /\* cov[2+nagesqr+k1]=Tinvresult[nres][Tvardk[k1][1]] * Tinvresult[nres][Tvardk[k1][2]]; *\/ */
3652: /* /\* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,TnsdVar[Tvard[k][1]])] * nbcode[Tvard[k][2]][codtabm(ij,TnsdVar[Tvard[k][2]])]; *\/ */
3653: /* /\* }else{ /\\* Product of dummy by quantitative *\\/ *\/ */
3654: /* /\* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,TnsdVar[Tvard[k][1]])] * Tqresult[nres][k]; *\/ */
3655: /* /\* cov[2+nagesqr+k1]=Tresult[nres][Tinvresult[nres][Tvardk[k1][1]]] * Tqresult[nres][Tinvresult[nres][Tvardk[k1][2]]]; *\/ */
3656: /* /\* } *\/ */
3657: /* /\* }else{ /\\* Product of quantitative by...*\\/ *\/ */
3658: /* /\* if(Dummy[Tvard[k][2]]==0){ /\\* quant by dummy *\\/ *\/ */
3659: /* /\* /\\* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][2]][codtabm(ij,TnsdVar[Tvard[k][2]])] * Tqinvresult[nres][Tvard[k][1]]; *\\/ *\/ */
3660: /* /\* cov[2+nagesqr+k1]=Tqresult[nres][Tinvresult[nres][Tvardk[k1][1]]] * Tresult[nres][Tinvresult[nres][Tvardk[k1][2]]] ; *\/ */
3661: /* /\* }else{ /\\* Product of two quant *\\/ *\/ */
3662: /* /\* /\\* cov[2+nagesqr+Tprod[k]]=Tqinvresult[nres][Tvard[k][1]]* Tqinvresult[nres][Tvard[k][2]]; *\\/ *\/ */
3663: /* /\* cov[2+nagesqr+k1]=Tqresult[nres][Tinvresult[nres][Tvardk[k1][1]]] * Tqresult[nres][Tinvresult[nres][Tvardk[k1][2]]] ; *\/ */
3664: /* /\* } *\/ */
3665: /* /\* }/\\*end of products quantitative *\\/ *\/ */
3666: /* }/\*end of products *\/ */
3667: /* } /\* End of loop on model equation *\/ */
3668: /* for (k=1; k<=cptcovn;k++) */
3669: /* cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,k)]; */
3670: /* for (k=1; k<=cptcovage;k++) /\* Should start at cptcovn+1 *\/ */
3671: /* cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,k)]*cov[2]; */
3672: /* for (k=1; k<=cptcovprod;k++) /\* Useless because included in cptcovn *\/ */
3673: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)]*nbcode[Tvard[k][2]][codtabm(ij,k)]; */
3674:
3675:
3676: /*printf("hxi cptcov=%d cptcode=%d\n",cptcov,cptcode);*/
3677: /*printf("h=%d d=%d age=%f cov=%f\n",h,d,age,cov[2]);*/
3678: /* right multiplication of oldm by the current matrix */
3679: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath,
3680: pmij(pmmij,cov,ncovmodel,x,nlstate));
3681: /* if((int)age == 70){ */
3682: /* printf(" Forward hpxij age=%d agexact=%f d=%d nhstepm=%d hstepm=%d\n", (int) age, agexact, d, nhstepm, hstepm); */
3683: /* for(i=1; i<=nlstate+ndeath; i++) { */
3684: /* printf("%d pmmij ",i); */
3685: /* for(j=1;j<=nlstate+ndeath;j++) { */
3686: /* printf("%f ",pmmij[i][j]); */
3687: /* } */
3688: /* printf(" oldm "); */
3689: /* for(j=1;j<=nlstate+ndeath;j++) { */
3690: /* printf("%f ",oldm[i][j]); */
3691: /* } */
3692: /* printf("\n"); */
3693: /* } */
3694: /* } */
3695: savm=oldm;
3696: oldm=newm;
3697: }
3698: for(i=1; i<=nlstate+ndeath; i++)
3699: for(j=1;j<=nlstate+ndeath;j++) {
3700: po[i][j][h]=newm[i][j];
3701: /*if(h==nhstepm) printf("po[%d][%d][%d]=%f ",i,j,h,po[i][j][h]);*/
3702: }
3703: /*printf("h=%d ",h);*/
3704: } /* end h */
3705: /* printf("\n H=%d \n",h); */
3706: return po;
3707: }
3708:
3709: /************* Higher Back Matrix Product ***************/
3710: /* 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 ) */
3711: double ***hbxij(double ***po, int nhstepm, double age, int hstepm, double *x, double ***prevacurrent, int nlstate, int stepm, int ij, int nres )
3712: {
3713: /* For dummy covariates given in each resultline (for historical, computes the corresponding combination ij),
3714: computes the transition matrix starting at age 'age' over
3715: 'nhstepm*hstepm*stepm' months (i.e. until
3716: age (in years) age+nhstepm*hstepm*stepm/12) by multiplying
3717: nhstepm*hstepm matrices.
3718: Output is stored in matrix po[i][j][h] for h every 'hstepm' step
3719: (typically every 2 years instead of every month which is too big
3720: for the memory).
3721: Model is determined by parameters x and covariates have to be
3722: included manually here. Then we use a call to bmij(x and cov)
3723: The addresss of po (p3mat allocated to the dimension of nhstepm) should be stored for output
3724: */
3725:
3726: int i, j, d, h, k, k1;
3727: double **out, cov[NCOVMAX+1], **bmij();
3728: double **newm, ***newmm;
3729: double agexact;
3730: double agebegin, ageend;
3731: double **oldm, **savm;
3732:
3733: newmm=po; /* To be saved */
3734: oldm=oldms;savm=savms; /* Global pointers */
3735: /* Hstepm could be zero and should return the unit matrix */
3736: for (i=1;i<=nlstate+ndeath;i++)
3737: for (j=1;j<=nlstate+ndeath;j++){
3738: oldm[i][j]=(i==j ? 1.0 : 0.0);
3739: po[i][j][0]=(i==j ? 1.0 : 0.0);
3740: }
3741: /* Even if hstepm = 1, at least one multiplication by the unit matrix */
3742: for(h=1; h <=nhstepm; h++){
3743: for(d=1; d <=hstepm; d++){
3744: newm=savm;
3745: /* Covariates have to be included here again */
3746: cov[1]=1.;
3747: agexact=age-( (h-1)*hstepm + (d) )*stepm/YEARM; /* age just before transition, d or d-1? */
3748: /* agexact=age+((h-1)*hstepm + (d-1))*stepm/YEARM; /\* age just before transition *\/ */
3749: /* Debug */
3750: /* printf("hBxij age=%lf, agexact=%lf\n", age, agexact); */
3751: cov[2]=agexact;
3752: if(nagesqr==1){
3753: cov[3]= agexact*agexact;
3754: }
3755: /** New code */
3756: for(k1=1;k1<=cptcovt;k1++){ /* loop on model equation (including products) */
3757: if(Typevar[k1]==1){ /* A product with age */
3758: cov[2+nagesqr+k1]=precov[nres][k1]*cov[2];
3759: }else{
3760: cov[2+nagesqr+k1]=precov[nres][k1];
3761: }
3762: }/* End of loop on model equation */
3763: /** End of new code */
3764: /** This was old code */
3765: /* for (k=1; k<=nsd;k++){ /\* For single dummy covariates only *\//\* cptcovn error *\/ */
3766: /* /\* cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,k)]; *\/ */
3767: /* /\* /\\* cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,Tvar[k])]; *\\/ *\/ */
3768: /* cov[2+nagesqr+TvarsDind[k]]=nbcode[TvarsD[k]][codtabm(ij,TvarsD[k])];/\* Bug valgrind *\/ */
3769: /* /\* printf("hbxij Dummy agexact=%.0f combi=%d k=%d TvarsD[%d]=V%d TvarsDind[%d]=%d nbcode=%d cov[%d]=%lf codtabm(%d,Tvar[%d])=%d \n",agexact,ij,k, k, TvarsD[k],k,TvarsDind[k],nbcode[TvarsD[k]][codtabm(ij,k)],2+nagesqr+TvarsDind[k],cov[2+nagesqr+TvarsDind[k]], ij, k, codtabm(ij,k)); *\/ */
3770: /* } */
3771: /* for (k=1; k<=nsq;k++) { /\* For single varying covariates only *\/ */
3772: /* /\* Here comes the value of quantitative after renumbering k with single quantitative covariates *\/ */
3773: /* cov[2+nagesqr+TvarsQind[k]]=Tqresult[nres][k]; */
3774: /* /\* printf("hPxij Quantitative k=%d TvarsQind[%d]=%d, TvarsQ[%d]=V%d,Tqresult[%d][%d]=%f\n",k,k,TvarsQind[k],k,TvarsQ[k],nres,k,Tqresult[nres][k]); *\/ */
3775: /* } */
3776: /* for (k=1; k<=cptcovage;k++){ /\* Should start at cptcovn+1 *\//\* For product with age *\/ */
3777: /* /\* if(Dummy[Tvar[Tage[k]]]== 2){ /\\* dummy with age error!!!*\\/ *\/ */
3778: /* if(Dummy[Tage[k]]== 2){ /\* dummy with age *\/ */
3779: /* cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,Tvar[Tage[k]])]*cov[2]; */
3780: /* } else if(Dummy[Tage[k]]== 3){ /\* quantitative with age *\/ */
3781: /* cov[2+nagesqr+Tage[k]]=Tqresult[nres][k]; */
3782: /* } */
3783: /* /\* printf("hBxij Age combi=%d k=%d Tage[%d]=V%d Tqresult[%d][%d]=%f\n",ij,k,k,Tage[k],nres,k,Tqresult[nres][k]); *\/ */
3784: /* } */
3785: /* for (k=1; k<=cptcovprod;k++){ /\* Useless because included in cptcovn *\/ */
3786: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,Tvard[k][1])]*nbcode[Tvard[k][2]][codtabm(ij,Tvard[k][2])]; */
3787: /* if(Dummy[Tvard[k][1]]==0){ */
3788: /* if(Dummy[Tvard[k][2]]==0){ */
3789: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,Tvard[k][1])] * nbcode[Tvard[k][2]][codtabm(ij,Tvard[k][1])]; */
3790: /* }else{ */
3791: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,Tvard[k][1])] * Tqresult[nres][k]; */
3792: /* } */
3793: /* }else{ */
3794: /* if(Dummy[Tvard[k][2]]==0){ */
3795: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][2]][codtabm(ij,Tvard[k][2])] * Tqinvresult[nres][Tvard[k][1]]; */
3796: /* }else{ */
3797: /* cov[2+nagesqr+Tprod[k]]=Tqinvresult[nres][Tvard[k][1]]* Tqinvresult[nres][Tvard[k][2]]; */
3798: /* } */
3799: /* } */
3800: /* } */
3801: /* /\*printf("hxi cptcov=%d cptcode=%d\n",cptcov,cptcode);*\/ */
3802: /* /\*printf("h=%d d=%d age=%f cov=%f\n",h,d,age,cov[2]);*\/ */
3803: /** End of old code */
3804:
3805: /* Careful transposed matrix */
3806: /* age is in cov[2], prevacurrent at beginning of transition. */
3807: /* out=matprod2(newm, bmij(pmmij,cov,ncovmodel,x,nlstate,prevacurrent, dnewm, doldm, dsavm,ij),\ */
3808: /* 1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm); */
3809: out=matprod2(newm, bmij(pmmij,cov,ncovmodel,x,nlstate,prevacurrent,ij),\
3810: 1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm);/* Bug valgrind */
3811: /* if((int)age == 70){ */
3812: /* printf(" Backward hbxij age=%d agexact=%f d=%d nhstepm=%d hstepm=%d\n", (int) age, agexact, d, nhstepm, hstepm); */
3813: /* for(i=1; i<=nlstate+ndeath; i++) { */
3814: /* printf("%d pmmij ",i); */
3815: /* for(j=1;j<=nlstate+ndeath;j++) { */
3816: /* printf("%f ",pmmij[i][j]); */
3817: /* } */
3818: /* printf(" oldm "); */
3819: /* for(j=1;j<=nlstate+ndeath;j++) { */
3820: /* printf("%f ",oldm[i][j]); */
3821: /* } */
3822: /* printf("\n"); */
3823: /* } */
3824: /* } */
3825: savm=oldm;
3826: oldm=newm;
3827: }
3828: for(i=1; i<=nlstate+ndeath; i++)
3829: for(j=1;j<=nlstate+ndeath;j++) {
3830: po[i][j][h]=newm[i][j];
3831: /* if(h==nhstepm) */
3832: /* printf("po[%d][%d][%d]=%f ",i,j,h,po[i][j][h]); */
3833: }
3834: /* printf("h=%d %.1f ",h, agexact); */
3835: } /* end h */
3836: /* printf("\n H=%d nhs=%d \n",h, nhstepm); */
3837: return po;
3838: }
3839:
3840:
3841: #ifdef NLOPT
3842: double myfunc(unsigned n, const double *p1, double *grad, void *pd){
3843: double fret;
3844: double *xt;
3845: int j;
3846: myfunc_data *d2 = (myfunc_data *) pd;
3847: /* xt = (p1-1); */
3848: xt=vector(1,n);
3849: for (j=1;j<=n;j++) xt[j]=p1[j-1]; /* xt[1]=p1[0] */
3850:
3851: fret=(d2->function)(xt); /* p xt[1]@8 is fine */
3852: /* fret=(*func)(xt); /\* p xt[1]@8 is fine *\/ */
3853: printf("Function = %.12lf ",fret);
3854: for (j=1;j<=n;j++) printf(" %d %.8lf", j, xt[j]);
3855: printf("\n");
3856: free_vector(xt,1,n);
3857: return fret;
3858: }
3859: #endif
3860:
3861: /*************** log-likelihood *************/
3862: double func( double *x)
3863: {
3864: int i, ii, j, k, mi, d, kk, kf=0;
3865: int ioffset=0;
3866: double l, ll[NLSTATEMAX+1], cov[NCOVMAX+1];
3867: double **out;
3868: double lli; /* Individual log likelihood */
3869: int s1, s2;
3870: int iv=0, iqv=0, itv=0, iqtv=0 ; /* Index of varying covariate, fixed quantitative cov, time varying covariate, quantitative time varying covariate */
3871:
3872: double bbh, survp;
3873: double agexact;
3874: double agebegin, ageend;
3875: /*extern weight */
3876: /* We are differentiating ll according to initial status */
3877: /* for (i=1;i<=npar;i++) printf("%f ", x[i]);*/
3878: /*for(i=1;i<imx;i++)
3879: printf(" %d\n",s[4][i]);
3880: */
3881:
3882: ++countcallfunc;
3883:
3884: cov[1]=1.;
3885:
3886: for(k=1; k<=nlstate; k++) ll[k]=0.;
3887: ioffset=0;
3888: if(mle==1){
3889: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
3890: /* Computes the values of the ncovmodel covariates of the model
3891: depending if the covariates are fixed or varying (age dependent) and stores them in cov[]
3892: Then computes with function pmij which return a matrix p[i][j] giving the elementary probability
3893: to be observed in j being in i according to the model.
3894: */
3895: ioffset=2+nagesqr ;
3896: /* Fixed */
3897: for (kf=1; kf<=ncovf;kf++){ /* For each fixed covariate dummu or quant or prod */
3898: /* # V1=sex, V2=raedyrs Quant Fixed, State=livarnb4..livarnb11, V3=iadl4..iald11, V4=adlw4..adlw11, V5=r4bmi..r11bmi */
3899: /* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
3900: /* TvarF[1]=Tvar[6]=2, TvarF[2]=Tvar[7]=7, TvarF[3]=Tvar[9]=1 ID of fixed covariates or product V2, V1*V2, V1 */
3901: /* TvarFind; TvarFind[1]=6, TvarFind[2]=7, TvarFind[3]=9 *//* Inverse V2(6) is first fixed (single or prod) */
3902: cov[ioffset+TvarFind[kf]]=covar[Tvar[TvarFind[kf]]][i];/* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1, only V1 is fixed (TvarFind[1]=6)*/
3903: /* V1*V2 (7) TvarFind[2]=7, TvarFind[3]=9 */
3904: }
3905: /* In model V2+V1*V4+age*V3+V3*V2 Tvar[1] is V2, Tvar[2=V1*V4]
3906: is 5, Tvar[3=age*V3] should not be computed because of age Tvar[4=V3*V2]=6
3907: has been calculated etc */
3908: /* For an individual i, wav[i] gives the number of effective waves */
3909: /* We compute the contribution to Likelihood of each effective transition
3910: mw[mi][i] is real wave of the mi th effectve wave */
3911: /* Then statuses are computed at each begin and end of an effective wave s1=s[ mw[mi][i] ][i];
3912: s2=s[mw[mi+1][i]][i];
3913: And the iv th varying covariate is the cotvar[mw[mi+1][i]][iv][i]
3914: But if the variable is not in the model TTvar[iv] is the real variable effective in the model:
3915: meaning that decodemodel should be used cotvar[mw[mi+1][i]][TTvar[iv]][i]
3916: */
3917: for(mi=1; mi<= wav[i]-1; mi++){ /* Varying with waves */
3918: /* Wave varying (but not age varying) */
3919: for(k=1; k <= ncovv ; k++){ /* Varying covariates in the model (single and product but no age )"V5+V4+V3+V4*V3+V5*age+V1*age+V1" +TvarVind 1,2,3,4(V4*V3) Tvar[1]@7{5, 4, 3, 6, 5, 1, 1 ; 6 because the created covar is after V5 and is 6, minus 1+1, 3,2,1,4 positions in cotvar*/
3920: /* cov[ioffset+TvarVind[k]]=cotvar[mw[mi][i]][Tvar[TvarVind[k]]][i]; but where is the crossproduct? */
3921: cov[ioffset+TvarVind[k]]=cotvar[mw[mi][i]][Tvar[TvarVind[k]]-ncovcol-nqv][i];
3922: }
3923: for (ii=1;ii<=nlstate+ndeath;ii++)
3924: for (j=1;j<=nlstate+ndeath;j++){
3925: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
3926: savm[ii][j]=(ii==j ? 1.0 : 0.0);
3927: }
3928:
3929: agebegin=agev[mw[mi][i]][i]; /* Age at beginning of effective wave */
3930: ageend=agev[mw[mi][i]][i] + (dh[mi][i])*stepm/YEARM; /* Age at end of effective wave and at the end of transition */
3931: for(d=0; d<dh[mi][i]; d++){
3932: newm=savm;
3933: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM;
3934: cov[2]=agexact;
3935: if(nagesqr==1)
3936: cov[3]= agexact*agexact; /* Should be changed here */
3937: for (kk=1; kk<=cptcovage;kk++) {
3938: if(!FixedV[Tvar[Tage[kk]]])
3939: cov[Tage[kk]+2+nagesqr]=covar[Tvar[Tage[kk]]][i]*agexact; /* Tage[kk] gives the data-covariate associated with age */
3940: else
3941: cov[Tage[kk]+2+nagesqr]=cotvar[mw[mi][i]][Tvar[Tage[kk]]-ncovcol-nqv][i]*agexact;
3942: }
3943: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
3944: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
3945: savm=oldm;
3946: oldm=newm;
3947: } /* end mult */
3948:
3949: /*lli=log(out[s[mw[mi][i]][i]][s[mw[mi+1][i]][i]]);*/ /* Original formula */
3950: /* But now since version 0.9 we anticipate for bias at large stepm.
3951: * If stepm is larger than one month (smallest stepm) and if the exact delay
3952: * (in months) between two waves is not a multiple of stepm, we rounded to
3953: * the nearest (and in case of equal distance, to the lowest) interval but now
3954: * we keep into memory the bias bh[mi][i] and also the previous matrix product
3955: * (i.e to dh[mi][i]-1) saved in 'savm'. Then we inter(extra)polate the
3956: * probability in order to take into account the bias as a fraction of the way
3957: * from savm to out if bh is negative or even beyond if bh is positive. bh varies
3958: * -stepm/2 to stepm/2 .
3959: * For stepm=1 the results are the same as for previous versions of Imach.
3960: * For stepm > 1 the results are less biased than in previous versions.
3961: */
3962: s1=s[mw[mi][i]][i];
3963: s2=s[mw[mi+1][i]][i];
3964: bbh=(double)bh[mi][i]/(double)stepm;
3965: /* bias bh is positive if real duration
3966: * is higher than the multiple of stepm and negative otherwise.
3967: */
3968: /* lli= (savm[s1][s2]>1.e-8 ?(1.+bbh)*log(out[s1][s2])- bbh*log(savm[s1][s2]):log((1.+bbh)*out[s1][s2]));*/
3969: if( s2 > nlstate){
3970: /* i.e. if s2 is a death state and if the date of death is known
3971: then the contribution to the likelihood is the probability to
3972: die between last step unit time and current step unit time,
3973: which is also equal to probability to die before dh
3974: minus probability to die before dh-stepm .
3975: In version up to 0.92 likelihood was computed
3976: as if date of death was unknown. Death was treated as any other
3977: health state: the date of the interview describes the actual state
3978: and not the date of a change in health state. The former idea was
3979: to consider that at each interview the state was recorded
3980: (healthy, disable or death) and IMaCh was corrected; but when we
3981: introduced the exact date of death then we should have modified
3982: the contribution of an exact death to the likelihood. This new
3983: contribution is smaller and very dependent of the step unit
3984: stepm. It is no more the probability to die between last interview
3985: and month of death but the probability to survive from last
3986: interview up to one month before death multiplied by the
3987: probability to die within a month. Thanks to Chris
3988: Jackson for correcting this bug. Former versions increased
3989: mortality artificially. The bad side is that we add another loop
3990: which slows down the processing. The difference can be up to 10%
3991: lower mortality.
3992: */
3993: /* If, at the beginning of the maximization mostly, the
3994: cumulative probability or probability to be dead is
3995: constant (ie = 1) over time d, the difference is equal to
3996: 0. out[s1][3] = savm[s1][3]: probability, being at state
3997: s1 at precedent wave, to be dead a month before current
3998: wave is equal to probability, being at state s1 at
3999: precedent wave, to be dead at mont of the current
4000: wave. Then the observed probability (that this person died)
4001: is null according to current estimated parameter. In fact,
4002: it should be very low but not zero otherwise the log go to
4003: infinity.
4004: */
4005: /* #ifdef INFINITYORIGINAL */
4006: /* lli=log(out[s1][s2] - savm[s1][s2]); */
4007: /* #else */
4008: /* if ((out[s1][s2] - savm[s1][s2]) < mytinydouble) */
4009: /* lli=log(mytinydouble); */
4010: /* else */
4011: /* lli=log(out[s1][s2] - savm[s1][s2]); */
4012: /* #endif */
4013: lli=log(out[s1][s2] - savm[s1][s2]);
4014:
4015: } else if ( s2==-1 ) { /* alive */
4016: for (j=1,survp=0. ; j<=nlstate; j++)
4017: survp += (1.+bbh)*out[s1][j]- bbh*savm[s1][j];
4018: /*survp += out[s1][j]; */
4019: lli= log(survp);
4020: }
4021: /* else if (s2==-4) { */
4022: /* for (j=3,survp=0. ; j<=nlstate; j++) */
4023: /* survp += (1.+bbh)*out[s1][j]- bbh*savm[s1][j]; */
4024: /* lli= log(survp); */
4025: /* } */
4026: /* else if (s2==-5) { */
4027: /* for (j=1,survp=0. ; j<=2; j++) */
4028: /* survp += (1.+bbh)*out[s1][j]- bbh*savm[s1][j]; */
4029: /* lli= log(survp); */
4030: /* } */
4031: else{
4032: lli= log((1.+bbh)*out[s1][s2]- bbh*savm[s1][s2]); /* linear interpolation */
4033: /* 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 */
4034: }
4035: /*lli=(1.+bbh)*log(out[s1][s2])- bbh*log(savm[s1][s2]);*/
4036: /*if(lli ==000.0)*/
4037: /*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); */
4038: ipmx +=1;
4039: sw += weight[i];
4040: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
4041: /* if (lli < log(mytinydouble)){ */
4042: /* 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); */
4043: /* 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]); */
4044: /* } */
4045: } /* end of wave */
4046: } /* end of individual */
4047: } else if(mle==2){
4048: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
4049: ioffset=2+nagesqr ;
4050: for (k=1; k<=ncovf;k++)
4051: cov[ioffset+TvarFind[k]]=covar[Tvar[TvarFind[k]]][i];
4052: for(mi=1; mi<= wav[i]-1; mi++){
4053: for(k=1; k <= ncovv ; k++){
4054: cov[ioffset+TvarVind[k]]=cotvar[mw[mi][i]][Tvar[TvarVind[k]]-ncovcol-nqv][i];
4055: }
4056: for (ii=1;ii<=nlstate+ndeath;ii++)
4057: for (j=1;j<=nlstate+ndeath;j++){
4058: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
4059: savm[ii][j]=(ii==j ? 1.0 : 0.0);
4060: }
4061: for(d=0; d<=dh[mi][i]; d++){
4062: newm=savm;
4063: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM;
4064: cov[2]=agexact;
4065: if(nagesqr==1)
4066: cov[3]= agexact*agexact;
4067: for (kk=1; kk<=cptcovage;kk++) {
4068: cov[Tage[kk]+2+nagesqr]=covar[Tvar[Tage[kk]]][i]*agexact;
4069: }
4070: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
4071: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
4072: savm=oldm;
4073: oldm=newm;
4074: } /* end mult */
4075:
4076: s1=s[mw[mi][i]][i];
4077: s2=s[mw[mi+1][i]][i];
4078: bbh=(double)bh[mi][i]/(double)stepm;
4079: 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 */
4080: ipmx +=1;
4081: sw += weight[i];
4082: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
4083: } /* end of wave */
4084: } /* end of individual */
4085: } else if(mle==3){ /* exponential inter-extrapolation */
4086: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
4087: for (k=1; k<=cptcovn;k++) cov[2+nagesqr+k]=covar[Tvar[k]][i];
4088: for(mi=1; mi<= wav[i]-1; mi++){
4089: for (ii=1;ii<=nlstate+ndeath;ii++)
4090: for (j=1;j<=nlstate+ndeath;j++){
4091: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
4092: savm[ii][j]=(ii==j ? 1.0 : 0.0);
4093: }
4094: for(d=0; d<dh[mi][i]; d++){
4095: newm=savm;
4096: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM;
4097: cov[2]=agexact;
4098: if(nagesqr==1)
4099: cov[3]= agexact*agexact;
4100: for (kk=1; kk<=cptcovage;kk++) {
4101: cov[Tage[kk]+2+nagesqr]=covar[Tvar[Tage[kk]]][i]*agexact;
4102: }
4103: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
4104: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
4105: savm=oldm;
4106: oldm=newm;
4107: } /* end mult */
4108:
4109: s1=s[mw[mi][i]][i];
4110: s2=s[mw[mi+1][i]][i];
4111: bbh=(double)bh[mi][i]/(double)stepm;
4112: 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 */
4113: ipmx +=1;
4114: sw += weight[i];
4115: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
4116: } /* end of wave */
4117: } /* end of individual */
4118: }else if (mle==4){ /* ml=4 no inter-extrapolation */
4119: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
4120: for (k=1; k<=cptcovn;k++) cov[2+nagesqr+k]=covar[Tvar[k]][i];
4121: for(mi=1; mi<= wav[i]-1; mi++){
4122: for (ii=1;ii<=nlstate+ndeath;ii++)
4123: for (j=1;j<=nlstate+ndeath;j++){
4124: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
4125: savm[ii][j]=(ii==j ? 1.0 : 0.0);
4126: }
4127: for(d=0; d<dh[mi][i]; d++){
4128: newm=savm;
4129: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM;
4130: cov[2]=agexact;
4131: if(nagesqr==1)
4132: cov[3]= agexact*agexact;
4133: for (kk=1; kk<=cptcovage;kk++) {
4134: cov[Tage[kk]+2+nagesqr]=covar[Tvar[Tage[kk]]][i]*agexact;
4135: }
4136:
4137: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
4138: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
4139: savm=oldm;
4140: oldm=newm;
4141: } /* end mult */
4142:
4143: s1=s[mw[mi][i]][i];
4144: s2=s[mw[mi+1][i]][i];
4145: if( s2 > nlstate){
4146: lli=log(out[s1][s2] - savm[s1][s2]);
4147: } else if ( s2==-1 ) { /* alive */
4148: for (j=1,survp=0. ; j<=nlstate; j++)
4149: survp += out[s1][j];
4150: lli= log(survp);
4151: }else{
4152: lli=log(out[s[mw[mi][i]][i]][s[mw[mi+1][i]][i]]); /* Original formula */
4153: }
4154: ipmx +=1;
4155: sw += weight[i];
4156: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
4157: /* 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]); */
4158: } /* end of wave */
4159: } /* end of individual */
4160: }else{ /* ml=5 no inter-extrapolation no jackson =0.8a */
4161: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
4162: for (k=1; k<=cptcovn;k++) cov[2+nagesqr+k]=covar[Tvar[k]][i];
4163: for(mi=1; mi<= wav[i]-1; mi++){
4164: for (ii=1;ii<=nlstate+ndeath;ii++)
4165: for (j=1;j<=nlstate+ndeath;j++){
4166: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
4167: savm[ii][j]=(ii==j ? 1.0 : 0.0);
4168: }
4169: for(d=0; d<dh[mi][i]; d++){
4170: newm=savm;
4171: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM;
4172: cov[2]=agexact;
4173: if(nagesqr==1)
4174: cov[3]= agexact*agexact;
4175: for (kk=1; kk<=cptcovage;kk++) {
4176: cov[Tage[kk]+2+nagesqr]=covar[Tvar[Tage[kk]]][i]*agexact;
4177: }
4178:
4179: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
4180: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
4181: savm=oldm;
4182: oldm=newm;
4183: } /* end mult */
4184:
4185: s1=s[mw[mi][i]][i];
4186: s2=s[mw[mi+1][i]][i];
4187: lli=log(out[s[mw[mi][i]][i]][s[mw[mi+1][i]][i]]); /* Original formula */
4188: ipmx +=1;
4189: sw += weight[i];
4190: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
4191: /*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]);*/
4192: } /* end of wave */
4193: } /* end of individual */
4194: } /* End of if */
4195: for(k=1,l=0.; k<=nlstate; k++) l += ll[k];
4196: /* printf("l1=%f l2=%f ",ll[1],ll[2]); */
4197: l= l*ipmx/sw; /* To get the same order of magnitude as if weight=1 for every body */
4198: return -l;
4199: }
4200:
4201: /*************** log-likelihood *************/
4202: double funcone( double *x)
4203: {
4204: /* Same as func but slower because of a lot of printf and if */
4205: int i, ii, j, k, mi, d, kk, kf=0;
4206: int ioffset=0;
4207: double l, ll[NLSTATEMAX+1], cov[NCOVMAX+1];
4208: double **out;
4209: double lli; /* Individual log likelihood */
4210: double llt;
4211: int s1, s2;
4212: int iv=0, iqv=0, itv=0, iqtv=0 ; /* Index of varying covariate, fixed quantitative cov, time varying covariate, quantitative time varying covariate */
4213:
4214: double bbh, survp;
4215: double agexact;
4216: double agebegin, ageend;
4217: /*extern weight */
4218: /* We are differentiating ll according to initial status */
4219: /* for (i=1;i<=npar;i++) printf("%f ", x[i]);*/
4220: /*for(i=1;i<imx;i++)
4221: printf(" %d\n",s[4][i]);
4222: */
4223: cov[1]=1.;
4224:
4225: for(k=1; k<=nlstate; k++) ll[k]=0.;
4226: ioffset=0;
4227: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
4228: /* Computes the values of the ncovmodel covariates of the model
4229: depending if the covariates are fixed or varying (age dependent) and stores them in cov[]
4230: Then computes with function pmij which return a matrix p[i][j] giving the elementary probability
4231: to be observed in j being in i according to the model.
4232: */
4233: /* ioffset=2+nagesqr+cptcovage; */
4234: ioffset=2+nagesqr;
4235: /* Fixed */
4236: /* for (k=1; k<=cptcovn;k++) cov[2+nagesqr+k]=covar[Tvar[k]][i]; */
4237: /* for (k=1; k<=ncoveff;k++){ /\* Simple and product fixed Dummy covariates without age* products *\/ */
4238: for (kf=1; kf<=ncovf;kf++){ /* Simple and product fixed covariates without age* products *//* Missing values are set to -1 but should be dropped */
4239: cov[ioffset+TvarFind[kf]]=covar[Tvar[TvarFind[kf]]][i];/* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1, only V1 is fixed (k=6)*/
4240: /* cov[ioffset+TvarFind[1]]=covar[Tvar[TvarFind[1]]][i]; */
4241: /* cov[2+6]=covar[Tvar[6]][i]; */
4242: /* cov[2+6]=covar[2][i]; V2 */
4243: /* cov[TvarFind[2]]=covar[Tvar[TvarFind[2]]][i]; */
4244: /* cov[2+7]=covar[Tvar[7]][i]; */
4245: /* cov[2+7]=covar[7][i]; V7=V1*V2 */
4246: /* cov[TvarFind[3]]=covar[Tvar[TvarFind[3]]][i]; */
4247: /* cov[2+9]=covar[Tvar[9]][i]; */
4248: /* cov[2+9]=covar[1][i]; V1 */
4249: }
4250: /* In model V2+V1*V4+age*V3+V3*V2 Tvar[1] is V2, Tvar[2=V1*V4]
4251: is 5, Tvar[3=age*V3] should not be computed because of age Tvar[4=V3*V2]=6
4252: has been calculated etc */
4253: /* For an individual i, wav[i] gives the number of effective waves */
4254: /* We compute the contribution to Likelihood of each effective transition
4255: mw[mi][i] is real wave of the mi th effectve wave */
4256: /* Then statuses are computed at each begin and end of an effective wave s1=s[ mw[mi][i] ][i];
4257: s2=s[mw[mi+1][i]][i];
4258: And the iv th varying covariate is the cotvar[mw[mi+1][i]][iv][i]
4259: But if the variable is not in the model TTvar[iv] is the real variable effective in the model:
4260: meaning that decodemodel should be used cotvar[mw[mi+1][i]][TTvar[iv]][i]
4261: */
4262: /* This part may be useless now because everythin should be in covar */
4263: /* for (k=1; k<=nqfveff;k++){ /\* Simple and product fixed Quantitative covariates without age* products *\/ */
4264: /* 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?)*\/ */
4265: /* } */
4266: /* for(iqv=1; iqv <= nqfveff; iqv++){ /\* Quantitative fixed covariates *\/ */
4267: /* cov[++ioffset]=coqvar[Tvar[iqv]][i]; /\* Only V2 k=6 and V1*V2 7 *\/ */
4268: /* } */
4269:
4270:
4271: for(mi=1; mi<= wav[i]-1; mi++){ /* Varying with waves */
4272: /* Wave varying (but not age varying) */
4273: for(k=1; k <= ncovv ; k++){ /* Varying covariates (single and product but no age )*/
4274: /* cov[ioffset+TvarVind[k]]=cotvar[mw[mi][i]][Tvar[TvarVind[k]]][i]; */
4275: cov[ioffset+TvarVind[k]]=cotvar[mw[mi][i]][Tvar[TvarVind[k]]-ncovcol-nqv][i];
4276: }
4277: /* for(itv=1; itv <= ntveff; itv++){ /\* Varying dummy covariates (single??)*\/ */
4278: /* iv= Tvar[Tmodelind[ioffset-2-nagesqr-cptcovage+itv]]-ncovcol-nqv; /\* Counting the # varying covariate from 1 to ntveff *\/ */
4279: /* cov[ioffset+iv]=cotvar[mw[mi][i]][iv][i]; */
4280: /* k=ioffset-2-nagesqr-cptcovage+itv; /\* position in simple model *\/ */
4281: /* cov[ioffset+itv]=cotvar[mw[mi][i]][TmodelInvind[itv]][i]; */
4282: /* 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]); */
4283: /* for(iqtv=1; iqtv <= nqtveff; iqtv++){ /\* Varying quantitatives covariates *\/ */
4284: /* iv=TmodelInvQind[iqtv]; /\* Counting the # varying covariate from 1 to ntveff *\/ */
4285: /* /\* 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]); *\/ */
4286: /* cov[ioffset+ntveff+iqtv]=cotqvar[mw[mi][i]][TmodelInvQind[iqtv]][i]; */
4287: /* } */
4288: for (ii=1;ii<=nlstate+ndeath;ii++)
4289: for (j=1;j<=nlstate+ndeath;j++){
4290: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
4291: savm[ii][j]=(ii==j ? 1.0 : 0.0);
4292: }
4293:
4294: agebegin=agev[mw[mi][i]][i]; /* Age at beginning of effective wave */
4295: ageend=agev[mw[mi][i]][i] + (dh[mi][i])*stepm/YEARM; /* Age at end of effective wave and at the end of transition */
4296: for(d=0; d<dh[mi][i]; d++){ /* Delay between two effective waves */
4297: /* for(d=0; d<=0; d++){ /\* Delay between two effective waves Only one matrix to speed up*\/ */
4298: /*dh[m][i] or dh[mw[mi][i]][i] is the delay between two effective waves m=mw[mi][i]
4299: and mw[mi+1][i]. dh depends on stepm.*/
4300: newm=savm;
4301: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM; /* Here d is needed */
4302: cov[2]=agexact;
4303: if(nagesqr==1)
4304: cov[3]= agexact*agexact;
4305: for (kk=1; kk<=cptcovage;kk++) {
4306: if(!FixedV[Tvar[Tage[kk]]])
4307: cov[Tage[kk]+2+nagesqr]=covar[Tvar[Tage[kk]]][i]*agexact;
4308: else
4309: cov[Tage[kk]+2+nagesqr]=cotvar[mw[mi][i]][Tvar[Tage[kk]]-ncovcol-nqv][i]*agexact;
4310: }
4311: /* printf("i=%d,mi=%d,d=%d,mw[mi][i]=%d\n",i, mi,d,mw[mi][i]); */
4312: /* savm=pmij(pmmij,cov,ncovmodel,x,nlstate); */
4313: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
4314: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
4315: /* out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath, */
4316: /* 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate)); */
4317: savm=oldm;
4318: oldm=newm;
4319: } /* end mult */
4320: /*lli=log(out[s[mw[mi][i]][i]][s[mw[mi+1][i]][i]]);*/ /* Original formula */
4321: /* But now since version 0.9 we anticipate for bias at large stepm.
4322: * If stepm is larger than one month (smallest stepm) and if the exact delay
4323: * (in months) between two waves is not a multiple of stepm, we rounded to
4324: * the nearest (and in case of equal distance, to the lowest) interval but now
4325: * we keep into memory the bias bh[mi][i] and also the previous matrix product
4326: * (i.e to dh[mi][i]-1) saved in 'savm'. Then we inter(extra)polate the
4327: * probability in order to take into account the bias as a fraction of the way
4328: * from savm to out if bh is negative or even beyond if bh is positive. bh varies
4329: * -stepm/2 to stepm/2 .
4330: * For stepm=1 the results are the same as for previous versions of Imach.
4331: * For stepm > 1 the results are less biased than in previous versions.
4332: */
4333: s1=s[mw[mi][i]][i];
4334: s2=s[mw[mi+1][i]][i];
4335: /* if(s2==-1){ */
4336: /* printf(" ERROR s1=%d, s2=%d i=%d \n", s1, s2, i); */
4337: /* /\* exit(1); *\/ */
4338: /* } */
4339: bbh=(double)bh[mi][i]/(double)stepm;
4340: /* bias is positive if real duration
4341: * is higher than the multiple of stepm and negative otherwise.
4342: */
4343: if( s2 > nlstate && (mle <5) ){ /* Jackson */
4344: lli=log(out[s1][s2] - savm[s1][s2]);
4345: } else if ( s2==-1 ) { /* alive */
4346: for (j=1,survp=0. ; j<=nlstate; j++)
4347: survp += (1.+bbh)*out[s1][j]- bbh*savm[s1][j];
4348: lli= log(survp);
4349: }else if (mle==1){
4350: lli= log((1.+bbh)*out[s1][s2]- bbh*savm[s1][s2]); /* linear interpolation */
4351: } else if(mle==2){
4352: 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 */
4353: } else if(mle==3){ /* exponential inter-extrapolation */
4354: 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 */
4355: } else if (mle==4){ /* mle=4 no inter-extrapolation */
4356: lli=log(out[s1][s2]); /* Original formula */
4357: } else{ /* mle=0 back to 1 */
4358: lli= log((1.+bbh)*out[s1][s2]- bbh*savm[s1][s2]); /* linear interpolation */
4359: /*lli=log(out[s1][s2]); */ /* Original formula */
4360: } /* End of if */
4361: ipmx +=1;
4362: sw += weight[i];
4363: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
4364: /* printf("Funcone 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],(s2==-1? -1: out[s1][s2]),(s2==-1? -1: savm[s1][s2])); */
4365: if(globpr){
4366: fprintf(ficresilk,"%09ld %6.1f %6.1f %6d %2d %2d %2d %2d %3d %15.6f %8.4f %8.3f\
4367: %11.6f %11.6f %11.6f ", \
4368: num[i], agebegin, ageend, i,s1,s2,mi,mw[mi][i],dh[mi][i],exp(lli),weight[i],weight[i]*gipmx/gsw,
4369: 2*weight[i]*lli,(s2==-1? -1: out[s1][s2]),(s2==-1? -1: savm[s1][s2]));
4370: /* printf("%09ld %6.1f %6.1f %6d %2d %2d %2d %2d %3d %15.6f %8.4f %8.3f\ */
4371: /* %11.6f %11.6f %11.6f ", \ */
4372: /* num[i], agebegin, ageend, i,s1,s2,mi,mw[mi][i],dh[mi][i],exp(lli),weight[i],weight[i]*gipmx/gsw, */
4373: /* 2*weight[i]*lli,(s2==-1? -1: out[s1][s2]),(s2==-1? -1: savm[s1][s2])); */
4374: for(k=1,llt=0.,l=0.; k<=nlstate; k++){
4375: llt +=ll[k]*gipmx/gsw;
4376: fprintf(ficresilk," %10.6f",-ll[k]*gipmx/gsw);
4377: /* printf(" %10.6f",-ll[k]*gipmx/gsw); */
4378: }
4379: fprintf(ficresilk," %10.6f\n", -llt);
4380: /* printf(" %10.6f\n", -llt); */
4381: }
4382: } /* end of wave */
4383: } /* end of individual */
4384: for(k=1,l=0.; k<=nlstate; k++) l += ll[k];
4385: /* printf("l1=%f l2=%f ",ll[1],ll[2]); */
4386: l= l*ipmx/sw; /* To get the same order of magnitude as if weight=1 for every body */
4387: if(globpr==0){ /* First time we count the contributions and weights */
4388: gipmx=ipmx;
4389: gsw=sw;
4390: }
4391: return -l;
4392: }
4393:
4394:
4395: /*************** function likelione ***********/
4396: void likelione(FILE *ficres,double p[], int npar, int nlstate, int *globpri, long *ipmx, double *sw, double *fretone, double (*func)(double []))
4397: {
4398: /* This routine should help understanding what is done with
4399: the selection of individuals/waves and
4400: to check the exact contribution to the likelihood.
4401: Plotting could be done.
4402: */
4403: int k;
4404:
4405: if(*globpri !=0){ /* Just counts and sums, no printings */
4406: strcpy(fileresilk,"ILK_");
4407: strcat(fileresilk,fileresu);
4408: if((ficresilk=fopen(fileresilk,"w"))==NULL) {
4409: printf("Problem with resultfile: %s\n", fileresilk);
4410: fprintf(ficlog,"Problem with resultfile: %s\n", fileresilk);
4411: }
4412: 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");
4413: fprintf(ficresilk, "#num_i ageb agend i s1 s2 mi mw dh likeli weight %%weight 2wlli out sav ");
4414: /* i,s1,s2,mi,mw[mi][i],dh[mi][i],exp(lli),weight[i],2*weight[i]*lli,out[s1][s2],savm[s1][s2]); */
4415: for(k=1; k<=nlstate; k++)
4416: fprintf(ficresilk," -2*gipw/gsw*weight*ll[%d]++",k);
4417: fprintf(ficresilk," -2*gipw/gsw*weight*ll(total)\n");
4418: }
4419:
4420: *fretone=(*func)(p);
4421: if(*globpri !=0){
4422: fclose(ficresilk);
4423: if (mle ==0)
4424: fprintf(fichtm,"\n<br>File of contributions to the likelihood computed with initial parameters and mle = %d.",mle);
4425: else if(mle >=1)
4426: fprintf(fichtm,"\n<br>File of contributions to the likelihood computed with optimized parameters mle = %d.",mle);
4427: 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));
4428: fprintf(fichtm,"\n<br>Equation of the model: <b>model=1+age+%s</b><br>\n",model);
4429:
4430: for (k=1; k<= nlstate ; k++) {
4431: 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> \
4432: <img src=\"%s-p%dj.png\">",k,k,subdirf2(optionfilefiname,"ILK_"),k,subdirf2(optionfilefiname,"ILK_"),k,subdirf2(optionfilefiname,"ILK_"),k);
4433: }
4434: 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> \
4435: <img src=\"%s-ori.png\">",subdirf2(optionfilefiname,"ILK_"),subdirf2(optionfilefiname,"ILK_"),subdirf2(optionfilefiname,"ILK_"));
4436: fprintf(fichtm,"<br>- and by state of destination <a href=\"%s-dest.png\">%s-dest.png</a><br> \
4437: <img src=\"%s-dest.png\">",subdirf2(optionfilefiname,"ILK_"),subdirf2(optionfilefiname,"ILK_"),subdirf2(optionfilefiname,"ILK_"));
4438: fflush(fichtm);
4439: }
4440: return;
4441: }
4442:
4443:
4444: /*********** Maximum Likelihood Estimation ***************/
4445:
4446: void mlikeli(FILE *ficres,double p[], int npar, int ncovmodel, int nlstate, double ftol, double (*func)(double []))
4447: {
4448: int i,j,k, jk, jkk=0, iter=0;
4449: double **xi;
4450: double fret;
4451: double fretone; /* Only one call to likelihood */
4452: /* char filerespow[FILENAMELENGTH];*/
4453:
4454: #ifdef NLOPT
4455: int creturn;
4456: nlopt_opt opt;
4457: /* double lb[9] = { -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL }; /\* lower bounds *\/ */
4458: double *lb;
4459: double minf; /* the minimum objective value, upon return */
4460: double * p1; /* Shifted parameters from 0 instead of 1 */
4461: myfunc_data dinst, *d = &dinst;
4462: #endif
4463:
4464:
4465: xi=matrix(1,npar,1,npar);
4466: for (i=1;i<=npar;i++)
4467: for (j=1;j<=npar;j++)
4468: xi[i][j]=(i==j ? 1.0 : 0.0);
4469: printf("Powell\n"); fprintf(ficlog,"Powell\n");
4470: strcpy(filerespow,"POW_");
4471: strcat(filerespow,fileres);
4472: if((ficrespow=fopen(filerespow,"w"))==NULL) {
4473: printf("Problem with resultfile: %s\n", filerespow);
4474: fprintf(ficlog,"Problem with resultfile: %s\n", filerespow);
4475: }
4476: fprintf(ficrespow,"# Powell\n# iter -2*LL");
4477: for (i=1;i<=nlstate;i++)
4478: for(j=1;j<=nlstate+ndeath;j++)
4479: if(j!=i)fprintf(ficrespow," p%1d%1d",i,j);
4480: fprintf(ficrespow,"\n");
4481: #ifdef POWELL
4482: #ifdef LINMINORIGINAL
4483: #else /* LINMINORIGINAL */
4484:
4485: flatdir=ivector(1,npar);
4486: for (j=1;j<=npar;j++) flatdir[j]=0;
4487: #endif /*LINMINORIGINAL */
4488:
4489: #ifdef FLATSUP
4490: powell(p,xi,npar,ftol,&iter,&fret,flatdir,func);
4491: /* reorganizing p by suppressing flat directions */
4492: for(i=1, jk=1; i <=nlstate; i++){
4493: for(k=1; k <=(nlstate+ndeath); k++){
4494: if (k != i) {
4495: printf("%d%d flatdir[%d]=%d",i,k,jk, flatdir[jk]);
4496: if(flatdir[jk]==1){
4497: printf(" To be skipped %d%d flatdir[%d]=%d ",i,k,jk, flatdir[jk]);
4498: }
4499: for(j=1; j <=ncovmodel; j++){
4500: printf("%12.7f ",p[jk]);
4501: jk++;
4502: }
4503: printf("\n");
4504: }
4505: }
4506: }
4507: /* skipping */
4508: /* for(i=1, jk=1, jkk=1;(flatdir[jk]==0)&& (i <=nlstate); i++){ */
4509: for(i=1, jk=1, jkk=1;i <=nlstate; i++){
4510: for(k=1; k <=(nlstate+ndeath); k++){
4511: if (k != i) {
4512: printf("%d%d flatdir[%d]=%d",i,k,jk, flatdir[jk]);
4513: if(flatdir[jk]==1){
4514: printf(" To be skipped %d%d flatdir[%d]=%d jk=%d p[%d] ",i,k,jk, flatdir[jk],jk, jk);
4515: for(j=1; j <=ncovmodel; jk++,j++){
4516: printf(" p[%d]=%12.7f",jk, p[jk]);
4517: /*q[jjk]=p[jk];*/
4518: }
4519: }else{
4520: printf(" To be kept %d%d flatdir[%d]=%d jk=%d q[%d]=p[%d] ",i,k,jk, flatdir[jk],jk, jkk, jk);
4521: for(j=1; j <=ncovmodel; jk++,jkk++,j++){
4522: printf(" p[%d]=%12.7f=q[%d]",jk, p[jk],jkk);
4523: /*q[jjk]=p[jk];*/
4524: }
4525: }
4526: printf("\n");
4527: }
4528: fflush(stdout);
4529: }
4530: }
4531: powell(p,xi,npar,ftol,&iter,&fret,flatdir,func);
4532: #else /* FLATSUP */
4533: powell(p,xi,npar,ftol,&iter,&fret,func);
4534: #endif /* FLATSUP */
4535:
4536: #ifdef LINMINORIGINAL
4537: #else
4538: free_ivector(flatdir,1,npar);
4539: #endif /* LINMINORIGINAL*/
4540: #endif /* POWELL */
4541:
4542: #ifdef NLOPT
4543: #ifdef NEWUOA
4544: opt = nlopt_create(NLOPT_LN_NEWUOA,npar);
4545: #else
4546: opt = nlopt_create(NLOPT_LN_BOBYQA,npar);
4547: #endif
4548: lb=vector(0,npar-1);
4549: for (i=0;i<npar;i++) lb[i]= -HUGE_VAL;
4550: nlopt_set_lower_bounds(opt, lb);
4551: nlopt_set_initial_step1(opt, 0.1);
4552:
4553: p1= (p+1); /* p *(p+1)@8 and p *(p1)@8 are equal p1[0]=p[1] */
4554: d->function = func;
4555: printf(" Func %.12lf \n",myfunc(npar,p1,NULL,d));
4556: nlopt_set_min_objective(opt, myfunc, d);
4557: nlopt_set_xtol_rel(opt, ftol);
4558: if ((creturn=nlopt_optimize(opt, p1, &minf)) < 0) {
4559: printf("nlopt failed! %d\n",creturn);
4560: }
4561: else {
4562: printf("found minimum after %d evaluations (NLOPT=%d)\n", countcallfunc ,NLOPT);
4563: printf("found minimum at f(%g,%g) = %0.10g\n", p[0], p[1], minf);
4564: iter=1; /* not equal */
4565: }
4566: nlopt_destroy(opt);
4567: #endif
4568: #ifdef FLATSUP
4569: /* npared = npar -flatd/ncovmodel; */
4570: /* xired= matrix(1,npared,1,npared); */
4571: /* paramred= ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel); */
4572: /* powell(pred,xired,npared,ftol,&iter,&fret,flatdir,func); */
4573: /* free_matrix(xire,1,npared,1,npared); */
4574: #else /* FLATSUP */
4575: #endif /* FLATSUP */
4576: free_matrix(xi,1,npar,1,npar);
4577: fclose(ficrespow);
4578: printf("\n#Number of iterations & function calls = %d & %d, -2 Log likelihood = %.12f\n",iter, countcallfunc,func(p));
4579: fprintf(ficlog,"\n#Number of iterations & function calls = %d & %d, -2 Log likelihood = %.12f\n",iter, countcallfunc,func(p));
4580: fprintf(ficres,"#Number of iterations & function calls = %d & %d, -2 Log likelihood = %.12f\n",iter, countcallfunc,func(p));
4581:
4582: }
4583:
4584: /**** Computes Hessian and covariance matrix ***/
4585: void hesscov(double **matcov, double **hess, double p[], int npar, double delti[], double ftolhess, double (*func)(double []))
4586: {
4587: double **a,**y,*x,pd;
4588: /* double **hess; */
4589: int i, j;
4590: int *indx;
4591:
4592: double hessii(double p[], double delta, int theta, double delti[],double (*func)(double []),int npar);
4593: double hessij(double p[], double **hess, double delti[], int i, int j,double (*func)(double []),int npar);
4594: void lubksb(double **a, int npar, int *indx, double b[]) ;
4595: void ludcmp(double **a, int npar, int *indx, double *d) ;
4596: double gompertz(double p[]);
4597: /* hess=matrix(1,npar,1,npar); */
4598:
4599: printf("\nCalculation of the hessian matrix. Wait...\n");
4600: fprintf(ficlog,"\nCalculation of the hessian matrix. Wait...\n");
4601: for (i=1;i<=npar;i++){
4602: printf("%d-",i);fflush(stdout);
4603: fprintf(ficlog,"%d-",i);fflush(ficlog);
4604:
4605: hess[i][i]=hessii(p,ftolhess,i,delti,func,npar);
4606:
4607: /* printf(" %f ",p[i]);
4608: printf(" %lf %lf %lf",hess[i][i],ftolhess,delti[i]);*/
4609: }
4610:
4611: for (i=1;i<=npar;i++) {
4612: for (j=1;j<=npar;j++) {
4613: if (j>i) {
4614: printf(".%d-%d",i,j);fflush(stdout);
4615: fprintf(ficlog,".%d-%d",i,j);fflush(ficlog);
4616: hess[i][j]=hessij(p,hess, delti,i,j,func,npar);
4617:
4618: hess[j][i]=hess[i][j];
4619: /*printf(" %lf ",hess[i][j]);*/
4620: }
4621: }
4622: }
4623: printf("\n");
4624: fprintf(ficlog,"\n");
4625:
4626: printf("\nInverting the hessian to get the covariance matrix. Wait...\n");
4627: fprintf(ficlog,"\nInverting the hessian to get the covariance matrix. Wait...\n");
4628:
4629: a=matrix(1,npar,1,npar);
4630: y=matrix(1,npar,1,npar);
4631: x=vector(1,npar);
4632: indx=ivector(1,npar);
4633: for (i=1;i<=npar;i++)
4634: for (j=1;j<=npar;j++) a[i][j]=hess[i][j];
4635: ludcmp(a,npar,indx,&pd);
4636:
4637: for (j=1;j<=npar;j++) {
4638: for (i=1;i<=npar;i++) x[i]=0;
4639: x[j]=1;
4640: lubksb(a,npar,indx,x);
4641: for (i=1;i<=npar;i++){
4642: matcov[i][j]=x[i];
4643: }
4644: }
4645:
4646: printf("\n#Hessian matrix#\n");
4647: fprintf(ficlog,"\n#Hessian matrix#\n");
4648: for (i=1;i<=npar;i++) {
4649: for (j=1;j<=npar;j++) {
4650: printf("%.6e ",hess[i][j]);
4651: fprintf(ficlog,"%.6e ",hess[i][j]);
4652: }
4653: printf("\n");
4654: fprintf(ficlog,"\n");
4655: }
4656:
4657: /* printf("\n#Covariance matrix#\n"); */
4658: /* fprintf(ficlog,"\n#Covariance matrix#\n"); */
4659: /* for (i=1;i<=npar;i++) { */
4660: /* for (j=1;j<=npar;j++) { */
4661: /* printf("%.6e ",matcov[i][j]); */
4662: /* fprintf(ficlog,"%.6e ",matcov[i][j]); */
4663: /* } */
4664: /* printf("\n"); */
4665: /* fprintf(ficlog,"\n"); */
4666: /* } */
4667:
4668: /* Recompute Inverse */
4669: /* for (i=1;i<=npar;i++) */
4670: /* for (j=1;j<=npar;j++) a[i][j]=matcov[i][j]; */
4671: /* ludcmp(a,npar,indx,&pd); */
4672:
4673: /* printf("\n#Hessian matrix recomputed#\n"); */
4674:
4675: /* for (j=1;j<=npar;j++) { */
4676: /* for (i=1;i<=npar;i++) x[i]=0; */
4677: /* x[j]=1; */
4678: /* lubksb(a,npar,indx,x); */
4679: /* for (i=1;i<=npar;i++){ */
4680: /* y[i][j]=x[i]; */
4681: /* printf("%.3e ",y[i][j]); */
4682: /* fprintf(ficlog,"%.3e ",y[i][j]); */
4683: /* } */
4684: /* printf("\n"); */
4685: /* fprintf(ficlog,"\n"); */
4686: /* } */
4687:
4688: /* Verifying the inverse matrix */
4689: #ifdef DEBUGHESS
4690: y=matprod2(y,hess,1,npar,1,npar,1,npar,matcov);
4691:
4692: printf("\n#Verification: multiplying the matrix of covariance by the Hessian matrix, should be unity:#\n");
4693: fprintf(ficlog,"\n#Verification: multiplying the matrix of covariance by the Hessian matrix. Should be unity:#\n");
4694:
4695: for (j=1;j<=npar;j++) {
4696: for (i=1;i<=npar;i++){
4697: printf("%.2f ",y[i][j]);
4698: fprintf(ficlog,"%.2f ",y[i][j]);
4699: }
4700: printf("\n");
4701: fprintf(ficlog,"\n");
4702: }
4703: #endif
4704:
4705: free_matrix(a,1,npar,1,npar);
4706: free_matrix(y,1,npar,1,npar);
4707: free_vector(x,1,npar);
4708: free_ivector(indx,1,npar);
4709: /* free_matrix(hess,1,npar,1,npar); */
4710:
4711:
4712: }
4713:
4714: /*************** hessian matrix ****************/
4715: double hessii(double x[], double delta, int theta, double delti[], double (*func)(double []), int npar)
4716: { /* Around values of x, computes the function func and returns the scales delti and hessian */
4717: int i;
4718: int l=1, lmax=20;
4719: double k1,k2, res, fx;
4720: double p2[MAXPARM+1]; /* identical to x */
4721: double delt=0.0001, delts, nkhi=10.,nkhif=1., khi=1.e-4;
4722: int k=0,kmax=10;
4723: double l1;
4724:
4725: fx=func(x);
4726: for (i=1;i<=npar;i++) p2[i]=x[i];
4727: for(l=0 ; l <=lmax; l++){ /* Enlarging the zone around the Maximum */
4728: l1=pow(10,l);
4729: delts=delt;
4730: for(k=1 ; k <kmax; k=k+1){
4731: delt = delta*(l1*k);
4732: p2[theta]=x[theta] +delt;
4733: k1=func(p2)-fx; /* Might be negative if too close to the theoretical maximum */
4734: p2[theta]=x[theta]-delt;
4735: k2=func(p2)-fx;
4736: /*res= (k1-2.0*fx+k2)/delt/delt; */
4737: res= (k1+k2)/delt/delt/2.; /* Divided by 2 because L and not 2*L */
4738:
4739: #ifdef DEBUGHESSII
4740: 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);
4741: 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);
4742: #endif
4743: /*if(fabs(k1-2.0*fx+k2) <1.e-13){ */
4744: if((k1 <khi/nkhi/2.) || (k2 <khi/nkhi/2.)){
4745: k=kmax;
4746: }
4747: else if((k1 >khi/nkhif) || (k2 >khi/nkhif)){ /* Keeps lastvalue before 3.84/2 KHI2 5% 1d.f. */
4748: k=kmax; l=lmax*10;
4749: }
4750: else if((k1 >khi/nkhi) || (k2 >khi/nkhi)){
4751: delts=delt;
4752: }
4753: } /* End loop k */
4754: }
4755: delti[theta]=delts;
4756: return res;
4757:
4758: }
4759:
4760: double hessij( double x[], double **hess, double delti[], int thetai,int thetaj,double (*func)(double []),int npar)
4761: {
4762: int i;
4763: int l=1, lmax=20;
4764: double k1,k2,k3,k4,res,fx;
4765: double p2[MAXPARM+1];
4766: int k, kmax=1;
4767: double v1, v2, cv12, lc1, lc2;
4768:
4769: int firstime=0;
4770:
4771: fx=func(x);
4772: for (k=1; k<=kmax; k=k+10) {
4773: for (i=1;i<=npar;i++) p2[i]=x[i];
4774: p2[thetai]=x[thetai]+delti[thetai]*k;
4775: p2[thetaj]=x[thetaj]+delti[thetaj]*k;
4776: k1=func(p2)-fx;
4777:
4778: p2[thetai]=x[thetai]+delti[thetai]*k;
4779: p2[thetaj]=x[thetaj]-delti[thetaj]*k;
4780: k2=func(p2)-fx;
4781:
4782: p2[thetai]=x[thetai]-delti[thetai]*k;
4783: p2[thetaj]=x[thetaj]+delti[thetaj]*k;
4784: k3=func(p2)-fx;
4785:
4786: p2[thetai]=x[thetai]-delti[thetai]*k;
4787: p2[thetaj]=x[thetaj]-delti[thetaj]*k;
4788: k4=func(p2)-fx;
4789: res=(k1-k2-k3+k4)/4.0/delti[thetai]/k/delti[thetaj]/k/2.; /* Because of L not 2*L */
4790: if(k1*k2*k3*k4 <0.){
4791: firstime=1;
4792: kmax=kmax+10;
4793: }
4794: if(kmax >=10 || firstime ==1){
4795: printf("Warning: directions %d-%d, you are not estimating the Hessian at the exact maximum likelihood; you could increase ftol=%.2e\n",thetai,thetaj, ftol);
4796: fprintf(ficlog,"Warning: directions %d-%d, you are not estimating the Hessian at the exact maximum likelihood; you could increase ftol=%.2e\n",thetai,thetaj, ftol);
4797: 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);
4798: 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);
4799: }
4800: #ifdef DEBUGHESSIJ
4801: v1=hess[thetai][thetai];
4802: v2=hess[thetaj][thetaj];
4803: cv12=res;
4804: /* Computing eigen value of Hessian matrix */
4805: lc1=((v1+v2)+sqrt((v1+v2)*(v1+v2) - 4*(v1*v2-cv12*cv12)))/2.;
4806: lc2=((v1+v2)-sqrt((v1+v2)*(v1+v2) - 4*(v1*v2-cv12*cv12)))/2.;
4807: if ((lc2 <0) || (lc1 <0) ){
4808: printf("Warning: sub Hessian matrix '%d%d' does not have positive eigen values \n",thetai,thetaj);
4809: fprintf(ficlog, "Warning: sub Hessian matrix '%d%d' does not have positive eigen values \n",thetai,thetaj);
4810: 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);
4811: 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);
4812: }
4813: #endif
4814: }
4815: return res;
4816: }
4817:
4818: /* Not done yet: Was supposed to fix if not exactly at the maximum */
4819: /* double hessij( double x[], double delti[], int thetai,int thetaj,double (*func)(double []),int npar) */
4820: /* { */
4821: /* int i; */
4822: /* int l=1, lmax=20; */
4823: /* double k1,k2,k3,k4,res,fx; */
4824: /* double p2[MAXPARM+1]; */
4825: /* double delt=0.0001, delts, nkhi=10.,nkhif=1., khi=1.e-4; */
4826: /* int k=0,kmax=10; */
4827: /* double l1; */
4828:
4829: /* fx=func(x); */
4830: /* for(l=0 ; l <=lmax; l++){ /\* Enlarging the zone around the Maximum *\/ */
4831: /* l1=pow(10,l); */
4832: /* delts=delt; */
4833: /* for(k=1 ; k <kmax; k=k+1){ */
4834: /* delt = delti*(l1*k); */
4835: /* for (i=1;i<=npar;i++) p2[i]=x[i]; */
4836: /* p2[thetai]=x[thetai]+delti[thetai]/k; */
4837: /* p2[thetaj]=x[thetaj]+delti[thetaj]/k; */
4838: /* k1=func(p2)-fx; */
4839:
4840: /* p2[thetai]=x[thetai]+delti[thetai]/k; */
4841: /* p2[thetaj]=x[thetaj]-delti[thetaj]/k; */
4842: /* k2=func(p2)-fx; */
4843:
4844: /* p2[thetai]=x[thetai]-delti[thetai]/k; */
4845: /* p2[thetaj]=x[thetaj]+delti[thetaj]/k; */
4846: /* k3=func(p2)-fx; */
4847:
4848: /* p2[thetai]=x[thetai]-delti[thetai]/k; */
4849: /* p2[thetaj]=x[thetaj]-delti[thetaj]/k; */
4850: /* k4=func(p2)-fx; */
4851: /* res=(k1-k2-k3+k4)/4.0/delti[thetai]*k/delti[thetaj]*k/2.; /\* Because of L not 2*L *\/ */
4852: /* #ifdef DEBUGHESSIJ */
4853: /* 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); */
4854: /* 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); */
4855: /* #endif */
4856: /* if((k1 <khi/nkhi/2.) || (k2 <khi/nkhi/2.)|| (k4 <khi/nkhi/2.)|| (k4 <khi/nkhi/2.)){ */
4857: /* k=kmax; */
4858: /* } */
4859: /* else if((k1 >khi/nkhif) || (k2 >khi/nkhif) || (k4 >khi/nkhif) || (k4 >khi/nkhif)){ /\* Keeps lastvalue before 3.84/2 KHI2 5% 1d.f. *\/ */
4860: /* k=kmax; l=lmax*10; */
4861: /* } */
4862: /* else if((k1 >khi/nkhi) || (k2 >khi/nkhi)){ */
4863: /* delts=delt; */
4864: /* } */
4865: /* } /\* End loop k *\/ */
4866: /* } */
4867: /* delti[theta]=delts; */
4868: /* return res; */
4869: /* } */
4870:
4871:
4872: /************** Inverse of matrix **************/
4873: void ludcmp(double **a, int n, int *indx, double *d)
4874: {
4875: int i,imax,j,k;
4876: double big,dum,sum,temp;
4877: double *vv;
4878:
4879: vv=vector(1,n);
4880: *d=1.0;
4881: for (i=1;i<=n;i++) {
4882: big=0.0;
4883: for (j=1;j<=n;j++)
4884: if ((temp=fabs(a[i][j])) > big) big=temp;
4885: if (big == 0.0){
4886: printf(" Singular Hessian matrix at row %d:\n",i);
4887: for (j=1;j<=n;j++) {
4888: printf(" a[%d][%d]=%f,",i,j,a[i][j]);
4889: fprintf(ficlog," a[%d][%d]=%f,",i,j,a[i][j]);
4890: }
4891: fflush(ficlog);
4892: fclose(ficlog);
4893: nrerror("Singular matrix in routine ludcmp");
4894: }
4895: vv[i]=1.0/big;
4896: }
4897: for (j=1;j<=n;j++) {
4898: for (i=1;i<j;i++) {
4899: sum=a[i][j];
4900: for (k=1;k<i;k++) sum -= a[i][k]*a[k][j];
4901: a[i][j]=sum;
4902: }
4903: big=0.0;
4904: for (i=j;i<=n;i++) {
4905: sum=a[i][j];
4906: for (k=1;k<j;k++)
4907: sum -= a[i][k]*a[k][j];
4908: a[i][j]=sum;
4909: if ( (dum=vv[i]*fabs(sum)) >= big) {
4910: big=dum;
4911: imax=i;
4912: }
4913: }
4914: if (j != imax) {
4915: for (k=1;k<=n;k++) {
4916: dum=a[imax][k];
4917: a[imax][k]=a[j][k];
4918: a[j][k]=dum;
4919: }
4920: *d = -(*d);
4921: vv[imax]=vv[j];
4922: }
4923: indx[j]=imax;
4924: if (a[j][j] == 0.0) a[j][j]=TINY;
4925: if (j != n) {
4926: dum=1.0/(a[j][j]);
4927: for (i=j+1;i<=n;i++) a[i][j] *= dum;
4928: }
4929: }
4930: free_vector(vv,1,n); /* Doesn't work */
4931: ;
4932: }
4933:
4934: void lubksb(double **a, int n, int *indx, double b[])
4935: {
4936: int i,ii=0,ip,j;
4937: double sum;
4938:
4939: for (i=1;i<=n;i++) {
4940: ip=indx[i];
4941: sum=b[ip];
4942: b[ip]=b[i];
4943: if (ii)
4944: for (j=ii;j<=i-1;j++) sum -= a[i][j]*b[j];
4945: else if (sum) ii=i;
4946: b[i]=sum;
4947: }
4948: for (i=n;i>=1;i--) {
4949: sum=b[i];
4950: for (j=i+1;j<=n;j++) sum -= a[i][j]*b[j];
4951: b[i]=sum/a[i][i];
4952: }
4953: }
4954:
4955: void pstamp(FILE *fichier)
4956: {
4957: fprintf(fichier,"# %s.%s\n#IMaCh version %s, %s\n#%s\n# %s", optionfilefiname,optionfilext,version,copyright, fullversion, strstart);
4958: }
4959:
4960: void date2dmy(double date,double *day, double *month, double *year){
4961: double yp=0., yp1=0., yp2=0.;
4962:
4963: yp1=modf(date,&yp);/* extracts integral of date in yp and
4964: fractional in yp1 */
4965: *year=yp;
4966: yp2=modf((yp1*12),&yp);
4967: *month=yp;
4968: yp1=modf((yp2*30.5),&yp);
4969: *day=yp;
4970: if(*day==0) *day=1;
4971: if(*month==0) *month=1;
4972: }
4973:
4974:
4975:
4976: /************ Frequencies ********************/
4977: void freqsummary(char fileres[], double p[], double pstart[], int iagemin, int iagemax, int **s, double **agev, int nlstate, int imx, \
4978: int *Tvaraff, int *invalidvarcomb, int **nbcode, int *ncodemax,double **mint,double **anint, char strstart[], \
4979: int firstpass, int lastpass, int stepm, int weightopt, char model[])
4980: { /* Some frequencies as well as proposing some starting values */
4981: /* Frequencies of any combination of dummy covariate used in the model equation */
4982: int i, m, jk, j1, bool, z1,j, nj, nl, k, iv, jj=0, s1=1, s2=1;
4983: int iind=0, iage=0;
4984: int mi; /* Effective wave */
4985: int first;
4986: double ***freq; /* Frequencies */
4987: double *x, *y, a=0.,b=0.,r=1., sa=0., sb=0.; /* for regression, y=b+m*x and r is the correlation coefficient */
4988: int no=0, linreg(int ifi, int ila, int *no, const double x[], const double y[], double* a, double* b, double* r, double* sa, double * sb);
4989: double *meanq, *stdq, *idq;
4990: double **meanqt;
4991: double *pp, **prop, *posprop, *pospropt;
4992: double pos=0., posproptt=0., pospropta=0., k2, dateintsum=0,k2cpt=0;
4993: char fileresp[FILENAMELENGTH], fileresphtm[FILENAMELENGTH], fileresphtmfr[FILENAMELENGTH];
4994: double agebegin, ageend;
4995:
4996: pp=vector(1,nlstate);
4997: prop=matrix(1,nlstate,iagemin-AGEMARGE,iagemax+4+AGEMARGE);
4998: posprop=vector(1,nlstate); /* Counting the number of transition starting from a live state per age */
4999: pospropt=vector(1,nlstate); /* Counting the number of transition starting from a live state */
5000: /* prop=matrix(1,nlstate,iagemin,iagemax+3); */
5001: meanq=vector(1,nqfveff); /* Number of Quantitative Fixed Variables Effective */
5002: stdq=vector(1,nqfveff); /* Number of Quantitative Fixed Variables Effective */
5003: idq=vector(1,nqfveff); /* Number of Quantitative Fixed Variables Effective */
5004: meanqt=matrix(1,lastpass,1,nqtveff);
5005: strcpy(fileresp,"P_");
5006: strcat(fileresp,fileresu);
5007: /*strcat(fileresphtm,fileresu);*/
5008: if((ficresp=fopen(fileresp,"w"))==NULL) {
5009: printf("Problem with prevalence resultfile: %s\n", fileresp);
5010: fprintf(ficlog,"Problem with prevalence resultfile: %s\n", fileresp);
5011: exit(0);
5012: }
5013:
5014: strcpy(fileresphtm,subdirfext(optionfilefiname,"PHTM_",".htm"));
5015: if((ficresphtm=fopen(fileresphtm,"w"))==NULL) {
5016: printf("Problem with prevalence HTM resultfile '%s' with errno='%s'\n",fileresphtm,strerror(errno));
5017: fprintf(ficlog,"Problem with prevalence HTM resultfile '%s' with errno='%s'\n",fileresphtm,strerror(errno));
5018: fflush(ficlog);
5019: exit(70);
5020: }
5021: else{
5022: fprintf(ficresphtm,"<html><head>\n<title>IMaCh PHTM_ %s</title></head>\n <body><font size=\"2\">%s <br> %s</font> \
5023: <hr size=\"2\" color=\"#EC5E5E\"> \n \
5024: Title=%s <br>Datafile=%s Firstpass=%d Lastpass=%d Stepm=%d Weight=%d Model=1+age+%s<br>\n",\
5025: fileresphtm,version,fullversion,title,datafile,firstpass,lastpass,stepm, weightopt, model);
5026: }
5027: fprintf(ficresphtm,"Current page is file <a href=\"%s\">%s</a><br>\n\n<h4>Frequencies (weight=%d) and prevalence by age at begin of transition and dummy covariate value at beginning of transition</h4>\n",fileresphtm, fileresphtm, weightopt);
5028:
5029: strcpy(fileresphtmfr,subdirfext(optionfilefiname,"PHTMFR_",".htm"));
5030: if((ficresphtmfr=fopen(fileresphtmfr,"w"))==NULL) {
5031: printf("Problem with frequency table HTM resultfile '%s' with errno='%s'\n",fileresphtmfr,strerror(errno));
5032: fprintf(ficlog,"Problem with frequency table HTM resultfile '%s' with errno='%s'\n",fileresphtmfr,strerror(errno));
5033: fflush(ficlog);
5034: exit(70);
5035: } else{
5036: fprintf(ficresphtmfr,"<html><head>\n<title>IMaCh PHTM_Frequency table %s</title></head>\n <body><font size=\"2\">%s <br> %s</font> \
5037: ,<hr size=\"2\" color=\"#EC5E5E\"> \n \
5038: Title=%s <br>Datafile=%s Firstpass=%d Lastpass=%d Stepm=%d Weight=%d Model=1+age+%s<br>\n",\
5039: fileresphtmfr,version,fullversion,title,datafile,firstpass,lastpass,stepm, weightopt, model);
5040: }
5041: fprintf(ficresphtmfr,"Current page is file <a href=\"%s\">%s</a><br>\n\n<h4>(weight=%d) frequencies of all effective transitions of the model, by age at begin of transition, and covariate value at the begin of transition (if the covariate is a varying covariate) </h4>Unknown status is -1<br/>\n",fileresphtmfr, fileresphtmfr,weightopt);
5042:
5043: y= vector(iagemin-AGEMARGE,iagemax+4+AGEMARGE);
5044: x= vector(iagemin-AGEMARGE,iagemax+4+AGEMARGE);
5045: freq= ma3x(-5,nlstate+ndeath,-5,nlstate+ndeath,iagemin-AGEMARGE,iagemax+4+AGEMARGE);
5046: j1=0;
5047:
5048: /* j=ncoveff; /\* Only fixed dummy covariates *\/ */
5049: j=cptcoveff; /* Only simple dummy covariates used in the model */
5050: /* j=cptcovn; /\* Only dummy covariates of the model *\/ */
5051: if (cptcovn<1) {j=1;ncodemax[1]=1;}
5052:
5053:
5054: /* Detects if a combination j1 is empty: for a multinomial variable like 3 education levels:
5055: reference=low_education V1=0,V2=0
5056: med_educ V1=1 V2=0,
5057: high_educ V1=0 V2=1
5058: Then V1=1 and V2=1 is a noisy combination that we want to exclude for the list 2**cptcovn
5059: */
5060: dateintsum=0;
5061: k2cpt=0;
5062:
5063: if(cptcoveff == 0 )
5064: nl=1; /* Constant and age model only */
5065: else
5066: nl=2;
5067:
5068: /* if a constant only model, one pass to compute frequency tables and to write it on ficresp */
5069: /* Loop on nj=1 or 2 if dummy covariates j!=0
5070: * Loop on j1(1 to 2**cptcoveff) covariate combination
5071: * freq[s1][s2][iage] =0.
5072: * Loop on iind
5073: * ++freq[s1][s2][iage] weighted
5074: * end iind
5075: * if covariate and j!0
5076: * headers Variable on one line
5077: * endif cov j!=0
5078: * header of frequency table by age
5079: * Loop on age
5080: * pp[s1]+=freq[s1][s2][iage] weighted
5081: * pos+=freq[s1][s2][iage] weighted
5082: * Loop on s1 initial state
5083: * fprintf(ficresp
5084: * end s1
5085: * end age
5086: * if j!=0 computes starting values
5087: * end compute starting values
5088: * end j1
5089: * end nl
5090: */
5091: for (nj = 1; nj <= nl; nj++){ /* nj= 1 constant model, nl number of loops. */
5092: if(nj==1)
5093: j=0; /* First pass for the constant */
5094: else{
5095: j=cptcoveff; /* Other passes for the covariate values number of simple covariates in the model V2+V1 =2 (simple dummy fixed or time varying) */
5096: }
5097: first=1;
5098: for (j1 = 1; j1 <= (int) pow(2,j); j1++){ /* Loop on all dummy covariates combination of the model, ie excluding quantitatives, V4=0, V3=0 for example, fixed or varying covariates */
5099: posproptt=0.;
5100: /*printf("cptcovn=%d Tvaraff=%d", cptcovn,Tvaraff[1]);
5101: scanf("%d", i);*/
5102: for (i=-5; i<=nlstate+ndeath; i++)
5103: for (s2=-5; s2<=nlstate+ndeath; s2++)
5104: for(m=iagemin; m <= iagemax+3; m++)
5105: freq[i][s2][m]=0;
5106:
5107: for (i=1; i<=nlstate; i++) {
5108: for(m=iagemin; m <= iagemax+3; m++)
5109: prop[i][m]=0;
5110: posprop[i]=0;
5111: pospropt[i]=0;
5112: }
5113: for (z1=1; z1<= nqfveff; z1++) { /* zeroing for each combination j1 as well as for the total */
5114: idq[z1]=0.;
5115: meanq[z1]=0.;
5116: stdq[z1]=0.;
5117: }
5118: /* for (z1=1; z1<= nqtveff; z1++) { */
5119: /* for(m=1;m<=lastpass;m++){ */
5120: /* meanqt[m][z1]=0.; */
5121: /* } */
5122: /* } */
5123: /* dateintsum=0; */
5124: /* k2cpt=0; */
5125:
5126: /* For that combination of covariates j1 (V4=1 V3=0 for example), we count and print the frequencies in one pass */
5127: for (iind=1; iind<=imx; iind++) { /* For each individual iind */
5128: bool=1;
5129: if(j !=0){
5130: if(anyvaryingduminmodel==0){ /* If All fixed covariates */
5131: if (cptcoveff >0) { /* Filter is here: Must be looked at for model=V1+V2+V3+V4 */
5132: for (z1=1; z1<=cptcoveff; z1++) { /* loops on covariates in the model */
5133: /* if(Tvaraff[z1] ==-20){ */
5134: /* /\* sumnew+=cotvar[mw[mi][iind]][z1][iind]; *\/ */
5135: /* }else if(Tvaraff[z1] ==-10){ */
5136: /* /\* sumnew+=coqvar[z1][iind]; *\/ */
5137: /* }else */ /* TODO TODO codtabm(j1,z1) or codtabm(j1,Tvaraff[z1]]z1)*/
5138: /* if( iind >=imx-3) printf("Searching error iind=%d Tvaraff[z1]=%d covar[Tvaraff[z1]][iind]=%.f TnsdVar[Tvaraff[z1]]=%d, cptcoveff=%d, cptcovs=%d \n",iind, Tvaraff[z1], covar[Tvaraff[z1]][iind],TnsdVar[Tvaraff[z1]],cptcoveff, cptcovs); */
5139: if(Tvaraff[z1]<1 || Tvaraff[z1]>=NCOVMAX)
5140: printf("Error Tvaraff[z1]=%d<1 or >=%d, cptcoveff=%d model=1+age+%s\n",Tvaraff[z1],NCOVMAX, cptcoveff, model);
5141: if (covar[Tvaraff[z1]][iind]!= nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]){ /* for combination j1 of covariates */
5142: /* Tests if the value of the covariate z1 for this individual iind responded to combination j1 (V4=1 V3=0) */
5143: bool=0; /* bool should be equal to 1 to be selected, one covariate value failed */
5144: /* 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", */
5145: /* bool,i,z1, z1, Tvaraff[z1],i,covar[Tvaraff[z1]][i],j1,z1,codtabm(j1,z1),*/
5146: /* j1,z1,nbcode[Tvaraff[z1]][codtabm(j1,z1)],j1);*/
5147: /* For j1=7 in V1+V2+V3+V4 = 0 1 1 0 and codtabm(7,3)=1 and nbcde[3][?]=1*/
5148: } /* Onlyf fixed */
5149: } /* end z1 */
5150: } /* cptcoveff > 0 */
5151: } /* end any */
5152: }/* end j==0 */
5153: if (bool==1){ /* We selected an individual iind satisfying combination j1 (V4=1 V3=0) or all fixed covariates */
5154: /* for(m=firstpass; m<=lastpass; m++){ */
5155: for(mi=1; mi<wav[iind];mi++){ /* For each wave */
5156: m=mw[mi][iind];
5157: if(j!=0){
5158: if(anyvaryingduminmodel==1){ /* Some are varying covariates */
5159: for (z1=1; z1<=cptcoveff; z1++) {
5160: if( Fixed[Tmodelind[z1]]==1){
5161: iv= Tvar[Tmodelind[z1]]-ncovcol-nqv;
5162: if (cotvar[m][iv][iind]!= nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]) /* iv=1 to ntv, right modality. If covariate's
5163: value is -1, we don't select. It differs from the
5164: constant and age model which counts them. */
5165: bool=0; /* not selected */
5166: }else if( Fixed[Tmodelind[z1]]== 0) { /* fixed */
5167: /* i1=Tvaraff[z1]; */
5168: /* i2=TnsdVar[i1]; */
5169: /* i3=nbcode[i1][i2]; */
5170: /* i4=covar[i1][iind]; */
5171: /* if(i4 != i3){ */
5172: if (covar[Tvaraff[z1]][iind]!= nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]) { /* Bug valgrind */
5173: bool=0;
5174: }
5175: }
5176: }
5177: }/* Some are varying covariates, we tried to speed up if all fixed covariates in the model, avoiding waves loop */
5178: } /* end j==0 */
5179: /* bool =0 we keep that guy which corresponds to the combination of dummy values */
5180: if(bool==1){ /*Selected */
5181: /* dh[m][iind] or dh[mw[mi][iind]][iind] is the delay between two effective (mi) waves m=mw[mi][iind]
5182: and mw[mi+1][iind]. dh depends on stepm. */
5183: agebegin=agev[m][iind]; /* Age at beginning of wave before transition*/
5184: ageend=agev[m][iind]+(dh[m][iind])*stepm/YEARM; /* Age at end of wave and transition */
5185: if(m >=firstpass && m <=lastpass){
5186: k2=anint[m][iind]+(mint[m][iind]/12.);
5187: /*if ((k2>=dateprev1) && (k2<=dateprev2)) {*/
5188: if(agev[m][iind]==0) agev[m][iind]=iagemax+1; /* All ages equal to 0 are in iagemax+1 */
5189: if(agev[m][iind]==1) agev[m][iind]=iagemax+2; /* All ages equal to 1 are in iagemax+2 */
5190: if (s[m][iind]>0 && s[m][iind]<=nlstate) /* If status at wave m is known and a live state */
5191: prop[s[m][iind]][(int)agev[m][iind]] += weight[iind]; /* At age of beginning of transition, where status is known */
5192: if (m<lastpass) {
5193: /* if(s[m][iind]==4 && s[m+1][iind]==4) */
5194: /* 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]); */
5195: if(s[m][iind]==-1)
5196: 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.));
5197: freq[s[m][iind]][s[m+1][iind]][(int)agev[m][iind]] += weight[iind]; /* At age of beginning of transition, where status is known */
5198: for (z1=1; z1<= nqfveff; z1++) { /* Quantitative variables, calculating mean on known values only */
5199: if(!isnan(covar[ncovcol+z1][iind])){
5200: idq[z1]=idq[z1]+weight[iind];
5201: meanq[z1]+=covar[ncovcol+z1][iind]*weight[iind]; /* Computes mean of quantitative with selected filter */
5202: /* stdq[z1]+=covar[ncovcol+z1][iind]*covar[ncovcol+z1][iind]*weight[iind]*weight[iind]; *//*error*/
5203: stdq[z1]+=covar[ncovcol+z1][iind]*covar[ncovcol+z1][iind]*weight[iind]; /* *weight[iind];*/ /* Computes mean of quantitative with selected filter */
5204: }
5205: }
5206: /* if((int)agev[m][iind] == 55) */
5207: /* printf("j=%d, j1=%d Age %d, iind=%d, num=%09ld m=%d\n",j,j1,(int)agev[m][iind],iind, num[iind],m); */
5208: /* freq[s[m][iind]][s[m+1][iind]][(int)((agebegin+ageend)/2.)] += weight[iind]; */
5209: 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 */
5210: }
5211: } /* end if between passes */
5212: if ((agev[m][iind]>1) && (agev[m][iind]< (iagemax+3)) && (anint[m][iind]!=9999) && (mint[m][iind]!=99) && (j==0)) {
5213: dateintsum=dateintsum+k2; /* on all covariates ?*/
5214: k2cpt++;
5215: /* printf("iind=%ld dateintmean = %lf dateintsum=%lf k2cpt=%lf k2=%lf\n",iind, dateintsum/k2cpt, dateintsum,k2cpt, k2); */
5216: }
5217: }else{
5218: bool=1;
5219: }/* end bool 2 */
5220: } /* end m */
5221: /* for (z1=1; z1<= nqfveff; z1++) { /\* Quantitative variables, calculating mean *\/ */
5222: /* idq[z1]=idq[z1]+weight[iind]; */
5223: /* meanq[z1]+=covar[ncovcol+z1][iind]*weight[iind]; /\* Computes mean of quantitative with selected filter *\/ */
5224: /* stdq[z1]+=covar[ncovcol+z1][iind]*covar[ncovcol+z1][iind]*weight[iind]*weight[iind]; /\* *weight[iind];*\/ /\* Computes mean of quantitative with selected filter *\/ */
5225: /* } */
5226: } /* end bool */
5227: } /* end iind = 1 to imx */
5228: /* prop[s][age] is fed for any initial and valid live state as well as
5229: freq[s1][s2][age] at single age of beginning the transition, for a combination j1 */
5230:
5231:
5232: /* fprintf(ficresp, "#Count between %.lf/%.lf/%.lf and %.lf/%.lf/%.lf\n",jprev1, mprev1,anprev1,jprev2, mprev2,anprev2);*/
5233: if(cptcoveff==0 && nj==1) /* no covariate and first pass */
5234: pstamp(ficresp);
5235: if (cptcoveff>0 && j!=0){
5236: pstamp(ficresp);
5237: printf( "\n#********** Variable ");
5238: fprintf(ficresp, "\n#********** Variable ");
5239: fprintf(ficresphtm, "\n<br/><br/><h3>********** Variable ");
5240: fprintf(ficresphtmfr, "\n<br/><br/><h3>********** Variable ");
5241: fprintf(ficlog, "\n#********** Variable ");
5242: for (z1=1; z1<=cptcovs; z1++){
5243: if(!FixedV[Tvaraff[z1]]){
5244: printf( "V%d(fixed)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]);
5245: fprintf(ficresp, "V%d(fixed)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]);
5246: fprintf(ficresphtm, "V%d(fixed)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]);
5247: fprintf(ficresphtmfr, "V%d(fixed)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]);
5248: fprintf(ficlog, "V%d(fixed)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]);
5249: }else{
5250: printf( "V%d(varying)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]);
5251: fprintf(ficresp, "V%d(varying)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]);
5252: fprintf(ficresphtm, "V%d(varying)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]);
5253: fprintf(ficresphtmfr, "V%d(varying)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]);
5254: fprintf(ficlog, "V%d(varying)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]);
5255: }
5256: }
5257: printf( "**********\n#");
5258: fprintf(ficresp, "**********\n#");
5259: fprintf(ficresphtm, "**********</h3>\n");
5260: fprintf(ficresphtmfr, "**********</h3>\n");
5261: fprintf(ficlog, "**********\n");
5262: }
5263: /*
5264: Printing means of quantitative variables if any
5265: */
5266: for (z1=1; z1<= nqfveff; z1++) {
5267: fprintf(ficlog,"Mean of fixed quantitative variable V%d on %.3g (weighted) individuals sum=%f", ncovcol+z1, idq[z1], meanq[z1]);
5268: fprintf(ficlog,", mean=%.3g\n",meanq[z1]/idq[z1]);
5269: if(weightopt==1){
5270: printf(" Weighted mean and standard deviation of");
5271: fprintf(ficlog," Weighted mean and standard deviation of");
5272: fprintf(ficresphtmfr," Weighted mean and standard deviation of");
5273: }
5274: /* mu = \frac{w x}{\sum w}
5275: var = \frac{\sum w (x-mu)^2}{\sum w} = \frac{w x^2}{\sum w} - mu^2
5276: */
5277: printf(" fixed quantitative variable V%d on %.3g (weighted) representatives of the population : %8.5g (%8.5g)\n", ncovcol+z1, idq[z1],meanq[z1]/idq[z1], sqrt(stdq[z1]/idq[z1]-meanq[z1]*meanq[z1]/idq[z1]/idq[z1]));
5278: fprintf(ficlog," fixed quantitative variable V%d on %.3g (weighted) representatives of the population : %8.5g (%8.5g)\n", ncovcol+z1, idq[z1],meanq[z1]/idq[z1], sqrt(stdq[z1]/idq[z1]-meanq[z1]*meanq[z1]/idq[z1]/idq[z1]));
5279: fprintf(ficresphtmfr," fixed quantitative variable V%d on %.3g (weighted) representatives of the population : %8.5g (%8.5g)<p>\n", ncovcol+z1, idq[z1],meanq[z1]/idq[z1], sqrt(stdq[z1]/idq[z1]-meanq[z1]*meanq[z1]/idq[z1]/idq[z1]));
5280: }
5281: /* for (z1=1; z1<= nqtveff; z1++) { */
5282: /* for(m=1;m<=lastpass;m++){ */
5283: /* fprintf(ficresphtmfr,"V quantitative id %d, pass id=%d, mean=%f<p>\n", z1, m, meanqt[m][z1]); */
5284: /* } */
5285: /* } */
5286:
5287: fprintf(ficresphtm,"<table style=\"text-align:center; border: 1px solid\">");
5288: if((cptcoveff==0 && nj==1)|| nj==2 ) /* no covariate and first pass */
5289: fprintf(ficresp, " Age");
5290: if(nj==2) for (z1=1; z1<=cptcoveff; z1++) {
5291: printf(" V%d=%d, z1=%d, Tvaraff[z1]=%d, j1=%d, TnsdVar[Tvaraff[%d]]=%d |",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])], z1, Tvaraff[z1], j1,z1,TnsdVar[Tvaraff[z1]]);
5292: fprintf(ficresp, " V%d=%d",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]);
5293: }
5294: for(i=1; i<=nlstate;i++) {
5295: if((cptcoveff==0 && nj==1)|| nj==2 ) fprintf(ficresp," Prev(%d) N(%d) N ",i,i);
5296: fprintf(ficresphtm, "<th>Age</th><th>Prev(%d)</th><th>N(%d)</th><th>N</th>",i,i);
5297: }
5298: if((cptcoveff==0 && nj==1)|| nj==2 ) fprintf(ficresp, "\n");
5299: fprintf(ficresphtm, "\n");
5300:
5301: /* Header of frequency table by age */
5302: fprintf(ficresphtmfr,"<table style=\"text-align:center; border: 1px solid\">");
5303: fprintf(ficresphtmfr,"<th>Age</th> ");
5304: for(s2=-1; s2 <=nlstate+ndeath; s2++){
5305: for(m=-1; m <=nlstate+ndeath; m++){
5306: if(s2!=0 && m!=0)
5307: fprintf(ficresphtmfr,"<th>%d%d</th> ",s2,m);
5308: }
5309: }
5310: fprintf(ficresphtmfr, "\n");
5311:
5312: /* For each age */
5313: for(iage=iagemin; iage <= iagemax+3; iage++){
5314: fprintf(ficresphtm,"<tr>");
5315: if(iage==iagemax+1){
5316: fprintf(ficlog,"1");
5317: fprintf(ficresphtmfr,"<tr><th>0</th> ");
5318: }else if(iage==iagemax+2){
5319: fprintf(ficlog,"0");
5320: fprintf(ficresphtmfr,"<tr><th>Unknown</th> ");
5321: }else if(iage==iagemax+3){
5322: fprintf(ficlog,"Total");
5323: fprintf(ficresphtmfr,"<tr><th>Total</th> ");
5324: }else{
5325: if(first==1){
5326: first=0;
5327: printf("See log file for details...\n");
5328: }
5329: fprintf(ficresphtmfr,"<tr><th>%d</th> ",iage);
5330: fprintf(ficlog,"Age %d", iage);
5331: }
5332: for(s1=1; s1 <=nlstate ; s1++){
5333: for(m=-1, pp[s1]=0; m <=nlstate+ndeath ; m++)
5334: pp[s1] += freq[s1][m][iage];
5335: }
5336: for(s1=1; s1 <=nlstate ; s1++){
5337: for(m=-1, pos=0; m <=0 ; m++)
5338: pos += freq[s1][m][iage];
5339: if(pp[s1]>=1.e-10){
5340: if(first==1){
5341: printf(" %d.=%.0f loss[%d]=%.1f%%",s1,pp[s1],s1,100*pos/pp[s1]);
5342: }
5343: fprintf(ficlog," %d.=%.0f loss[%d]=%.1f%%",s1,pp[s1],s1,100*pos/pp[s1]);
5344: }else{
5345: if(first==1)
5346: printf(" %d.=%.0f loss[%d]=NaNQ%%",s1,pp[s1],s1);
5347: fprintf(ficlog," %d.=%.0f loss[%d]=NaNQ%%",s1,pp[s1],s1);
5348: }
5349: }
5350:
5351: for(s1=1; s1 <=nlstate ; s1++){
5352: /* posprop[s1]=0; */
5353: for(m=0, pp[s1]=0; m <=nlstate+ndeath; m++)/* Summing on all ages */
5354: pp[s1] += freq[s1][m][iage];
5355: } /* pp[s1] is the total number of transitions starting from state s1 and any ending status until this age */
5356:
5357: for(s1=1,pos=0, pospropta=0.; s1 <=nlstate ; s1++){
5358: pos += pp[s1]; /* pos is the total number of transitions until this age */
5359: posprop[s1] += prop[s1][iage]; /* prop is the number of transitions from a live state
5360: from s1 at age iage prop[s[m][iind]][(int)agev[m][iind]] += weight[iind] */
5361: pospropta += prop[s1][iage]; /* prop is the number of transitions from a live state
5362: from s1 at age iage prop[s[m][iind]][(int)agev[m][iind]] += weight[iind] */
5363: }
5364:
5365: /* Writing ficresp */
5366: if(cptcoveff==0 && nj==1){ /* no covariate and first pass */
5367: if( iage <= iagemax){
5368: fprintf(ficresp," %d",iage);
5369: }
5370: }else if( nj==2){
5371: if( iage <= iagemax){
5372: fprintf(ficresp," %d",iage);
5373: for (z1=1; z1<=cptcoveff; z1++) fprintf(ficresp, " %d %d",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]);
5374: }
5375: }
5376: for(s1=1; s1 <=nlstate ; s1++){
5377: if(pos>=1.e-5){
5378: if(first==1)
5379: printf(" %d.=%.0f prev[%d]=%.1f%%",s1,pp[s1],s1,100*pp[s1]/pos);
5380: fprintf(ficlog," %d.=%.0f prev[%d]=%.1f%%",s1,pp[s1],s1,100*pp[s1]/pos);
5381: }else{
5382: if(first==1)
5383: printf(" %d.=%.0f prev[%d]=NaNQ%%",s1,pp[s1],s1);
5384: fprintf(ficlog," %d.=%.0f prev[%d]=NaNQ%%",s1,pp[s1],s1);
5385: }
5386: if( iage <= iagemax){
5387: if(pos>=1.e-5){
5388: if(cptcoveff==0 && nj==1){ /* no covariate and first pass */
5389: fprintf(ficresp," %.5f %.0f %.0f",prop[s1][iage]/pospropta, prop[s1][iage],pospropta);
5390: }else if( nj==2){
5391: fprintf(ficresp," %.5f %.0f %.0f",prop[s1][iage]/pospropta, prop[s1][iage],pospropta);
5392: }
5393: fprintf(ficresphtm,"<th>%d</th><td>%.5f</td><td>%.0f</td><td>%.0f</td>",iage,prop[s1][iage]/pospropta, prop[s1][iage],pospropta);
5394: /*probs[iage][s1][j1]= pp[s1]/pos;*/
5395: /*printf("\niage=%d s1=%d j1=%d %.5f %.0f %.0f %f",iage,s1,j1,pp[s1]/pos, pp[s1],pos,probs[iage][s1][j1]);*/
5396: } else{
5397: if((cptcoveff==0 && nj==1)|| nj==2 ) fprintf(ficresp," NaNq %.0f %.0f",prop[s1][iage],pospropta);
5398: fprintf(ficresphtm,"<th>%d</th><td>NaNq</td><td>%.0f</td><td>%.0f</td>",iage, prop[s1][iage],pospropta);
5399: }
5400: }
5401: pospropt[s1] +=posprop[s1];
5402: } /* end loop s1 */
5403: /* pospropt=0.; */
5404: for(s1=-1; s1 <=nlstate+ndeath; s1++){
5405: for(m=-1; m <=nlstate+ndeath; m++){
5406: if(freq[s1][m][iage] !=0 ) { /* minimizing output */
5407: if(first==1){
5408: printf(" %d%d=%.0f",s1,m,freq[s1][m][iage]);
5409: }
5410: /* printf(" %d%d=%.0f",s1,m,freq[s1][m][iage]); */
5411: fprintf(ficlog," %d%d=%.0f",s1,m,freq[s1][m][iage]);
5412: }
5413: if(s1!=0 && m!=0)
5414: fprintf(ficresphtmfr,"<td>%.0f</td> ",freq[s1][m][iage]);
5415: }
5416: } /* end loop s1 */
5417: posproptt=0.;
5418: for(s1=1; s1 <=nlstate; s1++){
5419: posproptt += pospropt[s1];
5420: }
5421: fprintf(ficresphtmfr,"</tr>\n ");
5422: fprintf(ficresphtm,"</tr>\n");
5423: if((cptcoveff==0 && nj==1)|| nj==2 ) {
5424: if(iage <= iagemax)
5425: fprintf(ficresp,"\n");
5426: }
5427: if(first==1)
5428: printf("Others in log...\n");
5429: fprintf(ficlog,"\n");
5430: } /* end loop age iage */
5431:
5432: fprintf(ficresphtm,"<tr><th>Tot</th>");
5433: for(s1=1; s1 <=nlstate ; s1++){
5434: if(posproptt < 1.e-5){
5435: fprintf(ficresphtm,"<td>Nanq</td><td>%.0f</td><td>%.0f</td>",pospropt[s1],posproptt);
5436: }else{
5437: fprintf(ficresphtm,"<td>%.5f</td><td>%.0f</td><td>%.0f</td>",pospropt[s1]/posproptt,pospropt[s1],posproptt);
5438: }
5439: }
5440: fprintf(ficresphtm,"</tr>\n");
5441: fprintf(ficresphtm,"</table>\n");
5442: fprintf(ficresphtmfr,"</table>\n");
5443: if(posproptt < 1.e-5){
5444: fprintf(ficresphtm,"\n <p><b> This combination (%d) is not valid and no result will be produced</b></p>",j1);
5445: fprintf(ficresphtmfr,"\n <p><b> This combination (%d) is not valid and no result will be produced</b></p>",j1);
5446: fprintf(ficlog,"# This combination (%d) is not valid and no result will be produced\n",j1);
5447: printf("# This combination (%d) is not valid and no result will be produced\n",j1);
5448: invalidvarcomb[j1]=1;
5449: }else{
5450: fprintf(ficresphtm,"\n <p> This combination (%d) is valid and result will be produced (or no resultline).</p>",j1);
5451: invalidvarcomb[j1]=0;
5452: }
5453: fprintf(ficresphtmfr,"</table>\n");
5454: fprintf(ficlog,"\n");
5455: if(j!=0){
5456: printf("#Freqsummary: Starting values for combination j1=%d:\n", j1);
5457: for(i=1,s1=1; i <=nlstate; i++){
5458: for(k=1; k <=(nlstate+ndeath); k++){
5459: if (k != i) {
5460: for(jj=1; jj <=ncovmodel; jj++){ /* For counting s1 */
5461: if(jj==1){ /* Constant case (in fact cste + age) */
5462: if(j1==1){ /* All dummy covariates to zero */
5463: freq[i][k][iagemax+4]=freq[i][k][iagemax+3]; /* Stores case 0 0 0 */
5464: freq[i][i][iagemax+4]=freq[i][i][iagemax+3]; /* Stores case 0 0 0 */
5465: printf("%d%d ",i,k);
5466: fprintf(ficlog,"%d%d ",i,k);
5467: printf("%12.7f ln(%.0f/%.0f)= %f, OR=%f sd=%f \n",p[s1],freq[i][k][iagemax+3],freq[i][i][iagemax+3], log(freq[i][k][iagemax+3]/freq[i][i][iagemax+3]),freq[i][k][iagemax+3]/freq[i][i][iagemax+3], sqrt(1/freq[i][k][iagemax+3]+1/freq[i][i][iagemax+3]));
5468: fprintf(ficlog,"%12.7f ln(%.0f/%.0f)= %12.7f \n",p[s1],freq[i][k][iagemax+3],freq[i][i][iagemax+3], log(freq[i][k][iagemax+3]/freq[i][i][iagemax+3]));
5469: pstart[s1]= log(freq[i][k][iagemax+3]/freq[i][i][iagemax+3]);
5470: }
5471: }else if((j1==1) && (jj==2 || nagesqr==1)){ /* age or age*age parameter without covariate V4*age (to be done later) */
5472: for(iage=iagemin; iage <= iagemax+3; iage++){
5473: x[iage]= (double)iage;
5474: y[iage]= log(freq[i][k][iage]/freq[i][i][iage]);
5475: /* printf("i=%d, k=%d, s1=%d, j1=%d, jj=%d, y[%d]=%f\n",i,k,s1,j1,jj, iage, y[iage]); */
5476: }
5477: /* Some are not finite, but linreg will ignore these ages */
5478: no=0;
5479: linreg(iagemin,iagemax,&no,x,y,&a,&b,&r, &sa, &sb ); /* y= a+b*x with standard errors */
5480: pstart[s1]=b;
5481: pstart[s1-1]=a;
5482: }else if( j1!=1 && (j1==2 || (log(j1-1.)/log(2.)-(int)(log(j1-1.)/log(2.))) <0.010) && ( TvarsDind[(int)(log(j1-1.)/log(2.))+1]+2+nagesqr == jj) && Dummy[jj-2-nagesqr]==0){ /* We want only if the position, jj, in model corresponds to unique covariate equal to 1 in j1 combination */
5483: printf("j1=%d, jj=%d, (int)(log(j1-1.)/log(2.))+1=%d, TvarsDind[(int)(log(j1-1.)/log(2.))+1]=%d\n",j1, jj,(int)(log(j1-1.)/log(2.))+1,TvarsDind[(int)(log(j1-1.)/log(2.))+1]);
5484: printf("j1=%d, jj=%d, (log(j1-1.)/log(2.))+1=%f, TvarsDind[(int)(log(j1-1.)/log(2.))+1]=%d\n",j1, jj,(log(j1-1.)/log(2.))+1,TvarsDind[(int)(log(j1-1.)/log(2.))+1]);
5485: pstart[s1]= log((freq[i][k][iagemax+3]/freq[i][i][iagemax+3])/(freq[i][k][iagemax+4]/freq[i][i][iagemax+4]));
5486: printf("%d%d ",i,k);
5487: fprintf(ficlog,"%d%d ",i,k);
5488: printf("s1=%d,i=%d,k=%d,p[%d]=%12.7f ln((%.0f/%.0f)/(%.0f/%.0f))= %f, OR=%f sd=%f \n",s1,i,k,s1,p[s1],freq[i][k][iagemax+3],freq[i][i][iagemax+3],freq[i][k][iagemax+4],freq[i][i][iagemax+4], log((freq[i][k][iagemax+3]/freq[i][i][iagemax+3])/(freq[i][k][iagemax+4]/freq[i][i][iagemax+4])),(freq[i][k][iagemax+3]/freq[i][i][iagemax+3])/(freq[i][k][iagemax+4]/freq[i][i][iagemax+4]), sqrt(1/freq[i][k][iagemax+3]+1/freq[i][i][iagemax+3]+1/freq[i][k][iagemax+4]+1/freq[i][i][iagemax+4]));
5489: }else{ /* Other cases, like quantitative fixed or varying covariates */
5490: ;
5491: }
5492: /* printf("%12.7f )", param[i][jj][k]); */
5493: /* fprintf(ficlog,"%12.7f )", param[i][jj][k]); */
5494: s1++;
5495: } /* end jj */
5496: } /* end k!= i */
5497: } /* end k */
5498: } /* end i, s1 */
5499: } /* end j !=0 */
5500: } /* end selected combination of covariate j1 */
5501: if(j==0){ /* We can estimate starting values from the occurences in each case */
5502: printf("#Freqsummary: Starting values for the constants:\n");
5503: fprintf(ficlog,"\n");
5504: for(i=1,s1=1; i <=nlstate; i++){
5505: for(k=1; k <=(nlstate+ndeath); k++){
5506: if (k != i) {
5507: printf("%d%d ",i,k);
5508: fprintf(ficlog,"%d%d ",i,k);
5509: for(jj=1; jj <=ncovmodel; jj++){
5510: pstart[s1]=p[s1]; /* Setting pstart to p values by default */
5511: if(jj==1){ /* Age has to be done */
5512: pstart[s1]= log(freq[i][k][iagemax+3]/freq[i][i][iagemax+3]);
5513: printf("%12.7f ln(%.0f/%.0f)= %12.7f ",p[s1],freq[i][k][iagemax+3],freq[i][i][iagemax+3], log(freq[i][k][iagemax+3]/freq[i][i][iagemax+3]));
5514: fprintf(ficlog,"%12.7f ln(%.0f/%.0f)= %12.7f ",p[s1],freq[i][k][iagemax+3],freq[i][i][iagemax+3], log(freq[i][k][iagemax+3]/freq[i][i][iagemax+3]));
5515: }
5516: /* printf("%12.7f )", param[i][jj][k]); */
5517: /* fprintf(ficlog,"%12.7f )", param[i][jj][k]); */
5518: s1++;
5519: }
5520: printf("\n");
5521: fprintf(ficlog,"\n");
5522: }
5523: }
5524: } /* end of state i */
5525: printf("#Freqsummary\n");
5526: fprintf(ficlog,"\n");
5527: for(s1=-1; s1 <=nlstate+ndeath; s1++){
5528: for(s2=-1; s2 <=nlstate+ndeath; s2++){
5529: /* param[i]|j][k]= freq[s1][s2][iagemax+3] */
5530: printf(" %d%d=%.0f",s1,s2,freq[s1][s2][iagemax+3]);
5531: fprintf(ficlog," %d%d=%.0f",s1,s2,freq[s1][s2][iagemax+3]);
5532: /* if(freq[s1][s2][iage] !=0 ) { /\* minimizing output *\/ */
5533: /* printf(" %d%d=%.0f",s1,s2,freq[s1][s2][iagemax+3]); */
5534: /* fprintf(ficlog," %d%d=%.0f",s1,s2,freq[s1][s2][iagemax+3]); */
5535: /* } */
5536: }
5537: } /* end loop s1 */
5538:
5539: printf("\n");
5540: fprintf(ficlog,"\n");
5541: } /* end j=0 */
5542: } /* end j */
5543:
5544: if(mle == -2){ /* We want to use these values as starting values */
5545: for(i=1, jk=1; i <=nlstate; i++){
5546: for(j=1; j <=nlstate+ndeath; j++){
5547: if(j!=i){
5548: /*ca[0]= k+'a'-1;ca[1]='\0';*/
5549: printf("%1d%1d",i,j);
5550: fprintf(ficparo,"%1d%1d",i,j);
5551: for(k=1; k<=ncovmodel;k++){
5552: /* printf(" %lf",param[i][j][k]); */
5553: /* fprintf(ficparo," %lf",param[i][j][k]); */
5554: p[jk]=pstart[jk];
5555: printf(" %f ",pstart[jk]);
5556: fprintf(ficparo," %f ",pstart[jk]);
5557: jk++;
5558: }
5559: printf("\n");
5560: fprintf(ficparo,"\n");
5561: }
5562: }
5563: }
5564: } /* end mle=-2 */
5565: dateintmean=dateintsum/k2cpt;
5566: date2dmy(dateintmean,&jintmean,&mintmean,&aintmean);
5567:
5568: fclose(ficresp);
5569: fclose(ficresphtm);
5570: fclose(ficresphtmfr);
5571: free_vector(idq,1,nqfveff);
5572: free_vector(meanq,1,nqfveff);
5573: free_vector(stdq,1,nqfveff);
5574: free_matrix(meanqt,1,lastpass,1,nqtveff);
5575: free_vector(x, iagemin-AGEMARGE, iagemax+4+AGEMARGE);
5576: free_vector(y, iagemin-AGEMARGE, iagemax+4+AGEMARGE);
5577: free_ma3x(freq,-5,nlstate+ndeath,-5,nlstate+ndeath, iagemin-AGEMARGE, iagemax+4+AGEMARGE);
5578: free_vector(pospropt,1,nlstate);
5579: free_vector(posprop,1,nlstate);
5580: free_matrix(prop,1,nlstate,iagemin-AGEMARGE, iagemax+4+AGEMARGE);
5581: free_vector(pp,1,nlstate);
5582: /* End of freqsummary */
5583: }
5584:
5585: /* Simple linear regression */
5586: int linreg(int ifi, int ila, int *no, const double x[], const double y[], double* a, double* b, double* r, double* sa, double * sb) {
5587:
5588: /* y=a+bx regression */
5589: double sumx = 0.0; /* sum of x */
5590: double sumx2 = 0.0; /* sum of x**2 */
5591: double sumxy = 0.0; /* sum of x * y */
5592: double sumy = 0.0; /* sum of y */
5593: double sumy2 = 0.0; /* sum of y**2 */
5594: double sume2 = 0.0; /* sum of square or residuals */
5595: double yhat;
5596:
5597: double denom=0;
5598: int i;
5599: int ne=*no;
5600:
5601: for ( i=ifi, ne=0;i<=ila;i++) {
5602: if(!isfinite(x[i]) || !isfinite(y[i])){
5603: /* printf(" x[%d]=%f, y[%d]=%f\n",i,x[i],i,y[i]); */
5604: continue;
5605: }
5606: ne=ne+1;
5607: sumx += x[i];
5608: sumx2 += x[i]*x[i];
5609: sumxy += x[i] * y[i];
5610: sumy += y[i];
5611: sumy2 += y[i]*y[i];
5612: denom = (ne * sumx2 - sumx*sumx);
5613: /* printf("ne=%d, i=%d,x[%d]=%f, y[%d]=%f sumx=%f, sumx2=%f, sumxy=%f, sumy=%f, sumy2=%f, denom=%f\n",ne,i,i,x[i],i,y[i], sumx, sumx2,sumxy, sumy, sumy2,denom); */
5614: }
5615:
5616: denom = (ne * sumx2 - sumx*sumx);
5617: if (denom == 0) {
5618: // vertical, slope m is infinity
5619: *b = INFINITY;
5620: *a = 0;
5621: if (r) *r = 0;
5622: return 1;
5623: }
5624:
5625: *b = (ne * sumxy - sumx * sumy) / denom;
5626: *a = (sumy * sumx2 - sumx * sumxy) / denom;
5627: if (r!=NULL) {
5628: *r = (sumxy - sumx * sumy / ne) / /* compute correlation coeff */
5629: sqrt((sumx2 - sumx*sumx/ne) *
5630: (sumy2 - sumy*sumy/ne));
5631: }
5632: *no=ne;
5633: for ( i=ifi, ne=0;i<=ila;i++) {
5634: if(!isfinite(x[i]) || !isfinite(y[i])){
5635: /* printf(" x[%d]=%f, y[%d]=%f\n",i,x[i],i,y[i]); */
5636: continue;
5637: }
5638: ne=ne+1;
5639: yhat = y[i] - *a -*b* x[i];
5640: sume2 += yhat * yhat ;
5641:
5642: denom = (ne * sumx2 - sumx*sumx);
5643: /* printf("ne=%d, i=%d,x[%d]=%f, y[%d]=%f sumx=%f, sumx2=%f, sumxy=%f, sumy=%f, sumy2=%f, denom=%f\n",ne,i,i,x[i],i,y[i], sumx, sumx2,sumxy, sumy, sumy2,denom); */
5644: }
5645: *sb = sqrt(sume2/(double)(ne-2)/(sumx2 - sumx * sumx /(double)ne));
5646: *sa= *sb * sqrt(sumx2/ne);
5647:
5648: return 0;
5649: }
5650:
5651: /************ Prevalence ********************/
5652: 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)
5653: {
5654: /* Compute observed prevalence between dateprev1 and dateprev2 by counting the number of people
5655: in each health status at the date of interview (if between dateprev1 and dateprev2).
5656: We still use firstpass and lastpass as another selection.
5657: */
5658:
5659: int i, m, jk, j1, bool, z1,j, iv;
5660: int mi; /* Effective wave */
5661: int iage;
5662: double agebegin, ageend;
5663:
5664: double **prop;
5665: double posprop;
5666: double y2; /* in fractional years */
5667: int iagemin, iagemax;
5668: int first; /** to stop verbosity which is redirected to log file */
5669:
5670: iagemin= (int) agemin;
5671: iagemax= (int) agemax;
5672: /*pp=vector(1,nlstate);*/
5673: prop=matrix(1,nlstate,iagemin-AGEMARGE,iagemax+4+AGEMARGE);
5674: /* freq=ma3x(-1,nlstate+ndeath,-1,nlstate+ndeath,iagemin,iagemax+3);*/
5675: j1=0;
5676:
5677: /*j=cptcoveff;*/
5678: if (cptcovn<1) {j=1;ncodemax[1]=1;}
5679:
5680: first=0;
5681: for(j1=1; j1<= (int) pow(2,cptcoveff);j1++){ /* For each combination of simple dummy covariates */
5682: for (i=1; i<=nlstate; i++)
5683: for(iage=iagemin-AGEMARGE; iage <= iagemax+4+AGEMARGE; iage++)
5684: prop[i][iage]=0.0;
5685: printf("Prevalence combination of varying and fixed dummies %d\n",j1);
5686: /* fprintf(ficlog," V%d=%d ",Tvaraff[j1],nbcode[Tvaraff[j1]][codtabm(k,j1)]); */
5687: fprintf(ficlog,"Prevalence combination of varying and fixed dummies %d\n",j1);
5688:
5689: for (i=1; i<=imx; i++) { /* Each individual */
5690: bool=1;
5691: /* for(m=firstpass; m<=lastpass; m++){/\* Other selection (we can limit to certain interviews*\/ */
5692: for(mi=1; mi<wav[i];mi++){ /* For this wave too look where individual can be counted V4=0 V3=0 */
5693: m=mw[mi][i];
5694: /* Tmodelind[z1]=k is the position of the varying covariate in the model, but which # within 1 to ntv? */
5695: /* Tvar[Tmodelind[z1]] is the n of Vn; n-ncovcol-nqv is the first time varying covariate or iv */
5696: for (z1=1; z1<=cptcoveff; z1++){
5697: if( Fixed[Tmodelind[z1]]==1){
5698: iv= Tvar[Tmodelind[z1]]-ncovcol-nqv;
5699: if (cotvar[m][iv][i]!= nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]) /* iv=1 to ntv, right modality */
5700: bool=0;
5701: }else if( Fixed[Tmodelind[z1]]== 0) /* fixed */
5702: if (covar[Tvaraff[z1]][i]!= nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]) {
5703: bool=0;
5704: }
5705: }
5706: if(bool==1){ /* Otherwise we skip that wave/person */
5707: agebegin=agev[m][i]; /* Age at beginning of wave before transition*/
5708: /* ageend=agev[m][i]+(dh[m][i])*stepm/YEARM; /\* Age at end of wave and transition *\/ */
5709: if(m >=firstpass && m <=lastpass){
5710: y2=anint[m][i]+(mint[m][i]/12.); /* Fractional date in year */
5711: if ((y2>=dateprev1) && (y2<=dateprev2)) { /* Here is the main selection (fractional years) */
5712: if(agev[m][i]==0) agev[m][i]=iagemax+1;
5713: if(agev[m][i]==1) agev[m][i]=iagemax+2;
5714: if((int)agev[m][i] <iagemin-AGEMARGE || (int)agev[m][i] >iagemax+4+AGEMARGE){
5715: 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);
5716: exit(1);
5717: }
5718: if (s[m][i]>0 && s[m][i]<=nlstate) {
5719: /*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]]);*/
5720: prop[s[m][i]][(int)agev[m][i]] += weight[i];/* At age of beginning of transition, where status is known */
5721: prop[s[m][i]][iagemax+3] += weight[i];
5722: } /* end valid statuses */
5723: } /* end selection of dates */
5724: } /* end selection of waves */
5725: } /* end bool */
5726: } /* end wave */
5727: } /* end individual */
5728: for(i=iagemin; i <= iagemax+3; i++){
5729: for(jk=1,posprop=0; jk <=nlstate ; jk++) {
5730: posprop += prop[jk][i];
5731: }
5732:
5733: for(jk=1; jk <=nlstate ; jk++){
5734: if( i <= iagemax){
5735: if(posprop>=1.e-5){
5736: probs[i][jk][j1]= prop[jk][i]/posprop;
5737: } else{
5738: if(!first){
5739: first=1;
5740: printf("Warning Observed prevalence doesn't sum to 1 for state %d: probs[%d][%d][%d]=%lf because of lack of cases\nSee others in log file...\n",jk,i,jk, j1,probs[i][jk][j1]);
5741: }else{
5742: fprintf(ficlog,"Warning Observed prevalence doesn't sum to 1 for state %d: probs[%d][%d][%d]=%lf because of lack of cases.\n",jk,i,jk, j1,probs[i][jk][j1]);
5743: }
5744: }
5745: }
5746: }/* end jk */
5747: }/* end i */
5748: /*} *//* end i1 */
5749: } /* end j1 */
5750:
5751: /* free_ma3x(freq,-1,nlstate+ndeath,-1,nlstate+ndeath, iagemin, iagemax+3);*/
5752: /*free_vector(pp,1,nlstate);*/
5753: free_matrix(prop,1,nlstate, iagemin-AGEMARGE,iagemax+4+AGEMARGE);
5754: } /* End of prevalence */
5755:
5756: /************* Waves Concatenation ***************/
5757:
5758: 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)
5759: {
5760: /* Concatenates waves: wav[i] is the number of effective (useful waves in the sense that a non interview is useless) of individual i.
5761: Death is a valid wave (if date is known).
5762: mw[mi][i] is the mi (mi=1 to wav[i]) effective wave of individual i
5763: dh[m][i] or dh[mw[mi][i]][i] is the delay between two effective waves m=mw[mi][i]
5764: and mw[mi+1][i]. dh depends on stepm. s[m][i] exists for any wave from firstpass to lastpass
5765: */
5766:
5767: int i=0, mi=0, m=0, mli=0;
5768: /* int j, k=0,jk, ju, jl,jmin=1e+5, jmax=-1;
5769: double sum=0., jmean=0.;*/
5770: int first=0, firstwo=0, firsthree=0, firstfour=0, firstfiv=0;
5771: int j, k=0,jk, ju, jl;
5772: double sum=0.;
5773: first=0;
5774: firstwo=0;
5775: firsthree=0;
5776: firstfour=0;
5777: jmin=100000;
5778: jmax=-1;
5779: jmean=0.;
5780:
5781: /* Treating live states */
5782: for(i=1; i<=imx; i++){ /* For simple cases and if state is death */
5783: mi=0; /* First valid wave */
5784: mli=0; /* Last valid wave */
5785: m=firstpass; /* Loop on waves */
5786: while(s[m][i] <= nlstate){ /* a live state or unknown state */
5787: 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 */
5788: mli=m-1;/* mw[++mi][i]=m-1; */
5789: }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 */
5790: mw[++mi][i]=m; /* Valid wave: incrementing mi and updating mi; mw[mi] is the wave number of mi_th valid transition */
5791: mli=m;
5792: } /* else might be a useless wave -1 and mi is not incremented and mw[mi] not updated */
5793: if(m < lastpass){ /* m < lastpass, standard case */
5794: m++; /* mi gives the "effective" current wave, m the current wave, go to next wave by incrementing m */
5795: }
5796: else{ /* m = lastpass, eventual special issue with warning */
5797: #ifdef UNKNOWNSTATUSNOTCONTRIBUTING
5798: break;
5799: #else
5800: if(s[m][i]==-1 && (int) andc[i] == 9999 && (int)anint[m][i] != 9999){ /* no death date and known date of interview, case -2 (vital status unknown is warned later */
5801: if(firsthree == 0){
5802: 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 1-p_{%d%d} .\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, s[m][i], nlstate+ndeath);
5803: firsthree=1;
5804: }else if(firsthree >=1 && firsthree < 10){
5805: 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 1-p_{%d%d} .\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, s[m][i], nlstate+ndeath);
5806: firsthree++;
5807: }else if(firsthree == 10){
5808: printf("Information, too many Information flags: no more reported to log either\n");
5809: fprintf(ficlog,"Information, too many Information flags: no more reported to log either\n");
5810: firsthree++;
5811: }else{
5812: firsthree++;
5813: }
5814: mw[++mi][i]=m; /* Valid transition with unknown status */
5815: mli=m;
5816: }
5817: if(s[m][i]==-2){ /* Vital status is really unknown */
5818: nbwarn++;
5819: if((int)anint[m][i] == 9999){ /* Has the vital status really been verified?not a transition */
5820: 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);
5821: 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);
5822: }
5823: break;
5824: }
5825: break;
5826: #endif
5827: }/* End m >= lastpass */
5828: }/* end while */
5829:
5830: /* mi is the last effective wave, m is lastpass, mw[j][i] gives the # of j-th effective wave for individual i */
5831: /* After last pass */
5832: /* Treating death states */
5833: if (s[m][i] > nlstate){ /* In a death state */
5834: /* if( mint[m][i]==mdc[m][i] && anint[m][i]==andc[m][i]){ /\* same date of death and date of interview *\/ */
5835: /* } */
5836: mi++; /* Death is another wave */
5837: /* if(mi==0) never been interviewed correctly before death */
5838: /* Only death is a correct wave */
5839: mw[mi][i]=m;
5840: } /* else not in a death state */
5841: #ifndef DISPATCHINGKNOWNDEATHAFTERLASTWAVE
5842: else if ((int) andc[i] != 9999) { /* Date of death is known */
5843: if ((int)anint[m][i]!= 9999) { /* date of last interview is known */
5844: if((andc[i]+moisdc[i]/12.) <=(anint[m][i]+mint[m][i]/12.)){ /* month of death occured before last wave month and status should have been death instead of -1 */
5845: nbwarn++;
5846: if(firstfiv==0){
5847: printf("Warning! Death for individual %ld line=%d occurred at %d/%d before last wave %d, interviewed on %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 );
5848: firstfiv=1;
5849: }else{
5850: fprintf(ficlog,"Warning! Death for individual %ld line=%d occurred at %d/%d before last wave %d, interviewed on %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 );
5851: }
5852: s[m][i]=nlstate+1; /* Fixing the status as death. Be careful if multiple death states */
5853: }else{ /* Month of Death occured afer last wave month, potential bias */
5854: nberr++;
5855: if(firstwo==0){
5856: printf("Error! Death for individual %ld line=%d occurred at %d/%d after last wave %d interviewed at %d/%d with status %d. Potential bias if other individuals are still alive on this date but ignored. This case (%d)/wave (%d) is skipped, no contribution to likelihood. Please add a new fictitious wave at the date of last vital status scan, with a dead status. See documentation\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 );
5857: firstwo=1;
5858: }
5859: fprintf(ficlog,"Error! Death for individual %ld line=%d occurred at %d/%d after last wave %d interviewed at %d/%d with status %d. Potential bias if other individuals are still alive on this date but ignored. This case (%d)/wave (%d) is skipped, no contribution to likelihood. Please add a new fictitious wave at the date of last vital status scan, with a dead status. See documentation\n\n",num[i],i,(int) moisdc[i], (int) andc[i], lastpass,(int)mint[m][i],(int)anint[m][i], s[m][i], i,m );
5860: }
5861: }else{ /* if date of interview is unknown */
5862: /* death is known but not confirmed by death status at any wave */
5863: if(firstfour==0){
5864: 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 with status %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], s[m][i], i,m );
5865: firstfour=1;
5866: }
5867: 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 with status %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], s[m][i], i,m );
5868: }
5869: } /* end if date of death is known */
5870: #endif
5871: wav[i]=mi; /* mi should be the last effective wave (or mli), */
5872: /* wav[i]=mw[mi][i]; */
5873: if(mi==0){
5874: nbwarn++;
5875: if(first==0){
5876: printf("Warning! No valid information for individual %ld line=%d (skipped) and may be others, see log file\n",num[i],i);
5877: first=1;
5878: }
5879: if(first==1){
5880: fprintf(ficlog,"Warning! No valid information for individual %ld line=%d (skipped)\n",num[i],i);
5881: }
5882: } /* end mi==0 */
5883: } /* End individuals */
5884: /* wav and mw are no more changed */
5885:
5886: printf("Information, you have to check %d informations which haven't been logged!\n",firsthree);
5887: fprintf(ficlog,"Information, you have to check %d informations which haven't been logged!\n",firsthree);
5888:
5889:
5890: for(i=1; i<=imx; i++){
5891: for(mi=1; mi<wav[i];mi++){
5892: if (stepm <=0)
5893: dh[mi][i]=1;
5894: else{
5895: if (s[mw[mi+1][i]][i] > nlstate) { /* A death, but what if date is unknown? */
5896: if (agedc[i] < 2*AGESUP) {
5897: j= rint(agedc[i]*12-agev[mw[mi][i]][i]*12);
5898: if(j==0) j=1; /* Survives at least one month after exam */
5899: else if(j<0){
5900: nberr++;
5901: 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]);
5902: j=1; /* Temporary Dangerous patch */
5903: 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);
5904: 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]);
5905: 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);
5906: }
5907: k=k+1;
5908: if (j >= jmax){
5909: jmax=j;
5910: ijmax=i;
5911: }
5912: if (j <= jmin){
5913: jmin=j;
5914: ijmin=i;
5915: }
5916: sum=sum+j;
5917: /*if (j<0) printf("j=%d num=%d \n",j,i);*/
5918: /* printf("%d %d %d %d\n", s[mw[mi][i]][i] ,s[mw[mi+1][i]][i],j,i);*/
5919: }
5920: }
5921: else{
5922: j= rint( (agev[mw[mi+1][i]][i]*12 - agev[mw[mi][i]][i]*12));
5923: /* 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]); */
5924:
5925: k=k+1;
5926: if (j >= jmax) {
5927: jmax=j;
5928: ijmax=i;
5929: }
5930: else if (j <= jmin){
5931: jmin=j;
5932: ijmin=i;
5933: }
5934: /* if (j<10) printf("j=%d jmin=%d num=%d ",j,jmin,i); */
5935: /*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]);*/
5936: if(j<0){
5937: nberr++;
5938: 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]);
5939: 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]);
5940: }
5941: sum=sum+j;
5942: }
5943: jk= j/stepm;
5944: jl= j -jk*stepm;
5945: ju= j -(jk+1)*stepm;
5946: if(mle <=1){ /* only if we use a the linear-interpoloation pseudo-likelihood */
5947: if(jl==0){
5948: dh[mi][i]=jk;
5949: bh[mi][i]=0;
5950: }else{ /* We want a negative bias in order to only have interpolation ie
5951: * to avoid the price of an extra matrix product in likelihood */
5952: dh[mi][i]=jk+1;
5953: bh[mi][i]=ju;
5954: }
5955: }else{
5956: if(jl <= -ju){
5957: dh[mi][i]=jk;
5958: bh[mi][i]=jl; /* bias is positive if real duration
5959: * is higher than the multiple of stepm and negative otherwise.
5960: */
5961: }
5962: else{
5963: dh[mi][i]=jk+1;
5964: bh[mi][i]=ju;
5965: }
5966: if(dh[mi][i]==0){
5967: dh[mi][i]=1; /* At least one step */
5968: bh[mi][i]=ju; /* At least one step */
5969: /* 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);*/
5970: }
5971: } /* end if mle */
5972: }
5973: } /* end wave */
5974: }
5975: jmean=sum/k;
5976: 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);
5977: 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);
5978: }
5979:
5980: /*********** Tricode ****************************/
5981: void tricode(int *cptcov, int *Tvar, int **nbcode, int imx, int *Ndum)
5982: {
5983: /**< Uses cptcovn+2*cptcovprod as the number of covariates */
5984: /* Tvar[i]=atoi(stre); find 'n' in Vn and stores in Tvar. If model=V2+V1 Tvar[1]=2 and Tvar[2]=1
5985: * Boring subroutine which should only output nbcode[Tvar[j]][k]
5986: * Tvar[5] in V2+V1+V3*age+V2*V4 is 4 (V4) even it is a time varying or quantitative variable
5987: * nbcode[Tvar[5]][1]= nbcode[4][1]=0, nbcode[4][2]=1 (usually);
5988: */
5989:
5990: int ij=1, k=0, j=0, i=0, maxncov=NCOVMAX;
5991: int modmaxcovj=0; /* Modality max of covariates j */
5992: int cptcode=0; /* Modality max of covariates j */
5993: int modmincovj=0; /* Modality min of covariates j */
5994:
5995:
5996: /* cptcoveff=0; */
5997: /* *cptcov=0; */
5998:
5999: for (k=1; k <= maxncov; k++) ncodemax[k]=0; /* Horrible constant again replaced by NCOVMAX */
6000: for (k=1; k <= maxncov; k++)
6001: for(j=1; j<=2; j++)
6002: nbcode[k][j]=0; /* Valgrind */
6003:
6004: /* Loop on covariates without age and products and no quantitative variable */
6005: for (k=1; k<=cptcovt; k++) { /* cptcovt: total number of covariates of the model (2) nbocc(+)+1 = 8 excepting constant and age and age*age */
6006: for (j=-1; (j < maxncov); j++) Ndum[j]=0;
6007: if(Dummy[k]==0 && Typevar[k] !=1){ /* Dummy covariate and not age product */
6008: switch(Fixed[k]) {
6009: case 0: /* Testing on fixed dummy covariate, simple or product of fixed */
6010: modmaxcovj=0;
6011: modmincovj=0;
6012: 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*/
6013: ij=(int)(covar[Tvar[k]][i]);
6014: /* ij=0 or 1 or -1. Value of the covariate Tvar[j] for individual i
6015: * If product of Vn*Vm, still boolean *:
6016: * If it was coded 1, 2, 3, 4 should be splitted into 3 boolean variables
6017: * 1 => 0 0 0, 2 => 0 0 1, 3 => 0 1 1, 4=1 0 0 */
6018: /* Finds for covariate j, n=Tvar[j] of Vn . ij is the
6019: modality of the nth covariate of individual i. */
6020: if (ij > modmaxcovj)
6021: modmaxcovj=ij;
6022: else if (ij < modmincovj)
6023: modmincovj=ij;
6024: if (ij <0 || ij >1 ){
6025: printf("ERROR, IMaCh doesn't treat covariate with missing values V%d=-1, individual %d will be skipped.\n",Tvar[k],i);
6026: fprintf(ficlog,"ERROR, currently IMaCh doesn't treat covariate with missing values V%d=-1, individual %d will be skipped.\n",Tvar[k],i);
6027: fflush(ficlog);
6028: exit(1);
6029: }
6030: if ((ij < -1) || (ij > NCOVMAX)){
6031: printf( "Error: minimal is less than -1 or maximal is bigger than %d. Exiting. \n", NCOVMAX );
6032: exit(1);
6033: }else
6034: Ndum[ij]++; /*counts and stores the occurence of this modality 0, 1, -1*/
6035: /* If coded 1, 2, 3 , counts the number of 1 Ndum[1], number of 2, Ndum[2], etc */
6036: /*printf("i=%d ij=%d Ndum[ij]=%d imx=%d",i,ij,Ndum[ij],imx);*/
6037: /* getting the maximum value of the modality of the covariate
6038: (should be 0 or 1 now) Tvar[j]. If V=sex and male is coded 0 and
6039: female ies 1, then modmaxcovj=1.
6040: */
6041: } /* end for loop on individuals i */
6042: printf(" Minimal and maximal values of %d th (fixed) covariate V%d: min=%d max=%d \n", k, Tvar[k], modmincovj, modmaxcovj);
6043: fprintf(ficlog," Minimal and maximal values of %d th (fixed) covariate V%d: min=%d max=%d \n", k, Tvar[k], modmincovj, modmaxcovj);
6044: cptcode=modmaxcovj;
6045: /* Ndum[0] = frequency of 0 for model-covariate j, Ndum[1] frequency of 1 etc. */
6046: /*for (i=0; i<=cptcode; i++) {*/
6047: for (j=modmincovj; j<=modmaxcovj; j++) { /* j=-1 ? 0 and 1*//* For each value j of the modality of model-cov k */
6048: printf("Frequencies of (fixed) covariate %d ie V%d with value %d: %d\n", k, Tvar[k], j, Ndum[j]);
6049: fprintf(ficlog, "Frequencies of (fixed) covariate %d ie V%d with value %d: %d\n", k, Tvar[k], j, Ndum[j]);
6050: if( Ndum[j] != 0 ){ /* Counts if nobody answered modality j ie empty modality, we skip it and reorder */
6051: if( j != -1){
6052: ncodemax[k]++; /* ncodemax[k]= Number of modalities of the k th
6053: covariate for which somebody answered excluding
6054: undefined. Usually 2: 0 and 1. */
6055: }
6056: ncodemaxwundef[k]++; /* ncodemax[j]= Number of modalities of the k th
6057: covariate for which somebody answered including
6058: undefined. Usually 3: -1, 0 and 1. */
6059: } /* In fact ncodemax[k]=2 (dichotom. variables only) but it could be more for
6060: * historical reasons: 3 if coded 1, 2, 3 and 4 and Ndum[2]=0 */
6061: } /* Ndum[-1] number of undefined modalities */
6062:
6063: /* j is a covariate, n=Tvar[j] of Vn; Fills nbcode */
6064: /* For covariate j, modalities could be 1, 2, 3, 4, 5, 6, 7. */
6065: /* If Ndum[1]=0, Ndum[2]=0, Ndum[3]= 635, Ndum[4]=0, Ndum[5]=0, Ndum[6]=27, Ndum[7]=125; */
6066: /* modmincovj=3; modmaxcovj = 7; */
6067: /* There are only 3 modalities non empty 3, 6, 7 (or 2 if 27 is too few) : ncodemax[j]=3; */
6068: /* which will be coded 0, 1, 2 which in binary on 2=3-1 digits are 0=00 1=01, 2=10; */
6069: /* defining two dummy variables: variables V1_1 and V1_2.*/
6070: /* nbcode[Tvar[j]][ij]=k; */
6071: /* nbcode[Tvar[j]][1]=0; */
6072: /* nbcode[Tvar[j]][2]=1; */
6073: /* nbcode[Tvar[j]][3]=2; */
6074: /* To be continued (not working yet). */
6075: ij=0; /* ij is similar to i but can jump over null modalities */
6076:
6077: /* 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*/
6078: /* Skipping the case of missing values by reducing nbcode to 0 and 1 and not -1, 0, 1 */
6079: /* model=V1+V2+V3, if V2=-1, 0 or 1, then nbcode[2][1]=0 and nbcode[2][2]=1 instead of
6080: * nbcode[2][1]=-1, nbcode[2][2]=0 and nbcode[2][3]=1 */
6081: /*, could be restored in the future */
6082: for (i=0; i<=1; i++) { /* i= 1 to 2 for dichotomous, or from 1 to 3 or from -1 or 0 to 1 currently*/
6083: if (Ndum[i] == 0) { /* If nobody responded to this modality k */
6084: break;
6085: }
6086: ij++;
6087: 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 . Could be -1*/
6088: cptcode = ij; /* New max modality for covar j */
6089: } /* end of loop on modality i=-1 to 1 or more */
6090: break;
6091: case 1: /* Testing on varying covariate, could be simple and
6092: * should look at waves or product of fixed *
6093: * varying. No time to test -1, assuming 0 and 1 only */
6094: ij=0;
6095: for(i=0; i<=1;i++){
6096: nbcode[Tvar[k]][++ij]=i;
6097: }
6098: break;
6099: default:
6100: break;
6101: } /* end switch */
6102: } /* end dummy test */
6103: if(Dummy[k]==1 && Typevar[k] !=1){ /* Quantitative covariate and not age product */
6104: 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*/
6105: if(Tvar[k]<=0 || Tvar[k]>=NCOVMAX){
6106: printf("Error k=%d \n",k);
6107: exit(1);
6108: }
6109: if(isnan(covar[Tvar[k]][i])){
6110: printf("ERROR, IMaCh doesn't treat fixed quantitative covariate with missing values V%d=., individual %d will be skipped.\n",Tvar[k],i);
6111: fprintf(ficlog,"ERROR, currently IMaCh doesn't treat covariate with missing values V%d=., individual %d will be skipped.\n",Tvar[k],i);
6112: fflush(ficlog);
6113: exit(1);
6114: }
6115: }
6116: } /* end Quanti */
6117: } /* end of loop on model-covariate k. nbcode[Tvark][1]=-1, nbcode[Tvark][1]=0 and nbcode[Tvark][2]=1 sets the value of covariate k*/
6118:
6119: for (k=-1; k< maxncov; k++) Ndum[k]=0;
6120: /* Look at fixed dummy (single or product) covariates to check empty modalities */
6121: for (i=1; i<=ncovmodel-2-nagesqr; i++) { /* -2, cste and age and eventually age*age */
6122: /* Listing of all covariables in statement model to see if some covariates appear twice. For example, V1 appears twice in V1+V1*V2.*/
6123: 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 */
6124: 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 */
6125: /* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1, {2, 1, 1, 1, 2, 1, 1, 0, 0} */
6126: } /* V4+V3+V5, Ndum[1]@5={0, 0, 1, 1, 1} */
6127:
6128: ij=0;
6129: /* for (i=0; i<= maxncov-1; i++) { /\* modmaxcovj is unknown here. Only Ndum[2(V2),3(age*V3), 5(V3*V2) 6(V1*V4) *\/ */
6130: for (k=1; k<= cptcovt; k++) { /* cptcovt: total number of covariates of the model (2) nbocc(+)+1 = 8 excepting constant and age and age*age */
6131: /* modmaxcovj is unknown here. Only Ndum[2(V2),3(age*V3), 5(V3*V2) 6(V1*V4) */
6132: /*printf("Ndum[%d]=%d\n",i, Ndum[i]);*/
6133: /* if((Ndum[i]!=0) && (i<=ncovcol)){ /\* Tvar[i] <= ncovmodel ? *\/ */
6134: if(Ndum[Tvar[k]]!=0 && Dummy[k] == 0 && Typevar[k]==0){ /* Only Dummy simple and non empty in the model */
6135: /* Typevar[k] =0 for simple covariate (dummy, quantitative, fixed or varying), 1 for age product, 2 for product */
6136: /* Dummy[k] 0=dummy (0 1), 1 quantitative (single or product without age), 2 dummy with age product, 3 quant with age product*/
6137: /* If product not in single variable we don't print results */
6138: /*printf("diff Ndum[%d]=%d\n",i, Ndum[i]);*/
6139: ++ij;/* V5 + V4 + V3 + V4*V3 + V5*age + V2 + V1*V2 + V1*age + V1, *//* V5 quanti, V2 quanti, V4, V3, V1 dummies */
6140: /* k= 1 2 3 4 5 6 7 8 9 */
6141: /* Tvar[k]= 5 4 3 6 5 2 7 1 1 */
6142: /* ij 1 2 3 */
6143: /* Tvaraff[ij]= 4 3 1 */
6144: /* Tmodelind[ij]=2 3 9 */
6145: /* TmodelInvind[ij]=2 1 1 */
6146: 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*/
6147: Tmodelind[ij]=k; /* Tmodelind: index in model of dummies Tmodelind[1]=2 V4: pos=2; V3: pos=3, V1=9 {2, 3, 9, ?, ?,} */
6148: 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 */
6149: if(Fixed[k]!=0)
6150: anyvaryingduminmodel=1;
6151: /* }else if((Ndum[i]!=0) && (i<=ncovcol+nqv)){ */
6152: /* Tvaraff[++ij]=-10; /\* Dont'n know how to treat quantitative variables yet *\/ */
6153: /* }else if((Ndum[i]!=0) && (i<=ncovcol+nqv+ntv)){ */
6154: /* Tvaraff[++ij]=i; /\*For printing (unclear) *\/ */
6155: /* }else if((Ndum[i]!=0) && (i<=ncovcol+nqv+ntv+nqtv)){ */
6156: /* Tvaraff[++ij]=-20; /\* Dont'n know how to treat quantitative variables yet *\/ */
6157: }
6158: } /* Tvaraff[1]@5 {3, 4, -20, 0, 0} Very strange */
6159: /* ij--; */
6160: /* cptcoveff=ij; /\*Number of total covariates*\/ */
6161: *cptcov=ij; /* cptcov= Number of total real effective simple dummies (fixed or time arying) effective (used as cptcoveff in other functions)
6162: * because they can be excluded from the model and real
6163: * if in the model but excluded because missing values, but how to get k from ij?*/
6164: for(j=ij+1; j<= cptcovt; j++){
6165: Tvaraff[j]=0;
6166: Tmodelind[j]=0;
6167: }
6168: for(j=ntveff+1; j<= cptcovt; j++){
6169: TmodelInvind[j]=0;
6170: }
6171: /* To be sorted */
6172: ;
6173: }
6174:
6175:
6176: /*********** Health Expectancies ****************/
6177:
6178: void evsij(double ***eij, double x[], int nlstate, int stepm, int bage, int fage, double **oldm, double **savm, int cij, int estepm,char strstart[], int nres )
6179:
6180: {
6181: /* Health expectancies, no variances */
6182: /* cij is the combination in the list of combination of dummy covariates */
6183: /* strstart is a string of time at start of computing */
6184: int i, j, nhstepm, hstepm, h, nstepm;
6185: int nhstepma, nstepma; /* Decreasing with age */
6186: double age, agelim, hf;
6187: double ***p3mat;
6188: double eip;
6189:
6190: /* pstamp(ficreseij); */
6191: fprintf(ficreseij,"# (a) Life expectancies by health status at initial age and (b) health expectancies by health status at initial age\n");
6192: fprintf(ficreseij,"# Age");
6193: for(i=1; i<=nlstate;i++){
6194: for(j=1; j<=nlstate;j++){
6195: fprintf(ficreseij," e%1d%1d ",i,j);
6196: }
6197: fprintf(ficreseij," e%1d. ",i);
6198: }
6199: fprintf(ficreseij,"\n");
6200:
6201:
6202: if(estepm < stepm){
6203: printf ("Problem %d lower than %d\n",estepm, stepm);
6204: }
6205: else hstepm=estepm;
6206: /* We compute the life expectancy from trapezoids spaced every estepm months
6207: * This is mainly to measure the difference between two models: for example
6208: * if stepm=24 months pijx are given only every 2 years and by summing them
6209: * we are calculating an estimate of the Life Expectancy assuming a linear
6210: * progression in between and thus overestimating or underestimating according
6211: * to the curvature of the survival function. If, for the same date, we
6212: * estimate the model with stepm=1 month, we can keep estepm to 24 months
6213: * to compare the new estimate of Life expectancy with the same linear
6214: * hypothesis. A more precise result, taking into account a more precise
6215: * curvature will be obtained if estepm is as small as stepm. */
6216:
6217: /* For example we decided to compute the life expectancy with the smallest unit */
6218: /* hstepm beeing the number of stepms, if hstepm=1 the length of hstepm is stepm.
6219: nhstepm is the number of hstepm from age to agelim
6220: nstepm is the number of stepm from age to agelin.
6221: Look at hpijx to understand the reason which relies in memory size consideration
6222: and note for a fixed period like estepm months */
6223: /* We decided (b) to get a life expectancy respecting the most precise curvature of the
6224: survival function given by stepm (the optimization length). Unfortunately it
6225: means that if the survival funtion is printed only each two years of age and if
6226: you sum them up and add 1 year (area under the trapezoids) you won't get the same
6227: results. So we changed our mind and took the option of the best precision.
6228: */
6229: hstepm=hstepm/stepm; /* Typically in stepm units, if stepm=6 & estepm=24 , = 24/6 months = 4 */
6230:
6231: agelim=AGESUP;
6232: /* If stepm=6 months */
6233: /* Computed by stepm unit matrices, product of hstepm matrices, stored
6234: in an array of nhstepm length: nhstepm=10, hstepm=4, stepm=6 months */
6235:
6236: /* nhstepm age range expressed in number of stepm */
6237: nstepm=(int) rint((agelim-bage)*YEARM/stepm); /* Biggest nstepm */
6238: /* Typically if 20 years nstepm = 20*12/6=40 stepm */
6239: /* if (stepm >= YEARM) hstepm=1;*/
6240: nhstepm = nstepm/hstepm;/* Expressed in hstepm, typically nhstepm=40/4=10 */
6241: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
6242:
6243: for (age=bage; age<=fage; age ++){
6244: nstepma=(int) rint((agelim-bage)*YEARM/stepm); /* Biggest nstepm */
6245: /* Typically if 20 years nstepm = 20*12/6=40 stepm */
6246: /* if (stepm >= YEARM) hstepm=1;*/
6247: nhstepma = nstepma/hstepm;/* Expressed in hstepm, typically nhstepma=40/4=10 */
6248:
6249: /* If stepm=6 months */
6250: /* Computed by stepm unit matrices, product of hstepma matrices, stored
6251: in an array of nhstepma length: nhstepma=10, hstepm=4, stepm=6 months */
6252: /* printf("HELLO evsij Entering hpxij age=%d cij=%d hstepm=%d x[1]=%f nres=%d\n",(int) age, cij, hstepm, x[1], nres); */
6253: hpxij(p3mat,nhstepma,age,hstepm,x,nlstate,stepm,oldm, savm, cij, nres);
6254:
6255: hf=hstepm*stepm/YEARM; /* Duration of hstepm expressed in year unit. */
6256:
6257: printf("%d|",(int)age);fflush(stdout);
6258: fprintf(ficlog,"%d|",(int)age);fflush(ficlog);
6259:
6260: /* Computing expectancies */
6261: for(i=1; i<=nlstate;i++)
6262: for(j=1; j<=nlstate;j++)
6263: for (h=0, eij[i][j][(int)age]=0; h<=nhstepm-1; h++){
6264: eij[i][j][(int)age] += (p3mat[i][j][h]+p3mat[i][j][h+1])/2.0*hf;
6265:
6266: /* 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]);*/
6267:
6268: }
6269:
6270: fprintf(ficreseij,"%3.0f",age );
6271: for(i=1; i<=nlstate;i++){
6272: eip=0;
6273: for(j=1; j<=nlstate;j++){
6274: eip +=eij[i][j][(int)age];
6275: fprintf(ficreseij,"%9.4f", eij[i][j][(int)age] );
6276: }
6277: fprintf(ficreseij,"%9.4f", eip );
6278: }
6279: fprintf(ficreseij,"\n");
6280:
6281: }
6282: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
6283: printf("\n");
6284: fprintf(ficlog,"\n");
6285:
6286: }
6287:
6288: void cvevsij(double ***eij, double x[], int nlstate, int stepm, int bage, int fage, double **oldm, double **savm, int cij, int estepm,double delti[],double **matcov,char strstart[], int nres )
6289:
6290: {
6291: /* Covariances of health expectancies eij and of total life expectancies according
6292: to initial status i, ei. .
6293: */
6294: /* Very time consuming function, but already optimized with precov */
6295: int i, j, nhstepm, hstepm, h, nstepm, k, cptj, cptj2, i2, j2, ij, ji;
6296: int nhstepma, nstepma; /* Decreasing with age */
6297: double age, agelim, hf;
6298: double ***p3matp, ***p3matm, ***varhe;
6299: double **dnewm,**doldm;
6300: double *xp, *xm;
6301: double **gp, **gm;
6302: double ***gradg, ***trgradg;
6303: int theta;
6304:
6305: double eip, vip;
6306:
6307: varhe=ma3x(1,nlstate*nlstate,1,nlstate*nlstate,(int) bage, (int) fage);
6308: xp=vector(1,npar);
6309: xm=vector(1,npar);
6310: dnewm=matrix(1,nlstate*nlstate,1,npar);
6311: doldm=matrix(1,nlstate*nlstate,1,nlstate*nlstate);
6312:
6313: pstamp(ficresstdeij);
6314: fprintf(ficresstdeij,"# Health expectancies with standard errors\n");
6315: fprintf(ficresstdeij,"# Age");
6316: for(i=1; i<=nlstate;i++){
6317: for(j=1; j<=nlstate;j++)
6318: fprintf(ficresstdeij," e%1d%1d (SE)",i,j);
6319: fprintf(ficresstdeij," e%1d. ",i);
6320: }
6321: fprintf(ficresstdeij,"\n");
6322:
6323: pstamp(ficrescveij);
6324: fprintf(ficrescveij,"# Subdiagonal matrix of covariances of health expectancies by age: cov(eij,ekl)\n");
6325: fprintf(ficrescveij,"# Age");
6326: for(i=1; i<=nlstate;i++)
6327: for(j=1; j<=nlstate;j++){
6328: cptj= (j-1)*nlstate+i;
6329: for(i2=1; i2<=nlstate;i2++)
6330: for(j2=1; j2<=nlstate;j2++){
6331: cptj2= (j2-1)*nlstate+i2;
6332: if(cptj2 <= cptj)
6333: fprintf(ficrescveij," %1d%1d,%1d%1d",i,j,i2,j2);
6334: }
6335: }
6336: fprintf(ficrescveij,"\n");
6337:
6338: if(estepm < stepm){
6339: printf ("Problem %d lower than %d\n",estepm, stepm);
6340: }
6341: else hstepm=estepm;
6342: /* We compute the life expectancy from trapezoids spaced every estepm months
6343: * This is mainly to measure the difference between two models: for example
6344: * if stepm=24 months pijx are given only every 2 years and by summing them
6345: * we are calculating an estimate of the Life Expectancy assuming a linear
6346: * progression in between and thus overestimating or underestimating according
6347: * to the curvature of the survival function. If, for the same date, we
6348: * estimate the model with stepm=1 month, we can keep estepm to 24 months
6349: * to compare the new estimate of Life expectancy with the same linear
6350: * hypothesis. A more precise result, taking into account a more precise
6351: * curvature will be obtained if estepm is as small as stepm. */
6352:
6353: /* For example we decided to compute the life expectancy with the smallest unit */
6354: /* hstepm beeing the number of stepms, if hstepm=1 the length of hstepm is stepm.
6355: nhstepm is the number of hstepm from age to agelim
6356: nstepm is the number of stepm from age to agelin.
6357: Look at hpijx to understand the reason of that which relies in memory size
6358: and note for a fixed period like estepm months */
6359: /* We decided (b) to get a life expectancy respecting the most precise curvature of the
6360: survival function given by stepm (the optimization length). Unfortunately it
6361: means that if the survival funtion is printed only each two years of age and if
6362: you sum them up and add 1 year (area under the trapezoids) you won't get the same
6363: results. So we changed our mind and took the option of the best precision.
6364: */
6365: hstepm=hstepm/stepm; /* Typically in stepm units, if stepm=6 & estepm=24 , = 24/6 months = 4 */
6366:
6367: /* If stepm=6 months */
6368: /* nhstepm age range expressed in number of stepm */
6369: agelim=AGESUP;
6370: nstepm=(int) rint((agelim-bage)*YEARM/stepm);
6371: /* Typically if 20 years nstepm = 20*12/6=40 stepm */
6372: /* if (stepm >= YEARM) hstepm=1;*/
6373: nhstepm = nstepm/hstepm;/* Expressed in hstepm, typically nhstepm=40/4=10 */
6374:
6375: p3matp=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
6376: p3matm=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
6377: gradg=ma3x(0,nhstepm,1,npar,1,nlstate*nlstate);
6378: trgradg =ma3x(0,nhstepm,1,nlstate*nlstate,1,npar);
6379: gp=matrix(0,nhstepm,1,nlstate*nlstate);
6380: gm=matrix(0,nhstepm,1,nlstate*nlstate);
6381:
6382: for (age=bage; age<=fage; age ++){
6383: nstepma=(int) rint((agelim-bage)*YEARM/stepm); /* Biggest nstepm */
6384: /* Typically if 20 years nstepm = 20*12/6=40 stepm */
6385: /* if (stepm >= YEARM) hstepm=1;*/
6386: nhstepma = nstepma/hstepm;/* Expressed in hstepm, typically nhstepma=40/4=10 */
6387:
6388: /* If stepm=6 months */
6389: /* Computed by stepm unit matrices, product of hstepma matrices, stored
6390: in an array of nhstepma length: nhstepma=10, hstepm=4, stepm=6 months */
6391:
6392: hf=hstepm*stepm/YEARM; /* Duration of hstepm expressed in year unit. */
6393:
6394: /* Computing Variances of health expectancies */
6395: /* Gradient is computed with plus gp and minus gm. Code is duplicated in order to
6396: decrease memory allocation */
6397: for(theta=1; theta <=npar; theta++){
6398: for(i=1; i<=npar; i++){
6399: xp[i] = x[i] + (i==theta ?delti[theta]:0);
6400: xm[i] = x[i] - (i==theta ?delti[theta]:0);
6401: }
6402: hpxij(p3matp,nhstepm,age,hstepm,xp,nlstate,stepm,oldm,savm, cij, nres);
6403: hpxij(p3matm,nhstepm,age,hstepm,xm,nlstate,stepm,oldm,savm, cij, nres);
6404:
6405: for(j=1; j<= nlstate; j++){
6406: for(i=1; i<=nlstate; i++){
6407: for(h=0; h<=nhstepm-1; h++){
6408: gp[h][(j-1)*nlstate + i] = (p3matp[i][j][h]+p3matp[i][j][h+1])/2.;
6409: gm[h][(j-1)*nlstate + i] = (p3matm[i][j][h]+p3matm[i][j][h+1])/2.;
6410: }
6411: }
6412: }
6413:
6414: for(ij=1; ij<= nlstate*nlstate; ij++)
6415: for(h=0; h<=nhstepm-1; h++){
6416: gradg[h][theta][ij]= (gp[h][ij]-gm[h][ij])/2./delti[theta];
6417: }
6418: }/* End theta */
6419:
6420:
6421: for(h=0; h<=nhstepm-1; h++)
6422: for(j=1; j<=nlstate*nlstate;j++)
6423: for(theta=1; theta <=npar; theta++)
6424: trgradg[h][j][theta]=gradg[h][theta][j];
6425:
6426:
6427: for(ij=1;ij<=nlstate*nlstate;ij++)
6428: for(ji=1;ji<=nlstate*nlstate;ji++)
6429: varhe[ij][ji][(int)age] =0.;
6430:
6431: printf("%d|",(int)age);fflush(stdout);
6432: fprintf(ficlog,"%d|",(int)age);fflush(ficlog);
6433: for(h=0;h<=nhstepm-1;h++){
6434: for(k=0;k<=nhstepm-1;k++){
6435: matprod2(dnewm,trgradg[h],1,nlstate*nlstate,1,npar,1,npar,matcov);
6436: matprod2(doldm,dnewm,1,nlstate*nlstate,1,npar,1,nlstate*nlstate,gradg[k]);
6437: for(ij=1;ij<=nlstate*nlstate;ij++)
6438: for(ji=1;ji<=nlstate*nlstate;ji++)
6439: varhe[ij][ji][(int)age] += doldm[ij][ji]*hf*hf;
6440: }
6441: }
6442: /* if((int)age ==50){ */
6443: /* printf(" age=%d cij=%d nres=%d varhe[%d][%d]=%f ",(int)age, cij, nres, 1,2,varhe[1][2]); */
6444: /* } */
6445: /* Computing expectancies */
6446: hpxij(p3matm,nhstepm,age,hstepm,x,nlstate,stepm,oldm, savm, cij,nres);
6447: for(i=1; i<=nlstate;i++)
6448: for(j=1; j<=nlstate;j++)
6449: for (h=0, eij[i][j][(int)age]=0; h<=nhstepm-1; h++){
6450: eij[i][j][(int)age] += (p3matm[i][j][h]+p3matm[i][j][h+1])/2.0*hf;
6451:
6452: /* 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]);*/
6453:
6454: }
6455:
6456: /* Standard deviation of expectancies ij */
6457: fprintf(ficresstdeij,"%3.0f",age );
6458: for(i=1; i<=nlstate;i++){
6459: eip=0.;
6460: vip=0.;
6461: for(j=1; j<=nlstate;j++){
6462: eip += eij[i][j][(int)age];
6463: for(k=1; k<=nlstate;k++) /* Sum on j and k of cov(eij,eik) */
6464: vip += varhe[(j-1)*nlstate+i][(k-1)*nlstate+i][(int)age];
6465: fprintf(ficresstdeij," %9.4f (%.4f)", eij[i][j][(int)age], sqrt(varhe[(j-1)*nlstate+i][(j-1)*nlstate+i][(int)age]) );
6466: }
6467: fprintf(ficresstdeij," %9.4f (%.4f)", eip, sqrt(vip));
6468: }
6469: fprintf(ficresstdeij,"\n");
6470:
6471: /* Variance of expectancies ij */
6472: fprintf(ficrescveij,"%3.0f",age );
6473: for(i=1; i<=nlstate;i++)
6474: for(j=1; j<=nlstate;j++){
6475: cptj= (j-1)*nlstate+i;
6476: for(i2=1; i2<=nlstate;i2++)
6477: for(j2=1; j2<=nlstate;j2++){
6478: cptj2= (j2-1)*nlstate+i2;
6479: if(cptj2 <= cptj)
6480: fprintf(ficrescveij," %.4f", varhe[cptj][cptj2][(int)age]);
6481: }
6482: }
6483: fprintf(ficrescveij,"\n");
6484:
6485: }
6486: free_matrix(gm,0,nhstepm,1,nlstate*nlstate);
6487: free_matrix(gp,0,nhstepm,1,nlstate*nlstate);
6488: free_ma3x(gradg,0,nhstepm,1,npar,1,nlstate*nlstate);
6489: free_ma3x(trgradg,0,nhstepm,1,nlstate*nlstate,1,npar);
6490: free_ma3x(p3matm,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
6491: free_ma3x(p3matp,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
6492: printf("\n");
6493: fprintf(ficlog,"\n");
6494:
6495: free_vector(xm,1,npar);
6496: free_vector(xp,1,npar);
6497: free_matrix(dnewm,1,nlstate*nlstate,1,npar);
6498: free_matrix(doldm,1,nlstate*nlstate,1,nlstate*nlstate);
6499: free_ma3x(varhe,1,nlstate*nlstate,1,nlstate*nlstate,(int) bage, (int)fage);
6500: }
6501:
6502: /************ Variance ******************/
6503: void varevsij(char optionfilefiname[], double ***vareij, double **matcov, double x[], double delti[], int nlstate, int stepm, double bage, double fage, double **oldm, double **savm, double **prlim, double ftolpl, int *ncvyearp, int ij, int estepm, int cptcov, int cptcod, int popbased, int mobilav, char strstart[], int nres)
6504: {
6505: /** Variance of health expectancies
6506: * double **prevalim(double **prlim, int nlstate, double *xp, double age, double **oldm, double ** savm,double ftolpl);
6507: * double **newm;
6508: * int movingaverage(double ***probs, double bage,double fage, double ***mobaverage, int mobilav)
6509: */
6510:
6511: /* int movingaverage(); */
6512: double **dnewm,**doldm;
6513: double **dnewmp,**doldmp;
6514: int i, j, nhstepm, hstepm, h, nstepm ;
6515: int first=0;
6516: int k;
6517: double *xp;
6518: double **gp, **gm; /**< for var eij */
6519: double ***gradg, ***trgradg; /**< for var eij */
6520: double **gradgp, **trgradgp; /**< for var p point j */
6521: double *gpp, *gmp; /**< for var p point j */
6522: double **varppt; /**< for var p point j nlstate to nlstate+ndeath */
6523: double ***p3mat;
6524: double age,agelim, hf;
6525: /* double ***mobaverage; */
6526: int theta;
6527: char digit[4];
6528: char digitp[25];
6529:
6530: char fileresprobmorprev[FILENAMELENGTH];
6531:
6532: if(popbased==1){
6533: if(mobilav!=0)
6534: strcpy(digitp,"-POPULBASED-MOBILAV_");
6535: else strcpy(digitp,"-POPULBASED-NOMOBIL_");
6536: }
6537: else
6538: strcpy(digitp,"-STABLBASED_");
6539:
6540: /* if (mobilav!=0) { */
6541: /* mobaverage= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
6542: /* if (movingaverage(probs, bage, fage, mobaverage,mobilav)!=0){ */
6543: /* fprintf(ficlog," Error in movingaverage mobilav=%d\n",mobilav); */
6544: /* printf(" Error in movingaverage mobilav=%d\n",mobilav); */
6545: /* } */
6546: /* } */
6547:
6548: strcpy(fileresprobmorprev,"PRMORPREV-");
6549: sprintf(digit,"%-d",ij);
6550: /*printf("DIGIT=%s, ij=%d ijr=%-d|\n",digit, ij,ij);*/
6551: strcat(fileresprobmorprev,digit); /* Tvar to be done */
6552: strcat(fileresprobmorprev,digitp); /* Popbased or not, mobilav or not */
6553: strcat(fileresprobmorprev,fileresu);
6554: if((ficresprobmorprev=fopen(fileresprobmorprev,"w"))==NULL) {
6555: printf("Problem with resultfile: %s\n", fileresprobmorprev);
6556: fprintf(ficlog,"Problem with resultfile: %s\n", fileresprobmorprev);
6557: }
6558: printf("Computing total mortality p.j=w1*p1j+w2*p2j+..: result on file '%s' \n",fileresprobmorprev);
6559: fprintf(ficlog,"Computing total mortality p.j=w1*p1j+w2*p2j+..: result on file '%s' \n",fileresprobmorprev);
6560: pstamp(ficresprobmorprev);
6561: 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);
6562: fprintf(ficresprobmorprev,"# Selected quantitative variables and dummies");
6563:
6564: /* We use TinvDoQresult[nres][resultmodel[nres][j] we sort according to the equation model and the resultline: it is a choice */
6565: /* for (j=1; j<= nsq; j++){ /\* For each selected (single) quantitative value *\/ /\* To be done*\/ */
6566: /* fprintf(ficresprobmorprev," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][resultmodel[nres][j]]); */
6567: /* } */
6568: for (j=1; j<= cptcovs; j++){ /* For each selected (single) quantitative value */ /* To be done*/
6569: fprintf(ficresprobmorprev," V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
6570: }
6571: /* for(j=1;j<=cptcoveff;j++) */
6572: /* fprintf(ficresprobmorprev," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(ij,TnsdVar[Tvaraff[j]])]); */
6573: fprintf(ficresprobmorprev,"\n");
6574:
6575: fprintf(ficresprobmorprev,"# Age cov=%-d",ij);
6576: for(j=nlstate+1; j<=(nlstate+ndeath);j++){
6577: fprintf(ficresprobmorprev," p.%-d SE",j);
6578: for(i=1; i<=nlstate;i++)
6579: fprintf(ficresprobmorprev," w%1d p%-d%-d",i,i,j);
6580: }
6581: fprintf(ficresprobmorprev,"\n");
6582:
6583: fprintf(ficgp,"\n# Routine varevsij");
6584: fprintf(ficgp,"\nunset title \n");
6585: /* fprintf(fichtm, "#Local time at start: %s", strstart);*/
6586: 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");
6587: fprintf(fichtm,"\n<br>%s <br>\n",digitp);
6588:
6589: varppt = matrix(nlstate+1,nlstate+ndeath,nlstate+1,nlstate+ndeath);
6590: pstamp(ficresvij);
6591: fprintf(ficresvij,"# Variance and covariance of health expectancies e.j \n# (weighted average of eij where weights are ");
6592: if(popbased==1)
6593: 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);
6594: else
6595: fprintf(ficresvij,"the age specific period (stable) prevalences in each health state \n");
6596: fprintf(ficresvij,"# Age");
6597: for(i=1; i<=nlstate;i++)
6598: for(j=1; j<=nlstate;j++)
6599: fprintf(ficresvij," Cov(e.%1d, e.%1d)",i,j);
6600: fprintf(ficresvij,"\n");
6601:
6602: xp=vector(1,npar);
6603: dnewm=matrix(1,nlstate,1,npar);
6604: doldm=matrix(1,nlstate,1,nlstate);
6605: dnewmp= matrix(nlstate+1,nlstate+ndeath,1,npar);
6606: doldmp= matrix(nlstate+1,nlstate+ndeath,nlstate+1,nlstate+ndeath);
6607:
6608: gradgp=matrix(1,npar,nlstate+1,nlstate+ndeath);
6609: gpp=vector(nlstate+1,nlstate+ndeath);
6610: gmp=vector(nlstate+1,nlstate+ndeath);
6611: trgradgp =matrix(nlstate+1,nlstate+ndeath,1,npar); /* mu or p point j*/
6612:
6613: if(estepm < stepm){
6614: printf ("Problem %d lower than %d\n",estepm, stepm);
6615: }
6616: else hstepm=estepm;
6617: /* For example we decided to compute the life expectancy with the smallest unit */
6618: /* hstepm beeing the number of stepms, if hstepm=1 the length of hstepm is stepm.
6619: nhstepm is the number of hstepm from age to agelim
6620: nstepm is the number of stepm from age to agelim.
6621: Look at function hpijx to understand why because of memory size limitations,
6622: we decided (b) to get a life expectancy respecting the most precise curvature of the
6623: survival function given by stepm (the optimization length). Unfortunately it
6624: means that if the survival funtion is printed every two years of age and if
6625: you sum them up and add 1 year (area under the trapezoids) you won't get the same
6626: results. So we changed our mind and took the option of the best precision.
6627: */
6628: hstepm=hstepm/stepm; /* Typically in stepm units, if stepm=6 & estepm=24 , = 24/6 months = 4 */
6629: agelim = AGESUP;
6630: for (age=bage; age<=fage; age ++){ /* If stepm=6 months */
6631: nstepm=(int) rint((agelim-age)*YEARM/stepm); /* Typically 20 years = 20*12/6=40 */
6632: nhstepm = nstepm/hstepm;/* Expressed in hstepm, typically nhstepm=40/4=10 */
6633: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
6634: gradg=ma3x(0,nhstepm,1,npar,1,nlstate);
6635: gp=matrix(0,nhstepm,1,nlstate);
6636: gm=matrix(0,nhstepm,1,nlstate);
6637:
6638:
6639: for(theta=1; theta <=npar; theta++){
6640: for(i=1; i<=npar; i++){ /* Computes gradient x + delta*/
6641: xp[i] = x[i] + (i==theta ?delti[theta]:0);
6642: }
6643: /**< Computes the prevalence limit with parameter theta shifted of delta up to ftolpl precision and
6644: * returns into prlim .
6645: */
6646: prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp,ij, nres);
6647:
6648: /* If popbased = 1 we use crossection prevalences. Previous step is useless but prlim is created */
6649: if (popbased==1) {
6650: if(mobilav ==0){
6651: for(i=1; i<=nlstate;i++)
6652: prlim[i][i]=probs[(int)age][i][ij];
6653: }else{ /* mobilav */
6654: for(i=1; i<=nlstate;i++)
6655: prlim[i][i]=mobaverage[(int)age][i][ij];
6656: }
6657: }
6658: /**< Computes the shifted transition matrix \f$ {}{h}_p^{ij}x\f$ at horizon h.
6659: */
6660: hpxij(p3mat,nhstepm,age,hstepm,xp,nlstate,stepm,oldm,savm, ij,nres); /* Returns p3mat[i][j][h] for h=0 to nhstepm */
6661: /**< And for each alive state j, sums over i \f$ w^i_x {}{h}_p^{ij}x\f$, which are the probability
6662: * at horizon h in state j including mortality.
6663: */
6664: for(j=1; j<= nlstate; j++){
6665: for(h=0; h<=nhstepm; h++){
6666: for(i=1, gp[h][j]=0.;i<=nlstate;i++)
6667: gp[h][j] += prlim[i][i]*p3mat[i][j][h];
6668: }
6669: }
6670: /* Next for computing shifted+ probability of death (h=1 means
6671: computed over hstepm matrices product = hstepm*stepm months)
6672: as a weighted average of prlim(i) * p(i,j) p.3=w1*p13 + w2*p23 .
6673: */
6674: for(j=nlstate+1;j<=nlstate+ndeath;j++){
6675: for(i=1,gpp[j]=0.; i<= nlstate; i++)
6676: gpp[j] += prlim[i][i]*p3mat[i][j][1];
6677: }
6678:
6679: /* Again with minus shift */
6680:
6681: for(i=1; i<=npar; i++) /* Computes gradient x - delta */
6682: xp[i] = x[i] - (i==theta ?delti[theta]:0);
6683:
6684: prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp, ij, nres);
6685:
6686: if (popbased==1) {
6687: if(mobilav ==0){
6688: for(i=1; i<=nlstate;i++)
6689: prlim[i][i]=probs[(int)age][i][ij];
6690: }else{ /* mobilav */
6691: for(i=1; i<=nlstate;i++)
6692: prlim[i][i]=mobaverage[(int)age][i][ij];
6693: }
6694: }
6695:
6696: hpxij(p3mat,nhstepm,age,hstepm,xp,nlstate,stepm,oldm,savm, ij,nres);
6697:
6698: for(j=1; j<= nlstate; j++){ /* Sum of wi * eij = e.j */
6699: for(h=0; h<=nhstepm; h++){
6700: for(i=1, gm[h][j]=0.;i<=nlstate;i++)
6701: gm[h][j] += prlim[i][i]*p3mat[i][j][h];
6702: }
6703: }
6704: /* This for computing probability of death (h=1 means
6705: computed over hstepm matrices product = hstepm*stepm months)
6706: as a weighted average of prlim.
6707: */
6708: for(j=nlstate+1;j<=nlstate+ndeath;j++){
6709: for(i=1,gmp[j]=0.; i<= nlstate; i++)
6710: gmp[j] += prlim[i][i]*p3mat[i][j][1];
6711: }
6712: /* end shifting computations */
6713:
6714: /**< Computing gradient matrix at horizon h
6715: */
6716: for(j=1; j<= nlstate; j++) /* vareij */
6717: for(h=0; h<=nhstepm; h++){
6718: gradg[h][theta][j]= (gp[h][j]-gm[h][j])/2./delti[theta];
6719: }
6720: /**< Gradient of overall mortality p.3 (or p.j)
6721: */
6722: for(j=nlstate+1; j<= nlstate+ndeath; j++){ /* var mu mortality from j */
6723: gradgp[theta][j]= (gpp[j]-gmp[j])/2./delti[theta];
6724: }
6725:
6726: } /* End theta */
6727:
6728: /* We got the gradient matrix for each theta and state j */
6729: trgradg =ma3x(0,nhstepm,1,nlstate,1,npar); /* veij */
6730:
6731: for(h=0; h<=nhstepm; h++) /* veij */
6732: for(j=1; j<=nlstate;j++)
6733: for(theta=1; theta <=npar; theta++)
6734: trgradg[h][j][theta]=gradg[h][theta][j];
6735:
6736: for(j=nlstate+1; j<=nlstate+ndeath;j++) /* mu */
6737: for(theta=1; theta <=npar; theta++)
6738: trgradgp[j][theta]=gradgp[theta][j];
6739: /**< as well as its transposed matrix
6740: */
6741:
6742: hf=hstepm*stepm/YEARM; /* Duration of hstepm expressed in year unit. */
6743: for(i=1;i<=nlstate;i++)
6744: for(j=1;j<=nlstate;j++)
6745: vareij[i][j][(int)age] =0.;
6746:
6747: /* Computing trgradg by matcov by gradg at age and summing over h
6748: * and k (nhstepm) formula 15 of article
6749: * Lievre-Brouard-Heathcote
6750: */
6751:
6752: for(h=0;h<=nhstepm;h++){
6753: for(k=0;k<=nhstepm;k++){
6754: matprod2(dnewm,trgradg[h],1,nlstate,1,npar,1,npar,matcov);
6755: matprod2(doldm,dnewm,1,nlstate,1,npar,1,nlstate,gradg[k]);
6756: for(i=1;i<=nlstate;i++)
6757: for(j=1;j<=nlstate;j++)
6758: vareij[i][j][(int)age] += doldm[i][j]*hf*hf;
6759: }
6760: }
6761:
6762: /* pptj is p.3 or p.j = trgradgp by cov by gradgp, variance of
6763: * p.j overall mortality formula 49 but computed directly because
6764: * we compute the grad (wix pijx) instead of grad (pijx),even if
6765: * wix is independent of theta.
6766: */
6767: matprod2(dnewmp,trgradgp,nlstate+1,nlstate+ndeath,1,npar,1,npar,matcov);
6768: matprod2(doldmp,dnewmp,nlstate+1,nlstate+ndeath,1,npar,nlstate+1,nlstate+ndeath,gradgp);
6769: for(j=nlstate+1;j<=nlstate+ndeath;j++)
6770: for(i=nlstate+1;i<=nlstate+ndeath;i++)
6771: varppt[j][i]=doldmp[j][i];
6772: /* end ppptj */
6773: /* x centered again */
6774:
6775: prevalim(prlim,nlstate,x,age,oldm,savm,ftolpl,ncvyearp,ij, nres);
6776:
6777: if (popbased==1) {
6778: if(mobilav ==0){
6779: for(i=1; i<=nlstate;i++)
6780: prlim[i][i]=probs[(int)age][i][ij];
6781: }else{ /* mobilav */
6782: for(i=1; i<=nlstate;i++)
6783: prlim[i][i]=mobaverage[(int)age][i][ij];
6784: }
6785: }
6786:
6787: /* This for computing probability of death (h=1 means
6788: computed over hstepm (estepm) matrices product = hstepm*stepm months)
6789: as a weighted average of prlim.
6790: */
6791: hpxij(p3mat,nhstepm,age,hstepm,x,nlstate,stepm,oldm,savm, ij, nres);
6792: for(j=nlstate+1;j<=nlstate+ndeath;j++){
6793: for(i=1,gmp[j]=0.;i<= nlstate; i++)
6794: gmp[j] += prlim[i][i]*p3mat[i][j][1];
6795: }
6796: /* end probability of death */
6797:
6798: fprintf(ficresprobmorprev,"%3d %d ",(int) age, ij);
6799: for(j=nlstate+1; j<=(nlstate+ndeath);j++){
6800: fprintf(ficresprobmorprev," %11.3e %11.3e",gmp[j], sqrt(varppt[j][j]));
6801: for(i=1; i<=nlstate;i++){
6802: fprintf(ficresprobmorprev," %11.3e %11.3e ",prlim[i][i],p3mat[i][j][1]);
6803: }
6804: }
6805: fprintf(ficresprobmorprev,"\n");
6806:
6807: fprintf(ficresvij,"%.0f ",age );
6808: for(i=1; i<=nlstate;i++)
6809: for(j=1; j<=nlstate;j++){
6810: fprintf(ficresvij," %.4f", vareij[i][j][(int)age]);
6811: }
6812: fprintf(ficresvij,"\n");
6813: free_matrix(gp,0,nhstepm,1,nlstate);
6814: free_matrix(gm,0,nhstepm,1,nlstate);
6815: free_ma3x(gradg,0,nhstepm,1,npar,1,nlstate);
6816: free_ma3x(trgradg,0,nhstepm,1,nlstate,1,npar);
6817: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
6818: } /* End age */
6819: free_vector(gpp,nlstate+1,nlstate+ndeath);
6820: free_vector(gmp,nlstate+1,nlstate+ndeath);
6821: free_matrix(gradgp,1,npar,nlstate+1,nlstate+ndeath);
6822: free_matrix(trgradgp,nlstate+1,nlstate+ndeath,1,npar); /* mu or p point j*/
6823: /* fprintf(ficgp,"\nunset parametric;unset label; set ter png small size 320, 240"); */
6824: fprintf(ficgp,"\nunset parametric;unset label; set ter svg size 640, 480");
6825: /* for(j=nlstate+1; j<= nlstate+ndeath; j++){ *//* Only the first actually */
6826: fprintf(ficgp,"\n set log y; unset log x;set xlabel \"Age\"; set ylabel \"Force of mortality (year-1)\";");
6827: fprintf(ficgp,"\nset out \"%s%s.svg\";",subdirf3(optionfilefiname,"VARMUPTJGR-",digitp),digit);
6828: /* fprintf(ficgp,"\n plot \"%s\" u 1:($3*%6.3f) not w l 1 ",fileresprobmorprev,YEARM/estepm); */
6829: /* fprintf(ficgp,"\n replot \"%s\" u 1:(($3+1.96*$4)*%6.3f) t \"95\%% interval\" w l 2 ",fileresprobmorprev,YEARM/estepm); */
6830: /* fprintf(ficgp,"\n replot \"%s\" u 1:(($3-1.96*$4)*%6.3f) not w l 2 ",fileresprobmorprev,YEARM/estepm); */
6831: fprintf(ficgp,"\n plot \"%s\" u 1:($3) not w l lt 1 ",subdirf(fileresprobmorprev));
6832: fprintf(ficgp,"\n replot \"%s\" u 1:(($3+1.96*$4)) t \"95%% interval\" w l lt 2 ",subdirf(fileresprobmorprev));
6833: fprintf(ficgp,"\n replot \"%s\" u 1:(($3-1.96*$4)) not w l lt 2 ",subdirf(fileresprobmorprev));
6834: fprintf(fichtm,"\n<br> File (multiple files are possible if covariates are present): <A href=\"%s\">%s</a>\n",subdirf(fileresprobmorprev),subdirf(fileresprobmorprev));
6835: 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);
6836: /* 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);
6837: */
6838: /* fprintf(ficgp,"\nset out \"varmuptjgr%s%s%s.svg\";replot;",digitp,optionfilefiname,digit); */
6839: fprintf(ficgp,"\nset out;\nset out \"%s%s.svg\";replot;set out;\n",subdirf3(optionfilefiname,"VARMUPTJGR-",digitp),digit);
6840:
6841: free_vector(xp,1,npar);
6842: free_matrix(doldm,1,nlstate,1,nlstate);
6843: free_matrix(dnewm,1,nlstate,1,npar);
6844: free_matrix(doldmp,nlstate+1,nlstate+ndeath,nlstate+1,nlstate+ndeath);
6845: free_matrix(dnewmp,nlstate+1,nlstate+ndeath,1,npar);
6846: free_matrix(varppt,nlstate+1,nlstate+ndeath,nlstate+1,nlstate+ndeath);
6847: /* if (mobilav!=0) free_ma3x(mobaverage,1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
6848: fclose(ficresprobmorprev);
6849: fflush(ficgp);
6850: fflush(fichtm);
6851: } /* end varevsij */
6852:
6853: /************ Variance of prevlim ******************/
6854: void varprevlim(char fileresvpl[], FILE *ficresvpl, double **varpl, double **matcov, double x[], double delti[], int nlstate, int stepm, double bage, double fage, double **oldm, double **savm, double **prlim, double ftolpl, int *ncvyearp, int ij, char strstart[], int nres)
6855: {
6856: /* Variance of prevalence limit for each state ij using current parameters x[] and estimates of neighbourhood give by delti*/
6857: /* double **prevalim(double **prlim, int nlstate, double *xp, double age, double **oldm, double **savm,double ftolpl);*/
6858:
6859: double **dnewmpar,**doldm;
6860: int i, j, nhstepm, hstepm;
6861: double *xp;
6862: double *gp, *gm;
6863: double **gradg, **trgradg;
6864: double **mgm, **mgp;
6865: double age,agelim;
6866: int theta;
6867:
6868: pstamp(ficresvpl);
6869: fprintf(ficresvpl,"# Standard deviation of period (forward stable) prevalences \n");
6870: fprintf(ficresvpl,"# Age ");
6871: if(nresult >=1)
6872: fprintf(ficresvpl," Result# ");
6873: for(i=1; i<=nlstate;i++)
6874: fprintf(ficresvpl," %1d-%1d",i,i);
6875: fprintf(ficresvpl,"\n");
6876:
6877: xp=vector(1,npar);
6878: dnewmpar=matrix(1,nlstate,1,npar);
6879: doldm=matrix(1,nlstate,1,nlstate);
6880:
6881: hstepm=1*YEARM; /* Every year of age */
6882: hstepm=hstepm/stepm; /* Typically in stepm units, if j= 2 years, = 2/6 months = 4 */
6883: agelim = AGESUP;
6884: for (age=bage; age<=fage; age ++){ /* If stepm=6 months */
6885: nhstepm=(int) rint((agelim-age)*YEARM/stepm); /* Typically 20 years = 20*12/6=40 */
6886: if (stepm >= YEARM) hstepm=1;
6887: nhstepm = nhstepm/hstepm; /* Typically 40/4=10 */
6888: gradg=matrix(1,npar,1,nlstate);
6889: mgp=matrix(1,npar,1,nlstate);
6890: mgm=matrix(1,npar,1,nlstate);
6891: gp=vector(1,nlstate);
6892: gm=vector(1,nlstate);
6893:
6894: for(theta=1; theta <=npar; theta++){
6895: for(i=1; i<=npar; i++){ /* Computes gradient */
6896: xp[i] = x[i] + (i==theta ?delti[theta]:0);
6897: }
6898: /* if((int)age==79 ||(int)age== 80 ||(int)age== 81 ) */
6899: /* prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp,ij,nres); */
6900: /* else */
6901: prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp,ij,nres);
6902: for(i=1;i<=nlstate;i++){
6903: gp[i] = prlim[i][i];
6904: mgp[theta][i] = prlim[i][i];
6905: }
6906: for(i=1; i<=npar; i++) /* Computes gradient */
6907: xp[i] = x[i] - (i==theta ?delti[theta]:0);
6908: /* if((int)age==79 ||(int)age== 80 ||(int)age== 81 ) */
6909: /* prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp,ij,nres); */
6910: /* else */
6911: prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp,ij,nres);
6912: for(i=1;i<=nlstate;i++){
6913: gm[i] = prlim[i][i];
6914: mgm[theta][i] = prlim[i][i];
6915: }
6916: for(i=1;i<=nlstate;i++)
6917: gradg[theta][i]= (gp[i]-gm[i])/2./delti[theta];
6918: /* gradg[theta][2]= -gradg[theta][1]; */ /* For testing if nlstate=2 */
6919: } /* End theta */
6920:
6921: trgradg =matrix(1,nlstate,1,npar);
6922:
6923: for(j=1; j<=nlstate;j++)
6924: for(theta=1; theta <=npar; theta++)
6925: trgradg[j][theta]=gradg[theta][j];
6926: /* if((int)age==79 ||(int)age== 80 ||(int)age== 81 ){ */
6927: /* printf("\nmgm mgp %d ",(int)age); */
6928: /* for(j=1; j<=nlstate;j++){ */
6929: /* printf(" %d ",j); */
6930: /* for(theta=1; theta <=npar; theta++) */
6931: /* printf(" %d %lf %lf",theta,mgm[theta][j],mgp[theta][j]); */
6932: /* printf("\n "); */
6933: /* } */
6934: /* } */
6935: /* if((int)age==79 ||(int)age== 80 ||(int)age== 81 ){ */
6936: /* printf("\n gradg %d ",(int)age); */
6937: /* for(j=1; j<=nlstate;j++){ */
6938: /* printf("%d ",j); */
6939: /* for(theta=1; theta <=npar; theta++) */
6940: /* printf("%d %lf ",theta,gradg[theta][j]); */
6941: /* printf("\n "); */
6942: /* } */
6943: /* } */
6944:
6945: for(i=1;i<=nlstate;i++)
6946: varpl[i][(int)age] =0.;
6947: if((int)age==79 ||(int)age== 80 ||(int)age== 81){
6948: matprod2(dnewmpar,trgradg,1,nlstate,1,npar,1,npar,matcov);
6949: matprod2(doldm,dnewmpar,1,nlstate,1,npar,1,nlstate,gradg);
6950: }else{
6951: matprod2(dnewmpar,trgradg,1,nlstate,1,npar,1,npar,matcov);
6952: matprod2(doldm,dnewmpar,1,nlstate,1,npar,1,nlstate,gradg);
6953: }
6954: for(i=1;i<=nlstate;i++)
6955: varpl[i][(int)age] = doldm[i][i]; /* Covariances are useless */
6956:
6957: fprintf(ficresvpl,"%.0f ",age );
6958: if(nresult >=1)
6959: fprintf(ficresvpl,"%d ",nres );
6960: for(i=1; i<=nlstate;i++){
6961: fprintf(ficresvpl," %.5f (%.5f)",prlim[i][i],sqrt(varpl[i][(int)age]));
6962: /* for(j=1;j<=nlstate;j++) */
6963: /* fprintf(ficresvpl," %d %.5f ",j,prlim[j][i]); */
6964: }
6965: fprintf(ficresvpl,"\n");
6966: free_vector(gp,1,nlstate);
6967: free_vector(gm,1,nlstate);
6968: free_matrix(mgm,1,npar,1,nlstate);
6969: free_matrix(mgp,1,npar,1,nlstate);
6970: free_matrix(gradg,1,npar,1,nlstate);
6971: free_matrix(trgradg,1,nlstate,1,npar);
6972: } /* End age */
6973:
6974: free_vector(xp,1,npar);
6975: free_matrix(doldm,1,nlstate,1,npar);
6976: free_matrix(dnewmpar,1,nlstate,1,nlstate);
6977:
6978: }
6979:
6980:
6981: /************ Variance of backprevalence limit ******************/
6982: void varbrevlim(char fileresvbl[], FILE *ficresvbl, double **varbpl, double **matcov, double x[], double delti[], int nlstate, int stepm, double bage, double fage, double **oldm, double **savm, double **bprlim, double ftolpl, int mobilavproj, int *ncvyearp, int ij, char strstart[], int nres)
6983: {
6984: /* Variance of backward prevalence limit for each state ij using current parameters x[] and estimates of neighbourhood give by delti*/
6985: /* double **prevalim(double **prlim, int nlstate, double *xp, double age, double **oldm, double **savm,double ftolpl);*/
6986:
6987: double **dnewmpar,**doldm;
6988: int i, j, nhstepm, hstepm;
6989: double *xp;
6990: double *gp, *gm;
6991: double **gradg, **trgradg;
6992: double **mgm, **mgp;
6993: double age,agelim;
6994: int theta;
6995:
6996: pstamp(ficresvbl);
6997: fprintf(ficresvbl,"# Standard deviation of back (stable) prevalences \n");
6998: fprintf(ficresvbl,"# Age ");
6999: if(nresult >=1)
7000: fprintf(ficresvbl," Result# ");
7001: for(i=1; i<=nlstate;i++)
7002: fprintf(ficresvbl," %1d-%1d",i,i);
7003: fprintf(ficresvbl,"\n");
7004:
7005: xp=vector(1,npar);
7006: dnewmpar=matrix(1,nlstate,1,npar);
7007: doldm=matrix(1,nlstate,1,nlstate);
7008:
7009: hstepm=1*YEARM; /* Every year of age */
7010: hstepm=hstepm/stepm; /* Typically in stepm units, if j= 2 years, = 2/6 months = 4 */
7011: agelim = AGEINF;
7012: for (age=fage; age>=bage; age --){ /* If stepm=6 months */
7013: nhstepm=(int) rint((age-agelim)*YEARM/stepm); /* Typically 20 years = 20*12/6=40 */
7014: if (stepm >= YEARM) hstepm=1;
7015: nhstepm = nhstepm/hstepm; /* Typically 40/4=10 */
7016: gradg=matrix(1,npar,1,nlstate);
7017: mgp=matrix(1,npar,1,nlstate);
7018: mgm=matrix(1,npar,1,nlstate);
7019: gp=vector(1,nlstate);
7020: gm=vector(1,nlstate);
7021:
7022: for(theta=1; theta <=npar; theta++){
7023: for(i=1; i<=npar; i++){ /* Computes gradient */
7024: xp[i] = x[i] + (i==theta ?delti[theta]:0);
7025: }
7026: if(mobilavproj > 0 )
7027: bprevalim(bprlim, mobaverage,nlstate,xp,age,ftolpl,ncvyearp,ij,nres);
7028: else
7029: bprevalim(bprlim, mobaverage,nlstate,xp,age,ftolpl,ncvyearp,ij,nres);
7030: for(i=1;i<=nlstate;i++){
7031: gp[i] = bprlim[i][i];
7032: mgp[theta][i] = bprlim[i][i];
7033: }
7034: for(i=1; i<=npar; i++) /* Computes gradient */
7035: xp[i] = x[i] - (i==theta ?delti[theta]:0);
7036: if(mobilavproj > 0 )
7037: bprevalim(bprlim, mobaverage,nlstate,xp,age,ftolpl,ncvyearp,ij,nres);
7038: else
7039: bprevalim(bprlim, mobaverage,nlstate,xp,age,ftolpl,ncvyearp,ij,nres);
7040: for(i=1;i<=nlstate;i++){
7041: gm[i] = bprlim[i][i];
7042: mgm[theta][i] = bprlim[i][i];
7043: }
7044: for(i=1;i<=nlstate;i++)
7045: gradg[theta][i]= (gp[i]-gm[i])/2./delti[theta];
7046: /* gradg[theta][2]= -gradg[theta][1]; */ /* For testing if nlstate=2 */
7047: } /* End theta */
7048:
7049: trgradg =matrix(1,nlstate,1,npar);
7050:
7051: for(j=1; j<=nlstate;j++)
7052: for(theta=1; theta <=npar; theta++)
7053: trgradg[j][theta]=gradg[theta][j];
7054: /* if((int)age==79 ||(int)age== 80 ||(int)age== 81 ){ */
7055: /* printf("\nmgm mgp %d ",(int)age); */
7056: /* for(j=1; j<=nlstate;j++){ */
7057: /* printf(" %d ",j); */
7058: /* for(theta=1; theta <=npar; theta++) */
7059: /* printf(" %d %lf %lf",theta,mgm[theta][j],mgp[theta][j]); */
7060: /* printf("\n "); */
7061: /* } */
7062: /* } */
7063: /* if((int)age==79 ||(int)age== 80 ||(int)age== 81 ){ */
7064: /* printf("\n gradg %d ",(int)age); */
7065: /* for(j=1; j<=nlstate;j++){ */
7066: /* printf("%d ",j); */
7067: /* for(theta=1; theta <=npar; theta++) */
7068: /* printf("%d %lf ",theta,gradg[theta][j]); */
7069: /* printf("\n "); */
7070: /* } */
7071: /* } */
7072:
7073: for(i=1;i<=nlstate;i++)
7074: varbpl[i][(int)age] =0.;
7075: if((int)age==79 ||(int)age== 80 ||(int)age== 81){
7076: matprod2(dnewmpar,trgradg,1,nlstate,1,npar,1,npar,matcov);
7077: matprod2(doldm,dnewmpar,1,nlstate,1,npar,1,nlstate,gradg);
7078: }else{
7079: matprod2(dnewmpar,trgradg,1,nlstate,1,npar,1,npar,matcov);
7080: matprod2(doldm,dnewmpar,1,nlstate,1,npar,1,nlstate,gradg);
7081: }
7082: for(i=1;i<=nlstate;i++)
7083: varbpl[i][(int)age] = doldm[i][i]; /* Covariances are useless */
7084:
7085: fprintf(ficresvbl,"%.0f ",age );
7086: if(nresult >=1)
7087: fprintf(ficresvbl,"%d ",nres );
7088: for(i=1; i<=nlstate;i++)
7089: fprintf(ficresvbl," %.5f (%.5f)",bprlim[i][i],sqrt(varbpl[i][(int)age]));
7090: fprintf(ficresvbl,"\n");
7091: free_vector(gp,1,nlstate);
7092: free_vector(gm,1,nlstate);
7093: free_matrix(mgm,1,npar,1,nlstate);
7094: free_matrix(mgp,1,npar,1,nlstate);
7095: free_matrix(gradg,1,npar,1,nlstate);
7096: free_matrix(trgradg,1,nlstate,1,npar);
7097: } /* End age */
7098:
7099: free_vector(xp,1,npar);
7100: free_matrix(doldm,1,nlstate,1,npar);
7101: free_matrix(dnewmpar,1,nlstate,1,nlstate);
7102:
7103: }
7104:
7105: /************ Variance of one-step probabilities ******************/
7106: 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[])
7107: {
7108: int i, j=0, k1, l1, tj;
7109: int k2, l2, j1, z1;
7110: int k=0, l;
7111: int first=1, first1, first2;
7112: int nres=0; /* New */
7113: double cv12, mu1, mu2, lc1, lc2, v12, v21, v11, v22,v1,v2, c12, tnalp;
7114: double **dnewm,**doldm;
7115: double *xp;
7116: double *gp, *gm;
7117: double **gradg, **trgradg;
7118: double **mu;
7119: double age, cov[NCOVMAX+1];
7120: double std=2.0; /* Number of standard deviation wide of confidence ellipsoids */
7121: int theta;
7122: char fileresprob[FILENAMELENGTH];
7123: char fileresprobcov[FILENAMELENGTH];
7124: char fileresprobcor[FILENAMELENGTH];
7125: double ***varpij;
7126:
7127: strcpy(fileresprob,"PROB_");
7128: strcat(fileresprob,fileres);
7129: if((ficresprob=fopen(fileresprob,"w"))==NULL) {
7130: printf("Problem with resultfile: %s\n", fileresprob);
7131: fprintf(ficlog,"Problem with resultfile: %s\n", fileresprob);
7132: }
7133: strcpy(fileresprobcov,"PROBCOV_");
7134: strcat(fileresprobcov,fileresu);
7135: if((ficresprobcov=fopen(fileresprobcov,"w"))==NULL) {
7136: printf("Problem with resultfile: %s\n", fileresprobcov);
7137: fprintf(ficlog,"Problem with resultfile: %s\n", fileresprobcov);
7138: }
7139: strcpy(fileresprobcor,"PROBCOR_");
7140: strcat(fileresprobcor,fileresu);
7141: if((ficresprobcor=fopen(fileresprobcor,"w"))==NULL) {
7142: printf("Problem with resultfile: %s\n", fileresprobcor);
7143: fprintf(ficlog,"Problem with resultfile: %s\n", fileresprobcor);
7144: }
7145: printf("Computing standard deviation of one-step probabilities: result on file '%s' \n",fileresprob);
7146: fprintf(ficlog,"Computing standard deviation of one-step probabilities: result on file '%s' \n",fileresprob);
7147: printf("Computing matrix of variance covariance of one-step probabilities: result on file '%s' \n",fileresprobcov);
7148: fprintf(ficlog,"Computing matrix of variance covariance of one-step probabilities: result on file '%s' \n",fileresprobcov);
7149: printf("and correlation matrix of one-step probabilities: result on file '%s' \n",fileresprobcor);
7150: fprintf(ficlog,"and correlation matrix of one-step probabilities: result on file '%s' \n",fileresprobcor);
7151: pstamp(ficresprob);
7152: fprintf(ficresprob,"#One-step probabilities and stand. devi in ()\n");
7153: fprintf(ficresprob,"# Age");
7154: pstamp(ficresprobcov);
7155: fprintf(ficresprobcov,"#One-step probabilities and covariance matrix\n");
7156: fprintf(ficresprobcov,"# Age");
7157: pstamp(ficresprobcor);
7158: fprintf(ficresprobcor,"#One-step probabilities and correlation matrix\n");
7159: fprintf(ficresprobcor,"# Age");
7160:
7161:
7162: for(i=1; i<=nlstate;i++)
7163: for(j=1; j<=(nlstate+ndeath);j++){
7164: fprintf(ficresprob," p%1d-%1d (SE)",i,j);
7165: fprintf(ficresprobcov," p%1d-%1d ",i,j);
7166: fprintf(ficresprobcor," p%1d-%1d ",i,j);
7167: }
7168: /* fprintf(ficresprob,"\n");
7169: fprintf(ficresprobcov,"\n");
7170: fprintf(ficresprobcor,"\n");
7171: */
7172: xp=vector(1,npar);
7173: dnewm=matrix(1,(nlstate)*(nlstate+ndeath),1,npar);
7174: doldm=matrix(1,(nlstate)*(nlstate+ndeath),1,(nlstate)*(nlstate+ndeath));
7175: mu=matrix(1,(nlstate)*(nlstate+ndeath), (int) bage, (int)fage);
7176: varpij=ma3x(1,nlstate*(nlstate+ndeath),1,nlstate*(nlstate+ndeath),(int) bage, (int) fage);
7177: first=1;
7178: fprintf(ficgp,"\n# Routine varprob");
7179: fprintf(fichtm,"\n<li><h4> Computing and drawing one step probabilities with their confidence intervals</h4></li>\n");
7180: fprintf(fichtm,"\n");
7181:
7182: 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. File %s</li>\n",optionfilehtmcov,optionfilehtmcov);
7183: 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);
7184: fprintf(fichtmcov,"\nEllipsoids of confidence centered on point (p<inf>ij</inf>, p<inf>kl</inf>) are estimated \
7185: and drawn. It helps understanding how is the covariance between two incidences.\
7186: They are expressed in year<sup>-1</sup> in order to be less dependent of stepm.<br>\n");
7187: 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. \
7188: It can be understood this way: if pij and pkl where uncorrelated the (2x2) matrix of covariance \
7189: would have been (1/(var pij), 0 , 0, 1/(var pkl)), and the confidence interval would be 2 \
7190: standard deviations wide on each axis. <br>\
7191: Now, if both incidences are correlated (usual case) we diagonalised the inverse of the covariance matrix\
7192: and made the appropriate rotation to look at the uncorrelated principal directions.<br>\
7193: To be simple, these graphs help to understand the significativity of each parameter in relation to a second other one.<br> \n");
7194:
7195: cov[1]=1;
7196: /* tj=cptcoveff; */
7197: tj = (int) pow(2,cptcoveff);
7198: if (cptcovn<1) {tj=1;ncodemax[1]=1;}
7199: j1=0;
7200:
7201: for(nres=1;nres <=nresult; nres++){ /* For each resultline */
7202: for(j1=1; j1<=tj;j1++){ /* For any combination of dummy covariates, fixed and varying */
7203: printf("Varprob TKresult[nres]=%d j1=%d, nres=%d, cptcovn=%d, cptcoveff=%d tj=%d cptcovs=%d\n", TKresult[nres], j1, nres, cptcovn, cptcoveff, tj, cptcovs);
7204: if(tj != 1 && TKresult[nres]!= j1)
7205: continue;
7206:
7207: /* for(j1=1; j1<=tj;j1++){ /\* For each valid combination of covariates or only once*\/ */
7208: /* for(nres=1;nres <=1; nres++){ /\* For each resultline *\/ */
7209: /* /\* for(nres=1;nres <=nresult; nres++){ /\\* For each resultline *\\/ *\/ */
7210: if (cptcovn>0) {
7211: fprintf(ficresprob, "\n#********** Variable ");
7212: fprintf(ficresprobcov, "\n#********** Variable ");
7213: fprintf(ficgp, "\n#********** Variable ");
7214: fprintf(fichtmcov, "\n<hr size=\"2\" color=\"#EC5E5E\">********** Variable ");
7215: fprintf(ficresprobcor, "\n#********** Variable ");
7216:
7217: /* Including quantitative variables of the resultline to be done */
7218: for (z1=1; z1<=cptcovs; z1++){ /* Loop on each variable of this resultline */
7219: printf("Varprob modelresult[%d][%d]=%d model=1+age+%s \n",nres, z1, modelresult[nres][z1], model);
7220: fprintf(ficlog,"Varprob modelresult[%d][%d]=%d model=1+age+%s \n",nres, z1, modelresult[nres][z1], model);
7221: /* fprintf(ficlog,"Varprob modelresult[%d][%d]=%d model=1+age+%s resultline[%d]=%s \n",nres, z1, modelresult[nres][z1], model, nres, resultline[nres]); */
7222: if(Dummy[modelresult[nres][z1]]==0){/* Dummy variable of the variable in position modelresult in the model corresponding to z1 in resultline */
7223: if(Fixed[modelresult[nres][z1]]==0){ /* Fixed referenced to model equation */
7224: fprintf(ficresprob,"V%d=%d ",Tvresult[nres][z1],Tresult[nres][z1]); /* Output of each value for the combination TKresult[nres], ordere by the covariate values in the resultline */
7225: fprintf(ficresprobcov,"V%d=%d ",Tvresult[nres][z1],Tresult[nres][z1]); /* Output of each value for the combination TKresult[nres], ordere by the covariate values in the resultline */
7226: fprintf(ficgp,"V%d=%d ",Tvresult[nres][z1],Tresult[nres][z1]); /* Output of each value for the combination TKresult[nres], ordere by the covariate values in the resultline */
7227: fprintf(fichtmcov,"V%d=%d ",Tvresult[nres][z1],Tresult[nres][z1]); /* Output of each value for the combination TKresult[nres], ordere by the covariate values in the resultline */
7228: fprintf(ficresprobcor,"V%d=%d ",Tvresult[nres][z1],Tresult[nres][z1]); /* Output of each value for the combination TKresult[nres], ordere by the covariate values in the resultline */
7229: fprintf(ficresprob,"fixed ");
7230: fprintf(ficresprobcov,"fixed ");
7231: fprintf(ficgp,"fixed ");
7232: fprintf(fichtmcov,"fixed ");
7233: fprintf(ficresprobcor,"fixed ");
7234: }else{
7235: fprintf(ficresprob,"varyi ");
7236: fprintf(ficresprobcov,"varyi ");
7237: fprintf(ficgp,"varyi ");
7238: fprintf(fichtmcov,"varyi ");
7239: fprintf(ficresprobcor,"varyi ");
7240: }
7241: }else if(Dummy[modelresult[nres][z1]]==1){ /* Quanti variable */
7242: /* For each selected (single) quantitative value */
7243: fprintf(ficresprob," V%d=%lg ",Tvqresult[nres][z1],Tqresult[nres][z1]);
7244: if(Fixed[modelresult[nres][z1]]==0){ /* Fixed */
7245: fprintf(ficresprob,"fixed ");
7246: fprintf(ficresprobcov,"fixed ");
7247: fprintf(ficgp,"fixed ");
7248: fprintf(fichtmcov,"fixed ");
7249: fprintf(ficresprobcor,"fixed ");
7250: }else{
7251: fprintf(ficresprob,"varyi ");
7252: fprintf(ficresprobcov,"varyi ");
7253: fprintf(ficgp,"varyi ");
7254: fprintf(fichtmcov,"varyi ");
7255: fprintf(ficresprobcor,"varyi ");
7256: }
7257: }else{
7258: printf("Error in varprob() Dummy[modelresult[%d][%d]]=%d, modelresult[%d][%d]=V%d cptcovs=%d, cptcoveff=%d \n", nres, z1, Dummy[modelresult[nres][z1]],nres,z1,modelresult[nres][z1],cptcovs, cptcoveff); /* end if dummy or quanti */
7259: fprintf(ficlog,"Error in varprob() Dummy[modelresult[%d][%d]]=%d, modelresult[%d][%d]=V%d cptcovs=%d, cptcoveff=%d \n", nres, z1, Dummy[modelresult[nres][z1]],nres,z1,modelresult[nres][z1],cptcovs, cptcoveff); /* end if dummy or quanti */
7260: exit(1);
7261: }
7262: } /* End loop on variable of this resultline */
7263: /* for (z1=1; z1<=cptcoveff; z1++) fprintf(ficresprob, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]); */
7264: fprintf(ficresprob, "**********\n#\n");
7265: fprintf(ficresprobcov, "**********\n#\n");
7266: fprintf(ficgp, "**********\n#\n");
7267: fprintf(fichtmcov, "**********\n<hr size=\"2\" color=\"#EC5E5E\">");
7268: fprintf(ficresprobcor, "**********\n#");
7269: if(invalidvarcomb[j1]){
7270: fprintf(ficgp,"\n#Combination (%d) ignored because no cases \n",j1);
7271: fprintf(fichtmcov,"\n<h3>Combination (%d) ignored because no cases </h3>\n",j1);
7272: continue;
7273: }
7274: }
7275: gradg=matrix(1,npar,1,(nlstate)*(nlstate+ndeath));
7276: trgradg=matrix(1,(nlstate)*(nlstate+ndeath),1,npar);
7277: gp=vector(1,(nlstate)*(nlstate+ndeath));
7278: gm=vector(1,(nlstate)*(nlstate+ndeath));
7279: for (age=bage; age<=fage; age ++){ /* Fo each age we feed the model equation with covariates, using precov as in hpxij() ? */
7280: cov[2]=age;
7281: if(nagesqr==1)
7282: cov[3]= age*age;
7283: /* New code end of combination but for each resultline */
7284: for(k1=1;k1<=cptcovt;k1++){ /* loop on model equation (including products) */
7285: if(Typevar[k1]==1){ /* A product with age */
7286: cov[2+nagesqr+k1]=precov[nres][k1]*cov[2];
7287: }else{
7288: cov[2+nagesqr+k1]=precov[nres][k1];
7289: }
7290: }/* End of loop on model equation */
7291: /* Old code */
7292: /* /\* for (k=1; k<=cptcovn;k++) { *\/ */
7293: /* /\* cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(j1,k)]; *\/ */
7294: /* for (k=1; k<=nsd;k++) { /\* For single dummy covariates only *\/ */
7295: /* /\* Here comes the value of the covariate 'j1' after renumbering k with single dummy covariates *\/ */
7296: /* cov[2+nagesqr+TvarsDind[k]]=nbcode[TvarsD[k]][codtabm(j1,TnsdVar[TvarsD[k]])]; */
7297: /* /\*cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(j1,Tvar[k])];*\//\* j1 1 2 3 4 */
7298: /* * 1 1 1 1 1 */
7299: /* * 2 2 1 1 1 */
7300: /* * 3 1 2 1 1 */
7301: /* *\/ */
7302: /* /\* nbcode[1][1]=0 nbcode[1][2]=1;*\/ */
7303: /* } */
7304: /* /\* V2+V1+V4+V3*age Tvar[4]=3 ; V1+V2*age Tvar[2]=2; V1+V1*age Tvar[2]=1, Tage[1]=2 *\/ */
7305: /* /\* ) p nbcode[Tvar[Tage[k]]][(1 & (ij-1) >> (k-1))+1] *\/ */
7306: /* /\*for (k=1; k<=cptcovage;k++) cov[2+Tage[k]]=cov[2+Tage[k]]*cov[2]; *\/ */
7307: /* for (k=1; k<=cptcovage;k++){ /\* For product with age *\/ */
7308: /* if(Dummy[Tage[k]]==2){ /\* dummy with age *\/ */
7309: /* cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(j1,TnsdVar[Tvar[Tage[k]]])]*cov[2]; */
7310: /* /\* cov[++k1]=nbcode[Tvar[Tage[k]]][codtabm(ij,k)]*cov[2]; *\/ */
7311: /* } else if(Dummy[Tage[k]]==3){ /\* quantitative with age *\/ */
7312: /* printf("Internal IMaCh error, don't know which value for quantitative covariate with age, Tage[k]%d, k=%d, Tvar[Tage[k]]=V%d, age=%d\n",Tage[k],k ,Tvar[Tage[k]], (int)cov[2]); */
7313: /* /\* cov[2+nagesqr+Tage[k]]=meanq[k]/idq[k]*cov[2];/\\* Using the mean of quantitative variable Tvar[Tage[k]] /\\* Tqresult[nres][k]; *\\/ *\/ */
7314: /* /\* exit(1); *\/ */
7315: /* /\* cov[++k1]=Tqresult[nres][k]; *\/ */
7316: /* } */
7317: /* /\* cov[2+Tage[k]+nagesqr]=nbcode[Tvar[Tage[k]]][codtabm(ij,k)]*cov[2]; *\/ */
7318: /* } */
7319: /* for (k=1; k<=cptcovprod;k++){/\* For product without age *\/ */
7320: /* if(Dummy[Tvard[k][1]]==0){ */
7321: /* if(Dummy[Tvard[k][2]]==0){ */
7322: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(j1,TnsdVar[Tvard[k][1]])] * nbcode[Tvard[k][2]][codtabm(j1,TnsdVar[Tvard[k][2]])]; */
7323: /* /\* cov[++k1]=nbcode[Tvard[k][1]][codtabm(ij,k)] * nbcode[Tvard[k][2]][codtabm(ij,k)]; *\/ */
7324: /* }else{ /\* Should we use the mean of the quantitative variables? *\/ */
7325: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(j1,TnsdVar[Tvard[k][1]])] * Tqresult[nres][resultmodel[nres][k]]; */
7326: /* /\* cov[++k1]=nbcode[Tvard[k][1]][codtabm(ij,k)] * Tqresult[nres][k]; *\/ */
7327: /* } */
7328: /* }else{ */
7329: /* if(Dummy[Tvard[k][2]]==0){ */
7330: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][2]][codtabm(j1,TnsdVar[Tvard[k][2]])] * Tqinvresult[nres][TnsdVar[Tvard[k][1]]]; */
7331: /* /\* cov[++k1]=nbcode[Tvard[k][2]][codtabm(ij,k)] * Tqinvresult[nres][Tvard[k][1]]; *\/ */
7332: /* }else{ */
7333: /* cov[2+nagesqr+Tprod[k]]=Tqinvresult[nres][TnsdVar[Tvard[k][1]]]* Tqinvresult[nres][TnsdVar[Tvard[k][2]]]; */
7334: /* /\* cov[++k1]=Tqinvresult[nres][Tvard[k][1]]* Tqinvresult[nres][Tvard[k][2]]; *\/ */
7335: /* } */
7336: /* } */
7337: /* /\* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)]*nbcode[Tvard[k][2]][codtabm(ij,k)]; *\/ */
7338: /* } */
7339: /* For each age and combination of dummy covariates we slightly move the parameters of delti in order to get the gradient*/
7340: for(theta=1; theta <=npar; theta++){
7341: for(i=1; i<=npar; i++)
7342: xp[i] = x[i] + (i==theta ?delti[theta]:(double)0);
7343:
7344: pmij(pmmij,cov,ncovmodel,xp,nlstate);
7345:
7346: k=0;
7347: for(i=1; i<= (nlstate); i++){
7348: for(j=1; j<=(nlstate+ndeath);j++){
7349: k=k+1;
7350: gp[k]=pmmij[i][j];
7351: }
7352: }
7353:
7354: for(i=1; i<=npar; i++)
7355: xp[i] = x[i] - (i==theta ?delti[theta]:(double)0);
7356:
7357: pmij(pmmij,cov,ncovmodel,xp,nlstate);
7358: k=0;
7359: for(i=1; i<=(nlstate); i++){
7360: for(j=1; j<=(nlstate+ndeath);j++){
7361: k=k+1;
7362: gm[k]=pmmij[i][j];
7363: }
7364: }
7365:
7366: for(i=1; i<= (nlstate)*(nlstate+ndeath); i++)
7367: gradg[theta][i]=(gp[i]-gm[i])/(double)2./delti[theta];
7368: }
7369:
7370: for(j=1; j<=(nlstate)*(nlstate+ndeath);j++)
7371: for(theta=1; theta <=npar; theta++)
7372: trgradg[j][theta]=gradg[theta][j];
7373:
7374: matprod2(dnewm,trgradg,1,(nlstate)*(nlstate+ndeath),1,npar,1,npar,matcov);
7375: matprod2(doldm,dnewm,1,(nlstate)*(nlstate+ndeath),1,npar,1,(nlstate)*(nlstate+ndeath),gradg);
7376:
7377: pmij(pmmij,cov,ncovmodel,x,nlstate);
7378:
7379: k=0;
7380: for(i=1; i<=(nlstate); i++){
7381: for(j=1; j<=(nlstate+ndeath);j++){
7382: k=k+1;
7383: mu[k][(int) age]=pmmij[i][j];
7384: }
7385: }
7386: for(i=1;i<=(nlstate)*(nlstate+ndeath);i++)
7387: for(j=1;j<=(nlstate)*(nlstate+ndeath);j++)
7388: varpij[i][j][(int)age] = doldm[i][j];
7389:
7390: /*printf("\n%d ",(int)age);
7391: for (i=1; i<=(nlstate)*(nlstate+ndeath);i++){
7392: printf("%e [%e ;%e] ",gm[i],gm[i]-2*sqrt(doldm[i][i]),gm[i]+2*sqrt(doldm[i][i]));
7393: fprintf(ficlog,"%e [%e ;%e] ",gm[i],gm[i]-2*sqrt(doldm[i][i]),gm[i]+2*sqrt(doldm[i][i]));
7394: }*/
7395:
7396: fprintf(ficresprob,"\n%d ",(int)age);
7397: fprintf(ficresprobcov,"\n%d ",(int)age);
7398: fprintf(ficresprobcor,"\n%d ",(int)age);
7399:
7400: for (i=1; i<=(nlstate)*(nlstate+ndeath);i++)
7401: fprintf(ficresprob,"%11.3e (%11.3e) ",mu[i][(int) age],sqrt(varpij[i][i][(int)age]));
7402: for (i=1; i<=(nlstate)*(nlstate+ndeath);i++){
7403: fprintf(ficresprobcov,"%11.3e ",mu[i][(int) age]);
7404: fprintf(ficresprobcor,"%11.3e ",mu[i][(int) age]);
7405: }
7406: i=0;
7407: for (k=1; k<=(nlstate);k++){
7408: for (l=1; l<=(nlstate+ndeath);l++){
7409: i++;
7410: fprintf(ficresprobcov,"\n%d %d-%d",(int)age,k,l);
7411: fprintf(ficresprobcor,"\n%d %d-%d",(int)age,k,l);
7412: for (j=1; j<=i;j++){
7413: /* printf(" k=%d l=%d i=%d j=%d\n",k,l,i,j);fflush(stdout); */
7414: fprintf(ficresprobcov," %11.3e",varpij[i][j][(int)age]);
7415: fprintf(ficresprobcor," %11.3e",varpij[i][j][(int) age]/sqrt(varpij[i][i][(int) age])/sqrt(varpij[j][j][(int)age]));
7416: }
7417: }
7418: }/* end of loop for state */
7419: } /* end of loop for age */
7420: free_vector(gp,1,(nlstate+ndeath)*(nlstate+ndeath));
7421: free_vector(gm,1,(nlstate+ndeath)*(nlstate+ndeath));
7422: free_matrix(trgradg,1,(nlstate+ndeath)*(nlstate+ndeath),1,npar);
7423: free_matrix(gradg,1,(nlstate+ndeath)*(nlstate+ndeath),1,npar);
7424:
7425: /* Confidence intervalle of pij */
7426: /*
7427: fprintf(ficgp,"\nunset parametric;unset label");
7428: fprintf(ficgp,"\nset log y;unset log x; set xlabel \"Age\";set ylabel \"probability (year-1)\"");
7429: fprintf(ficgp,"\nset ter png small\nset size 0.65,0.65");
7430: 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);
7431: fprintf(fichtm,"\n<br><img src=\"pijgr%s.png\"> ",optionfilefiname);
7432: fprintf(ficgp,"\nset out \"pijgr%s.png\"",optionfilefiname);
7433: fprintf(ficgp,"\nplot \"%s\" every :::%d::%d u 1:2 \"\%%lf",k1,k2,xfilevarprob);
7434: */
7435:
7436: /* Drawing ellipsoids of confidence of two variables p(k1-l1,k2-l2)*/
7437: first1=1;first2=2;
7438: for (k2=1; k2<=(nlstate);k2++){
7439: for (l2=1; l2<=(nlstate+ndeath);l2++){
7440: if(l2==k2) continue;
7441: j=(k2-1)*(nlstate+ndeath)+l2;
7442: for (k1=1; k1<=(nlstate);k1++){
7443: for (l1=1; l1<=(nlstate+ndeath);l1++){
7444: if(l1==k1) continue;
7445: i=(k1-1)*(nlstate+ndeath)+l1;
7446: if(i<=j) continue;
7447: for (age=bage; age<=fage; age ++){
7448: if ((int)age %5==0){
7449: v1=varpij[i][i][(int)age]/stepm*YEARM/stepm*YEARM;
7450: v2=varpij[j][j][(int)age]/stepm*YEARM/stepm*YEARM;
7451: cv12=varpij[i][j][(int)age]/stepm*YEARM/stepm*YEARM;
7452: mu1=mu[i][(int) age]/stepm*YEARM ;
7453: mu2=mu[j][(int) age]/stepm*YEARM;
7454: c12=cv12/sqrt(v1*v2);
7455: /* Computing eigen value of matrix of covariance */
7456: lc1=((v1+v2)+sqrt((v1+v2)*(v1+v2) - 4*(v1*v2-cv12*cv12)))/2.;
7457: lc2=((v1+v2)-sqrt((v1+v2)*(v1+v2) - 4*(v1*v2-cv12*cv12)))/2.;
7458: if ((lc2 <0) || (lc1 <0) ){
7459: if(first2==1){
7460: first1=0;
7461: 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);
7462: }
7463: 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);
7464: /* lc1=fabs(lc1); */ /* If we want to have them positive */
7465: /* lc2=fabs(lc2); */
7466: }
7467:
7468: /* Eigen vectors */
7469: if(1+(v1-lc1)*(v1-lc1)/cv12/cv12 <1.e-5){
7470: printf(" Error sqrt of a negative number: %lf\n",1+(v1-lc1)*(v1-lc1)/cv12/cv12);
7471: fprintf(ficlog," Error sqrt of a negative number: %lf\n",1+(v1-lc1)*(v1-lc1)/cv12/cv12);
7472: v11=(1./sqrt(fabs(1+(v1-lc1)*(v1-lc1)/cv12/cv12)));
7473: }else
7474: v11=(1./sqrt(1+(v1-lc1)*(v1-lc1)/cv12/cv12));
7475: /*v21=sqrt(1.-v11*v11); *//* error */
7476: v21=(lc1-v1)/cv12*v11;
7477: v12=-v21;
7478: v22=v11;
7479: tnalp=v21/v11;
7480: if(first1==1){
7481: first1=0;
7482: 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);
7483: }
7484: 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);
7485: /*printf(fignu*/
7486: /* mu1+ v11*lc1*cost + v12*lc2*sin(t) */
7487: /* mu2+ v21*lc1*cost + v22*lc2*sin(t) */
7488: if(first==1){
7489: first=0;
7490: fprintf(ficgp,"\n# Ellipsoids of confidence\n#\n");
7491: fprintf(ficgp,"\nset parametric;unset label");
7492: 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);
7493: fprintf(ficgp,"\nset ter svg size 640, 480");
7494: fprintf(fichtmcov,"\n<p><br>Ellipsoids of confidence cov(p%1d%1d,p%1d%1d) expressed in year<sup>-1</sup>\
7495: :<a href=\"%s_%d%1d%1d-%1d%1d.svg\"> \
7496: %s_%d%1d%1d-%1d%1d.svg</A>, ",k1,l1,k2,l2,\
7497: subdirf2(optionfilefiname,"VARPIJGR_"), j1,k1,l1,k2,l2, \
7498: subdirf2(optionfilefiname,"VARPIJGR_"), j1,k1,l1,k2,l2);
7499: fprintf(fichtmcov,"\n<br><img src=\"%s_%d%1d%1d-%1d%1d.svg\"> ",subdirf2(optionfilefiname,"VARPIJGR_"), j1,k1,l1,k2,l2);
7500: fprintf(fichtmcov,"\n<br> Correlation at age %d (%.3f),",(int) age, c12);
7501: fprintf(ficgp,"\nset out \"%s_%d%1d%1d-%1d%1d.svg\"",subdirf2(optionfilefiname,"VARPIJGR_"), j1,k1,l1,k2,l2);
7502: fprintf(ficgp,"\nset label \"%d\" at %11.3e,%11.3e center",(int) age, mu1,mu2);
7503: fprintf(ficgp,"\n# Age %d, p%1d%1d - p%1d%1d",(int) age, k1,l1,k2,l2);
7504: 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", \
7505: mu1,std,v11,sqrt(fabs(lc1)),v12,sqrt(fabs(lc2)), \
7506: mu2,std,v21,sqrt(fabs(lc1)),v22,sqrt(fabs(lc2))); /* For gnuplot only */
7507: }else{
7508: first=0;
7509: fprintf(fichtmcov," %d (%.3f),",(int) age, c12);
7510: fprintf(ficgp,"\n# Age %d, p%1d%1d - p%1d%1d",(int) age, k1,l1,k2,l2);
7511: fprintf(ficgp,"\nset label \"%d\" at %11.3e,%11.3e center",(int) age, mu1,mu2);
7512: 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", \
7513: mu1,std,v11,sqrt(lc1),v12,sqrt(fabs(lc2)), \
7514: mu2,std,v21,sqrt(lc1),v22,sqrt(fabs(lc2)));
7515: }/* if first */
7516: } /* age mod 5 */
7517: } /* end loop age */
7518: fprintf(ficgp,"\nset out;\nset out \"%s_%d%1d%1d-%1d%1d.svg\";replot;set out;",subdirf2(optionfilefiname,"VARPIJGR_"), j1,k1,l1,k2,l2);
7519: first=1;
7520: } /*l12 */
7521: } /* k12 */
7522: } /*l1 */
7523: }/* k1 */
7524: } /* loop on combination of covariates j1 */
7525: } /* loop on nres */
7526: free_ma3x(varpij,1,nlstate,1,nlstate+ndeath,(int) bage, (int)fage);
7527: free_matrix(mu,1,(nlstate+ndeath)*(nlstate+ndeath),(int) bage, (int)fage);
7528: free_matrix(doldm,1,(nlstate)*(nlstate+ndeath),1,(nlstate)*(nlstate+ndeath));
7529: free_matrix(dnewm,1,(nlstate)*(nlstate+ndeath),1,npar);
7530: free_vector(xp,1,npar);
7531: fclose(ficresprob);
7532: fclose(ficresprobcov);
7533: fclose(ficresprobcor);
7534: fflush(ficgp);
7535: fflush(fichtmcov);
7536: }
7537:
7538:
7539: /******************* Printing html file ***********/
7540: void printinghtml(char fileresu[], char title[], char datafile[], int firstpass, \
7541: int lastpass, int stepm, int weightopt, char model[],\
7542: int imx,int jmin, int jmax, double jmeanint,char rfileres[],\
7543: int popforecast, int mobilav, int prevfcast, int mobilavproj, int prevbcast, int estepm , \
7544: double jprev1, double mprev1,double anprev1, double dateprev1, double dateprojd, double dateback1, \
7545: double jprev2, double mprev2,double anprev2, double dateprev2, double dateprojf, double dateback2){
7546: int jj1, k1, i1, cpt, k4, nres;
7547: /* In fact some results are already printed in fichtm which is open */
7548: fprintf(fichtm,"<ul><li><a href='#firstorder'>Result files (first order: no variance)</a>\n \
7549: <li><a href='#secondorder'>Result files (second order (variance)</a>\n \
7550: </ul>");
7551: /* fprintf(fichtm,"<ul><li> model=1+age+%s\n \ */
7552: /* </ul>", model); */
7553: fprintf(fichtm,"<ul><li><h4><a name='firstorder'>Result files (first order: no variance)</a></h4>\n");
7554: 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",
7555: jprev1, mprev1,anprev1,jprev2, mprev2,anprev2,subdirfext3(optionfilefiname,"PHTMFR_",".htm"),subdirfext3(optionfilefiname,"PHTMFR_",".htm"));
7556: fprintf(fichtm,"<li> - Observed prevalence (cross-sectional prevalence) in each state (during the period defined between %.lf/%.lf/%.lf and %.lf/%.lf/%.lf): <a href=\"%s\">%s</a> (html file) ",
7557: jprev1, mprev1,anprev1,jprev2, mprev2,anprev2,subdirfext3(optionfilefiname,"PHTM_",".htm"),subdirfext3(optionfilefiname,"PHTM_",".htm"));
7558: fprintf(fichtm,", <a href=\"%s\">%s</a> (text file) <br>\n",subdirf2(fileresu,"P_"),subdirf2(fileresu,"P_"));
7559: fprintf(fichtm,"\
7560: - Estimated transition probabilities over %d (stepm) months: <a href=\"%s\">%s</a><br>\n ",
7561: stepm,subdirf2(fileresu,"PIJ_"),subdirf2(fileresu,"PIJ_"));
7562: fprintf(fichtm,"\
7563: - Estimated back transition probabilities over %d (stepm) months: <a href=\"%s\">%s</a><br>\n ",
7564: stepm,subdirf2(fileresu,"PIJB_"),subdirf2(fileresu,"PIJB_"));
7565: fprintf(fichtm,"\
7566: - Period (forward) prevalence in each health state: <a href=\"%s\">%s</a> <br>\n",
7567: subdirf2(fileresu,"PL_"),subdirf2(fileresu,"PL_"));
7568: fprintf(fichtm,"\
7569: - Backward prevalence in each health state: <a href=\"%s\">%s</a> <br>\n",
7570: subdirf2(fileresu,"PLB_"),subdirf2(fileresu,"PLB_"));
7571: fprintf(fichtm,"\
7572: - (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): \
7573: <a href=\"%s\">%s</a> <br>\n",
7574: estepm,subdirf2(fileresu,"E_"),subdirf2(fileresu,"E_"));
7575: if(prevfcast==1){
7576: fprintf(fichtm,"\
7577: - Prevalence projections by age and states: \
7578: <a href=\"%s\">%s</a> <br>\n</li>", subdirf2(fileresu,"F_"),subdirf2(fileresu,"F_"));
7579: }
7580:
7581:
7582: m=pow(2,cptcoveff);
7583: if (cptcovn < 1) {m=1;ncodemax[1]=1;}
7584:
7585: fprintf(fichtm," \n<ul><li><b>Graphs (first order)</b></li><p>");
7586:
7587: jj1=0;
7588:
7589: fprintf(fichtm," \n<ul>");
7590: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
7591: /* k1=nres; */
7592: k1=TKresult[nres];
7593: if(TKresult[nres]==0)k1=1; /* To be checked for no result */
7594: /* for(k1=1; k1<=m;k1++){ /\* For each combination of covariate *\/ */
7595: /* if(m != 1 && TKresult[nres]!= k1) */
7596: /* continue; */
7597: jj1++;
7598: if (cptcovn > 0) {
7599: fprintf(fichtm,"\n<li><a size=\"1\" color=\"#EC5E5E\" href=\"#rescov");
7600: for (cpt=1; cpt<=cptcovs;cpt++){ /**< cptcovs number of SIMPLE covariates in the model V2+V1 =2 (dummy or quantit or time varying) */
7601: fprintf(fichtm,"_V%d=%lg_",Tvresult[nres][cpt],TinvDoQresult[nres][Tvresult[nres][cpt]]);
7602: }
7603: /* for (cpt=1; cpt<=cptcoveff;cpt++){ */
7604: /* fprintf(fichtm,"_V%d=%d_",Tvresult[nres][cpt],(int)Tresult[nres][cpt]); */
7605: /* } */
7606: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
7607: /* fprintf(fichtm,"_V%d=%f_",Tvqresult[nres][k4],Tqresult[nres][k4]); */
7608: /* } */
7609: fprintf(fichtm,"\">");
7610:
7611: /* if(nqfveff+nqtveff 0) */ /* Test to be done */
7612: fprintf(fichtm,"************ Results for covariates");
7613: for (cpt=1; cpt<=cptcovs;cpt++){
7614: fprintf(fichtm," V%d=%lg ",Tvresult[nres][cpt],TinvDoQresult[nres][Tvresult[nres][cpt]]);
7615: }
7616: /* fprintf(fichtm,"************ Results for covariates"); */
7617: /* for (cpt=1; cpt<=cptcoveff;cpt++){ */
7618: /* fprintf(fichtm," V%d=%d ",Tvresult[nres][cpt],(int)Tresult[nres][cpt]); */
7619: /* } */
7620: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
7621: /* fprintf(fichtm," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); */
7622: /* } */
7623: if(invalidvarcomb[k1]){
7624: fprintf(fichtm," Warning Combination (%d) ignored because no cases ",k1);
7625: continue;
7626: }
7627: fprintf(fichtm,"</a></li>");
7628: } /* cptcovn >0 */
7629: }
7630: fprintf(fichtm," \n</ul>");
7631:
7632: jj1=0;
7633:
7634: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
7635: /* k1=nres; */
7636: k1=TKresult[nres];
7637: if(TKresult[nres]==0) k1=1; /* To be checked for noresult */
7638: /* for(k1=1; k1<=m;k1++){ /\* For each combination of covariate *\/ */
7639: /* if(m != 1 && TKresult[nres]!= k1) */
7640: /* continue; */
7641:
7642: /* for(i1=1; i1<=ncodemax[k1];i1++){ */
7643: jj1++;
7644: if (cptcovn > 0) {
7645: fprintf(fichtm,"\n<p><a name=\"rescov");
7646: for (cpt=1; cpt<=cptcovs;cpt++){
7647: fprintf(fichtm,"_V%d=%lg_",Tvresult[nres][cpt],TinvDoQresult[nres][Tvresult[nres][cpt]]);
7648: }
7649: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
7650: /* fprintf(fichtm,"_V%d=%f_",Tvqresult[nres][k4],Tqresult[nres][k4]); */
7651: /* } */
7652: fprintf(fichtm,"\"</a>");
7653:
7654: fprintf(fichtm,"<hr size=\"2\" color=\"#EC5E5E\">************ Results for covariates");
7655: for (cpt=1; cpt<=cptcovs;cpt++){
7656: fprintf(fichtm," V%d=%lg ",Tvresult[nres][cpt],TinvDoQresult[nres][Tvresult[nres][cpt]]);
7657: printf(" V%d=%lg ",Tvresult[nres][cpt],TinvDoQresult[nres][Tvresult[nres][cpt]]);
7658: /* fprintf(fichtm," V%d=%d ",Tvaraff[cpt],nbcode[Tvaraff[cpt]][codtabm(jj1,cpt)]); */
7659: /* printf(" V%d=%d ",Tvaraff[cpt],nbcode[Tvaraff[cpt]][codtabm(jj1,cpt)]);fflush(stdout); */
7660: }
7661: /* if(nqfveff+nqtveff 0) */ /* Test to be done */
7662: fprintf(fichtm," (model=1+age+%s) ************\n<hr size=\"2\" color=\"#EC5E5E\">",model);
7663: if(invalidvarcomb[k1]){
7664: fprintf(fichtm,"\n<h3>Combination (%d) ignored because no cases </h3>\n",k1);
7665: printf("\nCombination (%d) ignored because no cases \n",k1);
7666: continue;
7667: }
7668: }
7669: /* aij, bij */
7670: fprintf(fichtm,"<br>- Logit model (yours is: logit(pij)=log(pij/pii)= aij+ bij age+%s) as a function of age: <a href=\"%s_%d-1-%d.svg\">%s_%d-1-%d.svg</a><br> \
7671: <img src=\"%s_%d-1-%d.svg\">",model,subdirf2(optionfilefiname,"PE_"),k1,nres,subdirf2(optionfilefiname,"PE_"),k1,nres,subdirf2(optionfilefiname,"PE_"),k1,nres);
7672: /* Pij */
7673: fprintf(fichtm,"<br>\n- P<sub>ij</sub> or conditional probabilities to be observed in state j being in state i, %d (stepm) months before: <a href=\"%s_%d-2-%d.svg\">%s_%d-2-%d.svg</a><br> \
7674: <img src=\"%s_%d-2-%d.svg\">",stepm,subdirf2(optionfilefiname,"PE_"),k1,nres,subdirf2(optionfilefiname,"PE_"),k1,nres,subdirf2(optionfilefiname,"PE_"),k1,nres);
7675: /* Quasi-incidences */
7676: fprintf(fichtm,"<br>\n- I<sub>ij</sub> or Conditional probabilities to be observed in state j being in state i %d (stepm) months\
7677: before but expressed in per year i.e. quasi incidences if stepm is small and probabilities too, \
7678: incidence (rates) are the limit when h tends to zero of the ratio of the probability <sub>h</sub>P<sub>ij</sub> \
7679: divided by h: <sub>h</sub>P<sub>ij</sub>/h : <a href=\"%s_%d-3-%d.svg\">%s_%d-3-%d.svg</a><br> \
7680: <img src=\"%s_%d-3-%d.svg\">",stepm,subdirf2(optionfilefiname,"PE_"),k1,nres,subdirf2(optionfilefiname,"PE_"),k1,nres,subdirf2(optionfilefiname,"PE_"),k1,nres);
7681: /* Survival functions (period) in state j */
7682: for(cpt=1; cpt<=nlstate;cpt++){
7683: fprintf(fichtm,"<br>\n- Survival functions in state %d. And probability to be observed in state %d being in state (1 to %d) at different ages. <a href=\"%s_%d-%d-%d.svg\">%s_%d-%d-%d.svg</a><br>", cpt, cpt, nlstate, subdirf2(optionfilefiname,"LIJ_"),cpt,k1,nres,subdirf2(optionfilefiname,"LIJ_"),cpt,k1,nres);
7684: fprintf(fichtm," (data from text file <a href=\"%s.txt\">%s.txt</a>)\n<br>",subdirf2(optionfilefiname,"PIJ_"),subdirf2(optionfilefiname,"PIJ_"));
7685: fprintf(fichtm,"<img src=\"%s_%d-%d-%d.svg\">",subdirf2(optionfilefiname,"LIJ_"),cpt,k1,nres);
7686: }
7687: /* State specific survival functions (period) */
7688: for(cpt=1; cpt<=nlstate;cpt++){
7689: fprintf(fichtm,"<br>\n- Survival functions in state %d and in any other live state (total).\
7690: And probability to be observed in various states (up to %d) being in state %d at different ages. \
7691: <a href=\"%s_%d-%d-%d.svg\">%s_%d-%d-%d.svg</a><br> ", cpt, nlstate, cpt, subdirf2(optionfilefiname,"LIJT_"),cpt,k1,nres,subdirf2(optionfilefiname,"LIJT_"),cpt,k1,nres);
7692: fprintf(fichtm," (data from text file <a href=\"%s.txt\">%s.txt</a>)\n<br>",subdirf2(optionfilefiname,"PIJ_"),subdirf2(optionfilefiname,"PIJ_"));
7693: fprintf(fichtm,"<img src=\"%s_%d-%d-%d.svg\">",subdirf2(optionfilefiname,"LIJT_"),cpt,k1,nres);
7694: }
7695: /* Period (forward stable) prevalence in each health state */
7696: for(cpt=1; cpt<=nlstate;cpt++){
7697: fprintf(fichtm,"<br>\n- Convergence to period (stable) prevalence in state %d. Or probability for a person being in state (1 to %d) at different ages, to be in state %d some years after. <a href=\"%s_%d-%d-%d.svg\">%s_%d-%d-%d.svg</a><br>", cpt, nlstate, cpt, subdirf2(optionfilefiname,"P_"),cpt,k1,nres,subdirf2(optionfilefiname,"P_"),cpt,k1,nres);
7698: fprintf(fichtm," (data from text file <a href=\"%s.txt\">%s.txt</a>)\n<br>",subdirf2(optionfilefiname,"PIJ_"),subdirf2(optionfilefiname,"PIJ_"));
7699: fprintf(fichtm,"<img src=\"%s_%d-%d-%d.svg\">" ,subdirf2(optionfilefiname,"P_"),cpt,k1,nres);
7700: }
7701: if(prevbcast==1){
7702: /* Backward prevalence in each health state */
7703: for(cpt=1; cpt<=nlstate;cpt++){
7704: fprintf(fichtm,"<br>\n- Convergence to mixed (stable) back prevalence in state %d. Or probability for a person to be in state %d at a younger age, knowing that she/he was in state (1 to %d) at different older ages. <a href=\"%s_%d-%d-%d.svg\">%s_%d-%d-%d.svg</a><br>", cpt, cpt, nlstate, subdirf2(optionfilefiname,"PB_"),cpt,k1,nres,subdirf2(optionfilefiname,"PB_"),cpt,k1,nres);
7705: fprintf(fichtm," (data from text file <a href=\"%s.txt\">%s.txt</a>)\n<br>",subdirf2(optionfilefiname,"PIJB_"),subdirf2(optionfilefiname,"PIJB_"));
7706: fprintf(fichtm,"<img src=\"%s_%d-%d-%d.svg\">" ,subdirf2(optionfilefiname,"PB_"),cpt,k1,nres);
7707: }
7708: }
7709: if(prevfcast==1){
7710: /* Projection of prevalence up to period (forward stable) prevalence in each health state */
7711: for(cpt=1; cpt<=nlstate;cpt++){
7712: fprintf(fichtm,"<br>\n- Projection of cross-sectional prevalence (estimated with cases observed from %.1f to %.1f and mobil_average=%d), from year %.1f up to year %.1f tending to period (stable) forward prevalence in state %d. Or probability to be in state %d being in an observed weighted state (from 1 to %d). <a href=\"%s_%d-%d-%d.svg\">%s_%d-%d-%d.svg</a>", dateprev1, dateprev2, mobilavproj, dateprojd, dateprojf, cpt, cpt, nlstate, subdirf2(optionfilefiname,"PROJ_"),cpt,k1,nres,subdirf2(optionfilefiname,"PROJ_"),cpt,k1,nres);
7713: fprintf(fichtm," (data from text file <a href=\"%s.txt\">%s.txt</a>)\n<br>",subdirf2(optionfilefiname,"F_"),subdirf2(optionfilefiname,"F_"));
7714: fprintf(fichtm,"<img src=\"%s_%d-%d-%d.svg\">",
7715: subdirf2(optionfilefiname,"PROJ_"),cpt,k1,nres);
7716: }
7717: }
7718: if(prevbcast==1){
7719: /* Back projection of prevalence up to stable (mixed) back-prevalence in each health state */
7720: for(cpt=1; cpt<=nlstate;cpt++){
7721: fprintf(fichtm,"<br>\n- Back projection of cross-sectional prevalence (estimated with cases observed from %.1f to %.1f and mobil_average=%d), \
7722: from year %.1f up to year %.1f (probably close to stable [mixed] back prevalence in state %d (randomness in cross-sectional prevalence is not taken into \
7723: account but can visually be appreciated). Or probability to have been in an state %d, knowing that the person was in either state (1 or %d) \
7724: with weights corresponding to observed prevalence at different ages. <a href=\"%s_%d-%d-%d.svg\">%s_%d-%d-%d.svg</a>", dateprev1, dateprev2, mobilavproj, dateback1, dateback2, cpt, cpt, nlstate, subdirf2(optionfilefiname,"PROJB_"),cpt,k1,nres,subdirf2(optionfilefiname,"PROJB_"),cpt,k1,nres);
7725: fprintf(fichtm," (data from text file <a href=\"%s.txt\">%s.txt</a>)\n<br>",subdirf2(optionfilefiname,"FB_"),subdirf2(optionfilefiname,"FB_"));
7726: fprintf(fichtm," <img src=\"%s_%d-%d-%d.svg\">", subdirf2(optionfilefiname,"PROJB_"),cpt,k1,nres);
7727: }
7728: }
7729:
7730: for(cpt=1; cpt<=nlstate;cpt++) {
7731: fprintf(fichtm,"\n<br>- Life expectancy by health state (%d) at initial age and its decomposition into health expectancies in each alive state (1 to %d) (or area under each survival functions): <a href=\"%s_%d-%d-%d.svg\">%s_%d-%d-%d.svg</a>",cpt,nlstate,subdirf2(optionfilefiname,"EXP_"),cpt,k1,nres,subdirf2(optionfilefiname,"EXP_"),cpt,k1,nres);
7732: fprintf(fichtm," (data from text file <a href=\"%s.txt\"> %s.txt</a>)\n<br>",subdirf2(optionfilefiname,"E_"),subdirf2(optionfilefiname,"E_"));
7733: fprintf(fichtm,"<img src=\"%s_%d-%d-%d.svg\">", subdirf2(optionfilefiname,"EXP_"),cpt,k1,nres );
7734: }
7735: /* } /\* end i1 *\/ */
7736: }/* End k1=nres */
7737: fprintf(fichtm,"</ul>");
7738:
7739: fprintf(fichtm,"\
7740: \n<br><li><h4> <a name='secondorder'>Result files (second order: variances)</a></h4>\n\
7741: - Parameter file with estimated parameters and covariance matrix: <a href=\"%s\">%s</a> <br> \
7742: - 95%% confidence intervals and Wald tests of the estimated parameters are in the log file if optimization has been done (mle != 0).<br> \
7743: But because parameters are usually highly correlated (a higher incidence of disability \
7744: and a higher incidence of recovery can give very close observed transition) it might \
7745: be very useful to look not only at linear confidence intervals estimated from the \
7746: variances but at the covariance matrix. And instead of looking at the estimated coefficients \
7747: (parameters) of the logistic regression, it might be more meaningful to visualize the \
7748: covariance matrix of the one-step probabilities. \
7749: See page 'Matrix of variance-covariance of one-step probabilities' below. \n", rfileres,rfileres);
7750:
7751: fprintf(fichtm," - Standard deviation of one-step probabilities: <a href=\"%s\">%s</a> <br>\n",
7752: subdirf2(fileresu,"PROB_"),subdirf2(fileresu,"PROB_"));
7753: fprintf(fichtm,"\
7754: - Variance-covariance of one-step probabilities: <a href=\"%s\">%s</a> <br>\n",
7755: subdirf2(fileresu,"PROBCOV_"),subdirf2(fileresu,"PROBCOV_"));
7756:
7757: fprintf(fichtm,"\
7758: - Correlation matrix of one-step probabilities: <a href=\"%s\">%s</a> <br>\n",
7759: subdirf2(fileresu,"PROBCOR_"),subdirf2(fileresu,"PROBCOR_"));
7760: fprintf(fichtm,"\
7761: - 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): \
7762: <a href=\"%s\">%s</a> <br>\n</li>",
7763: estepm,subdirf2(fileresu,"CVE_"),subdirf2(fileresu,"CVE_"));
7764: fprintf(fichtm,"\
7765: - (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): \
7766: <a href=\"%s\">%s</a> <br>\n</li>",
7767: estepm,subdirf2(fileresu,"STDE_"),subdirf2(fileresu,"STDE_"));
7768: fprintf(fichtm,"\
7769: - Variances and covariances of health expectancies by age. Status (i) based health expectancies (in state j), e<sup>ij</sup> are weighted by the forward (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",
7770: estepm, subdirf2(fileresu,"V_"),subdirf2(fileresu,"V_"));
7771: fprintf(fichtm,"\
7772: - 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",
7773: estepm, subdirf2(fileresu,"T_"),subdirf2(fileresu,"T_"));
7774: fprintf(fichtm,"\
7775: - Standard deviation of forward (period) prevalences: <a href=\"%s\">%s</a> <br>\n",\
7776: subdirf2(fileresu,"VPL_"),subdirf2(fileresu,"VPL_"));
7777:
7778: /* if(popforecast==1) fprintf(fichtm,"\n */
7779: /* - Prevalences forecasting: <a href=\"f%s\">f%s</a> <br>\n */
7780: /* - Population forecasting (if popforecast=1): <a href=\"pop%s\">pop%s</a> <br>\n */
7781: /* <br>",fileres,fileres,fileres,fileres); */
7782: /* else */
7783: /* fprintf(fichtm,"\n No population forecast: popforecast = %d (instead of 1) or stepm = %d (instead of 1) or model=1+age+%s (instead of .)<br><br></li>\n",popforecast, stepm, model); */
7784: fflush(fichtm);
7785:
7786: m=pow(2,cptcoveff);
7787: if (cptcovn < 1) {m=1;ncodemax[1]=1;}
7788:
7789: fprintf(fichtm," <ul><li><b>Graphs (second order)</b></li><p>");
7790:
7791: jj1=0;
7792:
7793: fprintf(fichtm," \n<ul>");
7794: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
7795: /* k1=nres; */
7796: k1=TKresult[nres];
7797: /* for(k1=1; k1<=m;k1++){ /\* For each combination of covariate *\/ */
7798: /* if(m != 1 && TKresult[nres]!= k1) */
7799: /* continue; */
7800: jj1++;
7801: if (cptcovn > 0) {
7802: fprintf(fichtm,"\n<li><a size=\"1\" color=\"#EC5E5E\" href=\"#rescovsecond");
7803: for (cpt=1; cpt<=cptcovs;cpt++){
7804: fprintf(fichtm,"_V%d=%lg_",Tvresult[nres][cpt],TinvDoQresult[nres][Tvresult[nres][cpt]]);
7805: }
7806: fprintf(fichtm,"\">");
7807:
7808: /* if(nqfveff+nqtveff 0) */ /* Test to be done */
7809: fprintf(fichtm,"************ Results for covariates");
7810: for (cpt=1; cpt<=cptcovs;cpt++){
7811: fprintf(fichtm," V%d=%lg ",Tvresult[nres][cpt],TinvDoQresult[nres][Tvresult[nres][cpt]]);
7812: }
7813: if(invalidvarcomb[k1]){
7814: fprintf(fichtm," Warning Combination (%d) ignored because no cases ",k1);
7815: continue;
7816: }
7817: fprintf(fichtm,"</a></li>");
7818: } /* cptcovn >0 */
7819: } /* End nres */
7820: fprintf(fichtm," \n</ul>");
7821:
7822: jj1=0;
7823:
7824: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
7825: /* k1=nres; */
7826: k1=TKresult[nres];
7827: if(TKresult[nres]==0) k1=1; /* To be checked for noresult */
7828: /* for(k1=1; k1<=m;k1++){ */
7829: /* if(m != 1 && TKresult[nres]!= k1) */
7830: /* continue; */
7831: /* for(i1=1; i1<=ncodemax[k1];i1++){ */
7832: jj1++;
7833: if (cptcovn > 0) {
7834: fprintf(fichtm,"\n<p><a name=\"rescovsecond");
7835: for (cpt=1; cpt<=cptcovs;cpt++){
7836: fprintf(fichtm,"_V%d=%lg_",Tvresult[nres][cpt],TinvDoQresult[nres][Tvresult[nres][cpt]]);
7837: }
7838: fprintf(fichtm,"\"</a>");
7839:
7840: fprintf(fichtm,"<hr size=\"2\" color=\"#EC5E5E\">************ Results for covariates");
7841: for (cpt=1; cpt<=cptcovs;cpt++){ /**< cptcoveff number of variables */
7842: fprintf(fichtm," V%d=%lg ",Tvresult[nres][cpt],TinvDoQresult[nres][Tvresult[nres][cpt]]);
7843: printf(" V%d=%lg ",Tvresult[nres][cpt],TinvDoQresult[nres][Tvresult[nres][cpt]]);
7844: /* fprintf(fichtm," V%d=%d ",Tvaraff[cpt],nbcode[Tvaraff[cpt]][codtabm(jj1,cpt)]); */
7845: }
7846:
7847: fprintf(fichtm," (model=1+age+%s) ************\n<hr size=\"2\" color=\"#EC5E5E\">",model);
7848:
7849: if(invalidvarcomb[k1]){
7850: fprintf(fichtm,"\n<h4>Combination (%d) ignored because no cases </h4>\n",k1);
7851: continue;
7852: }
7853: } /* If cptcovn >0 */
7854: for(cpt=1; cpt<=nlstate;cpt++) {
7855: fprintf(fichtm,"\n<br>- Observed (cross-sectional with mov_average=%d) and period (incidence based) \
7856: prevalence (with 95%% confidence interval) in state (%d): <a href=\"%s_%d-%d-%d.svg\"> %s_%d-%d-%d.svg</a>",mobilav,cpt,subdirf2(optionfilefiname,"V_"),cpt,k1,nres,subdirf2(optionfilefiname,"V_"),cpt,k1,nres);
7857: fprintf(fichtm," (data from text file <a href=\"%s\">%s</a>)\n <br>",subdirf2(fileresu,"VPL_"),subdirf2(fileresu,"VPL_"));
7858: fprintf(fichtm,"<img src=\"%s_%d-%d-%d.svg\">",subdirf2(optionfilefiname,"V_"), cpt,k1,nres);
7859: }
7860: fprintf(fichtm,"\n<br>- Total life expectancy by age and \
7861: health expectancies in each live states (1 to %d). If popbased=1 the smooth (due to the model) \
7862: true period expectancies (those weighted with period prevalences are also\
7863: drawn in addition to the population based expectancies computed using\
7864: observed and cahotic prevalences: <a href=\"%s_%d-%d.svg\">%s_%d-%d.svg</a>",nlstate, subdirf2(optionfilefiname,"E_"),k1,nres,subdirf2(optionfilefiname,"E_"),k1,nres);
7865: fprintf(fichtm," (data from text file <a href=\"%s.txt\">%s.txt</a>) \n<br>",subdirf2(optionfilefiname,"T_"),subdirf2(optionfilefiname,"T_"));
7866: fprintf(fichtm,"<img src=\"%s_%d-%d.svg\">",subdirf2(optionfilefiname,"E_"),k1,nres);
7867: /* } /\* end i1 *\/ */
7868: }/* End nres */
7869: fprintf(fichtm,"</ul>");
7870: fflush(fichtm);
7871: }
7872:
7873: /******************* Gnuplot file **************/
7874: void printinggnuplot(char fileresu[], char optionfilefiname[], double ageminpar, double agemaxpar, double bage, double fage , int prevfcast, int prevbcast, char pathc[], double p[], int offyear, int offbyear){
7875:
7876: char dirfileres[132],optfileres[132];
7877: char gplotcondition[132], gplotlabel[132];
7878: int cpt=0,k1=0,i=0,k=0,j=0,jk=0,k2=0,k3=0,k4=0,ij=0, ijp=0, l=0;
7879: int lv=0, vlv=0, kl=0;
7880: int ng=0;
7881: int vpopbased;
7882: int ioffset; /* variable offset for columns */
7883: int iyearc=1; /* variable column for year of projection */
7884: int iagec=1; /* variable column for age of projection */
7885: int nres=0; /* Index of resultline */
7886: int istart=1; /* For starting graphs in projections */
7887:
7888: /* if((ficgp=fopen(optionfilegnuplot,"a"))==NULL) { */
7889: /* printf("Problem with file %s",optionfilegnuplot); */
7890: /* fprintf(ficlog,"Problem with file %s",optionfilegnuplot); */
7891: /* } */
7892:
7893: /*#ifdef windows */
7894: fprintf(ficgp,"cd \"%s\" \n",pathc);
7895: /*#endif */
7896: m=pow(2,cptcoveff);
7897:
7898: /* diagram of the model */
7899: fprintf(ficgp,"\n#Diagram of the model \n");
7900: fprintf(ficgp,"\ndelta=0.03;delta2=0.07;unset arrow;\n");
7901: fprintf(ficgp,"yoff=(%d > 2? 0:1);\n",nlstate);
7902: fprintf(ficgp,"\n#Peripheral arrows\nset for [i=1:%d] for [j=1:%d] arrow i*10+j from cos(pi*((1-(%d/2)*2./%d)/2+(i-1)*2./%d))-(i!=j?(i-j)/abs(i-j)*delta:0), yoff +sin(pi*((1-(%d/2)*2./%d)/2+(i-1)*2./%d)) + (i!=j?(i-j)/abs(i-j)*delta:0) rto -0.95*(cos(pi*((1-(%d/2)*2./%d)/2+(i-1)*2./%d))+(i!=j?(i-j)/abs(i-j)*delta:0) - cos(pi*((1-(%d/2)*2./%d)/2+(j-1)*2./%d)) + (i!=j?(i-j)/abs(i-j)*delta2:0)), -0.95*(sin(pi*((1-(%d/2)*2./%d)/2+(i-1)*2./%d)) + (i!=j?(i-j)/abs(i-j)*delta:0) - sin(pi*((1-(%d/2)*2./%d)/2+(j-1)*2./%d))+( i!=j?(i-j)/abs(i-j)*delta2:0)) ls (i < j? 1:2)\n",nlstate,nlstate,nlstate,nlstate,nlstate,nlstate,nlstate,nlstate,nlstate,nlstate,nlstate,nlstate,nlstate,nlstate,nlstate,nlstate,nlstate,nlstate,nlstate,nlstate);
7903:
7904: fprintf(ficgp,"\n#Centripete arrows (turning in other direction (1-i) instead of (i-1)) \nset for [i=1:%d] arrow (%d+1)*10+i from cos(pi*((1-(%d/2)*2./%d)/2+(1-i)*2./%d))-(i!=j?(i-j)/abs(i-j)*delta:0), yoff +sin(pi*((1-(%d/2)*2./%d)/2+(1-i)*2./%d)) + (i!=j?(i-j)/abs(i-j)*delta:0) rto -0.80*(cos(pi*((1-(%d/2)*2./%d)/2+(1-i)*2./%d))+(i!=j?(i-j)/abs(i-j)*delta:0) ), -0.80*(sin(pi*((1-(%d/2)*2./%d)/2+(1-i)*2./%d)) + (i!=j?(i-j)/abs(i-j)*delta:0) + yoff ) ls 4\n",nlstate,nlstate,nlstate,nlstate,nlstate,nlstate,nlstate,nlstate,nlstate,nlstate,nlstate,nlstate,nlstate,nlstate);
7905: fprintf(ficgp,"\n#show arrow\nunset label\n");
7906: fprintf(ficgp,"\n#States labels, starting from 2 (2-i) instead of (1-i), was (i-1)\nset for [i=1:%d] label i sprintf(\"State %%d\",i) center at cos(pi*((1-(%d/2)*2./%d)/2+(2-i)*2./%d)), yoff+sin(pi*((1-(%d/2)*2./%d)/2+(2-i)*2./%d)) font \"helvetica, 16\" tc rgbcolor \"blue\"\n",nlstate,nlstate,nlstate,nlstate,nlstate,nlstate,nlstate);
7907: fprintf(ficgp,"\nset label %d+1 sprintf(\"State %%d\",%d+1) center at 0.,0. font \"helvetica, 16\" tc rgbcolor \"red\"\n",nlstate,nlstate);
7908: fprintf(ficgp,"\n#show label\nunset border;unset xtics; unset ytics;\n");
7909: fprintf(ficgp,"\n\nset ter svg size 640, 480;set out \"%s_.svg\" \n",subdirf2(optionfilefiname,"D_"));
7910: fprintf(ficgp,"unset log y; plot [-1.2:1.2][yoff-1.2:1.2] 1/0 not; set out;reset;\n");
7911:
7912: /* Contribution to likelihood */
7913: /* Plot the probability implied in the likelihood */
7914: fprintf(ficgp,"\n# Contributions to the Likelihood, mle >=1. For mle=4 no interpolation, pure matrix products.\n#\n");
7915: fprintf(ficgp,"\n set log y; unset log x;set xlabel \"Age\"; set ylabel \"Likelihood (-2Log(L))\";");
7916: /* fprintf(ficgp,"\nset ter svg size 640, 480"); */ /* Too big for svg */
7917: fprintf(ficgp,"\nset ter pngcairo size 640, 480");
7918: /* nice for mle=4 plot by number of matrix products.
7919: replot "rrtest1/toto.txt" u 2:($4 == 1 && $5==2 ? $9 : 1/0):5 t "p12" with point lc 1 */
7920: /* replot exp(p1+p2*x)/(1+exp(p1+p2*x)+exp(p3+p4*x)+exp(p5+p6*x)) t "p12(x)" */
7921: /* fprintf(ficgp,"\nset out \"%s.svg\";",subdirf2(optionfilefiname,"ILK_")); */
7922: fprintf(ficgp,"\nset out \"%s-dest.png\";",subdirf2(optionfilefiname,"ILK_"));
7923: 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));
7924: fprintf(ficgp,"\nset out \"%s-ori.png\";",subdirf2(optionfilefiname,"ILK_"));
7925: 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));
7926: for (i=1; i<= nlstate ; i ++) {
7927: fprintf(ficgp,"\nset out \"%s-p%dj.png\";set ylabel \"Probability for each individual/wave\";",subdirf2(optionfilefiname,"ILK_"),i);
7928: fprintf(ficgp,"unset log;\n# plot weighted, mean weight should have point size of 0.5\n plot \"%s\"",subdirf(fileresilk));
7929: 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);
7930: for (j=2; j<= nlstate+ndeath ; j ++) {
7931: 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);
7932: }
7933: fprintf(ficgp,";\nset out; unset ylabel;\n");
7934: }
7935: /* 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 */
7936: /* fprintf(ficgp,"\nset log y;plot \"%s\" u 2:(-$11):3 t \"All sample, all transitions\" with dots lc variable",subdirf(fileresilk)); */
7937: /* fprintf(ficgp,"\nreplot \"%s\" u 2:($3 <= 3 ? -$11 : 1/0):3 t \"First 3 individuals\" with line lc variable", subdirf(fileresilk)); */
7938: fprintf(ficgp,"\nset out;unset log\n");
7939: /* fprintf(ficgp,"\nset out \"%s.svg\"; replot; set out; # bug gnuplot",subdirf2(optionfilefiname,"ILK_")); */
7940:
7941: strcpy(dirfileres,optionfilefiname);
7942: strcpy(optfileres,"vpl");
7943: /* 1eme*/
7944: for (cpt=1; cpt<= nlstate ; cpt ++){ /* For each live state */
7945: /* for (k1=1; k1<= m ; k1 ++){ /\* For each valid combination of covariate *\/ */
7946: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
7947: k1=TKresult[nres];
7948: if(TKresult[nres]==0) k1=1; /* To be checked for noresult */
7949: /* plot [100000000000000000000:-100000000000000000000] "mysbiaspar/vplrmysbiaspar.txt to check */
7950: /* if(m != 1 && TKresult[nres]!= k1) */
7951: /* continue; */
7952: /* We are interested in selected combination by the resultline */
7953: /* printf("\n# 1st: Period (stable) prevalence with CI: 'VPL_' files and live state =%d ", cpt); */
7954: fprintf(ficgp,"\n# 1st: Forward (stable period) prevalence with CI: 'VPL_' files and live state =%d ", cpt);
7955: strcpy(gplotlabel,"(");
7956: for (k=1; k<=cptcovs; k++){ /* For each covariate k get corresponding value lv for combination k1 */
7957: fprintf(ficgp," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
7958: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
7959:
7960: /* for (k=1; k<=cptcoveff; k++){ /\* For each covariate k get corresponding value lv for combination k1 *\/ */
7961: /* /\* lv= decodtabm(k1,k,cptcoveff); /\\* Should be the value of the covariate corresponding to k1 combination *\\/ *\/ */
7962: /* lv=codtabm(k1,TnsdVar[Tvaraff[k]]); */
7963: /* /\* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 *\/ */
7964: /* /\* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 *\/ */
7965: /* /\* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 *\/ */
7966: /* vlv= nbcode[Tvaraff[k]][lv]; /\* vlv is the value of the covariate lv, 0 or 1 *\/ */
7967: /* /\* For each combination of covariate k1 (V1=1, V3=0), we printed the current covariate k and its value vlv *\/ */
7968: /* /\* printf(" V%d=%d ",Tvaraff[k],vlv); *\/ */
7969: /* fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv); */
7970: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv); */
7971: /* } */
7972: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
7973: /* /\* printf(" V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); *\/ */
7974: /* fprintf(ficgp," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); */
7975: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); */
7976: }
7977: strcpy(gplotlabel+strlen(gplotlabel),")");
7978: /* printf("\n#\n"); */
7979: fprintf(ficgp,"\n#\n");
7980: if(invalidvarcomb[k1]){
7981: /*k1=k1-1;*/ /* To be checked */
7982: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
7983: continue;
7984: }
7985:
7986: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" \n",subdirf2(optionfilefiname,"V_"),cpt,k1,nres);
7987: fprintf(ficgp,"\n#set out \"V_%s_%d-%d-%d.svg\" \n",optionfilefiname,cpt,k1,nres);
7988: /* fprintf(ficgp,"set label \"Alive state %d %s\" at graph 0.98,0.5 center rotate font \"Helvetica,12\"\n",cpt,gplotlabel); */
7989: fprintf(ficgp,"set title \"Alive state %d %s model=1+age+%s\" font \"Helvetica,12\"\n",cpt,gplotlabel,model);
7990: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability\" \nset ter svg size 640, 480\nplot [%.f:%.f] \"%s\" every :::%d::%d u 1:($2==%d ? $3:1/0) \"%%lf %%lf",ageminpar,fage,subdirf2(fileresu,"VPL_"),nres-1,nres-1,nres);
7991: /* fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability\" \nset ter svg size 640, 480\nplot [%.f:%.f] \"%s\" every :::%d::%d u 1:($2==%d ? $3:1/0) \"%%lf %%lf",ageminpar,fage,subdirf2(fileresu,"VPL_"),k1-1,k1-1,nres); */
7992: /* k1-1 error should be nres-1*/
7993: for (i=1; i<= nlstate ; i ++) {
7994: if (i==cpt) fprintf(ficgp," %%lf (%%lf)");
7995: else fprintf(ficgp," %%*lf (%%*lf)");
7996: }
7997: fprintf(ficgp,"\" t\"Forward prevalence\" w l lt 0,\"%s\" every :::%d::%d u 1:($2==%d ? $3+1.96*$4 : 1/0) \"%%lf %%lf",subdirf2(fileresu,"VPL_"),nres-1,nres-1,nres);
7998: for (i=1; i<= nlstate ; i ++) {
7999: if (i==cpt) fprintf(ficgp," %%lf (%%lf)");
8000: else fprintf(ficgp," %%*lf (%%*lf)");
8001: }
8002: fprintf(ficgp,"\" t\"95%% CI\" w l lt 1,\"%s\" every :::%d::%d u 1:($2==%d ? $3-1.96*$4 : 1/0) \"%%lf %%lf",subdirf2(fileresu,"VPL_"),nres-1,nres-1,nres);
8003: for (i=1; i<= nlstate ; i ++) {
8004: if (i==cpt) fprintf(ficgp," %%lf (%%lf)");
8005: else fprintf(ficgp," %%*lf (%%*lf)");
8006: }
8007: /* 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)); */
8008:
8009: fprintf(ficgp,"\" t\"\" w l lt 1,\"%s\" u 1:((",subdirf2(fileresu,"P_"));
8010: if(cptcoveff ==0){
8011: fprintf(ficgp,"$%d)) t 'Observed prevalence in state %d' with line lt 3", 2+3*(cpt-1), cpt );
8012: }else{
8013: kl=0;
8014: for (k=1; k<=cptcoveff; k++){ /* For each combination of covariate */
8015: /* lv= decodtabm(k1,k,cptcoveff); /\* Should be the covariate value corresponding to k1 combination and kth covariate *\/ */
8016: lv=codtabm(k1,TnsdVar[Tvaraff[k]]);
8017: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
8018: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
8019: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
8020: vlv= nbcode[Tvaraff[k]][lv];
8021: kl++;
8022: /* 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 *\/ */
8023: /*6+(cpt-1)*(nlstate+1)+1+(i-1)+(nlstate+1)*nlstate; 6+(1-1)*(2+1)+1+(1-1) +(2+1)*2=13 */
8024: /*6+1+(i-1)+(nlstate+1)*nlstate; 6+1+(1-1) +(2+1)*2=13 */
8025: /* '' 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*/
8026: if(k==cptcoveff){
8027: fprintf(ficgp,"$%d==%d && $%d==%d)? $%d : 1/0) t 'Observed prevalence in state %d' w l lt 2",kl+1, Tvaraff[k],kl+1+1,nbcode[Tvaraff[k]][lv], \
8028: 2+cptcoveff*2+3*(cpt-1), cpt ); /* 4 or 6 ?*/
8029: }else{
8030: fprintf(ficgp,"$%d==%d && $%d==%d && ",kl+1, Tvaraff[k],kl+1+1,nbcode[Tvaraff[k]][lv]);
8031: kl++;
8032: }
8033: } /* end covariate */
8034: } /* end if no covariate */
8035:
8036: if(prevbcast==1){ /* We need to get the corresponding values of the covariates involved in this combination k1 */
8037: /* 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); */
8038: fprintf(ficgp,",\"%s\" u 1:((",subdirf2(fileresu,"PLB_")); /* Age is in 1, nres in 2 to be fixed */
8039: if(cptcoveff ==0){
8040: fprintf(ficgp,"$%d)) t 'Backward prevalence in state %d' with line lt 3", 2+(cpt-1), cpt );
8041: }else{
8042: kl=0;
8043: for (k=1; k<=cptcoveff; k++){ /* For each combination of covariate */
8044: /* lv= decodtabm(k1,k,cptcoveff); /\* Should be the covariate value corresponding to k1 combination and kth covariate *\/ */
8045: lv=codtabm(k1,TnsdVar[Tvaraff[k]]);
8046: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
8047: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
8048: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
8049: /* vlv= nbcode[Tvaraff[k]][lv]; */
8050: vlv= nbcode[Tvaraff[k]][codtabm(k1,TnsdVar[Tvaraff[k]])];
8051: kl++;
8052: /* 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 *\/ */
8053: /*6+(cpt-1)*(nlstate+1)+1+(i-1)+(nlstate+1)*nlstate; 6+(1-1)*(2+1)+1+(1-1) +(2+1)*2=13 */
8054: /*6+1+(i-1)+(nlstate+1)*nlstate; 6+1+(1-1) +(2+1)*2=13 */
8055: /* '' 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*/
8056: if(k==cptcoveff){
8057: fprintf(ficgp,"$%d==%d && $%d==%d)? $%d : 1/0) t 'Backward prevalence in state %d' w l lt 3",kl+1, Tvaraff[k],kl+1+1,nbcode[Tvaraff[k]][lv], \
8058: 2+cptcoveff*2+(cpt-1), cpt ); /* 4 or 6 ?*/
8059: }else{
8060: fprintf(ficgp,"$%d==%d && $%d==%d && ",kl+1, Tvaraff[k],kl+1+1,nbcode[Tvaraff[k]][codtabm(k1,TnsdVar[Tvaraff[k]])]);
8061: kl++;
8062: }
8063: } /* end covariate */
8064: } /* end if no covariate */
8065: if(prevbcast == 1){
8066: fprintf(ficgp,", \"%s\" every :::%d::%d u 1:($2==%d ? $3:1/0) \"%%lf %%lf",subdirf2(fileresu,"VBL_"),nres-1,nres-1,nres);
8067: /* k1-1 error should be nres-1*/
8068: for (i=1; i<= nlstate ; i ++) {
8069: if (i==cpt) fprintf(ficgp," %%lf (%%lf)");
8070: else fprintf(ficgp," %%*lf (%%*lf)");
8071: }
8072: fprintf(ficgp,"\" t\"Backward (stable) prevalence\" w l lt 6 dt 3,\"%s\" every :::%d::%d u 1:($2==%d ? $3+1.96*$4 : 1/0) \"%%lf %%lf",subdirf2(fileresu,"VBL_"),nres-1,nres-1,nres);
8073: for (i=1; i<= nlstate ; i ++) {
8074: if (i==cpt) fprintf(ficgp," %%lf (%%lf)");
8075: else fprintf(ficgp," %%*lf (%%*lf)");
8076: }
8077: fprintf(ficgp,"\" t\"95%% CI\" w l lt 4,\"%s\" every :::%d::%d u 1:($2==%d ? $3-1.96*$4 : 1/0) \"%%lf %%lf",subdirf2(fileresu,"VBL_"),nres-1,nres-1,nres);
8078: for (i=1; i<= nlstate ; i ++) {
8079: if (i==cpt) fprintf(ficgp," %%lf (%%lf)");
8080: else fprintf(ficgp," %%*lf (%%*lf)");
8081: }
8082: fprintf(ficgp,"\" t\"\" w l lt 4");
8083: } /* end if backprojcast */
8084: } /* end if prevbcast */
8085: /* fprintf(ficgp,"\nset out ;unset label;\n"); */
8086: fprintf(ficgp,"\nset out ;unset title;\n");
8087: } /* nres */
8088: /* } /\* k1 *\/ */
8089: } /* cpt */
8090:
8091:
8092: /*2 eme*/
8093: /* for (k1=1; k1<= m ; k1 ++){ */
8094: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
8095: k1=TKresult[nres];
8096: if(TKresult[nres]==0) k1=1; /* To be checked for noresult */
8097: /* if(m != 1 && TKresult[nres]!= k1) */
8098: /* continue; */
8099: fprintf(ficgp,"\n# 2nd: Total life expectancy with CI: 't' files ");
8100: strcpy(gplotlabel,"(");
8101: for (k=1; k<=cptcovs; k++){ /* For each covariate k get corresponding value lv for combination k1 */
8102: fprintf(ficgp," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
8103: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
8104: /* for (k=1; k<=cptcoveff; k++){ /\* For each covariate and each value *\/ */
8105: /* /\* lv= decodtabm(k1,k,cptcoveff); /\\* Should be the covariate number corresponding to k1 combination *\\/ *\/ */
8106: /* lv=codtabm(k1,TnsdVar[Tvaraff[k]]); */
8107: /* /\* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 *\/ */
8108: /* /\* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 *\/ */
8109: /* /\* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 *\/ */
8110: /* /\* vlv= nbcode[Tvaraff[k]][lv]; *\/ */
8111: /* vlv= nbcode[Tvaraff[k]][codtabm(k1,TnsdVar[Tvaraff[k]])]; */
8112: /* fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv); */
8113: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv); */
8114: /* } */
8115: /* /\* for(k=1; k <= ncovds; k++){ *\/ */
8116: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
8117: /* printf(" V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); */
8118: /* fprintf(ficgp," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); */
8119: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); */
8120: }
8121: strcpy(gplotlabel+strlen(gplotlabel),")");
8122: fprintf(ficgp,"\n#\n");
8123: if(invalidvarcomb[k1]){
8124: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
8125: continue;
8126: }
8127:
8128: fprintf(ficgp,"\nset out \"%s_%d-%d.svg\" \n",subdirf2(optionfilefiname,"E_"),k1,nres);
8129: for(vpopbased=0; vpopbased <= popbased; vpopbased++){ /* Done for vpopbased=0 and vpopbased=1 if popbased==1*/
8130: fprintf(ficgp,"\nset label \"popbased %d %s\" at graph 0.98,0.5 center rotate font \"Helvetica,12\"\n",vpopbased,gplotlabel);
8131: if(vpopbased==0){
8132: fprintf(ficgp,"set ylabel \"Years\" \nset ter svg size 640, 480\nplot [%.f:%.f] ",ageminpar,fage);
8133: }else
8134: fprintf(ficgp,"\nreplot ");
8135: for (i=1; i<= nlstate+1 ; i ++) {
8136: k=2*i;
8137: fprintf(ficgp,"\"%s\" every :::%d::%d u 1:($2==%d && $4!=0 ?$4 : 1/0) \"%%lf %%lf %%lf",subdirf2(fileresu,"T_"),nres-1,nres-1, vpopbased);
8138: for (j=1; j<= nlstate+1 ; j ++) {
8139: if (j==i) fprintf(ficgp," %%lf (%%lf)");
8140: else fprintf(ficgp," %%*lf (%%*lf)");
8141: }
8142: if (i== 1) fprintf(ficgp,"\" t\"TLE\" w l lt %d, \\\n",i);
8143: else fprintf(ficgp,"\" t\"LE in state (%d)\" w l lt %d, \\\n",i-1,i+1);
8144: fprintf(ficgp,"\"%s\" every :::%d::%d u 1:($2==%d && $4!=0 ? $4-$5*2 : 1/0) \"%%lf %%lf %%lf",subdirf2(fileresu,"T_"),nres-1,nres-1,vpopbased);
8145: for (j=1; j<= nlstate+1 ; j ++) {
8146: if (j==i) fprintf(ficgp," %%lf (%%lf)");
8147: else fprintf(ficgp," %%*lf (%%*lf)");
8148: }
8149: fprintf(ficgp,"\" t\"\" w l lt 0,");
8150: fprintf(ficgp,"\"%s\" every :::%d::%d u 1:($2==%d && $4!=0 ? $4+$5*2 : 1/0) \"%%lf %%lf %%lf",subdirf2(fileresu,"T_"),nres-1,nres-1,vpopbased);
8151: for (j=1; j<= nlstate+1 ; j ++) {
8152: if (j==i) fprintf(ficgp," %%lf (%%lf)");
8153: else fprintf(ficgp," %%*lf (%%*lf)");
8154: }
8155: if (i== (nlstate+1)) fprintf(ficgp,"\" t\"\" w l lt 0");
8156: else fprintf(ficgp,"\" t\"\" w l lt 0,\\\n");
8157: } /* state */
8158: } /* vpopbased */
8159: fprintf(ficgp,"\nset out;set out \"%s_%d-%d.svg\"; replot; set out; unset label;\n",subdirf2(optionfilefiname,"E_"),k1,nres); /* Buggy gnuplot */
8160: } /* end nres */
8161: /* } /\* k1 end 2 eme*\/ */
8162:
8163:
8164: /*3eme*/
8165: /* for (k1=1; k1<= m ; k1 ++){ */
8166: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
8167: k1=TKresult[nres];
8168: if(TKresult[nres]==0) k1=1; /* To be checked for noresult */
8169: /* if(m != 1 && TKresult[nres]!= k1) */
8170: /* continue; */
8171:
8172: for (cpt=1; cpt<= nlstate ; cpt ++) { /* Fragile no verification of covariate values */
8173: fprintf(ficgp,"\n\n# 3d: Life expectancy with EXP_ files: combination=%d state=%d",k1, cpt);
8174: strcpy(gplotlabel,"(");
8175: for (k=1; k<=cptcovs; k++){ /* For each covariate k get corresponding value lv for combination k1 */
8176: fprintf(ficgp," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
8177: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
8178: /* for (k=1; k<=cptcoveff; k++){ /\* For each covariate and each value *\/ */
8179: /* /\* lv= decodtabm(k1,k,cptcoveff); /\\* Should be the covariate number corresponding to k1 combination *\\/ *\/ */
8180: /* lv= codtabm(k1,TnsdVar[Tvaraff[k]]); /\* Should be the covariate value corresponding to combination k1 and covariate k *\/ */
8181: /* /\* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 *\/ */
8182: /* /\* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 *\/ */
8183: /* /\* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 *\/ */
8184: /* /\* vlv= nbcode[Tvaraff[k]][lv]; *\/ */
8185: /* vlv= nbcode[Tvaraff[k]][codtabm(k1,TnsdVar[Tvaraff[k]])]; */
8186: /* fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv); */
8187: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv); */
8188: /* } */
8189: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
8190: /* fprintf(ficgp," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][resultmodel[nres][k4]]); */
8191: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][resultmodel[nres][k4]]); */
8192: }
8193: strcpy(gplotlabel+strlen(gplotlabel),")");
8194: fprintf(ficgp,"\n#\n");
8195: if(invalidvarcomb[k1]){
8196: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
8197: continue;
8198: }
8199:
8200: /* k=2+nlstate*(2*cpt-2); */
8201: k=2+(nlstate+1)*(cpt-1);
8202: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" \n",subdirf2(optionfilefiname,"EXP_"),cpt,k1,nres);
8203: fprintf(ficgp,"set label \"%s\" at graph 0.98,0.5 center rotate font \"Helvetica,12\"\n",gplotlabel);
8204: fprintf(ficgp,"set ter svg size 640, 480\n\
8205: plot [%.f:%.f] \"%s\" every :::%d::%d u 1:%d t \"e%d1\" w l",ageminpar,fage,subdirf2(fileresu,"E_"),nres-1,nres-1,k,cpt);
8206: /*fprintf(ficgp,",\"e%s\" every :::%d::%d u 1:($%d-2*$%d) \"\%%lf ",fileres,k1-1,k1-1,k,k+1);
8207: for (i=1; i<= nlstate*2 ; i ++) fprintf(ficgp,"\%%lf (\%%lf) ");
8208: fprintf(ficgp,"\" t \"e%d1\" w l",cpt);
8209: fprintf(ficgp,",\"e%s\" every :::%d::%d u 1:($%d+2*$%d) \"\%%lf ",fileres,k1-1,k1-1,k,k+1);
8210: for (i=1; i<= nlstate*2 ; i ++) fprintf(ficgp,"\%%lf (\%%lf) ");
8211: fprintf(ficgp,"\" t \"e%d1\" w l",cpt);
8212:
8213: */
8214: for (i=1; i< nlstate ; i ++) {
8215: fprintf(ficgp," ,\"%s\" every :::%d::%d u 1:%d t \"e%d%d\" w l",subdirf2(fileresu,"E_"),nres-1,nres-1,k+i,cpt,i+1);
8216: /* 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);*/
8217:
8218: }
8219: fprintf(ficgp," ,\"%s\" every :::%d::%d u 1:%d t \"e%d.\" w l",subdirf2(fileresu,"E_"),nres-1,nres-1,k+nlstate,cpt);
8220: }
8221: fprintf(ficgp,"\nunset label;\n");
8222: } /* end nres */
8223: /* } /\* end kl 3eme *\/ */
8224:
8225: /* 4eme */
8226: /* Survival functions (period) from state i in state j by initial state i */
8227: /* for (k1=1; k1<=m; k1++){ /\* For each covariate and each value *\/ */
8228: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
8229: k1=TKresult[nres];
8230: if(TKresult[nres]==0) k1=1; /* To be checked for noresult */
8231: /* if(m != 1 && TKresult[nres]!= k1) */
8232: /* continue; */
8233: for (cpt=1; cpt<=nlstate ; cpt ++) { /* For each life state cpt*/
8234: strcpy(gplotlabel,"(");
8235: fprintf(ficgp,"\n#\n#\n# Survival functions in state %d : 'LIJ_' files, cov=%d state=%d", cpt, k1, cpt);
8236: for (k=1; k<=cptcovs; k++){ /* For each covariate k get corresponding value lv for combination k1 */
8237: fprintf(ficgp," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
8238: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
8239: /* for (k=1; k<=cptcoveff; k++){ /\* For each covariate and each value *\/ */
8240: /* lv=codtabm(k1,TnsdVar[Tvaraff[k]]); */
8241: /* /\* lv= decodtabm(k1,k,cptcoveff); /\\* Should be the covariate number corresponding to k1 combination *\\/ *\/ */
8242: /* /\* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 *\/ */
8243: /* /\* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 *\/ */
8244: /* /\* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 *\/ */
8245: /* /\* vlv= nbcode[Tvaraff[k]][lv]; *\/ */
8246: /* vlv= nbcode[Tvaraff[k]][codtabm(k1,TnsdVar[Tvaraff[k]])]; */
8247: /* fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv); */
8248: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv); */
8249: /* } */
8250: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
8251: /* fprintf(ficgp," V%d=%f ",Tvqresult[nres][resultmodel[nres][k4]],Tqresult[nres][resultmodel[nres][k4]]); */
8252: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][resultmodel[nres][k4]],Tqresult[nres][resultmodel[nres][k4]]); */
8253: }
8254: strcpy(gplotlabel+strlen(gplotlabel),")");
8255: fprintf(ficgp,"\n#\n");
8256: if(invalidvarcomb[k1]){
8257: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
8258: continue;
8259: }
8260:
8261: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" \n",subdirf2(optionfilefiname,"LIJ_"),cpt,k1,nres);
8262: fprintf(ficgp,"set label \"Alive state %d %s\" at graph 0.98,0.5 center rotate font \"Helvetica,12\"\n",cpt,gplotlabel);
8263: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability to be alive\" \n\
8264: set ter svg size 640, 480\nunset log y\nplot [%.f:%.f] ", ageminpar, agemaxpar);
8265: k=3;
8266: for (i=1; i<= nlstate ; i ++){
8267: if(i==1){
8268: fprintf(ficgp,"\"%s\"",subdirf2(fileresu,"PIJ_"));
8269: }else{
8270: fprintf(ficgp,", '' ");
8271: }
8272: l=(nlstate+ndeath)*(i-1)+1;
8273: fprintf(ficgp," u ($1==%d ? ($3):1/0):($%d/($%d",k1,k+l+(cpt-1),k+l);
8274: for (j=2; j<= nlstate+ndeath ; j ++)
8275: fprintf(ficgp,"+$%d",k+l+j-1);
8276: fprintf(ficgp,")) t \"l(%d,%d)\" w l",i,cpt);
8277: } /* nlstate */
8278: fprintf(ficgp,"\nset out; unset label;\n");
8279: } /* end cpt state*/
8280: } /* end nres */
8281: /* } /\* end covariate k1 *\/ */
8282:
8283: /* 5eme */
8284: /* Survival functions (period) from state i in state j by final state j */
8285: /* for (k1=1; k1<= m ; k1++){ /\* For each covariate combination if any *\/ */
8286: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
8287: k1=TKresult[nres];
8288: if(TKresult[nres]==0) k1=1; /* To be checked for noresult */
8289: /* if(m != 1 && TKresult[nres]!= k1) */
8290: /* continue; */
8291: for (cpt=1; cpt<=nlstate ; cpt ++) { /* For each inital state */
8292: strcpy(gplotlabel,"(");
8293: 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);
8294: for (k=1; k<=cptcovs; k++){ /* For each covariate k get corresponding value lv for combination k1 */
8295: fprintf(ficgp," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
8296: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
8297: /* for (k=1; k<=cptcoveff; k++){ /\* For each covariate and each value *\/ */
8298: /* lv=codtabm(k1,TnsdVar[Tvaraff[k]]); */
8299: /* /\* lv= decodtabm(k1,k,cptcoveff); /\\* Should be the covariate number corresponding to k1 combination *\\/ *\/ */
8300: /* /\* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 *\/ */
8301: /* /\* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 *\/ */
8302: /* /\* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 *\/ */
8303: /* /\* vlv= nbcode[Tvaraff[k]][lv]; *\/ */
8304: /* vlv= nbcode[Tvaraff[k]][codtabm(k1,TnsdVar[Tvaraff[k]])]; */
8305: /* fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv); */
8306: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv); */
8307: /* } */
8308: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
8309: /* fprintf(ficgp," V%d=%f ",Tvqresult[nres][resultmodel[nres][k4]],Tqresult[nres][resultmodel[nres][k4]]); */
8310: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][resultmodel[nres][k4]],Tqresult[nres][resultmodel[nres][k4]]); */
8311: }
8312: strcpy(gplotlabel+strlen(gplotlabel),")");
8313: fprintf(ficgp,"\n#\n");
8314: if(invalidvarcomb[k1]){
8315: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
8316: continue;
8317: }
8318:
8319: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" \n",subdirf2(optionfilefiname,"LIJT_"),cpt,k1,nres);
8320: fprintf(ficgp,"set label \"Alive state %d %s\" at graph 0.98,0.5 center rotate font \"Helvetica,12\"\n",cpt,gplotlabel);
8321: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability to be alive\" \n\
8322: set ter svg size 640, 480\nunset log y\nplot [%.f:%.f] ", ageminpar, agemaxpar);
8323: k=3;
8324: for (j=1; j<= nlstate ; j ++){ /* Lived in state j */
8325: if(j==1)
8326: fprintf(ficgp,"\"%s\"",subdirf2(fileresu,"PIJ_"));
8327: else
8328: fprintf(ficgp,", '' ");
8329: l=(nlstate+ndeath)*(cpt-1) +j;
8330: fprintf(ficgp," u (($1==%d && (floor($2)%%5 == 0)) ? ($3):1/0):($%d",k1,k+l);
8331: /* for (i=2; i<= nlstate+ndeath ; i ++) */
8332: /* fprintf(ficgp,"+$%d",k+l+i-1); */
8333: fprintf(ficgp,") t \"l(%d,%d)\" w l",cpt,j);
8334: } /* nlstate */
8335: fprintf(ficgp,", '' ");
8336: fprintf(ficgp," u (($1==%d && (floor($2)%%5 == 0)) ? ($3):1/0):(",k1);
8337: for (j=1; j<= nlstate ; j ++){ /* Lived in state j */
8338: l=(nlstate+ndeath)*(cpt-1) +j;
8339: if(j < nlstate)
8340: fprintf(ficgp,"$%d +",k+l);
8341: else
8342: fprintf(ficgp,"$%d) t\"l(%d,.)\" w l",k+l,cpt);
8343: }
8344: fprintf(ficgp,"\nset out; unset label;\n");
8345: } /* end cpt state*/
8346: /* } /\* end covariate *\/ */
8347: } /* end nres */
8348:
8349: /* 6eme */
8350: /* CV preval stable (period) for each covariate */
8351: /* for (k1=1; k1<= m ; k1 ++) /\* For each covariate combination if any *\/ */
8352: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
8353: k1=TKresult[nres];
8354: if(TKresult[nres]==0) k1=1; /* To be checked for noresult */
8355: /* if(m != 1 && TKresult[nres]!= k1) */
8356: /* continue; */
8357: for (cpt=1; cpt<=nlstate ; cpt ++) { /* For each life state of arrival */
8358: strcpy(gplotlabel,"(");
8359: fprintf(ficgp,"\n#\n#\n#CV preval stable (forward): 'pij' files, covariatecombination#=%d state=%d",k1, cpt);
8360: for (k=1; k<=cptcovs; k++){ /* For each covariate k get corresponding value lv for combination k1 */
8361: fprintf(ficgp," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
8362: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
8363: /* for (k=1; k<=cptcoveff; k++){ /\* For each covariate and each value *\/ */
8364: /* /\* lv= decodtabm(k1,k,cptcoveff); /\\* Should be the covariate number corresponding to k1 combination *\\/ *\/ */
8365: /* lv=codtabm(k1,TnsdVar[Tvaraff[k]]); */
8366: /* /\* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 *\/ */
8367: /* /\* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 *\/ */
8368: /* /\* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 *\/ */
8369: /* /\* vlv= nbcode[Tvaraff[k]][lv]; *\/ */
8370: /* vlv= nbcode[Tvaraff[k]][codtabm(k1,TnsdVar[Tvaraff[k]])]; */
8371: /* fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv); */
8372: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv); */
8373: /* } */
8374: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
8375: /* fprintf(ficgp," V%d=%f ",Tvqresult[nres][resultmodel[nres][k4]],Tqresult[nres][resultmodel[nres][k4]]); */
8376: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][resultmodel[nres][k4]],Tqresult[nres][resultmodel[nres][k4]]); */
8377: }
8378: strcpy(gplotlabel+strlen(gplotlabel),")");
8379: fprintf(ficgp,"\n#\n");
8380: if(invalidvarcomb[k1]){
8381: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
8382: continue;
8383: }
8384:
8385: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" \n",subdirf2(optionfilefiname,"P_"),cpt,k1,nres);
8386: fprintf(ficgp,"set label \"Alive state %d %s\" at graph 0.98,0.5 center rotate font \"Helvetica,12\"\n",cpt,gplotlabel);
8387: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability\" \n\
8388: set ter svg size 640, 480\nunset log y\nplot [%.f:%.f] ", ageminpar, agemaxpar);
8389: k=3; /* Offset */
8390: for (i=1; i<= nlstate ; i ++){ /* State of origin */
8391: if(i==1)
8392: fprintf(ficgp,"\"%s\"",subdirf2(fileresu,"PIJ_"));
8393: else
8394: fprintf(ficgp,", '' ");
8395: l=(nlstate+ndeath)*(i-1)+1; /* 1, 1+ nlstate+ndeath, 1+2*(nlstate+ndeath) */
8396: fprintf(ficgp," u ($1==%d ? ($3):1/0):($%d/($%d",k1,k+l+(cpt-1),k+l);
8397: for (j=2; j<= nlstate ; j ++)
8398: fprintf(ficgp,"+$%d",k+l+j-1);
8399: fprintf(ficgp,")) t \"prev(%d,%d)\" w l",i,cpt);
8400: } /* nlstate */
8401: fprintf(ficgp,"\nset out; unset label;\n");
8402: } /* end cpt state*/
8403: } /* end covariate */
8404:
8405:
8406: /* 7eme */
8407: if(prevbcast == 1){
8408: /* CV backward prevalence for each covariate */
8409: /* for (k1=1; k1<= m ; k1 ++) /\* For each covariate combination if any *\/ */
8410: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
8411: k1=TKresult[nres];
8412: if(TKresult[nres]==0) k1=1; /* To be checked for noresult */
8413: /* if(m != 1 && TKresult[nres]!= k1) */
8414: /* continue; */
8415: for (cpt=1; cpt<=nlstate ; cpt ++) { /* For each life origin state */
8416: strcpy(gplotlabel,"(");
8417: fprintf(ficgp,"\n#\n#\n#CV Backward stable prevalence: 'pijb' files, covariatecombination#=%d state=%d",k1, cpt);
8418: for (k=1; k<=cptcovs; k++){ /* For each covariate k get corresponding value lv for combination k1 */
8419: fprintf(ficgp," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
8420: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
8421: /* for (k=1; k<=cptcoveff; k++){ /\* For each covariate and each value *\/ */
8422: /* /\* lv= decodtabm(k1,k,cptcoveff); /\\* Should be the covariate number corresponding to k1 combination *\\/ *\/ */
8423: /* lv=codtabm(k1,TnsdVar[Tvaraff[k]]); */
8424: /* /\* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 *\/ */
8425: /* /\* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 *\/ */
8426: /* /\* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 *\/ */
8427: /* /\* vlv= nbcode[Tvaraff[k]][lv]; *\/ */
8428: /* vlv= nbcode[Tvaraff[k]][codtabm(k1,TnsdVar[Tvaraff[k]])]; */
8429: /* fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv); */
8430: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv); */
8431: /* } */
8432: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
8433: /* fprintf(ficgp," V%d=%f ",Tvqresult[nres][resultmodel[nres][k4]],Tqresult[nres][resultmodel[nres][k4]]); */
8434: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][resultmodel[nres][k4]],Tqresult[nres][resultmodel[nres][k4]]); */
8435: }
8436: strcpy(gplotlabel+strlen(gplotlabel),")");
8437: fprintf(ficgp,"\n#\n");
8438: if(invalidvarcomb[k1]){
8439: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
8440: continue;
8441: }
8442:
8443: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" \n",subdirf2(optionfilefiname,"PB_"),cpt,k1,nres);
8444: fprintf(ficgp,"set label \"Origin alive state %d %s\" at graph 0.98,0.5 center rotate font \"Helvetica,12\"\n",cpt,gplotlabel);
8445: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability\" \n\
8446: set ter svg size 640, 480\nunset log y\nplot [%.f:%.f] ", ageminpar, agemaxpar);
8447: k=3; /* Offset */
8448: for (i=1; i<= nlstate ; i ++){ /* State of arrival */
8449: if(i==1)
8450: fprintf(ficgp,"\"%s\"",subdirf2(fileresu,"PIJB_"));
8451: else
8452: fprintf(ficgp,", '' ");
8453: /* l=(nlstate+ndeath)*(i-1)+1; */
8454: l=(nlstate+ndeath)*(cpt-1)+1; /* fixed for i; cpt=1 1, cpt=2 1+ nlstate+ndeath, 1+2*(nlstate+ndeath) */
8455: /* fprintf(ficgp," u ($1==%d ? ($3):1/0):($%d/($%d",k1,k+l+(cpt-1),k+l); /\* a vérifier *\/ */
8456: /* fprintf(ficgp," u ($1==%d ? ($3):1/0):($%d/($%d",k1,k+l+(cpt-1),k+l+(cpt-1)+i-1); /\* a vérifier *\/ */
8457: fprintf(ficgp," u ($1==%d ? ($3):1/0):($%d",k1,k+l+i-1); /* To be verified */
8458: /* for (j=2; j<= nlstate ; j ++) */
8459: /* fprintf(ficgp,"+$%d",k+l+j-1); */
8460: /* /\* fprintf(ficgp,"+$%d",k+l+j-1); *\/ */
8461: fprintf(ficgp,") t \"bprev(%d,%d)\" w l",cpt,i);
8462: } /* nlstate */
8463: fprintf(ficgp,"\nset out; unset label;\n");
8464: } /* end cpt state*/
8465: } /* end covariate */
8466: } /* End if prevbcast */
8467:
8468: /* 8eme */
8469: if(prevfcast==1){
8470: /* Projection from cross-sectional to forward stable (period) prevalence for each covariate */
8471:
8472: /* for (k1=1; k1<= m ; k1 ++) /\* For each covariate combination if any *\/ */
8473: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
8474: k1=TKresult[nres];
8475: if(TKresult[nres]==0) k1=1; /* To be checked for noresult */
8476: /* if(m != 1 && TKresult[nres]!= k1) */
8477: /* continue; */
8478: for (cpt=1; cpt<=nlstate ; cpt ++) { /* For each life state */
8479: strcpy(gplotlabel,"(");
8480: fprintf(ficgp,"\n#\n#\n#Projection of prevalence to forward stable prevalence (period): 'PROJ_' files, covariatecombination#=%d state=%d",k1, cpt);
8481: for (k=1; k<=cptcovs; k++){ /* For each covariate k get corresponding value lv for combination k1 */
8482: fprintf(ficgp," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
8483: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
8484: /* for (k=1; k<=cptcoveff; k++){ /\* For each correspondig covariate value *\/ */
8485: /* /\* lv= decodtabm(k1,k,cptcoveff); /\\* Should be the covariate value corresponding to k1 combination and kth covariate *\\/ *\/ */
8486: /* lv=codtabm(k1,TnsdVar[Tvaraff[k]]); */
8487: /* /\* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 *\/ */
8488: /* /\* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 *\/ */
8489: /* /\* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 *\/ */
8490: /* /\* vlv= nbcode[Tvaraff[k]][lv]; *\/ */
8491: /* vlv= nbcode[Tvaraff[k]][codtabm(k1,TnsdVar[Tvaraff[k]])]; */
8492: /* fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv); */
8493: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv); */
8494: /* } */
8495: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
8496: /* fprintf(ficgp," V%d=%f ",Tvqresult[nres][resultmodel[nres][k4]],Tqresult[nres][resultmodel[nres][k4]]); */
8497: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][resultmodel[nres][k4]],Tqresult[nres][resultmodel[nres][k4]]); */
8498: }
8499: strcpy(gplotlabel+strlen(gplotlabel),")");
8500: fprintf(ficgp,"\n#\n");
8501: if(invalidvarcomb[k1]){
8502: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
8503: continue;
8504: }
8505:
8506: fprintf(ficgp,"# hpijx=probability over h years, hp.jx is weighted by observed prev\n ");
8507: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" \n",subdirf2(optionfilefiname,"PROJ_"),cpt,k1,nres);
8508: fprintf(ficgp,"set label \"Alive state %d %s\" at graph 0.98,0.5 center rotate font \"Helvetica,12\"\n",cpt,gplotlabel);
8509: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Prevalence\" \n\
8510: set ter svg size 640, 480\nunset log y\nplot [%.f:%.f] ", ageminpar, agemaxpar);
8511:
8512: /* for (i=1; i<= nlstate+1 ; i ++){ /\* nlstate +1 p11 p21 p.1 *\/ */
8513: istart=nlstate+1; /* Could be one if by state, but nlstate+1 is w.i projection only */
8514: /*istart=1;*/ /* Could be one if by state, but nlstate+1 is w.i projection only */
8515: for (i=istart; i<= nlstate+1 ; i ++){ /* nlstate +1 p11 p21 p.1 */
8516: /*# V1 = 1 V2 = 0 yearproj age p11 p21 p.1 p12 p22 p.2 p13 p23 p.3*/
8517: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
8518: /*# yearproj age p11 p21 p.1 p12 p22 p.2 p13 p23 p.3*/
8519: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
8520: if(i==istart){
8521: fprintf(ficgp,"\"%s\"",subdirf2(fileresu,"F_"));
8522: }else{
8523: fprintf(ficgp,",\\\n '' ");
8524: }
8525: if(cptcoveff ==0){ /* No covariate */
8526: ioffset=2; /* Age is in 2 */
8527: /*# yearproj age p11 p21 p31 p.1 p12 p22 p32 p.2 p13 p23 p33 p.3 p14 p24 p34 p.4*/
8528: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 */
8529: /*# V1 = 1 yearproj age p11 p21 p31 p.1 p12 p22 p32 p.2 p13 p23 p33 p.3 p14 p24 p34 p.4*/
8530: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 */
8531: fprintf(ficgp," u %d:(", ioffset);
8532: if(i==nlstate+1){
8533: fprintf(ficgp," $%d/(1.-$%d)):1 t 'pw.%d' with line lc variable ", \
8534: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset+1+(i-1)+(nlstate+1)*nlstate,cpt );
8535: fprintf(ficgp,",\\\n '' ");
8536: fprintf(ficgp," u %d:(",ioffset);
8537: fprintf(ficgp," (($1-$2) == %d ) ? $%d/(1.-$%d) : 1/0):1 with labels center not ", \
8538: offyear, \
8539: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset+1+(i-1)+(nlstate+1)*nlstate );
8540: }else
8541: fprintf(ficgp," $%d/(1.-$%d)) t 'p%d%d' with line ", \
8542: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset+1+(i-1)+(nlstate+1)*nlstate,i,cpt );
8543: }else{ /* more than 2 covariates */
8544: ioffset=2*cptcoveff+2; /* Age is in 4 or 6 or etc.*/
8545: /*# V1 = 1 V2 = 0 yearproj age p11 p21 p.1 p12 p22 p.2 p13 p23 p.3*/
8546: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
8547: iyearc=ioffset-1;
8548: iagec=ioffset;
8549: fprintf(ficgp," u %d:(",ioffset);
8550: kl=0;
8551: strcpy(gplotcondition,"(");
8552: for (k=1; k<=cptcoveff; k++){ /* For each covariate writing the chain of conditions */
8553: /* lv= decodtabm(k1,k,cptcoveff); /\* Should be the covariate value corresponding to combination k1 and covariate k *\/ */
8554: lv=codtabm(k1,TnsdVar[Tvaraff[k]]);
8555: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
8556: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
8557: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
8558: /* vlv= nbcode[Tvaraff[k]][lv]; /\* Value of the modality of Tvaraff[k] *\/ */
8559: vlv= nbcode[Tvaraff[k]][codtabm(k1,TnsdVar[Tvaraff[k]])];
8560: kl++;
8561: sprintf(gplotcondition+strlen(gplotcondition),"$%d==%d && $%d==%d " ,kl,Tvaraff[k], kl+1, nbcode[Tvaraff[k]][lv]);
8562: kl++;
8563: if(k <cptcoveff && cptcoveff>1)
8564: sprintf(gplotcondition+strlen(gplotcondition)," && ");
8565: }
8566: strcpy(gplotcondition+strlen(gplotcondition),")");
8567: /* 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 *\/ */
8568: /*6+(cpt-1)*(nlstate+1)+1+(i-1)+(nlstate+1)*nlstate; 6+(1-1)*(2+1)+1+(1-1) +(2+1)*2=13 */
8569: /*6+1+(i-1)+(nlstate+1)*nlstate; 6+1+(1-1) +(2+1)*2=13 */
8570: /* '' 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*/
8571: if(i==nlstate+1){
8572: fprintf(ficgp,"%s ? $%d/(1.-$%d) : 1/0):%d t 'p.%d' with line lc variable", gplotcondition, \
8573: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset+1+(i-1)+(nlstate+1)*nlstate,iyearc, cpt );
8574: fprintf(ficgp,",\\\n '' ");
8575: fprintf(ficgp," u %d:(",iagec);
8576: fprintf(ficgp,"%s && (($%d-$%d) == %d ) ? $%d/(1.-$%d) : 1/0):%d with labels center not ", gplotcondition, \
8577: iyearc, iagec, offyear, \
8578: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset+1+(i-1)+(nlstate+1)*nlstate, iyearc );
8579: /* '' u 6:(($1==1 && $2==0 && $3==2 && $4==0) && (($5-$6) == 1947) ? $10/(1.-$22) : 1/0):5 with labels center boxed not*/
8580: }else{
8581: fprintf(ficgp,"%s ? $%d/(1.-$%d) : 1/0) t 'p%d%d' with line ", gplotcondition, \
8582: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset +1+(i-1)+(nlstate+1)*nlstate,i,cpt );
8583: }
8584: } /* end if covariate */
8585: } /* nlstate */
8586: fprintf(ficgp,"\nset out; unset label;\n");
8587: } /* end cpt state*/
8588: } /* end covariate */
8589: } /* End if prevfcast */
8590:
8591: if(prevbcast==1){
8592: /* Back projection from cross-sectional to stable (mixed) for each covariate */
8593:
8594: /* for (k1=1; k1<= m ; k1 ++) /\* For each covariate combination if any *\/ */
8595: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
8596: k1=TKresult[nres];
8597: if(TKresult[nres]==0) k1=1; /* To be checked for noresult */
8598: /* if(m != 1 && TKresult[nres]!= k1) */
8599: /* continue; */
8600: for (cpt=1; cpt<=nlstate ; cpt ++) { /* For each life state */
8601: strcpy(gplotlabel,"(");
8602: fprintf(ficgp,"\n#\n#\n#Back projection of prevalence to stable (mixed) back prevalence: 'BPROJ_' files, covariatecombination#=%d originstate=%d",k1, cpt);
8603: for (k=1; k<=cptcovs; k++){ /* For each covariate k get corresponding value lv for combination k1 */
8604: fprintf(ficgp," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
8605: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
8606: /* for (k=1; k<=cptcoveff; k++){ /\* For each correspondig covariate value *\/ */
8607: /* /\* lv= decodtabm(k1,k,cptcoveff); /\\* Should be the covariate value corresponding to k1 combination and kth covariate *\\/ *\/ */
8608: /* lv= codtabm(k1,TnsdVar[Tvaraff[k]]); /\* Should be the covariate value corresponding to combination k1 and covariate k *\/ */
8609: /* /\* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 *\/ */
8610: /* /\* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 *\/ */
8611: /* /\* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 *\/ */
8612: /* /\* vlv= nbcode[Tvaraff[k]][lv]; *\/ */
8613: /* vlv= nbcode[Tvaraff[k]][codtabm(k1,TnsdVar[Tvaraff[k]])]; */
8614: /* fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv); */
8615: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv); */
8616: /* } */
8617: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
8618: /* fprintf(ficgp," V%d=%f ",Tvqresult[nres][resultmodel[nres][k4]],Tqresult[nres][resultmodel[nres][k4]]); */
8619: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][resultmodel[nres][k4]],Tqresult[nres][resultmodel[nres][k4]]); */
8620: }
8621: strcpy(gplotlabel+strlen(gplotlabel),")");
8622: fprintf(ficgp,"\n#\n");
8623: if(invalidvarcomb[k1]){
8624: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
8625: continue;
8626: }
8627:
8628: fprintf(ficgp,"# hbijx=backprobability over h years, hb.jx is weighted by observed prev at destination state\n ");
8629: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" \n",subdirf2(optionfilefiname,"PROJB_"),cpt,k1,nres);
8630: fprintf(ficgp,"set label \"Origin alive state %d %s\" at graph 0.98,0.5 center rotate font \"Helvetica,12\"\n",cpt,gplotlabel);
8631: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Prevalence\" \n\
8632: set ter svg size 640, 480\nunset log y\nplot [%.f:%.f] ", ageminpar, agemaxpar);
8633:
8634: /* for (i=1; i<= nlstate+1 ; i ++){ /\* nlstate +1 p11 p21 p.1 *\/ */
8635: istart=nlstate+1; /* Could be one if by state, but nlstate+1 is w.i projection only */
8636: /*istart=1;*/ /* Could be one if by state, but nlstate+1 is w.i projection only */
8637: for (i=istart; i<= nlstate+1 ; i ++){ /* nlstate +1 p11 p21 p.1 */
8638: /*# V1 = 1 V2 = 0 yearproj age p11 p21 p.1 p12 p22 p.2 p13 p23 p.3*/
8639: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
8640: /*# yearproj age p11 p21 p.1 p12 p22 p.2 p13 p23 p.3*/
8641: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
8642: if(i==istart){
8643: fprintf(ficgp,"\"%s\"",subdirf2(fileresu,"FB_"));
8644: }else{
8645: fprintf(ficgp,",\\\n '' ");
8646: }
8647: if(cptcoveff ==0){ /* No covariate */
8648: ioffset=2; /* Age is in 2 */
8649: /*# yearproj age p11 p21 p31 p.1 p12 p22 p32 p.2 p13 p23 p33 p.3 p14 p24 p34 p.4*/
8650: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 */
8651: /*# V1 = 1 yearproj age p11 p21 p31 p.1 p12 p22 p32 p.2 p13 p23 p33 p.3 p14 p24 p34 p.4*/
8652: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 */
8653: fprintf(ficgp," u %d:(", ioffset);
8654: if(i==nlstate+1){
8655: fprintf(ficgp," $%d/(1.-$%d)):1 t 'bw%d' with line lc variable ", \
8656: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset+1+(i-1)+(nlstate+1)*nlstate,cpt );
8657: fprintf(ficgp,",\\\n '' ");
8658: fprintf(ficgp," u %d:(",ioffset);
8659: fprintf(ficgp," (($1-$2) == %d ) ? $%d : 1/0):1 with labels center not ", \
8660: offbyear, \
8661: ioffset+(cpt-1)*(nlstate+1)+1+(i-1) );
8662: }else
8663: fprintf(ficgp," $%d/(1.-$%d)) t 'b%d%d' with line ", \
8664: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset+1+(i-1)+(nlstate+1)*nlstate,cpt,i );
8665: }else{ /* more than 2 covariates */
8666: ioffset=2*cptcoveff+2; /* Age is in 4 or 6 or etc.*/
8667: /*# V1 = 1 V2 = 0 yearproj age p11 p21 p.1 p12 p22 p.2 p13 p23 p.3*/
8668: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
8669: iyearc=ioffset-1;
8670: iagec=ioffset;
8671: fprintf(ficgp," u %d:(",ioffset);
8672: kl=0;
8673: strcpy(gplotcondition,"(");
8674: for (k=1; k<=cptcovs; k++){ /* For each covariate k of the resultline, get corresponding value lv for combination k1 */
8675: if(Dummy[modelresult[nres][k]]==0){ /* To be verified */
8676: /* for (k=1; k<=cptcoveff; k++){ /\* For each covariate writing the chain of conditions *\/ */
8677: /* lv= decodtabm(k1,k,cptcoveff); /\* Should be the covariate value corresponding to combination k1 and covariate k *\/ */
8678: /* lv= codtabm(k1,TnsdVar[Tvaraff[k]]); /\* Should be the covariate value corresponding to combination k1 and covariate k *\/ */
8679: lv=Tvresult[nres][k];
8680: vlv=TinvDoQresult[nres][Tvresult[nres][k]];
8681: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
8682: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
8683: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
8684: /* vlv= nbcode[Tvaraff[k]][lv]; /\* Value of the modality of Tvaraff[k] *\/ */
8685: /* vlv= nbcode[Tvaraff[k]][codtabm(k1,TnsdVar[Tvaraff[k]])]; */
8686: kl++;
8687: /* sprintf(gplotcondition+strlen(gplotcondition),"$%d==%d && $%d==%d " ,kl,Tvaraff[k], kl+1, nbcode[Tvaraff[k]][lv]); */
8688: sprintf(gplotcondition+strlen(gplotcondition),"$%d==%d && $%d==%lg " ,kl,Tvresult[nres][k], kl+1,TinvDoQresult[nres][Tvresult[nres][k]]);
8689: kl++;
8690: if(k <cptcovs && cptcovs>1)
8691: sprintf(gplotcondition+strlen(gplotcondition)," && ");
8692: }
8693: }
8694: strcpy(gplotcondition+strlen(gplotcondition),")");
8695: /* 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 *\/ */
8696: /*6+(cpt-1)*(nlstate+1)+1+(i-1)+(nlstate+1)*nlstate; 6+(1-1)*(2+1)+1+(1-1) +(2+1)*2=13 */
8697: /*6+1+(i-1)+(nlstate+1)*nlstate; 6+1+(1-1) +(2+1)*2=13 */
8698: /* '' 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*/
8699: if(i==nlstate+1){
8700: fprintf(ficgp,"%s ? $%d : 1/0):%d t 'bw%d' with line lc variable", gplotcondition, \
8701: ioffset+(cpt-1)*(nlstate+1)+1+(i-1),iyearc,cpt );
8702: fprintf(ficgp,",\\\n '' ");
8703: fprintf(ficgp," u %d:(",iagec);
8704: /* fprintf(ficgp,"%s && (($5-$6) == %d ) ? $%d/(1.-$%d) : 1/0):5 with labels center not ", gplotcondition, \ */
8705: fprintf(ficgp,"%s && (($%d-$%d) == %d ) ? $%d : 1/0):%d with labels center not ", gplotcondition, \
8706: iyearc,iagec,offbyear, \
8707: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), iyearc );
8708: /* '' u 6:(($1==1 && $2==0 && $3==2 && $4==0) && (($5-$6) == 1947) ? $10/(1.-$22) : 1/0):5 with labels center boxed not*/
8709: }else{
8710: /* fprintf(ficgp,"%s ? $%d/(1.-$%d) : 1/0) t 'p%d%d' with line ", gplotcondition, \ */
8711: fprintf(ficgp,"%s ? $%d : 1/0) t 'b%d%d' with line ", gplotcondition, \
8712: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), cpt,i );
8713: }
8714: } /* end if covariate */
8715: } /* nlstate */
8716: fprintf(ficgp,"\nset out; unset label;\n");
8717: } /* end cpt state*/
8718: } /* end covariate */
8719: } /* End if prevbcast */
8720:
8721:
8722: /* 9eme writing MLE parameters */
8723: fprintf(ficgp,"\n##############\n#9eme MLE estimated parameters\n#############\n");
8724: for(i=1,jk=1; i <=nlstate; i++){
8725: fprintf(ficgp,"# initial state %d\n",i);
8726: for(k=1; k <=(nlstate+ndeath); k++){
8727: if (k != i) {
8728: fprintf(ficgp,"# current state %d\n",k);
8729: for(j=1; j <=ncovmodel; j++){
8730: fprintf(ficgp,"p%d=%f; ",jk,p[jk]);
8731: jk++;
8732: }
8733: fprintf(ficgp,"\n");
8734: }
8735: }
8736: }
8737: fprintf(ficgp,"##############\n#\n");
8738:
8739: /*goto avoid;*/
8740: /* 10eme Graphics of probabilities or incidences using written MLE parameters */
8741: fprintf(ficgp,"\n##############\n#10eme Graphics of probabilities or incidences\n#############\n");
8742: fprintf(ficgp,"# logi(p12/p11)=a12+b12*age+c12age*age+d12*V1+e12*V1*age\n");
8743: fprintf(ficgp,"# logi(p12/p11)=p1 +p2*age +p3*age*age+ p4*V1+ p5*V1*age\n");
8744: fprintf(ficgp,"# logi(p13/p11)=a13+b13*age+c13age*age+d13*V1+e13*V1*age\n");
8745: fprintf(ficgp,"# logi(p13/p11)=p6 +p7*age +p8*age*age+ p9*V1+ p10*V1*age\n");
8746: fprintf(ficgp,"# p12+p13+p14+p11=1=p11(1+exp(a12+b12*age+c12age*age+d12*V1+e12*V1*age)\n");
8747: fprintf(ficgp,"# +exp(a13+b13*age+c13age*age+d13*V1+e13*V1*age)+...)\n");
8748: fprintf(ficgp,"# p11=1/(1+exp(a12+b12*age+c12age*age+d12*V1+e12*V1*age)\n");
8749: fprintf(ficgp,"# +exp(a13+b13*age+c13age*age+d13*V1+e13*V1*age)+...)\n");
8750: fprintf(ficgp,"# p12=exp(a12+b12*age+c12age*age+d12*V1+e12*V1*age)/\n");
8751: fprintf(ficgp,"# (1+exp(a12+b12*age+c12age*age+d12*V1+e12*V1*age)\n");
8752: fprintf(ficgp,"# +exp(a13+b13*age+c13age*age+d13*V1+e13*V1*age))\n");
8753: fprintf(ficgp,"# +exp(a14+b14*age+c14age*age+d14*V1+e14*V1*age)+...)\n");
8754: fprintf(ficgp,"#\n");
8755: for(ng=1; ng<=3;ng++){ /* Number of graphics: first is logit, 2nd is probabilities, third is incidences per year*/
8756: fprintf(ficgp,"#Number of graphics: first is logit, 2nd is probabilities, third is incidences per year\n");
8757: fprintf(ficgp,"#model=1+age+%s \n",model);
8758: fprintf(ficgp,"# Type of graphic ng=%d\n",ng);
8759: fprintf(ficgp,"# k1=1 to 2^%d=%d\n",cptcoveff,m);/* to be checked */
8760: /* for(k1=1; k1 <=m; k1++) /\* For each combination of covariate *\/ */
8761: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
8762: /* k1=nres; */
8763: k1=TKresult[nres];
8764: if(TKresult[nres]==0) k1=1; /* To be checked for noresult */
8765: fprintf(ficgp,"\n\n# Resultline k1=%d ",k1);
8766: strcpy(gplotlabel,"(");
8767: /*sprintf(gplotlabel+strlen(gplotlabel)," Dummy combination %d ",k1);*/
8768: for (k=1; k<=cptcovs; k++){ /**< cptcovs number of SIMPLE covariates in the model V2+V1 =2 (dummy or quantit or time varying) */
8769: /* for each resultline nres, and position k, Tvresult[nres][k] gives the name of the variable and
8770: TinvDoQresult[nres][Tvresult[nres][k]] gives its value double or integer) */
8771: fprintf(ficgp," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
8772: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
8773: }
8774: /* if(m != 1 && TKresult[nres]!= k1) */
8775: /* continue; */
8776: /* fprintf(ficgp,"\n\n# Combination of dummy k1=%d which is ",k1); */
8777: /* strcpy(gplotlabel,"("); */
8778: /* /\*sprintf(gplotlabel+strlen(gplotlabel)," Dummy combination %d ",k1);*\/ */
8779: /* for (k=1; k<=cptcoveff; k++){ /\* For each correspondig covariate value *\/ */
8780: /* /\* lv= decodtabm(k1,k,cptcoveff); /\\* Should be the covariate value corresponding to k1 combination and kth covariate *\\/ *\/ */
8781: /* lv= codtabm(k1,TnsdVar[Tvaraff[k]]); /\* Should be the covariate value corresponding to combination k1 and covariate k *\/ */
8782: /* /\* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 *\/ */
8783: /* /\* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 *\/ */
8784: /* /\* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 *\/ */
8785: /* /\* vlv= nbcode[Tvaraff[k]][lv]; *\/ */
8786: /* vlv= nbcode[Tvaraff[k]][codtabm(k1,TnsdVar[Tvaraff[k]])]; */
8787: /* fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv); */
8788: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv); */
8789: /* } */
8790: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
8791: /* fprintf(ficgp," V%d=%f ",Tvqresult[nres][resultmodel[nres][k4]],Tqresult[nres][resultmodel[nres][k4]]); */
8792: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][resultmodel[nres][k4]],Tqresult[nres][resultmodel[nres][k4]]); */
8793: /* } */
8794: strcpy(gplotlabel+strlen(gplotlabel),")");
8795: fprintf(ficgp,"\n#\n");
8796: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" ",subdirf2(optionfilefiname,"PE_"),k1,ng,nres);
8797: fprintf(ficgp,"\nset key outside ");
8798: /* fprintf(ficgp,"\nset label \"%s\" at graph 1.2,0.5 center rotate font \"Helvetica,12\"\n",gplotlabel); */
8799: fprintf(ficgp,"\nset title \"%s\" font \"Helvetica,12\"\n",gplotlabel);
8800: fprintf(ficgp,"\nset ter svg size 640, 480 ");
8801: if (ng==1){
8802: fprintf(ficgp,"\nset ylabel \"Value of the logit of the model\"\n"); /* exp(a12+b12*x) could be nice */
8803: fprintf(ficgp,"\nunset log y");
8804: }else if (ng==2){
8805: fprintf(ficgp,"\nset ylabel \"Probability\"\n");
8806: fprintf(ficgp,"\nset log y");
8807: }else if (ng==3){
8808: fprintf(ficgp,"\nset ylabel \"Quasi-incidence per year\"\n");
8809: fprintf(ficgp,"\nset log y");
8810: }else
8811: fprintf(ficgp,"\nunset title ");
8812: fprintf(ficgp,"\nplot [%.f:%.f] ",ageminpar,agemaxpar);
8813: i=1;
8814: for(k2=1; k2<=nlstate; k2++) {
8815: k3=i;
8816: for(k=1; k<=(nlstate+ndeath); k++) {
8817: if (k != k2){
8818: switch( ng) {
8819: case 1:
8820: if(nagesqr==0)
8821: fprintf(ficgp," p%d+p%d*x",i,i+1);
8822: else /* nagesqr =1 */
8823: fprintf(ficgp," p%d+p%d*x+p%d*x*x",i,i+1,i+1+nagesqr);
8824: break;
8825: case 2: /* ng=2 */
8826: if(nagesqr==0)
8827: fprintf(ficgp," exp(p%d+p%d*x",i,i+1);
8828: else /* nagesqr =1 */
8829: fprintf(ficgp," exp(p%d+p%d*x+p%d*x*x",i,i+1,i+1+nagesqr);
8830: break;
8831: case 3:
8832: if(nagesqr==0)
8833: fprintf(ficgp," %f*exp(p%d+p%d*x",YEARM/stepm,i,i+1);
8834: else /* nagesqr =1 */
8835: fprintf(ficgp," %f*exp(p%d+p%d*x+p%d*x*x",YEARM/stepm,i,i+1,i+1+nagesqr);
8836: break;
8837: }
8838: ij=1;/* To be checked else nbcode[0][0] wrong */
8839: ijp=1; /* product no age */
8840: /* for(j=3; j <=ncovmodel-nagesqr; j++) { */
8841: for(j=1; j <=cptcovt; j++) { /* For each covariate of the simplified model */
8842: /* printf("Tage[%d]=%d, j=%d\n", ij, Tage[ij], j); */
8843: switch(Typevar[j]){
8844: case 1:
8845: if(cptcovage >0){ /* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1, 2 V5 and V1 */
8846: if(j==Tage[ij]) { /* Product by age To be looked at!!*//* Bug valgrind */
8847: if(ij <=cptcovage) { /* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1, 2 V5 and V1 */
8848: if(DummyV[j]==0){/* Bug valgrind */
8849: fprintf(ficgp,"+p%d*%d*x",i+j+2+nagesqr-1,Tinvresult[nres][Tvar[j]]);;
8850: }else{ /* quantitative */
8851: fprintf(ficgp,"+p%d*%f*x",i+j+2+nagesqr-1,Tqinvresult[nres][Tvar[j]]); /* Tqinvresult in decoderesult */
8852: /* fprintf(ficgp,"+p%d*%d*x",i+j+nagesqr-1,nbcode[Tvar[j-2]][codtabm(k1,Tvar[j-2])]); */
8853: }
8854: ij++;
8855: }
8856: }
8857: }
8858: break;
8859: case 2:
8860: if(cptcovprod >0){
8861: if(j==Tprod[ijp]) { /* */
8862: /* printf("Tprod[%d]=%d, j=%d\n", ij, Tprod[ijp], j); */
8863: if(ijp <=cptcovprod) { /* Product */
8864: if(DummyV[Tvard[ijp][1]]==0){/* Vn is dummy */
8865: if(DummyV[Tvard[ijp][2]]==0){/* Vn and Vm are dummy */
8866: /* fprintf(ficgp,"+p%d*%d*%d",i+j+2+nagesqr-1,nbcode[Tvard[ijp][1]][codtabm(k1,j)],nbcode[Tvard[ijp][2]][codtabm(k1,j)]); */
8867: fprintf(ficgp,"+p%d*%d*%d",i+j+2+nagesqr-1,Tinvresult[nres][Tvard[ijp][1]],Tinvresult[nres][Tvard[ijp][2]]);
8868: }else{ /* Vn is dummy and Vm is quanti */
8869: /* fprintf(ficgp,"+p%d*%d*%f",i+j+2+nagesqr-1,nbcode[Tvard[ijp][1]][codtabm(k1,j)],Tqinvresult[nres][Tvard[ijp][2]]); */
8870: fprintf(ficgp,"+p%d*%d*%f",i+j+2+nagesqr-1,Tinvresult[nres][Tvard[ijp][1]],Tqinvresult[nres][Tvard[ijp][2]]);
8871: }
8872: }else{ /* Vn*Vm Vn is quanti */
8873: if(DummyV[Tvard[ijp][2]]==0){
8874: fprintf(ficgp,"+p%d*%d*%f",i+j+2+nagesqr-1,Tinvresult[nres][Tvard[ijp][2]],Tqinvresult[nres][Tvard[ijp][1]]);
8875: }else{ /* Both quanti */
8876: fprintf(ficgp,"+p%d*%f*%f",i+j+2+nagesqr-1,Tqinvresult[nres][Tvard[ijp][1]],Tqinvresult[nres][Tvard[ijp][2]]);
8877: }
8878: }
8879: ijp++;
8880: }
8881: } /* end Tprod */
8882: }
8883: break;
8884: case 0:
8885: /* simple covariate */
8886: /* fprintf(ficgp,"+p%d*%d",i+j+2+nagesqr-1,nbcode[Tvar[j]][codtabm(k1,j)]); /\* Valgrind bug nbcode *\/ */
8887: if(Dummy[j]==0){
8888: fprintf(ficgp,"+p%d*%d",i+j+2+nagesqr-1,Tinvresult[nres][Tvar[j]]); /* */
8889: }else{ /* quantitative */
8890: fprintf(ficgp,"+p%d*%f",i+j+2+nagesqr-1,Tqinvresult[nres][Tvar[j]]); /* */
8891: /* fprintf(ficgp,"+p%d*%d*x",i+j+nagesqr-1,nbcode[Tvar[j-2]][codtabm(k1,Tvar[j-2])]); */
8892: }
8893: /* end simple */
8894: break;
8895: default:
8896: break;
8897: } /* end switch */
8898: } /* end j */
8899: }else{ /* k=k2 */
8900: if(ng !=1 ){ /* For logit formula of log p11 is more difficult to get */
8901: fprintf(ficgp," (1.");i=i-ncovmodel;
8902: }else
8903: i=i-ncovmodel;
8904: }
8905:
8906: if(ng != 1){
8907: fprintf(ficgp,")/(1");
8908:
8909: for(cpt=1; cpt <=nlstate; cpt++){
8910: if(nagesqr==0)
8911: fprintf(ficgp,"+exp(p%d+p%d*x",k3+(cpt-1)*ncovmodel,k3+(cpt-1)*ncovmodel+1);
8912: else /* nagesqr =1 */
8913: fprintf(ficgp,"+exp(p%d+p%d*x+p%d*x*x",k3+(cpt-1)*ncovmodel,k3+(cpt-1)*ncovmodel+1,k3+(cpt-1)*ncovmodel+1+nagesqr);
8914:
8915: ij=1;
8916: ijp=1;
8917: /* for(j=3; j <=ncovmodel-nagesqr; j++){ */
8918: for(j=1; j <=cptcovt; j++) { /* For each covariate of the simplified model */
8919: switch(Typevar[j]){
8920: case 1:
8921: if(cptcovage >0){
8922: if(j==Tage[ij]) { /* Bug valgrind */
8923: if(ij <=cptcovage) { /* Bug valgrind */
8924: if(DummyV[j]==0){/* Bug valgrind */
8925: /* fprintf(ficgp,"+p%d*%d*x",k3+(cpt-1)*ncovmodel+1+j-2+nagesqr,nbcode[Tvar[j-2]][codtabm(k1,j-2)]); */
8926: /* fprintf(ficgp,"+p%d*%d*x",k3+(cpt-1)*ncovmodel+1+j+nagesqr,nbcode[Tvar[j]][codtabm(k1,j)]); */
8927: fprintf(ficgp,"+p%d*%d*x",k3+(cpt-1)*ncovmodel+1+j+nagesqr,Tinvresult[nres][Tvar[j]]);
8928: /* fprintf(ficgp,"+p%d*%d*x",i+j+2+nagesqr-1,Tinvresult[nres][Tvar[j]]);; */
8929: /* fprintf(ficgp,"+p%d*%d*x",k3+(cpt-1)*ncovmodel+1+j-2+nagesqr,nbcode[Tvar[j-2]][codtabm(k1,Tvar[j-2])]); */
8930: }else{ /* quantitative */
8931: /* fprintf(ficgp,"+p%d*%f*x",i+j+2+nagesqr-1,Tqinvresult[nres][Tvar[j]]); /\* Tqinvresult in decoderesult *\/ */
8932: fprintf(ficgp,"+p%d*%f*x",k3+(cpt-1)*ncovmodel+1+j+nagesqr,Tqinvresult[nres][Tvar[j]]); /* Tqinvresult in decoderesult */
8933: /* fprintf(ficgp,"+p%d*%f*x",i+j+2+nagesqr-1,Tqinvresult[nres][Tvar[j]]); /\* Tqinvresult in decoderesult *\/ */
8934: /* fprintf(ficgp,"+p%d*%d*x",i+j+nagesqr-1,nbcode[Tvar[j-2]][codtabm(k1,Tvar[j-2])]); */
8935: }
8936: ij++;
8937: }
8938: }
8939: }
8940: break;
8941: case 2:
8942: if(cptcovprod >0){
8943: if(j==Tprod[ijp]) { /* */
8944: /* printf("Tprod[%d]=%d, j=%d\n", ij, Tprod[ijp], j); */
8945: if(ijp <=cptcovprod) { /* Product */
8946: if(DummyV[Tvard[ijp][1]]==0){/* Vn is dummy */
8947: if(DummyV[Tvard[ijp][2]]==0){/* Vn and Vm are dummy */
8948: /* fprintf(ficgp,"+p%d*%d*%d",i+j+2+nagesqr-1,nbcode[Tvard[ijp][1]][codtabm(k1,j)],nbcode[Tvard[ijp][2]][codtabm(k1,j)]); */
8949: fprintf(ficgp,"+p%d*%d*%d",k3+(cpt-1)*ncovmodel+1+j+nagesqr,Tinvresult[nres][Tvard[ijp][1]],Tinvresult[nres][Tvard[ijp][2]]);
8950: /* fprintf(ficgp,"+p%d*%d*%d",i+j+2+nagesqr-1,Tinvresult[nres][Tvard[ijp][1]],Tinvresult[nres][Tvard[ijp][2]]); */
8951: }else{ /* Vn is dummy and Vm is quanti */
8952: /* fprintf(ficgp,"+p%d*%d*%f",i+j+2+nagesqr-1,nbcode[Tvard[ijp][1]][codtabm(k1,j)],Tqinvresult[nres][Tvard[ijp][2]]); */
8953: fprintf(ficgp,"+p%d*%d*%f",k3+(cpt-1)*ncovmodel+1+j+nagesqr,Tinvresult[nres][Tvard[ijp][1]],Tqinvresult[nres][Tvard[ijp][2]]);
8954: /* fprintf(ficgp,"+p%d*%d*%f",i+j+2+nagesqr-1,Tinvresult[nres][Tvard[ijp][1]],Tqinvresult[nres][Tvard[ijp][2]]); */
8955: }
8956: }else{ /* Vn*Vm Vn is quanti */
8957: if(DummyV[Tvard[ijp][2]]==0){
8958: fprintf(ficgp,"+p%d*%d*%f",k3+(cpt-1)*ncovmodel+1+j+nagesqr,Tinvresult[nres][Tvard[ijp][2]],Tqinvresult[nres][Tvard[ijp][1]]);
8959: /* fprintf(ficgp,"+p%d*%d*%f",i+j+2+nagesqr-1,Tinvresult[nres][Tvard[ijp][2]],Tqinvresult[nres][Tvard[ijp][1]]); */
8960: }else{ /* Both quanti */
8961: fprintf(ficgp,"+p%d*%f*%f",k3+(cpt-1)*ncovmodel+1+j+nagesqr,Tqinvresult[nres][Tvard[ijp][1]],Tqinvresult[nres][Tvard[ijp][2]]);
8962: /* fprintf(ficgp,"+p%d*%f*%f",i+j+2+nagesqr-1,Tqinvresult[nres][Tvard[ijp][1]],Tqinvresult[nres][Tvard[ijp][2]]); */
8963: }
8964: }
8965: ijp++;
8966: }
8967: } /* end Tprod */
8968: } /* end if */
8969: break;
8970: case 0:
8971: /* simple covariate */
8972: /* fprintf(ficgp,"+p%d*%d",i+j+2+nagesqr-1,nbcode[Tvar[j]][codtabm(k1,j)]); /\* Valgrind bug nbcode *\/ */
8973: if(Dummy[j]==0){
8974: /* fprintf(ficgp,"+p%d*%d",i+j+2+nagesqr-1,Tinvresult[nres][Tvar[j]]); /\* *\/ */
8975: fprintf(ficgp,"+p%d*%d",k3+(cpt-1)*ncovmodel+1+j+nagesqr,Tinvresult[nres][Tvar[j]]); /* */
8976: /* fprintf(ficgp,"+p%d*%d",i+j+2+nagesqr-1,Tinvresult[nres][Tvar[j]]); /\* *\/ */
8977: }else{ /* quantitative */
8978: fprintf(ficgp,"+p%d*%f",k3+(cpt-1)*ncovmodel+1+j+nagesqr,Tqinvresult[nres][Tvar[j]]); /* */
8979: /* fprintf(ficgp,"+p%d*%f",i+j+2+nagesqr-1,Tqinvresult[nres][Tvar[j]]); /\* *\/ */
8980: /* fprintf(ficgp,"+p%d*%d*x",i+j+nagesqr-1,nbcode[Tvar[j-2]][codtabm(k1,Tvar[j-2])]); */
8981: }
8982: /* end simple */
8983: /* fprintf(ficgp,"+p%d*%d",k3+(cpt-1)*ncovmodel+1+j-2+nagesqr,nbcode[Tvar[j-2]][codtabm(k1,j-2)]);/\* Valgrind bug nbcode *\/ */
8984: break;
8985: default:
8986: break;
8987: } /* end switch */
8988: }
8989: fprintf(ficgp,")");
8990: }
8991: fprintf(ficgp,")");
8992: if(ng ==2)
8993: fprintf(ficgp," w l lw 2 lt (%d*%d+%d)%%%d+1 dt %d t \"p%d%d\" ", nlstate+ndeath, k2, k, nlstate+ndeath, k2, k2,k);
8994: else /* ng= 3 */
8995: fprintf(ficgp," w l lw 2 lt (%d*%d+%d)%%%d+1 dt %d t \"i%d%d\" ", nlstate+ndeath, k2, k, nlstate+ndeath, k2, k2,k);
8996: }else{ /* end ng <> 1 */
8997: if( k !=k2) /* logit p11 is hard to draw */
8998: fprintf(ficgp," w l lw 2 lt (%d*%d+%d)%%%d+1 dt %d t \"logit(p%d%d)\" ", nlstate+ndeath, k2, k, nlstate+ndeath, k2, k2,k);
8999: }
9000: if ((k+k2)!= (nlstate*2+ndeath) && ng != 1)
9001: fprintf(ficgp,",");
9002: if (ng == 1 && k!=k2 && (k+k2)!= (nlstate*2+ndeath))
9003: fprintf(ficgp,",");
9004: i=i+ncovmodel;
9005: } /* end k */
9006: } /* end k2 */
9007: /* fprintf(ficgp,"\n set out; unset label;set key default;\n"); */
9008: fprintf(ficgp,"\n set out; unset title;set key default;\n");
9009: } /* end resultline */
9010: } /* end ng */
9011: /* avoid: */
9012: fflush(ficgp);
9013: } /* end gnuplot */
9014:
9015:
9016: /*************** Moving average **************/
9017: /* int movingaverage(double ***probs, double bage, double fage, double ***mobaverage, int mobilav, double bageout, double fageout){ */
9018: int movingaverage(double ***probs, double bage, double fage, double ***mobaverage, int mobilav){
9019:
9020: int i, cpt, cptcod;
9021: int modcovmax =1;
9022: int mobilavrange, mob;
9023: int iage=0;
9024: int firstA1=0, firstA2=0;
9025:
9026: double sum=0., sumr=0.;
9027: double age;
9028: double *sumnewp, *sumnewm, *sumnewmr;
9029: double *agemingood, *agemaxgood;
9030: double *agemingoodr, *agemaxgoodr;
9031:
9032:
9033: /* modcovmax=2*cptcoveff; Max number of modalities. We suppose */
9034: /* a covariate has 2 modalities, should be equal to ncovcombmax */
9035:
9036: sumnewp = vector(1,ncovcombmax);
9037: sumnewm = vector(1,ncovcombmax);
9038: sumnewmr = vector(1,ncovcombmax);
9039: agemingood = vector(1,ncovcombmax);
9040: agemingoodr = vector(1,ncovcombmax);
9041: agemaxgood = vector(1,ncovcombmax);
9042: agemaxgoodr = vector(1,ncovcombmax);
9043:
9044: for (cptcod=1;cptcod<=ncovcombmax;cptcod++){
9045: sumnewm[cptcod]=0.; sumnewmr[cptcod]=0.;
9046: sumnewp[cptcod]=0.;
9047: agemingood[cptcod]=0, agemingoodr[cptcod]=0;
9048: agemaxgood[cptcod]=0, agemaxgoodr[cptcod]=0;
9049: }
9050: if (cptcovn<1) ncovcombmax=1; /* At least 1 pass */
9051:
9052: if(mobilav==-1 || mobilav==1||mobilav ==3 ||mobilav==5 ||mobilav== 7){
9053: if(mobilav==1 || mobilav==-1) mobilavrange=5; /* default */
9054: else mobilavrange=mobilav;
9055: for (age=bage; age<=fage; age++)
9056: for (i=1; i<=nlstate;i++)
9057: for (cptcod=1;cptcod<=ncovcombmax;cptcod++)
9058: mobaverage[(int)age][i][cptcod]=probs[(int)age][i][cptcod];
9059: /* We keep the original values on the extreme ages bage, fage and for
9060: fage+1 and bage-1 we use a 3 terms moving average; for fage+2 bage+2
9061: we use a 5 terms etc. until the borders are no more concerned.
9062: */
9063: for (mob=3;mob <=mobilavrange;mob=mob+2){
9064: for (age=bage+(mob-1)/2; age<=fage-(mob-1)/2; age++){
9065: for (cptcod=1;cptcod<=ncovcombmax;cptcod++){
9066: sumnewm[cptcod]=0.;
9067: for (i=1; i<=nlstate;i++){
9068: mobaverage[(int)age][i][cptcod] =probs[(int)age][i][cptcod];
9069: for (cpt=1;cpt<=(mob-1)/2;cpt++){
9070: mobaverage[(int)age][i][cptcod] +=probs[(int)age-cpt][i][cptcod];
9071: mobaverage[(int)age][i][cptcod] +=probs[(int)age+cpt][i][cptcod];
9072: }
9073: mobaverage[(int)age][i][cptcod]=mobaverage[(int)age][i][cptcod]/mob;
9074: sumnewm[cptcod]+=mobaverage[(int)age][i][cptcod];
9075: } /* end i */
9076: if(sumnewm[cptcod] >1.e-3) mobaverage[(int)age][i][cptcod]=mobaverage[(int)age][i][cptcod]/sumnewm[cptcod]; /* Rescaling to sum one */
9077: } /* end cptcod */
9078: }/* end age */
9079: }/* end mob */
9080: }else{
9081: printf("Error internal in movingaverage, mobilav=%d.\n",mobilav);
9082: return -1;
9083: }
9084:
9085: for (cptcod=1;cptcod<=ncovcombmax;cptcod++){ /* for each combination */
9086: /* for (age=bage+(mob-1)/2; age<=fage-(mob-1)/2; age++){ */
9087: if(invalidvarcomb[cptcod]){
9088: printf("\nCombination (%d) ignored because no cases \n",cptcod);
9089: continue;
9090: }
9091:
9092: for (age=fage-(mob-1)/2; age>=bage+(mob-1)/2; age--){ /*looking for the youngest and oldest good age */
9093: sumnewm[cptcod]=0.;
9094: sumnewmr[cptcod]=0.;
9095: for (i=1; i<=nlstate;i++){
9096: sumnewm[cptcod]+=mobaverage[(int)age][i][cptcod];
9097: sumnewmr[cptcod]+=probs[(int)age][i][cptcod];
9098: }
9099: if(fabs(sumnewmr[cptcod] - 1.) <= 1.e-3) { /* good without smoothing */
9100: agemingoodr[cptcod]=age;
9101: }
9102: if(fabs(sumnewm[cptcod] - 1.) <= 1.e-3) { /* good */
9103: agemingood[cptcod]=age;
9104: }
9105: } /* age */
9106: for (age=bage+(mob-1)/2; age<=fage-(mob-1)/2; age++){ /*looking for the youngest and oldest good age */
9107: sumnewm[cptcod]=0.;
9108: sumnewmr[cptcod]=0.;
9109: for (i=1; i<=nlstate;i++){
9110: sumnewm[cptcod]+=mobaverage[(int)age][i][cptcod];
9111: sumnewmr[cptcod]+=probs[(int)age][i][cptcod];
9112: }
9113: if(fabs(sumnewmr[cptcod] - 1.) <= 1.e-3) { /* good without smoothing */
9114: agemaxgoodr[cptcod]=age;
9115: }
9116: if(fabs(sumnewm[cptcod] - 1.) <= 1.e-3) { /* good */
9117: agemaxgood[cptcod]=age;
9118: }
9119: } /* age */
9120: /* Thus we have agemingood and agemaxgood as well as goodr for raw (preobs) */
9121: /* but they will change */
9122: firstA1=0;firstA2=0;
9123: for (age=fage-(mob-1)/2; age>=bage; age--){/* From oldest to youngest, filling up to the youngest */
9124: sumnewm[cptcod]=0.;
9125: sumnewmr[cptcod]=0.;
9126: for (i=1; i<=nlstate;i++){
9127: sumnewm[cptcod]+=mobaverage[(int)age][i][cptcod];
9128: sumnewmr[cptcod]+=probs[(int)age][i][cptcod];
9129: }
9130: if(mobilav==-1){ /* Forcing raw ages if good else agemingood */
9131: if(fabs(sumnewmr[cptcod] - 1.) <= 1.e-3) { /* good without smoothing */
9132: agemaxgoodr[cptcod]=age; /* age min */
9133: for (i=1; i<=nlstate;i++)
9134: mobaverage[(int)age][i][cptcod]=probs[(int)age][i][cptcod];
9135: }else{ /* bad we change the value with the values of good ages */
9136: for (i=1; i<=nlstate;i++){
9137: mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemaxgoodr[cptcod]][i][cptcod];
9138: } /* i */
9139: } /* end bad */
9140: }else{
9141: if(fabs(sumnewm[cptcod] - 1.) <= 1.e-3) { /* good */
9142: agemaxgood[cptcod]=age;
9143: }else{ /* bad we change the value with the values of good ages */
9144: for (i=1; i<=nlstate;i++){
9145: mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemaxgood[cptcod]][i][cptcod];
9146: } /* i */
9147: } /* end bad */
9148: }/* end else */
9149: sum=0.;sumr=0.;
9150: for (i=1; i<=nlstate;i++){
9151: sum+=mobaverage[(int)age][i][cptcod];
9152: sumr+=probs[(int)age][i][cptcod];
9153: }
9154: if(fabs(sum - 1.) > 1.e-3) { /* bad */
9155: if(!firstA1){
9156: firstA1=1;
9157: printf("Moving average A1: For this combination of covariate cptcod=%d, we can't get a smoothed prevalence which sums to one (%f) at any descending age! age=%d, could you increase bage=%d. Others in log file...\n",cptcod,sumr, (int)age, (int)bage);
9158: }
9159: fprintf(ficlog,"Moving average A1: For this combination of covariate cptcod=%d, we can't get a smoothed prevalence which sums to one (%f) at any descending age! age=%d, could you increase bage=%d\n",cptcod,sumr, (int)age, (int)bage);
9160: } /* end bad */
9161: /* else{ /\* We found some ages summing to one, we will smooth the oldest *\/ */
9162: if(fabs(sumr - 1.) > 1.e-3) { /* bad */
9163: if(!firstA2){
9164: firstA2=1;
9165: printf("Moving average A2: For this combination of covariate cptcod=%d, the raw prevalence doesn't sums to one (%f) even with smoothed values at young ages! age=%d, could you increase bage=%d. Others in log file...\n",cptcod,sumr, (int)age, (int)bage);
9166: }
9167: fprintf(ficlog,"Moving average A2: For this combination of covariate cptcod=%d, the raw prevalence doesn't sums to one (%f) even with smoothed values at young ages! age=%d, could you increase bage=%d\n",cptcod,sumr, (int)age, (int)bage);
9168: } /* end bad */
9169: }/* age */
9170:
9171: for (age=bage+(mob-1)/2; age<=fage; age++){/* From youngest, finding the oldest wrong */
9172: sumnewm[cptcod]=0.;
9173: sumnewmr[cptcod]=0.;
9174: for (i=1; i<=nlstate;i++){
9175: sumnewm[cptcod]+=mobaverage[(int)age][i][cptcod];
9176: sumnewmr[cptcod]+=probs[(int)age][i][cptcod];
9177: }
9178: if(mobilav==-1){ /* Forcing raw ages if good else agemingood */
9179: if(fabs(sumnewmr[cptcod] - 1.) <= 1.e-3) { /* good */
9180: agemingoodr[cptcod]=age;
9181: for (i=1; i<=nlstate;i++)
9182: mobaverage[(int)age][i][cptcod]=probs[(int)age][i][cptcod];
9183: }else{ /* bad we change the value with the values of good ages */
9184: for (i=1; i<=nlstate;i++){
9185: mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemingoodr[cptcod]][i][cptcod];
9186: } /* i */
9187: } /* end bad */
9188: }else{
9189: if(fabs(sumnewm[cptcod] - 1.) <= 1.e-3) { /* good */
9190: agemingood[cptcod]=age;
9191: }else{ /* bad */
9192: for (i=1; i<=nlstate;i++){
9193: mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemingood[cptcod]][i][cptcod];
9194: } /* i */
9195: } /* end bad */
9196: }/* end else */
9197: sum=0.;sumr=0.;
9198: for (i=1; i<=nlstate;i++){
9199: sum+=mobaverage[(int)age][i][cptcod];
9200: sumr+=mobaverage[(int)age][i][cptcod];
9201: }
9202: if(fabs(sum - 1.) > 1.e-3) { /* bad */
9203: printf("Moving average B1: For this combination of covariate cptcod=%d, we can't get a smoothed prevalence which sums to one (%f) at any descending age! age=%d, could you decrease fage=%d?\n",cptcod, sum, (int) age, (int)fage);
9204: } /* end bad */
9205: /* else{ /\* We found some ages summing to one, we will smooth the oldest *\/ */
9206: if(fabs(sumr - 1.) > 1.e-3) { /* bad */
9207: printf("Moving average B2: For this combination of covariate cptcod=%d, the raw prevalence doesn't sums to one (%f) even with smoothed values at young ages! age=%d, could you increase fage=%d\n",cptcod,sumr, (int)age, (int)fage);
9208: } /* end bad */
9209: }/* age */
9210:
9211:
9212: for (age=bage; age<=fage; age++){
9213: /* printf("%d %d ", cptcod, (int)age); */
9214: sumnewp[cptcod]=0.;
9215: sumnewm[cptcod]=0.;
9216: for (i=1; i<=nlstate;i++){
9217: sumnewp[cptcod]+=probs[(int)age][i][cptcod];
9218: sumnewm[cptcod]+=mobaverage[(int)age][i][cptcod];
9219: /* printf("%.4f %.4f ",probs[(int)age][i][cptcod], mobaverage[(int)age][i][cptcod]); */
9220: }
9221: /* printf("%.4f %.4f \n",sumnewp[cptcod], sumnewm[cptcod]); */
9222: }
9223: /* printf("\n"); */
9224: /* } */
9225:
9226: /* brutal averaging */
9227: /* for (i=1; i<=nlstate;i++){ */
9228: /* for (age=1; age<=bage; age++){ */
9229: /* mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemingood[cptcod]][i][cptcod]; */
9230: /* /\* printf("age=%d i=%d cptcod=%d mobaverage=%.4f \n",(int)age,i, cptcod, mobaverage[(int)age][i][cptcod]); *\/ */
9231: /* } */
9232: /* for (age=fage; age<=AGESUP; age++){ */
9233: /* mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemaxgood[cptcod]][i][cptcod]; */
9234: /* /\* printf("age=%d i=%d cptcod=%d mobaverage=%.4f \n",(int)age,i, cptcod, mobaverage[(int)age][i][cptcod]); *\/ */
9235: /* } */
9236: /* } /\* end i status *\/ */
9237: /* for (i=nlstate+1; i<=nlstate+ndeath;i++){ */
9238: /* for (age=1; age<=AGESUP; age++){ */
9239: /* /\*printf("i=%d, age=%d, cptcod=%d\n",i, (int)age, cptcod);*\/ */
9240: /* mobaverage[(int)age][i][cptcod]=0.; */
9241: /* } */
9242: /* } */
9243: }/* end cptcod */
9244: free_vector(agemaxgoodr,1, ncovcombmax);
9245: free_vector(agemaxgood,1, ncovcombmax);
9246: free_vector(agemingood,1, ncovcombmax);
9247: free_vector(agemingoodr,1, ncovcombmax);
9248: free_vector(sumnewmr,1, ncovcombmax);
9249: free_vector(sumnewm,1, ncovcombmax);
9250: free_vector(sumnewp,1, ncovcombmax);
9251: return 0;
9252: }/* End movingaverage */
9253:
9254:
9255:
9256: /************** Forecasting ******************/
9257: /* void prevforecast(char fileres[], double dateintmean, double anprojd, double mprojd, double jprojd, double ageminpar, double agemax, double dateprev1, double dateprev2, int mobilav, double ***prev, double bage, double fage, int firstpass, int lastpass, double anprojf, double p[], int cptcoveff)*/
9258: void prevforecast(char fileres[], double dateintmean, double dateprojd, double dateprojf, double ageminpar, double agemax, double dateprev1, double dateprev2, int mobilav, double ***prev, double bage, double fage, int firstpass, int lastpass, double p[], int cptcoveff){
9259: /* dateintemean, mean date of interviews
9260: dateprojd, year, month, day of starting projection
9261: dateprojf date of end of projection;year of end of projection (same day and month as proj1).
9262: agemin, agemax range of age
9263: dateprev1 dateprev2 range of dates during which prevalence is computed
9264: */
9265: /* double anprojd, mprojd, jprojd; */
9266: /* double anprojf, mprojf, jprojf; */
9267: int yearp, stepsize, hstepm, nhstepm, j, k, cptcod, i, h, i1, k4, nres=0;
9268: double agec; /* generic age */
9269: double agelim, ppij, yp,yp1,yp2;
9270: double *popeffectif,*popcount;
9271: double ***p3mat;
9272: /* double ***mobaverage; */
9273: char fileresf[FILENAMELENGTH];
9274:
9275: agelim=AGESUP;
9276: /* Compute observed prevalence between dateprev1 and dateprev2 by counting the number of people
9277: in each health status at the date of interview (if between dateprev1 and dateprev2).
9278: We still use firstpass and lastpass as another selection.
9279: */
9280: /* freqsummary(fileres, agemin, agemax, s, agev, nlstate, imx,Tvaraff,nbcode, ncodemax,mint,anint,strstart,\ */
9281: /* firstpass, lastpass, stepm, weightopt, model); */
9282:
9283: strcpy(fileresf,"F_");
9284: strcat(fileresf,fileresu);
9285: if((ficresf=fopen(fileresf,"w"))==NULL) {
9286: printf("Problem with forecast resultfile: %s\n", fileresf);
9287: fprintf(ficlog,"Problem with forecast resultfile: %s\n", fileresf);
9288: }
9289: printf("\nComputing forecasting: result on file '%s', please wait... \n", fileresf);
9290: fprintf(ficlog,"\nComputing forecasting: result on file '%s', please wait... \n", fileresf);
9291:
9292: if (cptcoveff==0) ncodemax[cptcoveff]=1;
9293:
9294:
9295: stepsize=(int) (stepm+YEARM-1)/YEARM;
9296: if (stepm<=12) stepsize=1;
9297: if(estepm < stepm){
9298: printf ("Problem %d lower than %d\n",estepm, stepm);
9299: }
9300: else{
9301: hstepm=estepm;
9302: }
9303: if(estepm > stepm){ /* Yes every two year */
9304: stepsize=2;
9305: }
9306: hstepm=hstepm/stepm;
9307:
9308:
9309: /* yp1=modf(dateintmean,&yp);/\* extracts integral of datemean in yp and */
9310: /* fractional in yp1 *\/ */
9311: /* aintmean=yp; */
9312: /* yp2=modf((yp1*12),&yp); */
9313: /* mintmean=yp; */
9314: /* yp1=modf((yp2*30.5),&yp); */
9315: /* jintmean=yp; */
9316: /* if(jintmean==0) jintmean=1; */
9317: /* if(mintmean==0) mintmean=1; */
9318:
9319:
9320: /* date2dmy(dateintmean,&jintmean,&mintmean,&aintmean); */
9321: /* date2dmy(dateprojd,&jprojd, &mprojd, &anprojd); */
9322: /* date2dmy(dateprojf,&jprojf, &mprojf, &anprojf); */
9323: i1=pow(2,cptcoveff);
9324: if (cptcovn < 1){i1=1;}
9325:
9326: fprintf(ficresf,"# Mean day of interviews %.lf/%.lf/%.lf (%.2f) between %.2f and %.2f \n",jintmean,mintmean,aintmean,dateintmean,dateprev1,dateprev2);
9327:
9328: fprintf(ficresf,"#****** Routine prevforecast **\n");
9329:
9330: /* if (h==(int)(YEARM*yearp)){ */
9331: for(nres=1; nres <= nresult; nres++) /* For each resultline */
9332: for(k=1; k<=i1;k++){ /* We want to find the combination k corresponding to the values of the dummies given in this resut line (to be cleaned one day) */
9333: if(i1 != 1 && TKresult[nres]!= k)
9334: continue;
9335: if(invalidvarcomb[k]){
9336: printf("\nCombination (%d) projection ignored because no cases \n",k);
9337: continue;
9338: }
9339: fprintf(ficresf,"\n#****** hpijx=probability over h years, hp.jx is weighted by observed prev \n#");
9340: for(j=1;j<=cptcoveff;j++) {
9341: /* fprintf(ficresf," V%d (=) %d",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,Tvaraff[j])]); */
9342: fprintf(ficresf," V%d (=) %d",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]);
9343: }
9344: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
9345: fprintf(ficresf," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
9346: }
9347: fprintf(ficresf," yearproj age");
9348: for(j=1; j<=nlstate+ndeath;j++){
9349: for(i=1; i<=nlstate;i++)
9350: fprintf(ficresf," p%d%d",i,j);
9351: fprintf(ficresf," wp.%d",j);
9352: }
9353: for (yearp=0; yearp<=(anprojf-anprojd);yearp +=stepsize) {
9354: fprintf(ficresf,"\n");
9355: fprintf(ficresf,"\n# Forecasting at date %.lf/%.lf/%.lf ",jprojd,mprojd,anprojd+yearp);
9356: /* for (agec=fage; agec>=(ageminpar-1); agec--){ */
9357: for (agec=fage; agec>=(bage); agec--){
9358: nhstepm=(int) rint((agelim-agec)*YEARM/stepm);
9359: nhstepm = nhstepm/hstepm;
9360: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
9361: oldm=oldms;savm=savms;
9362: /* We compute pii at age agec over nhstepm);*/
9363: hpxij(p3mat,nhstepm,agec,hstepm,p,nlstate,stepm,oldm,savm, k,nres);
9364: /* Then we print p3mat for h corresponding to the right agec+h*stepms=yearp */
9365: for (h=0; h<=nhstepm; h++){
9366: if (h*hstepm/YEARM*stepm ==yearp) {
9367: break;
9368: }
9369: }
9370: fprintf(ficresf,"\n");
9371: for(j=1;j<=cptcoveff;j++)
9372: /* fprintf(ficresf,"%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,Tvaraff[j])]); /\* Tvaraff not correct *\/ */
9373: fprintf(ficresf,"%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); /* TnsdVar[Tvaraff] correct */
9374: fprintf(ficresf,"%.f %.f ",anprojd+yearp,agec+h*hstepm/YEARM*stepm);
9375:
9376: for(j=1; j<=nlstate+ndeath;j++) {
9377: ppij=0.;
9378: for(i=1; i<=nlstate;i++) {
9379: if (mobilav>=1)
9380: ppij=ppij+p3mat[i][j][h]*prev[(int)agec][i][k];
9381: else { /* even if mobilav==-1 we use mobaverage, probs may not sums to 1 */
9382: ppij=ppij+p3mat[i][j][h]*probs[(int)(agec)][i][k];
9383: }
9384: fprintf(ficresf," %.3f", p3mat[i][j][h]);
9385: } /* end i */
9386: fprintf(ficresf," %.3f", ppij);
9387: }/* end j */
9388: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
9389: } /* end agec */
9390: /* diffyear=(int) anproj1+yearp-ageminpar-1; */
9391: /*printf("Prevforecast %d+%d-%d=diffyear=%d\n",(int) anproj1, (int)yearp,(int)ageminpar,(int) anproj1-(int)ageminpar);*/
9392: } /* end yearp */
9393: } /* end k */
9394:
9395: fclose(ficresf);
9396: printf("End of Computing forecasting \n");
9397: fprintf(ficlog,"End of Computing forecasting\n");
9398:
9399: }
9400:
9401: /************** Back Forecasting ******************/
9402: /* void prevbackforecast(char fileres[], double ***prevacurrent, 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){ */
9403: void prevbackforecast(char fileres[], double ***prevacurrent, double dateintmean, double dateprojd, double dateprojf, double ageminpar, double agemax, double dateprev1, double dateprev2, int mobilav, double bage, double fage, int firstpass, int lastpass, double p[], int cptcoveff){
9404: /* back1, year, month, day of starting backprojection
9405: agemin, agemax range of age
9406: dateprev1 dateprev2 range of dates during which prevalence is computed
9407: anback2 year of end of backprojection (same day and month as back1).
9408: prevacurrent and prev are prevalences.
9409: */
9410: int yearp, stepsize, hstepm, nhstepm, j, k, cptcod, i, h, i1, k4, nres=0;
9411: double agec; /* generic age */
9412: double agelim, ppij, ppi, yp,yp1,yp2; /* ,jintmean,mintmean,aintmean;*/
9413: double *popeffectif,*popcount;
9414: double ***p3mat;
9415: /* double ***mobaverage; */
9416: char fileresfb[FILENAMELENGTH];
9417:
9418: agelim=AGEINF;
9419: /* Compute observed prevalence between dateprev1 and dateprev2 by counting the number of people
9420: in each health status at the date of interview (if between dateprev1 and dateprev2).
9421: We still use firstpass and lastpass as another selection.
9422: */
9423: /* freqsummary(fileres, agemin, agemax, s, agev, nlstate, imx,Tvaraff,nbcode, ncodemax,mint,anint,strstart,\ */
9424: /* firstpass, lastpass, stepm, weightopt, model); */
9425:
9426: /*Do we need to compute prevalence again?*/
9427:
9428: /* prevalence(probs, ageminpar, agemax, s, agev, nlstate, imx, Tvar, nbcode, ncodemax, mint, anint, dateprev1, dateprev2, firstpass, lastpass); */
9429:
9430: strcpy(fileresfb,"FB_");
9431: strcat(fileresfb,fileresu);
9432: if((ficresfb=fopen(fileresfb,"w"))==NULL) {
9433: printf("Problem with back forecast resultfile: %s\n", fileresfb);
9434: fprintf(ficlog,"Problem with back forecast resultfile: %s\n", fileresfb);
9435: }
9436: printf("\nComputing back forecasting: result on file '%s', please wait... \n", fileresfb);
9437: fprintf(ficlog,"\nComputing back forecasting: result on file '%s', please wait... \n", fileresfb);
9438:
9439: if (cptcoveff==0) ncodemax[cptcoveff]=1;
9440:
9441:
9442: stepsize=(int) (stepm+YEARM-1)/YEARM;
9443: if (stepm<=12) stepsize=1;
9444: if(estepm < stepm){
9445: printf ("Problem %d lower than %d\n",estepm, stepm);
9446: }
9447: else{
9448: hstepm=estepm;
9449: }
9450: if(estepm >= stepm){ /* Yes every two year */
9451: stepsize=2;
9452: }
9453:
9454: hstepm=hstepm/stepm;
9455: /* yp1=modf(dateintmean,&yp);/\* extracts integral of datemean in yp and */
9456: /* fractional in yp1 *\/ */
9457: /* aintmean=yp; */
9458: /* yp2=modf((yp1*12),&yp); */
9459: /* mintmean=yp; */
9460: /* yp1=modf((yp2*30.5),&yp); */
9461: /* jintmean=yp; */
9462: /* if(jintmean==0) jintmean=1; */
9463: /* if(mintmean==0) jintmean=1; */
9464:
9465: i1=pow(2,cptcoveff);
9466: if (cptcovn < 1){i1=1;}
9467:
9468: fprintf(ficresfb,"# Mean day of interviews %.lf/%.lf/%.lf (%.2f) between %.2f and %.2f \n",jintmean,mintmean,aintmean,dateintmean,dateprev1,dateprev2);
9469: printf("# Mean day of interviews %.lf/%.lf/%.lf (%.2f) between %.2f and %.2f \n",jintmean,mintmean,aintmean,dateintmean,dateprev1,dateprev2);
9470:
9471: fprintf(ficresfb,"#****** Routine prevbackforecast **\n");
9472:
9473: for(nres=1; nres <= nresult; nres++) /* For each resultline */
9474: for(k=1; k<=i1;k++){
9475: if(i1 != 1 && TKresult[nres]!= k)
9476: continue;
9477: if(invalidvarcomb[k]){
9478: printf("\nCombination (%d) projection ignored because no cases \n",k);
9479: continue;
9480: }
9481: fprintf(ficresfb,"\n#****** hbijx=probability over h years, hb.jx is weighted by observed prev \n#");
9482: for(j=1;j<=cptcoveff;j++) {
9483: fprintf(ficresfb," V%d (=) %d",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]);
9484: }
9485: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
9486: fprintf(ficresf," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
9487: }
9488: fprintf(ficresfb," yearbproj age");
9489: for(j=1; j<=nlstate+ndeath;j++){
9490: for(i=1; i<=nlstate;i++)
9491: fprintf(ficresfb," b%d%d",i,j);
9492: fprintf(ficresfb," b.%d",j);
9493: }
9494: for (yearp=0; yearp>=(anbackf-anbackd);yearp -=stepsize) {
9495: /* for (yearp=0; yearp<=(anproj2-anproj1);yearp +=stepsize) { */
9496: fprintf(ficresfb,"\n");
9497: fprintf(ficresfb,"\n# Back Forecasting at date %.lf/%.lf/%.lf ",jbackd,mbackd,anbackd+yearp);
9498: /* printf("\n# Back Forecasting at date %.lf/%.lf/%.lf ",jback1,mback1,anback1+yearp); */
9499: /* for (agec=bage; agec<=agemax-1; agec++){ /\* testing *\/ */
9500: for (agec=bage; agec<=fage; agec++){ /* testing */
9501: /* We compute bij at age agec over nhstepm, nhstepm decreases when agec increases because of agemax;*/
9502: nhstepm=(int) (agec-agelim) *YEARM/stepm;/* nhstepm=(int) rint((agec-agelim)*YEARM/stepm);*/
9503: nhstepm = nhstepm/hstepm;
9504: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
9505: oldm=oldms;savm=savms;
9506: /* computes hbxij at age agec over 1 to nhstepm */
9507: /* printf("####prevbackforecast debug agec=%.2f nhstepm=%d\n",agec, nhstepm);fflush(stdout); */
9508: hbxij(p3mat,nhstepm,agec,hstepm,p,prevacurrent,nlstate,stepm, k, nres);
9509: /* hpxij(p3mat,nhstepm,agec,hstepm,p, nlstate,stepm,oldm,savm, k,nres); */
9510: /* Then we print p3mat for h corresponding to the right agec+h*stepms=yearp */
9511: /* printf(" agec=%.2f\n",agec);fflush(stdout); */
9512: for (h=0; h<=nhstepm; h++){
9513: if (h*hstepm/YEARM*stepm ==-yearp) {
9514: break;
9515: }
9516: }
9517: fprintf(ficresfb,"\n");
9518: for(j=1;j<=cptcoveff;j++)
9519: fprintf(ficresfb,"%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]);
9520: fprintf(ficresfb,"%.f %.f ",anbackd+yearp,agec-h*hstepm/YEARM*stepm);
9521: for(i=1; i<=nlstate+ndeath;i++) {
9522: ppij=0.;ppi=0.;
9523: for(j=1; j<=nlstate;j++) {
9524: /* if (mobilav==1) */
9525: ppij=ppij+p3mat[i][j][h]*prevacurrent[(int)agec][j][k];
9526: ppi=ppi+prevacurrent[(int)agec][j][k];
9527: /* ppij=ppij+p3mat[i][j][h]*mobaverage[(int)agec][j][k]; */
9528: /* ppi=ppi+mobaverage[(int)agec][j][k]; */
9529: /* else { */
9530: /* ppij=ppij+p3mat[i][j][h]*probs[(int)(agec)][i][k]; */
9531: /* } */
9532: fprintf(ficresfb," %.3f", p3mat[i][j][h]);
9533: } /* end j */
9534: if(ppi <0.99){
9535: printf("Error in prevbackforecast, prevalence doesn't sum to 1 for state %d: %3f\n",i, ppi);
9536: fprintf(ficlog,"Error in prevbackforecast, prevalence doesn't sum to 1 for state %d: %3f\n",i, ppi);
9537: }
9538: fprintf(ficresfb," %.3f", ppij);
9539: }/* end j */
9540: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
9541: } /* end agec */
9542: } /* end yearp */
9543: } /* end k */
9544:
9545: /* if (mobilav!=0) free_ma3x(mobaverage,1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
9546:
9547: fclose(ficresfb);
9548: printf("End of Computing Back forecasting \n");
9549: fprintf(ficlog,"End of Computing Back forecasting\n");
9550:
9551: }
9552:
9553: /* Variance of prevalence limit: varprlim */
9554: void varprlim(char fileresu[], int nresult, double ***prevacurrent, int mobilavproj, double bage, double fage, double **prlim, int *ncvyearp, double ftolpl, double p[], double **matcov, double *delti, int stepm, int cptcoveff){
9555: /*------- Variance of forward period (stable) prevalence------*/
9556:
9557: char fileresvpl[FILENAMELENGTH];
9558: FILE *ficresvpl;
9559: double **oldm, **savm;
9560: double **varpl; /* Variances of prevalence limits by age */
9561: int i1, k, nres, j ;
9562:
9563: strcpy(fileresvpl,"VPL_");
9564: strcat(fileresvpl,fileresu);
9565: if((ficresvpl=fopen(fileresvpl,"w"))==NULL) {
9566: printf("Problem with variance of forward period (stable) prevalence resultfile: %s\n", fileresvpl);
9567: exit(0);
9568: }
9569: printf("Computing Variance-covariance of forward period (stable) prevalence: file '%s' ...", fileresvpl);fflush(stdout);
9570: fprintf(ficlog, "Computing Variance-covariance of forward period (stable) prevalence: file '%s' ...", fileresvpl);fflush(ficlog);
9571:
9572: /*for(cptcov=1,k=0;cptcov<=i1;cptcov++){
9573: for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){*/
9574:
9575: i1=pow(2,cptcoveff);
9576: if (cptcovn < 1){i1=1;}
9577:
9578: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
9579: k=TKresult[nres];
9580: if(TKresult[nres]==0) k=1; /* To be checked for noresult */
9581: /* for(k=1; k<=i1;k++){ /\* We find the combination equivalent to result line values of dummies *\/ */
9582: if(i1 != 1 && TKresult[nres]!= k)
9583: continue;
9584: fprintf(ficresvpl,"\n#****** ");
9585: printf("\n#****** ");
9586: fprintf(ficlog,"\n#****** ");
9587: for(j=1;j<=cptcovs;j++) {
9588: fprintf(ficresvpl,"V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
9589: fprintf(ficlog,"V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
9590: printf("V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
9591: /* fprintf(ficlog,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
9592: /* printf("V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
9593: }
9594: /* for (j=1; j<= nsq; j++){ /\* For each selected (single) quantitative value *\/ */
9595: /* printf(" V%d=%f ",Tvqresult[nres][j],Tqresult[nres][resultmodel[nres][j]]); */
9596: /* fprintf(ficresvpl," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][resultmodel[nres][j]]); */
9597: /* fprintf(ficlog," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][resultmodel[nres][j]]); */
9598: /* } */
9599: fprintf(ficresvpl,"******\n");
9600: printf("******\n");
9601: fprintf(ficlog,"******\n");
9602:
9603: varpl=matrix(1,nlstate,(int) bage, (int) fage);
9604: oldm=oldms;savm=savms;
9605: varprevlim(fileresvpl, ficresvpl, varpl, matcov, p, delti, nlstate, stepm, (int) bage, (int) fage, oldm, savm, prlim, ftolpl, ncvyearp, k, strstart, nres);
9606: free_matrix(varpl,1,nlstate,(int) bage, (int)fage);
9607: /*}*/
9608: }
9609:
9610: fclose(ficresvpl);
9611: printf("done variance-covariance of forward period prevalence\n");fflush(stdout);
9612: fprintf(ficlog,"done variance-covariance of forward period prevalence\n");fflush(ficlog);
9613:
9614: }
9615: /* Variance of back prevalence: varbprlim */
9616: void varbprlim(char fileresu[], int nresult, double ***prevacurrent, int mobilavproj, double bage, double fage, double **bprlim, int *ncvyearp, double ftolpl, double p[], double **matcov, double *delti, int stepm, int cptcoveff){
9617: /*------- Variance of back (stable) prevalence------*/
9618:
9619: char fileresvbl[FILENAMELENGTH];
9620: FILE *ficresvbl;
9621:
9622: double **oldm, **savm;
9623: double **varbpl; /* Variances of back prevalence limits by age */
9624: int i1, k, nres, j ;
9625:
9626: strcpy(fileresvbl,"VBL_");
9627: strcat(fileresvbl,fileresu);
9628: if((ficresvbl=fopen(fileresvbl,"w"))==NULL) {
9629: printf("Problem with variance of back (stable) prevalence resultfile: %s\n", fileresvbl);
9630: exit(0);
9631: }
9632: printf("Computing Variance-covariance of back (stable) prevalence: file '%s' ...", fileresvbl);fflush(stdout);
9633: fprintf(ficlog, "Computing Variance-covariance of back (stable) prevalence: file '%s' ...", fileresvbl);fflush(ficlog);
9634:
9635:
9636: i1=pow(2,cptcoveff);
9637: if (cptcovn < 1){i1=1;}
9638:
9639: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
9640: k=TKresult[nres];
9641: if(TKresult[nres]==0) k=1; /* To be checked for noresult */
9642: /* for(k=1; k<=i1;k++){ */
9643: /* if(i1 != 1 && TKresult[nres]!= k) */
9644: /* continue; */
9645: fprintf(ficresvbl,"\n#****** ");
9646: printf("\n#****** ");
9647: fprintf(ficlog,"\n#****** ");
9648: for (j=1; j<= cptcovs; j++){ /* For each selected (single) quantitative value */
9649: printf(" V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][resultmodel[nres][j]]);
9650: fprintf(ficresvbl," V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][resultmodel[nres][j]]);
9651: fprintf(ficlog," V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][resultmodel[nres][j]]);
9652: /* for(j=1;j<=cptcoveff;j++) { */
9653: /* fprintf(ficresvbl,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
9654: /* fprintf(ficlog,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
9655: /* printf("V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
9656: /* } */
9657: /* for (j=1; j<= nsq; j++){ /\* For each selected (single) quantitative value *\/ */
9658: /* printf(" V%d=%f ",Tvqresult[nres][j],Tqresult[nres][resultmodel[nres][j]]); */
9659: /* fprintf(ficresvbl," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][resultmodel[nres][j]]); */
9660: /* fprintf(ficlog," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][resultmodel[nres][j]]); */
9661: }
9662: fprintf(ficresvbl,"******\n");
9663: printf("******\n");
9664: fprintf(ficlog,"******\n");
9665:
9666: varbpl=matrix(1,nlstate,(int) bage, (int) fage);
9667: oldm=oldms;savm=savms;
9668:
9669: varbrevlim(fileresvbl, ficresvbl, varbpl, matcov, p, delti, nlstate, stepm, (int) bage, (int) fage, oldm, savm, bprlim, ftolpl, mobilavproj, ncvyearp, k, strstart, nres);
9670: free_matrix(varbpl,1,nlstate,(int) bage, (int)fage);
9671: /*}*/
9672: }
9673:
9674: fclose(ficresvbl);
9675: printf("done variance-covariance of back prevalence\n");fflush(stdout);
9676: fprintf(ficlog,"done variance-covariance of back prevalence\n");fflush(ficlog);
9677:
9678: } /* End of varbprlim */
9679:
9680: /************** Forecasting *****not tested NB*************/
9681: /* 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){ */
9682:
9683: /* int cpt, stepsize, hstepm, nhstepm, j,k,c, cptcod, i,h; */
9684: /* int *popage; */
9685: /* double calagedatem, agelim, kk1, kk2; */
9686: /* double *popeffectif,*popcount; */
9687: /* double ***p3mat,***tabpop,***tabpopprev; */
9688: /* /\* double ***mobaverage; *\/ */
9689: /* char filerespop[FILENAMELENGTH]; */
9690:
9691: /* tabpop= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
9692: /* tabpopprev= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
9693: /* agelim=AGESUP; */
9694: /* calagedatem=(anpyram+mpyram/12.+jpyram/365.-dateintmean)*YEARM; */
9695:
9696: /* prevalence(probs, ageminpar, agemax, s, agev, nlstate, imx, Tvar, nbcode, ncodemax, mint, anint, dateprev1, dateprev2, firstpass, lastpass); */
9697:
9698:
9699: /* strcpy(filerespop,"POP_"); */
9700: /* strcat(filerespop,fileresu); */
9701: /* if((ficrespop=fopen(filerespop,"w"))==NULL) { */
9702: /* printf("Problem with forecast resultfile: %s\n", filerespop); */
9703: /* fprintf(ficlog,"Problem with forecast resultfile: %s\n", filerespop); */
9704: /* } */
9705: /* printf("Computing forecasting: result on file '%s' \n", filerespop); */
9706: /* fprintf(ficlog,"Computing forecasting: result on file '%s' \n", filerespop); */
9707:
9708: /* if (cptcoveff==0) ncodemax[cptcoveff]=1; */
9709:
9710: /* /\* if (mobilav!=0) { *\/ */
9711: /* /\* mobaverage= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX); *\/ */
9712: /* /\* if (movingaverage(probs, ageminpar, fage, mobaverage,mobilav)!=0){ *\/ */
9713: /* /\* fprintf(ficlog," Error in movingaverage mobilav=%d\n",mobilav); *\/ */
9714: /* /\* printf(" Error in movingaverage mobilav=%d\n",mobilav); *\/ */
9715: /* /\* } *\/ */
9716: /* /\* } *\/ */
9717:
9718: /* stepsize=(int) (stepm+YEARM-1)/YEARM; */
9719: /* if (stepm<=12) stepsize=1; */
9720:
9721: /* agelim=AGESUP; */
9722:
9723: /* hstepm=1; */
9724: /* hstepm=hstepm/stepm; */
9725:
9726: /* if (popforecast==1) { */
9727: /* if((ficpop=fopen(popfile,"r"))==NULL) { */
9728: /* printf("Problem with population file : %s\n",popfile);exit(0); */
9729: /* fprintf(ficlog,"Problem with population file : %s\n",popfile);exit(0); */
9730: /* } */
9731: /* popage=ivector(0,AGESUP); */
9732: /* popeffectif=vector(0,AGESUP); */
9733: /* popcount=vector(0,AGESUP); */
9734:
9735: /* i=1; */
9736: /* while ((c=fscanf(ficpop,"%d %lf\n",&popage[i],&popcount[i])) != EOF) i=i+1; */
9737:
9738: /* imx=i; */
9739: /* for (i=1; i<imx;i++) popeffectif[popage[i]]=popcount[i]; */
9740: /* } */
9741:
9742: /* for(cptcov=1,k=0;cptcov<=i2;cptcov++){ */
9743: /* for(cptcod=1;cptcod<=ncodemax[cptcoveff];cptcod++){ */
9744: /* k=k+1; */
9745: /* fprintf(ficrespop,"\n#******"); */
9746: /* for(j=1;j<=cptcoveff;j++) { */
9747: /* fprintf(ficrespop," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]); */
9748: /* } */
9749: /* fprintf(ficrespop,"******\n"); */
9750: /* fprintf(ficrespop,"# Age"); */
9751: /* for(j=1; j<=nlstate+ndeath;j++) fprintf(ficrespop," P.%d",j); */
9752: /* if (popforecast==1) fprintf(ficrespop," [Population]"); */
9753:
9754: /* for (cpt=0; cpt<=0;cpt++) { */
9755: /* fprintf(ficrespop,"\n\n# Forecasting at date %.lf/%.lf/%.lf ",jpyram,mpyram,anpyram+cpt); */
9756:
9757: /* for (agedeb=(fage-((int)calagedatem %12/12.)); agedeb>=(ageminpar-((int)calagedatem %12)/12.); agedeb--){ */
9758: /* nhstepm=(int) rint((agelim-agedeb)*YEARM/stepm); */
9759: /* nhstepm = nhstepm/hstepm; */
9760:
9761: /* p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); */
9762: /* oldm=oldms;savm=savms; */
9763: /* hpxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k); */
9764:
9765: /* for (h=0; h<=nhstepm; h++){ */
9766: /* if (h==(int) (calagedatem+YEARM*cpt)) { */
9767: /* fprintf(ficrespop,"\n %3.f ",agedeb+h*hstepm/YEARM*stepm); */
9768: /* } */
9769: /* for(j=1; j<=nlstate+ndeath;j++) { */
9770: /* kk1=0.;kk2=0; */
9771: /* for(i=1; i<=nlstate;i++) { */
9772: /* if (mobilav==1) */
9773: /* kk1=kk1+p3mat[i][j][h]*mobaverage[(int)agedeb+1][i][cptcod]; */
9774: /* else { */
9775: /* kk1=kk1+p3mat[i][j][h]*probs[(int)(agedeb+1)][i][cptcod]; */
9776: /* } */
9777: /* } */
9778: /* if (h==(int)(calagedatem+12*cpt)){ */
9779: /* tabpop[(int)(agedeb)][j][cptcod]=kk1; */
9780: /* /\*fprintf(ficrespop," %.3f", kk1); */
9781: /* if (popforecast==1) fprintf(ficrespop," [%.f]", kk1*popeffectif[(int)agedeb+1]);*\/ */
9782: /* } */
9783: /* } */
9784: /* for(i=1; i<=nlstate;i++){ */
9785: /* kk1=0.; */
9786: /* for(j=1; j<=nlstate;j++){ */
9787: /* kk1= kk1+tabpop[(int)(agedeb)][j][cptcod]; */
9788: /* } */
9789: /* tabpopprev[(int)(agedeb)][i][cptcod]=tabpop[(int)(agedeb)][i][cptcod]/kk1*popeffectif[(int)(agedeb+(calagedatem+12*cpt)*hstepm/YEARM*stepm-1)]; */
9790: /* } */
9791:
9792: /* if (h==(int)(calagedatem+12*cpt)) */
9793: /* for(j=1; j<=nlstate;j++) */
9794: /* fprintf(ficrespop," %15.2f",tabpopprev[(int)(agedeb+1)][j][cptcod]); */
9795: /* } */
9796: /* free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); */
9797: /* } */
9798: /* } */
9799:
9800: /* /\******\/ */
9801:
9802: /* for (cpt=1; cpt<=(anpyram1-anpyram);cpt++) { */
9803: /* fprintf(ficrespop,"\n\n# Forecasting at date %.lf/%.lf/%.lf ",jpyram,mpyram,anpyram+cpt); */
9804: /* for (agedeb=(fage-((int)calagedatem %12/12.)); agedeb>=(ageminpar-((int)calagedatem %12)/12.); agedeb--){ */
9805: /* nhstepm=(int) rint((agelim-agedeb)*YEARM/stepm); */
9806: /* nhstepm = nhstepm/hstepm; */
9807:
9808: /* p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); */
9809: /* oldm=oldms;savm=savms; */
9810: /* hpxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k); */
9811: /* for (h=0; h<=nhstepm; h++){ */
9812: /* if (h==(int) (calagedatem+YEARM*cpt)) { */
9813: /* fprintf(ficresf,"\n %3.f ",agedeb+h*hstepm/YEARM*stepm); */
9814: /* } */
9815: /* for(j=1; j<=nlstate+ndeath;j++) { */
9816: /* kk1=0.;kk2=0; */
9817: /* for(i=1; i<=nlstate;i++) { */
9818: /* kk1=kk1+p3mat[i][j][h]*tabpopprev[(int)agedeb+1][i][cptcod]; */
9819: /* } */
9820: /* if (h==(int)(calagedatem+12*cpt)) fprintf(ficresf," %15.2f", kk1); */
9821: /* } */
9822: /* } */
9823: /* free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); */
9824: /* } */
9825: /* } */
9826: /* } */
9827: /* } */
9828:
9829: /* /\* if (mobilav!=0) free_ma3x(mobaverage,1, AGESUP,1,NCOVMAX, 1,NCOVMAX); *\/ */
9830:
9831: /* if (popforecast==1) { */
9832: /* free_ivector(popage,0,AGESUP); */
9833: /* free_vector(popeffectif,0,AGESUP); */
9834: /* free_vector(popcount,0,AGESUP); */
9835: /* } */
9836: /* free_ma3x(tabpop,1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
9837: /* free_ma3x(tabpopprev,1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
9838: /* fclose(ficrespop); */
9839: /* } /\* End of popforecast *\/ */
9840:
9841: int fileappend(FILE *fichier, char *optionfich)
9842: {
9843: if((fichier=fopen(optionfich,"a"))==NULL) {
9844: printf("Problem with file: %s\n", optionfich);
9845: fprintf(ficlog,"Problem with file: %s\n", optionfich);
9846: return (0);
9847: }
9848: fflush(fichier);
9849: return (1);
9850: }
9851:
9852:
9853: /**************** function prwizard **********************/
9854: void prwizard(int ncovmodel, int nlstate, int ndeath, char model[], FILE *ficparo)
9855: {
9856:
9857: /* Wizard to print covariance matrix template */
9858:
9859: char ca[32], cb[32];
9860: int i,j, k, li, lj, lk, ll, jj, npar, itimes;
9861: int numlinepar;
9862:
9863: printf("# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n");
9864: fprintf(ficparo,"# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n");
9865: for(i=1; i <=nlstate; i++){
9866: jj=0;
9867: for(j=1; j <=nlstate+ndeath; j++){
9868: if(j==i) continue;
9869: jj++;
9870: /*ca[0]= k+'a'-1;ca[1]='\0';*/
9871: printf("%1d%1d",i,j);
9872: fprintf(ficparo,"%1d%1d",i,j);
9873: for(k=1; k<=ncovmodel;k++){
9874: /* printf(" %lf",param[i][j][k]); */
9875: /* fprintf(ficparo," %lf",param[i][j][k]); */
9876: printf(" 0.");
9877: fprintf(ficparo," 0.");
9878: }
9879: printf("\n");
9880: fprintf(ficparo,"\n");
9881: }
9882: }
9883: printf("# Scales (for hessian or gradient estimation)\n");
9884: fprintf(ficparo,"# Scales (for hessian or gradient estimation)\n");
9885: npar= (nlstate+ndeath-1)*nlstate*ncovmodel; /* Number of parameters*/
9886: for(i=1; i <=nlstate; i++){
9887: jj=0;
9888: for(j=1; j <=nlstate+ndeath; j++){
9889: if(j==i) continue;
9890: jj++;
9891: fprintf(ficparo,"%1d%1d",i,j);
9892: printf("%1d%1d",i,j);
9893: fflush(stdout);
9894: for(k=1; k<=ncovmodel;k++){
9895: /* printf(" %le",delti3[i][j][k]); */
9896: /* fprintf(ficparo," %le",delti3[i][j][k]); */
9897: printf(" 0.");
9898: fprintf(ficparo," 0.");
9899: }
9900: numlinepar++;
9901: printf("\n");
9902: fprintf(ficparo,"\n");
9903: }
9904: }
9905: printf("# Covariance matrix\n");
9906: /* # 121 Var(a12)\n\ */
9907: /* # 122 Cov(b12,a12) Var(b12)\n\ */
9908: /* # 131 Cov(a13,a12) Cov(a13,b12, Var(a13)\n\ */
9909: /* # 132 Cov(b13,a12) Cov(b13,b12, Cov(b13,a13) Var(b13)\n\ */
9910: /* # 212 Cov(a21,a12) Cov(a21,b12, Cov(a21,a13) Cov(a21,b13) Var(a21)\n\ */
9911: /* # 212 Cov(b21,a12) Cov(b21,b12, Cov(b21,a13) Cov(b21,b13) Cov(b21,a21) Var(b21)\n\ */
9912: /* # 232 Cov(a23,a12) Cov(a23,b12, Cov(a23,a13) Cov(a23,b13) Cov(a23,a21) Cov(a23,b21) Var(a23)\n\ */
9913: /* # 232 Cov(b23,a12) Cov(b23,b12) ... Var (b23)\n" */
9914: fflush(stdout);
9915: fprintf(ficparo,"# Covariance matrix\n");
9916: /* # 121 Var(a12)\n\ */
9917: /* # 122 Cov(b12,a12) Var(b12)\n\ */
9918: /* # ...\n\ */
9919: /* # 232 Cov(b23,a12) Cov(b23,b12) ... Var (b23)\n" */
9920:
9921: for(itimes=1;itimes<=2;itimes++){
9922: jj=0;
9923: for(i=1; i <=nlstate; i++){
9924: for(j=1; j <=nlstate+ndeath; j++){
9925: if(j==i) continue;
9926: for(k=1; k<=ncovmodel;k++){
9927: jj++;
9928: ca[0]= k+'a'-1;ca[1]='\0';
9929: if(itimes==1){
9930: printf("#%1d%1d%d",i,j,k);
9931: fprintf(ficparo,"#%1d%1d%d",i,j,k);
9932: }else{
9933: printf("%1d%1d%d",i,j,k);
9934: fprintf(ficparo,"%1d%1d%d",i,j,k);
9935: /* printf(" %.5le",matcov[i][j]); */
9936: }
9937: ll=0;
9938: for(li=1;li <=nlstate; li++){
9939: for(lj=1;lj <=nlstate+ndeath; lj++){
9940: if(lj==li) continue;
9941: for(lk=1;lk<=ncovmodel;lk++){
9942: ll++;
9943: if(ll<=jj){
9944: cb[0]= lk +'a'-1;cb[1]='\0';
9945: if(ll<jj){
9946: if(itimes==1){
9947: printf(" Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj);
9948: fprintf(ficparo," Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj);
9949: }else{
9950: printf(" 0.");
9951: fprintf(ficparo," 0.");
9952: }
9953: }else{
9954: if(itimes==1){
9955: printf(" Var(%s%1d%1d)",ca,i,j);
9956: fprintf(ficparo," Var(%s%1d%1d)",ca,i,j);
9957: }else{
9958: printf(" 0.");
9959: fprintf(ficparo," 0.");
9960: }
9961: }
9962: }
9963: } /* end lk */
9964: } /* end lj */
9965: } /* end li */
9966: printf("\n");
9967: fprintf(ficparo,"\n");
9968: numlinepar++;
9969: } /* end k*/
9970: } /*end j */
9971: } /* end i */
9972: } /* end itimes */
9973:
9974: } /* end of prwizard */
9975: /******************* Gompertz Likelihood ******************************/
9976: double gompertz(double x[])
9977: {
9978: double A=0.0,B=0.,L=0.0,sump=0.,num=0.;
9979: int i,n=0; /* n is the size of the sample */
9980:
9981: for (i=1;i<=imx ; i++) {
9982: sump=sump+weight[i];
9983: /* sump=sump+1;*/
9984: num=num+1;
9985: }
9986: L=0.0;
9987: /* agegomp=AGEGOMP; */
9988: /* for (i=0; i<=imx; i++)
9989: 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]);*/
9990:
9991: for (i=1;i<=imx ; i++) {
9992: /* mu(a)=mu(agecomp)*exp(teta*(age-agegomp))
9993: mu(a)=x[1]*exp(x[2]*(age-agegomp)); x[1] and x[2] are per year.
9994: * L= Product mu(agedeces)exp(-\int_ageexam^agedc mu(u) du ) for a death between agedc (in month)
9995: * and agedc +1 month, cens[i]=0: log(x[1]/YEARM)
9996: * +
9997: * exp(-\int_ageexam^agecens mu(u) du ) when censored, cens[i]=1
9998: */
9999: if (wav[i] > 1 || agedc[i] < AGESUP) {
10000: if (cens[i] == 1){
10001: A=-x[1]/(x[2])*(exp(x[2]*(agecens[i]-agegomp))-exp(x[2]*(ageexmed[i]-agegomp)));
10002: } else if (cens[i] == 0){
10003: A=-x[1]/(x[2])*(exp(x[2]*(agedc[i]-agegomp))-exp(x[2]*(ageexmed[i]-agegomp)))
10004: +log(x[1]/YEARM) +x[2]*(agedc[i]-agegomp)+log(YEARM);
10005: } else
10006: printf("Gompertz cens[%d] neither 1 nor 0\n",i);
10007: /*if (wav[i] > 1 && agecens[i] > 15) {*/ /* ??? */
10008: L=L+A*weight[i];
10009: /* 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]);*/
10010: }
10011: }
10012:
10013: /*printf("x1=%2.9f x2=%2.9f x3=%2.9f L=%f\n",x[1],x[2],x[3],L);*/
10014:
10015: return -2*L*num/sump;
10016: }
10017:
10018: #ifdef GSL
10019: /******************* Gompertz_f Likelihood ******************************/
10020: double gompertz_f(const gsl_vector *v, void *params)
10021: {
10022: double A=0.,B=0.,LL=0.0,sump=0.,num=0.;
10023: double *x= (double *) v->data;
10024: int i,n=0; /* n is the size of the sample */
10025:
10026: for (i=0;i<=imx-1 ; i++) {
10027: sump=sump+weight[i];
10028: /* sump=sump+1;*/
10029: num=num+1;
10030: }
10031:
10032:
10033: /* for (i=0; i<=imx; i++)
10034: 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]);*/
10035: printf("x[0]=%lf x[1]=%lf\n",x[0],x[1]);
10036: for (i=1;i<=imx ; i++)
10037: {
10038: if (cens[i] == 1 && wav[i]>1)
10039: A=-x[0]/(x[1])*(exp(x[1]*(agecens[i]-agegomp))-exp(x[1]*(ageexmed[i]-agegomp)));
10040:
10041: if (cens[i] == 0 && wav[i]>1)
10042: A=-x[0]/(x[1])*(exp(x[1]*(agedc[i]-agegomp))-exp(x[1]*(ageexmed[i]-agegomp)))
10043: +log(x[0]/YEARM)+x[1]*(agedc[i]-agegomp)+log(YEARM);
10044:
10045: /*if (wav[i] > 1 && agecens[i] > 15) {*/ /* ??? */
10046: if (wav[i] > 1 ) { /* ??? */
10047: LL=LL+A*weight[i];
10048: /* 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]);*/
10049: }
10050: }
10051:
10052: /*printf("x1=%2.9f x2=%2.9f x3=%2.9f L=%f\n",x[1],x[2],x[3],L);*/
10053: printf("x[0]=%lf x[1]=%lf -2*LL*num/sump=%lf\n",x[0],x[1],-2*LL*num/sump);
10054:
10055: return -2*LL*num/sump;
10056: }
10057: #endif
10058:
10059: /******************* Printing html file ***********/
10060: void printinghtmlmort(char fileresu[], char title[], char datafile[], int firstpass, \
10061: int lastpass, int stepm, int weightopt, char model[],\
10062: int imx, double p[],double **matcov,double agemortsup){
10063: int i,k;
10064:
10065: fprintf(fichtm,"<ul><li><h4>Result files </h4>\n Force of mortality. Parameters of the Gompertz fit (with confidence interval in brackets):<br>");
10066: fprintf(fichtm," mu(age) =%lf*exp(%lf*(age-%d)) per year<br><br>",p[1],p[2],agegomp);
10067: for (i=1;i<=2;i++)
10068: 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]));
10069: fprintf(fichtm,"<br><br><img src=\"graphmort.svg\">");
10070: fprintf(fichtm,"</ul>");
10071:
10072: fprintf(fichtm,"<ul><li><h4>Life table</h4>\n <br>");
10073:
10074: 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>");
10075:
10076: for (k=agegomp;k<(agemortsup-2);k++)
10077: 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]);
10078:
10079:
10080: fflush(fichtm);
10081: }
10082:
10083: /******************* Gnuplot file **************/
10084: void printinggnuplotmort(char fileresu[], char optionfilefiname[], double ageminpar, double agemaxpar, double fage , char pathc[], double p[]){
10085:
10086: char dirfileres[132],optfileres[132];
10087:
10088: int ng;
10089:
10090:
10091: /*#ifdef windows */
10092: fprintf(ficgp,"cd \"%s\" \n",pathc);
10093: /*#endif */
10094:
10095:
10096: strcpy(dirfileres,optionfilefiname);
10097: strcpy(optfileres,"vpl");
10098: fprintf(ficgp,"set out \"graphmort.svg\"\n ");
10099: fprintf(ficgp,"set xlabel \"Age\"\n set ylabel \"Force of mortality (per year)\" \n ");
10100: fprintf(ficgp, "set ter svg size 640, 480\n set log y\n");
10101: /* fprintf(ficgp, "set size 0.65,0.65\n"); */
10102: fprintf(ficgp,"plot [%d:100] %lf*exp(%lf*(x-%d))",agegomp,p[1],p[2],agegomp);
10103:
10104: }
10105:
10106: int readdata(char datafile[], int firstobs, int lastobs, int *imax)
10107: {
10108:
10109: /*-------- data file ----------*/
10110: FILE *fic;
10111: char dummy[]=" ";
10112: int i=0, j=0, n=0, iv=0, v;
10113: int lstra;
10114: int linei, month, year,iout;
10115: int noffset=0; /* This is the offset if BOM data file */
10116: char line[MAXLINE], linetmp[MAXLINE];
10117: char stra[MAXLINE], strb[MAXLINE];
10118: char *stratrunc;
10119:
10120: DummyV=ivector(1,NCOVMAX); /* 1 to 3 */
10121: FixedV=ivector(1,NCOVMAX); /* 1 to 3 */
10122: for(v=1;v<NCOVMAX;v++){
10123: DummyV[v]=0;
10124: FixedV[v]=0;
10125: }
10126:
10127: for(v=1; v <=ncovcol;v++){
10128: DummyV[v]=0;
10129: FixedV[v]=0;
10130: }
10131: for(v=ncovcol+1; v <=ncovcol+nqv;v++){
10132: DummyV[v]=1;
10133: FixedV[v]=0;
10134: }
10135: for(v=ncovcol+nqv+1; v <=ncovcol+nqv+ntv;v++){
10136: DummyV[v]=0;
10137: FixedV[v]=1;
10138: }
10139: for(v=ncovcol+nqv+ntv+1; v <=ncovcol+nqv+ntv+nqtv;v++){
10140: DummyV[v]=1;
10141: FixedV[v]=1;
10142: }
10143: for(v=1; v <=ncovcol+nqv+ntv+nqtv;v++){
10144: printf("Covariate type in the data: V%d, DummyV(V%d)=%d, FixedV(V%d)=%d\n",v,v,DummyV[v],v,FixedV[v]);
10145: fprintf(ficlog,"Covariate type in the data: V%d, DummyV(V%d)=%d, FixedV(V%d)=%d\n",v,v,DummyV[v],v,FixedV[v]);
10146: }
10147:
10148: if((fic=fopen(datafile,"r"))==NULL) {
10149: printf("Problem while opening datafile: %s with errno='%s'\n", datafile,strerror(errno));fflush(stdout);
10150: fprintf(ficlog,"Problem while opening datafile: %s with errno='%s'\n", datafile,strerror(errno));fflush(ficlog);return 1;
10151: }
10152:
10153: /* Is it a BOM UTF-8 Windows file? */
10154: /* First data line */
10155: linei=0;
10156: while(fgets(line, MAXLINE, fic)) {
10157: noffset=0;
10158: if( line[0] == (char)0xEF && line[1] == (char)0xBB) /* EF BB BF */
10159: {
10160: noffset=noffset+3;
10161: printf("# Data file '%s' is an UTF8 BOM file, please convert to UTF8 or ascii file and rerun.\n",datafile);fflush(stdout);
10162: fprintf(ficlog,"# Data file '%s' is an UTF8 BOM file, please convert to UTF8 or ascii file and rerun.\n",datafile);
10163: fflush(ficlog); return 1;
10164: }
10165: /* else if( line[0] == (char)0xFE && line[1] == (char)0xFF)*/
10166: else if( line[0] == (char)0xFF && line[1] == (char)0xFE)
10167: {
10168: noffset=noffset+2;
10169: printf("# Error Data file '%s' is a huge UTF16BE BOM file, please convert to UTF8 or ascii file (for example with dos2unix) and rerun.\n",datafile);fflush(stdout);
10170: fprintf(ficlog,"# Error Data file '%s' is a huge UTF16BE BOM file, please convert to UTF8 or ascii file (for example with dos2unix) and rerun.\n",datafile);
10171: fflush(ficlog); return 1;
10172: }
10173: else if( line[0] == 0 && line[1] == 0)
10174: {
10175: if( line[2] == (char)0xFE && line[3] == (char)0xFF){
10176: noffset=noffset+4;
10177: printf("# Error Data file '%s' is a huge UTF16BE BOM file, please convert to UTF8 or ascii file (for example with dos2unix) and rerun.\n",datafile);fflush(stdout);
10178: fprintf(ficlog,"# Error Data file '%s' is a huge UTF16BE BOM file, please convert to UTF8 or ascii file (for example with dos2unix) and rerun.\n",datafile);
10179: fflush(ficlog); return 1;
10180: }
10181: } else{
10182: ;/*printf(" Not a BOM file\n");*/
10183: }
10184: /* If line starts with a # it is a comment */
10185: if (line[noffset] == '#') {
10186: linei=linei+1;
10187: break;
10188: }else{
10189: break;
10190: }
10191: }
10192: fclose(fic);
10193: if((fic=fopen(datafile,"r"))==NULL) {
10194: printf("Problem while opening datafile: %s with errno='%s'\n", datafile,strerror(errno));fflush(stdout);
10195: fprintf(ficlog,"Problem while opening datafile: %s with errno='%s'\n", datafile,strerror(errno));fflush(ficlog);return 1;
10196: }
10197: /* Not a Bom file */
10198:
10199: i=1;
10200: while ((fgets(line, MAXLINE, fic) != NULL) &&((i >= firstobs) && (i <=lastobs))) {
10201: linei=linei+1;
10202: for(j=strlen(line); j>=0;j--){ /* Untabifies line */
10203: if(line[j] == '\t')
10204: line[j] = ' ';
10205: }
10206: for(j=strlen(line)-1; (line[j]==' ')||(line[j]==10)||(line[j]==13);j--){
10207: ;
10208: };
10209: line[j+1]=0; /* Trims blanks at end of line */
10210: if(line[0]=='#'){
10211: fprintf(ficlog,"Comment line\n%s\n",line);
10212: printf("Comment line\n%s\n",line);
10213: continue;
10214: }
10215: trimbb(linetmp,line); /* Trims multiple blanks in line */
10216: strcpy(line, linetmp);
10217:
10218: /* Loops on waves */
10219: for (j=maxwav;j>=1;j--){
10220: for (iv=nqtv;iv>=1;iv--){ /* Loop on time varying quantitative variables */
10221: cutv(stra, strb, line, ' ');
10222: if(strb[0]=='.') { /* Missing value */
10223: lval=-1;
10224: cotqvar[j][iv][i]=-1; /* 0.0/0.0 */
10225: cotvar[j][ntv+iv][i]=-1; /* For performance reasons */
10226: if(isalpha(strb[1])) { /* .m or .d Really Missing value */
10227: 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);
10228: 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);
10229: return 1;
10230: }
10231: }else{
10232: errno=0;
10233: /* what_kind_of_number(strb); */
10234: dval=strtod(strb,&endptr);
10235: /* if( strb[0]=='\0' || (*endptr != '\0')){ */
10236: /* if(strb != endptr && *endptr == '\0') */
10237: /* dval=dlval; */
10238: /* if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN)) */
10239: if( strb[0]=='\0' || (*endptr != '\0')){
10240: 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);
10241: 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);
10242: return 1;
10243: }
10244: cotqvar[j][iv][i]=dval;
10245: cotvar[j][ntv+iv][i]=dval;
10246: }
10247: strcpy(line,stra);
10248: }/* end loop ntqv */
10249:
10250: for (iv=ntv;iv>=1;iv--){ /* Loop on time varying dummies */
10251: cutv(stra, strb, line, ' ');
10252: if(strb[0]=='.') { /* Missing value */
10253: lval=-1;
10254: }else{
10255: errno=0;
10256: lval=strtol(strb,&endptr,10);
10257: /* if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN))*/
10258: if( strb[0]=='\0' || (*endptr != '\0')){
10259: 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);
10260: 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);
10261: return 1;
10262: }
10263: }
10264: if(lval <-1 || lval >1){
10265: printf("Error reading data around '%ld' at line number %d for individual %d, '%s'\n \
10266: Should be a value of %d(nth) covariate of wave %d (0 should be the value for the reference and 1\n \
10267: for the alternative. IMaCh does not build design variables automatically, do it yourself.\n \
10268: For example, for multinomial values like 1, 2 and 3,\n \
10269: build V1=0 V2=0 for the reference value (1),\n \
10270: V1=1 V2=0 for (2) \n \
10271: and V1=0 V2=1 for (3). V1=1 V2=1 should not exist and the corresponding\n \
10272: output of IMaCh is often meaningless.\n \
10273: Exiting.\n",lval,linei, i,line,iv,j);
10274: fprintf(ficlog,"Error reading data around '%ld' at line number %d for individual %d, '%s'\n \
10275: Should be a value of %d(nth) covariate of wave %d (0 should be the value for the reference and 1\n \
10276: for the alternative. IMaCh does not build design variables automatically, do it yourself.\n \
10277: For example, for multinomial values like 1, 2 and 3,\n \
10278: build V1=0 V2=0 for the reference value (1),\n \
10279: V1=1 V2=0 for (2) \n \
10280: and V1=0 V2=1 for (3). V1=1 V2=1 should not exist and the corresponding\n \
10281: output of IMaCh is often meaningless.\n \
10282: Exiting.\n",lval,linei, i,line,iv,j);fflush(ficlog);
10283: return 1;
10284: }
10285: cotvar[j][iv][i]=(double)(lval);
10286: strcpy(line,stra);
10287: }/* end loop ntv */
10288:
10289: /* Statuses at wave */
10290: cutv(stra, strb, line, ' ');
10291: if(strb[0]=='.') { /* Missing value */
10292: lval=-1;
10293: }else{
10294: errno=0;
10295: lval=strtol(strb,&endptr,10);
10296: /* if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN))*/
10297: if( strb[0]=='\0' || (*endptr != '\0')){
10298: 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);
10299: 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);
10300: return 1;
10301: }
10302: }
10303:
10304: s[j][i]=lval;
10305:
10306: /* Date of Interview */
10307: strcpy(line,stra);
10308: cutv(stra, strb,line,' ');
10309: if( (iout=sscanf(strb,"%d/%d",&month, &year)) != 0){
10310: }
10311: else if( (iout=sscanf(strb,"%s.",dummy)) != 0){
10312: month=99;
10313: year=9999;
10314: }else{
10315: 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);
10316: 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);
10317: return 1;
10318: }
10319: anint[j][i]= (double) year;
10320: mint[j][i]= (double)month;
10321: /* if( (int)anint[j][i]+ (int)(mint[j][i])/12. < (int) (moisnais[i]/12.+annais[i])){ */
10322: /* printf("Warning reading data around '%s' at line number %d for individual %d, '%s'\nThe date of interview (%2d/%4d) at wave %d occurred before the date of birth (%2d/%4d).\n",strb, linei,i, line, mint[j][i],anint[j][i], moisnais[i],annais[i]); */
10323: /* fprintf(ficlog,"Warning reading data around '%s' at line number %d for individual %d, '%s'\nThe date of interview (%2d/%4d) at wave %d occurred before the date of birth (%2d/%4d).\n",strb, linei,i, line, mint[j][i],anint[j][i], moisnais[i],annais[i]); */
10324: /* } */
10325: strcpy(line,stra);
10326: } /* End loop on waves */
10327:
10328: /* Date of death */
10329: cutv(stra, strb,line,' ');
10330: if( (iout=sscanf(strb,"%d/%d",&month, &year)) != 0){
10331: }
10332: else if( (iout=sscanf(strb,"%s.",dummy)) != 0){
10333: month=99;
10334: year=9999;
10335: }else{
10336: 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);
10337: 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);
10338: return 1;
10339: }
10340: andc[i]=(double) year;
10341: moisdc[i]=(double) month;
10342: strcpy(line,stra);
10343:
10344: /* Date of birth */
10345: cutv(stra, strb,line,' ');
10346: if( (iout=sscanf(strb,"%d/%d",&month, &year)) != 0){
10347: }
10348: else if( (iout=sscanf(strb,"%s.", dummy)) != 0){
10349: month=99;
10350: year=9999;
10351: }else{
10352: 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);
10353: 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);
10354: return 1;
10355: }
10356: if (year==9999) {
10357: 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);
10358: 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);
10359: return 1;
10360:
10361: }
10362: annais[i]=(double)(year);
10363: moisnais[i]=(double)(month);
10364: for (j=1;j<=maxwav;j++){
10365: if( (int)anint[j][i]+ (int)(mint[j][i])/12. < (int) (moisnais[i]/12.+annais[i])){
10366: printf("Warning reading data around '%s' at line number %d for individual %d, '%s'\nThe date of interview (%2d/%4d) at wave %d occurred before the date of birth (%2d/%4d).\n",strb, linei,i, line, (int)mint[j][i],(int)anint[j][i], j,(int)moisnais[i],(int)annais[i]);
10367: fprintf(ficlog,"Warning reading data around '%s' at line number %d for individual %d, '%s'\nThe date of interview (%2d/%4d) at wave %d occurred before the date of birth (%2d/%4d).\n",strb, linei,i, line, (int)mint[j][i],(int)anint[j][i], j, (int)moisnais[i],(int)annais[i]);
10368: }
10369: }
10370:
10371: strcpy(line,stra);
10372:
10373: /* Sample weight */
10374: cutv(stra, strb,line,' ');
10375: errno=0;
10376: dval=strtod(strb,&endptr);
10377: if( strb[0]=='\0' || (*endptr != '\0')){
10378: printf("Error reading data around '%f' at line number %d, \"%s\" for individual %d\nShould be a weight. Exiting.\n",dval, i,line,linei);
10379: 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);
10380: fflush(ficlog);
10381: return 1;
10382: }
10383: weight[i]=dval;
10384: strcpy(line,stra);
10385:
10386: for (iv=nqv;iv>=1;iv--){ /* Loop on fixed quantitative variables */
10387: cutv(stra, strb, line, ' ');
10388: if(strb[0]=='.') { /* Missing value */
10389: lval=-1;
10390: coqvar[iv][i]=NAN;
10391: covar[ncovcol+iv][i]=NAN; /* including qvar in standard covar for performance reasons */
10392: }else{
10393: errno=0;
10394: /* what_kind_of_number(strb); */
10395: dval=strtod(strb,&endptr);
10396: /* if(strb != endptr && *endptr == '\0') */
10397: /* dval=dlval; */
10398: /* if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN)) */
10399: if( strb[0]=='\0' || (*endptr != '\0')){
10400: 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);
10401: 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);
10402: return 1;
10403: }
10404: coqvar[iv][i]=dval;
10405: covar[ncovcol+iv][i]=dval; /* including qvar in standard covar for performance reasons */
10406: }
10407: strcpy(line,stra);
10408: }/* end loop nqv */
10409:
10410: /* Covariate values */
10411: for (j=ncovcol;j>=1;j--){
10412: cutv(stra, strb,line,' ');
10413: if(strb[0]=='.') { /* Missing covariate value */
10414: lval=-1;
10415: }else{
10416: errno=0;
10417: lval=strtol(strb,&endptr,10);
10418: if( strb[0]=='\0' || (*endptr != '\0')){
10419: 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);
10420: 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);
10421: return 1;
10422: }
10423: }
10424: if(lval <-1 || lval >1){
10425: printf("Error reading data around '%ld' at line number %d for individual %d, '%s'\n \
10426: Should be a value of %d(nth) covariate (0 should be the value for the reference and 1\n \
10427: for the alternative. IMaCh does not build design variables automatically, do it yourself.\n \
10428: For example, for multinomial values like 1, 2 and 3,\n \
10429: build V1=0 V2=0 for the reference value (1),\n \
10430: V1=1 V2=0 for (2) \n \
10431: and V1=0 V2=1 for (3). V1=1 V2=1 should not exist and the corresponding\n \
10432: output of IMaCh is often meaningless.\n \
10433: Exiting.\n",lval,linei, i,line,j);
10434: fprintf(ficlog,"Error reading data around '%ld' at line number %d for individual %d, '%s'\n \
10435: Should be a value of %d(nth) covariate (0 should be the value for the reference and 1\n \
10436: for the alternative. IMaCh does not build design variables automatically, do it yourself.\n \
10437: For example, for multinomial values like 1, 2 and 3,\n \
10438: build V1=0 V2=0 for the reference value (1),\n \
10439: V1=1 V2=0 for (2) \n \
10440: and V1=0 V2=1 for (3). V1=1 V2=1 should not exist and the corresponding\n \
10441: output of IMaCh is often meaningless.\n \
10442: Exiting.\n",lval,linei, i,line,j);fflush(ficlog);
10443: return 1;
10444: }
10445: covar[j][i]=(double)(lval);
10446: strcpy(line,stra);
10447: }
10448: lstra=strlen(stra);
10449:
10450: if(lstra > 9){ /* More than 2**32 or max of what printf can write with %ld */
10451: stratrunc = &(stra[lstra-9]);
10452: num[i]=atol(stratrunc);
10453: }
10454: else
10455: num[i]=atol(stra);
10456: /*if((s[2][i]==2) && (s[3][i]==-1)&&(s[4][i]==9)){
10457: 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;}*/
10458:
10459: i=i+1;
10460: } /* End loop reading data */
10461:
10462: *imax=i-1; /* Number of individuals */
10463: fclose(fic);
10464:
10465: return (0);
10466: /* endread: */
10467: printf("Exiting readdata: ");
10468: fclose(fic);
10469: return (1);
10470: }
10471:
10472: void removefirstspace(char **stri){/*, char stro[]) {*/
10473: char *p1 = *stri, *p2 = *stri;
10474: while (*p2 == ' ')
10475: p2++;
10476: /* while ((*p1++ = *p2++) !=0) */
10477: /* ; */
10478: /* do */
10479: /* while (*p2 == ' ') */
10480: /* p2++; */
10481: /* while (*p1++ == *p2++); */
10482: *stri=p2;
10483: }
10484:
10485: int decoderesult( char resultline[], int nres)
10486: /**< This routine decode one result line and returns the combination # of dummy covariates only **/
10487: {
10488: int j=0, k=0, k1=0, k2=0, k3=0, k4=0, match=0, k2q=0, k3q=0, k4q=0;
10489: char resultsav[MAXLINE];
10490: /* int resultmodel[MAXLINE]; */
10491: /* int modelresult[MAXLINE]; */
10492: char stra[80], strb[80], strc[80], strd[80],stre[80];
10493:
10494: removefirstspace(&resultline);
10495: printf("decoderesult:%s\n",resultline);
10496:
10497: strcpy(resultsav,resultline);
10498: printf("Decoderesult resultsav=\"%s\" resultline=\"%s\"\n", resultsav, resultline);
10499: if (strlen(resultsav) >1){
10500: j=nbocc(resultsav,'='); /**< j=Number of covariate values'=' in this resultline */
10501: }
10502: if(j == 0){ /* Resultline but no = */
10503: TKresult[nres]=0; /* Combination for the nresult and the model */
10504: return (0);
10505: }
10506: if( j != cptcovs ){ /* Be careful if a variable is in a product but not single */
10507: printf("ERROR: the number of variables in the resultline which is %d, differs from the number %d of single variables used in the model line, %s.\n",j, cptcovs, model);
10508: fprintf(ficlog,"ERROR: the number of variables in the resultline which is %d, differs from the number %d of single variables used in the model line, %s.\n",j, cptcovs, model);
10509: /* return 1;*/
10510: }
10511: for(k=1; k<=j;k++){ /* Loop on any covariate of the RESULT LINE */
10512: if(nbocc(resultsav,'=') >1){
10513: cutl(stra,strb,resultsav,' '); /* keeps in strb after the first ' ' (stra is the rest of the resultline to be analyzed in the next loop *//* resultsav= "V4=1 V5=25.1 V3=0" stra= "V5=25.1 V3=0" strb= "V4=1" */
10514: /* If resultsav= "V4= 1 V5=25.1 V3=0" with a blank then strb="V4=" and stra="1 V5=25.1 V3=0" */
10515: cutl(strc,strd,strb,'='); /* strb:"V4=1" strc="1" strd="V4" */
10516: /* If a blank, then strc="V4=" and strd='\0' */
10517: if(strc[0]=='\0'){
10518: printf("Error in resultline, probably a blank after the \"%s\", \"result:%s\", stra=\"%s\" resultsav=\"%s\"\n",strb,resultline, stra, resultsav);
10519: fprintf(ficlog,"Error in resultline, probably a blank after the \"V%s=\", resultline=%s\n",strb,resultline);
10520: return 1;
10521: }
10522: }else
10523: cutl(strc,strd,resultsav,'=');
10524: Tvalsel[k]=atof(strc); /* 1 */ /* Tvalsel of k is the float value of the kth covariate appearing in this result line */
10525:
10526: cutl(strc,stre,strd,'V'); /* strd='V4' strc=4 stre='V' */;
10527: Tvarsel[k]=atoi(strc); /* 4 */ /* Tvarsel is the id of the kth covariate in the result line Tvarsel[1] in "V4=1.." is 4.*/
10528: /* Typevarsel[k]=1; /\* 1 for age product *\/ */
10529: /* cptcovsel++; */
10530: if (nbocc(stra,'=') >0)
10531: strcpy(resultsav,stra); /* and analyzes it */
10532: }
10533: /* Checking for missing or useless values in comparison of current model needs */
10534: /* Feeds resultmodel[nres][k1]=k2 for k1th product covariate with age in the model equation fed by the index k2 of the resutline*/
10535: for(k1=1; k1<= cptcovt ;k1++){ /* Loop on MODEL LINE V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
10536: if(Typevar[k1]==0){ /* Single covariate in model */
10537: /* 0 for simple covariate (dummy, quantitative, fixed or varying), 1 for age product, 2 for product */
10538: match=0;
10539: for(k2=1; k2 <=j;k2++){/* Loop on resultline. In result line V4=1 V5=24.1 V3=1 V2=8 V1=0 */
10540: if(Tvar[k1]==Tvarsel[k2]) {/* Tvar is coming from the model, Tvarsel from the result. Tvar[1]=5 == Tvarsel[2]=5 */
10541: modelresult[nres][k2]=k1;/* modelresult[2]=1 modelresult[1]=2 modelresult[3]=3 modelresult[6]=4 modelresult[9]=5 */
10542: match=1; /* modelresult of k2 variable of resultline is identical to k1 variable of the model good */
10543: break;
10544: }
10545: }
10546: if(match == 0){
10547: printf("Error in result line (Dummy single): V%d is missing in result: %s according to model=1+age+%s. Tvar[k1=%d]=%d is different from Tvarsel[k2=%d]=%d.\n",Tvar[k1], resultline, model,k1, Tvar[k1], k2, Tvarsel[k2]);
10548: fprintf(ficlog,"Error in result line (Dummy single): V%d is missing in result: %s according to model=1+age+%s\n",Tvar[k1], resultline, model);
10549: return 1;
10550: }
10551: }else if(Typevar[k1]==1){ /* Product with age We want to get the position k2 in the resultline of the product k1 in the model line*/
10552: /* We feed resultmodel[k1]=k2; */
10553: match=0;
10554: for(k2=1; k2 <=j;k2++){/* Loop on resultline. jth occurence of = signs in the result line. In result line V4=1 V5=24.1 V3=1 V2=8 V1=0 */
10555: if(Tvar[k1]==Tvarsel[k2]) {/* Tvar is coming from the model, Tvarsel from the result. Tvar[1]=5 == Tvarsel[2]=5 */
10556: modelresult[nres][k2]=k1;/* we found a Vn=1 corrresponding to Vn*age in the model modelresult[2]=1 modelresult[1]=2 modelresult[3]=3 modelresult[6]=4 modelresult[9]=5 */
10557: resultmodel[nres][k1]=k2; /* Added here */
10558: printf("Decoderesult first modelresult[k2=%d]=%d (k1) V%d*AGE\n",k2,k1,Tvar[k1]);
10559: match=1; /* modelresult of k2 variable of resultline is identical to k1 variable of the model good */
10560: break;
10561: }
10562: }
10563: if(match == 0){
10564: printf("Error in result line (Product with age): V%d is missing in result: %s according to model=1+age+%s (Tvarsel[k2=%d]=%d)\n",Tvar[k1], resultline, model, k2, Tvarsel[k2]);
10565: fprintf(ficlog,"Error in result line (Product with age): V%d is missing in result: %s according to model=1+age+%s (Tvarsel[k2=%d]=%d)\n",Tvar[k1], resultline, model, k2, Tvarsel[k2]);
10566: return 1;
10567: }
10568: }else if(Typevar[k1]==2){ /* Product No age We want to get the position in the resultline of the product in the model line*/
10569: /* resultmodel[nres][of such a Vn * Vm product k1] is not unique, so can't exist, we feed Tvard[k1][1] and [2] */
10570: match=0;
10571: printf("Decoderesult very first Product Tvardk[k1=%d][1]=%d Tvardk[k1=%d][2]=%d V%d * V%d\n",k1,Tvardk[k1][1],k1,Tvardk[k1][2],Tvardk[k1][1],Tvardk[k1][2]);
10572: for(k2=1; k2 <=j;k2++){/* Loop on resultline. In result line V4=1 V5=24.1 V3=1 V2=8 V1=0 */
10573: if(Tvardk[k1][1]==Tvarsel[k2]) {/* Tvardk is coming from the model, Tvarsel from the result. Tvar[1]=5 == Tvarsel[2]=5 */
10574: /* modelresult[k2]=k1; */
10575: printf("Decoderesult first Product modelresult[k2=%d]=%d (k1) V%d * \n",k2,k1,Tvarsel[k2]);
10576: match=1; /* modelresult of k2 variable of resultline is identical to k1 variable of the model good */
10577: }
10578: }
10579: if(match == 0){
10580: printf("Error in result line (Product without age first variable): V%d is missing in result: %s according to model=1+age+%s\n",Tvardk[k1][1], resultline, model);
10581: fprintf(ficlog,"Error in result line (Product without age first variable): V%d is missing in result: %s according to model=1+age+%s\n",Tvardk[k1][1], resultline, model);
10582: return 1;
10583: }
10584: match=0;
10585: for(k2=1; k2 <=j;k2++){/* Loop on resultline. In result line V4=1 V5=24.1 V3=1 V2=8 V1=0 */
10586: if(Tvardk[k1][2]==Tvarsel[k2]) {/* Tvardk is coming from the model, Tvarsel from the result. Tvar[1]=5 == Tvarsel[2]=5 */
10587: /* modelresult[k2]=k1;*/
10588: printf("Decoderesult second Product modelresult[k2=%d]=%d (k1) * V%d \n ",k2,k1,Tvarsel[k2]);
10589: match=1; /* modelresult of k2 variable of resultline is identical to k1 variable of the model good */
10590: break;
10591: }
10592: }
10593: if(match == 0){
10594: printf("Error in result line (Product without age second variable): V%d is missing in result: %s according to model=1+age+%s\n",Tvardk[k1][2], resultline, model);
10595: fprintf(ficlog,"Error in result line (Product without age second variable): V%d is missing in result : %s according to model=1+age+%s\n",Tvardk[k1][2], resultline, model);
10596: return 1;
10597: }
10598: }/* End of testing */
10599: }/* End loop cptcovt */
10600: /* Checking for missing or useless values in comparison of current model needs */
10601: /* Feeds resultmodel[nres][k1]=k2 for single covariate (k1) in the model equation */
10602: for(k2=1; k2 <=j;k2++){ /* j or cptcovs is the number of single covariates used either in the model line as well as in the result line (dummy or quantitative)
10603: * Loop on resultline variables: result line V4=1 V5=24.1 V3=1 V2=8 V1=0 */
10604: match=0;
10605: for(k1=1; k1<= cptcovt ;k1++){ /* loop on model: model line V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
10606: if(Typevar[k1]==0){ /* Single only */
10607: if(Tvar[k1]==Tvarsel[k2]) { /* Tvar[2]=4 == Tvarsel[1]=4 */
10608: resultmodel[nres][k1]=k2; /* k1th position in the model equation corresponds to k2th position in the result line. resultmodel[2]=1 resultmodel[1]=2 resultmodel[3]=3 resultmodel[6]=4 resultmodel[9]=5 */
10609: modelresult[nres][k2]=k1; /* k1th position in the model equation corresponds to k2th position in the result line. modelresult[1]=2 modelresult[2]=1 modelresult[3]=3 remodelresult[4]=6 modelresult[5]=9 */
10610: ++match;
10611: }
10612: }
10613: }
10614: if(match == 0){
10615: printf("Error in result line: variable V%d is missing in model; result: %s, model=1+age+%s\n",Tvarsel[k2], resultline, model);
10616: fprintf(ficlog,"Error in result line: variable V%d is missing in model; result: %s, model=1+age+%s\n",Tvarsel[k2], resultline, model);
10617: return 1;
10618: }else if(match > 1){
10619: printf("Error in result line: %d doubled; result: %s, model=1+age+%s\n",k2, resultline, model);
10620: fprintf(ficlog,"Error in result line: %d doubled; result: %s, model=1+age+%s\n",k2, resultline, model);
10621: return 1;
10622: }
10623: }
10624: /* cptcovres=j /\* Number of variables in the resultline is equal to cptcovs and thus useless *\/ */
10625: /* We need to deduce which combination number is chosen and save quantitative values */
10626: /* model line V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
10627: /* nres=1st result line: V4=1 V5=25.1 V3=0 V2=8 V1=1 */
10628: /* should correspond to the combination 6 of dummy: V4=1, V3=0, V1=1 => V4*2**(0) + V3*2**(1) + V1*2**(2) = 1*1 + 0*2 + 1*4 = 5 + (1offset) = 6*/
10629: /* nres=2nd result line: V4=1 V5=24.1 V3=1 V2=8 V1=0 */
10630: /* should give a combination of dummy V4=1, V3=1, V1=0 => V4*2**(0) + V3*2**(1) + V1*2**(2) = 3 + (1offset) = 4*/
10631: /* 1 0 0 0 */
10632: /* 2 1 0 0 */
10633: /* 3 0 1 0 */
10634: /* 4 1 1 0 */ /* V4=1, V3=1, V1=0 (nres=2)*/
10635: /* 5 0 0 1 */
10636: /* 6 1 0 1 */ /* V4=1, V3=0, V1=1 (nres=1)*/
10637: /* 7 0 1 1 */
10638: /* 8 1 1 1 */
10639: /* V(Tvresult)=Tresult V4=1 V3=0 V1=1 Tresult[nres=1][2]=0 */
10640: /* V(Tvqresult)=Tqresult V5=25.1 V2=8 Tqresult[nres=1][1]=25.1 */
10641: /* V5*age V5 known which value for nres? */
10642: /* Tqinvresult[2]=8 Tqinvresult[1]=25.1 */
10643: for(k1=1, k=0, k4=0, k4q=0; k1 <=cptcovt;k1++){ /* cptcovt number of covariates (excluding 1 and age or age*age) in the MODEL equation.
10644: * loop on position k1 in the MODEL LINE */
10645: /* k counting number of combination of single dummies in the equation model */
10646: /* k4 counting single dummies in the equation model */
10647: /* k4q counting single quantitatives in the equation model */
10648: if( Dummy[k1]==0 && Typevar[k1]==0 ){ /* Dummy and Single, k1 is sorting according to MODEL, but k3 to resultline */
10649: /* k4+1= (not always if quant in model) position in the resultline V(Tvarsel)=Tvalsel=Tresult[nres][pos](value); V(Tvresult[nres][pos] (variable): V(variable)=value) */
10650: /* modelresult[k3]=k1: k3th position in the result line corresponds to the k1 position in the model line (doesn't work with products)*/
10651: /* Value in the (current nres) resultline of the variable at the k1th position in the model equation resultmodel[nres][k1]= k3 */
10652: /* resultmodel[nres][k1]=k3: k1th position in the model correspond to the k3 position in the resultline */
10653: /* k3 is the position in the nres result line of the k1th variable of the model equation */
10654: /* Tvarsel[k3]: Name of the variable at the k3th position in the result line. */
10655: /* Tvalsel[k3]: Value of the variable at the k3th position in the result line. */
10656: /* Tresult[nres][result_position]= value of the dummy variable at the result_position in the nres resultline */
10657: /* Tvresult[nres][result_position]= name of the dummy variable at the result_position in the nres resultline */
10658: /* Tinvresult[nres][Name of a dummy variable]= value of the variable in the result line */
10659: /* TinvDoQresult[nres][Name of a Dummy or Q variable]= value of the variable in the result line */
10660: k3= resultmodel[nres][k1]; /* From position k1 in model get position k3 in result line */
10661: /* nres=1 k1=2 resultmodel[2(V4)] = 1=k3 ; k1=3 resultmodel[3(V3)] = 2=k3*/
10662: k2=(int)Tvarsel[k3]; /* from position k3 in resultline get name k2: nres=1 k1=2=>k3=1 Tvarsel[resultmodel[2]]= Tvarsel[1] = 4=k2 (V4); k1=3=>k3=2 Tvarsel[2]=3 (V3)*/
10663: k+=Tvalsel[k3]*pow(2,k4); /* nres=1 k1=2 Tvalsel[1]=1 (V4=1); k1=3 k3=2 Tvalsel[2]=0 (V3=0) */
10664: TinvDoQresult[nres][(int)Tvarsel[k3]]=Tvalsel[k3]; /* TinvDoQresult[nres][Name]=Value; stores the value into the name of the variable. */
10665: /* Tinvresult[nres][4]=1 */
10666: /* Tresult[nres][k4+1]=Tvalsel[k3];/\* Tresult[nres=2][1]=1(V4=1) Tresult[nres=2][2]=0(V3=0) *\/ */
10667: Tresult[nres][k3]=Tvalsel[k3];/* Tresult[nres=2][1]=1(V4=1) Tresult[nres=2][2]=0(V3=0) */
10668: /* Tvresult[nres][k4+1]=(int)Tvarsel[k3];/\* Tvresult[nres][1]=4 Tvresult[nres][3]=1 *\/ */
10669: Tvresult[nres][k3]=(int)Tvarsel[k3];/* Tvresult[nres][1]=4 Tvresult[nres][3]=1 */
10670: Tinvresult[nres][(int)Tvarsel[k3]]=Tvalsel[k3]; /* Tinvresult[nres][4]=1 */
10671: precov[nres][k1]=Tvalsel[k3]; /* Value from resultline of the variable at the k1 position in the model */
10672: printf("Decoderesult Dummy k=%d, k1=%d precov[nres=%d][k1=%d]=%.f V(k2=V%d)= Tvalsel[%d]=%d, 2**(%d)\n",k, k1, nres, k1,precov[nres][k1], k2, k3, (int)Tvalsel[k3], k4);
10673: k4++;;
10674: }else if( Dummy[k1]==1 && Typevar[k1]==0 ){ /* Quantitative and single */
10675: /* Tqresult[nres][result_position]= value of the variable at the result_position in the nres resultline */
10676: /* Tvqresult[nres][result_position]= id of the variable at the result_position in the nres resultline */
10677: /* Tqinvresult[nres][Name of a quantitative variable]= value of the variable in the result line */
10678: k3q= resultmodel[nres][k1]; /* resultmodel[1(V5)] = 5 =k3q */
10679: k2q=(int)Tvarsel[k3q]; /* Name of variable at k3q th position in the resultline */
10680: /* Tvarsel[resultmodel[1]]= Tvarsel[1] = 4=k2 */
10681: /* Tqresult[nres][k4q+1]=Tvalsel[k3q]; /\* Tqresult[nres][1]=25.1 *\/ */
10682: /* Tvresult[nres][k4q+1]=(int)Tvarsel[k3q];/\* Tvresult[nres][1]=4 Tvresult[nres][3]=1 *\/ */
10683: /* Tvqresult[nres][k4q+1]=(int)Tvarsel[k3q]; /\* Tvqresult[nres][1]=5 *\/ */
10684: Tqresult[nres][k3q]=Tvalsel[k3q]; /* Tqresult[nres][1]=25.1 */
10685: Tvresult[nres][k3q]=(int)Tvarsel[k3q];/* Tvresult[nres][1]=4 Tvresult[nres][3]=1 */
10686: Tvqresult[nres][k3q]=(int)Tvarsel[k3q]; /* Tvqresult[nres][1]=5 */
10687: Tqinvresult[nres][(int)Tvarsel[k3q]]=Tvalsel[k3q]; /* Tqinvresult[nres][5]=25.1 */
10688: TinvDoQresult[nres][(int)Tvarsel[k3q]]=Tvalsel[k3q]; /* Tqinvresult[nres][5]=25.1 */
10689: precov[nres][k1]=Tvalsel[k3q];
10690: printf("Decoderesult Quantitative nres=%d,precov[nres=%d][k1=%d]=%.f V(k2q=V%d)= Tvalsel[%d]=%d, Tvarsel[%d]=%f\n",nres, nres, k1,precov[nres][k1], k2q, k3q, Tvarsel[k3q], k3q, Tvalsel[k3q]);
10691: k4q++;;
10692: }else if( Dummy[k1]==2 ){ /* For dummy with age product */
10693: /* Tvar[k1]; */ /* Age variable */
10694: /* Wrong we want the value of variable name Tvar[k1] */
10695:
10696: k3= resultmodel[nres][k1]; /* nres=1 k1=2 resultmodel[2(V4)] = 1=k3 ; k1=3 resultmodel[3(V3)] = 2=k3*/
10697: k2=(int)Tvarsel[k3]; /* nres=1 k1=2=>k3=1 Tvarsel[resultmodel[2]]= Tvarsel[1] = 4=k2 (V4); k1=3=>k3=2 Tvarsel[2]=3 (V3)*/
10698: TinvDoQresult[nres][(int)Tvarsel[k3]]=Tvalsel[k3]; /* TinvDoQresult[nres][4]=1 */
10699: precov[nres][k1]=Tvalsel[k3];
10700: printf("Decoderesult Dummy with age k=%d, k1=%d precov[nres=%d][k1=%d]=%.f Tvar[%d]=V%d k2=Tvarsel[%d]=%d Tvalsel[%d]=%d\n",k, k1, nres, k1,precov[nres][k1], k1, Tvar[k1], k3,(int)Tvarsel[k3], k3, (int)Tvalsel[k3]);
10701: }else if( Dummy[k1]==3 ){ /* For quant with age product */
10702: k3q= resultmodel[nres][k1]; /* resultmodel[1(V5)] = 25.1=k3q */
10703: k2q=(int)Tvarsel[k3q]; /* Tvarsel[resultmodel[1]]= Tvarsel[1] = 4=k2 */
10704: TinvDoQresult[nres][(int)Tvarsel[k3q]]=Tvalsel[k3q]; /* TinvDoQresult[nres][5]=25.1 */
10705: precov[nres][k1]=Tvalsel[k3q];
10706: printf("Decoderesult Quantitative with age nres=%d, k1=%d, precov[nres=%d][k1=%d]=%f Tvar[%d]=V%d V(k2q=%d)= Tvarsel[%d]=%d, Tvalsel[%d]=%f\n",nres, k1, nres, k1,precov[nres][k1], k1, Tvar[k1], k2q, k3q, Tvarsel[k3q], k3q, Tvalsel[k3q]);
10707: }else if(Typevar[k1]==2 ){ /* For product quant or dummy (not with age) */
10708: precov[nres][k1]=TinvDoQresult[nres][Tvardk[k1][1]] * TinvDoQresult[nres][Tvardk[k1][2]];
10709: printf("Decoderesult Quantitative or Dummy (not with age) nres=%d k1=%d precov[nres=%d][k1=%d]=%.f V%d(=%.f) * V%d(=%.f) \n",nres, k1, nres, k1,precov[nres][k1], Tvardk[k1][1], TinvDoQresult[nres][Tvardk[k1][1]], Tvardk[k1][2], TinvDoQresult[nres][Tvardk[k1][2]]);
10710: }else{
10711: printf("Error Decoderesult probably a product Dummy[%d]==%d && Typevar[%d]==%d\n", k1, Dummy[k1], k1, Typevar[k1]);
10712: fprintf(ficlog,"Error Decoderesult probably a product Dummy[%d]==%d && Typevar[%d]==%d\n", k1, Dummy[k1], k1, Typevar[k1]);
10713: }
10714: }
10715:
10716: TKresult[nres]=++k; /* Number of combinations of dummies for the nresult and the model =Tvalsel[k3]*pow(2,k4) + 1*/
10717: return (0);
10718: }
10719:
10720: int decodemodel( char model[], int lastobs)
10721: /**< This routine decodes the model and returns:
10722: * Model V1+V2+V3+V8+V7*V8+V5*V6+V8*age+V3*age+age*age
10723: * - nagesqr = 1 if age*age in the model, otherwise 0.
10724: * - cptcovt total number of covariates of the model nbocc(+)+1 = 8 excepting constant and age and age*age
10725: * - cptcovn or number of covariates k of the models excluding age*products =6 and age*age
10726: * - cptcovage number of covariates with age*products =2
10727: * - cptcovs number of simple covariates
10728: * - 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
10729: * which is a new column after the 9 (ncovcol) variables.
10730: * - if k is a product Vn*Vm, covar[k][i] is filled with correct values for each individual
10731: * - Tprod[l] gives the kth covariates of the product Vn*Vm l=1 to cptcovprod-cptcovage
10732: * Tprod[1]@2 {5, 6}: position of first product V7*V8 is 5, and second V5*V6 is 6.
10733: * - Tvard[k] p Tvard[1][1]@4 {7, 8, 5, 6} for V7*V8 and V5*V6 .
10734: */
10735: /* V2+V1+V4+V3*age Tvar[4]=3 ; V1+V2*age Tvar[2]=2; V1+V1*age Tvar[2]=1, Tage[1]=2 */
10736: {
10737: int i, j, k, ks, v;
10738: int j1, k1, k2, k3, k4;
10739: char modelsav[80];
10740: char stra[80], strb[80], strc[80], strd[80],stre[80];
10741: char *strpt;
10742:
10743: /*removespace(model);*/
10744: if (strlen(model) >1){ /* If there is at least 1 covariate */
10745: j=0, j1=0, k1=0, k2=-1, ks=0, cptcovn=0;
10746: if (strstr(model,"AGE") !=0){
10747: printf("Error. AGE must be in lower case 'age' model=1+age+%s. ",model);
10748: fprintf(ficlog,"Error. AGE must be in lower case model=1+age+%s. ",model);fflush(ficlog);
10749: return 1;
10750: }
10751: if (strstr(model,"v") !=0){
10752: printf("Error. 'v' must be in upper case 'V' model=1+age+%s ",model);
10753: fprintf(ficlog,"Error. 'v' must be in upper case model=1+age+%s ",model);fflush(ficlog);
10754: return 1;
10755: }
10756: strcpy(modelsav,model);
10757: if ((strpt=strstr(model,"age*age")) !=0){
10758: printf(" strpt=%s, model=1+age+%s\n",strpt, model);
10759: if(strpt != model){
10760: printf("Error in model: 'model=1+age+%s'; 'age*age' should in first place before other covariates\n \
10761: 'model=1+age+age*age+V1.' or 'model=1+age+age*age+V1+V1*age.', please swap as well as \n \
10762: corresponding column of parameters.\n",model);
10763: fprintf(ficlog,"Error in model: 'model=1+age+%s'; 'age*age' should in first place before other covariates\n \
10764: 'model=1+age+age*age+V1.' or 'model=1+age+age*age+V1+V1*age.', please swap as well as \n \
10765: corresponding column of parameters.\n",model); fflush(ficlog);
10766: return 1;
10767: }
10768: nagesqr=1;
10769: if (strstr(model,"+age*age") !=0)
10770: substrchaine(modelsav, model, "+age*age");
10771: else if (strstr(model,"age*age+") !=0)
10772: substrchaine(modelsav, model, "age*age+");
10773: else
10774: substrchaine(modelsav, model, "age*age");
10775: }else
10776: nagesqr=0;
10777: if (strlen(modelsav) >1){
10778: j=nbocc(modelsav,'+'); /**< j=Number of '+' */
10779: j1=nbocc(modelsav,'*'); /**< j1=Number of '*' */
10780: cptcovs=j+1-j1; /**< Number of simple covariates V1+V1*age+V3 +V3*V4+age*age=> V1 + V3 =5-3=2 */
10781: cptcovt= j+1; /* Number of total covariates in the model, not including
10782: * cst, age and age*age
10783: * V1+V1*age+ V3 + V3*V4+age*age=> 3+1=4*/
10784: /* including age products which are counted in cptcovage.
10785: * but the covariates which are products must be treated
10786: * separately: ncovn=4- 2=2 (V1+V3). */
10787: cptcovprod=j1; /**< Number of products V1*V2 +v3*age = 2 */
10788: cptcovprodnoage=0; /**< Number of covariate products without age: V3*V4 =1 */
10789:
10790:
10791: /* Design
10792: * V1 V2 V3 V4 V5 V6 V7 V8 V9 Weight
10793: * < ncovcol=8 >
10794: * Model V2 + V1 + V3*age + V3 + V5*V6 + V7*V8 + V8*age + V8
10795: * k= 1 2 3 4 5 6 7 8
10796: * cptcovn number of covariates (not including constant and age ) = # of + plus 1 = 7+1=8
10797: * covar[k,i], value of kth covariate if not including age for individual i:
10798: * covar[1][i]= (V1), covar[4][i]=(V4), covar[8][i]=(V8)
10799: * Tvar[k] # of the kth covariate: Tvar[1]=2 Tvar[2]=1 Tvar[4]=3 Tvar[8]=8
10800: * if multiplied by age: V3*age Tvar[3=V3*age]=3 (V3) Tvar[7]=8 and
10801: * Tage[++cptcovage]=k
10802: * if products, new covar are created after ncovcol with k1
10803: * Tvar[k]=ncovcol+k1; # of the kth covariate product: Tvar[5]=ncovcol+1=10 Tvar[6]=ncovcol+1=11
10804: * Tprod[k1]=k; Tprod[1]=5 Tprod[2]= 6; gives the position of the k1th product
10805: * 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
10806: * Tvar[cptcovn+k2]=Tvard[k1][1];Tvar[cptcovn+k2+1]=Tvard[k1][2];
10807: * Tvar[8+1]=5;Tvar[8+2]=6;Tvar[8+3]=7;Tvar[8+4]=8 inverted
10808: * V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11
10809: * < ncovcol=8 >
10810: * Model V2 + V1 + V3*age + V3 + V5*V6 + V7*V8 + V8*age + V8 d1 d1 d2 d2
10811: * k= 1 2 3 4 5 6 7 8 9 10 11 12
10812: * Tvar[k]= 2 1 3 3 10 11 8 8 5 6 7 8
10813: * p Tvar[1]@12={2, 1, 3, 3, 11, 10, 8, 8, 7, 8, 5, 6}
10814: * p Tprod[1]@2={ 6, 5}
10815: *p Tvard[1][1]@4= {7, 8, 5, 6}
10816: * covar[k][i]= V2 V1 ? V3 V5*V6? V7*V8? ? V8
10817: * cov[Tage[kk]+2]=covar[Tvar[Tage[kk]]][i]*cov[2];
10818: *How to reorganize? Tvars(orted)
10819: * Model V1 + V2 + V3 + V8 + V5*V6 + V7*V8 + V3*age + V8*age
10820: * Tvars {2, 1, 3, 3, 11, 10, 8, 8, 7, 8, 5, 6}
10821: * {2, 1, 4, 8, 5, 6, 3, 7}
10822: * Struct []
10823: */
10824:
10825: /* This loop fills the array Tvar from the string 'model'.*/
10826: /* j is the number of + signs in the model V1+V2+V3 j=2 i=3 to 1 */
10827: /* modelsav=V2+V1+V4+age*V3 strb=age*V3 stra=V2+V1+V4 */
10828: /* k=4 (age*V3) Tvar[k=4]= 3 (from V3) Tage[cptcovage=1]=4 */
10829: /* k=3 V4 Tvar[k=3]= 4 (from V4) */
10830: /* k=2 V1 Tvar[k=2]= 1 (from V1) */
10831: /* k=1 Tvar[1]=2 (from V2) */
10832: /* k=5 Tvar[5] */
10833: /* for (k=1; k<=cptcovn;k++) { */
10834: /* cov[2+k]=nbcode[Tvar[k]][codtabm(ij,Tvar[k])]; */
10835: /* } */
10836: /* for (k=1; k<=cptcovage;k++) cov[2+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,Tvar[Tage[k])]]*cov[2]; */
10837: /*
10838: * Treating invertedly V2+V1+V3*age+V2*V4 is as if written V2*V4 +V3*age + V1 + V2 */
10839: for(k=cptcovt; k>=1;k--){ /**< Number of covariates not including constant and age, neither age*age*/
10840: Tvar[k]=0; Tprod[k]=0; Tposprod[k]=0;
10841: }
10842: cptcovage=0;
10843: for(k=1; k<=cptcovt;k++){ /* Loop on total covariates of the model line */
10844: cutl(stra,strb,modelsav,'+'); /* keeps in strb after the first '+' cutl from left to right
10845: modelsav==V2+V1+V5*age+V4+V3*age strb=V3*age stra=V2+V1V5*age+V4 */ /* <model> "V5+V4+V3+V4*V3+V5*age+V1*age+V1" strb="V5" stra="V4+V3+V4*V3+V5*age+V1*age+V1" */
10846: if (nbocc(modelsav,'+')==0)
10847: strcpy(strb,modelsav); /* and analyzes it */
10848: /* printf("i=%d a=%s b=%s sav=%s\n",i, stra,strb,modelsav);*/
10849: /*scanf("%d",i);*/
10850: if (strchr(strb,'*')) { /**< Model includes a product V2+V1+V5*age+ V4+V3*age strb=V3*age */
10851: cutl(strc,strd,strb,'*'); /**< k=1 strd*strc Vm*Vn: strb=V3*age(input) strc=age strd=V3 ; V3*V2 strc=V2, strd=V3 */
10852: if (strcmp(strc,"age")==0) { /**< Model includes age: Vn*age */
10853: /* covar is not filled and then is empty */
10854: cptcovprod--;
10855: cutl(stre,strb,strd,'V'); /* strd=V3(input): stre="3" */
10856: Tvar[k]=atoi(stre); /* V2+V1+V5*age+V4+V3*age Tvar[5]=3 ; V1+V2*age Tvar[2]=2; V1+V1*age Tvar[2]=1 */
10857: Typevar[k]=1; /* 1 for age product */
10858: cptcovage++; /* Counts the number of covariates which include age as a product */
10859: Tage[cptcovage]=k; /* V2+V1+V4+V3*age Tvar[4]=3, Tage[1] = 4 or V1+V1*age Tvar[2]=1, Tage[1]=2 */
10860: /*printf("stre=%s ", stre);*/
10861: } else if (strcmp(strd,"age")==0) { /* or age*Vn */
10862: cptcovprod--;
10863: cutl(stre,strb,strc,'V');
10864: Tvar[k]=atoi(stre);
10865: Typevar[k]=1; /* 1 for age product */
10866: cptcovage++;
10867: Tage[cptcovage]=k;
10868: } else { /* Age is not in the model product V2+V1+V1*V4+V3*age+V3*V2 strb=V3*V2*/
10869: /* loops on k1=1 (V3*V2) and k1=2 V4*V3 */
10870: cptcovn++;
10871: cptcovprodnoage++;k1++;
10872: cutl(stre,strb,strc,'V'); /* strc= Vn, stre is n; strb=V3*V2 stre=3 strc=*/
10873: Tvar[k]=ncovcol+nqv+ntv+nqtv+k1; /* For model-covariate k tells which data-covariate to use but
10874: because this model-covariate is a construction we invent a new column
10875: which is after existing variables ncovcol+nqv+ntv+nqtv + k1
10876: If already ncovcol=4 and model= V2 + V1 + V1*V4 + age*V3 + V3*V2
10877: thus after V4 we invent V5 and V6 because age*V3 will be computed in 4
10878: Tvar[3=V1*V4]=4+1=5 Tvar[5=V3*V2]=4 + 2= 6, Tvar[4=age*V3]=4 etc */
10879: /* Please remark that the new variables are model dependent */
10880: /* If we have 4 variable but the model uses only 3, like in
10881: * model= V1 + age*V1 + V2 + V3 + age*V2 + age*V3 + V1*V2 + V1*V3
10882: * k= 1 2 3 4 5 6 7 8
10883: * Tvar[k]=1 1 2 3 2 3 (5 6) (and not 4 5 because of V4 missing)
10884: * Tage[kk] [1]= 2 [2]=5 [3]=6 kk=1 to cptcovage=3
10885: * Tvar[Tage[kk]][1]=2 [2]=2 [3]=3
10886: */
10887: Typevar[k]=2; /* 2 for double fixed dummy covariates */
10888: cutl(strc,strb,strd,'V'); /* strd was Vm, strc is m */
10889: Tprod[k1]=k; /* Tprod[1]=3(=V1*V4) for V2+V1+V1*V4+age*V3+V3*V2 */
10890: Tposprod[k]=k1; /* Tposprod[3]=1, Tposprod[2]=5 */
10891: Tvard[k1][1] =atoi(strc); /* m 1 for V1*/
10892: Tvardk[k][1] =atoi(strc); /* m 1 for V1*/
10893: Tvard[k1][2] =atoi(stre); /* n 4 for V4*/
10894: Tvardk[k][2] =atoi(stre); /* n 4 for V4*/
10895: k2=k2+2; /* k2 is initialize to -1, We want to store the n and m in Vn*Vm at the end of Tvar */
10896: /* Tvar[cptcovt+k2]=Tvard[k1][1]; /\* Tvar[(cptcovt=4+k2=1)=5]= 1 (V1) *\/ */
10897: /* Tvar[cptcovt+k2+1]=Tvard[k1][2]; /\* Tvar[(cptcovt=4+(k2=1)+1)=6]= 4 (V4) *\/ */
10898: /*ncovcol=4 and model=V2+V1+V1*V4+age*V3+V3*V2, Tvar[3]=5, Tvar[4]=6, cptcovt=5 */
10899: /* 1 2 3 4 5 | Tvar[5+1)=1, Tvar[7]=2 */
10900: for (i=1; i<=lastobs;i++){
10901: /* Computes the new covariate which is a product of
10902: covar[n][i]* covar[m][i] and stores it at ncovol+k1 May not be defined */
10903: covar[ncovcol+k1][i]=covar[atoi(stre)][i]*covar[atoi(strc)][i];
10904: }
10905: } /* End age is not in the model */
10906: } /* End if model includes a product */
10907: else { /* not a product */
10908: /*printf("d=%s c=%s b=%s\n", strd,strc,strb);*/
10909: /* scanf("%d",i);*/
10910: cutl(strd,strc,strb,'V');
10911: ks++; /**< Number of simple covariates dummy or quantitative, fixe or varying */
10912: cptcovn++; /** V4+V3+V5: V4 and V3 timevarying dummy covariates, V5 timevarying quantitative */
10913: Tvar[k]=atoi(strd);
10914: Typevar[k]=0; /* 0 for simple covariates */
10915: }
10916: strcpy(modelsav,stra); /* modelsav=V2+V1+V4 stra=V2+V1+V4 */
10917: /*printf("a=%s b=%s sav=%s\n", stra,strb,modelsav);
10918: scanf("%d",i);*/
10919: } /* end of loop + on total covariates */
10920: } /* end if strlen(modelsave == 0) age*age might exist */
10921: } /* end if strlen(model == 0) */
10922:
10923: /*The number n of Vn is stored in Tvar. cptcovage =number of age covariate. Tage gives the position of age. cptcovprod= number of products.
10924: If model=V1+V1*age then Tvar[1]=1 Tvar[2]=1 cptcovage=1 Tage[1]=2 cptcovprod=0*/
10925:
10926: /* printf("tvar1=%d tvar2=%d tvar3=%d cptcovage=%d Tage=%d",Tvar[1],Tvar[2],Tvar[3],cptcovage,Tage[1]);
10927: printf("cptcovprod=%d ", cptcovprod);
10928: fprintf(ficlog,"cptcovprod=%d ", cptcovprod);
10929: scanf("%d ",i);*/
10930:
10931:
10932: /* Until here, decodemodel knows only the grammar (simple, product, age*) of the model but not what kind
10933: of variable (dummy vs quantitative, fixed vs time varying) is behind. But we know the # of each. */
10934: /* ncovcol= 1, nqv=1 | ntv=2, nqtv= 1 = 5 possible variables data: 2 fixed 3, varying
10935: model= V5 + V4 +V3 + V4*V3 + V5*age + V2 + V1*V2 + V1*age + V5*age, V1 is not used saving its place
10936: k = 1 2 3 4 5 6 7 8 9
10937: Tvar[k]= 5 4 3 1+1+2+1+1=6 5 2 7 1 5
10938: Typevar[k]= 0 0 0 2 1 0 2 1 0
10939: Fixed[k] 1 1 1 1 3 0 0 or 2 2 3
10940: Dummy[k] 1 0 0 0 3 1 1 2 3
10941: Tmodelind[combination of covar]=k;
10942: */
10943: /* Dispatching between quantitative and time varying covariates */
10944: /* If Tvar[k] >ncovcol it is a product */
10945: /* Tvar[k] is the value n of Vn with n varying for 1 to nvcol, or p Vp=Vn*Vm for product */
10946: /* Computing effective variables, ie used by the model, that is from the cptcovt variables */
10947: printf("Model=1+age+%s\n\
10948: Typevar: 0 for simple covariate (dummy, quantitative, fixed or varying), 1 for age product, 2 for product \n\
10949: Fixed[k] 0=fixed (product or simple), 1 varying, 2 fixed with age product, 3 varying with age product \n\
10950: 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);
10951: fprintf(ficlog,"Model=1+age+%s\n\
10952: Typevar: 0 for simple covariate (dummy, quantitative, fixed or varying), 1 for age product, 2 for product \n\
10953: Fixed[k] 0=fixed (product or simple), 1 varying, 2 fixed with age product, 3 varying with age product \n\
10954: 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);
10955: for(k=-1;k<=cptcovt; k++){ Fixed[k]=0; Dummy[k]=0;}
10956: 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 */
10957: if (Tvar[k] <=ncovcol && Typevar[k]==0 ){ /* Simple fixed dummy (<=ncovcol) covariates */
10958: Fixed[k]= 0;
10959: Dummy[k]= 0;
10960: ncoveff++;
10961: ncovf++;
10962: nsd++;
10963: modell[k].maintype= FTYPE;
10964: TvarsD[nsd]=Tvar[k];
10965: TvarsDind[nsd]=k;
10966: TnsdVar[Tvar[k]]=nsd;
10967: TvarF[ncovf]=Tvar[k];
10968: TvarFind[ncovf]=k;
10969: TvarFD[ncoveff]=Tvar[k]; /* TvarFD[1]=V1 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
10970: TvarFDind[ncoveff]=k; /* TvarFDind[1]=9 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
10971: }else if( Tvar[k] <=ncovcol && Typevar[k]==2){ /* Product of fixed dummy (<=ncovcol) covariates */
10972: Fixed[k]= 0;
10973: Dummy[k]= 0;
10974: ncoveff++;
10975: ncovf++;
10976: modell[k].maintype= FTYPE;
10977: TvarF[ncovf]=Tvar[k];
10978: /* TnsdVar[Tvar[k]]=nsd; */ /* To be done */
10979: TvarFind[ncovf]=k;
10980: TvarFD[ncoveff]=Tvar[k]; /* TvarFD[1]=V1 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
10981: TvarFDind[ncoveff]=k; /* TvarFDind[1]=9 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
10982: }else if( Tvar[k] <=ncovcol+nqv && Typevar[k]==0){/* Remind that product Vn*Vm are added in k Only simple fixed quantitative variable */
10983: Fixed[k]= 0;
10984: Dummy[k]= 1;
10985: nqfveff++;
10986: modell[k].maintype= FTYPE;
10987: modell[k].subtype= FQ;
10988: nsq++;
10989: TvarsQ[nsq]=Tvar[k]; /* Gives the variable name (extended to products) of first nsq simple quantitative covariates (fixed or time vary see below */
10990: TvarsQind[nsq]=k; /* Gives the position in the model equation of the first nsq simple quantitative covariates (fixed or time vary) */
10991: ncovf++;
10992: TvarF[ncovf]=Tvar[k];
10993: TvarFind[ncovf]=k;
10994: 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 */
10995: 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 */
10996: }else if( Tvar[k] <=ncovcol+nqv+ntv && Typevar[k]==0){/* Only simple time varying dummy variables */
10997: Fixed[k]= 1;
10998: Dummy[k]= 0;
10999: ntveff++; /* Only simple time varying dummy variable */
11000: modell[k].maintype= VTYPE;
11001: modell[k].subtype= VD;
11002: nsd++;
11003: TvarsD[nsd]=Tvar[k];
11004: TvarsDind[nsd]=k;
11005: TnsdVar[Tvar[k]]=nsd; /* To be verified */
11006: ncovv++; /* Only simple time varying variables */
11007: TvarV[ncovv]=Tvar[k];
11008: TvarVind[ncovv]=k; /* TvarVind[2]=2 TvarVind[3]=3 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ /* Any time varying singele */
11009: 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 */
11010: 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 */
11011: 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);
11012: printf("Quasi TmodelInvind[%d]=%d\n",k,Tvar[k]- ncovcol-nqv);
11013: }else if( Tvar[k] <=ncovcol+nqv+ntv+nqtv && Typevar[k]==0){ /* Only simple time varying quantitative variable V5*/
11014: Fixed[k]= 1;
11015: Dummy[k]= 1;
11016: nqtveff++;
11017: modell[k].maintype= VTYPE;
11018: modell[k].subtype= VQ;
11019: ncovv++; /* Only simple time varying variables */
11020: nsq++;
11021: TvarsQ[nsq]=Tvar[k]; /* k=1 Tvar=5 nsq=1 TvarsQ[1]=5 */ /* Gives the variable name (extended to products) of first nsq simple quantitative covariates (fixed or time vary here) */
11022: TvarsQind[nsq]=k; /* For single quantitative covariate gives the model position of each single quantitative covariate *//* Gives the position in the model equation of the first nsq simple quantitative covariates (fixed or time vary) */
11023: TvarV[ncovv]=Tvar[k];
11024: TvarVind[ncovv]=k; /* TvarVind[1]=1 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ /* Any time varying singele */
11025: 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 */
11026: 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 */
11027: TmodelInvQind[nqtveff]=Tvar[k]- ncovcol-nqv-ntv;/* Only simple time varying quantitative variable */
11028: /* Tmodeliqind[k]=nqtveff;/\* Only simple time varying quantitative variable *\/ */
11029: 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);
11030: printf("Quasi TmodelInvQind[%d]=%d\n",k,Tvar[k]- ncovcol-nqv-ntv);
11031: }else if (Typevar[k] == 1) { /* product with age */
11032: ncova++;
11033: TvarA[ncova]=Tvar[k];
11034: TvarAind[ncova]=k;
11035: if (Tvar[k] <=ncovcol ){ /* Product age with fixed dummy covariatee */
11036: Fixed[k]= 2;
11037: Dummy[k]= 2;
11038: modell[k].maintype= ATYPE;
11039: modell[k].subtype= APFD;
11040: /* ncoveff++; */
11041: }else if( Tvar[k] <=ncovcol+nqv) { /* Remind that product Vn*Vm are added in k*/
11042: Fixed[k]= 2;
11043: Dummy[k]= 3;
11044: modell[k].maintype= ATYPE;
11045: modell[k].subtype= APFQ; /* Product age * fixed quantitative */
11046: /* nqfveff++; /\* Only simple fixed quantitative variable *\/ */
11047: }else if( Tvar[k] <=ncovcol+nqv+ntv ){
11048: Fixed[k]= 3;
11049: Dummy[k]= 2;
11050: modell[k].maintype= ATYPE;
11051: modell[k].subtype= APVD; /* Product age * varying dummy */
11052: /* ntveff++; /\* Only simple time varying dummy variable *\/ */
11053: }else if( Tvar[k] <=ncovcol+nqv+ntv+nqtv){
11054: Fixed[k]= 3;
11055: Dummy[k]= 3;
11056: modell[k].maintype= ATYPE;
11057: modell[k].subtype= APVQ; /* Product age * varying quantitative */
11058: /* nqtveff++;/\* Only simple time varying quantitative variable *\/ */
11059: }
11060: }else if (Typevar[k] == 2) { /* product without age */
11061: k1=Tposprod[k];
11062: if(Tvard[k1][1] <=ncovcol){
11063: if(Tvard[k1][2] <=ncovcol){
11064: Fixed[k]= 1;
11065: Dummy[k]= 0;
11066: modell[k].maintype= FTYPE;
11067: modell[k].subtype= FPDD; /* Product fixed dummy * fixed dummy */
11068: ncovf++; /* Fixed variables without age */
11069: TvarF[ncovf]=Tvar[k];
11070: TvarFind[ncovf]=k;
11071: }else if(Tvard[k1][2] <=ncovcol+nqv){
11072: Fixed[k]= 0; /* or 2 ?*/
11073: Dummy[k]= 1;
11074: modell[k].maintype= FTYPE;
11075: modell[k].subtype= FPDQ; /* Product fixed dummy * fixed quantitative */
11076: ncovf++; /* Varying variables without age */
11077: TvarF[ncovf]=Tvar[k];
11078: TvarFind[ncovf]=k;
11079: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv){
11080: Fixed[k]= 1;
11081: Dummy[k]= 0;
11082: modell[k].maintype= VTYPE;
11083: modell[k].subtype= VPDD; /* Product fixed dummy * varying dummy */
11084: ncovv++; /* Varying variables without age */
11085: TvarV[ncovv]=Tvar[k];
11086: TvarVind[ncovv]=k;
11087: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv+nqtv){
11088: Fixed[k]= 1;
11089: Dummy[k]= 1;
11090: modell[k].maintype= VTYPE;
11091: modell[k].subtype= VPDQ; /* Product fixed dummy * varying quantitative */
11092: ncovv++; /* Varying variables without age */
11093: TvarV[ncovv]=Tvar[k];
11094: TvarVind[ncovv]=k;
11095: }
11096: }else if(Tvard[k1][1] <=ncovcol+nqv){
11097: if(Tvard[k1][2] <=ncovcol){
11098: Fixed[k]= 0; /* or 2 ?*/
11099: Dummy[k]= 1;
11100: modell[k].maintype= FTYPE;
11101: modell[k].subtype= FPDQ; /* Product fixed quantitative * fixed dummy */
11102: ncovf++; /* Fixed variables without age */
11103: TvarF[ncovf]=Tvar[k];
11104: TvarFind[ncovf]=k;
11105: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv){
11106: Fixed[k]= 1;
11107: Dummy[k]= 1;
11108: modell[k].maintype= VTYPE;
11109: modell[k].subtype= VPDQ; /* Product fixed quantitative * varying dummy */
11110: ncovv++; /* Varying variables without age */
11111: TvarV[ncovv]=Tvar[k];
11112: TvarVind[ncovv]=k;
11113: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv+nqtv){
11114: Fixed[k]= 1;
11115: Dummy[k]= 1;
11116: modell[k].maintype= VTYPE;
11117: modell[k].subtype= VPQQ; /* Product fixed quantitative * varying quantitative */
11118: ncovv++; /* Varying variables without age */
11119: TvarV[ncovv]=Tvar[k];
11120: TvarVind[ncovv]=k;
11121: ncovv++; /* Varying variables without age */
11122: TvarV[ncovv]=Tvar[k];
11123: TvarVind[ncovv]=k;
11124: }
11125: }else if(Tvard[k1][1] <=ncovcol+nqv+ntv){
11126: if(Tvard[k1][2] <=ncovcol){
11127: Fixed[k]= 1;
11128: Dummy[k]= 1;
11129: modell[k].maintype= VTYPE;
11130: modell[k].subtype= VPDD; /* Product time varying dummy * fixed dummy */
11131: ncovv++; /* Varying variables without age */
11132: TvarV[ncovv]=Tvar[k];
11133: TvarVind[ncovv]=k;
11134: }else if(Tvard[k1][2] <=ncovcol+nqv){
11135: Fixed[k]= 1;
11136: Dummy[k]= 1;
11137: modell[k].maintype= VTYPE;
11138: modell[k].subtype= VPDQ; /* Product time varying dummy * fixed quantitative */
11139: ncovv++; /* Varying variables without age */
11140: TvarV[ncovv]=Tvar[k];
11141: TvarVind[ncovv]=k;
11142: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv){
11143: Fixed[k]= 1;
11144: Dummy[k]= 0;
11145: modell[k].maintype= VTYPE;
11146: modell[k].subtype= VPDD; /* Product time varying dummy * time varying dummy */
11147: ncovv++; /* Varying variables without age */
11148: TvarV[ncovv]=Tvar[k];
11149: TvarVind[ncovv]=k;
11150: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv+nqtv){
11151: Fixed[k]= 1;
11152: Dummy[k]= 1;
11153: modell[k].maintype= VTYPE;
11154: modell[k].subtype= VPDQ; /* Product time varying dummy * time varying quantitative */
11155: ncovv++; /* Varying variables without age */
11156: TvarV[ncovv]=Tvar[k];
11157: TvarVind[ncovv]=k;
11158: }
11159: }else if(Tvard[k1][1] <=ncovcol+nqv+ntv+nqtv){
11160: if(Tvard[k1][2] <=ncovcol){
11161: Fixed[k]= 1;
11162: Dummy[k]= 1;
11163: modell[k].maintype= VTYPE;
11164: modell[k].subtype= VPDQ; /* Product time varying quantitative * fixed dummy */
11165: ncovv++; /* Varying variables without age */
11166: TvarV[ncovv]=Tvar[k];
11167: TvarVind[ncovv]=k;
11168: }else if(Tvard[k1][2] <=ncovcol+nqv){
11169: Fixed[k]= 1;
11170: Dummy[k]= 1;
11171: modell[k].maintype= VTYPE;
11172: modell[k].subtype= VPQQ; /* Product time varying quantitative * fixed quantitative */
11173: ncovv++; /* Varying variables without age */
11174: TvarV[ncovv]=Tvar[k];
11175: TvarVind[ncovv]=k;
11176: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv){
11177: Fixed[k]= 1;
11178: Dummy[k]= 1;
11179: modell[k].maintype= VTYPE;
11180: modell[k].subtype= VPDQ; /* Product time varying quantitative * time varying dummy */
11181: ncovv++; /* Varying variables without age */
11182: TvarV[ncovv]=Tvar[k];
11183: TvarVind[ncovv]=k;
11184: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv+nqtv){
11185: Fixed[k]= 1;
11186: Dummy[k]= 1;
11187: modell[k].maintype= VTYPE;
11188: modell[k].subtype= VPQQ; /* Product time varying quantitative * time varying quantitative */
11189: ncovv++; /* Varying variables without age */
11190: TvarV[ncovv]=Tvar[k];
11191: TvarVind[ncovv]=k;
11192: }
11193: }else{
11194: printf("Error unknown type of covariate: Tvard[%d][1]=%d,Tvard[%d][2]=%d\n",k1,Tvard[k1][1],k1,Tvard[k1][2]);
11195: fprintf(ficlog,"Error unknown type of covariate: Tvard[%d][1]=%d,Tvard[%d][2]=%d\n",k1,Tvard[k1][1],k1,Tvard[k1][2]);
11196: } /*end k1*/
11197: }else{
11198: printf("Error, current version can't treat for performance reasons, Tvar[%d]=%d, Typevar[%d]=%d\n", k, Tvar[k], k, Typevar[k]);
11199: fprintf(ficlog,"Error, current version can't treat for performance reasons, Tvar[%d]=%d, Typevar[%d]=%d\n", k, Tvar[k], k, Typevar[k]);
11200: }
11201: 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]);
11202: printf(" modell[%d].maintype=%d, modell[%d].subtype=%d\n",k,modell[k].maintype,k,modell[k].subtype);
11203: 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]);
11204: }
11205: /* Searching for doublons in the model */
11206: for(k1=1; k1<= cptcovt;k1++){
11207: for(k2=1; k2 <k1;k2++){
11208: /* if((Typevar[k1]==Typevar[k2]) && (Fixed[Tvar[k1]]==Fixed[Tvar[k2]]) && (Dummy[Tvar[k1]]==Dummy[Tvar[k2]] )){ */
11209: if((Typevar[k1]==Typevar[k2]) && (Fixed[k1]==Fixed[k2]) && (Dummy[k1]==Dummy[k2] )){
11210: if((Typevar[k1] == 0 || Typevar[k1] == 1)){ /* Simple or age product */
11211: if(Tvar[k1]==Tvar[k2]){
11212: printf("Error duplication in the model=1+age+%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[k1],Dummy[k1]);
11213: fprintf(ficlog,"Error duplication in the model=1+age+%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[k1],Dummy[k1]); fflush(ficlog);
11214: return(1);
11215: }
11216: }else if (Typevar[k1] ==2){
11217: k3=Tposprod[k1];
11218: k4=Tposprod[k2];
11219: 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])) ){
11220: printf("Error duplication in the model=1+age+%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]]);
11221: fprintf(ficlog,"Error duplication in the model=1+age+%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);
11222: return(1);
11223: }
11224: }
11225: }
11226: }
11227: }
11228: printf("ncoveff=%d, nqfveff=%d, ntveff=%d, nqtveff=%d, cptcovn=%d\n",ncoveff,nqfveff,ntveff,nqtveff,cptcovn);
11229: fprintf(ficlog,"ncoveff=%d, nqfveff=%d, ntveff=%d, nqtveff=%d, cptcovn=%d\n",ncoveff,nqfveff,ntveff,nqtveff,cptcovn);
11230: printf("ncovf=%d, ncovv=%d, ncova=%d, nsd=%d, nsq=%d\n",ncovf,ncovv,ncova,nsd,nsq);
11231: fprintf(ficlog,"ncovf=%d, ncovv=%d, ncova=%d, nsd=%d, nsq=%d\n",ncovf,ncovv,ncova,nsd, nsq);
11232: return (0); /* with covar[new additional covariate if product] and Tage if age */
11233: /*endread:*/
11234: printf("Exiting decodemodel: ");
11235: return (1);
11236: }
11237:
11238: int calandcheckages(int imx, int maxwav, double *agemin, double *agemax, int *nberr, int *nbwarn )
11239: {/* Check ages at death */
11240: int i, m;
11241: int firstone=0;
11242:
11243: for (i=1; i<=imx; i++) {
11244: for(m=2; (m<= maxwav); m++) {
11245: if (((int)mint[m][i]== 99) && (s[m][i] <= nlstate)){
11246: anint[m][i]=9999;
11247: if (s[m][i] != -2) /* Keeping initial status of unknown vital status */
11248: s[m][i]=-1;
11249: }
11250: if((int)moisdc[i]==99 && (int)andc[i]==9999 && s[m][i]>nlstate){
11251: *nberr = *nberr + 1;
11252: if(firstone == 0){
11253: firstone=1;
11254: printf("Warning (#%d)! Date of death (month %2d and year %4d) of individual %ld on line %d was unknown but status is a death state %d at wave %d. If you don't know the vital status, please enter -2. If he/she is still alive but don't know the state, please code with '-1 or '.'. Here, we do not believe in a death, skipped.\nOther similar cases in log file\n", *nberr,(int)moisdc[i],(int)andc[i],num[i],i,s[m][i],m);
11255: }
11256: fprintf(ficlog,"Warning (#%d)! Date of death (month %2d and year %4d) of individual %ld on line %d was unknown but status is a death state %d at wave %d. If you don't know the vital status, please enter -2. If he/she is still alive but don't know the state, please code with '-1 or '.'. Here, we do not believe in a death, skipped.\n", *nberr,(int)moisdc[i],(int)andc[i],num[i],i,s[m][i],m);
11257: s[m][i]=-1; /* Droping the death status */
11258: }
11259: if((int)moisdc[i]==99 && (int)andc[i]!=9999 && s[m][i]>nlstate){
11260: (*nberr)++;
11261: printf("Error (#%d)! Month of death of individual %ld on line %d was unknown (%2d) (year of death is %4d) and status is a death state %d at wave %d. Please impute an arbitrary (or not) month and rerun. Currently this transition to death will be skipped (status is set to -2).\nOther similar cases in log file\n", *nberr, num[i],i,(int)moisdc[i],(int)andc[i],s[m][i],m);
11262: fprintf(ficlog,"Error (#%d)! Month of death of individual %ld on line %d was unknown (%2d) (year of death is %4d) and status is a death state %d at wave %d. Please impute an arbitrary (or not) month and rerun. Currently this transition to death will be skipped (status is set to -2).\n", *nberr, num[i],i,(int)moisdc[i],(int)andc[i],s[m][i],m);
11263: s[m][i]=-2; /* We prefer to skip it (and to skip it in version 0.8a1 too */
11264: }
11265: }
11266: }
11267:
11268: for (i=1; i<=imx; i++) {
11269: agedc[i]=(moisdc[i]/12.+andc[i])-(moisnais[i]/12.+annais[i]);
11270: for(m=firstpass; (m<= lastpass); m++){
11271: 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 */
11272: if (s[m][i] >= nlstate+1) {
11273: if(agedc[i]>0){
11274: if((int)moisdc[i]!=99 && (int)andc[i]!=9999){
11275: agev[m][i]=agedc[i];
11276: /*if(moisdc[i]==99 && andc[i]==9999) s[m][i]=-1;*/
11277: }else {
11278: if ((int)andc[i]!=9999){
11279: nbwarn++;
11280: printf("Warning negative age at death: %ld line:%d\n",num[i],i);
11281: fprintf(ficlog,"Warning negative age at death: %ld line:%d\n",num[i],i);
11282: agev[m][i]=-1;
11283: }
11284: }
11285: } /* agedc > 0 */
11286: } /* end if */
11287: else if(s[m][i] !=9){ /* Standard case, age in fractional
11288: years but with the precision of a month */
11289: agev[m][i]=(mint[m][i]/12.+1./24.+anint[m][i])-(moisnais[i]/12.+1./24.+annais[i]);
11290: if((int)mint[m][i]==99 || (int)anint[m][i]==9999)
11291: agev[m][i]=1;
11292: else if(agev[m][i] < *agemin){
11293: *agemin=agev[m][i];
11294: printf(" Min anint[%d][%d]=%.2f annais[%d]=%.2f, agemin=%.2f\n",m,i,anint[m][i], i,annais[i], *agemin);
11295: }
11296: else if(agev[m][i] >*agemax){
11297: *agemax=agev[m][i];
11298: /* printf(" Max anint[%d][%d]=%.0f annais[%d]=%.0f, agemax=%.2f\n",m,i,anint[m][i], i,annais[i], *agemax);*/
11299: }
11300: /*agev[m][i]=anint[m][i]-annais[i];*/
11301: /* agev[m][i] = age[i]+2*m;*/
11302: } /* en if 9*/
11303: else { /* =9 */
11304: /* printf("Debug num[%d]=%ld s[%d][%d]=%d\n",i,num[i], m,i, s[m][i]); */
11305: agev[m][i]=1;
11306: s[m][i]=-1;
11307: }
11308: }
11309: else if(s[m][i]==0) /*= 0 Unknown */
11310: agev[m][i]=1;
11311: else{
11312: printf("Warning, num[%d]=%ld, s[%d][%d]=%d\n", i, num[i], m, i,s[m][i]);
11313: fprintf(ficlog, "Warning, num[%d]=%ld, s[%d][%d]=%d\n", i, num[i], m, i,s[m][i]);
11314: agev[m][i]=0;
11315: }
11316: } /* End for lastpass */
11317: }
11318:
11319: for (i=1; i<=imx; i++) {
11320: for(m=firstpass; (m<=lastpass); m++){
11321: if (s[m][i] > (nlstate+ndeath)) {
11322: (*nberr)++;
11323: 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);
11324: 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);
11325: return 1;
11326: }
11327: }
11328: }
11329:
11330: /*for (i=1; i<=imx; i++){
11331: for (m=firstpass; (m<lastpass); m++){
11332: printf("%ld %d %.lf %d %d\n", num[i],(covar[1][i]),agev[m][i],s[m][i],s[m+1][i]);
11333: }
11334:
11335: }*/
11336:
11337:
11338: printf("Total number of individuals= %d, Agemin = %.2f, Agemax= %.2f\n\n", imx, *agemin, *agemax);
11339: fprintf(ficlog,"Total number of individuals= %d, Agemin = %.2f, Agemax= %.2f\n\n", imx, *agemin, *agemax);
11340:
11341: return (0);
11342: /* endread:*/
11343: printf("Exiting calandcheckages: ");
11344: return (1);
11345: }
11346:
11347: #if defined(_MSC_VER)
11348: /*printf("Visual C++ compiler: %s \n;", _MSC_FULL_VER);*/
11349: /*fprintf(ficlog, "Visual C++ compiler: %s \n;", _MSC_FULL_VER);*/
11350: //#include "stdafx.h"
11351: //#include <stdio.h>
11352: //#include <tchar.h>
11353: //#include <windows.h>
11354: //#include <iostream>
11355: typedef BOOL(WINAPI *LPFN_ISWOW64PROCESS) (HANDLE, PBOOL);
11356:
11357: LPFN_ISWOW64PROCESS fnIsWow64Process;
11358:
11359: BOOL IsWow64()
11360: {
11361: BOOL bIsWow64 = FALSE;
11362:
11363: //typedef BOOL (APIENTRY *LPFN_ISWOW64PROCESS)
11364: // (HANDLE, PBOOL);
11365:
11366: //LPFN_ISWOW64PROCESS fnIsWow64Process;
11367:
11368: HMODULE module = GetModuleHandle(_T("kernel32"));
11369: const char funcName[] = "IsWow64Process";
11370: fnIsWow64Process = (LPFN_ISWOW64PROCESS)
11371: GetProcAddress(module, funcName);
11372:
11373: if (NULL != fnIsWow64Process)
11374: {
11375: if (!fnIsWow64Process(GetCurrentProcess(),
11376: &bIsWow64))
11377: //throw std::exception("Unknown error");
11378: printf("Unknown error\n");
11379: }
11380: return bIsWow64 != FALSE;
11381: }
11382: #endif
11383:
11384: void syscompilerinfo(int logged)
11385: {
11386: #include <stdint.h>
11387:
11388: /* #include "syscompilerinfo.h"*/
11389: /* command line Intel compiler 32bit windows, XP compatible:*/
11390: /* /GS /W3 /Gy
11391: /Zc:wchar_t /Zi /O2 /Fd"Release\vc120.pdb" /D "WIN32" /D "NDEBUG" /D
11392: "_CONSOLE" /D "_LIB" /D "_USING_V110_SDK71_" /D "_UNICODE" /D
11393: "UNICODE" /Qipo /Zc:forScope /Gd /Oi /MT /Fa"Release\" /EHsc /nologo
11394: /Fo"Release\" /Qprof-dir "Release\" /Fp"Release\IMaCh.pch"
11395: */
11396: /* 64 bits */
11397: /*
11398: /GS /W3 /Gy
11399: /Zc:wchar_t /Zi /O2 /Fd"x64\Release\vc120.pdb" /D "WIN32" /D "NDEBUG"
11400: /D "_CONSOLE" /D "_LIB" /D "_UNICODE" /D "UNICODE" /Qipo /Zc:forScope
11401: /Oi /MD /Fa"x64\Release\" /EHsc /nologo /Fo"x64\Release\" /Qprof-dir
11402: "x64\Release\" /Fp"x64\Release\IMaCh.pch" */
11403: /* Optimization are useless and O3 is slower than O2 */
11404: /*
11405: /GS /W3 /Gy /Zc:wchar_t /Zi /O3 /Fd"x64\Release\vc120.pdb" /D "WIN32"
11406: /D "NDEBUG" /D "_CONSOLE" /D "_LIB" /D "_UNICODE" /D "UNICODE" /Qipo
11407: /Zc:forScope /Oi /MD /Fa"x64\Release\" /EHsc /nologo /Qparallel
11408: /Fo"x64\Release\" /Qprof-dir "x64\Release\" /Fp"x64\Release\IMaCh.pch"
11409: */
11410: /* Link is */ /* /OUT:"visual studio
11411: 2013\Projects\IMaCh\Release\IMaCh.exe" /MANIFEST /NXCOMPAT
11412: /PDB:"visual studio
11413: 2013\Projects\IMaCh\Release\IMaCh.pdb" /DYNAMICBASE
11414: "kernel32.lib" "user32.lib" "gdi32.lib" "winspool.lib"
11415: "comdlg32.lib" "advapi32.lib" "shell32.lib" "ole32.lib"
11416: "oleaut32.lib" "uuid.lib" "odbc32.lib" "odbccp32.lib"
11417: /MACHINE:X86 /OPT:REF /SAFESEH /INCREMENTAL:NO
11418: /SUBSYSTEM:CONSOLE",5.01" /MANIFESTUAC:"level='asInvoker'
11419: uiAccess='false'"
11420: /ManifestFile:"Release\IMaCh.exe.intermediate.manifest" /OPT:ICF
11421: /NOLOGO /TLBID:1
11422: */
11423:
11424:
11425: #if defined __INTEL_COMPILER
11426: #if defined(__GNUC__)
11427: struct utsname sysInfo; /* For Intel on Linux and OS/X */
11428: #endif
11429: #elif defined(__GNUC__)
11430: #ifndef __APPLE__
11431: #include <gnu/libc-version.h> /* Only on gnu */
11432: #endif
11433: struct utsname sysInfo;
11434: int cross = CROSS;
11435: if (cross){
11436: printf("Cross-");
11437: if(logged) fprintf(ficlog, "Cross-");
11438: }
11439: #endif
11440:
11441: printf("Compiled with:");if(logged)fprintf(ficlog,"Compiled with:");
11442: #if defined(__clang__)
11443: printf(" Clang/LLVM");if(logged)fprintf(ficlog," Clang/LLVM"); /* Clang/LLVM. ---------------------------------------------- */
11444: #endif
11445: #if defined(__ICC) || defined(__INTEL_COMPILER)
11446: printf(" Intel ICC/ICPC");if(logged)fprintf(ficlog," Intel ICC/ICPC");/* Intel ICC/ICPC. ------------------------------------------ */
11447: #endif
11448: #if defined(__GNUC__) || defined(__GNUG__)
11449: printf(" GNU GCC/G++");if(logged)fprintf(ficlog," GNU GCC/G++");/* GNU GCC/G++. --------------------------------------------- */
11450: #endif
11451: #if defined(__HP_cc) || defined(__HP_aCC)
11452: printf(" Hewlett-Packard C/aC++");if(logged)fprintf(fcilog," Hewlett-Packard C/aC++"); /* Hewlett-Packard C/aC++. ---------------------------------- */
11453: #endif
11454: #if defined(__IBMC__) || defined(__IBMCPP__)
11455: printf(" IBM XL C/C++"); if(logged) fprintf(ficlog," IBM XL C/C++");/* IBM XL C/C++. -------------------------------------------- */
11456: #endif
11457: #if defined(_MSC_VER)
11458: printf(" Microsoft Visual Studio");if(logged)fprintf(ficlog," Microsoft Visual Studio");/* Microsoft Visual Studio. --------------------------------- */
11459: #endif
11460: #if defined(__PGI)
11461: printf(" Portland Group PGCC/PGCPP");if(logged) fprintf(ficlog," Portland Group PGCC/PGCPP");/* Portland Group PGCC/PGCPP. ------------------------------- */
11462: #endif
11463: #if defined(__SUNPRO_C) || defined(__SUNPRO_CC)
11464: printf(" Oracle Solaris Studio");if(logged)fprintf(ficlog," Oracle Solaris Studio\n");/* Oracle Solaris Studio. ----------------------------------- */
11465: #endif
11466: printf(" for "); if (logged) fprintf(ficlog, " for ");
11467:
11468: // http://stackoverflow.com/questions/4605842/how-to-identify-platform-compiler-from-preprocessor-macros
11469: #ifdef _WIN32 // note the underscore: without it, it's not msdn official!
11470: // Windows (x64 and x86)
11471: printf("Windows (x64 and x86) ");if(logged) fprintf(ficlog,"Windows (x64 and x86) ");
11472: #elif __unix__ // all unices, not all compilers
11473: // Unix
11474: printf("Unix ");if(logged) fprintf(ficlog,"Unix ");
11475: #elif __linux__
11476: // linux
11477: printf("linux ");if(logged) fprintf(ficlog,"linux ");
11478: #elif __APPLE__
11479: // Mac OS, not sure if this is covered by __posix__ and/or __unix__ though..
11480: printf("Mac OS ");if(logged) fprintf(ficlog,"Mac OS ");
11481: #endif
11482:
11483: /* __MINGW32__ */
11484: /* __CYGWIN__ */
11485: /* __MINGW64__ */
11486: // http://msdn.microsoft.com/en-us/library/b0084kay.aspx
11487: /* _MSC_VER //the Visual C++ compiler is 17.00.51106.1, the _MSC_VER macro evaluates to 1700. Type cl /? */
11488: /* _MSC_FULL_VER //the Visual C++ compiler is 15.00.20706.01, the _MSC_FULL_VER macro evaluates to 150020706 */
11489: /* _WIN64 // Defined for applications for Win64. */
11490: /* _M_X64 // Defined for compilations that target x64 processors. */
11491: /* _DEBUG // Defined when you compile with /LDd, /MDd, and /MTd. */
11492:
11493: #if UINTPTR_MAX == 0xffffffff
11494: printf(" 32-bit"); if(logged) fprintf(ficlog," 32-bit");/* 32-bit */
11495: #elif UINTPTR_MAX == 0xffffffffffffffff
11496: printf(" 64-bit"); if(logged) fprintf(ficlog," 64-bit");/* 64-bit */
11497: #else
11498: printf(" wtf-bit"); if(logged) fprintf(ficlog," wtf-bit");/* wtf */
11499: #endif
11500:
11501: #if defined(__GNUC__)
11502: # if defined(__GNUC_PATCHLEVEL__)
11503: # define __GNUC_VERSION__ (__GNUC__ * 10000 \
11504: + __GNUC_MINOR__ * 100 \
11505: + __GNUC_PATCHLEVEL__)
11506: # else
11507: # define __GNUC_VERSION__ (__GNUC__ * 10000 \
11508: + __GNUC_MINOR__ * 100)
11509: # endif
11510: printf(" using GNU C version %d.\n", __GNUC_VERSION__);
11511: if(logged) fprintf(ficlog, " using GNU C version %d.\n", __GNUC_VERSION__);
11512:
11513: if (uname(&sysInfo) != -1) {
11514: printf("Running on: %s %s %s %s %s\n",sysInfo.sysname, sysInfo.nodename, sysInfo.release, sysInfo.version, sysInfo.machine);
11515: if(logged) fprintf(ficlog,"Running on: %s %s %s %s %s\n ",sysInfo.sysname, sysInfo.nodename, sysInfo.release, sysInfo.version, sysInfo.machine);
11516: }
11517: else
11518: perror("uname() error");
11519: //#ifndef __INTEL_COMPILER
11520: #if !defined (__INTEL_COMPILER) && !defined(__APPLE__)
11521: printf("GNU libc version: %s\n", gnu_get_libc_version());
11522: if(logged) fprintf(ficlog,"GNU libc version: %s\n", gnu_get_libc_version());
11523: #endif
11524: #endif
11525:
11526: // void main ()
11527: // {
11528: #if defined(_MSC_VER)
11529: if (IsWow64()){
11530: printf("\nThe program (probably compiled for 32bit) is running under WOW64 (64bit) emulation.\n");
11531: if (logged) fprintf(ficlog, "\nThe program (probably compiled for 32bit) is running under WOW64 (64bit) emulation.\n");
11532: }
11533: else{
11534: printf("\nThe program is not running under WOW64 (i.e probably on a 64bit Windows).\n");
11535: if (logged) fprintf(ficlog, "\nThe programm is not running under WOW64 (i.e probably on a 64bit Windows).\n");
11536: }
11537: // printf("\nPress Enter to continue...");
11538: // getchar();
11539: // }
11540:
11541: #endif
11542:
11543:
11544: }
11545:
11546: int prevalence_limit(double *p, double **prlim, double ageminpar, double agemaxpar, double ftolpl, int *ncvyearp){
11547: /*--------------- Prevalence limit (forward period or forward stable prevalence) --------------*/
11548: /* Computes the prevalence limit for each combination of the dummy covariates */
11549: int i, j, k, i1, k4=0, nres=0 ;
11550: /* double ftolpl = 1.e-10; */
11551: double age, agebase, agelim;
11552: double tot;
11553:
11554: strcpy(filerespl,"PL_");
11555: strcat(filerespl,fileresu);
11556: if((ficrespl=fopen(filerespl,"w"))==NULL) {
11557: printf("Problem with forward period (stable) prevalence resultfile: %s\n", filerespl);return 1;
11558: fprintf(ficlog,"Problem with forward period (stable) prevalence resultfile: %s\n", filerespl);return 1;
11559: }
11560: printf("\nComputing forward period (stable) prevalence: result on file '%s' \n", filerespl);
11561: fprintf(ficlog,"\nComputing forward period (stable) prevalence: result on file '%s' \n", filerespl);
11562: pstamp(ficrespl);
11563: fprintf(ficrespl,"# Forward period (stable) prevalence. Precision given by ftolpl=%g \n", ftolpl);
11564: fprintf(ficrespl,"#Age ");
11565: for(i=1; i<=nlstate;i++) fprintf(ficrespl,"%d-%d ",i,i);
11566: fprintf(ficrespl,"\n");
11567:
11568: /* prlim=matrix(1,nlstate,1,nlstate);*/ /* back in main */
11569:
11570: agebase=ageminpar;
11571: agelim=agemaxpar;
11572:
11573: /* i1=pow(2,ncoveff); */
11574: i1=pow(2,cptcoveff); /* Number of combination of dummy covariates */
11575: if (cptcovn < 1){i1=1;}
11576:
11577: /* for(k=1; k<=i1;k++){ /\* For each combination k of dummy covariates in the model *\/ */
11578: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
11579: k=TKresult[nres];
11580: if(TKresult[nres]==0) k=1; /* To be checked for noresult */
11581: /* if(i1 != 1 && TKresult[nres]!= k) /\* We found the combination k corresponding to the resultline value of dummies *\/ */
11582: /* continue; */
11583:
11584: /* for(cptcov=1,k=0;cptcov<=i1;cptcov++){ */
11585: /* for(cptcov=1,k=0;cptcov<=1;cptcov++){ */
11586: //for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){
11587: /* k=k+1; */
11588: /* to clean */
11589: /*printf("cptcov=%d cptcod=%d codtab=%d\n",cptcov, cptcod,codtabm(cptcod,cptcov));*/
11590: fprintf(ficrespl,"#******");
11591: printf("#******");
11592: fprintf(ficlog,"#******");
11593: for(j=1;j<=cptcovs ;j++) {/**< cptcovs number of SIMPLE covariates in the model or resultline V2+V1 =2 (dummy or quantit or time varying) */
11594: /* fprintf(ficrespl," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,Tvaraff[j])]); /\* Here problem for varying dummy*\/ */
11595: /* printf(" V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
11596: /* fprintf(ficlog," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
11597: fprintf(ficrespl," V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
11598: printf(" V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
11599: fprintf(ficlog," V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
11600: }
11601: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
11602: /* printf(" V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); */
11603: /* fprintf(ficrespl," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); */
11604: /* fprintf(ficlog," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); */
11605: /* } */
11606: fprintf(ficrespl,"******\n");
11607: printf("******\n");
11608: fprintf(ficlog,"******\n");
11609: if(invalidvarcomb[k]){
11610: printf("\nCombination (%d) ignored because no case \n",k);
11611: fprintf(ficrespl,"#Combination (%d) ignored because no case \n",k);
11612: fprintf(ficlog,"\nCombination (%d) ignored because no case \n",k);
11613: continue;
11614: }
11615:
11616: fprintf(ficrespl,"#Age ");
11617: /* for(j=1;j<=cptcoveff;j++) { */
11618: /* fprintf(ficrespl,"V%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
11619: /* } */
11620: for(j=1;j<=cptcovs;j++) { /* New the quanti variable is added */
11621: fprintf(ficrespl,"V%d %lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
11622: }
11623: for(i=1; i<=nlstate;i++) fprintf(ficrespl," %d-%d ",i,i);
11624: fprintf(ficrespl,"Total Years_to_converge\n");
11625:
11626: for (age=agebase; age<=agelim; age++){
11627: /* for (age=agebase; age<=agebase; age++){ */
11628: /**< Computes the prevalence limit in each live state at age x and for covariate combination (k and) nres */
11629: prevalim(prlim, nlstate, p, age, oldm, savm, ftolpl, ncvyearp, k, nres); /* Nicely done */
11630: fprintf(ficrespl,"%.0f ",age );
11631: /* for(j=1;j<=cptcoveff;j++) */
11632: /* fprintf(ficrespl,"%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
11633: for(j=1;j<=cptcovs;j++)
11634: fprintf(ficrespl,"%d %lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
11635: tot=0.;
11636: for(i=1; i<=nlstate;i++){
11637: tot += prlim[i][i];
11638: fprintf(ficrespl," %.5f", prlim[i][i]);
11639: }
11640: fprintf(ficrespl," %.3f %d\n", tot, *ncvyearp);
11641: } /* Age */
11642: /* was end of cptcod */
11643: } /* nres */
11644: /* } /\* for each combination *\/ */
11645: return 0;
11646: }
11647:
11648: 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){
11649: /*--------------- Back Prevalence limit (backward stable prevalence) --------------*/
11650:
11651: /* Computes the back prevalence limit for any combination of covariate values
11652: * at any age between ageminpar and agemaxpar
11653: */
11654: int i, j, k, i1, nres=0 ;
11655: /* double ftolpl = 1.e-10; */
11656: double age, agebase, agelim;
11657: double tot;
11658: /* double ***mobaverage; */
11659: /* double **dnewm, **doldm, **dsavm; /\* for use *\/ */
11660:
11661: strcpy(fileresplb,"PLB_");
11662: strcat(fileresplb,fileresu);
11663: if((ficresplb=fopen(fileresplb,"w"))==NULL) {
11664: printf("Problem with backward prevalence resultfile: %s\n", fileresplb);return 1;
11665: fprintf(ficlog,"Problem with backward prevalence resultfile: %s\n", fileresplb);return 1;
11666: }
11667: printf("Computing backward prevalence: result on file '%s' \n", fileresplb);
11668: fprintf(ficlog,"Computing backward prevalence: result on file '%s' \n", fileresplb);
11669: pstamp(ficresplb);
11670: fprintf(ficresplb,"# Backward prevalence. Precision given by ftolpl=%g \n", ftolpl);
11671: fprintf(ficresplb,"#Age ");
11672: for(i=1; i<=nlstate;i++) fprintf(ficresplb,"%d-%d ",i,i);
11673: fprintf(ficresplb,"\n");
11674:
11675:
11676: /* prlim=matrix(1,nlstate,1,nlstate);*/ /* back in main */
11677:
11678: agebase=ageminpar;
11679: agelim=agemaxpar;
11680:
11681:
11682: i1=pow(2,cptcoveff);
11683: if (cptcovn < 1){i1=1;}
11684:
11685: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
11686: /* for(k=1; k<=i1;k++){ /\* For any combination of dummy covariates, fixed and varying *\/ */
11687: k=TKresult[nres];
11688: if(TKresult[nres]==0) k=1; /* To be checked for noresult */
11689: /* if(i1 != 1 && TKresult[nres]!= k) */
11690: /* continue; */
11691: /* /\*printf("cptcov=%d cptcod=%d codtab=%d\n",cptcov, cptcod,codtabm(cptcod,cptcov));*\/ */
11692: fprintf(ficresplb,"#******");
11693: printf("#******");
11694: fprintf(ficlog,"#******");
11695: for(j=1;j<=cptcovs ;j++) {/**< cptcovs number of SIMPLE covariates in the model or resultline V2+V1 =2 (dummy or quantit or time varying) */
11696: printf(" V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
11697: fprintf(ficresplb," V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
11698: fprintf(ficlog," V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
11699: }
11700: /* for(j=1;j<=cptcoveff ;j++) {/\* all covariates *\/ */
11701: /* fprintf(ficresplb," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
11702: /* printf(" V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
11703: /* fprintf(ficlog," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
11704: /* } */
11705: /* for (j=1; j<= nsq; j++){ /\* For each selected (single) quantitative value *\/ */
11706: /* printf(" V%d=%f ",Tvqresult[nres][j],Tqresult[nres][resultmodel[nres][j]]); */
11707: /* fprintf(ficresplb," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][resultmodel[nres][j]]); */
11708: /* fprintf(ficlog," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][resultmodel[nres][j]]); */
11709: /* } */
11710: fprintf(ficresplb,"******\n");
11711: printf("******\n");
11712: fprintf(ficlog,"******\n");
11713: if(invalidvarcomb[k]){
11714: printf("\nCombination (%d) ignored because no cases \n",k);
11715: fprintf(ficresplb,"#Combination (%d) ignored because no cases \n",k);
11716: fprintf(ficlog,"\nCombination (%d) ignored because no cases \n",k);
11717: continue;
11718: }
11719:
11720: fprintf(ficresplb,"#Age ");
11721: for(j=1;j<=cptcovs;j++) {
11722: fprintf(ficresplb,"V%d %lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
11723: }
11724: for(i=1; i<=nlstate;i++) fprintf(ficresplb," %d-%d ",i,i);
11725: fprintf(ficresplb,"Total Years_to_converge\n");
11726:
11727:
11728: for (age=agebase; age<=agelim; age++){
11729: /* for (age=agebase; age<=agebase; age++){ */
11730: if(mobilavproj > 0){
11731: /* bprevalim(bprlim, mobaverage, nlstate, p, age, ageminpar, agemaxpar, oldm, savm, doldm, dsavm, ftolpl, ncvyearp, k); */
11732: /* bprevalim(bprlim, mobaverage, nlstate, p, age, oldm, savm, dnewm, doldm, dsavm, ftolpl, ncvyearp, k); */
11733: bprevalim(bprlim, mobaverage, nlstate, p, age, ftolpl, ncvyearp, k, nres);
11734: }else if (mobilavproj == 0){
11735: 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);
11736: 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);
11737: exit(1);
11738: }else{
11739: /* bprevalim(bprlim, probs, nlstate, p, age, oldm, savm, dnewm, doldm, dsavm, ftolpl, ncvyearp, k); */
11740: bprevalim(bprlim, probs, nlstate, p, age, ftolpl, ncvyearp, k,nres);
11741: /* printf("TOTOT\n"); */
11742: /* exit(1); */
11743: }
11744: fprintf(ficresplb,"%.0f ",age );
11745: for(j=1;j<=cptcovs;j++)
11746: fprintf(ficresplb,"%d %lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
11747: tot=0.;
11748: for(i=1; i<=nlstate;i++){
11749: tot += bprlim[i][i];
11750: fprintf(ficresplb," %.5f", bprlim[i][i]);
11751: }
11752: fprintf(ficresplb," %.3f %d\n", tot, *ncvyearp);
11753: } /* Age */
11754: /* was end of cptcod */
11755: /*fprintf(ficresplb,"\n");*/ /* Seems to be necessary for gnuplot only if two result lines and no covariate. */
11756: /* } /\* end of any combination *\/ */
11757: } /* end of nres */
11758: /* hBijx(p, bage, fage); */
11759: /* fclose(ficrespijb); */
11760:
11761: return 0;
11762: }
11763:
11764: int hPijx(double *p, int bage, int fage){
11765: /*------------- h Pij x at various ages ------------*/
11766: /* to be optimized with precov */
11767: int stepsize;
11768: int agelim;
11769: int hstepm;
11770: int nhstepm;
11771: int h, i, i1, j, k, k4, nres=0;
11772:
11773: double agedeb;
11774: double ***p3mat;
11775:
11776: strcpy(filerespij,"PIJ_"); strcat(filerespij,fileresu);
11777: if((ficrespij=fopen(filerespij,"w"))==NULL) {
11778: printf("Problem with Pij resultfile: %s\n", filerespij); return 1;
11779: fprintf(ficlog,"Problem with Pij resultfile: %s\n", filerespij); return 1;
11780: }
11781: printf("Computing pij: result on file '%s' \n", filerespij);
11782: fprintf(ficlog,"Computing pij: result on file '%s' \n", filerespij);
11783:
11784: stepsize=(int) (stepm+YEARM-1)/YEARM;
11785: /*if (stepm<=24) stepsize=2;*/
11786:
11787: agelim=AGESUP;
11788: hstepm=stepsize*YEARM; /* Every year of age */
11789: hstepm=hstepm/stepm; /* Typically 2 years, = 2/6 months = 4 */
11790:
11791: /* hstepm=1; aff par mois*/
11792: pstamp(ficrespij);
11793: fprintf(ficrespij,"#****** h Pij x Probability to be in state j at age x+h being in i at x ");
11794: i1= pow(2,cptcoveff);
11795: /* for(cptcov=1,k=0;cptcov<=i1;cptcov++){ */
11796: /* /\*for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){*\/ */
11797: /* k=k+1; */
11798: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
11799: k=TKresult[nres];
11800: if(TKresult[nres]==0) k=1; /* To be checked for noresult */
11801: /* for(k=1; k<=i1;k++){ */
11802: /* if(i1 != 1 && TKresult[nres]!= k) */
11803: /* continue; */
11804: fprintf(ficrespij,"\n#****** ");
11805: for(j=1;j<=cptcovs;j++){
11806: fprintf(ficrespij," V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
11807: /* fprintf(ficrespij,"@wV%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
11808: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
11809: /* printf(" V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); */
11810: /* fprintf(ficrespij," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); */
11811: }
11812: fprintf(ficrespij,"******\n");
11813:
11814: for (agedeb=fage; agedeb>=bage; agedeb--){ /* If stepm=6 months */
11815: nhstepm=(int) rint((agelim-agedeb)*YEARM/stepm); /* Typically 20 years = 20*12/6=40 */
11816: nhstepm = nhstepm/hstepm; /* Typically 40/4=10 */
11817:
11818: /* nhstepm=nhstepm*YEARM; aff par mois*/
11819:
11820: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
11821: oldm=oldms;savm=savms;
11822: hpxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k, nres);
11823: fprintf(ficrespij,"# Cov Agex agex+h hpijx with i,j=");
11824: for(i=1; i<=nlstate;i++)
11825: for(j=1; j<=nlstate+ndeath;j++)
11826: fprintf(ficrespij," %1d-%1d",i,j);
11827: fprintf(ficrespij,"\n");
11828: for (h=0; h<=nhstepm; h++){
11829: /*agedebphstep = agedeb + h*hstepm/YEARM*stepm;*/
11830: fprintf(ficrespij,"%d %3.f %3.f",k, agedeb, agedeb + h*hstepm/YEARM*stepm );
11831: for(i=1; i<=nlstate;i++)
11832: for(j=1; j<=nlstate+ndeath;j++)
11833: fprintf(ficrespij," %.5f", p3mat[i][j][h]);
11834: fprintf(ficrespij,"\n");
11835: }
11836: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
11837: fprintf(ficrespij,"\n");
11838: }
11839: }
11840: /*}*/
11841: return 0;
11842: }
11843:
11844: int hBijx(double *p, int bage, int fage, double ***prevacurrent){
11845: /*------------- h Bij x at various ages ------------*/
11846: /* To be optimized with precov */
11847: int stepsize;
11848: /* int agelim; */
11849: int ageminl;
11850: int hstepm;
11851: int nhstepm;
11852: int h, i, i1, j, k, nres;
11853:
11854: double agedeb;
11855: double ***p3mat;
11856:
11857: strcpy(filerespijb,"PIJB_"); strcat(filerespijb,fileresu);
11858: if((ficrespijb=fopen(filerespijb,"w"))==NULL) {
11859: printf("Problem with Pij back resultfile: %s\n", filerespijb); return 1;
11860: fprintf(ficlog,"Problem with Pij back resultfile: %s\n", filerespijb); return 1;
11861: }
11862: printf("Computing pij back: result on file '%s' \n", filerespijb);
11863: fprintf(ficlog,"Computing pij back: result on file '%s' \n", filerespijb);
11864:
11865: stepsize=(int) (stepm+YEARM-1)/YEARM;
11866: /*if (stepm<=24) stepsize=2;*/
11867:
11868: /* agelim=AGESUP; */
11869: ageminl=AGEINF; /* was 30 */
11870: hstepm=stepsize*YEARM; /* Every year of age */
11871: hstepm=hstepm/stepm; /* Typically 2 years, = 2/6 months = 4 */
11872:
11873: /* hstepm=1; aff par mois*/
11874: pstamp(ficrespijb);
11875: fprintf(ficrespijb,"#****** h Bij x Back probability to be in state i at age x-h being in j at x: B1j+B2j+...=1 ");
11876: i1= pow(2,cptcoveff);
11877: /* for(cptcov=1,k=0;cptcov<=i1;cptcov++){ */
11878: /* /\*for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){*\/ */
11879: /* k=k+1; */
11880: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
11881: k=TKresult[nres];
11882: if(TKresult[nres]==0) k=1; /* To be checked for noresult */
11883: /* for(k=1; k<=i1;k++){ /\* For any combination of dummy covariates, fixed and varying *\/ */
11884: /* if(i1 != 1 && TKresult[nres]!= k) */
11885: /* continue; */
11886: fprintf(ficrespijb,"\n#****** ");
11887: for(j=1;j<=cptcovs;j++){
11888: fprintf(ficrespijb," V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
11889: /* for(j=1;j<=cptcoveff;j++) */
11890: /* fprintf(ficrespijb,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
11891: /* for (j=1; j<= nsq; j++){ /\* For each selected (single) quantitative value *\/ */
11892: /* fprintf(ficrespijb," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][resultmodel[nres][j]]); */
11893: }
11894: fprintf(ficrespijb,"******\n");
11895: if(invalidvarcomb[k]){ /* Is it necessary here? */
11896: fprintf(ficrespijb,"\n#Combination (%d) ignored because no cases \n",k);
11897: continue;
11898: }
11899:
11900: /* for (agedeb=fage; agedeb>=bage; agedeb--){ /\* If stepm=6 months *\/ */
11901: for (agedeb=bage; agedeb<=fage; agedeb++){ /* If stepm=6 months and estepm=24 (2 years) */
11902: /* nhstepm=(int) rint((agelim-agedeb)*YEARM/stepm); /\* Typically 20 years = 20*12/6=40 *\/ */
11903: nhstepm=(int) rint((agedeb-ageminl)*YEARM/stepm+0.1)-1; /* Typically 20 years = 20*12/6=40 or 55*12/24=27.5-1.1=>27 */
11904: nhstepm = nhstepm/hstepm; /* Typically 40/4=10, because estepm=24 stepm=6 => hstepm=24/6=4 or 28*/
11905:
11906: /* nhstepm=nhstepm*YEARM; aff par mois*/
11907:
11908: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); /* We can't have it at an upper level because of nhstepm */
11909: /* and memory limitations if stepm is small */
11910:
11911: /* oldm=oldms;savm=savms; */
11912: /* hbxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k); */
11913: hbxij(p3mat,nhstepm,agedeb,hstepm,p,prevacurrent,nlstate,stepm, k, nres);/* Bug valgrind */
11914: /* hbxij(p3mat,nhstepm,agedeb,hstepm,p,prevacurrent,nlstate,stepm,oldm,savm, dnewm, doldm, dsavm, k); */
11915: fprintf(ficrespijb,"# Cov Agex agex-h hbijx with i,j=");
11916: for(i=1; i<=nlstate;i++)
11917: for(j=1; j<=nlstate+ndeath;j++)
11918: fprintf(ficrespijb," %1d-%1d",i,j);
11919: fprintf(ficrespijb,"\n");
11920: for (h=0; h<=nhstepm; h++){
11921: /*agedebphstep = agedeb + h*hstepm/YEARM*stepm;*/
11922: fprintf(ficrespijb,"%d %3.f %3.f",k, agedeb, agedeb - h*hstepm/YEARM*stepm );
11923: /* fprintf(ficrespijb,"%d %3.f %3.f",k, agedeb, agedeb + h*hstepm/YEARM*stepm ); */
11924: for(i=1; i<=nlstate;i++)
11925: for(j=1; j<=nlstate+ndeath;j++)
11926: fprintf(ficrespijb," %.5f", p3mat[i][j][h]);/* Bug valgrind */
11927: fprintf(ficrespijb,"\n");
11928: }
11929: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
11930: fprintf(ficrespijb,"\n");
11931: } /* end age deb */
11932: /* } /\* end combination *\/ */
11933: } /* end nres */
11934: return 0;
11935: } /* hBijx */
11936:
11937:
11938: /***********************************************/
11939: /**************** Main Program *****************/
11940: /***********************************************/
11941:
11942: int main(int argc, char *argv[])
11943: {
11944: #ifdef GSL
11945: const gsl_multimin_fminimizer_type *T;
11946: size_t iteri = 0, it;
11947: int rval = GSL_CONTINUE;
11948: int status = GSL_SUCCESS;
11949: double ssval;
11950: #endif
11951: int movingaverage(double ***probs, double bage,double fage, double ***mobaverage, int mobilav);
11952: int i,j, k, iter=0,m,size=100, cptcod; /* Suppressing because nobs */
11953: /* int i,j, k, n=MAXN,iter=0,m,size=100, cptcod; */
11954: int ncvyear=0; /* Number of years needed for the period prevalence to converge */
11955: int jj, ll, li, lj, lk;
11956: int numlinepar=0; /* Current linenumber of parameter file */
11957: int num_filled;
11958: int itimes;
11959: int NDIM=2;
11960: int vpopbased=0;
11961: int nres=0;
11962: int endishere=0;
11963: int noffset=0;
11964: int ncurrv=0; /* Temporary variable */
11965:
11966: char ca[32], cb[32];
11967: /* FILE *fichtm; *//* Html File */
11968: /* FILE *ficgp;*/ /*Gnuplot File */
11969: struct stat info;
11970: double agedeb=0.;
11971:
11972: double ageminpar=AGEOVERFLOW,agemin=AGEOVERFLOW, agemaxpar=-AGEOVERFLOW, agemax=-AGEOVERFLOW;
11973: double ageminout=-AGEOVERFLOW,agemaxout=AGEOVERFLOW; /* Smaller Age range redefined after movingaverage */
11974:
11975: double fret;
11976: double dum=0.; /* Dummy variable */
11977: double ***p3mat;
11978: /* double ***mobaverage; */
11979: double wald;
11980:
11981: char line[MAXLINE];
11982: char path[MAXLINE],pathc[MAXLINE],pathcd[MAXLINE],pathtot[MAXLINE];
11983:
11984: char modeltemp[MAXLINE];
11985: char resultline[MAXLINE], resultlineori[MAXLINE];
11986:
11987: char pathr[MAXLINE], pathimach[MAXLINE];
11988: char *tok, *val; /* pathtot */
11989: /* int firstobs=1, lastobs=10; /\* nobs = lastobs-firstobs declared globally ;*\/ */
11990: int c, h , cpt, c2;
11991: int jl=0;
11992: int i1, j1, jk, stepsize=0;
11993: int count=0;
11994:
11995: int *tab;
11996: int mobilavproj=0 , prevfcast=0 ; /* moving average of prev, If prevfcast=1 prevalence projection */
11997: /* double anprojd, mprojd, jprojd; /\* For eventual projections *\/ */
11998: /* double anprojf, mprojf, jprojf; */
11999: /* double jintmean,mintmean,aintmean; */
12000: int prvforecast = 0; /* Might be 1 (date of beginning of projection is a choice or 2 is the dateintmean */
12001: int prvbackcast = 0; /* Might be 1 (date of beginning of projection is a choice or 2 is the dateintmean */
12002: double yrfproj= 10.0; /* Number of years of forward projections */
12003: double yrbproj= 10.0; /* Number of years of backward projections */
12004: int prevbcast=0; /* defined as global for mlikeli and mle, replacing backcast */
12005: int mobilav=0,popforecast=0;
12006: int hstepm=0, nhstepm=0;
12007: int agemortsup;
12008: float sumlpop=0.;
12009: double jprev1=1, mprev1=1,anprev1=2000,jprev2=1, mprev2=1,anprev2=2000;
12010: double jpyram=1, mpyram=1,anpyram=2000,jpyram1=1, mpyram1=1,anpyram1=2000;
12011:
12012: double bage=0, fage=110., age, agelim=0., agebase=0.;
12013: double ftolpl=FTOL;
12014: double **prlim;
12015: double **bprlim;
12016: double ***param; /* Matrix of parameters, param[i][j][k] param=ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel)
12017: state of origin, state of destination including death, for each covariate: constante, age, and V1 V2 etc. */
12018: double ***paramstart; /* Matrix of starting parameter values */
12019: double *p, *pstart; /* p=param[1][1] pstart is for starting values guessed by freqsummary */
12020: double **matcov; /* Matrix of covariance */
12021: double **hess; /* Hessian matrix */
12022: double ***delti3; /* Scale */
12023: double *delti; /* Scale */
12024: double ***eij, ***vareij;
12025: double **varpl; /* Variances of prevalence limits by age */
12026:
12027: double *epj, vepp;
12028:
12029: double dateprev1, dateprev2;
12030: double jproj1=1,mproj1=1,anproj1=2000,jproj2=1,mproj2=1,anproj2=2000, dateproj1=0, dateproj2=0, dateprojd=0, dateprojf=0;
12031: double jback1=1,mback1=1,anback1=2000,jback2=1,mback2=1,anback2=2000, dateback1=0, dateback2=0, datebackd=0, datebackf=0;
12032:
12033:
12034: double **ximort;
12035: char *alph[]={"a","a","b","c","d","e"}, str[4]="1234";
12036: int *dcwave;
12037:
12038: char z[1]="c";
12039:
12040: /*char *strt;*/
12041: char strtend[80];
12042:
12043:
12044: /* setlocale (LC_ALL, ""); */
12045: /* bindtextdomain (PACKAGE, LOCALEDIR); */
12046: /* textdomain (PACKAGE); */
12047: /* setlocale (LC_CTYPE, ""); */
12048: /* setlocale (LC_MESSAGES, ""); */
12049:
12050: /* gettimeofday(&start_time, (struct timezone*)0); */ /* at first time */
12051: rstart_time = time(NULL);
12052: /* (void) gettimeofday(&start_time,&tzp);*/
12053: start_time = *localtime(&rstart_time);
12054: curr_time=start_time;
12055: /*tml = *localtime(&start_time.tm_sec);*/
12056: /* strcpy(strstart,asctime(&tml)); */
12057: strcpy(strstart,asctime(&start_time));
12058:
12059: /* printf("Localtime (at start)=%s",strstart); */
12060: /* tp.tm_sec = tp.tm_sec +86400; */
12061: /* tm = *localtime(&start_time.tm_sec); */
12062: /* tmg.tm_year=tmg.tm_year +dsign*dyear; */
12063: /* tmg.tm_mon=tmg.tm_mon +dsign*dmonth; */
12064: /* tmg.tm_hour=tmg.tm_hour + 1; */
12065: /* tp.tm_sec = mktime(&tmg); */
12066: /* strt=asctime(&tmg); */
12067: /* printf("Time(after) =%s",strstart); */
12068: /* (void) time (&time_value);
12069: * printf("time=%d,t-=%d\n",time_value,time_value-86400);
12070: * tm = *localtime(&time_value);
12071: * strstart=asctime(&tm);
12072: * printf("tim_value=%d,asctime=%s\n",time_value,strstart);
12073: */
12074:
12075: nberr=0; /* Number of errors and warnings */
12076: nbwarn=0;
12077: #ifdef WIN32
12078: _getcwd(pathcd, size);
12079: #else
12080: getcwd(pathcd, size);
12081: #endif
12082: syscompilerinfo(0);
12083: printf("\nIMaCh version %s, %s\n%s",version, copyright, fullversion);
12084: if(argc <=1){
12085: printf("\nEnter the parameter file name: ");
12086: if(!fgets(pathr,FILENAMELENGTH,stdin)){
12087: printf("ERROR Empty parameter file name\n");
12088: goto end;
12089: }
12090: i=strlen(pathr);
12091: if(pathr[i-1]=='\n')
12092: pathr[i-1]='\0';
12093: i=strlen(pathr);
12094: if(i >= 1 && pathr[i-1]==' ') {/* This may happen when dragging on oS/X! */
12095: pathr[i-1]='\0';
12096: }
12097: i=strlen(pathr);
12098: if( i==0 ){
12099: printf("ERROR Empty parameter file name\n");
12100: goto end;
12101: }
12102: for (tok = pathr; tok != NULL; ){
12103: printf("Pathr |%s|\n",pathr);
12104: while ((val = strsep(&tok, "\"" )) != NULL && *val == '\0');
12105: printf("val= |%s| pathr=%s\n",val,pathr);
12106: strcpy (pathtot, val);
12107: if(pathr[0] == '\0') break; /* Dirty */
12108: }
12109: }
12110: else if (argc<=2){
12111: strcpy(pathtot,argv[1]);
12112: }
12113: else{
12114: strcpy(pathtot,argv[1]);
12115: strcpy(z,argv[2]);
12116: printf("\nargv[2]=%s z=%c\n",argv[2],z[0]);
12117: }
12118: /*if(getcwd(pathcd, MAXLINE)!= NULL)printf ("Error pathcd\n");*/
12119: /*cygwin_split_path(pathtot,path,optionfile);
12120: printf("pathtot=%s, path=%s, optionfile=%s\n",pathtot,path,optionfile);*/
12121: /* cutv(path,optionfile,pathtot,'\\');*/
12122:
12123: /* Split argv[0], imach program to get pathimach */
12124: printf("\nargv[0]=%s argv[1]=%s, \n",argv[0],argv[1]);
12125: split(argv[0],pathimach,optionfile,optionfilext,optionfilefiname);
12126: printf("\nargv[0]=%s pathimach=%s, \noptionfile=%s \noptionfilext=%s \noptionfilefiname=%s\n",argv[0],pathimach,optionfile,optionfilext,optionfilefiname);
12127: /* strcpy(pathimach,argv[0]); */
12128: /* Split argv[1]=pathtot, parameter file name to get path, optionfile, extension and name */
12129: split(pathtot,path,optionfile,optionfilext,optionfilefiname);
12130: printf("\npathtot=%s,\npath=%s,\noptionfile=%s \noptionfilext=%s \noptionfilefiname=%s\n",pathtot,path,optionfile,optionfilext,optionfilefiname);
12131: #ifdef WIN32
12132: _chdir(path); /* Can be a relative path */
12133: if(_getcwd(pathcd,MAXLINE) > 0) /* So pathcd is the full path */
12134: #else
12135: chdir(path); /* Can be a relative path */
12136: if (getcwd(pathcd, MAXLINE) > 0) /* So pathcd is the full path */
12137: #endif
12138: printf("Current directory %s!\n",pathcd);
12139: strcpy(command,"mkdir ");
12140: strcat(command,optionfilefiname);
12141: if((outcmd=system(command)) != 0){
12142: printf("Directory already exists (or can't create it) %s%s, err=%d\n",path,optionfilefiname,outcmd);
12143: /* fprintf(ficlog,"Problem creating directory %s%s\n",path,optionfilefiname); */
12144: /* fclose(ficlog); */
12145: /* exit(1); */
12146: }
12147: /* if((imk=mkdir(optionfilefiname))<0){ */
12148: /* perror("mkdir"); */
12149: /* } */
12150:
12151: /*-------- arguments in the command line --------*/
12152:
12153: /* Main Log file */
12154: strcat(filelog, optionfilefiname);
12155: strcat(filelog,".log"); /* */
12156: if((ficlog=fopen(filelog,"w"))==NULL) {
12157: printf("Problem with logfile %s\n",filelog);
12158: goto end;
12159: }
12160: fprintf(ficlog,"Log filename:%s\n",filelog);
12161: fprintf(ficlog,"Version %s %s",version,fullversion);
12162: fprintf(ficlog,"\nEnter the parameter file name: \n");
12163: fprintf(ficlog,"pathimach=%s\npathtot=%s\n\
12164: path=%s \n\
12165: optionfile=%s\n\
12166: optionfilext=%s\n\
12167: optionfilefiname='%s'\n",pathimach,pathtot,path,optionfile,optionfilext,optionfilefiname);
12168:
12169: syscompilerinfo(1);
12170:
12171: printf("Local time (at start):%s",strstart);
12172: fprintf(ficlog,"Local time (at start): %s",strstart);
12173: fflush(ficlog);
12174: /* (void) gettimeofday(&curr_time,&tzp); */
12175: /* printf("Elapsed time %d\n", asc_diff_time(curr_time.tm_sec-start_time.tm_sec,tmpout)); */
12176:
12177: /* */
12178: strcpy(fileres,"r");
12179: strcat(fileres, optionfilefiname);
12180: strcat(fileresu, optionfilefiname); /* Without r in front */
12181: strcat(fileres,".txt"); /* Other files have txt extension */
12182: strcat(fileresu,".txt"); /* Other files have txt extension */
12183:
12184: /* Main ---------arguments file --------*/
12185:
12186: if((ficpar=fopen(optionfile,"r"))==NULL) {
12187: printf("Problem with optionfile '%s' with errno='%s'\n",optionfile,strerror(errno));
12188: fprintf(ficlog,"Problem with optionfile '%s' with errno='%s'\n",optionfile,strerror(errno));
12189: fflush(ficlog);
12190: /* goto end; */
12191: exit(70);
12192: }
12193:
12194: strcpy(filereso,"o");
12195: strcat(filereso,fileresu);
12196: if((ficparo=fopen(filereso,"w"))==NULL) { /* opened on subdirectory */
12197: printf("Problem with Output resultfile: %s\n", filereso);
12198: fprintf(ficlog,"Problem with Output resultfile: %s\n", filereso);
12199: fflush(ficlog);
12200: goto end;
12201: }
12202: /*-------- Rewriting parameter file ----------*/
12203: strcpy(rfileres,"r"); /* "Rparameterfile */
12204: strcat(rfileres,optionfilefiname); /* Parameter file first name */
12205: strcat(rfileres,"."); /* */
12206: strcat(rfileres,optionfilext); /* Other files have txt extension */
12207: if((ficres =fopen(rfileres,"w"))==NULL) {
12208: printf("Problem writing new parameter file: %s\n", rfileres);goto end;
12209: fprintf(ficlog,"Problem writing new parameter file: %s\n", rfileres);goto end;
12210: fflush(ficlog);
12211: goto end;
12212: }
12213: fprintf(ficres,"#IMaCh %s\n",version);
12214:
12215:
12216: /* Reads comments: lines beginning with '#' */
12217: numlinepar=0;
12218: /* Is it a BOM UTF-8 Windows file? */
12219: /* First parameter line */
12220: while(fgets(line, MAXLINE, ficpar)) {
12221: noffset=0;
12222: if( line[0] == (char)0xEF && line[1] == (char)0xBB) /* EF BB BF */
12223: {
12224: noffset=noffset+3;
12225: printf("# File is an UTF8 Bom.\n"); // 0xBF
12226: }
12227: /* else if( line[0] == (char)0xFE && line[1] == (char)0xFF)*/
12228: else if( line[0] == (char)0xFF && line[1] == (char)0xFE)
12229: {
12230: noffset=noffset+2;
12231: printf("# File is an UTF16BE BOM file\n");
12232: }
12233: else if( line[0] == 0 && line[1] == 0)
12234: {
12235: if( line[2] == (char)0xFE && line[3] == (char)0xFF){
12236: noffset=noffset+4;
12237: printf("# File is an UTF16BE BOM file\n");
12238: }
12239: } else{
12240: ;/*printf(" Not a BOM file\n");*/
12241: }
12242:
12243: /* If line starts with a # it is a comment */
12244: if (line[noffset] == '#') {
12245: numlinepar++;
12246: fputs(line,stdout);
12247: fputs(line,ficparo);
12248: fputs(line,ficres);
12249: fputs(line,ficlog);
12250: continue;
12251: }else
12252: break;
12253: }
12254: if((num_filled=sscanf(line,"title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\n", \
12255: title, datafile, &lastobs, &firstpass,&lastpass)) !=EOF){
12256: if (num_filled != 5) {
12257: printf("Should be 5 parameters\n");
12258: fprintf(ficlog,"Should be 5 parameters\n");
12259: }
12260: numlinepar++;
12261: printf("title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\n", title, datafile, lastobs, firstpass,lastpass);
12262: fprintf(ficparo,"title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\n", title, datafile, lastobs, firstpass,lastpass);
12263: fprintf(ficres,"title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\n", title, datafile, lastobs, firstpass,lastpass);
12264: fprintf(ficlog,"title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\n", title, datafile, lastobs, firstpass,lastpass);
12265: }
12266: /* Second parameter line */
12267: while(fgets(line, MAXLINE, ficpar)) {
12268: /* while(fscanf(ficpar,"%[^\n]", line)) { */
12269: /* If line starts with a # it is a comment. Strangely fgets reads the EOL and fputs doesn't */
12270: if (line[0] == '#') {
12271: numlinepar++;
12272: printf("%s",line);
12273: fprintf(ficres,"%s",line);
12274: fprintf(ficparo,"%s",line);
12275: fprintf(ficlog,"%s",line);
12276: continue;
12277: }else
12278: break;
12279: }
12280: 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", \
12281: &ftol, &stepm, &ncovcol, &nqv, &ntv, &nqtv, &nlstate, &ndeath, &maxwav, &mle, &weightopt)) !=EOF){
12282: if (num_filled != 11) {
12283: 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");
12284: printf("but line=%s\n",line);
12285: fprintf(ficlog,"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");
12286: fprintf(ficlog,"but line=%s\n",line);
12287: }
12288: if( lastpass > maxwav){
12289: printf("Error (lastpass = %d) > (maxwav = %d)\n",lastpass, maxwav);
12290: fprintf(ficlog,"Error (lastpass = %d) > (maxwav = %d)\n",lastpass, maxwav);
12291: fflush(ficlog);
12292: goto end;
12293: }
12294: 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);
12295: fprintf(ficparo,"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);
12296: fprintf(ficres,"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, 0, weightopt);
12297: fprintf(ficlog,"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);
12298: }
12299: /* ftolpl=6*ftol*1.e5; /\* 6.e-3 make convergences in less than 80 loops for the prevalence limit *\/ */
12300: /*ftolpl=6.e-4; *//* 6.e-3 make convergences in less than 80 loops for the prevalence limit */
12301: /* Third parameter line */
12302: while(fgets(line, MAXLINE, ficpar)) {
12303: /* If line starts with a # it is a comment */
12304: if (line[0] == '#') {
12305: numlinepar++;
12306: printf("%s",line);
12307: fprintf(ficres,"%s",line);
12308: fprintf(ficparo,"%s",line);
12309: fprintf(ficlog,"%s",line);
12310: continue;
12311: }else
12312: break;
12313: }
12314: if((num_filled=sscanf(line,"model=1+age%[^.\n]", model)) !=EOF){
12315: if (num_filled != 1){
12316: printf("ERROR %d: Model should be at minimum 'model=1+age+' instead of '%s'\n",num_filled, line);
12317: fprintf(ficlog,"ERROR %d: Model should be at minimum 'model=1+age+' instead of '%s'\n",num_filled, line);
12318: model[0]='\0';
12319: goto end;
12320: }
12321: else{
12322: if (model[0]=='+'){
12323: for(i=1; i<=strlen(model);i++)
12324: modeltemp[i-1]=model[i];
12325: strcpy(model,modeltemp);
12326: }
12327: }
12328: /* printf(" model=1+age%s modeltemp= %s, model=1+age+%s\n",model, modeltemp, model);fflush(stdout); */
12329: printf("model=1+age+%s\n",model);fflush(stdout);
12330: fprintf(ficparo,"model=1+age+%s\n",model);fflush(stdout);
12331: fprintf(ficres,"model=1+age+%s\n",model);fflush(stdout);
12332: fprintf(ficlog,"model=1+age+%s\n",model);fflush(stdout);
12333: }
12334: /* 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); */
12335: /* numlinepar=numlinepar+3; /\* In general *\/ */
12336: /* 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); */
12337: /* 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); */
12338: /* 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); */
12339: fflush(ficlog);
12340: /* if(model[0]=='#'|| model[0]== '\0'){ */
12341: if(model[0]=='#'){
12342: printf("Error in 'model' line: model should start with 'model=1+age+' and end without space \n \
12343: 'model=1+age+' or 'model=1+age+V1.' or 'model=1+age+age*age+V1+V1*age' or \n \
12344: 'model=1+age+V1+V2' or 'model=1+age+V1+V2+V1*V2' etc. \n"); \
12345: if(mle != -1){
12346: printf("Fix the model line and run imach with mle=-1 to get a correct template of the parameter vectors and subdiagonal covariance matrix.\n");
12347: exit(1);
12348: }
12349: }
12350: while((c=getc(ficpar))=='#' && c!= EOF){
12351: ungetc(c,ficpar);
12352: fgets(line, MAXLINE, ficpar);
12353: numlinepar++;
12354: if(line[1]=='q'){ /* This #q will quit imach (the answer is q) */
12355: z[0]=line[1];
12356: }
12357: /* printf("****line [1] = %c \n",line[1]); */
12358: fputs(line, stdout);
12359: //puts(line);
12360: fputs(line,ficparo);
12361: fputs(line,ficlog);
12362: }
12363: ungetc(c,ficpar);
12364:
12365:
12366: covar=matrix(0,NCOVMAX,firstobs,lastobs); /**< used in readdata */
12367: if(nqv>=1)coqvar=matrix(1,nqv,firstobs,lastobs); /**< Fixed quantitative covariate */
12368: if(nqtv>=1)cotqvar=ma3x(1,maxwav,1,nqtv,firstobs,lastobs); /**< Time varying quantitative covariate */
12369: if(ntv+nqtv>=1)cotvar=ma3x(1,maxwav,1,ntv+nqtv,firstobs,lastobs); /**< Time varying covariate (dummy and quantitative)*/
12370: cptcovn=0; /*Number of covariates, i.e. number of '+' in model statement plus one, indepently of n in Vn*/
12371: /* v1+v2+v3+v2*v4+v5*age makes cptcovn = 5
12372: v1+v2*age+v2*v3 makes cptcovn = 3
12373: */
12374: if (strlen(model)>1)
12375: 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*/
12376: else
12377: ncovmodel=2; /* Constant and age */
12378: nforce= (nlstate+ndeath-1)*nlstate; /* Number of forces ij from state i to j */
12379: npar= nforce*ncovmodel; /* Number of parameters like aij*/
12380: if(npar >MAXPARM || nlstate >NLSTATEMAX || ndeath >NDEATHMAX || ncovmodel>NCOVMAX){
12381: 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);
12382: 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);
12383: fflush(stdout);
12384: fclose (ficlog);
12385: goto end;
12386: }
12387: delti3= ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel);
12388: delti=delti3[1][1];
12389: /*delti=vector(1,npar); *//* Scale of each paramater (output from hesscov)*/
12390: if(mle==-1){ /* Print a wizard for help writing covariance matrix */
12391: /* We could also provide initial parameters values giving by simple logistic regression
12392: * only one way, that is without matrix product. We will have nlstate maximizations */
12393: /* for(i=1;i<nlstate;i++){ */
12394: /* /\*reducing xi for 1 to npar to 1 to ncovmodel; *\/ */
12395: /* mlikeli(ficres,p, ncovmodel, ncovmodel, nlstate, ftol, funcnoprod); */
12396: /* } */
12397: prwizard(ncovmodel, nlstate, ndeath, model, ficparo);
12398: printf(" You chose mle=-1, look at file %s for a template of covariance matrix \n",filereso);
12399: fprintf(ficlog," You chose mle=-1, look at file %s for a template of covariance matrix \n",filereso);
12400: free_ma3x(delti3,1,nlstate,1, nlstate+ndeath-1,1,ncovmodel);
12401: fclose (ficparo);
12402: fclose (ficlog);
12403: goto end;
12404: exit(0);
12405: } else if(mle==-5) { /* Main Wizard */
12406: prwizard(ncovmodel, nlstate, ndeath, model, ficparo);
12407: printf(" You chose mle=-3, look at file %s for a template of covariance matrix \n",filereso);
12408: fprintf(ficlog," You chose mle=-3, look at file %s for a template of covariance matrix \n",filereso);
12409: param= ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel);
12410: matcov=matrix(1,npar,1,npar);
12411: hess=matrix(1,npar,1,npar);
12412: } else{ /* Begin of mle != -1 or -5 */
12413: /* Read guessed parameters */
12414: /* Reads comments: lines beginning with '#' */
12415: while((c=getc(ficpar))=='#' && c!= EOF){
12416: ungetc(c,ficpar);
12417: fgets(line, MAXLINE, ficpar);
12418: numlinepar++;
12419: fputs(line,stdout);
12420: fputs(line,ficparo);
12421: fputs(line,ficlog);
12422: }
12423: ungetc(c,ficpar);
12424:
12425: param= ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel);
12426: paramstart= ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel);
12427: for(i=1; i <=nlstate; i++){
12428: j=0;
12429: for(jj=1; jj <=nlstate+ndeath; jj++){
12430: if(jj==i) continue;
12431: j++;
12432: while((c=getc(ficpar))=='#' && c!= EOF){
12433: ungetc(c,ficpar);
12434: fgets(line, MAXLINE, ficpar);
12435: numlinepar++;
12436: fputs(line,stdout);
12437: fputs(line,ficparo);
12438: fputs(line,ficlog);
12439: }
12440: ungetc(c,ficpar);
12441: fscanf(ficpar,"%1d%1d",&i1,&j1);
12442: if ((i1 != i) || (j1 != jj)){
12443: printf("Error in line parameters number %d, %1d%1d instead of %1d%1d \n \
12444: It might be a problem of design; if ncovcol and the model are correct\n \
12445: run imach with mle=-1 to get a correct template of the parameter file.\n",numlinepar, i,j, i1, j1);
12446: exit(1);
12447: }
12448: fprintf(ficparo,"%1d%1d",i1,j1);
12449: if(mle==1)
12450: printf("%1d%1d",i,jj);
12451: fprintf(ficlog,"%1d%1d",i,jj);
12452: for(k=1; k<=ncovmodel;k++){
12453: fscanf(ficpar," %lf",¶m[i][j][k]);
12454: if(mle==1){
12455: printf(" %lf",param[i][j][k]);
12456: fprintf(ficlog," %lf",param[i][j][k]);
12457: }
12458: else
12459: fprintf(ficlog," %lf",param[i][j][k]);
12460: fprintf(ficparo," %lf",param[i][j][k]);
12461: }
12462: fscanf(ficpar,"\n");
12463: numlinepar++;
12464: if(mle==1)
12465: printf("\n");
12466: fprintf(ficlog,"\n");
12467: fprintf(ficparo,"\n");
12468: }
12469: }
12470: fflush(ficlog);
12471:
12472: /* Reads parameters values */
12473: p=param[1][1];
12474: pstart=paramstart[1][1];
12475:
12476: /* Reads comments: lines beginning with '#' */
12477: while((c=getc(ficpar))=='#' && c!= EOF){
12478: ungetc(c,ficpar);
12479: fgets(line, MAXLINE, ficpar);
12480: numlinepar++;
12481: fputs(line,stdout);
12482: fputs(line,ficparo);
12483: fputs(line,ficlog);
12484: }
12485: ungetc(c,ficpar);
12486:
12487: for(i=1; i <=nlstate; i++){
12488: for(j=1; j <=nlstate+ndeath-1; j++){
12489: fscanf(ficpar,"%1d%1d",&i1,&j1);
12490: if ( (i1-i) * (j1-j) != 0){
12491: printf("Error in line parameters number %d, %1d%1d instead of %1d%1d \n",numlinepar, i,j, i1, j1);
12492: exit(1);
12493: }
12494: printf("%1d%1d",i,j);
12495: fprintf(ficparo,"%1d%1d",i1,j1);
12496: fprintf(ficlog,"%1d%1d",i1,j1);
12497: for(k=1; k<=ncovmodel;k++){
12498: fscanf(ficpar,"%le",&delti3[i][j][k]);
12499: printf(" %le",delti3[i][j][k]);
12500: fprintf(ficparo," %le",delti3[i][j][k]);
12501: fprintf(ficlog," %le",delti3[i][j][k]);
12502: }
12503: fscanf(ficpar,"\n");
12504: numlinepar++;
12505: printf("\n");
12506: fprintf(ficparo,"\n");
12507: fprintf(ficlog,"\n");
12508: }
12509: }
12510: fflush(ficlog);
12511:
12512: /* Reads covariance matrix */
12513: delti=delti3[1][1];
12514:
12515:
12516: /* free_ma3x(delti3,1,nlstate,1,nlstate+ndeath-1,1,ncovmodel); */ /* Hasn't to to freed here otherwise delti is no more allocated */
12517:
12518: /* Reads comments: lines beginning with '#' */
12519: while((c=getc(ficpar))=='#' && c!= EOF){
12520: ungetc(c,ficpar);
12521: fgets(line, MAXLINE, ficpar);
12522: numlinepar++;
12523: fputs(line,stdout);
12524: fputs(line,ficparo);
12525: fputs(line,ficlog);
12526: }
12527: ungetc(c,ficpar);
12528:
12529: matcov=matrix(1,npar,1,npar);
12530: hess=matrix(1,npar,1,npar);
12531: for(i=1; i <=npar; i++)
12532: for(j=1; j <=npar; j++) matcov[i][j]=0.;
12533:
12534: /* Scans npar lines */
12535: for(i=1; i <=npar; i++){
12536: count=fscanf(ficpar,"%1d%1d%d",&i1,&j1,&jk);
12537: if(count != 3){
12538: printf("Error! Error in parameter file %s at line %d after line starting with %1d%1d%1d\n\
12539: This is probably because your covariance matrix doesn't \n contain exactly %d lines corresponding to your model line '1+age+%s'.\n\
12540: Please run with mle=-1 to get a correct covariance matrix.\n",optionfile,numlinepar, i1,j1,jk, npar, model);
12541: fprintf(ficlog,"Error! Error in parameter file %s at line %d after line starting with %1d%1d%1d\n\
12542: This is probably because your covariance matrix doesn't \n contain exactly %d lines corresponding to your model line '1+age+%s'.\n\
12543: Please run with mle=-1 to get a correct covariance matrix.\n",optionfile,numlinepar, i1,j1,jk, npar, model);
12544: exit(1);
12545: }else{
12546: if(mle==1)
12547: printf("%1d%1d%d",i1,j1,jk);
12548: }
12549: fprintf(ficlog,"%1d%1d%d",i1,j1,jk);
12550: fprintf(ficparo,"%1d%1d%d",i1,j1,jk);
12551: for(j=1; j <=i; j++){
12552: fscanf(ficpar," %le",&matcov[i][j]);
12553: if(mle==1){
12554: printf(" %.5le",matcov[i][j]);
12555: }
12556: fprintf(ficlog," %.5le",matcov[i][j]);
12557: fprintf(ficparo," %.5le",matcov[i][j]);
12558: }
12559: fscanf(ficpar,"\n");
12560: numlinepar++;
12561: if(mle==1)
12562: printf("\n");
12563: fprintf(ficlog,"\n");
12564: fprintf(ficparo,"\n");
12565: }
12566: /* End of read covariance matrix npar lines */
12567: for(i=1; i <=npar; i++)
12568: for(j=i+1;j<=npar;j++)
12569: matcov[i][j]=matcov[j][i];
12570:
12571: if(mle==1)
12572: printf("\n");
12573: fprintf(ficlog,"\n");
12574:
12575: fflush(ficlog);
12576:
12577: } /* End of mle != -3 */
12578:
12579: /* Main data
12580: */
12581: nobs=lastobs-firstobs+1; /* was = lastobs;*/
12582: /* num=lvector(1,n); */
12583: /* moisnais=vector(1,n); */
12584: /* annais=vector(1,n); */
12585: /* moisdc=vector(1,n); */
12586: /* andc=vector(1,n); */
12587: /* weight=vector(1,n); */
12588: /* agedc=vector(1,n); */
12589: /* cod=ivector(1,n); */
12590: /* for(i=1;i<=n;i++){ */
12591: num=lvector(firstobs,lastobs);
12592: moisnais=vector(firstobs,lastobs);
12593: annais=vector(firstobs,lastobs);
12594: moisdc=vector(firstobs,lastobs);
12595: andc=vector(firstobs,lastobs);
12596: weight=vector(firstobs,lastobs);
12597: agedc=vector(firstobs,lastobs);
12598: cod=ivector(firstobs,lastobs);
12599: for(i=firstobs;i<=lastobs;i++){
12600: num[i]=0;
12601: moisnais[i]=0;
12602: annais[i]=0;
12603: moisdc[i]=0;
12604: andc[i]=0;
12605: agedc[i]=0;
12606: cod[i]=0;
12607: weight[i]=1.0; /* Equal weights, 1 by default */
12608: }
12609: mint=matrix(1,maxwav,firstobs,lastobs);
12610: anint=matrix(1,maxwav,firstobs,lastobs);
12611: s=imatrix(1,maxwav+1,firstobs,lastobs); /* s[i][j] health state for wave i and individual j */
12612: /* printf("BUG ncovmodel=%d NCOVMAX=%d 2**ncovmodel=%f BUG\n",ncovmodel,NCOVMAX,pow(2,ncovmodel)); */
12613: tab=ivector(1,NCOVMAX);
12614: ncodemax=ivector(1,NCOVMAX); /* Number of code per covariate; if O and 1 only, 2**ncov; V1+V2+V3+V4=>16 */
12615: ncodemaxwundef=ivector(1,NCOVMAX); /* Number of code per covariate; if - 1 O and 1 only, 2**ncov; V1+V2+V3+V4=>16 */
12616:
12617: /* Reads data from file datafile */
12618: if (readdata(datafile, firstobs, lastobs, &imx)==1)
12619: goto end;
12620:
12621: /* Calculation of the number of parameters from char model */
12622: /* modelsav=V2+V1+V4+age*V3 strb=age*V3 stra=V2+V1+V4
12623: k=4 (age*V3) Tvar[k=4]= 3 (from V3) Tag[cptcovage=1]=4
12624: k=3 V4 Tvar[k=3]= 4 (from V4)
12625: k=2 V1 Tvar[k=2]= 1 (from V1)
12626: k=1 Tvar[1]=2 (from V2)
12627: */
12628:
12629: Tvar=ivector(1,NCOVMAX); /* Was 15 changed to NCOVMAX. */
12630: TvarsDind=ivector(1,NCOVMAX); /* */
12631: TnsdVar=ivector(1,NCOVMAX); /* */
12632: /* for(i=1; i<=NCOVMAX;i++) TnsdVar[i]=3; */
12633: TvarsD=ivector(1,NCOVMAX); /* */
12634: TvarsQind=ivector(1,NCOVMAX); /* */
12635: TvarsQ=ivector(1,NCOVMAX); /* */
12636: TvarF=ivector(1,NCOVMAX); /* */
12637: TvarFind=ivector(1,NCOVMAX); /* */
12638: TvarV=ivector(1,NCOVMAX); /* */
12639: TvarVind=ivector(1,NCOVMAX); /* */
12640: TvarA=ivector(1,NCOVMAX); /* */
12641: TvarAind=ivector(1,NCOVMAX); /* */
12642: TvarFD=ivector(1,NCOVMAX); /* */
12643: TvarFDind=ivector(1,NCOVMAX); /* */
12644: TvarFQ=ivector(1,NCOVMAX); /* */
12645: TvarFQind=ivector(1,NCOVMAX); /* */
12646: TvarVD=ivector(1,NCOVMAX); /* */
12647: TvarVDind=ivector(1,NCOVMAX); /* */
12648: TvarVQ=ivector(1,NCOVMAX); /* */
12649: TvarVQind=ivector(1,NCOVMAX); /* */
12650:
12651: Tvalsel=vector(1,NCOVMAX); /* */
12652: Tvarsel=ivector(1,NCOVMAX); /* */
12653: Typevar=ivector(-1,NCOVMAX); /* -1 to 2 */
12654: Fixed=ivector(-1,NCOVMAX); /* -1 to 3 */
12655: Dummy=ivector(-1,NCOVMAX); /* -1 to 3 */
12656: /* V2+V1+V4+age*V3 is a model with 4 covariates (3 plus signs).
12657: For each model-covariate stores the data-covariate id. Tvar[1]=2, Tvar[2]=1, Tvar[3]=4,
12658: Tvar[4=age*V3] is 3 and 'age' is recorded in Tage.
12659: */
12660: /* For model-covariate k tells which data-covariate to use but
12661: because this model-covariate is a construction we invent a new column
12662: ncovcol + k1
12663: If already ncovcol=4 and model=V2+V1+V1*V4+age*V3
12664: Tvar[3=V1*V4]=4+1 etc */
12665: Tprod=ivector(1,NCOVMAX); /* Gives the k position of the k1 product */
12666: Tposprod=ivector(1,NCOVMAX); /* Gives the k1 product from the k position */
12667: /* Tprod[k1=1]=3(=V1*V4) for V2+V1+V1*V4+age*V3
12668: if V2+V1+V1*V4+age*V3+V3*V2 TProd[k1=2]=5 (V3*V2)
12669: Tposprod[k]=k1 , Tposprod[3]=1, Tposprod[5]=2
12670: */
12671: Tvaraff=ivector(1,NCOVMAX); /* Unclear */
12672: 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
12673: * For V3*V2 (in V2+V1+V1*V4+age*V3+V3*V2), V3*V2 position is 2nd.
12674: * Tvard[k1=2][1]=3 (V3) Tvard[k1=2][2]=2(V2) */
12675: Tvardk=imatrix(1,NCOVMAX,1,2);
12676: Tage=ivector(1,NCOVMAX); /* Gives the covariate id of covariates associated with age: V2 + V1 + age*V4 + V3*age
12677: 4 covariates (3 plus signs)
12678: Tage[1=V3*age]= 4; Tage[2=age*V4] = 3
12679: */
12680: for(i=1;i<NCOVMAX;i++)
12681: Tage[i]=0;
12682: Tmodelind=ivector(1,NCOVMAX);/** gives the k model position of an
12683: * individual dummy, fixed or varying:
12684: * Tmodelind[Tvaraff[3]]=9,Tvaraff[1]@9={4,
12685: * 3, 1, 0, 0, 0, 0, 0, 0},
12686: * model=V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 ,
12687: * V1 df, V2 qf, V3 & V4 dv, V5 qv
12688: * Tmodelind[1]@9={9,0,3,2,}*/
12689: TmodelInvind=ivector(1,NCOVMAX); /* TmodelInvind=Tvar[k]- ncovcol-nqv={5-2-1=2,*/
12690: TmodelInvQind=ivector(1,NCOVMAX);/** gives the k model position of an
12691: * individual quantitative, fixed or varying:
12692: * Tmodelqind[1]=1,Tvaraff[1]@9={4,
12693: * 3, 1, 0, 0, 0, 0, 0, 0},
12694: * model=V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1*/
12695: /* Main decodemodel */
12696:
12697:
12698: if(decodemodel(model, lastobs) == 1) /* In order to get Tvar[k] V4+V3+V5 p Tvar[1]@3 = {4, 3, 5}*/
12699: goto end;
12700:
12701: if((double)(lastobs-imx)/(double)imx > 1.10){
12702: nbwarn++;
12703: 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);
12704: 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);
12705: }
12706: /* if(mle==1){*/
12707: if (weightopt != 1) { /* Maximisation without weights. We can have weights different from 1 but want no weight*/
12708: for(i=1;i<=imx;i++) weight[i]=1.0; /* changed to imx */
12709: }
12710:
12711: /*-calculation of age at interview from date of interview and age at death -*/
12712: agev=matrix(1,maxwav,1,imx);
12713:
12714: if(calandcheckages(imx, maxwav, &agemin, &agemax, &nberr, &nbwarn) == 1)
12715: goto end;
12716:
12717:
12718: agegomp=(int)agemin;
12719: free_vector(moisnais,firstobs,lastobs);
12720: free_vector(annais,firstobs,lastobs);
12721: /* free_matrix(mint,1,maxwav,1,n);
12722: free_matrix(anint,1,maxwav,1,n);*/
12723: /* free_vector(moisdc,1,n); */
12724: /* free_vector(andc,1,n); */
12725: /* */
12726:
12727: wav=ivector(1,imx);
12728: /* dh=imatrix(1,lastpass-firstpass+1,1,imx); */
12729: /* bh=imatrix(1,lastpass-firstpass+1,1,imx); */
12730: /* mw=imatrix(1,lastpass-firstpass+1,1,imx); */
12731: 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.*/
12732: bh=imatrix(1,lastpass-firstpass+2,1,imx);
12733: mw=imatrix(1,lastpass-firstpass+2,1,imx);
12734:
12735: /* Concatenates waves */
12736: /* Concatenates waves: wav[i] is the number of effective (useful waves) of individual i.
12737: Death is a valid wave (if date is known).
12738: mw[mi][i] is the number of (mi=1 to wav[i]) effective wave out of mi of individual i
12739: dh[m][i] or dh[mw[mi][i]][i] is the delay between two effective waves m=mw[mi][i]
12740: and mw[mi+1][i]. dh depends on stepm.
12741: */
12742:
12743: concatwav(wav, dh, bh, mw, s, agedc, agev, firstpass, lastpass, imx, nlstate, stepm);
12744: /* Concatenates waves */
12745:
12746: free_vector(moisdc,firstobs,lastobs);
12747: free_vector(andc,firstobs,lastobs);
12748:
12749: /* Routine tricode is to calculate cptcoveff (real number of unique covariates) and to associate covariable number and modality */
12750: nbcode=imatrix(0,NCOVMAX,0,NCOVMAX);
12751: ncodemax[1]=1;
12752: Ndum =ivector(-1,NCOVMAX);
12753: cptcoveff=0;
12754: if (ncovmodel-nagesqr > 2 ){ /* That is if covariate other than cst, age and age*age */
12755: tricode(&cptcoveff,Tvar,nbcode,imx, Ndum); /**< Fills nbcode[Tvar[j]][l]; as well as calculate cptcoveff or number of total effective dummy covariates*/
12756: }
12757:
12758: ncovcombmax=pow(2,cptcoveff);
12759: invalidvarcomb=ivector(0, ncovcombmax);
12760: for(i=0;i<ncovcombmax;i++)
12761: invalidvarcomb[i]=0;
12762:
12763: /* Nbcode gives the value of the lth modality (currently 1 to 2) of jth covariate, in
12764: V2+V1*age, there are 3 covariates Tvar[2]=1 (V1).*/
12765: /* 1 to ncodemax[j] which is the maximum value of this jth covariate */
12766:
12767: /* codtab=imatrix(1,100,1,10);*/ /* codtab[h,k]=( (h-1) - mod(k-1,2**(k-1) )/2**(k-1) */
12768: /*printf(" codtab[1,1],codtab[100,10]=%d,%d\n", codtab[1][1],codtabm(100,10));*/
12769: /* codtab gives the value 1 or 2 of the hth combination of k covariates (1 or 2).*/
12770: /* nbcode[Tvaraff[j]][codtabm(h,j)]) : if there are only 2 modalities for a covariate j,
12771: * codtabm(h,j) gives its value classified at position h and nbcode gives how it is coded
12772: * (currently 0 or 1) in the data.
12773: * In a loop on h=1 to 2**k, and a loop on j (=1 to k), we get the value of
12774: * corresponding modality (h,j).
12775: */
12776:
12777: h=0;
12778: /*if (cptcovn > 0) */
12779: m=pow(2,cptcoveff);
12780:
12781: /**< codtab(h,k) k = codtab[h,k]=( (h-1) - mod(k-1,2**(k-1) )/2**(k-1) + 1
12782: * For k=4 covariates, h goes from 1 to m=2**k
12783: * codtabm(h,k)= (1 & (h-1) >> (k-1)) + 1;
12784: * #define codtabm(h,k) (1 & (h-1) >> (k-1))+1
12785: * h\k 1 2 3 4 * h-1\k-1 4 3 2 1
12786: *______________________________ *______________________
12787: * 1 i=1 1 i=1 1 i=1 1 i=1 1 * 0 0 0 0 0
12788: * 2 2 1 1 1 * 1 0 0 0 1
12789: * 3 i=2 1 2 1 1 * 2 0 0 1 0
12790: * 4 2 2 1 1 * 3 0 0 1 1
12791: * 5 i=3 1 i=2 1 2 1 * 4 0 1 0 0
12792: * 6 2 1 2 1 * 5 0 1 0 1
12793: * 7 i=4 1 2 2 1 * 6 0 1 1 0
12794: * 8 2 2 2 1 * 7 0 1 1 1
12795: * 9 i=5 1 i=3 1 i=2 1 2 * 8 1 0 0 0
12796: * 10 2 1 1 2 * 9 1 0 0 1
12797: * 11 i=6 1 2 1 2 * 10 1 0 1 0
12798: * 12 2 2 1 2 * 11 1 0 1 1
12799: * 13 i=7 1 i=4 1 2 2 * 12 1 1 0 0
12800: * 14 2 1 2 2 * 13 1 1 0 1
12801: * 15 i=8 1 2 2 2 * 14 1 1 1 0
12802: * 16 2 2 2 2 * 15 1 1 1 1
12803: */
12804: /* How to do the opposite? From combination h (=1 to 2**k) how to get the value on the covariates? */
12805: /* from h=5 and m, we get then number of covariates k=log(m)/log(2)=4
12806: * and the value of each covariate?
12807: * V1=1, V2=1, V3=2, V4=1 ?
12808: * h-1=4 and 4 is 0100 or reverse 0010, and +1 is 1121 ok.
12809: * h=6, 6-1=5, 5 is 0101, 1010, 2121, V1=2nd, V2=1st, V3=2nd, V4=1st.
12810: * In order to get the real value in the data, we use nbcode
12811: * nbcode[Tvar[3][2nd]]=1 and nbcode[Tvar[4][1]]=0
12812: * We are keeping this crazy system in order to be able (in the future?)
12813: * to have more than 2 values (0 or 1) for a covariate.
12814: * #define codtabm(h,k) (1 & (h-1) >> (k-1))+1
12815: * h=6, k=2? h-1=5=0101, reverse 1010, +1=2121, k=2nd position: value is 1: codtabm(6,2)=1
12816: * bbbbbbbb
12817: * 76543210
12818: * h-1 00000101 (6-1=5)
12819: *(h-1)>>(k-1)= 00000010 >> (2-1) = 1 right shift
12820: * &
12821: * 1 00000001 (1)
12822: * 00000000 = 1 & ((h-1) >> (k-1))
12823: * +1= 00000001 =1
12824: *
12825: * h=14, k=3 => h'=h-1=13, k'=k-1=2
12826: * h' 1101 =2^3+2^2+0x2^1+2^0
12827: * >>k' 11
12828: * & 00000001
12829: * = 00000001
12830: * +1 = 00000010=2 = codtabm(14,3)
12831: * Reverse h=6 and m=16?
12832: * cptcoveff=log(16)/log(2)=4 covariate: 6-1=5=0101 reversed=1010 +1=2121 =>V1=2, V2=1, V3=2, V4=1.
12833: * for (j=1 to cptcoveff) Vj=decodtabm(j,h,cptcoveff)
12834: * decodtabm(h,j,cptcoveff)= (((h-1) >> (j-1)) & 1) +1
12835: * decodtabm(h,j,cptcoveff)= (h <= (1<<cptcoveff)?(((h-1) >> (j-1)) & 1) +1 : -1)
12836: * V3=decodtabm(14,3,2**4)=2
12837: * h'=13 1101 =2^3+2^2+0x2^1+2^0
12838: *(h-1) >> (j-1) 0011 =13 >> 2
12839: * &1 000000001
12840: * = 000000001
12841: * +1= 000000010 =2
12842: * 2211
12843: * V1=1+1, V2=0+1, V3=1+1, V4=1+1
12844: * V3=2
12845: * codtabm and decodtabm are identical
12846: */
12847:
12848:
12849: free_ivector(Ndum,-1,NCOVMAX);
12850:
12851:
12852:
12853: /* Initialisation of ----------- gnuplot -------------*/
12854: strcpy(optionfilegnuplot,optionfilefiname);
12855: if(mle==-3)
12856: strcat(optionfilegnuplot,"-MORT_");
12857: strcat(optionfilegnuplot,".gp");
12858:
12859: if((ficgp=fopen(optionfilegnuplot,"w"))==NULL) {
12860: printf("Problem with file %s",optionfilegnuplot);
12861: }
12862: else{
12863: fprintf(ficgp,"\n# IMaCh-%s\n", version);
12864: fprintf(ficgp,"# %s\n", optionfilegnuplot);
12865: //fprintf(ficgp,"set missing 'NaNq'\n");
12866: fprintf(ficgp,"set datafile missing 'NaNq'\n");
12867: }
12868: /* fclose(ficgp);*/
12869:
12870:
12871: /* Initialisation of --------- index.htm --------*/
12872:
12873: strcpy(optionfilehtm,optionfilefiname); /* Main html file */
12874: if(mle==-3)
12875: strcat(optionfilehtm,"-MORT_");
12876: strcat(optionfilehtm,".htm");
12877: if((fichtm=fopen(optionfilehtm,"w"))==NULL) {
12878: printf("Problem with %s \n",optionfilehtm);
12879: exit(0);
12880: }
12881:
12882: strcpy(optionfilehtmcov,optionfilefiname); /* Only for matrix of covariance */
12883: strcat(optionfilehtmcov,"-cov.htm");
12884: if((fichtmcov=fopen(optionfilehtmcov,"w"))==NULL) {
12885: printf("Problem with %s \n",optionfilehtmcov), exit(0);
12886: }
12887: else{
12888: fprintf(fichtmcov,"<html><head>\n<title>IMaCh Cov %s</title></head>\n <body><font size=\"2\">%s <br> %s</font> \
12889: <hr size=\"2\" color=\"#EC5E5E\"> \n\
12890: Title=%s <br>Datafile=%s Firstpass=%d Lastpass=%d Stepm=%d Weight=%d Model=1+age+%s<br>\n",\
12891: optionfilehtmcov,version,fullversion,title,datafile,firstpass,lastpass,stepm, weightopt, model);
12892: }
12893:
12894: fprintf(fichtm,"<html><head>\n<meta charset=\"utf-8\"/><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n\
12895: <title>IMaCh %s</title></head>\n\
12896: <body><font size=\"7\"><a href=http:/euroreves.ined.fr/imach>IMaCh for Interpolated Markov Chain</a> </font><br>\n\
12897: <font size=\"3\">Sponsored by Copyright (C) 2002-2015 <a href=http://www.ined.fr>INED</a>\
12898: -EUROREVES-Institut de longévité-2013-2022-Japan Society for the Promotion of Sciences 日本学術振興会 \
12899: (<a href=https://www.jsps.go.jp/english/e-grants/>Grant-in-Aid for Scientific Research 25293121</a>) - \
12900: <a href=https://software.intel.com/en-us>Intel Software 2015-2018</a></font><br> \n", optionfilehtm);
12901:
12902: fprintf(fichtm,"<hr size=\"2\" color=\"#EC5E5E\"> \n\
12903: <font size=\"2\">IMaCh-%s <br> %s</font> \
12904: <hr size=\"2\" color=\"#EC5E5E\"> \n\
12905: This file: <a href=\"%s\">%s</a></br>Title=%s <br>Datafile=<a href=\"%s\">%s</a> Firstpass=%d Lastpass=%d Stepm=%d Weight=%d Model=1+age+%s<br>\n\
12906: \n\
12907: <hr size=\"2\" color=\"#EC5E5E\">\
12908: <ul><li><h4>Parameter files</h4>\n\
12909: - Parameter file: <a href=\"%s.%s\">%s.%s</a><br>\n\
12910: - Copy of the parameter file: <a href=\"o%s\">o%s</a><br>\n\
12911: - Log file of the run: <a href=\"%s\">%s</a><br>\n\
12912: - Gnuplot file name: <a href=\"%s\">%s</a><br>\n\
12913: - Date and time at start: %s</ul>\n",\
12914: version,fullversion,optionfilehtm,optionfilehtm,title,datafile,datafile,firstpass,lastpass,stepm, weightopt, model, \
12915: optionfilefiname,optionfilext,optionfilefiname,optionfilext,\
12916: fileres,fileres,\
12917: filelog,filelog,optionfilegnuplot,optionfilegnuplot,strstart);
12918: fflush(fichtm);
12919:
12920: strcpy(pathr,path);
12921: strcat(pathr,optionfilefiname);
12922: #ifdef WIN32
12923: _chdir(optionfilefiname); /* Move to directory named optionfile */
12924: #else
12925: chdir(optionfilefiname); /* Move to directory named optionfile */
12926: #endif
12927:
12928:
12929: /* Calculates basic frequencies. Computes observed prevalence at single age
12930: and for any valid combination of covariates
12931: and prints on file fileres'p'. */
12932: freqsummary(fileres, p, pstart, agemin, agemax, s, agev, nlstate, imx, Tvaraff, invalidvarcomb, nbcode, ncodemax,mint,anint,strstart, \
12933: firstpass, lastpass, stepm, weightopt, model);
12934:
12935: fprintf(fichtm,"\n");
12936: fprintf(fichtm,"<h4>Parameter line 2</h4><ul><li>Tolerance for the convergence of the likelihood: ftol=%g \n<li>Interval for the elementary matrix (in month): stepm=%d",\
12937: ftol, stepm);
12938: fprintf(fichtm,"\n<li>Number of fixed dummy covariates: ncovcol=%d ", ncovcol);
12939: ncurrv=1;
12940: for(i=ncurrv; i <=ncovcol; i++) fprintf(fichtm,"V%d ", i);
12941: fprintf(fichtm,"\n<li> Number of fixed quantitative variables: nqv=%d ", nqv);
12942: ncurrv=i;
12943: for(i=ncurrv; i <=ncurrv-1+nqv; i++) fprintf(fichtm,"V%d ", i);
12944: fprintf(fichtm,"\n<li> Number of time varying (wave varying) dummy covariates: ntv=%d ", ntv);
12945: ncurrv=i;
12946: for(i=ncurrv; i <=ncurrv-1+ntv; i++) fprintf(fichtm,"V%d ", i);
12947: fprintf(fichtm,"\n<li>Number of time varying quantitative covariates: nqtv=%d ", nqtv);
12948: ncurrv=i;
12949: for(i=ncurrv; i <=ncurrv-1+nqtv; i++) fprintf(fichtm,"V%d ", i);
12950: fprintf(fichtm,"\n<li>Weights column \n<br>Number of alive states: nlstate=%d <br>Number of death states (not really implemented): ndeath=%d \n<li>Number of waves: maxwav=%d \n<li>Parameter for maximization (1), using parameter values (0), for design of parameters and variance-covariance matrix: mle=%d \n<li>Does the weight column be taken into account (1), or not (0): weight=%d</ul>\n", \
12951: nlstate, ndeath, maxwav, mle, weightopt);
12952:
12953: fprintf(fichtm,"<h4> Diagram of states <a href=\"%s_.svg\">%s_.svg</a></h4> \n\
12954: <img src=\"%s_.svg\">", subdirf2(optionfilefiname,"D_"),subdirf2(optionfilefiname,"D_"),subdirf2(optionfilefiname,"D_"));
12955:
12956:
12957: fprintf(fichtm,"\n<h4>Some descriptive statistics </h4>\n<br>Number of (used) observations=%d <br>\n\
12958: Youngest age at first (selected) pass %.2f, oldest age %.2f<br>\n\
12959: Interval (in months) between two waves: Min=%d Max=%d Mean=%.2lf<br>\n",\
12960: imx,agemin,agemax,jmin,jmax,jmean);
12961: pmmij= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
12962: oldms= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
12963: newms= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
12964: savms= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
12965: oldm=oldms; newm=newms; savm=savms; /* Keeps fixed addresses to free */
12966:
12967: /* For Powell, parameters are in a vector p[] starting at p[1]
12968: so we point p on param[1][1] so that p[1] maps on param[1][1][1] */
12969: p=param[1][1]; /* *(*(*(param +1)+1)+0) */
12970:
12971: globpr=0; /* To get the number ipmx of contributions and the sum of weights*/
12972: /* For mortality only */
12973: if (mle==-3){
12974: ximort=matrix(1,NDIM,1,NDIM);
12975: for(i=1;i<=NDIM;i++)
12976: for(j=1;j<=NDIM;j++)
12977: ximort[i][j]=0.;
12978: /* ximort=gsl_matrix_alloc(1,NDIM,1,NDIM); */
12979: cens=ivector(firstobs,lastobs);
12980: ageexmed=vector(firstobs,lastobs);
12981: agecens=vector(firstobs,lastobs);
12982: dcwave=ivector(firstobs,lastobs);
12983:
12984: for (i=1; i<=imx; i++){
12985: dcwave[i]=-1;
12986: for (m=firstpass; m<=lastpass; m++)
12987: if (s[m][i]>nlstate) {
12988: dcwave[i]=m;
12989: /* printf("i=%d j=%d s=%d dcwave=%d\n",i,j, s[j][i],dcwave[i]);*/
12990: break;
12991: }
12992: }
12993:
12994: for (i=1; i<=imx; i++) {
12995: if (wav[i]>0){
12996: ageexmed[i]=agev[mw[1][i]][i];
12997: j=wav[i];
12998: agecens[i]=1.;
12999:
13000: if (ageexmed[i]> 1 && wav[i] > 0){
13001: agecens[i]=agev[mw[j][i]][i];
13002: cens[i]= 1;
13003: }else if (ageexmed[i]< 1)
13004: cens[i]= -1;
13005: if (agedc[i]< AGESUP && agedc[i]>1 && dcwave[i]>firstpass && dcwave[i]<=lastpass)
13006: cens[i]=0 ;
13007: }
13008: else cens[i]=-1;
13009: }
13010:
13011: for (i=1;i<=NDIM;i++) {
13012: for (j=1;j<=NDIM;j++)
13013: ximort[i][j]=(i == j ? 1.0 : 0.0);
13014: }
13015:
13016: p[1]=0.0268; p[NDIM]=0.083;
13017: /* printf("%lf %lf", p[1], p[2]); */
13018:
13019:
13020: #ifdef GSL
13021: printf("GSL optimization\n"); fprintf(ficlog,"Powell\n");
13022: #else
13023: printf("Powell\n"); fprintf(ficlog,"Powell\n");
13024: #endif
13025: strcpy(filerespow,"POW-MORT_");
13026: strcat(filerespow,fileresu);
13027: if((ficrespow=fopen(filerespow,"w"))==NULL) {
13028: printf("Problem with resultfile: %s\n", filerespow);
13029: fprintf(ficlog,"Problem with resultfile: %s\n", filerespow);
13030: }
13031: #ifdef GSL
13032: fprintf(ficrespow,"# GSL optimization\n# iter -2*LL");
13033: #else
13034: fprintf(ficrespow,"# Powell\n# iter -2*LL");
13035: #endif
13036: /* for (i=1;i<=nlstate;i++)
13037: for(j=1;j<=nlstate+ndeath;j++)
13038: if(j!=i)fprintf(ficrespow," p%1d%1d",i,j);
13039: */
13040: fprintf(ficrespow,"\n");
13041: #ifdef GSL
13042: /* gsl starts here */
13043: T = gsl_multimin_fminimizer_nmsimplex;
13044: gsl_multimin_fminimizer *sfm = NULL;
13045: gsl_vector *ss, *x;
13046: gsl_multimin_function minex_func;
13047:
13048: /* Initial vertex size vector */
13049: ss = gsl_vector_alloc (NDIM);
13050:
13051: if (ss == NULL){
13052: GSL_ERROR_VAL ("failed to allocate space for ss", GSL_ENOMEM, 0);
13053: }
13054: /* Set all step sizes to 1 */
13055: gsl_vector_set_all (ss, 0.001);
13056:
13057: /* Starting point */
13058:
13059: x = gsl_vector_alloc (NDIM);
13060:
13061: if (x == NULL){
13062: gsl_vector_free(ss);
13063: GSL_ERROR_VAL ("failed to allocate space for x", GSL_ENOMEM, 0);
13064: }
13065:
13066: /* Initialize method and iterate */
13067: /* p[1]=0.0268; p[NDIM]=0.083; */
13068: /* gsl_vector_set(x, 0, 0.0268); */
13069: /* gsl_vector_set(x, 1, 0.083); */
13070: gsl_vector_set(x, 0, p[1]);
13071: gsl_vector_set(x, 1, p[2]);
13072:
13073: minex_func.f = &gompertz_f;
13074: minex_func.n = NDIM;
13075: minex_func.params = (void *)&p; /* ??? */
13076:
13077: sfm = gsl_multimin_fminimizer_alloc (T, NDIM);
13078: gsl_multimin_fminimizer_set (sfm, &minex_func, x, ss);
13079:
13080: printf("Iterations beginning .....\n\n");
13081: printf("Iter. # Intercept Slope -Log Likelihood Simplex size\n");
13082:
13083: iteri=0;
13084: while (rval == GSL_CONTINUE){
13085: iteri++;
13086: status = gsl_multimin_fminimizer_iterate(sfm);
13087:
13088: if (status) printf("error: %s\n", gsl_strerror (status));
13089: fflush(0);
13090:
13091: if (status)
13092: break;
13093:
13094: rval = gsl_multimin_test_size (gsl_multimin_fminimizer_size (sfm), 1e-6);
13095: ssval = gsl_multimin_fminimizer_size (sfm);
13096:
13097: if (rval == GSL_SUCCESS)
13098: printf ("converged to a local maximum at\n");
13099:
13100: printf("%5d ", iteri);
13101: for (it = 0; it < NDIM; it++){
13102: printf ("%10.5f ", gsl_vector_get (sfm->x, it));
13103: }
13104: printf("f() = %-10.5f ssize = %.7f\n", sfm->fval, ssval);
13105: }
13106:
13107: printf("\n\n Please note: Program should be run many times with varying starting points to detemine global maximum\n\n");
13108:
13109: gsl_vector_free(x); /* initial values */
13110: gsl_vector_free(ss); /* inital step size */
13111: for (it=0; it<NDIM; it++){
13112: p[it+1]=gsl_vector_get(sfm->x,it);
13113: fprintf(ficrespow," %.12lf", p[it]);
13114: }
13115: gsl_multimin_fminimizer_free (sfm); /* p *(sfm.x.data) et p *(sfm.x.data+1) */
13116: #endif
13117: #ifdef POWELL
13118: powell(p,ximort,NDIM,ftol,&iter,&fret,gompertz);
13119: #endif
13120: fclose(ficrespow);
13121:
13122: hesscov(matcov, hess, p, NDIM, delti, 1e-4, gompertz);
13123:
13124: for(i=1; i <=NDIM; i++)
13125: for(j=i+1;j<=NDIM;j++)
13126: matcov[i][j]=matcov[j][i];
13127:
13128: printf("\nCovariance matrix\n ");
13129: fprintf(ficlog,"\nCovariance matrix\n ");
13130: for(i=1; i <=NDIM; i++) {
13131: for(j=1;j<=NDIM;j++){
13132: printf("%f ",matcov[i][j]);
13133: fprintf(ficlog,"%f ",matcov[i][j]);
13134: }
13135: printf("\n "); fprintf(ficlog,"\n ");
13136: }
13137:
13138: printf("iter=%d MLE=%f Eq=%lf*exp(%lf*(age-%d))\n",iter,-gompertz(p),p[1],p[2],agegomp);
13139: for (i=1;i<=NDIM;i++) {
13140: printf("%f [%f ; %f]\n",p[i],p[i]-2*sqrt(matcov[i][i]),p[i]+2*sqrt(matcov[i][i]));
13141: fprintf(ficlog,"%f [%f ; %f]\n",p[i],p[i]-2*sqrt(matcov[i][i]),p[i]+2*sqrt(matcov[i][i]));
13142: }
13143: lsurv=vector(agegomp,AGESUP);
13144: lpop=vector(agegomp,AGESUP);
13145: tpop=vector(agegomp,AGESUP);
13146: lsurv[agegomp]=100000;
13147:
13148: for (k=agegomp;k<=AGESUP;k++) {
13149: agemortsup=k;
13150: if (p[1]*exp(p[2]*(k-agegomp))>1) break;
13151: }
13152:
13153: for (k=agegomp;k<agemortsup;k++)
13154: lsurv[k+1]=lsurv[k]-lsurv[k]*(p[1]*exp(p[2]*(k-agegomp)));
13155:
13156: for (k=agegomp;k<agemortsup;k++){
13157: lpop[k]=(lsurv[k]+lsurv[k+1])/2.;
13158: sumlpop=sumlpop+lpop[k];
13159: }
13160:
13161: tpop[agegomp]=sumlpop;
13162: for (k=agegomp;k<(agemortsup-3);k++){
13163: /* tpop[k+1]=2;*/
13164: tpop[k+1]=tpop[k]-lpop[k];
13165: }
13166:
13167:
13168: printf("\nAge lx qx dx Lx Tx e(x)\n");
13169: for (k=agegomp;k<(agemortsup-2);k++)
13170: 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]);
13171:
13172:
13173: replace_back_to_slash(pathc,pathcd); /* Even gnuplot wants a / */
13174: ageminpar=50;
13175: agemaxpar=100;
13176: if(ageminpar == AGEOVERFLOW ||agemaxpar == AGEOVERFLOW){
13177: printf("Warning! Error in gnuplot file with ageminpar %f or agemaxpar %f overflow\n\
13178: This is probably because your parameter file doesn't \n contain the exact number of lines (or columns) corresponding to your model line.\n\
13179: Please run with mle=-1 to get a correct covariance matrix.\n",ageminpar,agemaxpar);
13180: fprintf(ficlog,"Warning! Error in gnuplot file with ageminpar %f or agemaxpar %f overflow\n\
13181: This is probably because your parameter file doesn't \n contain the exact number of lines (or columns) corresponding to your model line.\n\
13182: Please run with mle=-1 to get a correct covariance matrix.\n",ageminpar,agemaxpar);
13183: }else{
13184: printf("Warning! ageminpar %f and agemaxpar %f have been fixed because for simplification until it is fixed...\n\n",ageminpar,agemaxpar);
13185: fprintf(ficlog,"Warning! ageminpar %f and agemaxpar %f have been fixed because for simplification until it is fixed...\n\n",ageminpar,agemaxpar);
13186: printinggnuplotmort(fileresu, optionfilefiname,ageminpar,agemaxpar,fage, pathc,p);
13187: }
13188: printinghtmlmort(fileresu,title,datafile, firstpass, lastpass, \
13189: stepm, weightopt,\
13190: model,imx,p,matcov,agemortsup);
13191:
13192: free_vector(lsurv,agegomp,AGESUP);
13193: free_vector(lpop,agegomp,AGESUP);
13194: free_vector(tpop,agegomp,AGESUP);
13195: free_matrix(ximort,1,NDIM,1,NDIM);
13196: free_ivector(dcwave,firstobs,lastobs);
13197: free_vector(agecens,firstobs,lastobs);
13198: free_vector(ageexmed,firstobs,lastobs);
13199: free_ivector(cens,firstobs,lastobs);
13200: #ifdef GSL
13201: #endif
13202: } /* Endof if mle==-3 mortality only */
13203: /* Standard */
13204: else{ /* For mle !=- 3, could be 0 or 1 or 4 etc. */
13205: globpr=0;/* Computes sum of likelihood for globpr=1 and funcone */
13206: /* Computes likelihood for initial parameters, uses funcone to compute gpimx and gsw */
13207: likelione(ficres, p, npar, nlstate, &globpr, &ipmx, &sw, &fretone, funcone); /* Prints the contributions to the likelihood */
13208: printf("First Likeli=%12.6f ipmx=%ld sw=%12.6f",fretone,ipmx,sw);
13209: for (k=1; k<=npar;k++)
13210: printf(" %d %8.5f",k,p[k]);
13211: printf("\n");
13212: if(mle>=1){ /* Could be 1 or 2, Real Maximization */
13213: /* mlikeli uses func not funcone */
13214: /* for(i=1;i<nlstate;i++){ */
13215: /* /\*reducing xi for 1 to npar to 1 to ncovmodel; *\/ */
13216: /* mlikeli(ficres,p, ncovmodel, ncovmodel, nlstate, ftol, funcnoprod); */
13217: /* } */
13218: mlikeli(ficres,p, npar, ncovmodel, nlstate, ftol, func);
13219: }
13220: if(mle==0) {/* No optimization, will print the likelihoods for the datafile */
13221: globpr=0;/* Computes sum of likelihood for globpr=1 and funcone */
13222: /* Computes likelihood for initial parameters, uses funcone to compute gpimx and gsw */
13223: likelione(ficres, p, npar, nlstate, &globpr, &ipmx, &sw, &fretone, funcone); /* Prints the contributions to the likelihood */
13224: }
13225: globpr=1; /* again, to print the individual contributions using computed gpimx and gsw */
13226: likelione(ficres, p, npar, nlstate, &globpr, &ipmx, &sw, &fretone, funcone); /* Prints the contributions to the likelihood */
13227: printf("Second Likeli=%12.6f ipmx=%ld sw=%12.6f",fretone,ipmx,sw);
13228: /* exit(0); */
13229: for (k=1; k<=npar;k++)
13230: printf(" %d %8.5f",k,p[k]);
13231: printf("\n");
13232:
13233: /*--------- results files --------------*/
13234: /* 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); */
13235:
13236:
13237: fprintf(ficres,"# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n");
13238: printf("# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n"); /* Printing model equation */
13239: fprintf(ficlog,"# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n");
13240:
13241: printf("#model= 1 + age ");
13242: fprintf(ficres,"#model= 1 + age ");
13243: fprintf(ficlog,"#model= 1 + age ");
13244: fprintf(fichtm,"\n<ul><li> model=1+age+%s\n \
13245: </ul>", model);
13246:
13247: fprintf(fichtm,"\n<table style=\"text-align:center; border: 1px solid\">\n");
13248: fprintf(fichtm, "<tr><th>Model=</th><th>1</th><th>+ age</th>");
13249: if(nagesqr==1){
13250: printf(" + age*age ");
13251: fprintf(ficres," + age*age ");
13252: fprintf(ficlog," + age*age ");
13253: fprintf(fichtm, "<th>+ age*age</th>");
13254: }
13255: for(j=1;j <=ncovmodel-2;j++){
13256: if(Typevar[j]==0) {
13257: printf(" + V%d ",Tvar[j]);
13258: fprintf(ficres," + V%d ",Tvar[j]);
13259: fprintf(ficlog," + V%d ",Tvar[j]);
13260: fprintf(fichtm, "<th>+ V%d</th>",Tvar[j]);
13261: }else if(Typevar[j]==1) {
13262: printf(" + V%d*age ",Tvar[j]);
13263: fprintf(ficres," + V%d*age ",Tvar[j]);
13264: fprintf(ficlog," + V%d*age ",Tvar[j]);
13265: fprintf(fichtm, "<th>+ V%d*age</th>",Tvar[j]);
13266: }else if(Typevar[j]==2) {
13267: printf(" + V%d*V%d ",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
13268: fprintf(ficres," + V%d*V%d ",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
13269: fprintf(ficlog," + V%d*V%d ",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
13270: fprintf(fichtm, "<th>+ V%d*V%d</th>",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
13271: }
13272: }
13273: printf("\n");
13274: fprintf(ficres,"\n");
13275: fprintf(ficlog,"\n");
13276: fprintf(fichtm, "</tr>");
13277: fprintf(fichtm, "\n");
13278:
13279:
13280: for(i=1,jk=1; i <=nlstate; i++){
13281: for(k=1; k <=(nlstate+ndeath); k++){
13282: if (k != i) {
13283: fprintf(fichtm, "<tr>");
13284: printf("%d%d ",i,k);
13285: fprintf(ficlog,"%d%d ",i,k);
13286: fprintf(ficres,"%1d%1d ",i,k);
13287: fprintf(fichtm, "<td>%1d%1d</td>",i,k);
13288: for(j=1; j <=ncovmodel; j++){
13289: printf("%12.7f ",p[jk]);
13290: fprintf(ficlog,"%12.7f ",p[jk]);
13291: fprintf(ficres,"%12.7f ",p[jk]);
13292: fprintf(fichtm, "<td>%12.7f</td>",p[jk]);
13293: jk++;
13294: }
13295: printf("\n");
13296: fprintf(ficlog,"\n");
13297: fprintf(ficres,"\n");
13298: fprintf(fichtm, "</tr>\n");
13299: }
13300: }
13301: }
13302: /* fprintf(fichtm,"</tr>\n"); */
13303: fprintf(fichtm,"</table>\n");
13304: fprintf(fichtm, "\n");
13305:
13306: if(mle != 0){
13307: /* Computing hessian and covariance matrix only at a peak of the Likelihood, that is after optimization */
13308: ftolhess=ftol; /* Usually correct */
13309: hesscov(matcov, hess, p, npar, delti, ftolhess, func);
13310: 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");
13311: 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");
13312: fprintf(fichtm, "\n<p>The Wald test results are output only if the maximimzation of the Likelihood is performed (mle=1)\n</br>Parameters, Wald tests and Wald-based confidence intervals\n</br> W is simply the result of the division of the parameter by the square root of covariance of the parameter.\n</br> And Wald-based confidence intervals plus and minus 1.96 * W \n </br> It might be better to visualize the covariance matrix. See the page '<a href=\"%s\">Matrix of variance-covariance of one-step probabilities and its graphs</a>'.\n</br>",optionfilehtmcov);
13313: fprintf(fichtm,"\n<table style=\"text-align:center; border: 1px solid\">");
13314: fprintf(fichtm, "\n<tr><th>Model=</th><th>1</th><th>+ age</th>");
13315: if(nagesqr==1){
13316: printf(" + age*age ");
13317: fprintf(ficres," + age*age ");
13318: fprintf(ficlog," + age*age ");
13319: fprintf(fichtm, "<th>+ age*age</th>");
13320: }
13321: for(j=1;j <=ncovmodel-2;j++){
13322: if(Typevar[j]==0) {
13323: printf(" + V%d ",Tvar[j]);
13324: fprintf(fichtm, "<th>+ V%d</th>",Tvar[j]);
13325: }else if(Typevar[j]==1) {
13326: printf(" + V%d*age ",Tvar[j]);
13327: fprintf(fichtm, "<th>+ V%d*age</th>",Tvar[j]);
13328: }else if(Typevar[j]==2) {
13329: fprintf(fichtm, "<th>+ V%d*V%d</th>",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
13330: }
13331: }
13332: fprintf(fichtm, "</tr>\n");
13333:
13334: for(i=1,jk=1; i <=nlstate; i++){
13335: for(k=1; k <=(nlstate+ndeath); k++){
13336: if (k != i) {
13337: fprintf(fichtm, "<tr valign=top>");
13338: printf("%d%d ",i,k);
13339: fprintf(ficlog,"%d%d ",i,k);
13340: fprintf(fichtm, "<td>%1d%1d</td>",i,k);
13341: for(j=1; j <=ncovmodel; j++){
13342: wald=p[jk]/sqrt(matcov[jk][jk]);
13343: printf("%12.7f(%12.7f) W=%8.3f CI=[%12.7f ; %12.7f] ",p[jk],sqrt(matcov[jk][jk]), p[jk]/sqrt(matcov[jk][jk]), p[jk]-1.96*sqrt(matcov[jk][jk]),p[jk]+1.96*sqrt(matcov[jk][jk]));
13344: fprintf(ficlog,"%12.7f(%12.7f) W=%8.3f CI=[%12.7f ; %12.7f] ",p[jk],sqrt(matcov[jk][jk]), p[jk]/sqrt(matcov[jk][jk]), p[jk]-1.96*sqrt(matcov[jk][jk]),p[jk]+1.96*sqrt(matcov[jk][jk]));
13345: if(fabs(wald) > 1.96){
13346: fprintf(fichtm, "<td><b>%12.7f</b></br> (%12.7f)</br>",p[jk],sqrt(matcov[jk][jk]));
13347: }else{
13348: fprintf(fichtm, "<td>%12.7f (%12.7f)</br>",p[jk],sqrt(matcov[jk][jk]));
13349: }
13350: fprintf(fichtm,"W=%8.3f</br>",wald);
13351: fprintf(fichtm,"[%12.7f;%12.7f]</br></td>", p[jk]-1.96*sqrt(matcov[jk][jk]),p[jk]+1.96*sqrt(matcov[jk][jk]));
13352: jk++;
13353: }
13354: printf("\n");
13355: fprintf(ficlog,"\n");
13356: fprintf(fichtm, "</tr>\n");
13357: }
13358: }
13359: }
13360: } /* end of hesscov and Wald tests */
13361: fprintf(fichtm,"</table>\n");
13362:
13363: /* */
13364: fprintf(ficres,"# Scales (for hessian or gradient estimation)\n");
13365: printf("# Scales (for hessian or gradient estimation)\n");
13366: fprintf(ficlog,"# Scales (for hessian or gradient estimation)\n");
13367: for(i=1,jk=1; i <=nlstate; i++){
13368: for(j=1; j <=nlstate+ndeath; j++){
13369: if (j!=i) {
13370: fprintf(ficres,"%1d%1d",i,j);
13371: printf("%1d%1d",i,j);
13372: fprintf(ficlog,"%1d%1d",i,j);
13373: for(k=1; k<=ncovmodel;k++){
13374: printf(" %.5e",delti[jk]);
13375: fprintf(ficlog," %.5e",delti[jk]);
13376: fprintf(ficres," %.5e",delti[jk]);
13377: jk++;
13378: }
13379: printf("\n");
13380: fprintf(ficlog,"\n");
13381: fprintf(ficres,"\n");
13382: }
13383: }
13384: }
13385:
13386: 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");
13387: if(mle >= 1) /* To big for the screen */
13388: 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");
13389: 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");
13390: /* # 121 Var(a12)\n\ */
13391: /* # 122 Cov(b12,a12) Var(b12)\n\ */
13392: /* # 131 Cov(a13,a12) Cov(a13,b12, Var(a13)\n\ */
13393: /* # 132 Cov(b13,a12) Cov(b13,b12, Cov(b13,a13) Var(b13)\n\ */
13394: /* # 212 Cov(a21,a12) Cov(a21,b12, Cov(a21,a13) Cov(a21,b13) Var(a21)\n\ */
13395: /* # 212 Cov(b21,a12) Cov(b21,b12, Cov(b21,a13) Cov(b21,b13) Cov(b21,a21) Var(b21)\n\ */
13396: /* # 232 Cov(a23,a12) Cov(a23,b12, Cov(a23,a13) Cov(a23,b13) Cov(a23,a21) Cov(a23,b21) Var(a23)\n\ */
13397: /* # 232 Cov(b23,a12) Cov(b23,b12) ... Var (b23)\n" */
13398:
13399:
13400: /* Just to have a covariance matrix which will be more understandable
13401: even is we still don't want to manage dictionary of variables
13402: */
13403: for(itimes=1;itimes<=2;itimes++){
13404: jj=0;
13405: for(i=1; i <=nlstate; i++){
13406: for(j=1; j <=nlstate+ndeath; j++){
13407: if(j==i) continue;
13408: for(k=1; k<=ncovmodel;k++){
13409: jj++;
13410: ca[0]= k+'a'-1;ca[1]='\0';
13411: if(itimes==1){
13412: if(mle>=1)
13413: printf("#%1d%1d%d",i,j,k);
13414: fprintf(ficlog,"#%1d%1d%d",i,j,k);
13415: fprintf(ficres,"#%1d%1d%d",i,j,k);
13416: }else{
13417: if(mle>=1)
13418: printf("%1d%1d%d",i,j,k);
13419: fprintf(ficlog,"%1d%1d%d",i,j,k);
13420: fprintf(ficres,"%1d%1d%d",i,j,k);
13421: }
13422: ll=0;
13423: for(li=1;li <=nlstate; li++){
13424: for(lj=1;lj <=nlstate+ndeath; lj++){
13425: if(lj==li) continue;
13426: for(lk=1;lk<=ncovmodel;lk++){
13427: ll++;
13428: if(ll<=jj){
13429: cb[0]= lk +'a'-1;cb[1]='\0';
13430: if(ll<jj){
13431: if(itimes==1){
13432: if(mle>=1)
13433: printf(" Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj);
13434: fprintf(ficlog," Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj);
13435: fprintf(ficres," Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj);
13436: }else{
13437: if(mle>=1)
13438: printf(" %.5e",matcov[jj][ll]);
13439: fprintf(ficlog," %.5e",matcov[jj][ll]);
13440: fprintf(ficres," %.5e",matcov[jj][ll]);
13441: }
13442: }else{
13443: if(itimes==1){
13444: if(mle>=1)
13445: printf(" Var(%s%1d%1d)",ca,i,j);
13446: fprintf(ficlog," Var(%s%1d%1d)",ca,i,j);
13447: fprintf(ficres," Var(%s%1d%1d)",ca,i,j);
13448: }else{
13449: if(mle>=1)
13450: printf(" %.7e",matcov[jj][ll]);
13451: fprintf(ficlog," %.7e",matcov[jj][ll]);
13452: fprintf(ficres," %.7e",matcov[jj][ll]);
13453: }
13454: }
13455: }
13456: } /* end lk */
13457: } /* end lj */
13458: } /* end li */
13459: if(mle>=1)
13460: printf("\n");
13461: fprintf(ficlog,"\n");
13462: fprintf(ficres,"\n");
13463: numlinepar++;
13464: } /* end k*/
13465: } /*end j */
13466: } /* end i */
13467: } /* end itimes */
13468:
13469: fflush(ficlog);
13470: fflush(ficres);
13471: while(fgets(line, MAXLINE, ficpar)) {
13472: /* If line starts with a # it is a comment */
13473: if (line[0] == '#') {
13474: numlinepar++;
13475: fputs(line,stdout);
13476: fputs(line,ficparo);
13477: fputs(line,ficlog);
13478: fputs(line,ficres);
13479: continue;
13480: }else
13481: break;
13482: }
13483:
13484: /* while((c=getc(ficpar))=='#' && c!= EOF){ */
13485: /* ungetc(c,ficpar); */
13486: /* fgets(line, MAXLINE, ficpar); */
13487: /* fputs(line,stdout); */
13488: /* fputs(line,ficparo); */
13489: /* } */
13490: /* ungetc(c,ficpar); */
13491:
13492: estepm=0;
13493: 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){
13494:
13495: if (num_filled != 6) {
13496: 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);
13497: 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);
13498: goto end;
13499: }
13500: printf("agemin=%lf agemax=%lf bage=%lf fage=%lf estepm=%d ftolpl=%lf\n",ageminpar,agemaxpar, bage, fage, estepm, ftolpl);
13501: }
13502: /* ftolpl=6*ftol*1.e5; /\* 6.e-3 make convergences in less than 80 loops for the prevalence limit *\/ */
13503: /*ftolpl=6.e-4;*/ /* 6.e-3 make convergences in less than 80 loops for the prevalence limit */
13504:
13505: /* fscanf(ficpar,"agemin=%lf agemax=%lf bage=%lf fage=%lf estepm=%d ftolpl=%\n",&ageminpar,&agemaxpar, &bage, &fage, &estepm); */
13506: if (estepm==0 || estepm < stepm) estepm=stepm;
13507: if (fage <= 2) {
13508: bage = ageminpar;
13509: fage = agemaxpar;
13510: }
13511:
13512: fprintf(ficres,"# agemin agemax for life expectancy, bage fage (if mle==0 ie no data nor Max likelihood).\n");
13513: fprintf(ficres,"agemin=%.0f agemax=%.0f bage=%.0f fage=%.0f estepm=%d ftolpl=%e\n",ageminpar,agemaxpar,bage,fage, estepm, ftolpl);
13514: fprintf(ficparo,"agemin=%.0f agemax=%.0f bage=%.0f fage=%.0f estepm=%d, ftolpl=%e\n",ageminpar,agemaxpar,bage,fage, estepm, ftolpl);
13515:
13516: /* Other stuffs, more or less useful */
13517: while(fgets(line, MAXLINE, ficpar)) {
13518: /* If line starts with a # it is a comment */
13519: if (line[0] == '#') {
13520: numlinepar++;
13521: fputs(line,stdout);
13522: fputs(line,ficparo);
13523: fputs(line,ficlog);
13524: fputs(line,ficres);
13525: continue;
13526: }else
13527: break;
13528: }
13529:
13530: if((num_filled=sscanf(line,"begin-prev-date=%lf/%lf/%lf end-prev-date=%lf/%lf/%lf mov_average=%d\n",&jprev1, &mprev1,&anprev1,&jprev2, &mprev2,&anprev2,&mobilav)) !=EOF){
13531:
13532: if (num_filled != 7) {
13533: printf("Error: Not 7 (data)parameters in line but %d, for example:begin-prev-date=1/1/1990 end-prev-date=1/6/2004 mov_average=0\n, your line=%s . Probably you are running an older format.\n",num_filled,line);
13534: fprintf(ficlog,"Error: Not 7 (data)parameters in line but %d, for example:begin-prev-date=1/1/1990 end-prev-date=1/6/2004 mov_average=0\n, your line=%s . Probably you are running an older format.\n",num_filled,line);
13535: goto end;
13536: }
13537: printf("begin-prev-date=%.lf/%.lf/%.lf end-prev-date=%.lf/%.lf/%.lf mov_average=%d\n",jprev1, mprev1,anprev1,jprev2, mprev2,anprev2,mobilav);
13538: 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);
13539: 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);
13540: 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);
13541: }
13542:
13543: while(fgets(line, MAXLINE, ficpar)) {
13544: /* If line starts with a # it is a comment */
13545: if (line[0] == '#') {
13546: numlinepar++;
13547: fputs(line,stdout);
13548: fputs(line,ficparo);
13549: fputs(line,ficlog);
13550: fputs(line,ficres);
13551: continue;
13552: }else
13553: break;
13554: }
13555:
13556:
13557: dateprev1=anprev1+(mprev1-1)/12.+(jprev1-1)/365.;
13558: dateprev2=anprev2+(mprev2-1)/12.+(jprev2-1)/365.;
13559:
13560: if((num_filled=sscanf(line,"pop_based=%d\n",&popbased)) !=EOF){
13561: if (num_filled != 1) {
13562: printf("Error: Not 1 (data)parameters in line but %d, for example:pop_based=0\n, your line=%s . Probably you are running an older format.\n",num_filled,line);
13563: fprintf(ficlog,"Error: Not 1 (data)parameters in line but %d, for example: pop_based=1\n, your line=%s . Probably you are running an older format.\n",num_filled,line);
13564: goto end;
13565: }
13566: printf("pop_based=%d\n",popbased);
13567: fprintf(ficlog,"pop_based=%d\n",popbased);
13568: fprintf(ficparo,"pop_based=%d\n",popbased);
13569: fprintf(ficres,"pop_based=%d\n",popbased);
13570: }
13571:
13572: /* Results */
13573: /* Value of covariate in each resultine will be compututed (if product) and sorted according to model rank */
13574: /* It is precov[] because we need the varying age in order to compute the real cov[] of the model equation */
13575: precov=matrix(1,MAXRESULTLINESPONE,1,NCOVMAX+1);
13576: endishere=0;
13577: nresult=0;
13578: parameterline=0;
13579: do{
13580: if(!fgets(line, MAXLINE, ficpar)){
13581: endishere=1;
13582: parameterline=15;
13583: }else if (line[0] == '#') {
13584: /* If line starts with a # it is a comment */
13585: numlinepar++;
13586: fputs(line,stdout);
13587: fputs(line,ficparo);
13588: fputs(line,ficlog);
13589: fputs(line,ficres);
13590: continue;
13591: }else if(sscanf(line,"prevforecast=%[^\n]\n",modeltemp))
13592: parameterline=11;
13593: else if(sscanf(line,"prevbackcast=%[^\n]\n",modeltemp))
13594: parameterline=12;
13595: else if(sscanf(line,"result:%[^\n]\n",modeltemp)){
13596: parameterline=13;
13597: }
13598: else{
13599: parameterline=14;
13600: }
13601: switch (parameterline){ /* =0 only if only comments */
13602: case 11:
13603: if((num_filled=sscanf(line,"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)) !=EOF && (num_filled == 8)){
13604: 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);
13605: 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);
13606: 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);
13607: 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);
13608: /* day and month of proj2 are not used but only year anproj2.*/
13609: dateproj1=anproj1+(mproj1-1)/12.+(jproj1-1)/365.;
13610: dateproj2=anproj2+(mproj2-1)/12.+(jproj2-1)/365.;
13611: prvforecast = 1;
13612: }
13613: else if((num_filled=sscanf(line,"prevforecast=%d yearsfproj=%lf mobil_average=%d\n",&prevfcast,&yrfproj,&mobilavproj)) !=EOF){/* && (num_filled == 3))*/
13614: printf("prevforecast=%d yearsfproj=%.2lf mobil_average=%d\n",prevfcast,yrfproj,mobilavproj);
13615: fprintf(ficlog,"prevforecast=%d yearsfproj=%.2lf mobil_average=%d\n",prevfcast,yrfproj,mobilavproj);
13616: fprintf(ficres,"prevforecast=%d yearsfproj=%.2lf mobil_average=%d\n",prevfcast,yrfproj,mobilavproj);
13617: prvforecast = 2;
13618: }
13619: else {
13620: printf("Error: Not 8 (data)parameters in line but %d, for example:prevforecast=1 starting-proj-date=1/1/1990 final-proj-date=1/1/2000 mobil_average=0\nnor 3 (data)parameters, for example:prevforecast=1 yearsfproj=10 mobil_average=0. Your line=%s . You are running probably an older format.\n, ",num_filled,line);
13621: fprintf(ficlog,"Error: Not 8 (data)parameters in line but %d, for example:prevforecast=1 starting-proj-date=1/1/1990 final-proj-date=1/1/2000 mobil_average=0\nnor 3 (data)parameters, for example:prevforecast=1 yearproj=10 mobil_average=0. Your line=%s . You are running probably an older format.\n, ",num_filled,line);
13622: goto end;
13623: }
13624: break;
13625: case 12:
13626: if((num_filled=sscanf(line,"prevbackcast=%d starting-back-date=%lf/%lf/%lf final-back-date=%lf/%lf/%lf mobil_average=%d\n",&prevbcast,&jback1,&mback1,&anback1,&jback2,&mback2,&anback2,&mobilavproj)) !=EOF && (num_filled == 8)){
13627: fprintf(ficparo,"prevbackcast=%d starting-back-date=%.lf/%.lf/%.lf final-back-date=%.lf/%.lf/%.lf mobil_average=%d\n",prevbcast,jback1,mback1,anback1,jback2,mback2,anback2,mobilavproj);
13628: printf("prevbackcast=%d starting-back-date=%.lf/%.lf/%.lf final-back-date=%.lf/%.lf/%.lf mobil_average=%d\n",prevbcast,jback1,mback1,anback1,jback2,mback2,anback2,mobilavproj);
13629: fprintf(ficlog,"prevbackcast=%d starting-back-date=%.lf/%.lf/%.lf final-back-date=%.lf/%.lf/%.lf mobil_average=%d\n",prevbcast,jback1,mback1,anback1,jback2,mback2,anback2,mobilavproj);
13630: fprintf(ficres,"prevbackcast=%d starting-back-date=%.lf/%.lf/%.lf final-back-date=%.lf/%.lf/%.lf mobil_average=%d\n",prevbcast,jback1,mback1,anback1,jback2,mback2,anback2,mobilavproj);
13631: /* day and month of back2 are not used but only year anback2.*/
13632: dateback1=anback1+(mback1-1)/12.+(jback1-1)/365.;
13633: dateback2=anback2+(mback2-1)/12.+(jback2-1)/365.;
13634: prvbackcast = 1;
13635: }
13636: else if((num_filled=sscanf(line,"prevbackcast=%d yearsbproj=%lf mobil_average=%d\n",&prevbcast,&yrbproj,&mobilavproj)) ==3){/* && (num_filled == 3))*/
13637: printf("prevbackcast=%d yearsbproj=%.2lf mobil_average=%d\n",prevbcast,yrbproj,mobilavproj);
13638: fprintf(ficlog,"prevbackcast=%d yearsbproj=%.2lf mobil_average=%d\n",prevbcast,yrbproj,mobilavproj);
13639: fprintf(ficres,"prevbackcast=%d yearsbproj=%.2lf mobil_average=%d\n",prevbcast,yrbproj,mobilavproj);
13640: prvbackcast = 2;
13641: }
13642: else {
13643: printf("Error: Not 8 (data)parameters in line but %d, for example:prevbackcast=1 starting-back-date=1/1/1990 final-back-date=1/1/2000 mobil_average=0\nnor 3 (data)parameters, for example:prevbackcast=1 yearsbproj=10 mobil_average=0. Your line=%s . You are running probably an older format.\n, ",num_filled,line);
13644: fprintf(ficlog,"Error: Not 8 (data)parameters in line but %d, for example:prevbackcast=1 starting-back-date=1/1/1990 final-back-date=1/1/2000 mobil_average=0\nnor 3 (data)parameters, for example:prevbackcast=1 yearbproj=10 mobil_average=0. Your line=%s . You are running probably an older format.\n, ",num_filled,line);
13645: goto end;
13646: }
13647: break;
13648: case 13:
13649: num_filled=sscanf(line,"result:%[^\n]\n",resultlineori);
13650: nresult++; /* Sum of resultlines */
13651: printf("Result %d: result:%s\n",nresult, resultlineori);
13652: /* removefirstspace(&resultlineori); */
13653:
13654: if(strstr(resultlineori,"v") !=0){
13655: printf("Error. 'v' must be in upper case 'V' result: %s ",resultlineori);
13656: fprintf(ficlog,"Error. 'v' must be in upper case result: %s ",resultlineori);fflush(ficlog);
13657: return 1;
13658: }
13659: trimbb(resultline, resultlineori); /* Suppressing double blank in the resultline */
13660: printf("Decoderesult resultline=\"%s\" resultlineori=\"%s\"\n", resultline, resultlineori);
13661: if(nresult > MAXRESULTLINESPONE-1){
13662: printf("ERROR: Current version of IMaCh limits the number of resultlines to %d, you used %d\nYou can use the 'r' parameter file '%s' which uses option mle=0 to get other results. ",MAXRESULTLINESPONE-1,nresult,rfileres);
13663: fprintf(ficlog,"ERROR: Current version of IMaCh limits the number of resultlines to %d, you used %d\nYou can use the 'r' parameter file '%s' which uses option mle=0 to get other results. ",MAXRESULTLINESPONE-1,nresult,rfileres);
13664: goto end;
13665: }
13666:
13667: if(!decoderesult(resultline, nresult)){ /* Fills TKresult[nresult] combination and Tresult[nresult][k4+1] combination values */
13668: fprintf(ficparo,"result: %s\n",resultline);
13669: fprintf(ficres,"result: %s\n",resultline);
13670: fprintf(ficlog,"result: %s\n",resultline);
13671: } else
13672: goto end;
13673: break;
13674: case 14:
13675: printf("Error: Unknown command '%s'\n",line);
13676: fprintf(ficlog,"Error: Unknown command '%s'\n",line);
13677: if(line[0] == ' ' || line[0] == '\n'){
13678: printf("It should not be an empty line '%s'\n",line);
13679: fprintf(ficlog,"It should not be an empty line '%s'\n",line);
13680: }
13681: if(ncovmodel >=2 && nresult==0 ){
13682: printf("ERROR: no result lines! It should be at minimum 'result: V2=0 V1=1 or result:.' %s\n",line);
13683: fprintf(ficlog,"ERROR: no result lines! It should be at minimum 'result: V2=0 V1=1 or result:.' %s\n",line);
13684: }
13685: /* goto end; */
13686: break;
13687: case 15:
13688: printf("End of resultlines.\n");
13689: fprintf(ficlog,"End of resultlines.\n");
13690: break;
13691: default: /* parameterline =0 */
13692: nresult=1;
13693: decoderesult(".",nresult ); /* No covariate */
13694: } /* End switch parameterline */
13695: }while(endishere==0); /* End do */
13696:
13697: /* freqsummary(fileres, agemin, agemax, s, agev, nlstate, imx,Tvaraff,nbcode, ncodemax,mint,anint); */
13698: /* ,dateprev1,dateprev2,jprev1, mprev1,anprev1,jprev2, mprev2,anprev2); */
13699:
13700: replace_back_to_slash(pathc,pathcd); /* Even gnuplot wants a / */
13701: if(ageminpar == AGEOVERFLOW ||agemaxpar == -AGEOVERFLOW){
13702: printf("Warning! Error in gnuplot file with ageminpar %f or agemaxpar %f overflow\n\
13703: This is probably because your parameter file doesn't \n contain the exact number of lines (or columns) corresponding to your model line.\n\
13704: Please run with mle=-1 to get a correct covariance matrix.\n",ageminpar,agemaxpar);
13705: fprintf(ficlog,"Warning! Error in gnuplot file with ageminpar %f or agemaxpar %f overflow\n\
13706: This is probably because your parameter file doesn't \n contain the exact number of lines (or columns) corresponding to your model line.\n\
13707: Please run with mle=-1 to get a correct covariance matrix.\n",ageminpar,agemaxpar);
13708: }else{
13709: /* printinggnuplot(fileresu, optionfilefiname,ageminpar,agemaxpar,fage, prevfcast, backcast, pathc,p, (int)anproj1-(int)agemin, (int)anback1-(int)agemax+1); */
13710: /* It seems that anprojd which is computed from the mean year at interview which is known yet because of freqsummary */
13711: /* date2dmy(dateintmean,&jintmean,&mintmean,&aintmean); */ /* Done in freqsummary */
13712: if(prvforecast==1){
13713: dateprojd=(jproj1+12*mproj1+365*anproj1)/365;
13714: jprojd=jproj1;
13715: mprojd=mproj1;
13716: anprojd=anproj1;
13717: dateprojf=(jproj2+12*mproj2+365*anproj2)/365;
13718: jprojf=jproj2;
13719: mprojf=mproj2;
13720: anprojf=anproj2;
13721: } else if(prvforecast == 2){
13722: dateprojd=dateintmean;
13723: date2dmy(dateprojd,&jprojd, &mprojd, &anprojd);
13724: dateprojf=dateintmean+yrfproj;
13725: date2dmy(dateprojf,&jprojf, &mprojf, &anprojf);
13726: }
13727: if(prvbackcast==1){
13728: datebackd=(jback1+12*mback1+365*anback1)/365;
13729: jbackd=jback1;
13730: mbackd=mback1;
13731: anbackd=anback1;
13732: datebackf=(jback2+12*mback2+365*anback2)/365;
13733: jbackf=jback2;
13734: mbackf=mback2;
13735: anbackf=anback2;
13736: } else if(prvbackcast == 2){
13737: datebackd=dateintmean;
13738: date2dmy(datebackd,&jbackd, &mbackd, &anbackd);
13739: datebackf=dateintmean-yrbproj;
13740: date2dmy(datebackf,&jbackf, &mbackf, &anbackf);
13741: }
13742:
13743: printinggnuplot(fileresu, optionfilefiname,ageminpar,agemaxpar,bage, fage, prevfcast, prevbcast, pathc,p, (int)anprojd-bage, (int)anbackd-fage);
13744: }
13745: printinghtml(fileresu,title,datafile, firstpass, lastpass, stepm, weightopt, \
13746: model,imx,jmin,jmax,jmean,rfileres,popforecast,mobilav,prevfcast,mobilavproj,prevbcast, estepm, \
13747: jprev1,mprev1,anprev1,dateprev1, dateprojd, datebackd,jprev2,mprev2,anprev2,dateprev2,dateprojf, datebackf);
13748:
13749: /*------------ free_vector -------------*/
13750: /* chdir(path); */
13751:
13752: /* free_ivector(wav,1,imx); */ /* Moved after last prevalence call */
13753: /* free_imatrix(dh,1,lastpass-firstpass+2,1,imx); */
13754: /* free_imatrix(bh,1,lastpass-firstpass+2,1,imx); */
13755: /* free_imatrix(mw,1,lastpass-firstpass+2,1,imx); */
13756: free_lvector(num,firstobs,lastobs);
13757: free_vector(agedc,firstobs,lastobs);
13758: /*free_matrix(covar,0,NCOVMAX,1,n);*/
13759: /*free_matrix(covar,1,NCOVMAX,1,n);*/
13760: fclose(ficparo);
13761: fclose(ficres);
13762:
13763:
13764: /* Other results (useful)*/
13765:
13766:
13767: /*--------------- Prevalence limit (period or stable prevalence) --------------*/
13768: /*#include "prevlim.h"*/ /* Use ficrespl, ficlog */
13769: prlim=matrix(1,nlstate,1,nlstate);
13770: /* Computes the prevalence limit for each combination k of the dummy covariates by calling prevalim(k) */
13771: prevalence_limit(p, prlim, ageminpar, agemaxpar, ftolpl, &ncvyear);
13772: fclose(ficrespl);
13773:
13774: /*------------- h Pij x at various ages ------------*/
13775: /*#include "hpijx.h"*/
13776: /** h Pij x Probability to be in state j at age x+h being in i at x, for each combination k of dummies in the model line or to nres?*/
13777: /* calls hpxij with combination k */
13778: hPijx(p, bage, fage);
13779: fclose(ficrespij);
13780:
13781: /* ncovcombmax= pow(2,cptcoveff); */
13782: /*-------------- Variance of one-step probabilities for a combination ij or for nres ?---*/
13783: k=1;
13784: varprob(optionfilefiname, matcov, p, delti, nlstate, bage, fage,k,Tvar,nbcode, ncodemax,strstart);
13785:
13786: /* Prevalence for each covariate combination in probs[age][status][cov] */
13787: probs= ma3x(AGEINF,AGESUP,1,nlstate+ndeath, 1,ncovcombmax);
13788: for(i=AGEINF;i<=AGESUP;i++)
13789: for(j=1;j<=nlstate+ndeath;j++) /* ndeath is useless but a necessity to be compared with mobaverages */
13790: for(k=1;k<=ncovcombmax;k++)
13791: probs[i][j][k]=0.;
13792: prevalence(probs, ageminpar, agemaxpar, s, agev, nlstate, imx, Tvar, nbcode,
13793: ncodemax, mint, anint, dateprev1, dateprev2, firstpass, lastpass);
13794: if (mobilav!=0 ||mobilavproj !=0 ) {
13795: mobaverages= ma3x(AGEINF, AGESUP,1,nlstate+ndeath, 1,ncovcombmax);
13796: for(i=AGEINF;i<=AGESUP;i++)
13797: for(j=1;j<=nlstate+ndeath;j++)
13798: for(k=1;k<=ncovcombmax;k++)
13799: mobaverages[i][j][k]=0.;
13800: mobaverage=mobaverages;
13801: if (mobilav!=0) {
13802: printf("Movingaveraging observed prevalence\n");
13803: fprintf(ficlog,"Movingaveraging observed prevalence\n");
13804: if (movingaverage(probs, ageminpar, agemaxpar, mobaverage, mobilav)!=0){
13805: fprintf(ficlog," Error in movingaverage mobilav=%d\n",mobilav);
13806: printf(" Error in movingaverage mobilav=%d\n",mobilav);
13807: }
13808: } else if (mobilavproj !=0) {
13809: printf("Movingaveraging projected observed prevalence\n");
13810: fprintf(ficlog,"Movingaveraging projected observed prevalence\n");
13811: if (movingaverage(probs, ageminpar, agemaxpar, mobaverage, mobilavproj)!=0){
13812: fprintf(ficlog," Error in movingaverage mobilavproj=%d\n",mobilavproj);
13813: printf(" Error in movingaverage mobilavproj=%d\n",mobilavproj);
13814: }
13815: }else{
13816: printf("Internal error moving average\n");
13817: fflush(stdout);
13818: exit(1);
13819: }
13820: }/* end if moving average */
13821:
13822: /*---------- Forecasting ------------------*/
13823: if(prevfcast==1){
13824: /* /\* if(stepm ==1){*\/ */
13825: /* /\* anproj1, mproj1, jproj1 either read explicitly or yrfproj *\/ */
13826: /*This done previously after freqsummary.*/
13827: /* dateprojd=(jproj1+12*mproj1+365*anproj1)/365; */
13828: /* dateprojf=(jproj2+12*mproj2+365*anproj2)/365; */
13829:
13830: /* } else if (prvforecast==2){ */
13831: /* /\* if(stepm ==1){*\/ */
13832: /* /\* anproj1, mproj1, jproj1 either read explicitly or yrfproj *\/ */
13833: /* } */
13834: /*prevforecast(fileresu, dateintmean, anproj1, mproj1, jproj1, agemin, agemax, dateprev1, dateprev2, mobilavproj, mobaverage, bage, fage, firstpass, lastpass, anproj2, p, cptcoveff);*/
13835: prevforecast(fileresu,dateintmean, dateprojd, dateprojf, agemin, agemax, dateprev1, dateprev2, mobilavproj, mobaverage, bage, fage, firstpass, lastpass, p, cptcoveff);
13836: }
13837:
13838: /* Prevbcasting */
13839: if(prevbcast==1){
13840: ddnewms=matrix(1,nlstate+ndeath,1,nlstate+ndeath);
13841: ddoldms=matrix(1,nlstate+ndeath,1,nlstate+ndeath);
13842: ddsavms=matrix(1,nlstate+ndeath,1,nlstate+ndeath);
13843:
13844: /*--------------- Back Prevalence limit (period or stable prevalence) --------------*/
13845:
13846: bprlim=matrix(1,nlstate,1,nlstate);
13847:
13848: back_prevalence_limit(p, bprlim, ageminpar, agemaxpar, ftolpl, &ncvyear, dateprev1, dateprev2, firstpass, lastpass, mobilavproj);
13849: fclose(ficresplb);
13850:
13851: hBijx(p, bage, fage, mobaverage);
13852: fclose(ficrespijb);
13853:
13854: /* /\* prevbackforecast(fileresu, mobaverage, anback1, mback1, jback1, agemin, agemax, dateprev1, dateprev2, *\/ */
13855: /* /\* mobilavproj, bage, fage, firstpass, lastpass, anback2, p, cptcoveff); *\/ */
13856: /* prevbackforecast(fileresu, mobaverage, anback1, mback1, jback1, agemin, agemax, dateprev1, dateprev2, */
13857: /* mobilavproj, bage, fage, firstpass, lastpass, anback2, p, cptcoveff); */
13858: prevbackforecast(fileresu, mobaverage, dateintmean, dateprojd, dateprojf, agemin, agemax, dateprev1, dateprev2,
13859: mobilavproj, bage, fage, firstpass, lastpass, p, cptcoveff);
13860:
13861:
13862: varbprlim(fileresu, nresult, mobaverage, mobilavproj, bage, fage, bprlim, &ncvyear, ftolpl, p, matcov, delti, stepm, cptcoveff);
13863:
13864:
13865: free_matrix(bprlim,1,nlstate,1,nlstate); /*here or after loop ? */
13866: free_matrix(ddnewms, 1, nlstate+ndeath, 1, nlstate+ndeath);
13867: free_matrix(ddsavms, 1, nlstate+ndeath, 1, nlstate+ndeath);
13868: free_matrix(ddoldms, 1, nlstate+ndeath, 1, nlstate+ndeath);
13869: } /* end Prevbcasting */
13870:
13871:
13872: /* ------ Other prevalence ratios------------ */
13873:
13874: free_ivector(wav,1,imx);
13875: free_imatrix(dh,1,lastpass-firstpass+2,1,imx);
13876: free_imatrix(bh,1,lastpass-firstpass+2,1,imx);
13877: free_imatrix(mw,1,lastpass-firstpass+2,1,imx);
13878:
13879:
13880: /*---------- Health expectancies, no variances ------------*/
13881:
13882: strcpy(filerese,"E_");
13883: strcat(filerese,fileresu);
13884: if((ficreseij=fopen(filerese,"w"))==NULL) {
13885: printf("Problem with Health Exp. resultfile: %s\n", filerese); exit(0);
13886: fprintf(ficlog,"Problem with Health Exp. resultfile: %s\n", filerese); exit(0);
13887: }
13888: printf("Computing Health Expectancies: result on file '%s' ...", filerese);fflush(stdout);
13889: fprintf(ficlog,"Computing Health Expectancies: result on file '%s' ...", filerese);fflush(ficlog);
13890:
13891: pstamp(ficreseij);
13892:
13893: i1=pow(2,cptcoveff); /* Number of combination of dummy covariates */
13894: if (cptcovn < 1){i1=1;}
13895:
13896: for(nres=1; nres <= nresult; nres++) /* For each resultline */
13897: for(k=1; k<=i1;k++){ /* For any combination of dummy covariates, fixed and varying */
13898: if(i1 != 1 && TKresult[nres]!= k)
13899: continue;
13900: fprintf(ficreseij,"\n#****** ");
13901: printf("\n#****** ");
13902: for(j=1;j<=cptcoveff;j++) {
13903: fprintf(ficreseij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]);
13904: printf("V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]);
13905: }
13906: for (j=1; j<= nsq; j++){ /* For each selected (single) quantitative value */
13907: printf(" V%d=%lg ",TvarsQ[j], TinvDoQresult[nres][TvarsQ[j]]); /* TvarsQ[j] gives the name of the jth quantitative (fixed or time v) */
13908: fprintf(ficreseij,"V%d=%lg ",TvarsQ[j], TinvDoQresult[nres][TvarsQ[j]]);
13909: }
13910: fprintf(ficreseij,"******\n");
13911: printf("******\n");
13912:
13913: eij=ma3x(1,nlstate,1,nlstate,(int) bage, (int) fage);
13914: oldm=oldms;savm=savms;
13915: /* printf("HELLO Entering evsij bage=%d fage=%d k=%d estepm=%d nres=%d\n",(int) bage, (int)fage, k, estepm, nres); */
13916: evsij(eij, p, nlstate, stepm, (int) bage, (int)fage, oldm, savm, k, estepm, strstart, nres);
13917:
13918: free_ma3x(eij,1,nlstate,1,nlstate,(int) bage, (int)fage);
13919: }
13920: fclose(ficreseij);
13921: printf("done evsij\n");fflush(stdout);
13922: fprintf(ficlog,"done evsij\n");fflush(ficlog);
13923:
13924:
13925: /*---------- State-specific expectancies and variances ------------*/
13926: /* Should be moved in a function */
13927: strcpy(filerest,"T_");
13928: strcat(filerest,fileresu);
13929: if((ficrest=fopen(filerest,"w"))==NULL) {
13930: printf("Problem with total LE resultfile: %s\n", filerest);goto end;
13931: fprintf(ficlog,"Problem with total LE resultfile: %s\n", filerest);goto end;
13932: }
13933: printf("Computing Total Life expectancies with their standard errors: file '%s' ...\n", filerest); fflush(stdout);
13934: fprintf(ficlog,"Computing Total Life expectancies with their standard errors: file '%s' ...\n", filerest); fflush(ficlog);
13935: strcpy(fileresstde,"STDE_");
13936: strcat(fileresstde,fileresu);
13937: if((ficresstdeij=fopen(fileresstde,"w"))==NULL) {
13938: printf("Problem with State specific Exp. and std errors resultfile: %s\n", fileresstde); exit(0);
13939: fprintf(ficlog,"Problem with State specific Exp. and std errors resultfile: %s\n", fileresstde); exit(0);
13940: }
13941: printf(" Computing State-specific Expectancies and standard errors: result on file '%s' \n", fileresstde);
13942: fprintf(ficlog," Computing State-specific Expectancies and standard errors: result on file '%s' \n", fileresstde);
13943:
13944: strcpy(filerescve,"CVE_");
13945: strcat(filerescve,fileresu);
13946: if((ficrescveij=fopen(filerescve,"w"))==NULL) {
13947: printf("Problem with Covar. State-specific Exp. resultfile: %s\n", filerescve); exit(0);
13948: fprintf(ficlog,"Problem with Covar. State-specific Exp. resultfile: %s\n", filerescve); exit(0);
13949: }
13950: printf(" Computing Covar. of State-specific Expectancies: result on file '%s' \n", filerescve);
13951: fprintf(ficlog," Computing Covar. of State-specific Expectancies: result on file '%s' \n", filerescve);
13952:
13953: strcpy(fileresv,"V_");
13954: strcat(fileresv,fileresu);
13955: if((ficresvij=fopen(fileresv,"w"))==NULL) {
13956: printf("Problem with variance resultfile: %s\n", fileresv);exit(0);
13957: fprintf(ficlog,"Problem with variance resultfile: %s\n", fileresv);exit(0);
13958: }
13959: printf(" Computing Variance-covariance of State-specific Expectancies: file '%s' ... ", fileresv);fflush(stdout);
13960: fprintf(ficlog," Computing Variance-covariance of State-specific Expectancies: file '%s' ... ", fileresv);fflush(ficlog);
13961:
13962: i1=pow(2,cptcoveff); /* Number of combination of dummy covariates */
13963: if (cptcovn < 1){i1=1;}
13964:
13965: for(nres=1; nres <= nresult; nres++) /* For each resultline, find the combination and output results according to the values of dummies and then quanti. */
13966: for(k=1; k<=i1;k++){ /* For any combination of dummy covariates, fixed and varying. For each nres and each value at position k
13967: * we know Tresult[nres][result_position]= value of the dummy variable at the result_position in the nres resultline
13968: * Tvqresult[nres][result_position]= id of the variable at the result_position in the nres resultline
13969: * and Tqresult[nres][result_position]= value of the variable at the result_position in the nres resultline */
13970: /* */
13971: if(i1 != 1 && TKresult[nres]!= k) /* TKresult[nres] is the combination of this nres resultline. All the i1 combinations are not output */
13972: continue;
13973: printf("\n# model %s \n#****** Result for:", model);
13974: fprintf(ficrest,"\n# model %s \n#****** Result for:", model);
13975: fprintf(ficlog,"\n# model %s \n#****** Result for:", model);
13976: /* It might not be a good idea to mix dummies and quantitative */
13977: /* for(j=1;j<=cptcoveff;j++){ /\* j=resultpos. Could be a loop on cptcovs: number of single dummy covariate in the result line as well as in the model *\/ */
13978: for(j=1;j<=cptcovs;j++){ /* j=resultpos. Could be a loop on cptcovs: number of single covariate (dummy or quantitative) in the result line as well as in the model */
13979: /* printf("V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); /\* Output by variables in the resultline *\/ */
13980: /* Tvaraff[j] is the name of the dummy variable in position j in the equation model:
13981: * Tvaraff[1]@9={4, 3, 0, 0, 0, 0, 0, 0, 0}, in model=V5+V4+V3+V4*V3+V5*age
13982: * (V5 is quanti) V4 and V3 are dummies
13983: * TnsdVar[4] is the position 1 and TnsdVar[3]=2 in codtabm(k,l)(V4 V3)=V4 V3
13984: * l=1 l=2
13985: * k=1 1 1 0 0
13986: * k=2 2 1 1 0
13987: * k=3 [1] [2] 0 1
13988: * k=4 2 2 1 1
13989: * If nres=1 result: V3=1 V4=0 then k=3 and outputs
13990: * If nres=2 result: V4=1 V3=0 then k=2 and outputs
13991: * nres=1 =>k=3 j=1 V4= nbcode[4][codtabm(3,1)=1)=0; j=2 V3= nbcode[3][codtabm(3,2)=2]=1
13992: * nres=2 =>k=2 j=1 V4= nbcode[4][codtabm(2,1)=2)=1; j=2 V3= nbcode[3][codtabm(2,2)=1]=0
13993: */
13994: /* Tvresult[nres][j] Name of the variable at position j in this resultline */
13995: /* Tresult[nres][j] Value of this variable at position j could be a float if quantitative */
13996: /* We give up with the combinations!! */
13997: printf("\n j=%d In computing T_ Dummy[modelresult[%d][%d]]=%d, modelresult[%d][%d]=%d cptcovs=%d, cptcoveff=%d Fixed[modelresult[nres][j]]=%d\n", j, nres, j, Dummy[modelresult[nres][j]],nres,j,modelresult[nres][j],cptcovs, cptcoveff,Fixed[modelresult[nres][j]]); /* end if dummy or quanti */
13998:
13999: if(Dummy[modelresult[nres][j]]==0){/* Dummy variable of the variable in position modelresult in the model corresponding to j in resultline */
14000: printf("V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][j]); /* Output of each value for the combination TKresult[nres], ordere by the covariate values in the resultline */
14001: fprintf(ficlog,"V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][j]); /* Output of each value for the combination TKresult[nres], ordere by the covariate values in the resultline */
14002: fprintf(ficrest,"V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][j]); /* Output of each value for the combination TKresult[nres], ordere by the covariate values in the resultline */
14003: if(Fixed[modelresult[nres][j]]==0){ /* Fixed */
14004: printf("fixed ");fprintf(ficlog,"fixed ");fprintf(ficrest,"fixed ");
14005: }else{
14006: printf("varyi ");fprintf(ficlog,"varyi ");fprintf(ficrest,"varyi ");
14007: }
14008: /* fprintf(ficrest,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
14009: /* fprintf(ficlog,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
14010: }else if(Dummy[modelresult[nres][j]]==1){ /* Quanti variable */
14011: /* For each selected (single) quantitative value */
14012: printf(" V%d=%lg ",Tvqresult[nres][j],Tqresult[nres][j]);
14013: fprintf(ficlog," V%d=%lg ",Tvqresult[nres][j],Tqresult[nres][j]);
14014: fprintf(ficrest," V%d=%lg ",Tvqresult[nres][j],Tqresult[nres][j]);
14015: if(Fixed[modelresult[nres][j]]==0){ /* Fixed */
14016: printf("fixed ");fprintf(ficlog,"fixed ");fprintf(ficrest,"fixed ");
14017: }else{
14018: printf("varyi ");fprintf(ficlog,"varyi ");fprintf(ficrest,"varyi ");
14019: }
14020: }else{
14021: printf("Error in computing T_ Dummy[modelresult[%d][%d]]=%d, modelresult[%d][%d]=%d cptcovs=%d, cptcoveff=%d \n", nres, j, Dummy[modelresult[nres][j]],nres,j,modelresult[nres][j],cptcovs, cptcoveff); /* end if dummy or quanti */
14022: fprintf(ficlog,"Error in computing T_ Dummy[modelresult[%d][%d]]=%d, modelresult[%d][%d]=%d cptcovs=%d, cptcoveff=%d \n", nres, j, Dummy[modelresult[nres][j]],nres,j,modelresult[nres][j],cptcovs, cptcoveff); /* end if dummy or quanti */
14023: exit(1);
14024: }
14025: } /* End loop for each variable in the resultline */
14026: /* for (j=1; j<= nsq; j++){ /\* For each selected (single) quantitative value *\/ */
14027: /* printf(" V%d=%f ",Tvqresult[nres][j],Tqresult[nres][resultmodel[nres][j]]); /\* Wrong j is not in the equation model *\/ */
14028: /* fprintf(ficrest," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][resultmodel[nres][j]]); */
14029: /* fprintf(ficlog," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][resultmodel[nres][j]]); */
14030: /* } */
14031: fprintf(ficrest,"******\n");
14032: fprintf(ficlog,"******\n");
14033: printf("******\n");
14034:
14035: fprintf(ficresstdeij,"\n#****** ");
14036: fprintf(ficrescveij,"\n#****** ");
14037: /* It could have been: for(j=1;j<=cptcoveff;j++) {printf("V=%d=%lg",Tvresult[nres][cpt],TinvDoQresult[nres][Tvresult[nres][cpt]]);} */
14038: /* But it won't be sorted and depends on how the resultline is ordered */
14039: for(j=1;j<=cptcoveff;j++) {
14040: fprintf(ficresstdeij,"V%d=%d ",Tvresult[nres][j],Tresult[nres][j]);
14041: /* fprintf(ficresstdeij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
14042: /* fprintf(ficrescveij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
14043: }
14044: for (j=1; j<= nsq; j++){ /* For each selected (single) quantitative value, TvarsQind gives the position of a quantitative in model equation */
14045: fprintf(ficresstdeij," V%d=%lg ",Tvar[TvarsQind[j]],Tqresult[nres][resultmodel[nres][TvarsQind[j]]]);
14046: fprintf(ficrescveij," V%d=%lg ",Tvar[TvarsQind[j]],Tqresult[nres][resultmodel[nres][TvarsQind[j]]]);
14047: }
14048: fprintf(ficresstdeij,"******\n");
14049: fprintf(ficrescveij,"******\n");
14050:
14051: fprintf(ficresvij,"\n#****** ");
14052: /* pstamp(ficresvij); */
14053: for(j=1;j<=cptcoveff;j++)
14054: fprintf(ficresvij,"V%d=%d ",Tvresult[nres][j],Tresult[nres][j]);
14055: /* fprintf(ficresvij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[TnsdVar[Tvaraff[j]]])]); */
14056: for (j=1; j<= nsq; j++){ /* For each selected (single) quantitative value */
14057: /* fprintf(ficresvij," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]); /\* To solve *\/ */
14058: fprintf(ficresvij," V%d=%lg ",Tvqresult[nres][j],Tqresult[nres][resultmodel[nres][j]]); /* Solved */
14059: }
14060: fprintf(ficresvij,"******\n");
14061:
14062: eij=ma3x(1,nlstate,1,nlstate,(int) bage, (int) fage);
14063: oldm=oldms;savm=savms;
14064: printf(" cvevsij ");
14065: fprintf(ficlog, " cvevsij ");
14066: cvevsij(eij, p, nlstate, stepm, (int) bage, (int)fage, oldm, savm, k, estepm, delti, matcov, strstart, nres);
14067: printf(" end cvevsij \n ");
14068: fprintf(ficlog, " end cvevsij \n ");
14069:
14070: /*
14071: */
14072: /* goto endfree; */
14073:
14074: vareij=ma3x(1,nlstate,1,nlstate,(int) bage, (int) fage);
14075: pstamp(ficrest);
14076:
14077: epj=vector(1,nlstate+1);
14078: for(vpopbased=0; vpopbased <= popbased; vpopbased++){ /* Done for vpopbased=0 and vpopbased=1 if popbased==1*/
14079: oldm=oldms;savm=savms; /* ZZ Segmentation fault */
14080: cptcod= 0; /* To be deleted */
14081: printf("varevsij vpopbased=%d \n",vpopbased);
14082: fprintf(ficlog, "varevsij vpopbased=%d \n",vpopbased);
14083: varevsij(optionfilefiname, vareij, matcov, p, delti, nlstate, stepm, (int) bage, (int) fage, oldm, savm, prlim, ftolpl, &ncvyear, k, estepm, cptcov,cptcod,vpopbased,mobilav, strstart, nres); /* cptcod not initialized Intel */
14084: 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 ");
14085: if(vpopbased==1)
14086: 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);
14087: else
14088: fprintf(ficrest,"the age specific forward period (stable) prevalences in each health state \n");
14089: fprintf(ficrest,"# Age popbased mobilav e.. (std) "); /* Adding covariate values? */
14090: for (i=1;i<=nlstate;i++) fprintf(ficrest,"e.%d (std) ",i);
14091: fprintf(ficrest,"\n");
14092: /* printf("Which p?\n"); for(i=1;i<=npar;i++)printf("p[i=%d]=%lf,",i,p[i]);printf("\n"); */
14093: printf("Computing age specific forward period (stable) prevalences in each health state \n");
14094: fprintf(ficlog,"Computing age specific forward period (stable) prevalences in each health state \n");
14095: for(age=bage; age <=fage ;age++){
14096: prevalim(prlim, nlstate, p, age, oldm, savm, ftolpl, &ncvyear, k, nres); /*ZZ Is it the correct prevalim */
14097: if (vpopbased==1) {
14098: if(mobilav ==0){
14099: for(i=1; i<=nlstate;i++)
14100: prlim[i][i]=probs[(int)age][i][k];
14101: }else{ /* mobilav */
14102: for(i=1; i<=nlstate;i++)
14103: prlim[i][i]=mobaverage[(int)age][i][k];
14104: }
14105: }
14106:
14107: fprintf(ficrest," %4.0f %d %d",age, vpopbased, mobilav);
14108: /* fprintf(ficrest," %4.0f %d %d %d %d",age, vpopbased, mobilav,Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]); */ /* to be done */
14109: /* printf(" age %4.0f ",age); */
14110: for(j=1, epj[nlstate+1]=0.;j <=nlstate;j++){
14111: for(i=1, epj[j]=0.;i <=nlstate;i++) {
14112: epj[j] += prlim[i][i]*eij[i][j][(int)age];
14113: /*ZZZ printf("%lf %lf ", prlim[i][i] ,eij[i][j][(int)age]);*/
14114: /* printf("%lf %lf ", prlim[i][i] ,eij[i][j][(int)age]); */
14115: }
14116: epj[nlstate+1] +=epj[j];
14117: }
14118: /* printf(" age %4.0f \n",age); */
14119:
14120: for(i=1, vepp=0.;i <=nlstate;i++)
14121: for(j=1;j <=nlstate;j++)
14122: vepp += vareij[i][j][(int)age];
14123: fprintf(ficrest," %7.3f (%7.3f)", epj[nlstate+1],sqrt(vepp));
14124: for(j=1;j <=nlstate;j++){
14125: fprintf(ficrest," %7.3f (%7.3f)", epj[j],sqrt(vareij[j][j][(int)age]));
14126: }
14127: fprintf(ficrest,"\n");
14128: }
14129: } /* End vpopbased */
14130: free_vector(epj,1,nlstate+1);
14131: free_ma3x(eij,1,nlstate,1,nlstate,(int) bage, (int)fage);
14132: free_ma3x(vareij,1,nlstate,1,nlstate,(int) bage, (int)fage);
14133: printf("done selection\n");fflush(stdout);
14134: fprintf(ficlog,"done selection\n");fflush(ficlog);
14135:
14136: } /* End k selection or end covariate selection for nres */
14137:
14138: printf("done State-specific expectancies\n");fflush(stdout);
14139: fprintf(ficlog,"done State-specific expectancies\n");fflush(ficlog);
14140:
14141: /* variance-covariance of forward period prevalence */
14142: varprlim(fileresu, nresult, mobaverage, mobilavproj, bage, fage, prlim, &ncvyear, ftolpl, p, matcov, delti, stepm, cptcoveff);
14143:
14144:
14145: free_vector(weight,firstobs,lastobs);
14146: free_imatrix(Tvardk,1,NCOVMAX,1,2);
14147: free_imatrix(Tvard,1,NCOVMAX,1,2);
14148: free_imatrix(s,1,maxwav+1,firstobs,lastobs);
14149: free_matrix(anint,1,maxwav,firstobs,lastobs);
14150: free_matrix(mint,1,maxwav,firstobs,lastobs);
14151: free_ivector(cod,firstobs,lastobs);
14152: free_ivector(tab,1,NCOVMAX);
14153: fclose(ficresstdeij);
14154: fclose(ficrescveij);
14155: fclose(ficresvij);
14156: fclose(ficrest);
14157: fclose(ficpar);
14158:
14159:
14160: /*---------- End : free ----------------*/
14161: if (mobilav!=0 ||mobilavproj !=0)
14162: free_ma3x(mobaverages,AGEINF, AGESUP,1,nlstate+ndeath, 1,ncovcombmax); /* We need to have a squared matrix with prevalence of the dead! */
14163: free_ma3x(probs,AGEINF,AGESUP,1,nlstate+ndeath, 1,ncovcombmax);
14164: free_matrix(prlim,1,nlstate,1,nlstate); /*here or after loop ? */
14165: free_matrix(pmmij,1,nlstate+ndeath,1,nlstate+ndeath);
14166: } /* mle==-3 arrives here for freeing */
14167: /* endfree:*/
14168: free_matrix(oldms, 1,nlstate+ndeath,1,nlstate+ndeath);
14169: free_matrix(newms, 1,nlstate+ndeath,1,nlstate+ndeath);
14170: free_matrix(savms, 1,nlstate+ndeath,1,nlstate+ndeath);
14171: if(ntv+nqtv>=1)free_ma3x(cotvar,1,maxwav,1,ntv+nqtv,firstobs,lastobs);
14172: if(nqtv>=1)free_ma3x(cotqvar,1,maxwav,1,nqtv,firstobs,lastobs);
14173: if(nqv>=1)free_matrix(coqvar,1,nqv,firstobs,lastobs);
14174: free_matrix(covar,0,NCOVMAX,firstobs,lastobs);
14175: free_matrix(matcov,1,npar,1,npar);
14176: free_matrix(hess,1,npar,1,npar);
14177: /*free_vector(delti,1,npar);*/
14178: free_ma3x(delti3,1,nlstate,1, nlstate+ndeath-1,1,ncovmodel);
14179: free_matrix(agev,1,maxwav,1,imx);
14180: free_ma3x(paramstart,1,nlstate,1, nlstate+ndeath-1,1,ncovmodel);
14181: free_ma3x(param,1,nlstate,1, nlstate+ndeath-1,1,ncovmodel);
14182:
14183: free_ivector(ncodemax,1,NCOVMAX);
14184: free_ivector(ncodemaxwundef,1,NCOVMAX);
14185: free_ivector(Dummy,-1,NCOVMAX);
14186: free_ivector(Fixed,-1,NCOVMAX);
14187: free_ivector(DummyV,1,NCOVMAX);
14188: free_ivector(FixedV,1,NCOVMAX);
14189: free_ivector(Typevar,-1,NCOVMAX);
14190: free_ivector(Tvar,1,NCOVMAX);
14191: free_ivector(TvarsQ,1,NCOVMAX);
14192: free_ivector(TvarsQind,1,NCOVMAX);
14193: free_ivector(TvarsD,1,NCOVMAX);
14194: free_ivector(TnsdVar,1,NCOVMAX);
14195: free_ivector(TvarsDind,1,NCOVMAX);
14196: free_ivector(TvarFD,1,NCOVMAX);
14197: free_ivector(TvarFDind,1,NCOVMAX);
14198: free_ivector(TvarF,1,NCOVMAX);
14199: free_ivector(TvarFind,1,NCOVMAX);
14200: free_ivector(TvarV,1,NCOVMAX);
14201: free_ivector(TvarVind,1,NCOVMAX);
14202: free_ivector(TvarA,1,NCOVMAX);
14203: free_ivector(TvarAind,1,NCOVMAX);
14204: free_ivector(TvarFQ,1,NCOVMAX);
14205: free_ivector(TvarFQind,1,NCOVMAX);
14206: free_ivector(TvarVD,1,NCOVMAX);
14207: free_ivector(TvarVDind,1,NCOVMAX);
14208: free_ivector(TvarVQ,1,NCOVMAX);
14209: free_ivector(TvarVQind,1,NCOVMAX);
14210: free_ivector(Tvarsel,1,NCOVMAX);
14211: free_vector(Tvalsel,1,NCOVMAX);
14212: free_ivector(Tposprod,1,NCOVMAX);
14213: free_ivector(Tprod,1,NCOVMAX);
14214: free_ivector(Tvaraff,1,NCOVMAX);
14215: free_ivector(invalidvarcomb,0,ncovcombmax);
14216: free_ivector(Tage,1,NCOVMAX);
14217: free_ivector(Tmodelind,1,NCOVMAX);
14218: free_ivector(TmodelInvind,1,NCOVMAX);
14219: free_ivector(TmodelInvQind,1,NCOVMAX);
14220:
14221: free_matrix(precov, 1,MAXRESULTLINESPONE,1,NCOVMAX+1); /* Could be elsewhere ?*/
14222:
14223: free_imatrix(nbcode,0,NCOVMAX,0,NCOVMAX);
14224: /* free_imatrix(codtab,1,100,1,10); */
14225: fflush(fichtm);
14226: fflush(ficgp);
14227:
14228:
14229: if((nberr >0) || (nbwarn>0)){
14230: printf("End of Imach with %d errors and/or %d warnings. Please look at the log file for details.\n",nberr,nbwarn);
14231: fprintf(ficlog,"End of Imach with %d errors and/or warnings %d. Please look at the log file for details.\n",nberr,nbwarn);
14232: }else{
14233: printf("End of Imach\n");
14234: fprintf(ficlog,"End of Imach\n");
14235: }
14236: printf("See log file on %s\n",filelog);
14237: /* gettimeofday(&end_time, (struct timezone*)0);*/ /* after time */
14238: /*(void) gettimeofday(&end_time,&tzp);*/
14239: rend_time = time(NULL);
14240: end_time = *localtime(&rend_time);
14241: /* tml = *localtime(&end_time.tm_sec); */
14242: strcpy(strtend,asctime(&end_time));
14243: printf("Local time at start %s\nLocal time at end %s",strstart, strtend);
14244: fprintf(ficlog,"Local time at start %s\nLocal time at end %s\n",strstart, strtend);
14245: printf("Total time used %s\n", asc_diff_time(rend_time -rstart_time,tmpout));
14246:
14247: printf("Total time was %.0lf Sec.\n", difftime(rend_time,rstart_time));
14248: fprintf(ficlog,"Total time used %s\n", asc_diff_time(rend_time -rstart_time,tmpout));
14249: fprintf(ficlog,"Total time was %.0lf Sec.\n", difftime(rend_time,rstart_time));
14250: /* printf("Total time was %d uSec.\n", total_usecs);*/
14251: /* if(fileappend(fichtm,optionfilehtm)){ */
14252: fprintf(fichtm,"<br>Local time at start %s<br>Local time at end %s<br>\n</body></html>",strstart, strtend);
14253: fclose(fichtm);
14254: fprintf(fichtmcov,"<br>Local time at start %s<br>Local time at end %s<br>\n</body></html>",strstart, strtend);
14255: fclose(fichtmcov);
14256: fclose(ficgp);
14257: fclose(ficlog);
14258: /*------ End -----------*/
14259:
14260:
14261: /* Executes gnuplot */
14262:
14263: printf("Before Current directory %s!\n",pathcd);
14264: #ifdef WIN32
14265: if (_chdir(pathcd) != 0)
14266: printf("Can't move to directory %s!\n",path);
14267: if(_getcwd(pathcd,MAXLINE) > 0)
14268: #else
14269: if(chdir(pathcd) != 0)
14270: printf("Can't move to directory %s!\n", path);
14271: if (getcwd(pathcd, MAXLINE) > 0)
14272: #endif
14273: printf("Current directory %s!\n",pathcd);
14274: /*strcat(plotcmd,CHARSEPARATOR);*/
14275: sprintf(plotcmd,"gnuplot");
14276: #ifdef _WIN32
14277: sprintf(plotcmd,"\"%sgnuplot.exe\"",pathimach);
14278: #endif
14279: if(!stat(plotcmd,&info)){
14280: printf("Error or gnuplot program not found: '%s'\n",plotcmd);fflush(stdout);
14281: if(!stat(getenv("GNUPLOTBIN"),&info)){
14282: printf("Error or gnuplot program not found: '%s' Environment GNUPLOTBIN not set.\n",plotcmd);fflush(stdout);
14283: }else
14284: strcpy(pplotcmd,plotcmd);
14285: #ifdef __unix
14286: strcpy(plotcmd,GNUPLOTPROGRAM);
14287: if(!stat(plotcmd,&info)){
14288: printf("Error gnuplot program not found: '%s'\n",plotcmd);fflush(stdout);
14289: }else
14290: strcpy(pplotcmd,plotcmd);
14291: #endif
14292: }else
14293: strcpy(pplotcmd,plotcmd);
14294:
14295: sprintf(plotcmd,"%s %s",pplotcmd, optionfilegnuplot);
14296: printf("Starting graphs with: '%s'\n",plotcmd);fflush(stdout);
14297: strcpy(pplotcmd,plotcmd);
14298:
14299: if((outcmd=system(plotcmd)) != 0){
14300: printf("Error in gnuplot, command might not be in your path: '%s', err=%d\n", plotcmd, outcmd);
14301: printf("\n Trying if gnuplot resides on the same directory that IMaCh\n");
14302: sprintf(plotcmd,"%sgnuplot %s", pathimach, optionfilegnuplot);
14303: if((outcmd=system(plotcmd)) != 0){
14304: printf("\n Still a problem with gnuplot command %s, err=%d\n", plotcmd, outcmd);
14305: strcpy(plotcmd,pplotcmd);
14306: }
14307: }
14308: printf(" Successful, please wait...");
14309: while (z[0] != 'q') {
14310: /* chdir(path); */
14311: printf("\nType e to edit results with your browser, g to graph again and q for exit: ");
14312: scanf("%s",z);
14313: /* if (z[0] == 'c') system("./imach"); */
14314: if (z[0] == 'e') {
14315: #ifdef __APPLE__
14316: sprintf(pplotcmd, "open %s", optionfilehtm);
14317: #elif __linux
14318: sprintf(pplotcmd, "xdg-open %s", optionfilehtm);
14319: #else
14320: sprintf(pplotcmd, "%s", optionfilehtm);
14321: #endif
14322: printf("Starting browser with: %s",pplotcmd);fflush(stdout);
14323: system(pplotcmd);
14324: }
14325: else if (z[0] == 'g') system(plotcmd);
14326: else if (z[0] == 'q') exit(0);
14327: }
14328: end:
14329: while (z[0] != 'q') {
14330: printf("\nType q for exiting: "); fflush(stdout);
14331: scanf("%s",z);
14332: }
14333: printf("End\n");
14334: exit(0);
14335: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>