File:
[Local Repository] /
imach /
src /
imach.c
Revision
1.361:
download - view:
text,
annotated -
select for diffs
Sun May 12 20:29:32 2024 UTC (3 months, 1 week ago) by
brouard
Branches:
MAIN
CVS tags:
HEAD
Summary: Version 0.99s5
* src/imach.c Version 0.99s5 In fact, the covariance of total life
expectancy e.. with a partial life expectancy e.j is high,
therefore the complete matrix of variance covariance has to be
included in the formula of the standard error of the proportion of
total life expectancy spent in a specific state:
var(X/Y)=mu_x^2/mu_y^2*(sigma_x^2/mu_x^2 -2
sigma_xy/mu_x/mu_y+sigma^2/mu_y^2). Also an error with mle=-3
made the program core dump. It is fixed in this version.
1: /* $Id: imach.c,v 1.361 2024/05/12 20:29:32 brouard Exp $
2: $State: Exp $
3: $Log: imach.c,v $
4: Revision 1.361 2024/05/12 20:29:32 brouard
5: Summary: Version 0.99s5
6:
7: * src/imach.c Version 0.99s5 In fact, the covariance of total life
8: expectancy e.. with a partial life expectancy e.j is high,
9: therefore the complete matrix of variance covariance has to be
10: included in the formula of the standard error of the proportion of
11: total life expectancy spent in a specific state:
12: var(X/Y)=mu_x^2/mu_y^2*(sigma_x^2/mu_x^2 -2
13: sigma_xy/mu_x/mu_y+sigma^2/mu_y^2). Also an error with mle=-3
14: made the program core dump. It is fixed in this version.
15:
16: Revision 1.360 2024/04/30 10:59:22 brouard
17: Summary: Version 0.99s4 and estimation of std of e.j/e..
18:
19: Revision 1.359 2024/04/24 21:21:17 brouard
20: Summary: First IMaCh version using Brent Praxis software based on Buckhardt and Gegenfürtner C codes
21:
22: Revision 1.6 2024/04/24 21:10:29 brouard
23: Summary: First IMaCh version using Brent Praxis software based on Buckhardt and Gegenfürtner C codes
24:
25: Revision 1.5 2023/10/09 09:10:01 brouard
26: Summary: trying to reconsider
27:
28: Revision 1.4 2023/06/22 12:50:51 brouard
29: Summary: stil on going
30:
31: Revision 1.3 2023/06/22 11:28:07 brouard
32: *** empty log message ***
33:
34: Revision 1.2 2023/06/22 11:22:40 brouard
35: Summary: with svd but not working yet
36:
37: Revision 1.353 2023/05/08 18:48:22 brouard
38: *** empty log message ***
39:
40: Revision 1.352 2023/04/29 10:46:21 brouard
41: *** empty log message ***
42:
43: Revision 1.351 2023/04/29 10:43:47 brouard
44: Summary: 099r45
45:
46: Revision 1.350 2023/04/24 11:38:06 brouard
47: *** empty log message ***
48:
49: Revision 1.349 2023/01/31 09:19:37 brouard
50: Summary: Improvements in models with age*Vn*Vm
51:
52: Revision 1.347 2022/09/18 14:36:44 brouard
53: Summary: version 0.99r42
54:
55: Revision 1.346 2022/09/16 13:52:36 brouard
56: * src/imach.c (Module): 0.99r41 Was an error when product of timevarying and fixed. Using FixedV[of name] now. Thank you Feinuo
57:
58: Revision 1.345 2022/09/16 13:40:11 brouard
59: Summary: Version 0.99r41
60:
61: * imach.c (Module): 0.99r41 Was an error when product of timevarying and fixed. Using FixedV[of name] now. Thank you Feinuo
62:
63: Revision 1.344 2022/09/14 19:33:30 brouard
64: Summary: version 0.99r40
65:
66: * imach.c (Module): Fixing names of variables in T_ (thanks to Feinuo)
67:
68: Revision 1.343 2022/09/14 14:22:16 brouard
69: Summary: version 0.99r39
70:
71: * imach.c (Module): Version 0.99r39 with colored dummy covariates
72: (fixed or time varying), using new last columns of
73: ILK_parameter.txt file.
74:
75: Revision 1.342 2022/09/11 19:54:09 brouard
76: Summary: 0.99r38
77:
78: * imach.c (Module): Adding timevarying products of any kinds,
79: should work before shifting cotvar from ncovcol+nqv columns in
80: order to have a correspondance between the column of cotvar and
81: the id of column.
82: (Module): Some cleaning and adding covariates in ILK.txt
83:
84: Revision 1.341 2022/09/11 07:58:42 brouard
85: Summary: Version 0.99r38
86:
87: After adding change in cotvar.
88:
89: Revision 1.340 2022/09/11 07:53:11 brouard
90: Summary: Version imach 0.99r37
91:
92: * imach.c (Module): Adding timevarying products of any kinds,
93: should work before shifting cotvar from ncovcol+nqv columns in
94: order to have a correspondance between the column of cotvar and
95: the id of column.
96:
97: Revision 1.339 2022/09/09 17:55:22 brouard
98: Summary: version 0.99r37
99:
100: * imach.c (Module): Many improvements for fixing products of fixed
101: timevarying as well as fixed * fixed, and test with quantitative
102: covariate.
103:
104: Revision 1.338 2022/09/04 17:40:33 brouard
105: Summary: 0.99r36
106:
107: * imach.c (Module): Now the easy runs i.e. without result or
108: model=1+age only did not work. The defautl combination should be 1
109: and not 0 because everything hasn't been tranformed yet.
110:
111: Revision 1.337 2022/09/02 14:26:02 brouard
112: Summary: version 0.99r35
113:
114: * src/imach.c: Version 0.99r35 because it outputs same results with
115: 1+age+V1+V1*age for females and 1+age for females only
116: (education=1 noweight)
117:
118: Revision 1.336 2022/08/31 09:52:36 brouard
119: *** empty log message ***
120:
121: Revision 1.335 2022/08/31 08:23:16 brouard
122: Summary: improvements...
123:
124: Revision 1.334 2022/08/25 09:08:41 brouard
125: Summary: In progress for quantitative
126:
127: Revision 1.333 2022/08/21 09:10:30 brouard
128: * src/imach.c (Module): Version 0.99r33 A lot of changes in
129: reassigning covariates: my first idea was that people will always
130: use the first covariate V1 into the model but in fact they are
131: producing data with many covariates and can use an equation model
132: with some of the covariate; it means that in a model V2+V3 instead
133: of codtabm(k,Tvaraff[j]) which calculates for combination k, for
134: three covariates (V1, V2, V3) the value of Tvaraff[j], but in fact
135: the equation model is restricted to two variables only (V2, V3)
136: and the combination for V2 should be codtabm(k,1) instead of
137: (codtabm(k,2), and the code should be
138: codtabm(k,TnsdVar[Tvaraff[j]]. Many many changes have been
139: made. All of these should be simplified once a day like we did in
140: hpxij() for example by using precov[nres] which is computed in
141: decoderesult for each nres of each resultline. Loop should be done
142: on the equation model globally by distinguishing only product with
143: age (which are changing with age) and no more on type of
144: covariates, single dummies, single covariates.
145:
146: Revision 1.332 2022/08/21 09:06:25 brouard
147: Summary: Version 0.99r33
148:
149: * src/imach.c (Module): Version 0.99r33 A lot of changes in
150: reassigning covariates: my first idea was that people will always
151: use the first covariate V1 into the model but in fact they are
152: producing data with many covariates and can use an equation model
153: with some of the covariate; it means that in a model V2+V3 instead
154: of codtabm(k,Tvaraff[j]) which calculates for combination k, for
155: three covariates (V1, V2, V3) the value of Tvaraff[j], but in fact
156: the equation model is restricted to two variables only (V2, V3)
157: and the combination for V2 should be codtabm(k,1) instead of
158: (codtabm(k,2), and the code should be
159: codtabm(k,TnsdVar[Tvaraff[j]]. Many many changes have been
160: made. All of these should be simplified once a day like we did in
161: hpxij() for example by using precov[nres] which is computed in
162: decoderesult for each nres of each resultline. Loop should be done
163: on the equation model globally by distinguishing only product with
164: age (which are changing with age) and no more on type of
165: covariates, single dummies, single covariates.
166:
167: Revision 1.331 2022/08/07 05:40:09 brouard
168: *** empty log message ***
169:
170: Revision 1.330 2022/08/06 07:18:25 brouard
171: Summary: last 0.99r31
172:
173: * imach.c (Module): Version of imach using partly decoderesult to rebuild xpxij function
174:
175: Revision 1.329 2022/08/03 17:29:54 brouard
176: * imach.c (Module): Many errors in graphs fixed with Vn*age covariates.
177:
178: Revision 1.328 2022/07/27 17:40:48 brouard
179: Summary: valgrind bug fixed by initializing to zero DummyV as well as Tage
180:
181: Revision 1.327 2022/07/27 14:47:35 brouard
182: Summary: Still a problem for one-step probabilities in case of quantitative variables
183:
184: Revision 1.326 2022/07/26 17:33:55 brouard
185: Summary: some test with nres=1
186:
187: Revision 1.325 2022/07/25 14:27:23 brouard
188: Summary: r30
189:
190: * imach.c (Module): Error cptcovn instead of nsd in bmij (was
191: coredumped, revealed by Feiuno, thank you.
192:
193: Revision 1.324 2022/07/23 17:44:26 brouard
194: *** empty log message ***
195:
196: Revision 1.323 2022/07/22 12:30:08 brouard
197: * imach.c (Module): Output of Wald test in the htm file and not only in the log.
198:
199: Revision 1.322 2022/07/22 12:27:48 brouard
200: * imach.c (Module): Output of Wald test in the htm file and not only in the log.
201:
202: Revision 1.321 2022/07/22 12:04:24 brouard
203: Summary: r28
204:
205: * imach.c (Module): Output of Wald test in the htm file and not only in the log.
206:
207: Revision 1.320 2022/06/02 05:10:11 brouard
208: *** empty log message ***
209:
210: Revision 1.319 2022/06/02 04:45:11 brouard
211: * imach.c (Module): Adding the Wald tests from the log to the main
212: htm for better display of the maximum likelihood estimators.
213:
214: Revision 1.318 2022/05/24 08:10:59 brouard
215: * imach.c (Module): Some attempts to find a bug of wrong estimates
216: of confidencce intervals with product in the equation modelC
217:
218: Revision 1.317 2022/05/15 15:06:23 brouard
219: * imach.c (Module): Some minor improvements
220:
221: Revision 1.316 2022/05/11 15:11:31 brouard
222: Summary: r27
223:
224: Revision 1.315 2022/05/11 15:06:32 brouard
225: *** empty log message ***
226:
227: Revision 1.314 2022/04/13 17:43:09 brouard
228: * imach.c (Module): Adding link to text data files
229:
230: Revision 1.313 2022/04/11 15:57:42 brouard
231: * imach.c (Module): Error in rewriting the 'r' file with yearsfproj or yearsbproj fixed
232:
233: Revision 1.312 2022/04/05 21:24:39 brouard
234: *** empty log message ***
235:
236: Revision 1.311 2022/04/05 21:03:51 brouard
237: Summary: Fixed quantitative covariates
238:
239: Fixed covariates (dummy or quantitative)
240: with missing values have never been allowed but are ERRORS and
241: program quits. Standard deviations of fixed covariates were
242: wrongly computed. Mean and standard deviations of time varying
243: covariates are still not computed.
244:
245: Revision 1.310 2022/03/17 08:45:53 brouard
246: Summary: 99r25
247:
248: Improving detection of errors: result lines should be compatible with
249: the model.
250:
251: Revision 1.309 2021/05/20 12:39:14 brouard
252: Summary: Version 0.99r24
253:
254: Revision 1.308 2021/03/31 13:11:57 brouard
255: Summary: Version 0.99r23
256:
257:
258: * imach.c (Module): Still bugs in the result loop. Thank to Holly Benett
259:
260: Revision 1.307 2021/03/08 18:11:32 brouard
261: Summary: 0.99r22 fixed bug on result:
262:
263: Revision 1.306 2021/02/20 15:44:02 brouard
264: Summary: Version 0.99r21
265:
266: * imach.c (Module): Fix bug on quitting after result lines!
267: (Module): Version 0.99r21
268:
269: Revision 1.305 2021/02/20 15:28:30 brouard
270: * imach.c (Module): Fix bug on quitting after result lines!
271:
272: Revision 1.304 2021/02/12 11:34:20 brouard
273: * imach.c (Module): The use of a Windows BOM (huge) file is now an error
274:
275: Revision 1.303 2021/02/11 19:50:15 brouard
276: * (Module): imach.c Someone entered 'results:' instead of 'result:'. Now it is an error which is printed.
277:
278: Revision 1.302 2020/02/22 21:00:05 brouard
279: * (Module): imach.c Update mle=-3 (for computing Life expectancy
280: and life table from the data without any state)
281:
282: Revision 1.301 2019/06/04 13:51:20 brouard
283: Summary: Error in 'r'parameter file backcast yearsbproj instead of yearsfproj
284:
285: Revision 1.300 2019/05/22 19:09:45 brouard
286: Summary: version 0.99r19 of May 2019
287:
288: Revision 1.299 2019/05/22 18:37:08 brouard
289: Summary: Cleaned 0.99r19
290:
291: Revision 1.298 2019/05/22 18:19:56 brouard
292: *** empty log message ***
293:
294: Revision 1.297 2019/05/22 17:56:10 brouard
295: Summary: Fix bug by moving date2dmy and nhstepm which gaefin=-1
296:
297: Revision 1.296 2019/05/20 13:03:18 brouard
298: Summary: Projection syntax simplified
299:
300:
301: We can now start projections, forward or backward, from the mean date
302: of inteviews up to or down to a number of years of projection:
303: prevforecast=1 yearsfproj=15.3 mobil_average=0
304: or
305: prevforecast=1 starting-proj-date=1/1/2007 final-proj-date=12/31/2017 mobil_average=0
306: or
307: prevbackcast=1 yearsbproj=12.3 mobil_average=1
308: or
309: prevbackcast=1 starting-back-date=1/10/1999 final-back-date=1/1/1985 mobil_average=1
310:
311: Revision 1.295 2019/05/18 09:52:50 brouard
312: Summary: doxygen tex bug
313:
314: Revision 1.294 2019/05/16 14:54:33 brouard
315: Summary: There was some wrong lines added
316:
317: Revision 1.293 2019/05/09 15:17:34 brouard
318: *** empty log message ***
319:
320: Revision 1.292 2019/05/09 14:17:20 brouard
321: Summary: Some updates
322:
323: Revision 1.291 2019/05/09 13:44:18 brouard
324: Summary: Before ncovmax
325:
326: Revision 1.290 2019/05/09 13:39:37 brouard
327: Summary: 0.99r18 unlimited number of individuals
328:
329: 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.
330:
331: Revision 1.289 2018/12/13 09:16:26 brouard
332: Summary: Bug for young ages (<-30) will be in r17
333:
334: Revision 1.288 2018/05/02 20:58:27 brouard
335: Summary: Some bugs fixed
336:
337: Revision 1.287 2018/05/01 17:57:25 brouard
338: Summary: Bug fixed by providing frequencies only for non missing covariates
339:
340: Revision 1.286 2018/04/27 14:27:04 brouard
341: Summary: some minor bugs
342:
343: Revision 1.285 2018/04/21 21:02:16 brouard
344: Summary: Some bugs fixed, valgrind tested
345:
346: Revision 1.284 2018/04/20 05:22:13 brouard
347: Summary: Computing mean and stdeviation of fixed quantitative variables
348:
349: Revision 1.283 2018/04/19 14:49:16 brouard
350: Summary: Some minor bugs fixed
351:
352: Revision 1.282 2018/02/27 22:50:02 brouard
353: *** empty log message ***
354:
355: Revision 1.281 2018/02/27 19:25:23 brouard
356: Summary: Adding second argument for quitting
357:
358: Revision 1.280 2018/02/21 07:58:13 brouard
359: Summary: 0.99r15
360:
361: New Makefile with recent VirtualBox 5.26. Bug in sqrt negatve in imach.c
362:
363: Revision 1.279 2017/07/20 13:35:01 brouard
364: Summary: temporary working
365:
366: Revision 1.278 2017/07/19 14:09:02 brouard
367: Summary: Bug for mobil_average=0 and prevforecast fixed(?)
368:
369: Revision 1.277 2017/07/17 08:53:49 brouard
370: Summary: BOM files can be read now
371:
372: Revision 1.276 2017/06/30 15:48:31 brouard
373: Summary: Graphs improvements
374:
375: Revision 1.275 2017/06/30 13:39:33 brouard
376: Summary: Saito's color
377:
378: Revision 1.274 2017/06/29 09:47:08 brouard
379: Summary: Version 0.99r14
380:
381: Revision 1.273 2017/06/27 11:06:02 brouard
382: Summary: More documentation on projections
383:
384: Revision 1.272 2017/06/27 10:22:40 brouard
385: Summary: Color of backprojection changed from 6 to 5(yellow)
386:
387: Revision 1.271 2017/06/27 10:17:50 brouard
388: Summary: Some bug with rint
389:
390: Revision 1.270 2017/05/24 05:45:29 brouard
391: *** empty log message ***
392:
393: Revision 1.269 2017/05/23 08:39:25 brouard
394: Summary: Code into subroutine, cleanings
395:
396: Revision 1.268 2017/05/18 20:09:32 brouard
397: Summary: backprojection and confidence intervals of backprevalence
398:
399: Revision 1.267 2017/05/13 10:25:05 brouard
400: Summary: temporary save for backprojection
401:
402: Revision 1.266 2017/05/13 07:26:12 brouard
403: Summary: Version 0.99r13 (improvements and bugs fixed)
404:
405: Revision 1.265 2017/04/26 16:22:11 brouard
406: Summary: imach 0.99r13 Some bugs fixed
407:
408: Revision 1.264 2017/04/26 06:01:29 brouard
409: Summary: Labels in graphs
410:
411: Revision 1.263 2017/04/24 15:23:15 brouard
412: Summary: to save
413:
414: Revision 1.262 2017/04/18 16:48:12 brouard
415: *** empty log message ***
416:
417: Revision 1.261 2017/04/05 10:14:09 brouard
418: Summary: Bug in E_ as well as in T_ fixed nres-1 vs k1-1
419:
420: Revision 1.260 2017/04/04 17:46:59 brouard
421: Summary: Gnuplot indexations fixed (humm)
422:
423: Revision 1.259 2017/04/04 13:01:16 brouard
424: Summary: Some errors to warnings only if date of death is unknown but status is death we could set to pi3
425:
426: Revision 1.258 2017/04/03 10:17:47 brouard
427: Summary: Version 0.99r12
428:
429: Some cleanings, conformed with updated documentation.
430:
431: Revision 1.257 2017/03/29 16:53:30 brouard
432: Summary: Temp
433:
434: Revision 1.256 2017/03/27 05:50:23 brouard
435: Summary: Temporary
436:
437: Revision 1.255 2017/03/08 16:02:28 brouard
438: Summary: IMaCh version 0.99r10 bugs in gnuplot fixed
439:
440: Revision 1.254 2017/03/08 07:13:00 brouard
441: Summary: Fixing data parameter line
442:
443: Revision 1.253 2016/12/15 11:59:41 brouard
444: Summary: 0.99 in progress
445:
446: Revision 1.252 2016/09/15 21:15:37 brouard
447: *** empty log message ***
448:
449: Revision 1.251 2016/09/15 15:01:13 brouard
450: Summary: not working
451:
452: Revision 1.250 2016/09/08 16:07:27 brouard
453: Summary: continue
454:
455: Revision 1.249 2016/09/07 17:14:18 brouard
456: Summary: Starting values from frequencies
457:
458: Revision 1.248 2016/09/07 14:10:18 brouard
459: *** empty log message ***
460:
461: Revision 1.247 2016/09/02 11:11:21 brouard
462: *** empty log message ***
463:
464: Revision 1.246 2016/09/02 08:49:22 brouard
465: *** empty log message ***
466:
467: Revision 1.245 2016/09/02 07:25:01 brouard
468: *** empty log message ***
469:
470: Revision 1.244 2016/09/02 07:17:34 brouard
471: *** empty log message ***
472:
473: Revision 1.243 2016/09/02 06:45:35 brouard
474: *** empty log message ***
475:
476: Revision 1.242 2016/08/30 15:01:20 brouard
477: Summary: Fixing a lots
478:
479: Revision 1.241 2016/08/29 17:17:25 brouard
480: Summary: gnuplot problem in Back projection to fix
481:
482: Revision 1.240 2016/08/29 07:53:18 brouard
483: Summary: Better
484:
485: Revision 1.239 2016/08/26 15:51:03 brouard
486: Summary: Improvement in Powell output in order to copy and paste
487:
488: Author:
489:
490: Revision 1.238 2016/08/26 14:23:35 brouard
491: Summary: Starting tests of 0.99
492:
493: Revision 1.237 2016/08/26 09:20:19 brouard
494: Summary: to valgrind
495:
496: Revision 1.236 2016/08/25 10:50:18 brouard
497: *** empty log message ***
498:
499: Revision 1.235 2016/08/25 06:59:23 brouard
500: *** empty log message ***
501:
502: Revision 1.234 2016/08/23 16:51:20 brouard
503: *** empty log message ***
504:
505: Revision 1.233 2016/08/23 07:40:50 brouard
506: Summary: not working
507:
508: Revision 1.232 2016/08/22 14:20:21 brouard
509: Summary: not working
510:
511: Revision 1.231 2016/08/22 07:17:15 brouard
512: Summary: not working
513:
514: Revision 1.230 2016/08/22 06:55:53 brouard
515: Summary: Not working
516:
517: Revision 1.229 2016/07/23 09:45:53 brouard
518: Summary: Completing for func too
519:
520: Revision 1.228 2016/07/22 17:45:30 brouard
521: Summary: Fixing some arrays, still debugging
522:
523: Revision 1.226 2016/07/12 18:42:34 brouard
524: Summary: temp
525:
526: Revision 1.225 2016/07/12 08:40:03 brouard
527: Summary: saving but not running
528:
529: Revision 1.224 2016/07/01 13:16:01 brouard
530: Summary: Fixes
531:
532: Revision 1.223 2016/02/19 09:23:35 brouard
533: Summary: temporary
534:
535: Revision 1.222 2016/02/17 08:14:50 brouard
536: Summary: Probably last 0.98 stable version 0.98r6
537:
538: Revision 1.221 2016/02/15 23:35:36 brouard
539: Summary: minor bug
540:
541: Revision 1.219 2016/02/15 00:48:12 brouard
542: *** empty log message ***
543:
544: Revision 1.218 2016/02/12 11:29:23 brouard
545: Summary: 0.99 Back projections
546:
547: Revision 1.217 2015/12/23 17:18:31 brouard
548: Summary: Experimental backcast
549:
550: Revision 1.216 2015/12/18 17:32:11 brouard
551: Summary: 0.98r4 Warning and status=-2
552:
553: Version 0.98r4 is now:
554: - displaying an error when status is -1, date of interview unknown and date of death known;
555: - permitting a status -2 when the vital status is unknown at a known date of right truncation.
556: Older changes concerning s=-2, dating from 2005 have been supersed.
557:
558: Revision 1.215 2015/12/16 08:52:24 brouard
559: Summary: 0.98r4 working
560:
561: Revision 1.214 2015/12/16 06:57:54 brouard
562: Summary: temporary not working
563:
564: Revision 1.213 2015/12/11 18:22:17 brouard
565: Summary: 0.98r4
566:
567: Revision 1.212 2015/11/21 12:47:24 brouard
568: Summary: minor typo
569:
570: Revision 1.211 2015/11/21 12:41:11 brouard
571: Summary: 0.98r3 with some graph of projected cross-sectional
572:
573: Author: Nicolas Brouard
574:
575: Revision 1.210 2015/11/18 17:41:20 brouard
576: Summary: Start working on projected prevalences Revision 1.209 2015/11/17 22:12:03 brouard
577: Summary: Adding ftolpl parameter
578: Author: N Brouard
579:
580: We had difficulties to get smoothed confidence intervals. It was due
581: to the period prevalence which wasn't computed accurately. The inner
582: parameter ftolpl is now an outer parameter of the .imach parameter
583: file after estepm. If ftolpl is small 1.e-4 and estepm too,
584: computation are long.
585:
586: Revision 1.208 2015/11/17 14:31:57 brouard
587: Summary: temporary
588:
589: Revision 1.207 2015/10/27 17:36:57 brouard
590: *** empty log message ***
591:
592: Revision 1.206 2015/10/24 07:14:11 brouard
593: *** empty log message ***
594:
595: Revision 1.205 2015/10/23 15:50:53 brouard
596: Summary: 0.98r3 some clarification for graphs on likelihood contributions
597:
598: Revision 1.204 2015/10/01 16:20:26 brouard
599: Summary: Some new graphs of contribution to likelihood
600:
601: Revision 1.203 2015/09/30 17:45:14 brouard
602: Summary: looking at better estimation of the hessian
603:
604: Also a better criteria for convergence to the period prevalence And
605: therefore adding the number of years needed to converge. (The
606: prevalence in any alive state shold sum to one
607:
608: Revision 1.202 2015/09/22 19:45:16 brouard
609: Summary: Adding some overall graph on contribution to likelihood. Might change
610:
611: Revision 1.201 2015/09/15 17:34:58 brouard
612: Summary: 0.98r0
613:
614: - Some new graphs like suvival functions
615: - Some bugs fixed like model=1+age+V2.
616:
617: Revision 1.200 2015/09/09 16:53:55 brouard
618: Summary: Big bug thanks to Flavia
619:
620: Even model=1+age+V2. did not work anymore
621:
622: Revision 1.199 2015/09/07 14:09:23 brouard
623: Summary: 0.98q6 changing default small png format for graph to vectorized svg.
624:
625: Revision 1.198 2015/09/03 07:14:39 brouard
626: Summary: 0.98q5 Flavia
627:
628: Revision 1.197 2015/09/01 18:24:39 brouard
629: *** empty log message ***
630:
631: Revision 1.196 2015/08/18 23:17:52 brouard
632: Summary: 0.98q5
633:
634: Revision 1.195 2015/08/18 16:28:39 brouard
635: Summary: Adding a hack for testing purpose
636:
637: After reading the title, ftol and model lines, if the comment line has
638: a q, starting with #q, the answer at the end of the run is quit. It
639: permits to run test files in batch with ctest. The former workaround was
640: $ echo q | imach foo.imach
641:
642: Revision 1.194 2015/08/18 13:32:00 brouard
643: Summary: Adding error when the covariance matrix doesn't contain the exact number of lines required by the model line.
644:
645: Revision 1.193 2015/08/04 07:17:42 brouard
646: Summary: 0.98q4
647:
648: Revision 1.192 2015/07/16 16:49:02 brouard
649: Summary: Fixing some outputs
650:
651: Revision 1.191 2015/07/14 10:00:33 brouard
652: Summary: Some fixes
653:
654: Revision 1.190 2015/05/05 08:51:13 brouard
655: Summary: Adding digits in output parameters (7 digits instead of 6)
656:
657: Fix 1+age+.
658:
659: Revision 1.189 2015/04/30 14:45:16 brouard
660: Summary: 0.98q2
661:
662: Revision 1.188 2015/04/30 08:27:53 brouard
663: *** empty log message ***
664:
665: Revision 1.187 2015/04/29 09:11:15 brouard
666: *** empty log message ***
667:
668: Revision 1.186 2015/04/23 12:01:52 brouard
669: Summary: V1*age is working now, version 0.98q1
670:
671: Some codes had been disabled in order to simplify and Vn*age was
672: working in the optimization phase, ie, giving correct MLE parameters,
673: but, as usual, outputs were not correct and program core dumped.
674:
675: Revision 1.185 2015/03/11 13:26:42 brouard
676: Summary: Inclusion of compile and links command line for Intel Compiler
677:
678: Revision 1.184 2015/03/11 11:52:39 brouard
679: Summary: Back from Windows 8. Intel Compiler
680:
681: Revision 1.183 2015/03/10 20:34:32 brouard
682: Summary: 0.98q0, trying with directest, mnbrak fixed
683:
684: We use directest instead of original Powell test; probably no
685: incidence on the results, but better justifications;
686: We fixed Numerical Recipes mnbrak routine which was wrong and gave
687: wrong results.
688:
689: Revision 1.182 2015/02/12 08:19:57 brouard
690: Summary: Trying to keep directest which seems simpler and more general
691: Author: Nicolas Brouard
692:
693: Revision 1.181 2015/02/11 23:22:24 brouard
694: Summary: Comments on Powell added
695:
696: Author:
697:
698: Revision 1.180 2015/02/11 17:33:45 brouard
699: Summary: Finishing move from main to function (hpijx and prevalence_limit)
700:
701: Revision 1.179 2015/01/04 09:57:06 brouard
702: Summary: back to OS/X
703:
704: Revision 1.178 2015/01/04 09:35:48 brouard
705: *** empty log message ***
706:
707: Revision 1.177 2015/01/03 18:40:56 brouard
708: Summary: Still testing ilc32 on OSX
709:
710: Revision 1.176 2015/01/03 16:45:04 brouard
711: *** empty log message ***
712:
713: Revision 1.175 2015/01/03 16:33:42 brouard
714: *** empty log message ***
715:
716: Revision 1.174 2015/01/03 16:15:49 brouard
717: Summary: Still in cross-compilation
718:
719: Revision 1.173 2015/01/03 12:06:26 brouard
720: Summary: trying to detect cross-compilation
721:
722: Revision 1.172 2014/12/27 12:07:47 brouard
723: Summary: Back from Visual Studio and Intel, options for compiling for Windows XP
724:
725: Revision 1.171 2014/12/23 13:26:59 brouard
726: Summary: Back from Visual C
727:
728: Still problem with utsname.h on Windows
729:
730: Revision 1.170 2014/12/23 11:17:12 brouard
731: Summary: Cleaning some \%% back to %%
732:
733: The escape was mandatory for a specific compiler (which one?), but too many warnings.
734:
735: Revision 1.169 2014/12/22 23:08:31 brouard
736: Summary: 0.98p
737:
738: Outputs some informations on compiler used, OS etc. Testing on different platforms.
739:
740: Revision 1.168 2014/12/22 15:17:42 brouard
741: Summary: update
742:
743: Revision 1.167 2014/12/22 13:50:56 brouard
744: Summary: Testing uname and compiler version and if compiled 32 or 64
745:
746: Testing on Linux 64
747:
748: Revision 1.166 2014/12/22 11:40:47 brouard
749: *** empty log message ***
750:
751: Revision 1.165 2014/12/16 11:20:36 brouard
752: Summary: After compiling on Visual C
753:
754: * imach.c (Module): Merging 1.61 to 1.162
755:
756: Revision 1.164 2014/12/16 10:52:11 brouard
757: Summary: Merging with Visual C after suppressing some warnings for unused variables. Also fixing Saito's bug 0.98Xn
758:
759: * imach.c (Module): Merging 1.61 to 1.162
760:
761: Revision 1.163 2014/12/16 10:30:11 brouard
762: * imach.c (Module): Merging 1.61 to 1.162
763:
764: Revision 1.162 2014/09/25 11:43:39 brouard
765: Summary: temporary backup 0.99!
766:
767: Revision 1.1 2014/09/16 11:06:58 brouard
768: Summary: With some code (wrong) for nlopt
769:
770: Author:
771:
772: Revision 1.161 2014/09/15 20:41:41 brouard
773: Summary: Problem with macro SQR on Intel compiler
774:
775: Revision 1.160 2014/09/02 09:24:05 brouard
776: *** empty log message ***
777:
778: Revision 1.159 2014/09/01 10:34:10 brouard
779: Summary: WIN32
780: Author: Brouard
781:
782: Revision 1.158 2014/08/27 17:11:51 brouard
783: *** empty log message ***
784:
785: Revision 1.157 2014/08/27 16:26:55 brouard
786: Summary: Preparing windows Visual studio version
787: Author: Brouard
788:
789: In order to compile on Visual studio, time.h is now correct and time_t
790: and tm struct should be used. difftime should be used but sometimes I
791: just make the differences in raw time format (time(&now).
792: Trying to suppress #ifdef LINUX
793: Add xdg-open for __linux in order to open default browser.
794:
795: Revision 1.156 2014/08/25 20:10:10 brouard
796: *** empty log message ***
797:
798: Revision 1.155 2014/08/25 18:32:34 brouard
799: Summary: New compile, minor changes
800: Author: Brouard
801:
802: Revision 1.154 2014/06/20 17:32:08 brouard
803: Summary: Outputs now all graphs of convergence to period prevalence
804:
805: Revision 1.153 2014/06/20 16:45:46 brouard
806: Summary: If 3 live state, convergence to period prevalence on same graph
807: Author: Brouard
808:
809: Revision 1.152 2014/06/18 17:54:09 brouard
810: Summary: open browser, use gnuplot on same dir than imach if not found in the path
811:
812: Revision 1.151 2014/06/18 16:43:30 brouard
813: *** empty log message ***
814:
815: Revision 1.150 2014/06/18 16:42:35 brouard
816: Summary: If gnuplot is not in the path try on same directory than imach binary (OSX)
817: Author: brouard
818:
819: Revision 1.149 2014/06/18 15:51:14 brouard
820: Summary: Some fixes in parameter files errors
821: Author: Nicolas Brouard
822:
823: Revision 1.148 2014/06/17 17:38:48 brouard
824: Summary: Nothing new
825: Author: Brouard
826:
827: Just a new packaging for OS/X version 0.98nS
828:
829: Revision 1.147 2014/06/16 10:33:11 brouard
830: *** empty log message ***
831:
832: Revision 1.146 2014/06/16 10:20:28 brouard
833: Summary: Merge
834: Author: Brouard
835:
836: Merge, before building revised version.
837:
838: Revision 1.145 2014/06/10 21:23:15 brouard
839: Summary: Debugging with valgrind
840: Author: Nicolas Brouard
841:
842: Lot of changes in order to output the results with some covariates
843: After the Edimburgh REVES conference 2014, it seems mandatory to
844: improve the code.
845: No more memory valgrind error but a lot has to be done in order to
846: continue the work of splitting the code into subroutines.
847: Also, decodemodel has been improved. Tricode is still not
848: optimal. nbcode should be improved. Documentation has been added in
849: the source code.
850:
851: Revision 1.143 2014/01/26 09:45:38 brouard
852: Summary: Version 0.98nR (to be improved, but gives same optimization results as 0.98k. Nice, promising
853:
854: * imach.c (Module): Trying to merge old staffs together while being at Tokyo. Not tested...
855: (Module): Version 0.98nR Running ok, but output format still only works for three covariates.
856:
857: Revision 1.142 2014/01/26 03:57:36 brouard
858: Summary: gnuplot changed plot w l 1 has to be changed to plot w l lt 2
859:
860: * imach.c (Module): Trying to merge old staffs together while being at Tokyo. Not tested...
861:
862: Revision 1.141 2014/01/26 02:42:01 brouard
863: * imach.c (Module): Trying to merge old staffs together while being at Tokyo. Not tested...
864:
865: Revision 1.140 2011/09/02 10:37:54 brouard
866: Summary: times.h is ok with mingw32 now.
867:
868: Revision 1.139 2010/06/14 07:50:17 brouard
869: After the theft of my laptop, I probably lost some lines of codes which were not uploaded to the CVS tree.
870: I remember having already fixed agemin agemax which are pointers now but not cvs saved.
871:
872: Revision 1.138 2010/04/30 18:19:40 brouard
873: *** empty log message ***
874:
875: Revision 1.137 2010/04/29 18:11:38 brouard
876: (Module): Checking covariates for more complex models
877: than V1+V2. A lot of change to be done. Unstable.
878:
879: Revision 1.136 2010/04/26 20:30:53 brouard
880: (Module): merging some libgsl code. Fixing computation
881: of likelione (using inter/intrapolation if mle = 0) in order to
882: get same likelihood as if mle=1.
883: Some cleaning of code and comments added.
884:
885: Revision 1.135 2009/10/29 15:33:14 brouard
886: (Module): Now imach stops if date of birth, at least year of birth, is not given. Some cleaning of the code.
887:
888: Revision 1.134 2009/10/29 13:18:53 brouard
889: (Module): Now imach stops if date of birth, at least year of birth, is not given. Some cleaning of the code.
890:
891: Revision 1.133 2009/07/06 10:21:25 brouard
892: just nforces
893:
894: Revision 1.132 2009/07/06 08:22:05 brouard
895: Many tings
896:
897: Revision 1.131 2009/06/20 16:22:47 brouard
898: Some dimensions resccaled
899:
900: Revision 1.130 2009/05/26 06:44:34 brouard
901: (Module): Max Covariate is now set to 20 instead of 8. A
902: lot of cleaning with variables initialized to 0. Trying to make
903: V2+V3*age+V1+V4 strb=V3*age+V1+V4 working better.
904:
905: Revision 1.129 2007/08/31 13:49:27 lievre
906: Modification of the way of exiting when the covariate is not binary in order to see on the window the error message before exiting
907:
908: Revision 1.128 2006/06/30 13:02:05 brouard
909: (Module): Clarifications on computing e.j
910:
911: Revision 1.127 2006/04/28 18:11:50 brouard
912: (Module): Yes the sum of survivors was wrong since
913: imach-114 because nhstepm was no more computed in the age
914: loop. Now we define nhstepma in the age loop.
915: (Module): In order to speed up (in case of numerous covariates) we
916: compute health expectancies (without variances) in a first step
917: and then all the health expectancies with variances or standard
918: deviation (needs data from the Hessian matrices) which slows the
919: computation.
920: In the future we should be able to stop the program is only health
921: expectancies and graph are needed without standard deviations.
922:
923: Revision 1.126 2006/04/28 17:23:28 brouard
924: (Module): Yes the sum of survivors was wrong since
925: imach-114 because nhstepm was no more computed in the age
926: loop. Now we define nhstepma in the age loop.
927: Version 0.98h
928:
929: Revision 1.125 2006/04/04 15:20:31 lievre
930: Errors in calculation of health expectancies. Age was not initialized.
931: Forecasting file added.
932:
933: Revision 1.124 2006/03/22 17:13:53 lievre
934: Parameters are printed with %lf instead of %f (more numbers after the comma).
935: The log-likelihood is printed in the log file
936:
937: Revision 1.123 2006/03/20 10:52:43 brouard
938: * imach.c (Module): <title> changed, corresponds to .htm file
939: name. <head> headers where missing.
940:
941: * imach.c (Module): Weights can have a decimal point as for
942: English (a comma might work with a correct LC_NUMERIC environment,
943: otherwise the weight is truncated).
944: Modification of warning when the covariates values are not 0 or
945: 1.
946: Version 0.98g
947:
948: Revision 1.122 2006/03/20 09:45:41 brouard
949: (Module): Weights can have a decimal point as for
950: English (a comma might work with a correct LC_NUMERIC environment,
951: otherwise the weight is truncated).
952: Modification of warning when the covariates values are not 0 or
953: 1.
954: Version 0.98g
955:
956: Revision 1.121 2006/03/16 17:45:01 lievre
957: * imach.c (Module): Comments concerning covariates added
958:
959: * imach.c (Module): refinements in the computation of lli if
960: status=-2 in order to have more reliable computation if stepm is
961: not 1 month. Version 0.98f
962:
963: Revision 1.120 2006/03/16 15:10:38 lievre
964: (Module): refinements in the computation of lli if
965: status=-2 in order to have more reliable computation if stepm is
966: not 1 month. Version 0.98f
967:
968: Revision 1.119 2006/03/15 17:42:26 brouard
969: (Module): Bug if status = -2, the loglikelihood was
970: computed as likelihood omitting the logarithm. Version O.98e
971:
972: Revision 1.118 2006/03/14 18:20:07 brouard
973: (Module): varevsij Comments added explaining the second
974: table of variances if popbased=1 .
975: (Module): Covariances of eij, ekl added, graphs fixed, new html link.
976: (Module): Function pstamp added
977: (Module): Version 0.98d
978:
979: Revision 1.117 2006/03/14 17:16:22 brouard
980: (Module): varevsij Comments added explaining the second
981: table of variances if popbased=1 .
982: (Module): Covariances of eij, ekl added, graphs fixed, new html link.
983: (Module): Function pstamp added
984: (Module): Version 0.98d
985:
986: Revision 1.116 2006/03/06 10:29:27 brouard
987: (Module): Variance-covariance wrong links and
988: varian-covariance of ej. is needed (Saito).
989:
990: Revision 1.115 2006/02/27 12:17:45 brouard
991: (Module): One freematrix added in mlikeli! 0.98c
992:
993: Revision 1.114 2006/02/26 12:57:58 brouard
994: (Module): Some improvements in processing parameter
995: filename with strsep.
996:
997: Revision 1.113 2006/02/24 14:20:24 brouard
998: (Module): Memory leaks checks with valgrind and:
999: datafile was not closed, some imatrix were not freed and on matrix
1000: allocation too.
1001:
1002: Revision 1.112 2006/01/30 09:55:26 brouard
1003: (Module): Back to gnuplot.exe instead of wgnuplot.exe
1004:
1005: Revision 1.111 2006/01/25 20:38:18 brouard
1006: (Module): Lots of cleaning and bugs added (Gompertz)
1007: (Module): Comments can be added in data file. Missing date values
1008: can be a simple dot '.'.
1009:
1010: Revision 1.110 2006/01/25 00:51:50 brouard
1011: (Module): Lots of cleaning and bugs added (Gompertz)
1012:
1013: Revision 1.109 2006/01/24 19:37:15 brouard
1014: (Module): Comments (lines starting with a #) are allowed in data.
1015:
1016: Revision 1.108 2006/01/19 18:05:42 lievre
1017: Gnuplot problem appeared...
1018: To be fixed
1019:
1020: Revision 1.107 2006/01/19 16:20:37 brouard
1021: Test existence of gnuplot in imach path
1022:
1023: Revision 1.106 2006/01/19 13:24:36 brouard
1024: Some cleaning and links added in html output
1025:
1026: Revision 1.105 2006/01/05 20:23:19 lievre
1027: *** empty log message ***
1028:
1029: Revision 1.104 2005/09/30 16:11:43 lievre
1030: (Module): sump fixed, loop imx fixed, and simplifications.
1031: (Module): If the status is missing at the last wave but we know
1032: that the person is alive, then we can code his/her status as -2
1033: (instead of missing=-1 in earlier versions) and his/her
1034: contributions to the likelihood is 1 - Prob of dying from last
1035: health status (= 1-p13= p11+p12 in the easiest case of somebody in
1036: the healthy state at last known wave). Version is 0.98
1037:
1038: Revision 1.103 2005/09/30 15:54:49 lievre
1039: (Module): sump fixed, loop imx fixed, and simplifications.
1040:
1041: Revision 1.102 2004/09/15 17:31:30 brouard
1042: Add the possibility to read data file including tab characters.
1043:
1044: Revision 1.101 2004/09/15 10:38:38 brouard
1045: Fix on curr_time
1046:
1047: Revision 1.100 2004/07/12 18:29:06 brouard
1048: Add version for Mac OS X. Just define UNIX in Makefile
1049:
1050: Revision 1.99 2004/06/05 08:57:40 brouard
1051: *** empty log message ***
1052:
1053: Revision 1.98 2004/05/16 15:05:56 brouard
1054: New version 0.97 . First attempt to estimate force of mortality
1055: directly from the data i.e. without the need of knowing the health
1056: state at each age, but using a Gompertz model: log u =a + b*age .
1057: This is the basic analysis of mortality and should be done before any
1058: other analysis, in order to test if the mortality estimated from the
1059: cross-longitudinal survey is different from the mortality estimated
1060: from other sources like vital statistic data.
1061:
1062: The same imach parameter file can be used but the option for mle should be -3.
1063:
1064: Agnès, who wrote this part of the code, tried to keep most of the
1065: former routines in order to include the new code within the former code.
1066:
1067: The output is very simple: only an estimate of the intercept and of
1068: the slope with 95% confident intervals.
1069:
1070: Current limitations:
1071: A) Even if you enter covariates, i.e. with the
1072: model= V1+V2 equation for example, the programm does only estimate a unique global model without covariates.
1073: B) There is no computation of Life Expectancy nor Life Table.
1074:
1075: Revision 1.97 2004/02/20 13:25:42 lievre
1076: Version 0.96d. Population forecasting command line is (temporarily)
1077: suppressed.
1078:
1079: Revision 1.96 2003/07/15 15:38:55 brouard
1080: * imach.c (Repository): Errors in subdirf, 2, 3 while printing tmpout is
1081: rewritten within the same printf. Workaround: many printfs.
1082:
1083: Revision 1.95 2003/07/08 07:54:34 brouard
1084: * imach.c (Repository):
1085: (Repository): Using imachwizard code to output a more meaningful covariance
1086: matrix (cov(a12,c31) instead of numbers.
1087:
1088: Revision 1.94 2003/06/27 13:00:02 brouard
1089: Just cleaning
1090:
1091: Revision 1.93 2003/06/25 16:33:55 brouard
1092: (Module): On windows (cygwin) function asctime_r doesn't
1093: exist so I changed back to asctime which exists.
1094: (Module): Version 0.96b
1095:
1096: Revision 1.92 2003/06/25 16:30:45 brouard
1097: (Module): On windows (cygwin) function asctime_r doesn't
1098: exist so I changed back to asctime which exists.
1099:
1100: Revision 1.91 2003/06/25 15:30:29 brouard
1101: * imach.c (Repository): Duplicated warning errors corrected.
1102: (Repository): Elapsed time after each iteration is now output. It
1103: helps to forecast when convergence will be reached. Elapsed time
1104: is stamped in powell. We created a new html file for the graphs
1105: concerning matrix of covariance. It has extension -cov.htm.
1106:
1107: Revision 1.90 2003/06/24 12:34:15 brouard
1108: (Module): Some bugs corrected for windows. Also, when
1109: mle=-1 a template is output in file "or"mypar.txt with the design
1110: of the covariance matrix to be input.
1111:
1112: Revision 1.89 2003/06/24 12:30:52 brouard
1113: (Module): Some bugs corrected for windows. Also, when
1114: mle=-1 a template is output in file "or"mypar.txt with the design
1115: of the covariance matrix to be input.
1116:
1117: Revision 1.88 2003/06/23 17:54:56 brouard
1118: * 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.
1119:
1120: Revision 1.87 2003/06/18 12:26:01 brouard
1121: Version 0.96
1122:
1123: Revision 1.86 2003/06/17 20:04:08 brouard
1124: (Module): Change position of html and gnuplot routines and added
1125: routine fileappend.
1126:
1127: Revision 1.85 2003/06/17 13:12:43 brouard
1128: * imach.c (Repository): Check when date of death was earlier that
1129: current date of interview. It may happen when the death was just
1130: prior to the death. In this case, dh was negative and likelihood
1131: was wrong (infinity). We still send an "Error" but patch by
1132: assuming that the date of death was just one stepm after the
1133: interview.
1134: (Repository): Because some people have very long ID (first column)
1135: we changed int to long in num[] and we added a new lvector for
1136: memory allocation. But we also truncated to 8 characters (left
1137: truncation)
1138: (Repository): No more line truncation errors.
1139:
1140: Revision 1.84 2003/06/13 21:44:43 brouard
1141: * imach.c (Repository): Replace "freqsummary" at a correct
1142: place. It differs from routine "prevalence" which may be called
1143: many times. Probs is memory consuming and must be used with
1144: parcimony.
1145: Version 0.95a3 (should output exactly the same maximization than 0.8a2)
1146:
1147: Revision 1.83 2003/06/10 13:39:11 lievre
1148: *** empty log message ***
1149:
1150: Revision 1.82 2003/06/05 15:57:20 brouard
1151: Add log in imach.c and fullversion number is now printed.
1152:
1153: */
1154: /*
1155: Interpolated Markov Chain
1156:
1157: Short summary of the programme:
1158:
1159: This program computes Healthy Life Expectancies or State-specific
1160: (if states aren't health statuses) Expectancies from
1161: cross-longitudinal data. Cross-longitudinal data consist in:
1162:
1163: -1- a first survey ("cross") where individuals from different ages
1164: are interviewed on their health status or degree of disability (in
1165: the case of a health survey which is our main interest)
1166:
1167: -2- at least a second wave of interviews ("longitudinal") which
1168: measure each change (if any) in individual health status. Health
1169: expectancies are computed from the time spent in each health state
1170: according to a model. More health states you consider, more time is
1171: necessary to reach the Maximum Likelihood of the parameters involved
1172: in the model. The simplest model is the multinomial logistic model
1173: where pij is the probability to be observed in state j at the second
1174: wave conditional to be observed in state i at the first
1175: wave. Therefore the model is: log(pij/pii)= aij + bij*age+ cij*sex +
1176: etc , where 'age' is age and 'sex' is a covariate. If you want to
1177: have a more complex model than "constant and age", you should modify
1178: the program where the markup *Covariates have to be included here
1179: again* invites you to do it. More covariates you add, slower the
1180: convergence.
1181:
1182: The advantage of this computer programme, compared to a simple
1183: multinomial logistic model, is clear when the delay between waves is not
1184: identical for each individual. Also, if a individual missed an
1185: intermediate interview, the information is lost, but taken into
1186: account using an interpolation or extrapolation.
1187:
1188: hPijx is the probability to be observed in state i at age x+h
1189: conditional to the observed state i at age x. The delay 'h' can be
1190: split into an exact number (nh*stepm) of unobserved intermediate
1191: states. This elementary transition (by month, quarter,
1192: semester or year) is modelled as a multinomial logistic. The hPx
1193: matrix is simply the matrix product of nh*stepm elementary matrices
1194: and the contribution of each individual to the likelihood is simply
1195: hPijx.
1196:
1197: Also this programme outputs the covariance matrix of the parameters but also
1198: of the life expectancies. It also computes the period (stable) prevalence.
1199:
1200: Back prevalence and projections:
1201:
1202: - back_prevalence_limit(double *p, double **bprlim, double ageminpar,
1203: double agemaxpar, double ftolpl, int *ncvyearp, double
1204: dateprev1,double dateprev2, int firstpass, int lastpass, int
1205: mobilavproj)
1206:
1207: Computes the back prevalence limit for any combination of
1208: covariate values k at any age between ageminpar and agemaxpar and
1209: returns it in **bprlim. In the loops,
1210:
1211: - **bprevalim(**bprlim, ***mobaverage, nlstate, *p, age, **oldm,
1212: **savm, **dnewm, **doldm, **dsavm, ftolpl, ncvyearp, k);
1213:
1214: - hBijx Back Probability to be in state i at age x-h being in j at x
1215: Computes for any combination of covariates k and any age between bage and fage
1216: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
1217: oldm=oldms;savm=savms;
1218:
1219: - hbxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k, nres);
1220: Computes the transition matrix starting at age 'age' over
1221: 'nhstepm*hstepm*stepm' months (i.e. until
1222: age (in years) age+nhstepm*hstepm*stepm/12) by multiplying
1223: nhstepm*hstepm matrices.
1224:
1225: Returns p3mat[i][j][h] after calling
1226: p3mat[i][j][h]=matprod2(newm,
1227: bmij(pmmij,cov,ncovmodel,x,nlstate,prevacurrent, dnewm, doldm,
1228: dsavm,ij),\ 1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath,
1229: oldm);
1230:
1231: Important routines
1232:
1233: - func (or funcone), computes logit (pij) distinguishing
1234: o fixed variables (single or product dummies or quantitative);
1235: o varying variables by:
1236: (1) wave (single, product dummies, quantitative),
1237: (2) by age (can be month) age (done), age*age (done), age*Vn where Vn can be:
1238: % fixed dummy (treated) or quantitative (not done because time-consuming);
1239: % varying dummy (not done) or quantitative (not done);
1240: - Tricode which tests the modality of dummy variables (in order to warn with wrong or empty modalities)
1241: and returns the number of efficient covariates cptcoveff and modalities nbcode[Tvar[k]][1]= 0 and nbcode[Tvar[k]][2]= 1 usually.
1242: - printinghtml which outputs results like life expectancy in and from a state for a combination of modalities of dummy variables
1243: o There are 2**cptcoveff combinations of (0,1) for cptcoveff variables. Outputting only combinations with people, éliminating 1 1 if
1244: race White (0 0), Black vs White (1 0), Hispanic (0 1) and 1 1 being meaningless.
1245:
1246:
1247:
1248: Authors: Nicolas Brouard (brouard@ined.fr) and Agnès Lièvre (lievre@ined.fr).
1249: Institut national d'études démographiques, Paris.
1250: This software have been partly granted by Euro-REVES, a concerted action
1251: from the European Union.
1252: It is copyrighted identically to a GNU software product, ie programme and
1253: software can be distributed freely for non commercial use. Latest version
1254: can be accessed at http://euroreves.ined.fr/imach .
1255:
1256: Help to debug: LD_PRELOAD=/usr/local/lib/libnjamd.so ./imach foo.imach
1257: or better on gdb : set env LD_PRELOAD=/usr/local/lib/libnjamd.so
1258:
1259: **********************************************************************/
1260: /*
1261: main
1262: read parameterfile
1263: read datafile
1264: concatwav
1265: freqsummary
1266: if (mle >= 1)
1267: mlikeli
1268: print results files
1269: if mle==1
1270: computes hessian
1271: read end of parameter file: agemin, agemax, bage, fage, estepm
1272: begin-prev-date,...
1273: open gnuplot file
1274: open html file
1275: period (stable) prevalence | pl_nom 1-1 2-2 etc by covariate
1276: for age prevalim() | #****** V1=0 V2=1 V3=1 V4=0 ******
1277: | 65 1 0 2 1 3 1 4 0 0.96326 0.03674
1278: freexexit2 possible for memory heap.
1279:
1280: h Pij x | pij_nom ficrestpij
1281: # Cov Agex agex+h hpijx with i,j= 1-1 1-2 1-3 2-1 2-2 2-3
1282: 1 85 85 1.00000 0.00000 0.00000 0.00000 1.00000 0.00000
1283: 1 85 86 0.68299 0.22291 0.09410 0.71093 0.00000 0.28907
1284:
1285: 1 65 99 0.00364 0.00322 0.99314 0.00350 0.00310 0.99340
1286: 1 65 100 0.00214 0.00204 0.99581 0.00206 0.00196 0.99597
1287: variance of p one-step probabilities varprob | prob_nom ficresprob #One-step probabilities and stand. devi in ()
1288: Standard deviation of one-step probabilities | probcor_nom ficresprobcor #One-step probabilities and correlation matrix
1289: Matrix of variance covariance of one-step probabilities | probcov_nom ficresprobcov #One-step probabilities and covariance matrix
1290:
1291: forecasting if prevfcast==1 prevforecast call prevalence()
1292: health expectancies
1293: Variance-covariance of DFLE
1294: prevalence()
1295: movingaverage()
1296: varevsij()
1297: if popbased==1 varevsij(,popbased)
1298: total life expectancies
1299: Variance of period (stable) prevalence
1300: end
1301: */
1302:
1303: /* #define DEBUG */
1304: /* #define DEBUGBRENT */
1305: /* #define DEBUGLINMIN */
1306: /* #define DEBUGHESS */
1307: #define DEBUGHESSIJ
1308: /* #define LINMINORIGINAL /\* Don't use loop on scale in linmin (accepting nan) *\/ */
1309: #define POWELL /* Instead of NLOPT */
1310: #define POWELLNOF3INFF1TEST /* Skip test */
1311: /* #define POWELLORIGINAL /\* Don't use Directest to decide new direction but original Powell test *\/ */
1312: /* #define MNBRAKORIGINAL /\* Don't use mnbrak fix *\/ */
1313: /* #define FLATSUP *//* Suppresses directions where likelihood is flat */
1314: /* #define POWELLORIGINCONJUGATE /\* Don't use conjugate but biggest decrease if valuable *\/ */
1315: /* #define NOTMINFIT */
1316:
1317: #include <math.h>
1318: #include <stdio.h>
1319: #include <stdlib.h>
1320: #include <string.h>
1321: #include <ctype.h>
1322:
1323: #ifdef _WIN32
1324: #include <io.h>
1325: #include <windows.h>
1326: #include <tchar.h>
1327: #else
1328: #include <unistd.h>
1329: #endif
1330:
1331: #include <limits.h>
1332: #include <sys/types.h>
1333:
1334: #if defined(__GNUC__)
1335: #include <sys/utsname.h> /* Doesn't work on Windows */
1336: #endif
1337:
1338: #include <sys/stat.h>
1339: #include <errno.h>
1340: /* extern int errno; */
1341:
1342: /* #ifdef LINUX */
1343: /* #include <time.h> */
1344: /* #include "timeval.h" */
1345: /* #else */
1346: /* #include <sys/time.h> */
1347: /* #endif */
1348:
1349: #include <time.h>
1350:
1351: #ifdef GSL
1352: #include <gsl/gsl_errno.h>
1353: #include <gsl/gsl_multimin.h>
1354: #endif
1355:
1356:
1357: #ifdef NLOPT
1358: #include <nlopt.h>
1359: typedef struct {
1360: double (* function)(double [] );
1361: } myfunc_data ;
1362: #endif
1363:
1364: /* #include <libintl.h> */
1365: /* #define _(String) gettext (String) */
1366:
1367: #define MAXLINE 16384 /* Was 256 and 1024 and 2048. Overflow with 312 with 2 states and 4 covariates. Should be ok */
1368:
1369: #define GNUPLOTPROGRAM "gnuplot"
1370: #define GNUPLOTVERSION 5.1
1371: double gnuplotversion=GNUPLOTVERSION;
1372: /*#define GNUPLOTPROGRAM "..\\gp37mgw\\wgnuplot"*/
1373: #define FILENAMELENGTH 256
1374:
1375: #define GLOCK_ERROR_NOPATH -1 /* empty path */
1376: #define GLOCK_ERROR_GETCWD -2 /* cannot get cwd */
1377:
1378: #define MAXPARM 216 /**< Maximum number of parameters for the optimization was 128 */
1379: #define NPARMAX 64 /**< (nlstate+ndeath-1)*nlstate*ncovmodel */
1380:
1381: #define NINTERVMAX 8
1382: #define NLSTATEMAX 8 /**< Maximum number of live states (for func) */
1383: #define NDEATHMAX 8 /**< Maximum number of dead states (for func) */
1384: #define NCOVMAX 30 /**< Maximum number of covariates used in the model, including generated covariates V1*V2 or V1*age */
1385: #define codtabm(h,k) (1 & (h-1) >> (k-1))+1
1386: /*#define decodtabm(h,k,cptcoveff)= (h <= (1<<cptcoveff)?(((h-1) >> (k-1)) & 1) +1 : -1)*/
1387: #define decodtabm(h,k,cptcoveff) (((h-1) >> (k-1)) & 1) +1
1388: /*#define MAXN 20000 */ /* Should by replaced by nobs, real number of observations and unlimited */
1389: #define YEARM 12. /**< Number of months per year */
1390: /* #define AGESUP 130 */
1391: /* #define AGESUP 150 */
1392: #define AGESUP 200
1393: #define AGEINF 0
1394: #define AGEMARGE 25 /* Marge for agemin and agemax for(iage=agemin-AGEMARGE; iage <= agemax+3+AGEMARGE; iage++) */
1395: #define AGEBASE 40
1396: #define AGEOVERFLOW 1.e20
1397: #define AGEGOMP 10 /**< Minimal age for Gompertz adjustment */
1398: #ifdef _WIN32
1399: #define DIRSEPARATOR '\\'
1400: #define CHARSEPARATOR "\\"
1401: #define ODIRSEPARATOR '/'
1402: #else
1403: #define DIRSEPARATOR '/'
1404: #define CHARSEPARATOR "/"
1405: #define ODIRSEPARATOR '\\'
1406: #endif
1407:
1408: /* $Id: imach.c,v 1.361 2024/05/12 20:29:32 brouard Exp $ */
1409: /* $State: Exp $ */
1410: #include "version.h"
1411: char version[]=__IMACH_VERSION__;
1412: char copyright[]="April 2024,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-2024";
1413: char fullversion[]="$Revision: 1.361 $ $Date: 2024/05/12 20:29:32 $";
1414: char strstart[80];
1415: char optionfilext[10], optionfilefiname[FILENAMELENGTH];
1416: int erreur=0, nberr=0, nbwarn=0; /* Error number, number of errors number of warnings */
1417: int debugILK=0; /* debugILK is set by a #d in a comment line */
1418: int nagesqr=0, nforce=0; /* nagesqr=1 if model is including age*age, number of forces */
1419: /* Number of covariates model (1)=V2+V1+ V3*age+V2*V4 */
1420: /* Model(2) V1 + V2 + V3 + V8 + V7*V8 + V5*V6 + V8*age + V3*age + age*age */
1421: int cptcovn=0; /**< cptcovn decodemodel: number of covariates k of the models excluding age*products =6 and age*age but including products */
1422: int cptcovt=0; /**< cptcovt: total number of covariates of the model (2) nbocc(+)+1 = 8 excepting constant and age and age*age */
1423: int cptcovs=0; /**< cptcovs number of SIMPLE covariates in the model V2+V1 =2 (dummy or quantit or time varying) */
1424: int cptcovsnq=0; /**< cptcovsnq number of SIMPLE covariates in the model but non quantitative V2+V1 =2 */
1425: int cptcovage=0; /**< Number of covariates with age: V3*age only =1 */
1426: int cptcovprodage=0; /**< Number of fixed covariates with age: V3*age or V2*V3*age 1 */
1427: int cptcovprodvage=0; /**< Number of varying covariates with age: V7*age or V7*V6*age */
1428: int cptcovdageprod=0; /**< Number of doubleproducts with age, since 0.99r44 only: age*Vn*Vm for gnuplot printing*/
1429: int cptcovprodnoage=0; /**< Number of covariate products without age */
1430: 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) */
1431: int ncovf=0; /* Total number of effective fixed covariates (dummy or quantitative) in the model */
1432: int ncovv=0; /* Total number of effective (wave) varying covariates (dummy or quantitative) in the model */
1433: int ncovvt=0; /* Total number of effective (wave) varying covariates (dummy or quantitative or products [without age]) in the model */
1434: int ncovvta=0; /* +age*V6 + age*V7+ age*V6*V3 +age*V7*V3 + age*V6*V4 +age*V7*V4 Total number of expandend products [with age]) in the model */
1435: int ncovta=0; /*age*V3*V2 +age*V2+agev3+ageV4 +age*V6 + age*V7+ age*V6*V3 +age*V7*V3 + age*V6*V4 +age*V7*V4 Total number of expandend products [with age]) in the model */
1436: int ncova=0; /* Total number of effective (wave and stepm) varying with age covariates (single or product, dummy or quantitative) in the model */
1437: int ncovva=0; /* +age*V6 + age*V7+ge*V6*V3 +age*V7*V3 + age*V6*V4 +age*V7*V4 Total number of effective (wave and stepm) varying with age covariates (single or product, dummy or quantitative) in the model */
1438: int nsd=0; /**< Total number of single dummy variables (output) */
1439: int nsq=0; /**< Total number of single quantitative variables (output) */
1440: int ncoveff=0; /* Total number of effective fixed dummy covariates in the model */
1441: int nqfveff=0; /**< nqfveff Number of Quantitative Fixed Variables Effective */
1442: int ntveff=0; /**< ntveff number of effective time varying variables */
1443: int nqtveff=0; /**< ntqveff number of effective time varying quantitative variables */
1444: int cptcov=0; /* Working variable */
1445: int firstobs=1, lastobs=10; /* nobs = lastobs-firstobs+1 declared globally ;*/
1446: int nobs=10; /* Number of observations in the data lastobs-firstobs */
1447: int ncovcombmax=NCOVMAX; /* Maximum calculated number of covariate combination = pow(2, cptcoveff) */
1448: int npar=NPARMAX; /* Number of parameters (nlstate+ndeath-1)*nlstate*ncovmodel; */
1449: int nlstate=2; /* Number of live states */
1450: int ndeath=1; /* Number of dead states */
1451: int ncovmodel=0, ncovcol=0; /* Total number of covariables including constant a12*1 +b12*x ncovmodel=2 */
1452: int nqv=0, ntv=0, nqtv=0; /* Total number of quantitative variables, time variable (dummy), quantitative and time variable*/
1453: int ncovcolt=0; /* ncovcolt=ncovcol+nqv+ntv+nqtv; total of covariates in the data, not in the model equation*/
1454: int popbased=0;
1455:
1456: int *wav; /* Number of waves for this individuual 0 is possible */
1457: int maxwav=0; /* Maxim number of waves */
1458: int jmin=0, jmax=0; /* min, max spacing between 2 waves */
1459: int ijmin=0, ijmax=0; /* Individuals having jmin and jmax */
1460: int gipmx = 0;
1461: double gsw = 0; /* Global variables on the number of contributions
1462: to the likelihood and the sum of weights (done by funcone)*/
1463: int mle=1, weightopt=0;
1464: int **mw; /* mw[mi][i] is number of the mi wave for this individual */
1465: int **dh; /* dh[mi][i] is number of steps between mi,mi+1 for this individual */
1466: int **bh; /* bh[mi][i] is the bias (+ or -) for this individual if the delay between
1467: * wave mi and wave mi+1 is not an exact multiple of stepm. */
1468: int countcallfunc=0; /* Count the number of calls to func */
1469: int selected(int kvar); /* Is covariate kvar selected for printing results */
1470:
1471: double jmean=1; /* Mean space between 2 waves */
1472: double **matprod2(); /* test */
1473: double **oldm, **newm, **savm; /* Working pointers to matrices */
1474: double **oldms, **newms, **savms; /* Fixed working pointers to matrices */
1475: double **ddnewms, **ddoldms, **ddsavms; /* for freeing later */
1476:
1477: /*FILE *fic ; */ /* Used in readdata only */
1478: FILE *ficpar, *ficparo,*ficres, *ficresp, *ficresphtm, *ficresphtmfr, *ficrespl, *ficresplb,*ficrespij, *ficrespijb, *ficrest,*ficresf, *ficresfb,*ficrespop;
1479: FILE *ficlog, *ficrespow;
1480: int globpr=0; /* Global variable for printing or not */
1481: double fretone; /* Only one call to likelihood */
1482: long ipmx=0; /* Number of contributions */
1483: double sw; /* Sum of weights */
1484: char filerespow[FILENAMELENGTH];
1485: char fileresilk[FILENAMELENGTH]; /* File of individual contributions to the likelihood */
1486: FILE *ficresilk;
1487: FILE *ficgp,*ficresprob,*ficpop, *ficresprobcov, *ficresprobcor;
1488: FILE *ficresprobmorprev;
1489: FILE *fichtm, *fichtmcov; /* Html File */
1490: FILE *ficreseij;
1491: char filerese[FILENAMELENGTH];
1492: FILE *ficresstdeij;
1493: char fileresstde[FILENAMELENGTH];
1494: FILE *ficrescveij;
1495: char filerescve[FILENAMELENGTH];
1496: FILE *ficresvij;
1497: char fileresv[FILENAMELENGTH];
1498:
1499: char title[MAXLINE];
1500: char model[MAXLINE]; /**< The model line */
1501: char optionfile[FILENAMELENGTH], datafile[FILENAMELENGTH], filerespl[FILENAMELENGTH], fileresplb[FILENAMELENGTH];
1502: char plotcmd[FILENAMELENGTH], pplotcmd[FILENAMELENGTH];
1503: char tmpout[FILENAMELENGTH], tmpout2[FILENAMELENGTH];
1504: char command[FILENAMELENGTH];
1505: int outcmd=0;
1506:
1507: char fileres[FILENAMELENGTH], filerespij[FILENAMELENGTH], filerespijb[FILENAMELENGTH], filereso[FILENAMELENGTH], rfileres[FILENAMELENGTH];
1508: char fileresu[FILENAMELENGTH]; /* fileres without r in front */
1509: char filelog[FILENAMELENGTH]; /* Log file */
1510: char filerest[FILENAMELENGTH];
1511: char fileregp[FILENAMELENGTH];
1512: char popfile[FILENAMELENGTH];
1513:
1514: char optionfilegnuplot[FILENAMELENGTH], optionfilehtm[FILENAMELENGTH], optionfilehtmcov[FILENAMELENGTH] ;
1515:
1516: /* struct timeval start_time, end_time, curr_time, last_time, forecast_time; */
1517: /* struct timezone tzp; */
1518: /* extern int gettimeofday(); */
1519: struct tm tml, *gmtime(), *localtime();
1520:
1521: extern time_t time();
1522:
1523: struct tm start_time, end_time, curr_time, last_time, forecast_time;
1524: time_t rstart_time, rend_time, rcurr_time, rlast_time, rforecast_time; /* raw time */
1525: time_t rlast_btime; /* raw time */
1526: struct tm tm;
1527:
1528: char strcurr[80], strfor[80];
1529:
1530: char *endptr;
1531: long lval;
1532: double dval;
1533:
1534: #define NR_END 1
1535: #define FREE_ARG char*
1536: #define FTOL 1.0e-10
1537:
1538: #define NRANSI
1539: #define ITMAX 200
1540: #define ITPOWMAX 20 /* This is now multiplied by the number of parameters */
1541:
1542: #define TOL 2.0e-4
1543:
1544: #define CGOLD 0.3819660
1545: #define ZEPS 1.0e-10
1546: #define SHFT(a,b,c,d) (a)=(b);(b)=(c);(c)=(d);
1547:
1548: #define GOLD 1.618034
1549: #define GLIMIT 100.0
1550: #define TINY 1.0e-20
1551:
1552: static double maxarg1,maxarg2;
1553: #define FMAX(a,b) (maxarg1=(a),maxarg2=(b),(maxarg1)>(maxarg2)? (maxarg1):(maxarg2))
1554: #define FMIN(a,b) (maxarg1=(a),maxarg2=(b),(maxarg1)<(maxarg2)? (maxarg1):(maxarg2))
1555:
1556: #define SIGN(a,b) ((b)>0.0 ? fabs(a) : -fabs(a))
1557: #define rint(a) floor(a+0.5)
1558: /* http://www.thphys.uni-heidelberg.de/~robbers/cmbeasy/doc/html/myutils_8h-source.html */
1559: #define mytinydouble 1.0e-16
1560: /* #define DEQUAL(a,b) (fabs((a)-(b))<mytinydouble) */
1561: /* http://www.thphys.uni-heidelberg.de/~robbers/cmbeasy/doc/html/mynrutils_8h-source.html */
1562: /* static double dsqrarg; */
1563: /* #define DSQR(a) (DEQUAL((dsqrarg=(a)),0.0) ? 0.0 : dsqrarg*dsqrarg) */
1564: static double sqrarg;
1565: #define SQR(a) ((sqrarg=(a)) == 0.0 ? 0.0 :sqrarg*sqrarg)
1566: #define SWAP(a,b) {temp=(a);(a)=(b);(b)=temp;}
1567: int agegomp= AGEGOMP;
1568:
1569: int imx;
1570: int stepm=1;
1571: /* Stepm, step in month: minimum step interpolation*/
1572:
1573: int estepm;
1574: /* Estepm, step in month to interpolate survival function in order to approximate Life Expectancy*/
1575:
1576: int m,nb;
1577: long *num;
1578: int firstpass=0, lastpass=4,*cod, *cens;
1579: int *ncodemax; /* ncodemax[j]= Number of modalities of the j th
1580: covariate for which somebody answered excluding
1581: undefined. Usually 2: 0 and 1. */
1582: int *ncodemaxwundef; /* ncodemax[j]= Number of modalities of the j th
1583: covariate for which somebody answered including
1584: undefined. Usually 3: -1, 0 and 1. */
1585: double **agev,*moisnais, *annais, *moisdc, *andc,**mint, **anint;
1586: double **pmmij, ***probs; /* Global pointer */
1587: double ***mobaverage, ***mobaverages; /* New global variable */
1588: double **precov; /* New global variable to store for each resultline, values of model covariates given by the resultlines (in order to speed up) */
1589: double *ageexmed,*agecens;
1590: double dateintmean=0;
1591: double anprojd, mprojd, jprojd; /* For eventual projections */
1592: double anprojf, mprojf, jprojf;
1593:
1594: double anbackd, mbackd, jbackd; /* For eventual backprojections */
1595: double anbackf, mbackf, jbackf;
1596: double jintmean,mintmean,aintmean;
1597: double *weight;
1598: int **s; /* Status */
1599: double *agedc;
1600: double **covar; /**< covar[j,i], value of jth covariate for individual i,
1601: * covar=matrix(0,NCOVMAX,1,n);
1602: * cov[Tage[kk]+2]=covar[Tvar[Tage[kk]]][i]*age; */
1603: double **coqvar; /* Fixed quantitative covariate nqv */
1604: double ***cotvar; /* Time varying covariate start at ncovcol + nqv + (1 to ntv) */
1605: double ***cotqvar; /* Time varying quantitative covariate itqv */
1606: double idx;
1607: int **nbcode, *Tvar; /**< model=V2 => Tvar[1]= 2 */
1608: /* Some documentation */
1609: /* Design original data
1610: * V1 V2 V3 V4 V5 V6 V7 V8 Weight ddb ddth d1st s1 V9 V10 V11 V12 s2 V9 V10 V11 V12
1611: * < ncovcol=6 > nqv=2 (V7 V8) dv dv dv qtv dv dv dvv qtv
1612: * ntv=3 nqtv=1
1613: * cptcovn number of covariates (not including constant and age or age*age) = number of plus sign + 1 = 10+1=11
1614: * For time varying covariate, quanti or dummies
1615: * cotqvar[wav][iv(1 to nqtv)][i]= [1][12][i]=(V12) quanti
1616: * cotvar[wav][ncovcol+nqv+ iv(1 to nqtv)][i]= [(1 to nqtv)][i]=(V12) quanti
1617: * cotvar[wav][iv(1 to ntv)][i]= [1][1][i]=(V9) dummies at wav 1
1618: * cotvar[wav][iv(1 to ntv)][i]= [1][2][i]=(V10) dummies at wav 1
1619: * covar[Vk,i], value of the Vkth fixed covariate dummy or quanti for individual i:
1620: * covar[1][i]= (V1), covar[4][i]=(V4), covar[8][i]=(V8)
1621: * Model V2 + V1 + V3*age + V3 + V5*V6 + V7*V8 + V8*age + V8 + V9 + V9*age + V10
1622: * k= 1 2 3 4 5 6 7 8 9 10 11
1623: */
1624: /* According to the model, more columns can be added to covar by the product of covariates */
1625: /* 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
1626: # States 1=Coresidence, 2 Living alone, 3 Institution
1627: # V1=sex, V2=raedyrs Quant Fixed, State=livarnb4..livarnb11, V3=iadl4..iald11, V4=adlw4..adlw11, V5=r4bmi..r11bmi
1628: */
1629: /* V5+V4+ V3+V4*V3 +V5*age+V2 +V1*V2+V1*age+V1+V4*V3*age */
1630: /* kmodel 1 2 3 4 5 6 7 8 9 10 */
1631: /*Typevar[k]= 0 0 0 2 1 0 2 1 0 3 *//*0 for simple covariate (dummy, quantitative,*/
1632: /* fixed or varying), 1 for age product, 2 for*/
1633: /* product without age, 3 for age and double product */
1634: /*Dummy[k]= 1 0 0 1 3 1 1 2 0 3 *//*Dummy[k] 0=dummy (0 1), 1 quantitative */
1635: /*(single or product without age), 2 dummy*/
1636: /* with age product, 3 quant with age product*/
1637: /*Tvar[k]= 5 4 3 6 5 2 7 1 1 6 */
1638: /* nsd 1 2 3 */ /* Counting single dummies covar fixed or tv */
1639: /*TnsdVar[Tvar] 1 2 3 */
1640: /*Tvaraff[nsd] 4 3 1 */ /* ID of single dummy cova fixed or timevary*/
1641: /*TvarsD[nsd] 4 3 1 */ /* ID of single dummy cova fixed or timevary*/
1642: /*TvarsDind[nsd] 2 3 9 */ /* position K of single dummy cova */
1643: /* nsq 1 2 */ /* Counting single quantit tv */
1644: /* TvarsQ[k] 5 2 */ /* Number of single quantitative cova */
1645: /* TvarsQind 1 6 */ /* position K of single quantitative cova */
1646: /* Tprod[i]=k 1 2 */ /* Position in model of the ith prod without age */
1647: /* cptcovage 1 2 3 */ /* Counting cov*age in the model equation */
1648: /* Tage[cptcovage]=k 5 8 10 */ /* Position in the model of ith cov*age */
1649: /* model="V2+V3+V4+V6+V7+V6*V2+V7*V2+V6*V3+V7*V3+V6*V4+V7*V4+age*V2+age*V3+age*V4+age*V6+age*V7+age*V6*V2+age*V6*V3+age*V7*V3+age*V6*V4+age*V7*V4\r"*/
1650: /* p Tvard[1][1]@21 = {6, 2, 7, 2, 6, 3, 7, 3, 6, 4, 7, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0}*/
1651: /* p Tvard[2][1]@21 = {7, 2, 6, 3, 7, 3, 6, 4, 7, 4, 0 <repeats 11 times>} */
1652: /* p Tvardk[1][1]@24 = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 2, 7, 2, 6, 3, 7, 3, 6, 4, 7, 4, 0, 0}*/
1653: /* p Tvardk[1][1]@22 = {0, 0, 0, 0, 0, 0, 0, 0, 6, 2, 7, 2, 6, 3, 7, 3, 6, 4, 7, 4, 0, 0} */
1654: /* Tvard[1][1]@4={4,3,1,2} V4*V3 V1*V2 */ /* Position in model of the ith prod without age */
1655: /* 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*/
1656: /* 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 */
1657: /* TvarFind; TvarFind[1]=6, TvarFind[2]=7, TvarFind[3]=9 *//* Inverse V2(6) is first fixed (single or prod) */
1658: /* Type */
1659: /* V 1 2 3 4 5 */
1660: /* F F V V V */
1661: /* D Q D D Q */
1662: /* */
1663: int *TvarsD;
1664: int *TnsdVar;
1665: int *TvarsDind;
1666: int *TvarsQ;
1667: int *TvarsQind;
1668:
1669: #define MAXRESULTLINESPONE 10+1
1670: int nresult=0;
1671: int parameterline=0; /* # of the parameter (type) line */
1672: int TKresult[MAXRESULTLINESPONE]; /* TKresult[nres]=k for each resultline nres give the corresponding combination of dummies */
1673: int resultmodel[MAXRESULTLINESPONE][NCOVMAX];/* resultmodel[k1]=k3: k1th position in the model corresponds to the k3 position in the resultline */
1674: int modelresult[MAXRESULTLINESPONE][NCOVMAX];/* modelresult[k3]=k1: k1th position in the model corresponds to the k3 position in the resultline */
1675: int Tresult[MAXRESULTLINESPONE][NCOVMAX];/* Tresult[nres][result_position]= value of the dummy variable at the result_position in the nres resultline */
1676: int Tinvresult[MAXRESULTLINESPONE][NCOVMAX];/* Tinvresult[nres][Name of a dummy variable]= value of the variable in the result line */
1677: double TinvDoQresult[MAXRESULTLINESPONE][NCOVMAX];/* TinvDoQresult[nres][Name of a Dummy or Q variable]= value of the variable in the result line */
1678: int Tvresult[MAXRESULTLINESPONE][NCOVMAX]; /* Tvresult[nres][result_position]= name of the dummy variable at the result_position in the nres resultline */
1679: double Tqresult[MAXRESULTLINESPONE][NCOVMAX]; /* Tqresult[nres][result_position]= value of the variable at the result_position in the nres resultline */
1680: double Tqinvresult[MAXRESULTLINESPONE][NCOVMAX]; /* For quantitative variable , value (output) */
1681: int Tvqresult[MAXRESULTLINESPONE][NCOVMAX]; /* Tvqresult[nres][result_position]= id of the variable at the result_position in the nres resultline */
1682:
1683: /* 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
1684: # States 1=Coresidence, 2 Living alone, 3 Institution
1685: # V1=sex, V2=raedyrs Quant Fixed, State=livarnb4..livarnb11, V3=iadl4..iald11, V4=adlw4..adlw11, V5=r4bmi..r11bmi
1686: */
1687: /* 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 *\/ */
1688: 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 */
1689: 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 */
1690: 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 */
1691: int *TvarVind; /**< TvarVind[1]=1, TvarVind[2]=2 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
1692: 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 */
1693: int *TvarAind; /**< TvarindA[1]=5, TvarAind[2]=8 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
1694: int *TvarFD; /**< TvarFD[1]=V1 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
1695: int *TvarFDind; /* TvarFDind[1]=9 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
1696: int *TvarFQ; /* TvarFQ[1]=V2 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ /* Only simple fixed quantitative variable */
1697: int *TvarFQind; /* TvarFQind[1]=6 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ /* Only simple fixed quantitative variable */
1698: int *TvarVD; /* TvarVD[1]=V5 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ /* Only simple fixed quantitative variable */
1699: int *TvarVDind; /* TvarVDind[1]=1 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ /* Only simple fixed quantitative variable */
1700: 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 */
1701: 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 */
1702: int *TvarVV; /* We count ncovvt time varying covariates (single or products without age) and put their name into TvarVV */
1703: int *TvarVVind; /* We count ncovvt time varying covariates (single or products without age) and put their name into TvarVV */
1704: int *TvarVVA; /* We count ncovvt time varying covariates (single or products with age) and put their name into TvarVVA */
1705: int *TvarVVAind; /* We count ncovvt time varying covariates (single or products without age) and put their name into TvarVV */
1706: int *TvarAVVA; /* We count ALL ncovta time varying covariates (single or products with age) and put their name into TvarVVA */
1707: int *TvarAVVAind; /* We count ALL ncovta time varying covariates (single or products without age) and put their name into TvarVV */
1708: /*# ID V1 V2 weight birth death 1st s1 V3 V4 V5 2nd s2 */
1709: /* model V1+V3+age*V1+age*V3+V1*V3 + V1*V3*age */
1710: /* Tvar={1, 3, 1, 3, 6, 6}, the 6 comes from the fact that there are already V1, V2, V3, V4, V5 native covariates */
1711: /* TvarVV={3,1,3,1,3}, for V3 and then the product V1*V3 is decomposed into V1 and V3 */
1712: /* TvarVVind={2,5,5,6,6}, for V3 and then the product V1*V3 is decomposed into V1 and V3 and V1*V3*age into 6,6 */
1713: int *Tvarsel; /**< Selected covariates for output */
1714: double *Tvalsel; /**< Selected modality value of covariate for output */
1715: int *Typevar; /**< 0 for simple covariate (dummy, quantitative, fixed or varying), 1 for age product, 2 for product, 3 age*Vn*Vm */
1716: int *Fixed; /** Fixed[k] 0=fixed, 1 varying, 2 fixed with age product, 3 varying with age product */
1717: 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 */
1718: int *DummyV; /** Dummy[v] 0=dummy (0 1), 1 quantitative */
1719: int *FixedV; /** FixedV[v] 0 fixed, 1 varying */
1720: int *Tage;
1721: int anyvaryingduminmodel=0; /**< Any varying dummy in Model=1 yes, 0 no, to avoid a loop on waves in freq */
1722: 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*/
1723: 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*/
1724: 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 */
1725: int *Ndum; /** Freq of modality (tricode */
1726: /* int **codtab;*/ /**< codtab=imatrix(1,100,1,10); */
1727: int **Tvard;
1728: int **Tvardk;
1729: int *Tprod;/**< Gives the k position of the k1 product */
1730: /* Tprod[k1=1]=3(=V1*V4) for V2+V1+V1*V4+age*V3 */
1731: int *Tposprod; /**< Gives the k1 product from the k position */
1732: /* if V2+V1+V1*V4+age*V3+V3*V2 TProd[k1=2]=5 (V3*V2) */
1733: /* Tposprod[k]=k1 , Tposprod[3]=1, Tposprod[5(V3*V2)]=2 (2nd product without age) */
1734: int cptcovprod, *Tvaraff, *invalidvarcomb;
1735: double *lsurv, *lpop, *tpop;
1736:
1737: #define FD 1; /* Fixed dummy covariate */
1738: #define FQ 2; /* Fixed quantitative covariate */
1739: #define FP 3; /* Fixed product covariate */
1740: #define FPDD 7; /* Fixed product dummy*dummy covariate */
1741: #define FPDQ 8; /* Fixed product dummy*quantitative covariate */
1742: #define FPQQ 9; /* Fixed product quantitative*quantitative covariate */
1743: #define VD 10; /* Varying dummy covariate */
1744: #define VQ 11; /* Varying quantitative covariate */
1745: #define VP 12; /* Varying product covariate */
1746: #define VPDD 13; /* Varying product dummy*dummy covariate */
1747: #define VPDQ 14; /* Varying product dummy*quantitative covariate */
1748: #define VPQQ 15; /* Varying product quantitative*quantitative covariate */
1749: #define APFD 16; /* Age product * fixed dummy covariate */
1750: #define APFQ 17; /* Age product * fixed quantitative covariate */
1751: #define APVD 18; /* Age product * varying dummy covariate */
1752: #define APVQ 19; /* Age product * varying quantitative covariate */
1753:
1754: #define FTYPE 1; /* Fixed covariate */
1755: #define VTYPE 2; /* Varying covariate (loop in wave) */
1756: #define ATYPE 2; /* Age product covariate (loop in dh within wave)*/
1757:
1758: struct kmodel{
1759: int maintype; /* main type */
1760: int subtype; /* subtype */
1761: };
1762: struct kmodel modell[NCOVMAX];
1763:
1764: double ftol=FTOL; /**< Tolerance for computing Max Likelihood */
1765: double ftolhess; /**< Tolerance for computing hessian */
1766:
1767: /**************** split *************************/
1768: static int split( char *path, char *dirc, char *name, char *ext, char *finame )
1769: {
1770: /* From a file name with (full) path (either Unix or Windows) we extract the directory (dirc)
1771: the name of the file (name), its extension only (ext) and its first part of the name (finame)
1772: */
1773: char *ss; /* pointer */
1774: int l1=0, l2=0; /* length counters */
1775:
1776: l1 = strlen(path ); /* length of path */
1777: if ( l1 == 0 ) return( GLOCK_ERROR_NOPATH );
1778: ss= strrchr( path, DIRSEPARATOR ); /* find last / */
1779: if ( ss == NULL ) { /* no directory, so determine current directory */
1780: strcpy( name, path ); /* we got the fullname name because no directory */
1781: /*if(strrchr(path, ODIRSEPARATOR )==NULL)
1782: printf("Warning you should use %s as a separator\n",DIRSEPARATOR);*/
1783: /* get current working directory */
1784: /* extern char* getcwd ( char *buf , int len);*/
1785: #ifdef WIN32
1786: if (_getcwd( dirc, FILENAME_MAX ) == NULL ) {
1787: #else
1788: if (getcwd(dirc, FILENAME_MAX) == NULL) {
1789: #endif
1790: return( GLOCK_ERROR_GETCWD );
1791: }
1792: /* got dirc from getcwd*/
1793: printf(" DIRC = %s \n",dirc);
1794: } else { /* strip directory from path */
1795: ss++; /* after this, the filename */
1796: l2 = strlen( ss ); /* length of filename */
1797: if ( l2 == 0 ) return( GLOCK_ERROR_NOPATH );
1798: strcpy( name, ss ); /* save file name */
1799: strncpy( dirc, path, l1 - l2 ); /* now the directory */
1800: dirc[l1-l2] = '\0'; /* add zero */
1801: printf(" DIRC2 = %s \n",dirc);
1802: }
1803: /* We add a separator at the end of dirc if not exists */
1804: l1 = strlen( dirc ); /* length of directory */
1805: if( dirc[l1-1] != DIRSEPARATOR ){
1806: dirc[l1] = DIRSEPARATOR;
1807: dirc[l1+1] = 0;
1808: printf(" DIRC3 = %s \n",dirc);
1809: }
1810: ss = strrchr( name, '.' ); /* find last / */
1811: if (ss >0){
1812: ss++;
1813: strcpy(ext,ss); /* save extension */
1814: l1= strlen( name);
1815: l2= strlen(ss)+1;
1816: strncpy( finame, name, l1-l2);
1817: finame[l1-l2]= 0;
1818: }
1819:
1820: return( 0 ); /* we're done */
1821: }
1822:
1823:
1824: /******************************************/
1825:
1826: void replace_back_to_slash(char *s, char*t)
1827: {
1828: int i;
1829: int lg=0;
1830: i=0;
1831: lg=strlen(t);
1832: for(i=0; i<= lg; i++) {
1833: (s[i] = t[i]);
1834: if (t[i]== '\\') s[i]='/';
1835: }
1836: }
1837:
1838: char *trimbb(char *out, char *in)
1839: { /* Trim multiple blanks in line but keeps first blanks if line starts with blanks */
1840: char *s;
1841: s=out;
1842: while (*in != '\0'){
1843: while( *in == ' ' && *(in+1) == ' '){ /* && *(in+1) != '\0'){*/
1844: in++;
1845: }
1846: *out++ = *in++;
1847: }
1848: *out='\0';
1849: return s;
1850: }
1851:
1852: char *trimbtab(char *out, char *in)
1853: { /* Trim blanks or tabs in line but keeps first blanks if line starts with blanks */
1854: char *s;
1855: s=out;
1856: while (*in != '\0'){
1857: while( (*in == ' ' || *in == '\t')){ /* && *(in+1) != '\0'){*/
1858: in++;
1859: }
1860: *out++ = *in++;
1861: }
1862: *out='\0';
1863: return s;
1864: }
1865:
1866: /* char *substrchaine(char *out, char *in, char *chain) */
1867: /* { */
1868: /* /\* Substract chain 'chain' from 'in', return and output 'out' *\/ */
1869: /* char *s, *t; */
1870: /* t=in;s=out; */
1871: /* while ((*in != *chain) && (*in != '\0')){ */
1872: /* *out++ = *in++; */
1873: /* } */
1874:
1875: /* /\* *in matches *chain *\/ */
1876: /* while ((*in++ == *chain++) && (*in != '\0')){ */
1877: /* printf("*in = %c, *out= %c *chain= %c \n", *in, *out, *chain); */
1878: /* } */
1879: /* in--; chain--; */
1880: /* while ( (*in != '\0')){ */
1881: /* printf("Bef *in = %c, *out= %c *chain= %c \n", *in, *out, *chain); */
1882: /* *out++ = *in++; */
1883: /* printf("Aft *in = %c, *out= %c *chain= %c \n", *in, *out, *chain); */
1884: /* } */
1885: /* *out='\0'; */
1886: /* out=s; */
1887: /* return out; */
1888: /* } */
1889: char *substrchaine(char *out, char *in, char *chain)
1890: {
1891: /* Substract chain 'chain' from 'in', return and output 'out' */
1892: /* in="V1+V1*age+age*age+V2", chain="+age*age" out="V1+V1*age+V2" */
1893:
1894: char *strloc;
1895:
1896: strcpy (out, in); /* out="V1+V1*age+age*age+V2" */
1897: strloc = strstr(out, chain); /* strloc points to out at "+age*age+V2" */
1898: printf("Bef strloc=%s chain=%s out=%s \n", strloc, chain, out); /* strloc=+age*age+V2 chain="+age*age", out="V1+V1*age+age*age+V2" */
1899: if(strloc != NULL){
1900: /* will affect out */ /* strloc+strlen(chain)=|+V2 = "V1+V1*age+age*age|+V2" */ /* Will also work in Unicodek */
1901: memmove(strloc,strloc+strlen(chain), strlen(strloc+strlen(chain))+1); /* move number of bytes corresponding to the length of "+V2" which is 3, plus one is 4 (including the null)*/
1902: /* equivalent to strcpy (strloc, strloc +strlen(chain)) if no overlap; Copies from "+V2" to V1+V1*age+ */
1903: }
1904: printf("Aft strloc=%s chain=%s in=%s out=%s \n", strloc, chain, in, out); /* strloc=+V2 chain="+age*age", in="V1+V1*age+age*age+V2", out="V1+V1*age+V2" */
1905: return out;
1906: }
1907:
1908:
1909: char *cutl(char *blocc, char *alocc, char *in, char occ)
1910: {
1911: /* cuts string in into blocc and alocc where blocc ends before FIRST occurence of char 'occ'
1912: and alocc starts after first occurence of char 'occ' : ex cutl(blocc,alocc,"abcdef2ghi2j",'2')
1913: gives alocc="abcdef" and blocc="ghi2j".
1914: If occ is not found blocc is null and alocc is equal to in. Returns blocc
1915: */
1916: char *s, *t;
1917: t=in;s=in;
1918: while ((*in != occ) && (*in != '\0')){
1919: *alocc++ = *in++;
1920: }
1921: if( *in == occ){
1922: *(alocc)='\0';
1923: s=++in;
1924: }
1925:
1926: if (s == t) {/* occ not found */
1927: *(alocc-(in-s))='\0';
1928: in=s;
1929: }
1930: while ( *in != '\0'){
1931: *blocc++ = *in++;
1932: }
1933:
1934: *blocc='\0';
1935: return t;
1936: }
1937: char *cutv(char *blocc, char *alocc, char *in, char occ)
1938: {
1939: /* cuts string in into blocc and alocc where blocc ends before LAST occurence of char 'occ'
1940: and alocc starts after last occurence of char 'occ' : ex cutv(blocc,alocc,"abcdef2ghi2j",'2')
1941: gives blocc="abcdef2ghi" and alocc="j".
1942: If occ is not found blocc is null and alocc is equal to in. Returns alocc
1943: */
1944: char *s, *t;
1945: t=in;s=in;
1946: while (*in != '\0'){
1947: while( *in == occ){
1948: *blocc++ = *in++;
1949: s=in;
1950: }
1951: *blocc++ = *in++;
1952: }
1953: if (s == t) /* occ not found */
1954: *(blocc-(in-s))='\0';
1955: else
1956: *(blocc-(in-s)-1)='\0';
1957: in=s;
1958: while ( *in != '\0'){
1959: *alocc++ = *in++;
1960: }
1961:
1962: *alocc='\0';
1963: return s;
1964: }
1965:
1966: int nbocc(char *s, char occ)
1967: {
1968: int i,j=0;
1969: int lg=20;
1970: i=0;
1971: lg=strlen(s);
1972: for(i=0; i<= lg; i++) {
1973: if (s[i] == occ ) j++;
1974: }
1975: return j;
1976: }
1977:
1978: int nboccstr(char *textin, char *chain)
1979: {
1980: /* Counts the number of occurence of "chain" in string textin */
1981: /* in="+V7*V4+age*V2+age*V3+age*V4" chain="age" */
1982: char *strloc;
1983:
1984: int i,j=0;
1985:
1986: i=0;
1987:
1988: strloc=textin; /* strloc points to "^+V7*V4+age+..." in textin */
1989: for(;;) {
1990: strloc= strstr(strloc,chain); /* strloc points to first character of chain in textin if found. Example strloc points^ to "+V7*V4+^age" in textin */
1991: if(strloc != NULL){
1992: strloc = strloc+strlen(chain); /* strloc points to "+V7*V4+age^" in textin */
1993: j++;
1994: }else
1995: break;
1996: }
1997: return j;
1998:
1999: }
2000: /* void cutv(char *u,char *v, char*t, char occ) */
2001: /* { */
2002: /* /\* cuts string t into u and v where u ends before last occurence of char 'occ' */
2003: /* and v starts after last occurence of char 'occ' : ex cutv(u,v,"abcdef2ghi2j",'2') */
2004: /* gives u="abcdef2ghi" and v="j" *\/ */
2005: /* int i,lg,j,p=0; */
2006: /* i=0; */
2007: /* lg=strlen(t); */
2008: /* for(j=0; j<=lg-1; j++) { */
2009: /* if((t[j]!= occ) && (t[j+1]== occ)) p=j+1; */
2010: /* } */
2011:
2012: /* for(j=0; j<p; j++) { */
2013: /* (u[j] = t[j]); */
2014: /* } */
2015: /* u[p]='\0'; */
2016:
2017: /* for(j=0; j<= lg; j++) { */
2018: /* if (j>=(p+1))(v[j-p-1] = t[j]); */
2019: /* } */
2020: /* } */
2021:
2022: #ifdef _WIN32
2023: char * strsep(char **pp, const char *delim)
2024: {
2025: char *p, *q;
2026:
2027: if ((p = *pp) == NULL)
2028: return 0;
2029: if ((q = strpbrk (p, delim)) != NULL)
2030: {
2031: *pp = q + 1;
2032: *q = '\0';
2033: }
2034: else
2035: *pp = 0;
2036: return p;
2037: }
2038: #endif
2039:
2040: /********************** nrerror ********************/
2041:
2042: void nrerror(char error_text[])
2043: {
2044: fprintf(stderr,"ERREUR ...\n");
2045: fprintf(stderr,"%s\n",error_text);
2046: exit(EXIT_FAILURE);
2047: }
2048: /*********************** vector *******************/
2049: double *vector(int nl, int nh)
2050: {
2051: double *v;
2052: v=(double *) malloc((size_t)((nh-nl+1+NR_END)*sizeof(double)));
2053: if (!v) nrerror("allocation failure in vector");
2054: return v-nl+NR_END;
2055: }
2056:
2057: /************************ free vector ******************/
2058: void free_vector(double*v, int nl, int nh)
2059: {
2060: free((FREE_ARG)(v+nl-NR_END));
2061: }
2062:
2063: /************************ivector *******************************/
2064: int *ivector(long nl,long nh)
2065: {
2066: int *v;
2067: v=(int *) malloc((size_t)((nh-nl+1+NR_END)*sizeof(int)));
2068: if (!v) nrerror("allocation failure in ivector");
2069: return v-nl+NR_END;
2070: }
2071:
2072: /******************free ivector **************************/
2073: void free_ivector(int *v, long nl, long nh)
2074: {
2075: free((FREE_ARG)(v+nl-NR_END));
2076: }
2077:
2078: /************************lvector *******************************/
2079: long *lvector(long nl,long nh)
2080: {
2081: long *v;
2082: v=(long *) malloc((size_t)((nh-nl+1+NR_END)*sizeof(long)));
2083: if (!v) nrerror("allocation failure in ivector");
2084: return v-nl+NR_END;
2085: }
2086:
2087: /******************free lvector **************************/
2088: void free_lvector(long *v, long nl, long nh)
2089: {
2090: free((FREE_ARG)(v+nl-NR_END));
2091: }
2092:
2093: /******************* imatrix *******************************/
2094: int **imatrix(long nrl, long nrh, long ncl, long nch)
2095: /* allocate a int matrix with subscript range m[nrl..nrh][ncl..nch] */
2096: {
2097: long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
2098: int **m;
2099:
2100: /* allocate pointers to rows */
2101: m=(int **) malloc((size_t)((nrow+NR_END)*sizeof(int*)));
2102: if (!m) nrerror("allocation failure 1 in matrix()");
2103: m += NR_END;
2104: m -= nrl;
2105:
2106:
2107: /* allocate rows and set pointers to them */
2108: m[nrl]=(int *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(int)));
2109: if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
2110: m[nrl] += NR_END;
2111: m[nrl] -= ncl;
2112:
2113: for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
2114:
2115: /* return pointer to array of pointers to rows */
2116: return m;
2117: }
2118:
2119: /****************** free_imatrix *************************/
2120: void free_imatrix(m,nrl,nrh,ncl,nch)
2121: int **m;
2122: long nch,ncl,nrh,nrl;
2123: /* free an int matrix allocated by imatrix() */
2124: {
2125: free((FREE_ARG) (m[nrl]+ncl-NR_END));
2126: free((FREE_ARG) (m+nrl-NR_END));
2127: }
2128:
2129: /******************* matrix *******************************/
2130: double **matrix(long nrl, long nrh, long ncl, long nch)
2131: {
2132: long i, nrow=nrh-nrl+1, ncol=nch-ncl+1;
2133: double **m;
2134:
2135: m=(double **) malloc((size_t)((nrow+NR_END)*sizeof(double*)));
2136: if (!m) nrerror("allocation failure 1 in matrix()");
2137: m += NR_END;
2138: m -= nrl;
2139:
2140: m[nrl]=(double *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(double)));
2141: if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
2142: m[nrl] += NR_END;
2143: m[nrl] -= ncl;
2144:
2145: for (i=nrl+1; i<=nrh; i++) m[i]=m[i-1]+ncol;
2146: return m;
2147: /* print *(*(m+1)+70) or print m[1][70]; print m+1 or print &(m[1]) or &(m[1][0])
2148: m[i] = address of ith row of the table. &(m[i]) is its value which is another adress
2149: that of m[i][0]. In order to get the value p m[i][0] but it is unitialized.
2150: */
2151: }
2152:
2153: /*************************free matrix ************************/
2154: void free_matrix(double **m, long nrl, long nrh, long ncl, long nch)
2155: {
2156: free((FREE_ARG)(m[nrl]+ncl-NR_END));
2157: free((FREE_ARG)(m+nrl-NR_END));
2158: }
2159:
2160: /******************* ma3x *******************************/
2161: double ***ma3x(long nrl, long nrh, long ncl, long nch, long nll, long nlh)
2162: {
2163: long i, j, nrow=nrh-nrl+1, ncol=nch-ncl+1, nlay=nlh-nll+1;
2164: double ***m;
2165:
2166: m=(double ***) malloc((size_t)((nrow+NR_END)*sizeof(double*)));
2167: if (!m) nrerror("allocation failure 1 in matrix()");
2168: m += NR_END;
2169: m -= nrl;
2170:
2171: m[nrl]=(double **) malloc((size_t)((nrow*ncol+NR_END)*sizeof(double)));
2172: if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
2173: m[nrl] += NR_END;
2174: m[nrl] -= ncl;
2175:
2176: for (i=nrl+1; i<=nrh; i++) m[i]=m[i-1]+ncol;
2177:
2178: m[nrl][ncl]=(double *) malloc((size_t)((nrow*ncol*nlay+NR_END)*sizeof(double)));
2179: if (!m[nrl][ncl]) nrerror("allocation failure 3 in matrix()");
2180: m[nrl][ncl] += NR_END;
2181: m[nrl][ncl] -= nll;
2182: for (j=ncl+1; j<=nch; j++)
2183: m[nrl][j]=m[nrl][j-1]+nlay;
2184:
2185: for (i=nrl+1; i<=nrh; i++) {
2186: m[i][ncl]=m[i-1l][ncl]+ncol*nlay;
2187: for (j=ncl+1; j<=nch; j++)
2188: m[i][j]=m[i][j-1]+nlay;
2189: }
2190: return m;
2191: /* gdb: p *(m+1) <=> p m[1] and p (m+1) <=> p (m+1) <=> p &(m[1])
2192: &(m[i][j][k]) <=> *((*(m+i) + j)+k)
2193: */
2194: }
2195:
2196: /*************************free ma3x ************************/
2197: void free_ma3x(double ***m, long nrl, long nrh, long ncl, long nch,long nll, long nlh)
2198: {
2199: free((FREE_ARG)(m[nrl][ncl]+ nll-NR_END));
2200: free((FREE_ARG)(m[nrl]+ncl-NR_END));
2201: free((FREE_ARG)(m+nrl-NR_END));
2202: }
2203:
2204: /*************** function subdirf ***********/
2205: char *subdirf(char fileres[])
2206: {
2207: /* Caution optionfilefiname is hidden */
2208: strcpy(tmpout,optionfilefiname);
2209: strcat(tmpout,"/"); /* Add to the right */
2210: strcat(tmpout,fileres);
2211: return tmpout;
2212: }
2213:
2214: /*************** function subdirf2 ***********/
2215: char *subdirf2(char fileres[], char *preop)
2216: {
2217: /* Example subdirf2(optionfilefiname,"FB_") with optionfilefiname="texte", result="texte/FB_texte"
2218: Errors in subdirf, 2, 3 while printing tmpout is
2219: rewritten within the same printf. Workaround: many printfs */
2220: /* Caution optionfilefiname is hidden */
2221: strcpy(tmpout,optionfilefiname);
2222: strcat(tmpout,"/");
2223: strcat(tmpout,preop);
2224: strcat(tmpout,fileres);
2225: return tmpout;
2226: }
2227:
2228: /*************** function subdirf3 ***********/
2229: char *subdirf3(char fileres[], char *preop, char *preop2)
2230: {
2231:
2232: /* Caution optionfilefiname is hidden */
2233: strcpy(tmpout,optionfilefiname);
2234: strcat(tmpout,"/");
2235: strcat(tmpout,preop);
2236: strcat(tmpout,preop2);
2237: strcat(tmpout,fileres);
2238: return tmpout;
2239: }
2240:
2241: /*************** function subdirfext ***********/
2242: char *subdirfext(char fileres[], char *preop, char *postop)
2243: {
2244:
2245: strcpy(tmpout,preop);
2246: strcat(tmpout,fileres);
2247: strcat(tmpout,postop);
2248: return tmpout;
2249: }
2250:
2251: /*************** function subdirfext3 ***********/
2252: char *subdirfext3(char fileres[], char *preop, char *postop)
2253: {
2254:
2255: /* Caution optionfilefiname is hidden */
2256: strcpy(tmpout,optionfilefiname);
2257: strcat(tmpout,"/");
2258: strcat(tmpout,preop);
2259: strcat(tmpout,fileres);
2260: strcat(tmpout,postop);
2261: return tmpout;
2262: }
2263:
2264: char *asc_diff_time(long time_sec, char ascdiff[])
2265: {
2266: long sec_left, days, hours, minutes;
2267: days = (time_sec) / (60*60*24);
2268: sec_left = (time_sec) % (60*60*24);
2269: hours = (sec_left) / (60*60) ;
2270: sec_left = (sec_left) %(60*60);
2271: minutes = (sec_left) /60;
2272: sec_left = (sec_left) % (60);
2273: sprintf(ascdiff,"%ld day(s) %ld hour(s) %ld minute(s) %ld second(s)",days, hours, minutes, sec_left);
2274: return ascdiff;
2275: }
2276:
2277: /***************** f1dim *************************/
2278: extern int ncom;
2279: extern double *pcom,*xicom;
2280: extern double (*nrfunc)(double []);
2281:
2282: double f1dim(double x)
2283: {
2284: int j;
2285: double f;
2286: double *xt;
2287:
2288: xt=vector(1,ncom);
2289: for (j=1;j<=ncom;j++) xt[j]=pcom[j]+x*xicom[j];
2290: f=(*nrfunc)(xt);
2291: free_vector(xt,1,ncom);
2292: return f;
2293: }
2294:
2295: /*****************brent *************************/
2296: double brent(double ax, double bx, double cx, double (*f)(double), double tol, double *xmin)
2297: {
2298: /* Given a function f, and given a bracketing triplet of abscissas ax, bx, cx (such that bx is
2299: * between ax and cx, and f(bx) is less than both f(ax) and f(cx) ), this routine isolates
2300: * the minimum to a fractional precision of about tol using Brent’s method. The abscissa of
2301: * the minimum is returned as xmin, and the minimum function value is returned as brent , the
2302: * returned function value.
2303: */
2304: int iter;
2305: double a,b,d,etemp;
2306: double fu=0,fv,fw,fx;
2307: double ftemp=0.;
2308: double p,q,r,tol1,tol2,u,v,w,x,xm;
2309: double e=0.0;
2310:
2311: a=(ax < cx ? ax : cx);
2312: b=(ax > cx ? ax : cx);
2313: x=w=v=bx;
2314: fw=fv=fx=(*f)(x);
2315: for (iter=1;iter<=ITMAX;iter++) {
2316: xm=0.5*(a+b);
2317: tol2=2.0*(tol1=tol*fabs(x)+ZEPS);
2318: /* if (2.0*fabs(fp-(*fret)) <= ftol*(fabs(fp)+fabs(*fret)))*/
2319: printf(".");fflush(stdout);
2320: fprintf(ficlog,".");fflush(ficlog);
2321: #ifdef DEBUGBRENT
2322: 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);
2323: 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);
2324: /* if ((fabs(x-xm) <= (tol2-0.5*(b-a)))||(2.0*fabs(fu-ftemp) <= ftol*1.e-2*(fabs(fu)+fabs(ftemp)))) { */
2325: #endif
2326: if (fabs(x-xm) <= (tol2-0.5*(b-a))){
2327: *xmin=x;
2328: return fx;
2329: }
2330: ftemp=fu;
2331: if (fabs(e) > tol1) {
2332: r=(x-w)*(fx-fv);
2333: q=(x-v)*(fx-fw);
2334: p=(x-v)*q-(x-w)*r;
2335: q=2.0*(q-r);
2336: if (q > 0.0) p = -p;
2337: q=fabs(q);
2338: etemp=e;
2339: e=d;
2340: if (fabs(p) >= fabs(0.5*q*etemp) || p <= q*(a-x) || p >= q*(b-x))
2341: d=CGOLD*(e=(x >= xm ? a-x : b-x));
2342: else {
2343: d=p/q;
2344: u=x+d;
2345: if (u-a < tol2 || b-u < tol2)
2346: d=SIGN(tol1,xm-x);
2347: }
2348: } else {
2349: d=CGOLD*(e=(x >= xm ? a-x : b-x));
2350: }
2351: u=(fabs(d) >= tol1 ? x+d : x+SIGN(tol1,d));
2352: fu=(*f)(u);
2353: if (fu <= fx) {
2354: if (u >= x) a=x; else b=x;
2355: SHFT(v,w,x,u)
2356: SHFT(fv,fw,fx,fu)
2357: } else {
2358: if (u < x) a=u; else b=u;
2359: if (fu <= fw || w == x) {
2360: v=w;
2361: w=u;
2362: fv=fw;
2363: fw=fu;
2364: } else if (fu <= fv || v == x || v == w) {
2365: v=u;
2366: fv=fu;
2367: }
2368: }
2369: }
2370: nrerror("Too many iterations in brent");
2371: *xmin=x;
2372: return fx;
2373: }
2374:
2375: /****************** mnbrak ***********************/
2376:
2377: void mnbrak(double *ax, double *bx, double *cx, double *fa, double *fb, double *fc,
2378: double (*func)(double))
2379: { /* Given a function func , and given distinct initial points ax and bx , this routine searches in
2380: the downhill direction (defined by the function as evaluated at the initial points) and returns
2381: new points ax , bx , cx that bracket a minimum of the function. Also returned are the function
2382: values at the three points, fa, fb , and fc such that fa > fb and fb < fc.
2383: */
2384: double ulim,u,r,q, dum;
2385: double fu;
2386:
2387: double scale=10.;
2388: int iterscale=0;
2389:
2390: *fa=(*func)(*ax); /* xta[j]=pcom[j]+(*ax)*xicom[j]; fa=f(xta[j])*/
2391: *fb=(*func)(*bx); /* xtb[j]=pcom[j]+(*bx)*xicom[j]; fb=f(xtb[j]) */
2392:
2393:
2394: /* while(*fb != *fb){ /\* *ax should be ok, reducing distance to *ax *\/ */
2395: /* printf("Warning mnbrak *fb = %lf, *bx=%lf *ax=%lf *fa==%lf iter=%d\n",*fb, *bx, *ax, *fa, iterscale++); */
2396: /* *bx = *ax - (*ax - *bx)/scale; */
2397: /* *fb=(*func)(*bx); /\* xtb[j]=pcom[j]+(*bx)*xicom[j]; fb=f(xtb[j]) *\/ */
2398: /* } */
2399:
2400: if (*fb > *fa) {
2401: SHFT(dum,*ax,*bx,dum)
2402: SHFT(dum,*fb,*fa,dum)
2403: }
2404: *cx=(*bx)+GOLD*(*bx-*ax);
2405: *fc=(*func)(*cx);
2406: #ifdef DEBUG
2407: printf("mnbrak0 a=%lf *fa=%lf, b=%lf *fb=%lf, c=%lf *fc=%lf\n",*ax,*fa,*bx,*fb,*cx, *fc);
2408: fprintf(ficlog,"mnbrak0 a=%lf *fa=%lf, b=%lf *fb=%lf, c=%lf *fc=%lf\n",*ax,*fa,*bx,*fb,*cx, *fc);
2409: #endif
2410: while (*fb > *fc) { /* Declining a,b,c with fa> fb > fc. If fc=inf it exits and if flat fb=fc it exits too.*/
2411: r=(*bx-*ax)*(*fb-*fc);
2412: q=(*bx-*cx)*(*fb-*fa); /* What if fa=inf */
2413: u=(*bx)-((*bx-*cx)*q-(*bx-*ax)*r)/
2414: (2.0*SIGN(FMAX(fabs(q-r),TINY),q-r)); /* Minimum abscissa of a parabolic estimated from (a,fa), (b,fb) and (c,fc). */
2415: ulim=(*bx)+GLIMIT*(*cx-*bx); /* Maximum abscissa where function should be evaluated */
2416: if ((*bx-u)*(u-*cx) > 0.0) { /* if u_p is between b and c */
2417: fu=(*func)(u);
2418: #ifdef DEBUG
2419: /* f(x)=A(x-u)**2+f(u) */
2420: double A, fparabu;
2421: A= (*fb - *fa)/(*bx-*ax)/(*bx+*ax-2*u);
2422: fparabu= *fa - A*(*ax-u)*(*ax-u);
2423: 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);
2424: 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);
2425: /* And thus,it can be that fu > *fc even if fparabu < *fc */
2426: /* mnbrak (*ax=7.666299858533, *fa=299039.693133272231), (*bx=8.595447774979, *fb=298976.598289369489),
2427: (*cx=10.098840694817, *fc=298946.631474258087), (*u=9.852501168332, fu=298948.773013752128, fparabu=298945.434711494134) */
2428: /* In that case, there is no bracket in the output! Routine is wrong with many consequences.*/
2429: #endif
2430: #ifdef MNBRAKORIGINAL
2431: #else
2432: /* if (fu > *fc) { */
2433: /* #ifdef DEBUG */
2434: /* printf("mnbrak4 fu > fc \n"); */
2435: /* fprintf(ficlog, "mnbrak4 fu > fc\n"); */
2436: /* #endif */
2437: /* /\* 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 *\\/ *\/ */
2438: /* /\* SHFT(*fa,*fc,fu,*fc) /\\* (b, u, c) is a bracket while test fb > fc will be fu > fc will exit *\\/ *\/ */
2439: /* dum=u; /\* Shifting c and u *\/ */
2440: /* u = *cx; */
2441: /* *cx = dum; */
2442: /* dum = fu; */
2443: /* fu = *fc; */
2444: /* *fc =dum; */
2445: /* } else { /\* end *\/ */
2446: /* #ifdef DEBUG */
2447: /* printf("mnbrak3 fu < fc \n"); */
2448: /* fprintf(ficlog, "mnbrak3 fu < fc\n"); */
2449: /* #endif */
2450: /* dum=u; /\* Shifting c and u *\/ */
2451: /* u = *cx; */
2452: /* *cx = dum; */
2453: /* dum = fu; */
2454: /* fu = *fc; */
2455: /* *fc =dum; */
2456: /* } */
2457: #ifdef DEBUGMNBRAK
2458: double A, fparabu;
2459: A= (*fb - *fa)/(*bx-*ax)/(*bx+*ax-2*u);
2460: fparabu= *fa - A*(*ax-u)*(*ax-u);
2461: 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);
2462: 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);
2463: #endif
2464: dum=u; /* Shifting c and u */
2465: u = *cx;
2466: *cx = dum;
2467: dum = fu;
2468: fu = *fc;
2469: *fc =dum;
2470: #endif
2471: } else if ((*cx-u)*(u-ulim) > 0.0) { /* u is after c but before ulim */
2472: #ifdef DEBUG
2473: printf("\nmnbrak2 u=%lf after c=%lf but before ulim\n",u,*cx);
2474: fprintf(ficlog,"\nmnbrak2 u=%lf after c=%lf but before ulim\n",u,*cx);
2475: #endif
2476: fu=(*func)(u);
2477: if (fu < *fc) {
2478: #ifdef DEBUG
2479: printf("\nmnbrak2 u=%lf after c=%lf but before ulim=%lf AND fu=%lf < %lf=fc\n",u,*cx,ulim,fu, *fc);
2480: fprintf(ficlog,"\nmnbrak2 u=%lf after c=%lf but before ulim=%lf AND fu=%lf < %lf=fc\n",u,*cx,ulim,fu, *fc);
2481: #endif
2482: SHFT(*bx,*cx,u,*cx+GOLD*(*cx-*bx))
2483: SHFT(*fb,*fc,fu,(*func)(u))
2484: #ifdef DEBUG
2485: printf("\nmnbrak2 shift GOLD c=%lf",*cx+GOLD*(*cx-*bx));
2486: #endif
2487: }
2488: } else if ((u-ulim)*(ulim-*cx) >= 0.0) { /* u outside ulim (verifying that ulim is beyond c) */
2489: #ifdef DEBUG
2490: printf("\nmnbrak2 u=%lf outside ulim=%lf (verifying that ulim is beyond c=%lf)\n",u,ulim,*cx);
2491: fprintf(ficlog,"\nmnbrak2 u=%lf outside ulim=%lf (verifying that ulim is beyond c=%lf)\n",u,ulim,*cx);
2492: #endif
2493: u=ulim;
2494: fu=(*func)(u);
2495: } else { /* u could be left to b (if r > q parabola has a maximum) */
2496: #ifdef DEBUG
2497: printf("\nmnbrak2 u=%lf could be left to b=%lf (if r=%lf > q=%lf parabola has a maximum)\n",u,*bx,r,q);
2498: 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);
2499: #endif
2500: u=(*cx)+GOLD*(*cx-*bx);
2501: fu=(*func)(u);
2502: #ifdef DEBUG
2503: printf("\nmnbrak2 new u=%lf fu=%lf shifted gold left from c=%lf and b=%lf \n",u,fu,*cx,*bx);
2504: fprintf(ficlog,"\nmnbrak2 new u=%lf fu=%lf shifted gold left from c=%lf and b=%lf \n",u,fu,*cx,*bx);
2505: #endif
2506: } /* end tests */
2507: SHFT(*ax,*bx,*cx,u)
2508: SHFT(*fa,*fb,*fc,fu)
2509: #ifdef DEBUG
2510: printf("\nmnbrak2 shift (*ax=%.12f, *fa=%.12lf), (*bx=%.12f, *fb=%.12lf), (*cx=%.12f, *fc=%.12lf)\n",*ax,*fa,*bx,*fb,*cx,*fc);
2511: fprintf(ficlog, "\nmnbrak2 shift (*ax=%.12f, *fa=%.12lf), (*bx=%.12f, *fb=%.12lf), (*cx=%.12f, *fc=%.12lf)\n",*ax,*fa,*bx,*fb,*cx,*fc);
2512: #endif
2513: } /* end while; ie return (a, b, c, fa, fb, fc) such that a < b < c with f(a) > f(b) and fb < f(c) */
2514: }
2515:
2516: /*************** linmin ************************/
2517: /* Given an n -dimensional point p[1..n] and an n -dimensional direction xi[1..n] , moves and
2518: resets p to where the function func(p) takes on a minimum along the direction xi from p ,
2519: and replaces xi by the actual vector displacement that p was moved. Also returns as fret
2520: the value of func at the returned location p . This is actually all accomplished by calling the
2521: routines mnbrak and brent .*/
2522: int ncom;
2523: double *pcom,*xicom;
2524: double (*nrfunc)(double []);
2525:
2526: #ifdef LINMINORIGINAL
2527: void linmin(double p[], double xi[], int n, double *fret,double (*func)(double []))
2528: #else
2529: void linmin(double p[], double xi[], int n, double *fret,double (*func)(double []), int *flat)
2530: #endif
2531: {
2532: double brent(double ax, double bx, double cx,
2533: double (*f)(double), double tol, double *xmin);
2534: double f1dim(double x);
2535: void mnbrak(double *ax, double *bx, double *cx, double *fa, double *fb,
2536: double *fc, double (*func)(double));
2537: int j;
2538: double xx,xmin,bx,ax;
2539: double fx,fb,fa;
2540:
2541: #ifdef LINMINORIGINAL
2542: #else
2543: double scale=10., axs, xxs; /* Scale added for infinity */
2544: #endif
2545:
2546: ncom=n;
2547: pcom=vector(1,n);
2548: xicom=vector(1,n);
2549: nrfunc=func;
2550: for (j=1;j<=n;j++) {
2551: pcom[j]=p[j];
2552: xicom[j]=xi[j]; /* Former scale xi[j] of currrent direction i */
2553: }
2554:
2555: #ifdef LINMINORIGINAL
2556: xx=1.;
2557: #else
2558: axs=0.0;
2559: xxs=1.;
2560: do{
2561: xx= xxs;
2562: #endif
2563: ax=0.;
2564: mnbrak(&ax,&xx,&bx,&fa,&fx,&fb,f1dim); /* Outputs: xtx[j]=pcom[j]+(*xx)*xicom[j]; fx=f(xtx[j]) */
2565: /* brackets with inputs ax=0 and xx=1, but points, pcom=p, and directions values, xicom=xi, are sent via f1dim(x) */
2566: /* 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)) */
2567: /* Outputs: fa=f(p(j)) and fx=f(p(j) + xxs * xi(j) ) and f(bx)= f(p(j)+ bx* xi(j)) */
2568: /* Given input ax=axs and xx=xxs, xx might be too far from ax to get a finite f(xx) */
2569: /* Searches on line, outputs (ax, xx, bx) such that fx < min(fa and fb) */
2570: /* 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]]*/
2571: #ifdef LINMINORIGINAL
2572: #else
2573: if (fx != fx){
2574: xxs=xxs/scale; /* Trying a smaller xx, closer to initial ax=0 */
2575: printf("|");
2576: fprintf(ficlog,"|");
2577: #ifdef DEBUGLINMIN
2578: 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);
2579: #endif
2580: }
2581: }while(fx != fx && xxs > 1.e-5);
2582: #endif
2583:
2584: #ifdef DEBUGLINMIN
2585: 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);
2586: 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);
2587: #endif
2588: #ifdef LINMINORIGINAL
2589: #else
2590: if(fb == fx){ /* Flat function in the direction */
2591: xmin=xx;
2592: *flat=1;
2593: }else{
2594: *flat=0;
2595: #endif
2596: /*Flat mnbrak2 shift (*ax=0.000000000000, *fa=51626.272983130431), (*bx=-1.618034000000, *fb=51590.149499362531), (*cx=-4.236068025156, *fc=51590.149499362531) */
2597: *fret=brent(ax,xx,bx,f1dim,TOL,&xmin); /* Giving a bracketting triplet (ax, xx, bx), find a minimum, xmin, according to f1dim, *fret(xmin),*/
2598: /* fa = f(p[j] + ax * xi[j]), fx = f(p[j] + xx * xi[j]), fb = f(p[j] + bx * xi[j]) */
2599: /* fmin = f(p[j] + xmin * xi[j]) */
2600: /* P+lambda n in that direction (lambdamin), with TOL between abscisses */
2601: /* f1dim(xmin): for (j=1;j<=ncom;j++) xt[j]=pcom[j]+xmin*xicom[j]; */
2602: #ifdef DEBUG
2603: 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);
2604: 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);
2605: #endif
2606: #ifdef LINMINORIGINAL
2607: #else
2608: }
2609: #endif
2610: #ifdef DEBUGLINMIN
2611: printf("linmin end ");
2612: fprintf(ficlog,"linmin end ");
2613: #endif
2614: for (j=1;j<=n;j++) {
2615: #ifdef LINMINORIGINAL
2616: xi[j] *= xmin;
2617: #else
2618: #ifdef DEBUGLINMIN
2619: if(xxs <1.0)
2620: printf(" before xi[%d]=%12.8f", j,xi[j]);
2621: #endif
2622: 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) */
2623: #ifdef DEBUGLINMIN
2624: if(xxs <1.0)
2625: 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 );
2626: #endif
2627: #endif
2628: p[j] += xi[j]; /* Parameters values are updated accordingly */
2629: }
2630: #ifdef DEBUGLINMIN
2631: printf("\n");
2632: printf("Comparing last *frec(xmin=%12.8f)=%12.8f from Brent and frec(0.)=%12.8f \n", xmin, *fret, (*func)(p));
2633: fprintf(ficlog,"Comparing last *frec(xmin=%12.8f)=%12.8f from Brent and frec(0.)=%12.8f \n", xmin, *fret, (*func)(p));
2634: for (j=1;j<=n;j++) {
2635: printf(" xi[%d]= %14.10f p[%d]= %12.7f",j,xi[j],j,p[j]);
2636: fprintf(ficlog," xi[%d]= %14.10f p[%d]= %12.7f",j,xi[j],j,p[j]);
2637: if(j % ncovmodel == 0){
2638: printf("\n");
2639: fprintf(ficlog,"\n");
2640: }
2641: }
2642: #else
2643: #endif
2644: free_vector(xicom,1,n);
2645: free_vector(pcom,1,n);
2646: }
2647:
2648: /**** praxis gegen ****/
2649:
2650: /* This has been tested by Visual C from Microsoft and works */
2651: /* meaning tha valgrind could be wrong */
2652: /*********************************************************************/
2653: /* f u n c t i o n p r a x i s */
2654: /* */
2655: /* praxis is a general purpose routine for the minimization of a */
2656: /* function in several variables. the algorithm used is a modifi- */
2657: /* cation of conjugate gradient search method by powell. the changes */
2658: /* are due to r.p. brent, who gives an algol-w program, which served */
2659: /* as a basis for this function. */
2660: /* */
2661: /* references: */
2662: /* - powell, m.j.d., 1964. an efficient method for finding */
2663: /* the minimum of a function in several variables without */
2664: /* calculating derivatives, computer journal, 7, 155-162 */
2665: /* - brent, r.p., 1973. algorithms for minimization without */
2666: /* derivatives, prentice hall, englewood cliffs. */
2667: /* */
2668: /* problems, suggestions or improvements are always wellcome */
2669: /* karl gegenfurtner 07/08/87 */
2670: /* c - version */
2671: /*********************************************************************/
2672: /* */
2673: /* usage: min = praxis(tol, macheps, h, n, prin, x, func) */
2674: /* macheps has been suppressed because it is replaced by DBL_EPSILON */
2675: /* and if it was an argument of praxis (as it is in original brent) */
2676: /* it should be declared external */
2677: /* usage: min = praxis(tol, h, n, prin, x, func) */
2678: /* was min = praxis(fun, x, n); */
2679: /* */
2680: /* fun the function to be minimized. fun is called from */
2681: /* praxis with x and n as arguments */
2682: /* x a double array containing the initial guesses for */
2683: /* the minimum, which will contain the solution on */
2684: /* return */
2685: /* n an integer specifying the number of unknown */
2686: /* parameters */
2687: /* min praxis returns the least calculated value of fun */
2688: /* */
2689: /* some additional global variables control some more aspects of */
2690: /* the inner workings of praxis. setting them is optional, they */
2691: /* are all set to some reasonable default values given below. */
2692: /* */
2693: /* prin controls the printed output from the routine. */
2694: /* 0 -> no output */
2695: /* 1 -> print only starting and final values */
2696: /* 2 -> detailed map of the minimization process */
2697: /* 3 -> print also eigenvalues and vectors of the */
2698: /* search directions */
2699: /* the default value is 1 */
2700: /* tol is the tolerance allowed for the precision of the */
2701: /* solution. praxis returns if the criterion */
2702: /* 2 * ||x[k]-x[k-1]|| <= sqrt(macheps) * ||x[k]|| + tol */
2703: /* is fulfilled more than ktm times. */
2704: /* the default value depends on the machine precision */
2705: /* ktm see just above. default is 1, and a value of 4 leads */
2706: /* to a very(!) cautious stopping criterion. */
2707: /* h0 or step is a steplength parameter and should be set equal */
2708: /* to the expected distance from the solution. */
2709: /* exceptionally small or large values of step lead to */
2710: /* slower convergence on the first few iterations */
2711: /* the default value for step is 1.0 */
2712: /* scbd is a scaling parameter. 1.0 is the default and */
2713: /* indicates no scaling. if the scales for the different */
2714: /* parameters are very different, scbd should be set to */
2715: /* a value of about 10.0. */
2716: /* illc should be set to true (1) if the problem is known to */
2717: /* be ill-conditioned. the default is false (0). this */
2718: /* variable is automatically set, when praxis finds */
2719: /* the problem to be ill-conditioned during iterations. */
2720: /* maxfun is the maximum number of calls to fun allowed. praxis */
2721: /* will return after maxfun calls to fun even when the */
2722: /* minimum is not yet found. the default value of 0 */
2723: /* indicates no limit on the number of calls. */
2724: /* this return condition is only checked every n */
2725: /* iterations. */
2726: /* */
2727: /*********************************************************************/
2728:
2729: #include <math.h>
2730: #include <stdio.h>
2731: #include <stdlib.h>
2732: #include <float.h> /* for DBL_EPSILON */
2733: /* #include "machine.h" */
2734:
2735:
2736: /* extern void minfit(int n, double eps, double tol, double **ab, double q[]); */
2737: /* extern void minfit(int n, double eps, double tol, double ab[N][N], double q[]); */
2738: /* control parameters */
2739: /* control parameters */
2740: #define SQREPSILON 1.0e-19
2741: /* #define EPSILON 1.0e-8 */ /* in main */
2742:
2743: double tol = SQREPSILON,
2744: scbd = 1.0,
2745: step = 1.0;
2746: int ktm = 1,
2747: /* prin = 2, */
2748: maxfun = 0,
2749: illc = 0;
2750:
2751: /* some global variables */
2752: static int i, j, k, k2, nl, nf, kl, kt;
2753: /* static double s; */
2754: double sl, dn, dmin,
2755: fx, f1, lds, ldt, sf, df,
2756: qf1, qd0, qd1, qa, qb, qc,
2757: m2, m4, small_windows, vsmall, large,
2758: vlarge, ldfac, t2;
2759: /* static double d[N], y[N], z[N], */
2760: /* q0[N], q1[N], v[N][N]; */
2761:
2762: static double *d, *y, *z;
2763: static double *q0, *q1, **v;
2764: double *tflin; /* used in flin: return (*fun)(tflin, n); */
2765: double *e; /* used in minfit, don't konw how to free memory and thus made global */
2766: /* static double s, sl, dn, dmin, */
2767: /* fx, f1, lds, ldt, sf, df, */
2768: /* qf1, qd0, qd1, qa, qb, qc, */
2769: /* m2, m4, small, vsmall, large, */
2770: /* vlarge, ldfac, t2; */
2771: /* static double d[N], y[N], z[N], */
2772: /* q0[N], q1[N], v[N][N]; */
2773:
2774: /* these will be set by praxis to point to it's arguments */
2775: static int prin; /* added */
2776: static int n;
2777: static double *x;
2778: static double (*fun)();
2779: /* static double (*fun)(double *x, int n); */
2780:
2781: /* these will be set by praxis to the global control parameters */
2782: /* static double h, macheps, t; */
2783: extern double macheps;
2784: static double h;
2785: static double t;
2786:
2787: static double
2788: drandom() /* return random no between 0 and 1 */
2789: {
2790: return (double)(rand()%(8192*2))/(double)(8192*2);
2791: }
2792:
2793: static void sort() /* d and v in descending order */
2794: {
2795: int k, i, j;
2796: double s;
2797:
2798: for (i=1; i<=n-1; i++) {
2799: k = i; s = d[i];
2800: for (j=i+1; j<=n; j++) {
2801: if (d[j] > s) {
2802: k = j;
2803: s = d[j];
2804: }
2805: }
2806: if (k > i) {
2807: d[k] = d[i];
2808: d[i] = s;
2809: for (j=1; j<=n; j++) {
2810: s = v[j][i];
2811: v[j][i] = v[j][k];
2812: v[j][k] = s;
2813: }
2814: }
2815: }
2816: }
2817:
2818: double randbrent ( int *naught )
2819: {
2820: double ran1, ran3[127], half;
2821: int ran2, q, r, i, j;
2822: int init=0; /* false */
2823: double rr;
2824: /* REAL*8 RAN1,RAN3(127),HALF */
2825:
2826: /* INTEGER RAN2,Q,R */
2827: /* LOGICAL INIT */
2828: /* DATA INIT/.FALSE./ */
2829: /* IF (INIT) GO TO 3 */
2830: if(!init){
2831: /* R = MOD(NAUGHT,8190) + 1 *//* 1804289383 rand () */
2832: r = *naught % 8190 + 1;/* printf(" naught r %d %d",*naught,r); */
2833: ran2=127;
2834: for(i=ran2; i>0; i--){
2835: /* RAN2 = 128 */
2836: /* DO 2 I=1,127 */
2837: ran2 = ran2-1;
2838: /* RAN2 = RAN2 - 1 */
2839: ran1 = -pow(2.0,55);
2840: /* RAN1 = -2.D0**55 */
2841: /* DO 1 J=1,7 */
2842: for(j=1; j<=7;j++){
2843: /* R = MOD(1756*R,8191) */
2844: r = (1756*r) % 8191;/* printf(" i=%d (1756*r)%8191=%d",j,r); */
2845: q=r/32;
2846: /* Q = R/32 */
2847: /* 1 RAN1 = (RAN1 + Q)*(1.0D0/256) */
2848: ran1 =(ran1+q)*(1.0/256);
2849: }
2850: /* 2 RAN3(RAN2) = RAN1 */
2851: ran3[ran2] = ran1; /* printf(" ran2=%d ran1=%.7g \n",ran2,ran1); */
2852: }
2853: /* INIT = .TRUE. */
2854: init=1;
2855: /* 3 IF (RAN2.EQ.1) RAN2 = 128 */
2856: }
2857: if(ran2 == 0) ran2 = 126;
2858: else ran2 = ran2 -1;
2859: /* RAN2 = RAN2 - 1 */
2860: /* RAN1 = RAN1 + RAN3(RAN2) */
2861: ran1 = ran1 + ran3[ran2];/* printf("BIS ran2=%d ran1=%.7g \n",ran2,ran1); */
2862: half= 0.5;
2863: /* HALF = .5D0 */
2864: /* IF (RAN1.GE.0.D0) HALF = -HALF */
2865: if(ran1 >= 0.) half =-half;
2866: ran1 = ran1 +half;
2867: ran3[ran2] = ran1;
2868: rr= ran1+0.5;
2869: /* RAN1 = RAN1 + HALF */
2870: /* RAN3(RAN2) = RAN1 */
2871: /* RANDOM = RAN1 + .5D0 */
2872: /* r = ( ( double ) ( *seed ) ) * 4.656612875E-10; */
2873: return rr;
2874: }
2875: static void matprint(char *s, double **v, int m, int n)
2876: /* char *s; */
2877: /* double v[N][N]; */
2878: {
2879: #define INCX 8
2880: int i;
2881:
2882: int i2hi;
2883: int ihi;
2884: int ilo;
2885: int i2lo;
2886: int jlo=1;
2887: int j;
2888: int j2hi;
2889: int jhi;
2890: int j2lo;
2891: ilo=1;
2892: ihi=n;
2893: jlo=1;
2894: jhi=n;
2895:
2896: printf ("\n" );
2897: printf ("%s\n", s );
2898: for ( j2lo = jlo; j2lo <= jhi; j2lo = j2lo + INCX )
2899: {
2900: j2hi = j2lo + INCX - 1;
2901: if ( n < j2hi )
2902: {
2903: j2hi = n;
2904: }
2905: if ( jhi < j2hi )
2906: {
2907: j2hi = jhi;
2908: }
2909:
2910: /* fprintf ( ficlog, "\n" ); */
2911: printf ("\n" );
2912: /*
2913: For each column J in the current range...
2914:
2915: Write the header.
2916: */
2917: /* fprintf ( ficlog, " Col: "); */
2918: printf ("Col:");
2919: for ( j = j2lo; j <= j2hi; j++ )
2920: {
2921: /* fprintf ( ficlog, " %7d ", j - 1 ); */
2922: /* printf (" %9d ", j - 1 ); */
2923: printf (" %9d ", j );
2924: }
2925: /* fprintf ( ficlog, "\n" ); */
2926: /* fprintf ( ficlog, " Row\n" ); */
2927: /* fprintf ( ficlog, "\n" ); */
2928: printf ("\n" );
2929: printf (" Row\n" );
2930: printf ("\n" );
2931: /*
2932: Determine the range of the rows in this strip.
2933: */
2934: if ( 1 < ilo ){
2935: i2lo = ilo;
2936: }else{
2937: i2lo = 1;
2938: }
2939: if ( m < ihi ){
2940: i2hi = m;
2941: }else{
2942: i2hi = ihi;
2943: }
2944:
2945: for ( i = i2lo; i <= i2hi; i++ ){
2946: /*
2947: Print out (up to) 5 entries in row I, that lie in the current strip.
2948: */
2949: /* fprintf ( ficlog, "%5d:", i - 1 ); */
2950: /* printf ("%5d:", i - 1 ); */
2951: printf ("%5d:", i );
2952: for ( j = j2lo; j <= j2hi; j++ )
2953: {
2954: /* fprintf ( ficlog, " %14g", a[i-1+(j-1)*m] ); */
2955: /* printf ("%14.7g ", a[i-1+(j-1)*m] ); */
2956: /* printf("%14.7f ", v[i-1][j-1]); */
2957: printf("%14.7f ", v[i][j]);
2958: /* fprintf ( stdout, " %14g", a[i-1+(j-1)*m] ); */
2959: }
2960: /* fprintf ( ficlog, "\n" ); */
2961: printf ("\n" );
2962: }
2963: }
2964:
2965: /* printf("%s\n", s); */
2966: /* for (k=0; k<n; k++) { */
2967: /* for (i=0; i<n; i++) { */
2968: /* /\* printf("%20.10e ", v[k][i]); *\/ */
2969: /* } */
2970: /* printf("\n"); */
2971: /* } */
2972: #undef INCX
2973: }
2974:
2975: void vecprint(char *s, double *x, int n)
2976: /* char *s; */
2977: /* double x[N]; */
2978: {
2979: int i=0;
2980:
2981: printf(" %s", s);
2982: /* for (i=0; i<n; i++) */
2983: for (i=1; i<=n; i++)
2984: printf (" %14.7g", x[i] );
2985: /* printf(" %8d: %14g\n", i, x[i]); */
2986: printf ("\n" );
2987: }
2988:
2989: static void print() /* print a line of traces */
2990: {
2991:
2992:
2993: printf("\n");
2994: /* printf("... chi square reduced to ... %20.10e\n", fx); */
2995: /* printf("... after %u function calls ...\n", nf); */
2996: /* printf("... including %u linear searches ...\n", nl); */
2997: printf("%10d %10d%14.7g",nl, nf, fx);
2998: vecprint("... current values of x ...", x, n);
2999: }
3000: /* static void print2(int n, double *x, int prin, double fx, int nf, int nl) */ /* print a line of traces */
3001: static void print2() /* print a line of traces */
3002: {
3003: int i; double fmin=0.;
3004:
3005: /* printf("\n"); */
3006: /* printf("... chi square reduced to ... %20.10e\n", fx); */
3007: /* printf("... after %u function calls ...\n", nf); */
3008: /* printf("... including %u linear searches ...\n", nl); */
3009: /* printf("%10d %10d%14.7g",nl, nf, fx); */
3010: printf ( "\n" );
3011: printf ( " Linear searches %d", nl );
3012: /* printf ( " Linear searches %d\n", nl ); */
3013: /* printf ( " Function evaluations %d\n", nf ); */
3014: /* printf ( " Function value FX = %g\n", fx ); */
3015: printf ( " Function evaluations %d", nf );
3016: printf ( " Function value FX = %.12lf\n", fx );
3017: #ifdef DEBUGPRAX
3018: printf("n=%d prin=%d\n",n,prin);
3019: #endif
3020: if(fx <= fmin) printf(" UNDEFINED "); else printf("%14.7g",log(fx-fmin));
3021: if ( n <= 4 || 2 < prin )
3022: {
3023: /* for(i=1;i<=n;i++)printf("%14.7g",x[i-1]); */
3024: for(i=1;i<=n;i++)printf("%14.7g",x[i]);
3025: /* r8vec_print ( n, x, " X:" ); */
3026: }
3027: printf("\n");
3028: }
3029:
3030:
3031: /* #ifdef MSDOS */
3032: /* static double tflin[N]; */
3033: /* #endif */
3034:
3035: static double flin(double l, int j)
3036: /* double l; */
3037: {
3038: int i;
3039: /* #ifndef MSDOS */
3040: /* double tflin[N]; */
3041: /* #endif */
3042: /* double *tflin; */ /* Be careful to put tflin on a vector n */
3043:
3044: /* j is used from 0 to n-1 and can be -1 for parabolic search */
3045:
3046: /* if (j != -1) { /\* linear search *\/ */
3047: if (j > 0) { /* linear search */
3048: /* for (i=0; i<n; i++){ */
3049: for (i=1; i<=n; i++){
3050: tflin[i] = x[i] + l *v[i][j];
3051: #ifdef DEBUGPRAX
3052: /* printf(" flin i=%14d t=%14.7f x=%14.7f l=%14.7f v[%d,%d]=%14.7f nf=%14d\n",i+1, tflin[i],x[i],l,i,j,v[i][j],nf); */
3053: printf(" flin i=%14d t=%14.7f x=%14.7f l=%14.7f v[%d,%d]=%14.7f nf=%14d\n",i, tflin[i],x[i],l,i,j,v[i][j],nf);
3054: #endif
3055: }
3056: }
3057: else { /* search along parabolic space curve */
3058: qa = l*(l-qd1)/(qd0*(qd0+qd1));
3059: qb = (l+qd0)*(qd1-l)/(qd0*qd1);
3060: qc = l*(l+qd0)/(qd1*(qd0+qd1));
3061: #ifdef DEBUGPRAX
3062: printf(" search along a parabolic space curve. j=%14d nf=%14d l=%14.7f qd0=%14.7f qd1=%14.7f\n",j,nf,l,qd0,qd1);
3063: #endif
3064: /* for (i=0; i<n; i++){ */
3065: for (i=1; i<=n; i++){
3066: tflin[i] = qa*q0[i]+qb*x[i]+qc*q1[i];
3067: #ifdef DEBUGPRAX
3068: /* printf(" parabole i=%14d t(i)=%14.7f q0=%14.7f x=%14.7f q1=%14.7f\n",i+1,tflin[i],q0[i],x[i],q1[i]); */
3069: printf(" parabole i=%14d t(i)=%14.7e q0=%14.7e x=%14.7e q1=%14.7e\n",i,tflin[i],q0[i],x[i],q1[i]);
3070: #endif
3071: }
3072: }
3073: nf++;
3074:
3075: #ifdef NR_SHIFT
3076: return (*fun)((tflin-1), n);
3077: #else
3078: /* return (*fun)(tflin, n);*/
3079: return (*fun)(tflin);
3080: #endif
3081: }
3082:
3083: void minny(int j, int nits, double *d2, double *x1, double f1, int fk)
3084: /* double *d2, *x1, f1; */
3085: {
3086: /* here j is from 0 to n-1 and can be -1 for parabolic search */
3087: /* MINIMIZES F FROM X IN THE DIRECTION V(*,J) */
3088: /* UNLESS J<1, WHEN A QUADRATIC SEARCH IS DONE */
3089: /* IN THE PLANE DEFINED BY Q0, Q1 AND X. */
3090: /* D2 AN APPROXIMATION TO HALF F'' (OR ZERO), */
3091: /* X1 AN ESTIMATE OF DISTANCE TO MINIMUM, */
3092: /* RETURNED AS THE DISTANCE FOUND. */
3093: /* IF FK = TRUE THEN F1 IS FLIN(X1), OTHERWISE */
3094: /* X1 AND F1 ARE IGNORED ON ENTRY UNLESS FINAL */
3095: /* FX > F1. NITS CONTROLS THE NUMBER OF TIMES */
3096: /* AN ATTEMPT IS MADE TO HALVE THE INTERVAL. */
3097: /* SIDE EFFECTS: USES AND ALTERS X, FX, NF, NL. */
3098: /* IF J < 1 USES VARIABLES Q... . */
3099: /* USES H, N, T, M2, M4, LDT, DMIN, MACHEPS; */
3100: int k, i, dz;
3101: double x2, xm, f0, f2, fm, d1, t2, sf1, sx1;
3102: double s;
3103: double macheps;
3104: macheps=pow(16.0,-13.0);
3105: sf1 = f1; sx1 = *x1;
3106: k = 0; xm = 0.0; fm = f0 = fx; dz = *d2 < macheps;
3107: /* h=1.0;*/ /* To be revised */
3108: #ifdef DEBUGPRAX
3109: /* printf("min macheps=%14g h=%14g step=%14g t=%14g fx=%14g\n",macheps,h, step,t, fx); */
3110: /* Where is fx coming from */
3111: printf(" min macheps=%14g h=%14g t=%14g fx=%.9lf dirj=%d\n",macheps, h, t, fx, j);
3112: matprint(" min vectors:",v,n,n);
3113: #endif
3114: /* find step size */
3115: s = 0.;
3116: /* for (i=0; i<n; i++) s += x[i]*x[i]; */
3117: for (i=1; i<=n; i++) s += x[i]*x[i];
3118: s = sqrt(s);
3119: if (dz)
3120: t2 = m4*sqrt(fabs(fx)/dmin + s*ldt) + m2*ldt;
3121: else
3122: t2 = m4*sqrt(fabs(fx)/(*d2) + s*ldt) + m2*ldt;
3123: s = s*m4 + t;
3124: if (dz && t2 > s) t2 = s;
3125: if (t2 < small_windows) t2 = small_windows;
3126: if (t2 > 0.01*h) t2 = 0.01 * h;
3127: if (fk && f1 <= fm) {
3128: xm = *x1;
3129: fm = f1;
3130: }
3131: #ifdef DEBUGPRAX
3132: printf(" additional flin X1=%14.7f t2=%14.7f *f1=%14.7f fm=%14.7f fk=%d\n",*x1,t2,f1,fm,fk);
3133: #endif
3134: if (!fk || fabs(*x1) < t2) {
3135: *x1 = (*x1 >= 0 ? t2 : -t2);
3136: /* *x1 = (*x1 > 0 ? t2 : -t2); */ /* kind of error */
3137: #ifdef DEBUGPRAX
3138: printf(" additional flin X1=%16.10e dirj=%d fk=%d\n",*x1, j, fk);
3139: #endif
3140: f1 = flin(*x1, j);
3141: #ifdef DEBUGPRAX
3142: printf(" after flin f1=%18.12e dirj=%d fk=%d\n",f1, j,fk);
3143: #endif
3144: }
3145: if (f1 <= fm) {
3146: xm = *x1;
3147: fm = f1;
3148: }
3149: L0: /*L0 loop or next */
3150: /*
3151: Evaluate FLIN at another point and estimate the second derivative.
3152: */
3153: if (dz) {
3154: x2 = (f0 < f1 ? -(*x1) : 2*(*x1));
3155: #ifdef DEBUGPRAX
3156: printf(" additional second flin x2=%14.8e x1=%14.8e f0=%14.8e f1=%18.12e dirj=%d\n",x2,*x1,f0,f1,j);
3157: #endif
3158: f2 = flin(x2, j);
3159: #ifdef DEBUGPRAX
3160: printf(" additional second flin x2=%16.10e x1=%16.10e f1=%18.12e f0=%18.10e f2=%18.10e fm=%18.10e\n",x2, *x1, f1,f0,f2,fm);
3161: #endif
3162: if (f2 <= fm) {
3163: xm = x2;
3164: fm = f2;
3165: }
3166: /* d2 is the curvature or double difference f1 doesn't seem to be accurately computed */
3167: *d2 = (x2*(f1-f0) - (*x1)*(f2-f0))/((*x1)*x2*((*x1)-x2));
3168: #ifdef DEBUGPRAX
3169: double d11,d12;
3170: d11=(f1-f0)/(*x1);d12=(f2-f0)/x2;
3171: printf(" d11=%18.12e d12=%18.12e d11-d12=%18.12e x1-x2=%18.12e (d11-d12)/(x2-(*x1))=%18.12e\n", d11 ,d12, d11-d12, x2-(*x1), (d11-d12)/(x2-(*x1)));
3172: printf(" original computing f1=%18.12e *d2=%16.10e f0=%18.12e f1-f0=%16.10e f2-f0=%16.10e\n",f1,*d2,f0,f1-f0, f2-f0);
3173: double ff1=7.783920622852e+04;
3174: double f1mf0=9.0344736236e-05;
3175: *d2 = (f1mf0)/ (*x1)/((*x1)-x2) - (f2-f0)/x2/((*x1)-x2);
3176: /* *d2 = (ff1-f0)/ (*x1)/((*x1)-x2) - (f2-f0)/x2/((*x1)-x2); */
3177: printf(" simpliff computing *d2=%16.10e f1mf0=%18.12e,f1=f0+f1mf0=%18.12e\n",*d2,f1mf0,f0+f1mf0);
3178: *d2 = ((f1-f0)/ (*x1) - (f2-f0)/x2)/((*x1)-x2);
3179: printf(" overlifi computing *d2=%16.10e\n",*d2);
3180: #endif
3181: *d2 = ((f1-f0)/ (*x1) - (f2-f0)/x2)/((*x1)-x2);
3182: }
3183: #ifdef DEBUGPRAX
3184: printf(" additional second flin xm=%14.8e fm=%14.8e *d2=%14.8e\n",xm, fm,*d2);
3185: #endif
3186: /*
3187: Estimate the first derivative at 0.
3188: */
3189: d1 = (f1-f0)/(*x1) - *x1**d2; dz = 1;
3190: /*
3191: Predict the minimum.
3192: */
3193: if (*d2 <= small_windows) {
3194: x2 = (d1 < 0 ? h : -h);
3195: }
3196: else {
3197: x2 = - 0.5*d1/(*d2);
3198: }
3199: #ifdef DEBUGPRAX
3200: printf(" AT d1=%14.8e d2=%14.8e small=%14.8e dz=%d x1=%14.8e x2=%14.8e\n",d1,*d2,small_windows,dz,*x1,x2);
3201: #endif
3202: if (fabs(x2) > h)
3203: x2 = (x2 > 0 ? h : -h);
3204: L1: /* L1 or try loop */
3205: #ifdef DEBUGPRAX
3206: printf(" AT predicted minimum flin x2=%14.8e x1=%14.8e K=%14d NITS=%14d dirj=%d\n",x2,*x1,k,nits,j);
3207: #endif
3208: f2 = flin(x2, j); /* x[i]+x2*v[i][j] */
3209: #ifdef DEBUGPRAX
3210: printf(" after flin f0=%14.8e f1=%14.8e f2=%14.8e fm=%14.8e\n",f0,f1,f2, fm);
3211: #endif
3212: if ((k < nits) && (f2 > f0)) {
3213: #ifdef DEBUGPRAX
3214: printf(" NO SUCCESS SO TRY AGAIN;\n");
3215: #endif
3216: k++;
3217: if ((f0 < f1) && (*x1*x2 > 0.0))
3218: goto L0; /* or next */
3219: x2 *= 0.5;
3220: goto L1;
3221: }
3222: nl++;
3223: #ifdef DEBUGPRAX
3224: printf(" bebeBE end of min x1=%14.8e x2=%14.8e f1=%14.8e f2=%14.8e f0=%14.8e fm=%14.8e d2=%14.8e\n",*x1, x2, f1, f2, f0, fm, *d2);
3225: #endif
3226: if (f2 > fm) x2 = xm; else fm = f2;
3227: if (fabs(x2*(x2-*x1)) > small_windows) {
3228: *d2 = (x2*(f1-f0) - *x1*(fm-f0))/(*x1*x2*(*x1-x2));
3229: }
3230: else {
3231: if (k > 0) *d2 = 0;
3232: }
3233: #ifdef DEBUGPRAX
3234: printf(" bebe end of min x1=%14.8e fx=%14.8e d2=%14.8e\n",*x1, fx, *d2);
3235: #endif
3236: if (*d2 <= small_windows) *d2 = small_windows;
3237: *x1 = x2; fx = fm;
3238: if (sf1 < fx) {
3239: fx = sf1;
3240: *x1 = sx1;
3241: }
3242: /*
3243: Update X for linear search.
3244: */
3245: #ifdef DEBUGPRAX
3246: printf(" end of min x1=%14.8e fx=%14.8e d2=%14.8e\n",*x1, fx, *d2);
3247: #endif
3248:
3249: /* if (j != -1) */
3250: /* for (i=0; i<n; i++) */
3251: /* x[i] += (*x1)*v[i][j]; */
3252: if (j > 0)
3253: for (i=1; i<=n; i++)
3254: x[i] += (*x1)*v[i][j];
3255: }
3256:
3257: void quad() /* look for a minimum along the curve q0, q1, q2 */
3258: {
3259: int i;
3260: double l, s;
3261:
3262: s = fx; fx = qf1; qf1 = s; qd1 = 0.0;
3263: /* for (i=0; i<n; i++) { */
3264: for (i=1; i<=n; i++) {
3265: s = x[i]; l = q1[i]; x[i] = l; q1[i] = s;
3266: qd1 = qd1 + (s-l)*(s-l);
3267: }
3268: s = 0.0; qd1 = sqrt(qd1); l = qd1;
3269: #ifdef DEBUGPRAX
3270: printf(" QUAD after sqrt qd1=%14.8e \n",qd1);
3271: #endif
3272:
3273: if (qd0>0.0 && qd1>0.0 &&nl>=3*n*n) {
3274: #ifdef DEBUGPRAX
3275: printf(" QUAD before min value=%14.8e \n",qf1);
3276: #endif
3277: /* min(-1, 2, &s, &l, qf1, 1); */
3278: minny(0, 2, &s, &l, qf1, 1);
3279: qa = l*(l-qd1)/(qd0*(qd0+qd1));
3280: qb = (l+qd0)*(qd1-l)/(qd0*qd1);
3281: qc = l*(l+qd0)/(qd1*(qd0+qd1));
3282: }
3283: else {
3284: fx = qf1; qa = qb = 0.0; qc = 1.0;
3285: }
3286: #ifdef DEBUGPRAX
3287: printf("after eventual min qd0=%14.8e qd1=%14.8e nl=%d\n",qd0, qd1,nl);
3288: #endif
3289: qd0 = qd1;
3290: /* for (i=0; i<n; i++) { */
3291: for (i=1; i<=n; i++) {
3292: s = q0[i]; q0[i] = x[i];
3293: x[i] = qa*s + qb*x[i] + qc*q1[i];
3294: }
3295: #ifdef DEBUGQUAD
3296: vecprint ( " X after QUAD:" , x, n );
3297: #endif
3298: }
3299:
3300: /* void minfit(int n, double eps, double tol, double ab[N][N], double q[]) */
3301: void minfit(int n, double eps, double tol, double **ab, double q[])
3302: /* int n; */
3303: /* double eps, tol, ab[N][N], q[N]; */
3304: {
3305: int l, kt, l2, i, j, k;
3306: double c, f, g, h, s, x, y, z;
3307: /* double eps; */
3308: /* #ifndef MSDOS */
3309: /* double e[N]; /\* plenty of stack on a vax *\/ */
3310: /* #endif */
3311: /* double *e; */
3312: /* e=vector(0,n-1); /\* should be freed somewhere but gotos *\/ */
3313:
3314: /* householder's reduction to bidiagonal form */
3315:
3316: if(n==1){
3317: /* q[1-1]=ab[1-1][1-1]; */
3318: /* ab[1-1][1-1]=1.0; */
3319: q[1]=ab[1][1];
3320: ab[1][1]=1.0;
3321: return; /* added from hardt */
3322: }
3323: /* eps=macheps; */ /* added */
3324: x = g = 0.0;
3325: #ifdef DEBUGPRAX
3326: matprint (" HOUSE holder:", ab, n, n);
3327: #endif
3328:
3329: /* for (i=0; i<n; i++) { /\* FOR I := 1 UNTIL N DO *\/ */
3330: for (i=1; i<=n; i++) { /* FOR I := 1 UNTIL N DO */
3331: e[i] = g; s = 0.0; l = i+1;
3332: /* for (j=i; j<n; j++) /\* FOR J := I UNTIL N DO S := S*AB(J,I)**2; *\/ /\* not correct *\/ */
3333: for (j=i; j<=n; j++) /* FOR J := I UNTIL N DO S := S*AB(J,I)**2; */ /* not correct */
3334: s += ab[j][i] * ab[j][i];
3335: #ifdef DEBUGPRAXFIN
3336: printf("i=%d s=%d %.7g tol=%.7g",i,s,tol);
3337: #endif
3338: if (s < tol) {
3339: g = 0.0;
3340: }
3341: else {
3342: /* f = ab[i][i]; */
3343: f = ab[i][i];
3344: if (f < 0.0)
3345: g = sqrt(s);
3346: else
3347: g = -sqrt(s);
3348: /* h = f*g - s; ab[i][i] = f - g; */
3349: h = f*g - s; ab[i][i] = f - g;
3350: /* for (j=l; j<n; j++) { */ /* FOR J := L UNTIL N DO */ /* wrong */
3351: for (j=l; j<=n; j++) {
3352: f = 0.0;
3353: /* for (k=i; k<n; k++) /\* FOR K := I UNTIL N DO *\/ /\* wrong *\/ */
3354: for (k=i; k<=n; k++) /* FOR K := I UNTIL N DO */
3355: /* f += ab[k][i] * ab[k][j]; */
3356: f += ab[k][i] * ab[k][j];
3357: f /= h;
3358: for (k=i; k<=n; k++) /* FOR K := I UNTIL N DO */
3359: /* for (k=i; k<n; k++)/\* FOR K := I UNTIL N DO *\/ /\* wrong *\/ */
3360: ab[k][j] += f * ab[k][i];
3361: /* ab[k][j] += f * ab[k][i]; */
3362: #ifdef DEBUGPRAX
3363: printf("Holder J=%d F=%.7g",j,f);
3364: #endif
3365: }
3366: } /* end s */
3367: /* q[i] = g; s = 0.0; */
3368: q[i] = g; s = 0.0;
3369: #ifdef DEBUGPRAX
3370: printf(" I Q=%d %.7g",i,q[i]);
3371: #endif
3372:
3373: /* if (i < n) */
3374: /* if (i <= n) /\* I is always lower or equal to n wasn't in golub reinsch*\/ */
3375: /* for (j=l; j<n; j++) */
3376: for (j=l; j<=n; j++)
3377: s += ab[i][j] * ab[i][j];
3378: /* s += ab[i][j] * ab[i][j]; */
3379: if (s < tol) {
3380: g = 0.0;
3381: }
3382: else {
3383: if(i<n)
3384: /* f = ab[i][i+1]; */ /* Brent golub overflow */
3385: f = ab[i][i+1];
3386: if (f < 0.0)
3387: g = sqrt(s);
3388: else
3389: g = - sqrt(s);
3390: h = f*g - s;
3391: /* h = f*g - s; ab[i][i+1] = f - g; */ /* Overflow for i=n Error in Golub too but not Burkardt*/
3392: /* for (j=l; j<n; j++) */
3393: /* e[j] = ab[i][j]/h; */
3394: if(i<n){
3395: ab[i][i+1] = f - g;
3396: for (j=l; j<=n; j++)
3397: e[j] = ab[i][j]/h;
3398: /* for (j=l; j<n; j++) { */
3399: for (j=l; j<=n; j++) {
3400: s = 0.0;
3401: /* for (k=l; k<n; k++) s += ab[j][k]*ab[i][k]; */
3402: for (k=l; k<=n; k++) s += ab[j][k]*ab[i][k];
3403: /* for (k=l; k<n; k++) ab[j][k] += s * e[k]; */
3404: for (k=l; k<=n; k++) ab[j][k] += s * e[k];
3405: } /* END J */
3406: } /* END i <n */
3407: } /* end s */
3408: /* y = fabs(q[i]) + fabs(e[i]); */
3409: y = fabs(q[i]) + fabs(e[i]);
3410: if (y > x) x = y;
3411: #ifdef DEBUGPRAX
3412: printf(" I Y=%d %.7g",i,y);
3413: #endif
3414: #ifdef DEBUGPRAX
3415: printf(" i=%d e(i) %.7g",i,e[i]);
3416: #endif
3417: } /* end i */
3418: /*
3419: Accumulation of right hand transformations */
3420: /* for (i=n-1; i >= 0; i--) { */ /* FOR I := N STEP -1 UNTIL 1 DO */
3421: /* We should avoid the overflow in Golub */
3422: /* ab[n-1][n-1] = 1.0; */
3423: /* g = e[n-1]; */
3424: ab[n][n] = 1.0;
3425: g = e[n];
3426: l = n;
3427:
3428: /* for (i=n; i >= 1; i--) { */
3429: for (i=n-1; i >= 1; i--) { /* n-1 loops, different from brent and golub*/
3430: if (g != 0.0) {
3431: /* h = ab[i-1][i]*g; */
3432: h = ab[i][i+1]*g;
3433: for (j=l; j<=n; j++) ab[j][i] = ab[i][j] / h;
3434: for (j=l; j<=n; j++) {
3435: /* h = ab[i][i+1]*g; */
3436: /* for (j=l; j<n; j++) ab[j][i] = ab[i][j] / h; */
3437: /* for (j=l; j<n; j++) { */
3438: s = 0.0;
3439: /* for (k=l; k<n; k++) s += ab[i][k] * ab[k][j]; */
3440: /* for (k=l; k<n; k++) ab[k][j] += s * ab[k][i]; */
3441: for (k=l; k<=n; k++) s += ab[i][k] * ab[k][j];
3442: for (k=l; k<=n; k++) ab[k][j] += s * ab[k][i];
3443: }/* END J */
3444: }/* END G */
3445: /* for (j=l; j<n; j++) */
3446: /* ab[i][j] = ab[j][i] = 0.0; */
3447: /* ab[i][i] = 1.0; g = e[i]; l = i; */
3448: for (j=l; j<=n; j++)
3449: ab[i][j] = ab[j][i] = 0.0;
3450: ab[i][i] = 1.0; g = e[i]; l = i;
3451: }/* END I */
3452: #ifdef DEBUGPRAX
3453: matprint (" HOUSE accumulation:",ab,n, n );
3454: #endif
3455:
3456: /* diagonalization to bidiagonal form */
3457: eps *= x;
3458: /* for (k=n-1; k>= 0; k--) { */
3459: for (k=n; k>= 1; k--) {
3460: kt = 0;
3461: TestFsplitting:
3462: #ifdef DEBUGPRAX
3463: printf(" TestFsplitting: k=%d kt=%d\n",k,kt);
3464: /* for(i=1;i<=n;i++)printf(" e(%d)=%.14f",i,e[i]);printf("\n"); */
3465: #endif
3466: kt = kt+1;
3467: /* TestFsplitting: */
3468: /* if (++kt > 30) { */
3469: if (kt > 30) {
3470: e[k] = 0.0;
3471: fprintf(stderr, "\n+++ MINFIT - Fatal error\n");
3472: fprintf ( stderr, " The QR algorithm failed to converge.\n" );
3473: }
3474: /* for (l2=k; l2>=0; l2--) { */
3475: for (l2=k; l2>=1; l2--) {
3476: l = l2;
3477: #ifdef DEBUGPRAX
3478: printf(" l e(l)< eps %d %.7g %.7g ",l,e[l], eps);
3479: #endif
3480: /* if (fabs(e[l]) <= eps) */
3481: if (fabs(e[l]) <= eps)
3482: goto TestFconvergence;
3483: /* if (fabs(q[l-1]) <= eps)*/ /* missing if ( 1 < l ){ *//* printf(" q(l-1)< eps %d %.7g %.7g ",l-1,q[l-2], eps); */
3484: if (fabs(q[l-1]) <= eps)
3485: break; /* goto Cancellation; */
3486: }
3487: Cancellation:
3488: #ifdef DEBUGPRAX
3489: printf(" Cancellation:\n");
3490: #endif
3491: c = 0.0; s = 1.0;
3492: for (i=l; i<=k; i++) {
3493: f = s * e[i]; e[i] *= c;
3494: /* f = s * e[i]; e[i] *= c; */
3495: if (fabs(f) <= eps)
3496: goto TestFconvergence;
3497: /* g = q[i]; */
3498: g = q[i];
3499: if (fabs(f) < fabs(g)) {
3500: double fg = f/g;
3501: h = fabs(g)*sqrt(1.0+fg*fg);
3502: }
3503: else {
3504: double gf = g/f;
3505: h = (f!=0.0 ? fabs(f)*sqrt(1.0+gf*gf) : 0.0);
3506: }
3507: /* COMMENT: THE ABOVE REPLACES Q(I):=H:=LONGSQRT(G*G+F*F) */
3508: /* WHICH MAY GIVE INCORRECT RESULTS IF THE */
3509: /* SQUARES UNDERFLOW OR IF F = G = 0; */
3510:
3511: /* q[i] = h; */
3512: q[i] = h;
3513: if (h == 0.0) { h = 1.0; g = 1.0; }
3514: c = g/h; s = -f/h;
3515: }
3516: TestFconvergence:
3517: #ifdef DEBUGPRAX
3518: printf(" TestFconvergence: l=%d k=%d\n",l,k);
3519: #endif
3520: /* z = q[k]; */
3521: z = q[k];
3522: if (l == k)
3523: goto Convergence;
3524: /* shift from bottom 2x2 minor */
3525: /* x = q[l]; y = q[k-l]; g = e[k-1]; h = e[k]; */ /* Error */
3526: x = q[l]; y = q[k-1]; g = e[k-1]; h = e[k];
3527: f = ((y-z)*(y+z) + (g-h)*(g+h)) / (2.0*h*y);
3528: g = sqrt(f*f+1.0);
3529: if (f <= 0.0)
3530: f = ((x-z)*(x+z) + h*(y/(f-g)-h))/x;
3531: else
3532: f = ((x-z)*(x+z) + h*(y/(f+g)-h))/x;
3533: /* next qr transformation */
3534: s = c = 1.0;
3535: for (i=l+1; i<=k; i++) {
3536: #ifdef DEBUGPRAXQR
3537: printf(" Before Mid TestFconvergence: l+1=%d i=%d k=%d h=%.6e e(i)=%14.8f e(i-1)=%14.8f\n",l+1,i,k, h, e[i],e[i-1]);
3538: #endif
3539: /* g = e[i]; y = q[i]; h = s*g; g *= c; */
3540: g = e[i]; y = q[i]; h = s*g; g *= c;
3541: if (fabs(f) < fabs(h)) {
3542: double fh = f/h;
3543: z = fabs(h) * sqrt(1.0 + fh*fh);
3544: }
3545: else {
3546: double hf = h/f;
3547: z = (f!=0.0 ? fabs(f)*sqrt(1.0+hf*hf) : 0.0);
3548: }
3549: /* e[i-1] = z; */
3550: e[i-1] = z;
3551: #ifdef DEBUGPRAXQR
3552: printf(" Mid TestFconvergence: l+1=%d i=%d k=%d h=%.6e e(i)=%14.8f e(i-1)=%14.8f\n",l+1,i,k, h, e[i],e[i-1]);
3553: #endif
3554: if (z == 0.0)
3555: f = z = 1.0;
3556: c = f/z; s = h/z;
3557: f = x*c + g*s; g = - x*s + g*c; h = y*s;
3558: y *= c;
3559: /* for (j=0; j<n; j++) { */
3560: /* x = ab[j][i-1]; z = ab[j][i]; */
3561: /* ab[j][i-1] = x*c + z*s; */
3562: /* ab[j][i] = - x*s + z*c; */
3563: /* } */
3564: for (j=1; j<=n; j++) {
3565: x = ab[j][i-1]; z = ab[j][i];
3566: ab[j][i-1] = x*c + z*s;
3567: ab[j][i] = - x*s + z*c;
3568: }
3569: if (fabs(f) < fabs(h)) {
3570: double fh = f/h;
3571: z = fabs(h) * sqrt(1.0 + fh*fh);
3572: }
3573: else {
3574: double hf = h/f;
3575: z = (f!=0.0 ? fabs(f)*sqrt(1.0+hf*hf) : 0.0);
3576: }
3577: #ifdef DEBUGPRAXQR
3578: printf(" qr transformation z f h=%.7g %.7g %.7g i=%d k=%d\n",z,f,h, i, k);
3579: #endif
3580: q[i-1] = z;
3581: if (z == 0.0)
3582: z = f = 1.0;
3583: c = f/z; s = h/z;
3584: f = c*g + s*y; /* f can be very small */
3585: x = - s*g + c*y;
3586: }
3587: /* e[l] = 0.0; e[k] = f; q[k] = x; */
3588: e[l] = 0.0; e[k] = f; q[k] = x;
3589: #ifdef DEBUGPRAXQR
3590: printf(" aftermid loop l=%d k=%d e(l)=%7g e(k)=%.7g q(k)=%.7g x=%.7g\n",l,k,e[l],e[k],q[k],x);
3591: #endif
3592: goto TestFsplitting;
3593: Convergence:
3594: #ifdef DEBUGPRAX
3595: printf(" Convergence:\n");
3596: #endif
3597: if (z < 0.0) {
3598: /* q[k] = - z; */
3599: /* for (j=0; j<n; j++) ab[j][k] = - ab[j][k]; */
3600: q[k] = - z;
3601: for (j=1; j<=n; j++) ab[j][k] = - ab[j][k];
3602: }/* END Z */
3603: }/* END K */
3604: } /* END MINFIT */
3605:
3606:
3607: double praxis(double tol, double macheps, double h0, int _n, int _prin, double *_x, double (*_fun)(double *_x))
3608: /* double praxis(double tol, double macheps, double h0, int _n, int _prin, double *_x, double (*_fun)(double *_x, int _n)) */
3609: /* double praxis(double (*_fun)(), double _x[], int _n) */
3610: /* double (*_fun)(); */
3611: /* double _x[N]; */
3612: /* double (*_fun)(); */
3613: /* double _x[N]; */
3614: {
3615: /* init global extern variables and parameters */
3616: /* double *d, *y, *z, */
3617: /* *q0, *q1, **v; */
3618: /* double *tflin; /\* used in flin: return (*fun)(tflin, n); *\/ */
3619: /* double *e; /\* used in minfit, don't konw how to free memory and thus made global *\/ */
3620:
3621:
3622: int seed; /* added */
3623: int biter=0;
3624: double r;
3625: double randbrent( int (*));
3626: double s, sf;
3627:
3628: h = h0; /* step; */
3629: t = tol;
3630: scbd = 1.0;
3631: illc = 0;
3632: ktm = 1;
3633:
3634: macheps = DBL_EPSILON;
3635: /* prin=4; */
3636: #ifdef DEBUGPRAX
3637: printf("Praxis macheps=%14g h=%14g step=%14g tol=%14g\n",macheps,h, h0,tol);
3638: #endif
3639: n = _n;
3640: x = _x;
3641: prin = _prin;
3642: fun = _fun;
3643: d=vector(1, n);
3644: y=vector(1, n);
3645: z=vector(1, n);
3646: q0=vector(1, n);
3647: q1=vector(1, n);
3648: e=vector(1, n);
3649: tflin=vector(1, n);
3650: v=matrix(1, n, 1, n);
3651: for(i=1;i<=n;i++){d[i]=y[i]=z[i]=q0[0]=e[i]=tflin[i]=0.;}
3652: small_windows = (macheps) * (macheps); vsmall = small_windows*small_windows;
3653: large = 1.0/small_windows; vlarge = 1.0/vsmall;
3654: m2 = sqrt(macheps); m4 = sqrt(m2);
3655: seed = 123456789; /* added */
3656: ldfac = (illc ? 0.1 : 0.01);
3657: for(i=1;i<=n;i++) z[i]=0.; /* Was missing in Gegenfurtner as well as Brent's algol or fortran */
3658: nl = kt = 0; nf = 1;
3659: #ifdef NR_SHIFT
3660: fx = (*fun)((x-1), n);
3661: #else
3662: fx = (*fun)(x);
3663: #endif
3664: qf1 = fx;
3665: t2 = small_windows + fabs(t); t = t2; dmin = small_windows;
3666: #ifdef DEBUGPRAX
3667: printf("praxis2 macheps=%14g h=%14g step=%14g small=%14g t=%14g\n",macheps,h, h0,small_windows, t);
3668: #endif
3669: if (h < 100.0*t) h = 100.0*t;
3670: #ifdef DEBUGPRAX
3671: printf("praxis3 macheps=%14g h=%14g step=%14g small=%14g t=%14g\n",macheps,h, h0,small_windows, t);
3672: #endif
3673: ldt = h;
3674: /* for (i=0; i<n; i++) for (j=0; j<n; j++) */
3675: for (i=1; i<=n; i++) for (j=1; j<=n; j++)
3676: v[i][j] = (i == j ? 1.0 : 0.0);
3677: d[1] = 0.0; qd0 = 0.0;
3678: /* for (i=0; i<n; i++) q1[i] = x[i]; */
3679: for (i=1; i<=n; i++) q1[i] = x[i];
3680: if (prin > 1) {
3681: printf("\n------------- enter function praxis -----------\n");
3682: printf("... current parameter settings ...\n");
3683: printf("... scaling ... %20.10e\n", scbd);
3684: printf("... tol ... %20.10e\n", t);
3685: printf("... maxstep ... %20.10e\n", h);
3686: printf("... illc ... %20u\n", illc);
3687: printf("... ktm ... %20u\n", ktm);
3688: printf("... maxfun ... %20u\n", maxfun);
3689: }
3690: if (prin) print2();
3691:
3692: mloop:
3693: biter++; /* Added to count the loops */
3694: /* sf = d[0]; */
3695: /* s = d[0] = 0.0; */
3696: printf("\n Big iteration %d \n",biter);
3697: fprintf(ficlog,"\n Big iteration %d \n",biter);
3698: sf = d[1];
3699: s = d[1] = 0.0;
3700:
3701: /* minimize along first direction V(*,1) */
3702: #ifdef DEBUGPRAX
3703: printf(" Minimize along the first direction V(*,1). illc=%d\n",illc);
3704: /* fprintf(ficlog," Minimize along the first direction V(*,1).\n"); */
3705: #endif
3706: #ifdef DEBUGPRAX2
3707: printf("praxis4 macheps=%14g h=%14g step=%14g small=%14g t=%14g\n",macheps,h, h0,small_windows, t);
3708: #endif
3709: /* min(0, 2, &d[0], &s, fx, 0); /\* mac heps not global *\/ */
3710: minny(1, 2, &d[1], &s, fx, 0); /* mac heps not global */
3711: #ifdef DEBUGPRAX
3712: printf("praxis5 macheps=%14g h=%14g looks at sign of s=%14g fx=%14g\n",macheps,h, s,fx);
3713: #endif
3714: if (s <= 0.0)
3715: /* for (i=0; i < n; i++) */
3716: for (i=1; i <= n; i++)
3717: v[i][1] = -v[i][1];
3718: /* if ((sf <= (0.9 * d[0])) || ((0.9 * sf) >= d[0])) */
3719: if ((sf <= (0.9 * d[1])) || ((0.9 * sf) >= d[1]))
3720: /* for (i=1; i<n; i++) */
3721: for (i=2; i<=n; i++)
3722: d[i] = 0.0;
3723: /* for (k=1; k<n; k++) { */
3724: for (k=2; k<=n; k++) {
3725: /*
3726: The inner loop starts here.
3727: */
3728: #ifdef DEBUGPRAX
3729: printf(" The inner loop here from k=%d to n=%d.\n",k,n);
3730: /* fprintf(ficlog," The inner loop here from k=%d to n=%d.\n",k,n); */
3731: #endif
3732: /* for (i=0; i<n; i++) */
3733: for (i=1; i<=n; i++)
3734: y[i] = x[i];
3735: sf = fx;
3736: #ifdef DEBUGPRAX
3737: printf(" illc=%d and kt=%d and ktm=%d\n", illc, kt, ktm);
3738: #endif
3739: illc = illc || (kt > 0);
3740: next:
3741: kl = k;
3742: df = 0.0;
3743: if (illc) { /* random step to get off resolution valley */
3744: #ifdef DEBUGPRAX
3745: printf(" A random step follows, to avoid resolution valleys.\n");
3746: matprint(" before rand, vectors:",v,n,n);
3747: #endif
3748: for (i=1; i<=n; i++) {
3749: #ifdef NOBRENTRAND
3750: r = drandom();
3751: #else
3752: seed=i;
3753: /* seed=i+1; */
3754: #ifdef DEBUGRAND
3755: printf(" Random seed=%d, brent i=%d",seed,i); /* YYYY i=5 j=1 vji= -0.0001170073 */
3756: #endif
3757: r = randbrent ( &seed );
3758: #endif
3759: #ifdef DEBUGRAND
3760: printf(" Random r=%.7g \n",r);
3761: #endif
3762: z[i] = (0.1 * ldt + t2 * pow(10.0,(double)kt)) * (r - 0.5);
3763: /* z[i] = (0.1 * ldt + t2 * pow(10.0,(double)kt)) * (drandom() - 0.5); */
3764:
3765: s = z[i];
3766: for (j=1; j <= n; j++)
3767: x[j] += s * v[j][i];
3768: }
3769: #ifdef DEBUGRAND
3770: matprint(" after rand, vectors:",v,n,n);
3771: #endif
3772: #ifdef NR_SHIFT
3773: fx = (*fun)((x-1), n);
3774: #else
3775: fx = (*fun)(x, n);
3776: #endif
3777: /* fx = (*func) ( (x-1) ); *//* This for func which is computed from x[1] and not from x[0] xm1=(x-1)*/
3778: nf++;
3779: }
3780: /* minimize along non-conjugate directions */
3781: #ifdef DEBUGPRAX
3782: printf(" Minimize along the 'non-conjugate' directions (dots printed) V(*,%d),...,V(*,%d).\n",k,n);
3783: /* fprintf(ficlog," Minimize along the 'non-conjugate' directions (dots printed) V(*,%d),...,V(*,%d).\n",k,n); */
3784: #endif
3785: /* for (k2=k; k2<n; k2++) { /\* Be careful here k2 <=n ? *\/ */
3786: for (k2=k; k2<=n; k2++) { /* Be careful here k2 <=n ? */
3787: sl = fx;
3788: s = 0.0;
3789: #ifdef DEBUGPRAX
3790: printf(" Minimize along the 'NON-CONJUGATE' true direction k2=%14d fx=%14.7f\n",k2, fx);
3791: matprint(" before min vectors:",v,n,n);
3792: #endif
3793: /* min(k2, 2, &d[k2], &s, fx, 0); */
3794: /* jsearch=k2-1; */
3795: /* min(jsearch, 2, &d[jsearch], &s, fx, 0); */
3796: minny(k2, 2, &d[k2], &s, fx, 0);
3797: #ifdef DEBUGPRAX
3798: printf(" . D(%d)=%14.7f d[k2]=%14.7f z[k2]=%14.7f illc=%14d fx=%14.7f\n",k2,d[k2],d[k2],z[k2],illc,fx);
3799: #endif
3800: if (illc) {
3801: /* double szk = s + z[k2]; */
3802: /* s = d[k2] * szk*szk; */
3803: double szk = s + z[k2];
3804: s = d[k2] * szk*szk;
3805: }
3806: else
3807: s = sl - fx;
3808: /* if (df < s) { */
3809: if (df <= s) {
3810: df = s;
3811: kl = k2;
3812: #ifdef DEBUGPRAX
3813: printf(" df=%.7g and choose kl=%d \n",df,kl); /* UUUU */
3814: #endif
3815: }
3816: } /* end loop k2 */
3817: /*
3818: If there was not much improvement on the first try, set
3819: ILLC = true and start the inner loop again.
3820: */
3821: #ifdef DEBUGPRAX
3822: printf(" If there was not much improvement on the first try, set ILLC = true and start the inner loop again. illc=%d\n",illc);
3823: /* fprintf(ficlog," If there was not much improvement on the first try, set ILLC = true and start the inner loop again.\n"); */
3824: #endif
3825: if (!illc && (df < fabs(100.0 * (macheps) * fx))) {
3826: #ifdef DEBUGPRAX
3827: printf("\n NO SUCCESS because DF is small, starts inner loop with same K(=%d), fabs( 100.0 * machep(=%.10e) * fx(=%.9e) )=%.9e > df(=%.9e) break illc=%d\n", k, macheps, fx, fabs ( 100.0 * macheps * fx ), df, illc);
3828: #endif
3829: illc = 1;
3830: goto next;
3831: }
3832: #ifdef DEBUGPRAX
3833: printf("\n SUCCESS, BREAKS inner loop K(=%d) because DF is big, fabs( 100.0 * machep(=%.10e) * fx(=%.9e) )=%.9e <= df(=%.9e) break illc=%d\n", k, macheps, fx, fabs ( 100.0 * macheps * fx ), df, illc);
3834: #endif
3835:
3836: /* if ((k == 1) && (prin > 1)){ /\* be careful k=2 *\/ */
3837: if ((k == 2) && (prin > 1)){ /* be careful k=2 */
3838: #ifdef DEBUGPRAX
3839: printf(" NEW D The second difference array d:\n" );
3840: /* fprintf(ficlog, " NEW D The second difference array d:\n" ); */
3841: #endif
3842: vecprint(" NEW D The second difference array d:",d,n);
3843: }
3844: /* minimize along conjugate directions */
3845: /*
3846: Minimize along the "conjugate" directions V(*,1),...,V(*,K-1).
3847: */
3848: #ifdef DEBUGPRAX
3849: printf("Minimize along the 'conjugate' directions V(*,1),...,V(*,K-1=%d).\n",k-1);
3850: /* fprintf(ficlog,"Minimize along the 'conjugate' directions V(*,1),...,V(*,K-1=%d).\n",k-1); */
3851: #endif
3852: /* for (k2=0; k2<=k-1; k2++) { */
3853: for (k2=1; k2<=k-1; k2++) {
3854: s = 0.0;
3855: /* min(k2-1, 2, &d[k2-1], &s, fx, 0); */
3856: minny(k2, 2, &d[k2], &s, fx, 0);
3857: }
3858: f1 = fx;
3859: fx = sf;
3860: lds = 0.0;
3861: /* for (i=0; i<n; i++) { */
3862: for (i=1; i<=n; i++) {
3863: sl = x[i];
3864: x[i] = y[i];
3865: y[i] = sl - y[i];
3866: sl = y[i];
3867: lds = lds + sl*sl;
3868: }
3869: lds = sqrt(lds);
3870: #ifdef DEBUGPRAX
3871: printf("Minimization done 'conjugate', shifted all points, computed lds=%.8f\n",lds);
3872: #endif
3873: /*
3874: Discard direction V(*,kl).
3875:
3876: If no random step was taken, V(*,KL) is the "non-conjugate"
3877: direction along which the greatest improvement was made.
3878: */
3879: if (lds > small_windows) {
3880: #ifdef DEBUGPRAX
3881: printf("lds big enough to throw direction V(*,kl=%d). If no random step was taken, V(*,KL) is the 'non-conjugate' direction along which the greatest improvement was made.\n",kl);
3882: matprint(" before shift new conjugate vectors:",v,n,n);
3883: #endif
3884: for (i=kl-1; i>=k; i--) {
3885: /* for (j=0; j < n; j++) */
3886: for (j=1; j <= n; j++)
3887: /* v[j][i+1] = v[j][i]; */ /* This is v[j][i+1]=v[j][i] i=kl-1 to k */
3888: v[j][i+1] = v[j][i]; /* This is v[j][i+1]=v[j][i] i=kl-1 to k */
3889: /* v[j][i+1] = v[j][i]; */
3890: /* d[i+1] = d[i];*/ /* last is d[k+1]= d[k] */
3891: d[i+1] = d[i]; /* last is d[k]= d[k-1] */
3892: }
3893: #ifdef DEBUGPRAX
3894: matprint(" after shift new conjugate vectors:",v,n,n);
3895: #endif /* d[k] = 0.0; */
3896: d[k] = 0.0;
3897: for (i=1; i <= n; i++)
3898: v[i][k] = y[i] / lds;
3899: /* v[i][k] = y[i] / lds; */
3900: #ifdef DEBUGPRAX
3901: printf("Minimize along the new 'conjugate' direction V(*,k=%d), which is the normalized vector: (new x) - (old x). d2=%14.7g lds=%.10f\n",k,d[k],lds);
3902: /* fprintf(ficlog,"Minimize along the new 'conjugate' direction V(*,k=%d), which is the normalized vector: (new x) - (old x).\n",k); */
3903: matprint(" before min new conjugate vectors:",v,n,n);
3904: #endif
3905: /* min(k-1, 4, &d[k-1], &lds, f1, 1); */
3906: minny(k, 4, &d[k], &lds, f1, 1);
3907: #ifdef DEBUGPRAX
3908: printf(" after min d(k)=%d %.7g lds=%14f\n",k,d[k],lds);
3909: matprint(" after min vectors:",v,n,n);
3910: #endif
3911: if (lds <= 0.0) {
3912: lds = -lds;
3913: #ifdef DEBUGPRAX
3914: printf(" lds changed sign lds=%.14f k=%d\n",lds,k);
3915: #endif
3916: /* for (i=0; i<n; i++) */
3917: /* v[i][k] = -v[i][k]; */
3918: for (i=1; i<=n; i++)
3919: v[i][k] = -v[i][k];
3920: }
3921: }
3922: ldt = ldfac * ldt;
3923: if (ldt < lds)
3924: ldt = lds;
3925: if (prin > 0){
3926: #ifdef DEBUGPRAX
3927: printf(" k=%d",k);
3928: /* fprintf(ficlog," k=%d",k); */
3929: #endif
3930: print2();/* n, x, prin, fx, nf, nl ); */
3931: }
3932: t2 = 0.0;
3933: /* for (i=0; i<n; i++) */
3934: for (i=1; i<=n; i++)
3935: t2 += x[i]*x[i];
3936: t2 = m2 * sqrt(t2) + t;
3937: /*
3938: See whether the length of the step taken since starting the
3939: inner loop exceeds half the tolerance.
3940: */
3941: #ifdef DEBUGPRAX
3942: printf("See if step length exceeds half the tolerance.\n"); /* ZZZZZ */
3943: /* fprintf(ficlog,"See if step length exceeds half the tolerance.\n"); */
3944: #endif
3945: if (ldt > (0.5 * t2))
3946: kt = 0;
3947: else
3948: kt++;
3949: #ifdef DEBUGPRAX
3950: printf("if kt=%d >? ktm=%d gotoL2 loop\n",kt,ktm);
3951: #endif
3952: if (kt > ktm){
3953: if ( 0 < prin ){
3954: /* printf("\nr8vec_print\n X:\n"); */
3955: /* fprintf(ficlog,"\nr8vec_print\n X:\n"); */
3956: vecprint ("END X:", x, n );
3957: }
3958: goto fret;
3959: }
3960: #ifdef DEBUGPRAX
3961: matprint(" end of L2 loop vectors:",v,n,n);
3962: #endif
3963:
3964: }
3965: /* printf("The inner loop ends here.\n"); */
3966: /* fprintf(ficlog,"The inner loop ends here.\n"); */
3967: /*
3968: The inner loop ends here.
3969:
3970: Try quadratic extrapolation in case we are in a curved valley.
3971: */
3972: #ifdef DEBUGPRAX
3973: printf("Try QUAD ratic extrapolation in case we are in a curved valley.\n");
3974: #endif
3975: /* try quadratic extrapolation in case */
3976: /* we are stuck in a curved valley */
3977: quad();
3978: dn = 0.0;
3979: /* for (i=0; i<n; i++) { */
3980: for (i=1; i<=n; i++) {
3981: d[i] = 1.0 / sqrt(d[i]);
3982: if (dn < d[i])
3983: dn = d[i];
3984: }
3985: if (prin > 2)
3986: matprint(" NEW DIRECTIONS vectors:",v,n,n);
3987: /* for (j=0; j<n; j++) { */
3988: for (j=1; j<=n; j++) {
3989: s = d[j] / dn;
3990: /* for (i=0; i < n; i++) */
3991: for (i=1; i <= n; i++)
3992: v[i][j] *= s;
3993: }
3994:
3995: if (scbd > 1.0) { /* scale axis to reduce condition number */
3996: #ifdef DEBUGPRAX
3997: printf("Scale the axes to try to reduce the condition number.\n");
3998: #endif
3999: /* fprintf(ficlog,"Scale the axes to try to reduce the condition number.\n"); */
4000: s = vlarge;
4001: /* for (i=0; i<n; i++) { */
4002: for (i=1; i<=n; i++) {
4003: sl = 0.0;
4004: /* for (j=0; j < n; j++) */
4005: for (j=1; j <= n; j++)
4006: sl += v[i][j]*v[i][j];
4007: z[i] = sqrt(sl);
4008: if (z[i] < m4)
4009: z[i] = m4;
4010: if (s > z[i])
4011: s = z[i];
4012: }
4013: /* for (i=0; i<n; i++) { */
4014: for (i=1; i<=n; i++) {
4015: sl = s / z[i];
4016: z[i] = 1.0 / sl;
4017: if (z[i] > scbd) {
4018: sl = 1.0 / scbd;
4019: z[i] = scbd;
4020: }
4021: }
4022: }
4023: for (i=1; i<=n; i++)
4024: /* for (j=0; j<=i-1; j++) { */
4025: /* for (j=1; j<=i; j++) { */
4026: for (j=1; j<=i-1; j++) {
4027: s = v[i][j];
4028: v[i][j] = v[j][i];
4029: v[j][i] = s;
4030: }
4031: #ifdef DEBUGPRAX
4032: printf(" Calculate a new set of orthogonal directions before repeating the main loop.\n Transpose V for MINFIT:...\n");
4033: #endif
4034: /*
4035: MINFIT finds the singular value decomposition of V.
4036:
4037: This gives the principal values and principal directions of the
4038: approximating quadratic form without squaring the condition number.
4039: */
4040: #ifdef DEBUGPRAX
4041: printf(" MINFIT finds the singular value decomposition of V. \n This gives the principal values and principal directions of the\n approximating quadratic form without squaring the condition number...\n");
4042: #endif
4043:
4044: minfit(n, macheps, vsmall, v, d);
4045: /* for(i=0; i<n;i++)printf(" %14.7g",d[i]); */
4046: /* v is overwritten with R. */
4047: /*
4048: Unscale the axes.
4049: */
4050: if (scbd > 1.0) {
4051: #ifdef DEBUGPRAX
4052: printf(" Unscale the axes.\n");
4053: #endif
4054: /* for (i=0; i<n; i++) { */
4055: for (i=1; i<=n; i++) {
4056: s = z[i];
4057: /* for (j=0; j<n; j++) */
4058: for (j=1; j<=n; j++)
4059: v[i][j] *= s;
4060: }
4061: /* for (i=0; i<n; i++) { */
4062: for (i=1; i<=n; i++) {
4063: s = 0.0;
4064: /* for (j=0; j<n; j++) */
4065: for (j=1; j<=n; j++)
4066: s += v[j][i]*v[j][i];
4067: s = sqrt(s);
4068: d[i] *= s;
4069: s = 1.0 / s;
4070: /* for (j=0; j<n; j++) */
4071: for (j=1; j<=n; j++)
4072: v[j][i] *= s;
4073: }
4074: }
4075: /* for (i=0; i<n; i++) { */
4076: double dni; /* added for compatibility with buckhardt but not brent */
4077: for (i=1; i<=n; i++) {
4078: dni=dn*d[i]; /* added for compatibility with buckhardt but not brent */
4079: if ((dn * d[i]) > large)
4080: d[i] = vsmall;
4081: else if ((dn * d[i]) < small_windows)
4082: d[i] = vlarge;
4083: else
4084: d[i] = 1.0 / dni / dni; /* added for compatibility with buckhardt but not brent */
4085: /* d[i] = pow(dn * d[i],-2.0); */
4086: }
4087: #ifdef DEBUGPRAX
4088: vecprint ("\n Before sort Eigenvalues of a:",d,n );
4089: #endif
4090:
4091: sort(); /* the new eigenvalues and eigenvectors */
4092: #ifdef DEBUGPRAX
4093: vecprint( " After sort the eigenvalues ....\n", d, n);
4094: matprint( " After sort the eigenvectors....\n", v, n,n);
4095: #endif
4096: #ifdef DEBUGPRAX
4097: printf(" Determine the smallest eigenvalue.\n");
4098: #endif
4099: /* dmin = d[n-1]; */
4100: dmin = d[n];
4101: if (dmin < small_windows)
4102: dmin = small_windows;
4103: /*
4104: The ratio of the smallest to largest eigenvalue determines whether
4105: the system is ill conditioned.
4106: */
4107:
4108: /* illc = (m2 * d[0]) > dmin; */
4109: illc = (m2 * d[1]) > dmin;
4110: #ifdef DEBUGPRAX
4111: printf(" The ratio of the smallest to largest eigenvalue determines whether\n the system is ill conditioned=%d . dmin=%.10lf < m2=%.10lf * d[1]=%.10lf \n",illc, dmin,m2, d[1]);
4112: #endif
4113:
4114: if ((prin > 2) && (scbd > 1.0))
4115: vecprint("\n The scale factors:",z,n);
4116: if (prin > 2)
4117: vecprint(" Principal values (EIGEN VALUES OF A) of the quadratic form:",d,n);
4118: if (prin > 2)
4119: matprint(" The principal axes (EIGEN VECTORS OF A:",v,n, n);
4120:
4121: if ((maxfun > 0) && (nf > maxfun)) {
4122: if (prin)
4123: printf("\n... maximum number of function calls reached ...\n");
4124: goto fret;
4125: }
4126: #ifdef DEBUGPRAX
4127: printf("Goto main loop\n");
4128: #endif
4129: goto mloop; /* back to main loop */
4130:
4131: fret:
4132: if (prin > 0) {
4133: vecprint("\n X:", x, n);
4134: /* printf("\n... ChiSq reduced to %20.10e ...\n", fx); */
4135: /* printf("... after %20u function calls.\n", nf); */
4136: }
4137: free_vector(d, 1, n);
4138: free_vector(y, 1, n);
4139: free_vector(z, 1, n);
4140: free_vector(q0, 1, n);
4141: free_vector(q1, 1, n);
4142: free_matrix(v, 1, n, 1, n);
4143: /* double *d, *y, *z, */
4144: /* *q0, *q1, **v; */
4145: free_vector(tflin, 1, n);
4146: /* double *tflin; /\* used in flin: return (*fun)(tflin, n); *\/ */
4147: free_vector(e, 1, n);
4148: /* double *e; /\* used in minfit, don't konw how to free memory and thus made global *\/ */
4149:
4150: return(fx);
4151: }
4152:
4153: /* end praxis gegen */
4154:
4155: /*************** powell ************************/
4156: /*
4157: Minimization of a function func of n variables. Input consists in an initial starting point
4158: p[1..n] ; an initial matrix xi[1..n][1..n] whose columns contain the initial set of di-
4159: rections (usually the n unit vectors); and ftol, the fractional tolerance in the function value
4160: such that failure to decrease by more than this amount in one iteration signals doneness. On
4161: output, p is set to the best point found, xi is the then-current direction set, fret is the returned
4162: function value at p , and iter is the number of iterations taken. The routine linmin is used.
4163: */
4164: #ifdef LINMINORIGINAL
4165: #else
4166: int *flatdir; /* Function is vanishing in that direction */
4167: int flat=0, flatd=0; /* Function is vanishing in that direction */
4168: #endif
4169: void powell(double p[], double **xi, int n, double ftol, int *iter, double *fret,
4170: double (*func)(double []))
4171: {
4172: #ifdef LINMINORIGINAL
4173: void linmin(double p[], double xi[], int n, double *fret,
4174: double (*func)(double []));
4175: #else
4176: void linmin(double p[], double xi[], int n, double *fret,
4177: double (*func)(double []),int *flat);
4178: #endif
4179: int i,ibig,j,jk,k;
4180: double del,t,*pt,*ptt,*xit;
4181: double directest;
4182: double fp,fptt;
4183: double *xits;
4184: int niterf, itmp;
4185: int Bigter=0, nBigterf=1;
4186:
4187: pt=vector(1,n);
4188: ptt=vector(1,n);
4189: xit=vector(1,n);
4190: xits=vector(1,n);
4191: *fret=(*func)(p);
4192: for (j=1;j<=n;j++) pt[j]=p[j];
4193: rcurr_time = time(NULL);
4194: fp=(*fret); /* Initialisation */
4195: for (*iter=1;;++(*iter)) {
4196: ibig=0;
4197: del=0.0;
4198: rlast_time=rcurr_time;
4199: rlast_btime=rcurr_time;
4200: /* (void) gettimeofday(&curr_time,&tzp); */
4201: rcurr_time = time(NULL);
4202: curr_time = *localtime(&rcurr_time);
4203: /* 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); */
4204: /* 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); */
4205: /* Bigter=(*iter - *iter % ncovmodel)/ncovmodel +1; /\* Big iteration, i.e on ncovmodel cycle *\/ */
4206: Bigter=(*iter - (*iter-1) % n)/n +1; /* Big iteration, i.e on ncovmodel cycle */
4207: printf("\nPowell iter=%d Big Iter=%d -2*LL=%.12f gain=%.3lg %ld sec. %ld sec.",*iter,Bigter,*fret,fp-*fret, rcurr_time-rlast_time, rcurr_time-rstart_time);fflush(stdout);
4208: fprintf(ficlog,"\nPowell iter=%d Big Iter=%d -2*LL=%.12f gain=%.3lg %ld sec. %ld sec.",*iter,Bigter,*fret,fp-*fret,rcurr_time-rlast_time, rcurr_time-rstart_time); fflush(ficlog);
4209: fprintf(ficrespow,"%d %d %.12f %d",*iter,Bigter, *fret,curr_time.tm_sec-start_time.tm_sec);
4210: fp=(*fret); /* From former iteration or initial value */
4211: for (i=1;i<=n;i++) {
4212: fprintf(ficrespow," %.12lf", p[i]);
4213: }
4214: fprintf(ficrespow,"\n");fflush(ficrespow);
4215: printf("\n#model= 1 + age ");
4216: fprintf(ficlog,"\n#model= 1 + age ");
4217: if(nagesqr==1){
4218: printf(" + age*age ");
4219: fprintf(ficlog," + age*age ");
4220: }
4221: for(j=1;j <=ncovmodel-2;j++){
4222: if(Typevar[j]==0) {
4223: printf(" + V%d ",Tvar[j]);
4224: fprintf(ficlog," + V%d ",Tvar[j]);
4225: }else if(Typevar[j]==1) {
4226: printf(" + V%d*age ",Tvar[j]);
4227: fprintf(ficlog," + V%d*age ",Tvar[j]);
4228: }else if(Typevar[j]==2) {
4229: printf(" + V%d*V%d ",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
4230: fprintf(ficlog," + V%d*V%d ",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
4231: }else if(Typevar[j]==3) {
4232: printf(" + V%d*V%d*age ",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
4233: fprintf(ficlog," + V%d*V%d*age ",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
4234: }
4235: }
4236: printf("\n");
4237: /* printf("12 47.0114589 0.0154322 33.2424412 0.3279905 2.3731903 */
4238: /* 13 -21.5392400 0.1118147 1.2680506 1.2973408 -1.0663662 */
4239: fprintf(ficlog,"\n");
4240: for(i=1,jk=1; i <=nlstate; i++){
4241: for(k=1; k <=(nlstate+ndeath); k++){
4242: if (k != i) {
4243: printf("%d%d ",i,k);
4244: fprintf(ficlog,"%d%d ",i,k);
4245: for(j=1; j <=ncovmodel; j++){
4246: printf("%12.7f ",p[jk]);
4247: fprintf(ficlog,"%12.7f ",p[jk]);
4248: jk++;
4249: }
4250: printf("\n");
4251: fprintf(ficlog,"\n");
4252: }
4253: }
4254: }
4255: if(*iter <=3 && *iter >1){
4256: tml = *localtime(&rcurr_time);
4257: strcpy(strcurr,asctime(&tml));
4258: rforecast_time=rcurr_time;
4259: itmp = strlen(strcurr);
4260: if(strcurr[itmp-1]=='\n') /* Windows outputs with a new line */
4261: strcurr[itmp-1]='\0';
4262: printf("\nConsidering the time needed for the last iteration #%d: %ld seconds,\n",*iter,rcurr_time-rlast_time);
4263: fprintf(ficlog,"\nConsidering the time needed for this last iteration #%d: %ld seconds,\n",*iter,rcurr_time-rlast_time);
4264: for(nBigterf=1;nBigterf<=31;nBigterf+=10){
4265: niterf=nBigterf*ncovmodel;
4266: /* rforecast_time=rcurr_time+(niterf-*iter)*(rcurr_time-rlast_time); */
4267: rforecast_time=rcurr_time+(niterf-*iter)*(rcurr_time-rlast_time);
4268: forecast_time = *localtime(&rforecast_time);
4269: strcpy(strfor,asctime(&forecast_time));
4270: itmp = strlen(strfor);
4271: if(strfor[itmp-1]=='\n')
4272: strfor[itmp-1]='\0';
4273: printf(" - if your program needs %d BIG iterations (%d iterations) to converge, convergence will be \n reached in %s i.e.\n on %s (current time is %s);\n",nBigterf, niterf, asc_diff_time(rforecast_time-rcurr_time,tmpout),strfor,strcurr);
4274: fprintf(ficlog," - if your program needs %d BIG iterations (%d iterations) to converge, convergence will be \n reached in %s i.e.\n on %s (current time is %s);\n",nBigterf, niterf, asc_diff_time(rforecast_time-rcurr_time,tmpout),strfor,strcurr);
4275: }
4276: }
4277: for (i=1;i<=n;i++) { /* For each direction i, maximisation after loading directions */
4278: for (j=1;j<=n;j++) xit[j]=xi[j][i]; /* Directions stored from previous iteration with previous scales. xi is not changed but one dim xit */
4279:
4280: fptt=(*fret); /* Computes likelihood for parameters xit */
4281: #ifdef DEBUG
4282: printf("fret=%lf, %lf, %lf \n", *fret, *fret, *fret);
4283: fprintf(ficlog, "fret=%lf, %lf, %lf \n", *fret, *fret, *fret);
4284: #endif
4285: printf("%d",i);fflush(stdout); /* print direction (parameter) i */
4286: fprintf(ficlog,"%d",i);fflush(ficlog);
4287: #ifdef LINMINORIGINAL
4288: linmin(p,xit,n,fret,func); /* New point i minimizing in direction xit, i has coordinates p[j].*/
4289: /* xit[j] gives the n coordinates of direction i as input.*/
4290: /* *fret gives the maximum value on direction xit */
4291: #else
4292: linmin(p,xit,n,fret,func,&flat); /* Point p[n]. xit[n] has been loaded for direction i as input.*/
4293: flatdir[i]=flat; /* Function is vanishing in that direction i */
4294: #endif
4295: /* Outputs are fret(new point p) p is updated and xit rescaled */
4296: if (fabs(fptt-(*fret)) > del) { /* We are keeping the max gain on each of the n directions */
4297: /* because that direction will be replaced unless the gain del is small */
4298: /* in comparison with the 'probable' gain, mu^2, with the last average direction. */
4299: /* Unless the n directions are conjugate some gain in the determinant may be obtained */
4300: /* with the new direction. */
4301: del=fabs(fptt-(*fret));
4302: ibig=i;
4303: }
4304: #ifdef DEBUG
4305: printf("%d %.12e",i,(*fret));
4306: fprintf(ficlog,"%d %.12e",i,(*fret));
4307: for (j=1;j<=n;j++) {
4308: xits[j]=FMAX(fabs(p[j]-pt[j]),1.e-5);
4309: printf(" x(%d)=%.12e",j,xit[j]);
4310: fprintf(ficlog," x(%d)=%.12e",j,xit[j]);
4311: }
4312: for(j=1;j<=n;j++) {
4313: printf(" p(%d)=%.12e",j,p[j]);
4314: fprintf(ficlog," p(%d)=%.12e",j,p[j]);
4315: }
4316: printf("\n");
4317: fprintf(ficlog,"\n");
4318: #endif
4319: } /* end loop on each direction i */
4320: /* Convergence test will use last linmin estimation (fret) and compare to former iteration (fp) */
4321: /* But p and xit have been updated at the end of linmin, *fret corresponds to new p, xit */
4322: /* New value of last point Pn is not computed, P(n-1) */
4323: for(j=1;j<=n;j++) {
4324: if(flatdir[j] >0){
4325: printf(" p(%d)=%lf flat=%d ",j,p[j],flatdir[j]);
4326: fprintf(ficlog," p(%d)=%lf flat=%d ",j,p[j],flatdir[j]);
4327: }
4328: /* printf("\n"); */
4329: /* fprintf(ficlog,"\n"); */
4330: }
4331: /* if (2.0*fabs(fp-(*fret)) <= ftol*(fabs(fp)+fabs(*fret))) { /\* Did we reach enough precision? *\/ */
4332: if (2.0*fabs(fp-(*fret)) <= ftol) { /* Did we reach enough precision? */
4333: /* We could compare with a chi^2. chisquare(0.95,ddl=1)=3.84 */
4334: /* By adding age*age in a model, the new -2LL should be lower and the difference follows a */
4335: /* a chisquare statistics with 1 degree. To be significant at the 95% level, it should have */
4336: /* decreased of more than 3.84 */
4337: /* By adding age*age and V1*age the gain (-2LL) should be more than 5.99 (ddl=2) */
4338: /* By using V1+V2+V3, the gain should be 7.82, compared with basic 1+age. */
4339: /* By adding 10 parameters more the gain should be 18.31 */
4340:
4341: /* Starting the program with initial values given by a former maximization will simply change */
4342: /* the scales of the directions and the directions, because the are reset to canonical directions */
4343: /* Thus the first calls to linmin will give new points and better maximizations until fp-(*fret) is */
4344: /* under the tolerance value. If the tolerance is very small 1.e-9, it could last long. */
4345: #ifdef DEBUG
4346: int k[2],l;
4347: k[0]=1;
4348: k[1]=-1;
4349: printf("Max: %.12e",(*func)(p));
4350: fprintf(ficlog,"Max: %.12e",(*func)(p));
4351: for (j=1;j<=n;j++) {
4352: printf(" %.12e",p[j]);
4353: fprintf(ficlog," %.12e",p[j]);
4354: }
4355: printf("\n");
4356: fprintf(ficlog,"\n");
4357: for(l=0;l<=1;l++) {
4358: for (j=1;j<=n;j++) {
4359: ptt[j]=p[j]+(p[j]-pt[j])*k[l];
4360: printf("l=%d j=%d ptt=%.12e, xits=%.12e, p=%.12e, xit=%.12e", l,j,ptt[j],xits[j],p[j],xit[j]);
4361: fprintf(ficlog,"l=%d j=%d ptt=%.12e, xits=%.12e, p=%.12e, xit=%.12e", l,j,ptt[j],xits[j],p[j],xit[j]);
4362: }
4363: printf("func(ptt)=%.12e, deriv=%.12e\n",(*func)(ptt),(ptt[j]-p[j])/((*func)(ptt)-(*func)(p)));
4364: fprintf(ficlog,"func(ptt)=%.12e, deriv=%.12e\n",(*func)(ptt),(ptt[j]-p[j])/((*func)(ptt)-(*func)(p)));
4365: }
4366: #endif
4367:
4368: free_vector(xit,1,n);
4369: free_vector(xits,1,n);
4370: free_vector(ptt,1,n);
4371: free_vector(pt,1,n);
4372: return;
4373: } /* enough precision */
4374: if (*iter == ITMAX*n) nrerror("powell exceeding maximum iterations.");
4375: for (j=1;j<=n;j++) { /* Computes the extrapolated point and value f3, P_0 + 2 (P_n-P_0)=2Pn-P0 and xit is direction Pn-P0 */
4376: ptt[j]=2.0*p[j]-pt[j];
4377: xit[j]=p[j]-pt[j]; /* Coordinate j of last direction xi_n=P_n-P_0 */
4378: #ifdef DEBUG
4379: printf("\n %d xit=%12.7g p=%12.7g pt=%12.7g ",j,xit[j],p[j],pt[j]);
4380: #endif
4381: pt[j]=p[j]; /* New P0 is Pn */
4382: }
4383: #ifdef DEBUG
4384: printf("\n");
4385: #endif
4386: fptt=(*func)(ptt); /* f_3 */
4387: #ifdef NODIRECTIONCHANGEDUNTILNITER /* No change in directions until some iterations are done */
4388: if (*iter <=4) {
4389: #else
4390: #endif
4391: #ifdef POWELLNOF3INFF1TEST /* skips test F3 <F1 */
4392: #else
4393: if (fptt < fp) { /* If extrapolated point is better, decide if we keep that new direction or not */
4394: #endif
4395: /* (x1 f1=fp), (x2 f2=*fret), (x3 f3=fptt), (xm fm) */
4396: /* From x1 (P0) distance of x2 is at h and x3 is 2h */
4397: /* Let f"(x2) be the 2nd derivative equal everywhere. */
4398: /* Then the parabolic through (x1,f1), (x2,f2) and (x3,f3) */
4399: /* will reach at f3 = fm + h^2/2 f"m ; f" = (f1 -2f2 +f3 ) / h**2 */
4400: /* Conditional for using this new direction is that mu^2 = (f1-2f2+f3)^2 /2 < del or directest <0 */
4401: /* also lamda^2=(f1-f2)^2/mu² is a parasite solution of powell */
4402: /* For powell, inclusion of this average direction is only if t(del)<0 or del inbetween mu^2 and lambda^2 */
4403: /* t=2.0*(fp-2.0*(*fret)+fptt)*SQR(fp-(*fret)-del)-del*SQR(fp-fptt); */
4404: /* Even if f3 <f1, directest can be negative and t >0 */
4405: /* mu² and del² are equal when f3=f1 */
4406: /* f3 < f1 : mu² < del <= lambda^2 both test are equivalent */
4407: /* f3 < f1 : mu² < lambda^2 < del then directtest is negative and powell t is positive */
4408: /* f3 > f1 : lambda² < mu^2 < del then t is negative and directest >0 */
4409: /* f3 > f1 : lambda² < del < mu^2 then t is positive and directest >0 */
4410: #ifdef NRCORIGINAL
4411: t=2.0*(fp-2.0*(*fret)+fptt)*SQR(fp-(*fret)-del)- del*SQR(fp-fptt); /* Original Numerical Recipes in C*/
4412: #else
4413: t=2.0*(fp-2.0*(*fret)+fptt)*SQR(fp-(*fret)-del); /* Intel compiler doesn't work on one line; bug reported */
4414: t= t- del*SQR(fp-fptt);
4415: #endif
4416: directest = fp-2.0*(*fret)+fptt - 2.0 * del; /* If delta was big enough we change it for a new direction */
4417: #ifdef DEBUG
4418: 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);
4419: 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);
4420: printf("t3= %.12lf, t4= %.12lf, t3*= %.12lf, t4*= %.12lf\n",SQR(fp-(*fret)-del),SQR(fp-fptt),
4421: (fp-(*fret)-del)*(fp-(*fret)-del),(fp-fptt)*(fp-fptt));
4422: fprintf(ficlog,"t3= %.12lf, t4= %.12lf, t3*= %.12lf, t4*= %.12lf\n",SQR(fp-(*fret)-del),SQR(fp-fptt),
4423: (fp-(*fret)-del)*(fp-(*fret)-del),(fp-fptt)*(fp-fptt));
4424: 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);
4425: 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);
4426: #endif
4427: #ifdef POWELLORIGINAL
4428: if (t < 0.0) { /* Then we use it for new direction */
4429: #else /* Not POWELLOriginal but Brouard's */
4430: if (directest*t < 0.0) { /* Contradiction between both tests */
4431: 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);
4432: printf("f1-2f2+f3= %.12lf, f1-f2-del= %.12lf, f1-f3= %.12lf\n",fp-2.0*(*fret)+fptt, fp -(*fret) -del, fp-fptt);
4433: 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);
4434: fprintf(ficlog,"f1-2f2+f3= %.12lf, f1-f2-del= %.12lf, f1-f3= %.12lf\n",fp-2.0*(*fret)+fptt, fp -(*fret) -del, fp-fptt);
4435: }
4436: if (directest < 0.0) { /* Then we use (P0, Pn) for new direction Xi_n or Xi_iBig */
4437: #endif
4438: #ifdef DEBUGLINMIN
4439: printf("Before linmin in direction P%d-P0\n",n);
4440: for (j=1;j<=n;j++) {
4441: printf(" Before xit[%d]= %12.7f p[%d]= %12.7f",j,xit[j],j,p[j]);
4442: fprintf(ficlog," Before xit[%d]= %12.7f p[%d]= %12.7f",j,xit[j],j,p[j]);
4443: if(j % ncovmodel == 0){
4444: printf("\n");
4445: fprintf(ficlog,"\n");
4446: }
4447: }
4448: #endif
4449: #ifdef LINMINORIGINAL
4450: linmin(p,xit,n,fret,func); /* computes minimum on the extrapolated direction: changes p and rescales xit.*/
4451: #else
4452: linmin(p,xit,n,fret,func,&flat); /* computes minimum on the extrapolated direction: changes p and rescales xit.*/
4453: flatdir[i]=flat; /* Function is vanishing in that direction i */
4454: #endif
4455:
4456: #ifdef DEBUGLINMIN
4457: for (j=1;j<=n;j++) {
4458: printf("After xit[%d]= %12.7f p[%d]= %12.7f",j,xit[j],j,p[j]);
4459: fprintf(ficlog,"After xit[%d]= %12.7f p[%d]= %12.7f",j,xit[j],j,p[j]);
4460: if(j % ncovmodel == 0){
4461: printf("\n");
4462: fprintf(ficlog,"\n");
4463: }
4464: }
4465: #endif
4466: for (j=1;j<=n;j++) {
4467: xi[j][ibig]=xi[j][n]; /* Replace direction with biggest decrease by last direction n */
4468: xi[j][n]=xit[j]; /* and this nth direction by the by the average p_0 p_n */
4469: }
4470:
4471: /* #else */
4472: /* for (i=1;i<=n-1;i++) { */
4473: /* for (j=1;j<=n;j++) { */
4474: /* xi[j][i]=xi[j][i+1]; /\* Standard method of conjugate directions, not Powell who changes the nth direction by p0 pn . *\/ */
4475: /* } */
4476: /* } */
4477: /* for (j=1;j<=n;j++) { */
4478: /* xi[j][n]=xit[j]; /\* and this nth direction by the by the average p_0 p_n *\/ */
4479: /* } */
4480: /* /\* for (j=1;j<=n-1;j++) { *\/ */
4481: /* /\* xi[j][1]=xi[j][j+1]; /\\* Standard method of conjugate directions *\\/ *\/ */
4482: /* /\* xi[j][n]=xit[j]; /\\* and this nth direction by the by the average p_0 p_n *\\/ *\/ */
4483: /* /\* } *\/ */
4484: /* #endif */
4485: #ifdef LINMINORIGINAL
4486: #else
4487: for (j=1, flatd=0;j<=n;j++) {
4488: if(flatdir[j]>0)
4489: flatd++;
4490: }
4491: if(flatd >0){
4492: printf("%d flat directions: ",flatd);
4493: fprintf(ficlog,"%d flat directions :",flatd);
4494: for (j=1;j<=n;j++) {
4495: if(flatdir[j]>0){
4496: printf("%d ",j);
4497: fprintf(ficlog,"%d ",j);
4498: }
4499: }
4500: printf("\n");
4501: fprintf(ficlog,"\n");
4502: #ifdef FLATSUP
4503: free_vector(xit,1,n);
4504: free_vector(xits,1,n);
4505: free_vector(ptt,1,n);
4506: free_vector(pt,1,n);
4507: return;
4508: #endif
4509: } /* endif(flatd >0) */
4510: #endif /* LINMINORIGINAL */
4511: printf("Gaining to use new average direction of P0 P%d instead of biggest increase direction %d :\n",n,ibig);
4512: fprintf(ficlog,"Gaining to use new average direction of P0 P%d instead of biggest increase direction %d :\n",n,ibig);
4513:
4514: #ifdef DEBUG
4515: printf("Direction changed last moved %d in place of ibig=%d, new last is the average:\n",n,ibig);
4516: fprintf(ficlog,"Direction changed last moved %d in place of ibig=%d, new last is the average:\n",n,ibig);
4517: for(j=1;j<=n;j++){
4518: printf(" %lf",xit[j]);
4519: fprintf(ficlog," %lf",xit[j]);
4520: }
4521: printf("\n");
4522: fprintf(ficlog,"\n");
4523: #endif
4524: } /* end of t or directest negative */
4525: printf(" Directest is positive, P_n-P_0 does not increase the conjugacy. n=%d\n",n);
4526: fprintf(ficlog," Directest is positive, P_n-P_0 does not increase the conjugacy. n=%d\n",n);
4527: #ifdef POWELLNOF3INFF1TEST
4528: #else
4529: } /* end if (fptt < fp) */
4530: #endif
4531: #ifdef NODIRECTIONCHANGEDUNTILNITER /* No change in drections until some iterations are done */
4532: } /*NODIRECTIONCHANGEDUNTILNITER No change in drections until some iterations are done */
4533: #else
4534: #endif
4535: } /* loop iteration */
4536: }
4537:
4538: /**** Prevalence limit (stable or period prevalence) ****************/
4539:
4540: double **prevalim(double **prlim, int nlstate, double x[], double age, double **oldm, double **savm, double ftolpl, int *ncvyear, int ij, int nres)
4541: {
4542: /**< Computes the prevalence limit in each live state at age x and for covariate combination ij . Nicely done
4543: * (and selected quantitative values in nres)
4544: * by left multiplying the unit
4545: * matrix by transitions matrix until convergence is reached with precision ftolpl
4546: * Wx= Wx-1 Px-1= Wx-2 Px-2 Px-1 = Wx-n Px-n ... Px-2 Px-1 I
4547: * Wx is row vector: population in state 1, population in state 2, population dead
4548: * or prevalence in state 1, prevalence in state 2, 0
4549: * newm is the matrix after multiplications, its rows are identical at a factor.
4550: * Inputs are the parameter, age, a tolerance for the prevalence limit ftolpl.
4551: * Output is prlim.
4552: * Initial matrix pimij
4553: */
4554: /* {0.85204250825084937, 0.13044499163996345, 0.017512500109187184, */
4555: /* 0.090851990222114765, 0.88271245433047185, 0.026435555447413338, */
4556: /* 0, 0 , 1} */
4557: /*
4558: * and after some iteration: */
4559: /* {0.45504275246439968, 0.42731458730878791, 0.11764266022681241, */
4560: /* 0.45201005341706885, 0.42865420071559901, 0.11933574586733192, */
4561: /* 0, 0 , 1} */
4562: /* And prevalence by suppressing the deaths are close to identical rows in prlim: */
4563: /* {0.51571254859325999, 0.4842874514067399, */
4564: /* 0.51326036147820708, 0.48673963852179264} */
4565: /* If we start from prlim again, prlim tends to a constant matrix */
4566:
4567: int i, ii,j,k, k1;
4568: double *min, *max, *meandiff, maxmax,sumnew=0.;
4569: /* double **matprod2(); */ /* test */
4570: double **out, cov[NCOVMAX+1], **pmij(); /* **pmmij is a global variable feeded with oldms etc */
4571: double **newm;
4572: double agefin, delaymax=200. ; /* 100 Max number of years to converge */
4573: int ncvloop=0;
4574: int first=0;
4575:
4576: min=vector(1,nlstate);
4577: max=vector(1,nlstate);
4578: meandiff=vector(1,nlstate);
4579:
4580: /* Starting with matrix unity */
4581: for (ii=1;ii<=nlstate+ndeath;ii++)
4582: for (j=1;j<=nlstate+ndeath;j++){
4583: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
4584: }
4585:
4586: cov[1]=1.;
4587:
4588: /* Even if hstepm = 1, at least one multiplication by the unit matrix */
4589: /* Start at agefin= age, computes the matrix of passage and loops decreasing agefin until convergence is reached */
4590: for(agefin=age-stepm/YEARM; agefin>=age-delaymax; agefin=agefin-stepm/YEARM){
4591: ncvloop++;
4592: newm=savm;
4593: /* Covariates have to be included here again */
4594: cov[2]=agefin;
4595: if(nagesqr==1){
4596: cov[3]= agefin*agefin;
4597: }
4598: /* Model(2) V1 + V2 + V3 + V8 + V7*V8 + V5*V6 + V8*age + V3*age + age*age */
4599: /* total number of covariates of the model nbocc(+)+1 = 8 excepting constant and age and age*age */
4600: for(k1=1;k1<=cptcovt;k1++){ /* loop on model equation (including products) */
4601: if(Typevar[k1]==1 || Typevar[k1]==3){ /* A product with age */
4602: cov[2+nagesqr+k1]=precov[nres][k1]*cov[2];
4603: }else{
4604: cov[2+nagesqr+k1]=precov[nres][k1];
4605: }
4606: }/* End of loop on model equation */
4607:
4608: /* Start of old code (replaced by a loop on position in the model equation */
4609: /* for (k=1; k<=nsd;k++) { /\* For single dummy covariates only of the model *\/ */
4610: /* /\* Here comes the value of the covariate 'ij' after renumbering k with single dummy covariates *\/ */
4611: /* /\* cov[2+nagesqr+TvarsDind[k]]=nbcode[TvarsD[k]][codtabm(ij,TvarsD[k])]; *\/ */
4612: /* cov[2+nagesqr+TvarsDind[k]]=nbcode[TvarsD[k]][codtabm(ij,TnsdVar[TvarsD[k]])]; */
4613: /* /\* model = 1 +age + V1*V3 + age*V1 + V2 + V1 + age*V2 + V3 + V3*age + V1*V2 */
4614: /* * k 1 2 3 4 5 6 7 8 */
4615: /* *cov[] 1 2 3 4 5 6 7 8 9 10 */
4616: /* *TypeVar[k] 2 1 0 0 1 0 1 2 */
4617: /* *Dummy[k] 0 2 0 0 2 0 2 0 */
4618: /* *Tvar[k] 4 1 2 1 2 3 3 5 */
4619: /* *nsd=3 (1) (2) (3) */
4620: /* *TvarsD[nsd] [1]=2 1 3 */
4621: /* *TnsdVar [2]=2 [1]=1 [3]=3 */
4622: /* *TvarsDind[nsd](=k) [1]=3 [2]=4 [3]=6 */
4623: /* *Tage[] [1]=1 [2]=2 [3]=3 */
4624: /* *Tvard[] [1][1]=1 [2][1]=1 */
4625: /* * [1][2]=3 [2][2]=2 */
4626: /* *Tprod[](=k) [1]=1 [2]=8 */
4627: /* *TvarsDp(=Tvar) [1]=1 [2]=2 [3]=3 [4]=5 */
4628: /* *TvarD (=k) [1]=1 [2]=3 [3]=4 [3]=6 [4]=6 */
4629: /* *TvarsDpType */
4630: /* *si model= 1 + age + V3 + V2*age + V2 + V3*age */
4631: /* * nsd=1 (1) (2) */
4632: /* *TvarsD[nsd] 3 2 */
4633: /* *TnsdVar (3)=1 (2)=2 */
4634: /* *TvarsDind[nsd](=k) [1]=1 [2]=3 */
4635: /* *Tage[] [1]=2 [2]= 3 */
4636: /* *\/ */
4637: /* /\* cov[++k1]=nbcode[TvarsD[k]][codtabm(ij,k)]; *\/ */
4638: /* /\* 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)); *\/ */
4639: /* } */
4640: /* for (k=1; k<=nsq;k++) { /\* For single quantitative varying covariates only of the model *\/ */
4641: /* /\* Here comes the value of quantitative after renumbering k with single quantitative covariates *\/ */
4642: /* /\* Tqresult[nres][result_position]= value of the variable at the result_position in the nres resultline *\/ */
4643: /* /\* cov[2+nagesqr+TvarsQind[k]]=Tqresult[nres][k]; *\/ */
4644: /* cov[2+nagesqr+TvarsQind[k]]=Tqresult[nres][resultmodel[nres][k1]] */
4645: /* /\* cov[++k1]=Tqresult[nres][k]; *\/ */
4646: /* /\* 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]); *\/ */
4647: /* } */
4648: /* for (k=1; k<=cptcovage;k++){ /\* For product with age *\/ */
4649: /* if(Dummy[Tage[k]]==2){ /\* dummy with age *\/ */
4650: /* cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,Tvar[Tage[k]])]*cov[2]; */
4651: /* /\* cov[++k1]=nbcode[Tvar[Tage[k]]][codtabm(ij,k)]*cov[2]; *\/ */
4652: /* } else if(Dummy[Tage[k]]==3){ /\* quantitative with age *\/ */
4653: /* cov[2+nagesqr+Tage[k]]=Tqresult[nres][k]; */
4654: /* /\* cov[++k1]=Tqresult[nres][k]; *\/ */
4655: /* } */
4656: /* /\* 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]); *\/ */
4657: /* } */
4658: /* for (k=1; k<=cptcovprod;k++){ /\* For product without age *\/ */
4659: /* /\* 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]); *\/ */
4660: /* if(Dummy[Tvard[k][1]]==0){ */
4661: /* if(Dummy[Tvard[k][2]]==0){ */
4662: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,Tvard[k][1])] * nbcode[Tvard[k][2]][codtabm(ij,Tvard[k][2])]; */
4663: /* /\* cov[++k1]=nbcode[Tvard[k][1]][codtabm(ij,k)] * nbcode[Tvard[k][2]][codtabm(ij,k)]; *\/ */
4664: /* }else{ */
4665: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,Tvard[k][1])] * Tqresult[nres][k]; */
4666: /* /\* cov[++k1]=nbcode[Tvard[k][1]][codtabm(ij,k)] * Tqresult[nres][k]; *\/ */
4667: /* } */
4668: /* }else{ */
4669: /* if(Dummy[Tvard[k][2]]==0){ */
4670: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][2]][codtabm(ij,Tvard[k][2])] * Tqinvresult[nres][Tvard[k][1]]; */
4671: /* /\* cov[++k1]=nbcode[Tvard[k][2]][codtabm(ij,k)] * Tqinvresult[nres][Tvard[k][1]]; *\/ */
4672: /* }else{ */
4673: /* cov[2+nagesqr+Tprod[k]]=Tqinvresult[nres][Tvard[k][1]]* Tqinvresult[nres][Tvard[k][2]]; */
4674: /* /\* cov[++k1]=Tqinvresult[nres][Tvard[k][1]]* Tqinvresult[nres][Tvard[k][2]]; *\/ */
4675: /* } */
4676: /* } */
4677: /* } /\* End product without age *\/ */
4678: /* ENd of old code */
4679: /*printf("ij=%d cptcovprod=%d tvar=%d ", ij, cptcovprod, Tvar[1]);*/
4680: /*printf("ij=%d cov[3]=%lf cov[4]=%lf \n",ij, cov[3],cov[4]);*/
4681: /*printf("ij=%d cov[3]=%lf \n",ij, cov[3]);*/
4682: /* savm=pmij(pmmij,cov,ncovmodel,x,nlstate); */
4683: /* out=matprod2(newm, pmij(pmmij,cov,ncovmodel,x,nlstate),1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm); /\* Bug Valgrind *\/ */
4684: /* age and covariate values of ij are in 'cov' */
4685: out=matprod2(newm, pmij(pmmij,cov,ncovmodel,x,nlstate),1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm); /* Bug Valgrind */
4686:
4687: savm=oldm;
4688: oldm=newm;
4689:
4690: for(j=1; j<=nlstate; j++){
4691: max[j]=0.;
4692: min[j]=1.;
4693: }
4694: for(i=1;i<=nlstate;i++){
4695: sumnew=0;
4696: for(k=1; k<=ndeath; k++) sumnew+=newm[i][nlstate+k];
4697: for(j=1; j<=nlstate; j++){
4698: prlim[i][j]= newm[i][j]/(1-sumnew);
4699: max[j]=FMAX(max[j],prlim[i][j]);
4700: min[j]=FMIN(min[j],prlim[i][j]);
4701: }
4702: }
4703:
4704: maxmax=0.;
4705: for(j=1; j<=nlstate; j++){
4706: meandiff[j]=(max[j]-min[j])/(max[j]+min[j])*2.; /* mean difference for each column */
4707: maxmax=FMAX(maxmax,meandiff[j]);
4708: /* 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); */
4709: } /* j loop */
4710: *ncvyear= (int)age- (int)agefin;
4711: /* printf("maxmax=%lf maxmin=%lf ncvloop=%d, age=%d, agefin=%d ncvyear=%d \n", maxmax, maxmin, ncvloop, (int)age, (int)agefin, *ncvyear); */
4712: if(maxmax < ftolpl){
4713: /* printf("maxmax=%lf ncvloop=%ld, age=%d, agefin=%d ncvyear=%d \n", maxmax, ncvloop, (int)age, (int)agefin, *ncvyear); */
4714: free_vector(min,1,nlstate);
4715: free_vector(max,1,nlstate);
4716: free_vector(meandiff,1,nlstate);
4717: return prlim;
4718: }
4719: } /* agefin loop */
4720: /* After some age loop it doesn't converge */
4721: if(!first){
4722: first=1;
4723: 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);
4724: 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);
4725: }else if (first >=1 && first <10){
4726: 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);
4727: first++;
4728: }else if (first ==10){
4729: 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);
4730: 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");
4731: fprintf(ficlog,"Warning: the stable prevalence no convergence; too many cases, giving up noticing, even in log file\n");
4732: first++;
4733: }
4734:
4735: /* Try to lower 'ftol', for example from 1.e-8 to 6.e-9.\n", ftolpl,
4736: * (int)age, (int)delaymax, (int)agefin, ncvloop,
4737: * (int)age-(int)agefin); */
4738: free_vector(min,1,nlstate);
4739: free_vector(max,1,nlstate);
4740: free_vector(meandiff,1,nlstate);
4741:
4742: return prlim; /* should not reach here */
4743: }
4744:
4745:
4746: /**** Back Prevalence limit (stable or period prevalence) ****************/
4747:
4748: /* 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) */
4749: /* 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) */
4750: double **bprevalim(double **bprlim, double ***prevacurrent, int nlstate, double x[], double age, double ftolpl, int *ncvyear, int ij, int nres)
4751: {
4752: /* Computes the prevalence limit in each live state at age x and for covariate combination ij (<=2**cptcoveff) by left multiplying the unit
4753: matrix by transitions matrix until convergence is reached with precision ftolpl */
4754: /* Wx= Wx-1 Px-1= Wx-2 Px-2 Px-1 = Wx-n Px-n ... Px-2 Px-1 I */
4755: /* Wx is row vector: population in state 1, population in state 2, population dead */
4756: /* or prevalence in state 1, prevalence in state 2, 0 */
4757: /* newm is the matrix after multiplications, its rows are identical at a factor */
4758: /* Initial matrix pimij */
4759: /* {0.85204250825084937, 0.13044499163996345, 0.017512500109187184, */
4760: /* 0.090851990222114765, 0.88271245433047185, 0.026435555447413338, */
4761: /* 0, 0 , 1} */
4762: /*
4763: * and after some iteration: */
4764: /* {0.45504275246439968, 0.42731458730878791, 0.11764266022681241, */
4765: /* 0.45201005341706885, 0.42865420071559901, 0.11933574586733192, */
4766: /* 0, 0 , 1} */
4767: /* And prevalence by suppressing the deaths are close to identical rows in prlim: */
4768: /* {0.51571254859325999, 0.4842874514067399, */
4769: /* 0.51326036147820708, 0.48673963852179264} */
4770: /* If we start from prlim again, prlim tends to a constant matrix */
4771:
4772: int i, ii,j, k1;
4773: int first=0;
4774: double *min, *max, *meandiff, maxmax,sumnew=0.;
4775: /* double **matprod2(); */ /* test */
4776: double **out, cov[NCOVMAX+1], **bmij();
4777: double **newm;
4778: double **dnewm, **doldm, **dsavm; /* for use */
4779: double **oldm, **savm; /* for use */
4780:
4781: double agefin, delaymax=200. ; /* 100 Max number of years to converge */
4782: int ncvloop=0;
4783:
4784: min=vector(1,nlstate);
4785: max=vector(1,nlstate);
4786: meandiff=vector(1,nlstate);
4787:
4788: dnewm=ddnewms; doldm=ddoldms; dsavm=ddsavms;
4789: oldm=oldms; savm=savms;
4790:
4791: /* Starting with matrix unity */
4792: for (ii=1;ii<=nlstate+ndeath;ii++)
4793: for (j=1;j<=nlstate+ndeath;j++){
4794: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
4795: }
4796:
4797: cov[1]=1.;
4798:
4799: /* Even if hstepm = 1, at least one multiplication by the unit matrix */
4800: /* Start at agefin= age, computes the matrix of passage and loops decreasing agefin until convergence is reached */
4801: /* for(agefin=age+stepm/YEARM; agefin<=age+delaymax; agefin=agefin+stepm/YEARM){ /\* A changer en age *\/ */
4802: /* for(agefin=age; agefin<AGESUP; agefin=agefin+stepm/YEARM){ /\* A changer en age *\/ */
4803: for(agefin=age; agefin<FMIN(AGESUP,age+delaymax); agefin=agefin+stepm/YEARM){ /* A changer en age */
4804: ncvloop++;
4805: newm=savm; /* oldm should be kept from previous iteration or unity at start */
4806: /* newm points to the allocated table savm passed by the function it can be written, savm could be reallocated */
4807: /* Covariates have to be included here again */
4808: cov[2]=agefin;
4809: if(nagesqr==1){
4810: cov[3]= agefin*agefin;;
4811: }
4812: for(k1=1;k1<=cptcovt;k1++){ /* loop on model equation (including products) */
4813: if(Typevar[k1]==1 || Typevar[k1]==3){ /* A product with age */
4814: cov[2+nagesqr+k1]=precov[nres][k1]*cov[2];
4815: }else{
4816: cov[2+nagesqr+k1]=precov[nres][k1];
4817: }
4818: }/* End of loop on model equation */
4819:
4820: /* Old code */
4821:
4822: /* for (k=1; k<=nsd;k++) { /\* For single dummy covariates only *\/ */
4823: /* /\* Here comes the value of the covariate 'ij' after renumbering k with single dummy covariates *\/ */
4824: /* cov[2+nagesqr+TvarsDind[k]]=nbcode[TvarsD[k]][codtabm(ij,TvarsD[k])]; */
4825: /* /\* 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)); *\/ */
4826: /* } */
4827: /* /\* for (k=1; k<=cptcovn;k++) { *\/ */
4828: /* /\* /\\* cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,Tvar[k])]; *\\/ *\/ */
4829: /* /\* cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,k)]; *\/ */
4830: /* /\* /\\* 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])]); *\\/ *\/ */
4831: /* /\* } *\/ */
4832: /* for (k=1; k<=nsq;k++) { /\* For single varying covariates only *\/ */
4833: /* /\* Here comes the value of quantitative after renumbering k with single quantitative covariates *\/ */
4834: /* cov[2+nagesqr+TvarsQind[k]]=Tqresult[nres][k]; */
4835: /* /\* 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]); *\/ */
4836: /* } */
4837: /* /\* for (k=1; k<=cptcovage;k++) cov[2+nagesqr+Tage[k]]=nbcode[Tvar[k]][codtabm(ij,k)]*cov[2]; *\/ */
4838: /* /\* for (k=1; k<=cptcovprod;k++) /\\* Useless *\\/ *\/ */
4839: /* /\* /\\* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,Tvard[k][1])] * nbcode[Tvard[k][2]][codtabm(ij,Tvard[k][2])]; *\\/ *\/ */
4840: /* /\* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)] * nbcode[Tvard[k][2]][codtabm(ij,k)]; *\/ */
4841: /* for (k=1; k<=cptcovage;k++){ /\* For product with age *\/ */
4842: /* /\* if(Dummy[Tvar[Tage[k]]]== 2){ /\\* dummy with age *\\/ ERROR ???*\/ */
4843: /* if(Dummy[Tage[k]]== 2){ /\* dummy with age *\/ */
4844: /* cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,Tvar[Tage[k]])]*cov[2]; */
4845: /* } else if(Dummy[Tage[k]]== 3){ /\* quantitative with age *\/ */
4846: /* cov[2+nagesqr+Tage[k]]=Tqresult[nres][k]; */
4847: /* } */
4848: /* /\* 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]); *\/ */
4849: /* } */
4850: /* for (k=1; k<=cptcovprod;k++){ /\* For product without age *\/ */
4851: /* /\* 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]); *\/ */
4852: /* if(Dummy[Tvard[k][1]]==0){ */
4853: /* if(Dummy[Tvard[k][2]]==0){ */
4854: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,Tvard[k][1])] * nbcode[Tvard[k][2]][codtabm(ij,Tvard[k][2])]; */
4855: /* }else{ */
4856: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,Tvard[k][1])] * Tqresult[nres][k]; */
4857: /* } */
4858: /* }else{ */
4859: /* if(Dummy[Tvard[k][2]]==0){ */
4860: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][2]][codtabm(ij,Tvard[k][2])] * Tqinvresult[nres][Tvard[k][1]]; */
4861: /* }else{ */
4862: /* cov[2+nagesqr+Tprod[k]]=Tqinvresult[nres][Tvard[k][1]]* Tqinvresult[nres][Tvard[k][2]]; */
4863: /* } */
4864: /* } */
4865: /* } */
4866:
4867: /*printf("ij=%d cptcovprod=%d tvar=%d ", ij, cptcovprod, Tvar[1]);*/
4868: /*printf("ij=%d cov[3]=%lf cov[4]=%lf \n",ij, cov[3],cov[4]);*/
4869: /*printf("ij=%d cov[3]=%lf \n",ij, cov[3]);*/
4870: /* savm=pmij(pmmij,cov,ncovmodel,x,nlstate); */
4871: /* out=matprod2(newm, pmij(pmmij,cov,ncovmodel,x,nlstate),1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm); /\* Bug Valgrind *\/ */
4872: /* ij should be linked to the correct index of cov */
4873: /* age and covariate values ij are in 'cov', but we need to pass
4874: * ij for the observed prevalence at age and status and covariate
4875: * number: prevacurrent[(int)agefin][ii][ij]
4876: */
4877: /* 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 *\/ */
4878: /* 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 *\/ */
4879: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, bmij(pmmij,cov,ncovmodel,x,nlstate,prevacurrent,ij)); /* Bug Valgrind */
4880: /* if((int)age == 86 || (int)age == 87){ */
4881: /* printf(" Backward prevalim age=%d agefin=%d \n", (int) age, (int) agefin); */
4882: /* for(i=1; i<=nlstate+ndeath; i++) { */
4883: /* printf("%d newm= ",i); */
4884: /* for(j=1;j<=nlstate+ndeath;j++) { */
4885: /* printf("%f ",newm[i][j]); */
4886: /* } */
4887: /* printf("oldm * "); */
4888: /* for(j=1;j<=nlstate+ndeath;j++) { */
4889: /* printf("%f ",oldm[i][j]); */
4890: /* } */
4891: /* printf(" bmmij "); */
4892: /* for(j=1;j<=nlstate+ndeath;j++) { */
4893: /* printf("%f ",pmmij[i][j]); */
4894: /* } */
4895: /* printf("\n"); */
4896: /* } */
4897: /* } */
4898: savm=oldm;
4899: oldm=newm;
4900:
4901: for(j=1; j<=nlstate; j++){
4902: max[j]=0.;
4903: min[j]=1.;
4904: }
4905: for(j=1; j<=nlstate; j++){
4906: for(i=1;i<=nlstate;i++){
4907: /* bprlim[i][j]= newm[i][j]/(1-sumnew); */
4908: bprlim[i][j]= newm[i][j];
4909: max[i]=FMAX(max[i],bprlim[i][j]); /* Max in line */
4910: min[i]=FMIN(min[i],bprlim[i][j]);
4911: }
4912: }
4913:
4914: maxmax=0.;
4915: for(i=1; i<=nlstate; i++){
4916: meandiff[i]=(max[i]-min[i])/(max[i]+min[i])*2.; /* mean difference for each column, could be nan! */
4917: maxmax=FMAX(maxmax,meandiff[i]);
4918: /* 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); */
4919: } /* i loop */
4920: *ncvyear= -( (int)age- (int)agefin);
4921: /* printf("Back maxmax=%lf ncvloop=%d, age=%d, agefin=%d ncvyear=%d \n", maxmax, ncvloop, (int)age, (int)agefin, *ncvyear); */
4922: if(maxmax < ftolpl){
4923: /* printf("OK Back maxmax=%lf ncvloop=%d, age=%d, agefin=%d ncvyear=%d \n", maxmax, ncvloop, (int)age, (int)agefin, *ncvyear); */
4924: free_vector(min,1,nlstate);
4925: free_vector(max,1,nlstate);
4926: free_vector(meandiff,1,nlstate);
4927: return bprlim;
4928: }
4929: } /* agefin loop */
4930: /* After some age loop it doesn't converge */
4931: if(!first){
4932: first=1;
4933: 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\
4934: 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);
4935: }
4936: 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\
4937: 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);
4938: /* 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); */
4939: free_vector(min,1,nlstate);
4940: free_vector(max,1,nlstate);
4941: free_vector(meandiff,1,nlstate);
4942:
4943: return bprlim; /* should not reach here */
4944: }
4945:
4946: /*************** transition probabilities ***************/
4947:
4948: double **pmij(double **ps, double *cov, int ncovmodel, double *x, int nlstate )
4949: {
4950: /* According to parameters values stored in x and the covariate's values stored in cov,
4951: computes the probability to be observed in state j (after stepm years) being in state i by appying the
4952: model to the ncovmodel covariates (including constant and age).
4953: lnpijopii=ln(pij/pii)= aij+bij*age+cij*v1+dij*v2+... = sum_nc=1^ncovmodel xij(nc)*cov[nc]
4954: and, according on how parameters are entered, the position of the coefficient xij(nc) of the
4955: ncth covariate in the global vector x is given by the formula:
4956: j<i nc+((i-1)*(nlstate+ndeath-1)+j-1)*ncovmodel
4957: j>=i nc + ((i-1)*(nlstate+ndeath-1)+(j-2))*ncovmodel
4958: Computes ln(pij/pii) (lnpijopii), deduces pij/pii by exponentiation,
4959: sums on j different of i to get 1-pii/pii, deduces pii, and then all pij.
4960: Outputs ps[i][j] or probability to be observed in j being in i according to
4961: the values of the covariates cov[nc] and corresponding parameter values x[nc+shiftij]
4962: Sum on j ps[i][j] should equal to 1.
4963: */
4964: double s1, lnpijopii;
4965: /*double t34;*/
4966: int i,j, nc, ii, jj;
4967:
4968: for(i=1; i<= nlstate; i++){
4969: for(j=1; j<i;j++){
4970: for (nc=1, lnpijopii=0.;nc <=ncovmodel; nc++){
4971: /*lnpijopii += param[i][j][nc]*cov[nc];*/
4972: lnpijopii += x[nc+((i-1)*(nlstate+ndeath-1)+j-1)*ncovmodel]*cov[nc];
4973: /* printf("Int j<i s1=%.17e, lnpijopii=%.17e\n",s1,lnpijopii); */
4974: }
4975: ps[i][j]=lnpijopii; /* In fact ln(pij/pii) */
4976: /* printf("Debug pmij() i=%d j=%d nc=%d s1=%.17f, lnpijopii=%.17f\n",i,j,nc, s1,lnpijopii); */
4977: }
4978: for(j=i+1; j<=nlstate+ndeath;j++){
4979: for (nc=1, lnpijopii=0.;nc <=ncovmodel; nc++){
4980: /*lnpijopii += x[(i-1)*nlstate*ncovmodel+(j-2)*ncovmodel+nc+(i-1)*(ndeath-1)*ncovmodel]*cov[nc];*/
4981: lnpijopii += x[nc + ((i-1)*(nlstate+ndeath-1)+(j-2))*ncovmodel]*cov[nc];
4982: /* printf("Int j>i s1=%.17e, lnpijopii=%.17e %lx %lx\n",s1,lnpijopii,s1,lnpijopii); */
4983: }
4984: ps[i][j]=lnpijopii; /* In fact ln(pij/pii) */
4985: /* printf("Debug pmij() i=%d j=%d nc=%d s1=%.17f, lnpijopii=%.17f\n",i,j,nc, s1,lnpijopii); */
4986: }
4987: }
4988:
4989: for(i=1; i<= nlstate; i++){
4990: s1=0;
4991: for(j=1; j<i; j++){
4992: /* printf("debug1 %d %d ps=%lf exp(ps)=%lf \n",i,j,ps[i][j],exp(ps[i][j])); */
4993: s1+=exp(ps[i][j]); /* In fact sums pij/pii */
4994: }
4995: for(j=i+1; j<=nlstate+ndeath; j++){
4996: /* printf("debug2 %d %d ps=%lf exp(ps)=%lf \n",i,j,ps[i][j],exp(ps[i][j])); */
4997: s1+=exp(ps[i][j]); /* In fact sums pij/pii */
4998: }
4999: /* s1= sum_{j<>i} pij/pii=(1-pii)/pii and thus pii is known from s1 */
5000: ps[i][i]=1./(s1+1.);
5001: /* Computing other pijs */
5002: for(j=1; j<i; j++)
5003: ps[i][j]= exp(ps[i][j])*ps[i][i];/* Bug valgrind */
5004: for(j=i+1; j<=nlstate+ndeath; j++)
5005: ps[i][j]= exp(ps[i][j])*ps[i][i];
5006: /* ps[i][nlstate+1]=1.-s1- ps[i][i];*/ /* Sum should be 1 */
5007: } /* end i */
5008:
5009: for(ii=nlstate+1; ii<= nlstate+ndeath; ii++){
5010: for(jj=1; jj<= nlstate+ndeath; jj++){
5011: ps[ii][jj]=0;
5012: ps[ii][ii]=1;
5013: }
5014: }
5015:
5016:
5017: /* for(ii=1; ii<= nlstate+ndeath; ii++){ */
5018: /* for(jj=1; jj<= nlstate+ndeath; jj++){ */
5019: /* printf(" pmij ps[%d][%d]=%lf ",ii,jj,ps[ii][jj]); */
5020: /* } */
5021: /* printf("\n "); */
5022: /* } */
5023: /* printf("\n ");printf("%lf ",cov[2]);*/
5024: /*
5025: for(i=1; i<= npar; i++) printf("%f ",x[i]);
5026: goto end;*/
5027: return ps; /* Pointer is unchanged since its call */
5028: }
5029:
5030: /*************** backward transition probabilities ***************/
5031:
5032: /* 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 ) */
5033: /* double **bmij(double **ps, double *cov, int ncovmodel, double *x, int nlstate, double ***prevacurrent, double ***dnewm, double **doldm, double **dsavm, int ij ) */
5034: double **bmij(double **ps, double *cov, int ncovmodel, double *x, int nlstate, double ***prevacurrent, int ij )
5035: {
5036: /* Computes the backward probability at age agefin, cov[2], and covariate combination 'ij'. In fact cov is already filled and x too.
5037: * Call to pmij(cov and x), call to cross prevalence, sums and inverses, left multiply, and returns in **ps as well as **bmij.
5038: */
5039: int ii, j;
5040:
5041: double **pmij();
5042: double sumnew=0.;
5043: double agefin;
5044: double k3=0.; /* constant of the w_x diagonal matrix (in order for B to sum to 1 even for death state) */
5045: double **dnewm, **dsavm, **doldm;
5046: double **bbmij;
5047:
5048: doldm=ddoldms; /* global pointers */
5049: dnewm=ddnewms;
5050: dsavm=ddsavms;
5051:
5052: /* Debug */
5053: /* printf("Bmij ij=%d, cov[2}=%f\n", ij, cov[2]); */
5054: agefin=cov[2];
5055: /* Bx = Diag(w_x) P_x Diag(Sum_i w^i_x p^ij_x */
5056: /* bmij *//* age is cov[2], ij is included in cov, but we need for
5057: the observed prevalence (with this covariate ij) at beginning of transition */
5058: /* dsavm=pmij(pmmij,cov,ncovmodel,x,nlstate); */
5059:
5060: /* P_x */
5061: pmmij=pmij(pmmij,cov,ncovmodel,x,nlstate); /*This is forward probability from agefin to agefin + stepm *//* Bug valgrind */
5062: /* outputs pmmij which is a stochastic matrix in row */
5063:
5064: /* Diag(w_x) */
5065: /* Rescaling the cross-sectional prevalence: Problem with prevacurrent which can be zero */
5066: sumnew=0.;
5067: /*for (ii=1;ii<=nlstate+ndeath;ii++){*/
5068: for (ii=1;ii<=nlstate;ii++){ /* Only on live states */
5069: /* printf(" agefin=%d, ii=%d, ij=%d, prev=%f\n",(int)agefin,ii, ij, prevacurrent[(int)agefin][ii][ij]); */
5070: sumnew+=prevacurrent[(int)agefin][ii][ij];
5071: }
5072: if(sumnew >0.01){ /* At least some value in the prevalence */
5073: for (ii=1;ii<=nlstate+ndeath;ii++){
5074: for (j=1;j<=nlstate+ndeath;j++)
5075: doldm[ii][j]=(ii==j ? prevacurrent[(int)agefin][ii][ij]/sumnew : 0.0);
5076: }
5077: }else{
5078: for (ii=1;ii<=nlstate+ndeath;ii++){
5079: for (j=1;j<=nlstate+ndeath;j++)
5080: doldm[ii][j]=(ii==j ? 1./nlstate : 0.0);
5081: }
5082: /* if(sumnew <0.9){ */
5083: /* printf("Problem internal bmij B: sum on i wi <0.9: j=%d, sum_i wi=%lf,agefin=%d\n",j,sumnew, (int)agefin); */
5084: /* } */
5085: }
5086: k3=0.0; /* We put the last diagonal to 0 */
5087: for (ii=nlstate+1;ii<=nlstate+ndeath;ii++){
5088: doldm[ii][ii]= k3;
5089: }
5090: /* End doldm, At the end doldm is diag[(w_i)] */
5091:
5092: /* Left product of this diag matrix by pmmij=Px (dnewm=dsavm*doldm): diag[(w_i)*Px */
5093: bbmij=matprod2(dnewm, doldm,1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, pmmij); /* was a Bug Valgrind */
5094:
5095: /* Diag(Sum_i w^i_x p^ij_x, should be the prevalence at age x+stepm */
5096: /* w1 p11 + w2 p21 only on live states N1./N..*N11/N1. + N2./N..*N21/N2.=(N11+N21)/N..=N.1/N.. */
5097: for (j=1;j<=nlstate+ndeath;j++){
5098: sumnew=0.;
5099: for (ii=1;ii<=nlstate;ii++){
5100: /* sumnew+=dsavm[ii][j]*prevacurrent[(int)agefin][ii][ij]; */
5101: sumnew+=pmmij[ii][j]*doldm[ii][ii]; /* Yes prevalence at beginning of transition */
5102: } /* sumnew is (N11+N21)/N..= N.1/N.. = sum on i of w_i pij */
5103: for (ii=1;ii<=nlstate+ndeath;ii++){
5104: /* if(agefin >= agemaxpar && agefin <= agemaxpar+stepm/YEARM){ */
5105: /* dsavm[ii][j]=(ii==j ? 1./sumnew : 0.0); */
5106: /* }else if(agefin >= agemaxpar+stepm/YEARM){ */
5107: /* dsavm[ii][j]=(ii==j ? 1./sumnew : 0.0); */
5108: /* }else */
5109: dsavm[ii][j]=(ii==j ? 1./sumnew : 0.0);
5110: } /*End ii */
5111: } /* 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 */
5112:
5113: ps=matprod2(ps, dnewm,1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, dsavm); /* was a Bug Valgrind */
5114: /* ps is now diag[w_i] * Px * diag [1/(w_1p1i+w_2 p2i)] */
5115: /* end bmij */
5116: return ps; /*pointer is unchanged */
5117: }
5118: /*************** transition probabilities ***************/
5119:
5120: double **bpmij(double **ps, double *cov, int ncovmodel, double *x, int nlstate )
5121: {
5122: /* According to parameters values stored in x and the covariate's values stored in cov,
5123: computes the probability to be observed in state j being in state i by appying the
5124: model to the ncovmodel covariates (including constant and age).
5125: lnpijopii=ln(pij/pii)= aij+bij*age+cij*v1+dij*v2+... = sum_nc=1^ncovmodel xij(nc)*cov[nc]
5126: and, according on how parameters are entered, the position of the coefficient xij(nc) of the
5127: ncth covariate in the global vector x is given by the formula:
5128: j<i nc+((i-1)*(nlstate+ndeath-1)+j-1)*ncovmodel
5129: j>=i nc + ((i-1)*(nlstate+ndeath-1)+(j-2))*ncovmodel
5130: Computes ln(pij/pii) (lnpijopii), deduces pij/pii by exponentiation,
5131: sums on j different of i to get 1-pii/pii, deduces pii, and then all pij.
5132: Outputs ps[i][j] the probability to be observed in j being in j according to
5133: the values of the covariates cov[nc] and corresponding parameter values x[nc+shiftij]
5134: */
5135: double s1, lnpijopii;
5136: /*double t34;*/
5137: int i,j, nc, ii, jj;
5138:
5139: for(i=1; i<= nlstate; i++){
5140: for(j=1; j<i;j++){
5141: for (nc=1, lnpijopii=0.;nc <=ncovmodel; nc++){
5142: /*lnpijopii += param[i][j][nc]*cov[nc];*/
5143: lnpijopii += x[nc+((i-1)*(nlstate+ndeath-1)+j-1)*ncovmodel]*cov[nc];
5144: /* printf("Int j<i s1=%.17e, lnpijopii=%.17e\n",s1,lnpijopii); */
5145: }
5146: ps[i][j]=lnpijopii; /* In fact ln(pij/pii) */
5147: /* printf("s1=%.17e, lnpijopii=%.17e\n",s1,lnpijopii); */
5148: }
5149: for(j=i+1; j<=nlstate+ndeath;j++){
5150: for (nc=1, lnpijopii=0.;nc <=ncovmodel; nc++){
5151: /*lnpijopii += x[(i-1)*nlstate*ncovmodel+(j-2)*ncovmodel+nc+(i-1)*(ndeath-1)*ncovmodel]*cov[nc];*/
5152: lnpijopii += x[nc + ((i-1)*(nlstate+ndeath-1)+(j-2))*ncovmodel]*cov[nc];
5153: /* printf("Int j>i s1=%.17e, lnpijopii=%.17e %lx %lx\n",s1,lnpijopii,s1,lnpijopii); */
5154: }
5155: ps[i][j]=lnpijopii; /* In fact ln(pij/pii) */
5156: }
5157: }
5158:
5159: for(i=1; i<= nlstate; i++){
5160: s1=0;
5161: for(j=1; j<i; j++){
5162: s1+=exp(ps[i][j]); /* In fact sums pij/pii */
5163: /*printf("debug1 %d %d ps=%lf exp(ps)=%lf s1+=%lf\n",i,j,ps[i][j],exp(ps[i][j]),s1); */
5164: }
5165: for(j=i+1; j<=nlstate+ndeath; j++){
5166: s1+=exp(ps[i][j]); /* In fact sums pij/pii */
5167: /*printf("debug2 %d %d ps=%lf exp(ps)=%lf s1+=%lf\n",i,j,ps[i][j],exp(ps[i][j]),s1); */
5168: }
5169: /* s1= sum_{j<>i} pij/pii=(1-pii)/pii and thus pii is known from s1 */
5170: ps[i][i]=1./(s1+1.);
5171: /* Computing other pijs */
5172: for(j=1; j<i; j++)
5173: ps[i][j]= exp(ps[i][j])*ps[i][i];
5174: for(j=i+1; j<=nlstate+ndeath; j++)
5175: ps[i][j]= exp(ps[i][j])*ps[i][i];
5176: /* ps[i][nlstate+1]=1.-s1- ps[i][i];*/ /* Sum should be 1 */
5177: } /* end i */
5178:
5179: for(ii=nlstate+1; ii<= nlstate+ndeath; ii++){
5180: for(jj=1; jj<= nlstate+ndeath; jj++){
5181: ps[ii][jj]=0;
5182: ps[ii][ii]=1;
5183: }
5184: }
5185: /* Added for prevbcast */ /* Transposed matrix too */
5186: for(jj=1; jj<= nlstate+ndeath; jj++){
5187: s1=0.;
5188: for(ii=1; ii<= nlstate+ndeath; ii++){
5189: s1+=ps[ii][jj];
5190: }
5191: for(ii=1; ii<= nlstate; ii++){
5192: ps[ii][jj]=ps[ii][jj]/s1;
5193: }
5194: }
5195: /* Transposition */
5196: for(jj=1; jj<= nlstate+ndeath; jj++){
5197: for(ii=jj; ii<= nlstate+ndeath; ii++){
5198: s1=ps[ii][jj];
5199: ps[ii][jj]=ps[jj][ii];
5200: ps[jj][ii]=s1;
5201: }
5202: }
5203: /* for(ii=1; ii<= nlstate+ndeath; ii++){ */
5204: /* for(jj=1; jj<= nlstate+ndeath; jj++){ */
5205: /* printf(" pmij ps[%d][%d]=%lf ",ii,jj,ps[ii][jj]); */
5206: /* } */
5207: /* printf("\n "); */
5208: /* } */
5209: /* printf("\n ");printf("%lf ",cov[2]);*/
5210: /*
5211: for(i=1; i<= npar; i++) printf("%f ",x[i]);
5212: goto end;*/
5213: return ps;
5214: }
5215:
5216:
5217: /**************** Product of 2 matrices ******************/
5218:
5219: double **matprod2(double **out, double **in,int nrl, int nrh, int ncl, int nch, int ncolol, int ncoloh, double **b)
5220: {
5221: /* Computes the matrix product of in(1,nrh-nrl+1)(1,nch-ncl+1) times
5222: b(1,nch-ncl+1)(1,ncoloh-ncolol+1) into out(...) */
5223: /* in, b, out are matrice of pointers which should have been initialized
5224: before: only the contents of out is modified. The function returns
5225: a pointer to pointers identical to out */
5226: int i, j, k;
5227: for(i=nrl; i<= nrh; i++)
5228: for(k=ncolol; k<=ncoloh; k++){
5229: out[i][k]=0.;
5230: for(j=ncl; j<=nch; j++)
5231: out[i][k] +=in[i][j]*b[j][k];
5232: }
5233: return out;
5234: }
5235:
5236:
5237: /************* Higher Matrix Product ***************/
5238:
5239: double ***hpxij(double ***po, int nhstepm, double age, int hstepm, double *x, int nlstate, int stepm, double **oldm, double **savm, int ij, int nres )
5240: {
5241: /* Already optimized with precov.
5242: Computes the transition matrix starting at age 'age' and dummies values in each resultline (loop on ij to find the corresponding combination) to over
5243: 'nhstepm*hstepm*stepm' months (i.e. until
5244: age (in years) age+nhstepm*hstepm*stepm/12) by multiplying
5245: nhstepm*hstepm matrices.
5246: Output is stored in matrix po[i][j][h] for h every 'hstepm' step
5247: (typically every 2 years instead of every month which is too big
5248: for the memory).
5249: Model is determined by parameters x and covariates have to be
5250: included manually here.
5251:
5252: */
5253:
5254: int i, j, d, h, k1;
5255: double **out, cov[NCOVMAX+1];
5256: double **newm;
5257: double agexact;
5258: /*double agebegin, ageend;*/
5259:
5260: /* Hstepm could be zero and should return the unit matrix */
5261: for (i=1;i<=nlstate+ndeath;i++)
5262: for (j=1;j<=nlstate+ndeath;j++){
5263: oldm[i][j]=(i==j ? 1.0 : 0.0);
5264: po[i][j][0]=(i==j ? 1.0 : 0.0);
5265: }
5266: /* Even if hstepm = 1, at least one multiplication by the unit matrix */
5267: for(h=1; h <=nhstepm; h++){
5268: for(d=1; d <=hstepm; d++){
5269: newm=savm;
5270: /* Covariates have to be included here again */
5271: cov[1]=1.;
5272: agexact=age+((h-1)*hstepm + (d-1))*stepm/YEARM; /* age just before transition */
5273: cov[2]=agexact;
5274: if(nagesqr==1){
5275: cov[3]= agexact*agexact;
5276: }
5277: /* Model(2) V1 + V2 + V3 + V8 + V7*V8 + V5*V6 + V8*age + V3*age + age*age */
5278: /* total number of covariates of the model nbocc(+)+1 = 8 excepting constant and age and age*age */
5279: for(k1=1;k1<=cptcovt;k1++){ /* loop on model equation (including products) */
5280: if(Typevar[k1]==1 || Typevar[k1]==3){ /* A product with age */
5281: cov[2+nagesqr+k1]=precov[nres][k1]*cov[2];
5282: }else{
5283: cov[2+nagesqr+k1]=precov[nres][k1];
5284: }
5285: }/* End of loop on model equation */
5286: /* Old code */
5287: /* if( Dummy[k1]==0 && Typevar[k1]==0 ){ /\* Single dummy *\/ */
5288: /* /\* V(Tvarsel)=Tvalsel=Tresult[nres][pos](value); V(Tvresult[nres][pos] (variable): V(variable)=value) *\/ */
5289: /* /\* for (k=1; k<=nsd;k++) { /\\* For single dummy covariates only *\\/ *\/ */
5290: /* /\* /\\* Here comes the value of the covariate 'ij' after renumbering k with single dummy covariates *\\/ *\/ */
5291: /* /\* codtabm(ij,k) (1 & (ij-1) >> (k-1))+1 *\/ */
5292: /* /\* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 *\/ */
5293: /* /\* k 1 2 3 4 5 6 7 8 9 *\/ */
5294: /* /\*Tvar[k]= 5 4 3 6 5 2 7 1 1 *\/ */
5295: /* /\* nsd 1 2 3 *\/ /\* Counting single dummies covar fixed or tv *\/ */
5296: /* /\*TvarsD[nsd] 4 3 1 *\/ /\* ID of single dummy cova fixed or timevary*\/ */
5297: /* /\*TvarsDind[k] 2 3 9 *\/ /\* position K of single dummy cova *\/ */
5298: /* /\* cov[2+nagesqr+TvarsDind[k]]=nbcode[TvarsD[k]][codtabm(ij,k)];or [codtabm(ij,TnsdVar[TvarsD[k]] *\/ */
5299: /* cov[2+nagesqr+k1]=Tresult[nres][resultmodel[nres][k1]]; */
5300: /* /\* 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]])); *\/ */
5301: /* 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); */
5302: /* printf("hpxij new Dummy precov[nres=%d][k1=%d]=%.4f\n", nres, k1, precov[nres][k1]); */
5303: /* }else if( Dummy[k1]==1 && Typevar[k1]==0 ){ /\* Single quantitative variables *\/ */
5304: /* /\* resultmodel[nres][k1]=k3: k1th position in the model correspond to the k3 position in the resultline *\/ */
5305: /* cov[2+nagesqr+k1]=Tqresult[nres][resultmodel[nres][k1]]; */
5306: /* /\* for (k=1; k<=nsq;k++) { /\\* For single varying covariates only *\\/ *\/ */
5307: /* /\* /\\* Here comes the value of quantitative after renumbering k with single quantitative covariates *\\/ *\/ */
5308: /* /\* cov[2+nagesqr+TvarsQind[k]]=Tqresult[nres][k]; *\/ */
5309: /* 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]]); */
5310: /* printf("hpxij new Quanti precov[nres=%d][k1=%d]=%.4f\n", nres, k1, precov[nres][k1]); */
5311: /* }else if( Dummy[k1]==2 ){ /\* For dummy with age product *\/ */
5312: /* /\* Tvar[k1] Variable in the age product age*V1 is 1 *\/ */
5313: /* /\* [Tinvresult[nres][V1] is its value in the resultline nres *\/ */
5314: /* cov[2+nagesqr+k1]=TinvDoQresult[nres][Tvar[k1]]*cov[2]; */
5315: /* 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]); */
5316: /* printf("hpxij new Dummy with age product precov[nres=%d][k1=%d]=%.4f * age=%.2f\n", nres, k1, precov[nres][k1], cov[2]); */
5317:
5318: /* /\* cov[2+nagesqr+k1]=Tresult[nres][resultmodel[nres][k1]]; *\/ */
5319: /* /\* for (k=1; k<=cptcovage;k++){ /\\* For product with age V1+V1*age +V4 +age*V3 *\\/ *\/ */
5320: /* /\* 1+2 Tage[1]=2 TVar[2]=1 Dummy[2]=2, Tage[2]=4 TVar[4]=3 Dummy[4]=3 quant*\/ */
5321: /* /\* *\/ */
5322: /* /\* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 *\/ */
5323: /* /\* k 1 2 3 4 5 6 7 8 9 *\/ */
5324: /* /\*Tvar[k]= 5 4 3 6 5 2 7 1 1 *\/ */
5325: /* /\*cptcovage=2 1 2 *\/ */
5326: /* /\*Tage[k]= 5 8 *\/ */
5327: /* }else if( Dummy[k1]==3 ){ /\* For quant with age product *\/ */
5328: /* cov[2+nagesqr+k1]=Tresult[nres][resultmodel[nres][k1]]; */
5329: /* 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]]); */
5330: /* printf("hpxij new Quanti with age product precov[nres=%d][k1=%d] * age=%.2f\n", nres, k1, precov[nres][k1], cov[2]); */
5331: /* /\* if(Dummy[Tage[k]]== 2){ /\\* dummy with age *\\/ *\/ */
5332: /* /\* /\\* if(Dummy[Tvar[Tage[k]]]== 2){ /\\\* dummy with age *\\\/ *\\/ *\/ */
5333: /* /\* /\\* cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,k)]*cov[2]; *\\/ *\/ */
5334: /* /\* /\\* cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,TnsdVar[TvarsD[Tvar[Tage[k]]]])]*cov[2]; *\\/ *\/ */
5335: /* /\* cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,TnsdVar[TvarsD[Tvar[Tage[k]]]])]*cov[2]; *\/ */
5336: /* /\* 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); *\/ */
5337: /* /\* } else if(Dummy[Tage[k]]== 3){ /\\* quantitative with age *\\/ *\/ */
5338: /* /\* cov[2+nagesqr+Tage[k]]=Tqresult[nres][k]; *\/ */
5339: /* /\* } *\/ */
5340: /* /\* 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]); *\/ */
5341: /* }else if(Typevar[k1]==2 ){ /\* For product (not with age) *\/ */
5342: /* /\* for (k=1; k<=cptcovprod;k++){ /\\* For product without age *\\/ *\/ */
5343: /* /\* /\\* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 *\\/ *\/ */
5344: /* /\* /\\* k 1 2 3 4 5 6 7 8 9 *\\/ *\/ */
5345: /* /\* /\\*Tvar[k]= 5 4 3 6 5 2 7 1 1 *\\/ *\/ */
5346: /* /\* /\\*cptcovprod=1 1 2 *\\/ *\/ */
5347: /* /\* /\\*Tprod[]= 4 7 *\\/ *\/ */
5348: /* /\* /\\*Tvard[][1] 4 1 *\\/ *\/ */
5349: /* /\* /\\*Tvard[][2] 3 2 *\\/ *\/ */
5350:
5351: /* /\* 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])]); *\/ */
5352: /* /\* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)] * nbcode[Tvard[k][2]][codtabm(ij,k)]; *\/ */
5353: /* cov[2+nagesqr+k1]=TinvDoQresult[nres][Tvardk[k1][1]] * TinvDoQresult[nres][Tvardk[k1][2]]; */
5354: /* 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]]); */
5355: /* printf("hpxij new Product no age product precov[nres=%d][k1=%d]=%.4f\n", nres, k1, precov[nres][k1]); */
5356:
5357: /* /\* if(Dummy[Tvardk[k1][1]]==0){ *\/ */
5358: /* /\* if(Dummy[Tvardk[k1][2]]==0){ /\\* Product of dummies *\\/ *\/ */
5359: /* /\* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)] * nbcode[Tvard[k][2]][codtabm(ij,k)]; *\/ */
5360: /* /\* cov[2+nagesqr+k1]=Tinvresult[nres][Tvardk[k1][1]] * Tinvresult[nres][Tvardk[k1][2]]; *\/ */
5361: /* /\* 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]])]; *\/ */
5362: /* /\* }else{ /\\* Product of dummy by quantitative *\\/ *\/ */
5363: /* /\* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,TnsdVar[Tvard[k][1]])] * Tqresult[nres][k]; *\/ */
5364: /* /\* cov[2+nagesqr+k1]=Tresult[nres][Tinvresult[nres][Tvardk[k1][1]]] * Tqresult[nres][Tinvresult[nres][Tvardk[k1][2]]]; *\/ */
5365: /* /\* } *\/ */
5366: /* /\* }else{ /\\* Product of quantitative by...*\\/ *\/ */
5367: /* /\* if(Dummy[Tvard[k][2]]==0){ /\\* quant by dummy *\\/ *\/ */
5368: /* /\* /\\* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][2]][codtabm(ij,TnsdVar[Tvard[k][2]])] * Tqinvresult[nres][Tvard[k][1]]; *\\/ *\/ */
5369: /* /\* cov[2+nagesqr+k1]=Tqresult[nres][Tinvresult[nres][Tvardk[k1][1]]] * Tresult[nres][Tinvresult[nres][Tvardk[k1][2]]] ; *\/ */
5370: /* /\* }else{ /\\* Product of two quant *\\/ *\/ */
5371: /* /\* /\\* cov[2+nagesqr+Tprod[k]]=Tqinvresult[nres][Tvard[k][1]]* Tqinvresult[nres][Tvard[k][2]]; *\\/ *\/ */
5372: /* /\* cov[2+nagesqr+k1]=Tqresult[nres][Tinvresult[nres][Tvardk[k1][1]]] * Tqresult[nres][Tinvresult[nres][Tvardk[k1][2]]] ; *\/ */
5373: /* /\* } *\/ */
5374: /* /\* }/\\*end of products quantitative *\\/ *\/ */
5375: /* }/\*end of products *\/ */
5376: /* } /\* End of loop on model equation *\/ */
5377: /* for (k=1; k<=cptcovn;k++) */
5378: /* cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,k)]; */
5379: /* for (k=1; k<=cptcovage;k++) /\* Should start at cptcovn+1 *\/ */
5380: /* cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,k)]*cov[2]; */
5381: /* for (k=1; k<=cptcovprod;k++) /\* Useless because included in cptcovn *\/ */
5382: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)]*nbcode[Tvard[k][2]][codtabm(ij,k)]; */
5383:
5384:
5385: /*printf("hxi cptcov=%d cptcode=%d\n",cptcov,cptcode);*/
5386: /*printf("h=%d d=%d age=%f cov=%f\n",h,d,age,cov[2]);*/
5387: /* right multiplication of oldm by the current matrix */
5388: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath,
5389: pmij(pmmij,cov,ncovmodel,x,nlstate));
5390: /* if((int)age == 70){ */
5391: /* printf(" Forward hpxij age=%d agexact=%f d=%d nhstepm=%d hstepm=%d\n", (int) age, agexact, d, nhstepm, hstepm); */
5392: /* for(i=1; i<=nlstate+ndeath; i++) { */
5393: /* printf("%d pmmij ",i); */
5394: /* for(j=1;j<=nlstate+ndeath;j++) { */
5395: /* printf("%f ",pmmij[i][j]); */
5396: /* } */
5397: /* printf(" oldm "); */
5398: /* for(j=1;j<=nlstate+ndeath;j++) { */
5399: /* printf("%f ",oldm[i][j]); */
5400: /* } */
5401: /* printf("\n"); */
5402: /* } */
5403: /* } */
5404: savm=oldm;
5405: oldm=newm;
5406: }
5407: for(i=1; i<=nlstate+ndeath; i++)
5408: for(j=1;j<=nlstate+ndeath;j++) {
5409: po[i][j][h]=newm[i][j];
5410: /*if(h==nhstepm) printf("po[%d][%d][%d]=%f ",i,j,h,po[i][j][h]);*/
5411: }
5412: /*printf("h=%d ",h);*/
5413: } /* end h */
5414: /* printf("\n H=%d \n",h); */
5415: return po;
5416: }
5417:
5418: /************* Higher Back Matrix Product ***************/
5419: /* 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 ) */
5420: double ***hbxij(double ***po, int nhstepm, double age, int hstepm, double *x, double ***prevacurrent, int nlstate, int stepm, int ij, int nres )
5421: {
5422: /* For dummy covariates given in each resultline (for historical, computes the corresponding combination ij),
5423: computes the transition matrix starting at age 'age' over
5424: 'nhstepm*hstepm*stepm' months (i.e. until
5425: age (in years) age+nhstepm*hstepm*stepm/12) by multiplying
5426: nhstepm*hstepm matrices.
5427: Output is stored in matrix po[i][j][h] for h every 'hstepm' step
5428: (typically every 2 years instead of every month which is too big
5429: for the memory).
5430: Model is determined by parameters x and covariates have to be
5431: included manually here. Then we use a call to bmij(x and cov)
5432: The addresss of po (p3mat allocated to the dimension of nhstepm) should be stored for output
5433: */
5434:
5435: int i, j, d, h, k1;
5436: double **out, cov[NCOVMAX+1], **bmij();
5437: double **newm, ***newmm;
5438: double agexact;
5439: /*double agebegin, ageend;*/
5440: double **oldm, **savm;
5441:
5442: newmm=po; /* To be saved */
5443: oldm=oldms;savm=savms; /* Global pointers */
5444: /* Hstepm could be zero and should return the unit matrix */
5445: for (i=1;i<=nlstate+ndeath;i++)
5446: for (j=1;j<=nlstate+ndeath;j++){
5447: oldm[i][j]=(i==j ? 1.0 : 0.0);
5448: po[i][j][0]=(i==j ? 1.0 : 0.0);
5449: }
5450: /* Even if hstepm = 1, at least one multiplication by the unit matrix */
5451: for(h=1; h <=nhstepm; h++){
5452: for(d=1; d <=hstepm; d++){
5453: newm=savm;
5454: /* Covariates have to be included here again */
5455: cov[1]=1.;
5456: agexact=age-( (h-1)*hstepm + (d) )*stepm/YEARM; /* age just before transition, d or d-1? */
5457: /* agexact=age+((h-1)*hstepm + (d-1))*stepm/YEARM; /\* age just before transition *\/ */
5458: /* Debug */
5459: /* printf("hBxij age=%lf, agexact=%lf\n", age, agexact); */
5460: cov[2]=agexact;
5461: if(nagesqr==1){
5462: cov[3]= agexact*agexact;
5463: }
5464: /** New code */
5465: for(k1=1;k1<=cptcovt;k1++){ /* loop on model equation (including products) */
5466: if(Typevar[k1]==1 || Typevar[k1]==3){ /* A product with age */
5467: cov[2+nagesqr+k1]=precov[nres][k1]*cov[2];
5468: }else{
5469: cov[2+nagesqr+k1]=precov[nres][k1];
5470: }
5471: }/* End of loop on model equation */
5472: /** End of new code */
5473: /** This was old code */
5474: /* for (k=1; k<=nsd;k++){ /\* For single dummy covariates only *\//\* cptcovn error *\/ */
5475: /* /\* cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,k)]; *\/ */
5476: /* /\* /\\* cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,Tvar[k])]; *\\/ *\/ */
5477: /* cov[2+nagesqr+TvarsDind[k]]=nbcode[TvarsD[k]][codtabm(ij,TvarsD[k])];/\* Bug valgrind *\/ */
5478: /* /\* 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)); *\/ */
5479: /* } */
5480: /* for (k=1; k<=nsq;k++) { /\* For single varying covariates only *\/ */
5481: /* /\* Here comes the value of quantitative after renumbering k with single quantitative covariates *\/ */
5482: /* cov[2+nagesqr+TvarsQind[k]]=Tqresult[nres][k]; */
5483: /* /\* 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]); *\/ */
5484: /* } */
5485: /* for (k=1; k<=cptcovage;k++){ /\* Should start at cptcovn+1 *\//\* For product with age *\/ */
5486: /* /\* if(Dummy[Tvar[Tage[k]]]== 2){ /\\* dummy with age error!!!*\\/ *\/ */
5487: /* if(Dummy[Tage[k]]== 2){ /\* dummy with age *\/ */
5488: /* cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,Tvar[Tage[k]])]*cov[2]; */
5489: /* } else if(Dummy[Tage[k]]== 3){ /\* quantitative with age *\/ */
5490: /* cov[2+nagesqr+Tage[k]]=Tqresult[nres][k]; */
5491: /* } */
5492: /* /\* 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]); *\/ */
5493: /* } */
5494: /* for (k=1; k<=cptcovprod;k++){ /\* Useless because included in cptcovn *\/ */
5495: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,Tvard[k][1])]*nbcode[Tvard[k][2]][codtabm(ij,Tvard[k][2])]; */
5496: /* if(Dummy[Tvard[k][1]]==0){ */
5497: /* if(Dummy[Tvard[k][2]]==0){ */
5498: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,Tvard[k][1])] * nbcode[Tvard[k][2]][codtabm(ij,Tvard[k][1])]; */
5499: /* }else{ */
5500: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,Tvard[k][1])] * Tqresult[nres][k]; */
5501: /* } */
5502: /* }else{ */
5503: /* if(Dummy[Tvard[k][2]]==0){ */
5504: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][2]][codtabm(ij,Tvard[k][2])] * Tqinvresult[nres][Tvard[k][1]]; */
5505: /* }else{ */
5506: /* cov[2+nagesqr+Tprod[k]]=Tqinvresult[nres][Tvard[k][1]]* Tqinvresult[nres][Tvard[k][2]]; */
5507: /* } */
5508: /* } */
5509: /* } */
5510: /* /\*printf("hxi cptcov=%d cptcode=%d\n",cptcov,cptcode);*\/ */
5511: /* /\*printf("h=%d d=%d age=%f cov=%f\n",h,d,age,cov[2]);*\/ */
5512: /** End of old code */
5513:
5514: /* Careful transposed matrix */
5515: /* age is in cov[2], prevacurrent at beginning of transition. */
5516: /* out=matprod2(newm, bmij(pmmij,cov,ncovmodel,x,nlstate,prevacurrent, dnewm, doldm, dsavm,ij),\ */
5517: /* 1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm); */
5518: out=matprod2(newm, bmij(pmmij,cov,ncovmodel,x,nlstate,prevacurrent,ij),\
5519: 1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm);/* Bug valgrind */
5520: /* if((int)age == 70){ */
5521: /* printf(" Backward hbxij age=%d agexact=%f d=%d nhstepm=%d hstepm=%d\n", (int) age, agexact, d, nhstepm, hstepm); */
5522: /* for(i=1; i<=nlstate+ndeath; i++) { */
5523: /* printf("%d pmmij ",i); */
5524: /* for(j=1;j<=nlstate+ndeath;j++) { */
5525: /* printf("%f ",pmmij[i][j]); */
5526: /* } */
5527: /* printf(" oldm "); */
5528: /* for(j=1;j<=nlstate+ndeath;j++) { */
5529: /* printf("%f ",oldm[i][j]); */
5530: /* } */
5531: /* printf("\n"); */
5532: /* } */
5533: /* } */
5534: savm=oldm;
5535: oldm=newm;
5536: }
5537: for(i=1; i<=nlstate+ndeath; i++)
5538: for(j=1;j<=nlstate+ndeath;j++) {
5539: po[i][j][h]=newm[i][j];
5540: /* if(h==nhstepm) */
5541: /* printf("po[%d][%d][%d]=%f ",i,j,h,po[i][j][h]); */
5542: }
5543: /* printf("h=%d %.1f ",h, agexact); */
5544: } /* end h */
5545: /* printf("\n H=%d nhs=%d \n",h, nhstepm); */
5546: return po;
5547: }
5548:
5549:
5550: #ifdef NLOPT
5551: double myfunc(unsigned n, const double *p1, double *grad, void *pd){
5552: double fret;
5553: double *xt;
5554: int j;
5555: myfunc_data *d2 = (myfunc_data *) pd;
5556: /* xt = (p1-1); */
5557: xt=vector(1,n);
5558: for (j=1;j<=n;j++) xt[j]=p1[j-1]; /* xt[1]=p1[0] */
5559:
5560: fret=(d2->function)(xt); /* p xt[1]@8 is fine */
5561: /* fret=(*func)(xt); /\* p xt[1]@8 is fine *\/ */
5562: printf("Function = %.12lf ",fret);
5563: for (j=1;j<=n;j++) printf(" %d %.8lf", j, xt[j]);
5564: printf("\n");
5565: free_vector(xt,1,n);
5566: return fret;
5567: }
5568: #endif
5569:
5570: /*************** log-likelihood *************/
5571: double func( double *x)
5572: {
5573: int i, ii, j, k, mi, d, kk, kf=0;
5574: int ioffset=0;
5575: int ipos=0,iposold=0,ncovv=0;
5576:
5577: double cotvarv, cotvarvold;
5578: double l, ll[NLSTATEMAX+1], cov[NCOVMAX+1];
5579: double **out;
5580: double lli; /* Individual log likelihood */
5581: int s1, s2;
5582: int iv=0, iqv=0, itv=0, iqtv=0 ; /* Index of varying covariate, fixed quantitative cov, time varying covariate, quantitative time varying covariate */
5583:
5584: double bbh, survp;
5585: double agexact;
5586: double agebegin, ageend;
5587: /*extern weight */
5588: /* We are differentiating ll according to initial status */
5589: /* for (i=1;i<=npar;i++) printf("%f ", x[i]);*/
5590: /*for(i=1;i<imx;i++)
5591: printf(" %d\n",s[4][i]);
5592: */
5593:
5594: ++countcallfunc;
5595:
5596: cov[1]=1.;
5597:
5598: for(k=1; k<=nlstate; k++) ll[k]=0.;
5599: ioffset=0;
5600: if(mle==1){
5601: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
5602: /* Computes the values of the ncovmodel covariates of the model
5603: depending if the covariates are fixed or varying (age dependent) and stores them in cov[]
5604: Then computes with function pmij which return a matrix p[i][j] giving the elementary probability
5605: to be observed in j being in i according to the model.
5606: */
5607: ioffset=2+nagesqr ;
5608: /* Fixed */
5609: for (kf=1; kf<=ncovf;kf++){ /* For each fixed covariate dummy or quant or prod */
5610: /* # V1=sex, V2=raedyrs Quant Fixed, State=livarnb4..livarnb11, V3=iadl4..iald11, V4=adlw4..adlw11, V5=r4bmi..r11bmi */
5611: /* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
5612: /* 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 */
5613: /* TvarFind; TvarFind[1]=6, TvarFind[2]=7, TvarFind[3]=9 *//* Inverse V2(6) is first fixed (single or prod) */
5614: 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)*/
5615: /* V1*V2 (7) TvarFind[2]=7, TvarFind[3]=9 */
5616: }
5617: /* In model V2+V1*V4+age*V3+V3*V2 Tvar[1] is V2, Tvar[2=V1*V4]
5618: is 5, Tvar[3=age*V3] should not be computed because of age Tvar[4=V3*V2]=6
5619: has been calculated etc */
5620: /* For an individual i, wav[i] gives the number of effective waves */
5621: /* We compute the contribution to Likelihood of each effective transition
5622: mw[mi][i] is real wave of the mi th effectve wave */
5623: /* Then statuses are computed at each begin and end of an effective wave s1=s[ mw[mi][i] ][i];
5624: s2=s[mw[mi+1][i]][i];
5625: And the iv th varying covariate is the cotvar[mw[mi+1][i]][iv][i] because now is moved after nvocol+nqv
5626: But if the variable is not in the model TTvar[iv] is the real variable effective in the model:
5627: meaning that decodemodel should be used cotvar[mw[mi+1][i]][TTvar[iv]][i]
5628: */
5629: for(mi=1; mi<= wav[i]-1; mi++){ /* Varying with waves */
5630: /* Wave varying (but not age varying) */
5631: /* 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*\/ */
5632: /* /\* cov[ioffset+TvarVind[k]]=cotvar[mw[mi][i]][Tvar[TvarVind[k]]][i]; but where is the crossproduct? *\/ */
5633: /* cov[ioffset+TvarVind[k]]=cotvar[mw[mi][i]][Tvar[TvarVind[k]]-ncovcol-nqv][i]; */
5634: /* } */
5635: for(ncovv=1, iposold=0; ncovv <= ncovvt ; ncovv++){ /* Varying covariates (single and product but no age )*/
5636: itv=TvarVV[ncovv]; /* TvarVV={3, 1, 3} gives the name of each varying covariate */
5637: ipos=TvarVVind[ncovv]; /* TvarVVind={2, 5, 5] gives the position in the model of the ncovv th varying covariate*/
5638: if(FixedV[itv]!=0){ /* Not a fixed covariate */
5639: cotvarv=cotvar[mw[mi][i]][TvarVV[ncovv]][i]; /* cotvar[wav][ncovcol+nqv+iv][i] */
5640: }else{ /* fixed covariate */
5641: cotvarv=covar[itv][i]; /* Error: TvarFind gives the name, that is the true column of fixed covariates, but Tvar of the model */
5642: }
5643: if(ipos!=iposold){ /* Not a product or first of a product */
5644: cotvarvold=cotvarv;
5645: }else{ /* A second product */
5646: cotvarv=cotvarv*cotvarvold;
5647: }
5648: iposold=ipos;
5649: cov[ioffset+ipos]=cotvarv;
5650: }
5651: /* for products of time varying to be done */
5652: for (ii=1;ii<=nlstate+ndeath;ii++)
5653: for (j=1;j<=nlstate+ndeath;j++){
5654: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
5655: savm[ii][j]=(ii==j ? 1.0 : 0.0);
5656: }
5657:
5658: agebegin=agev[mw[mi][i]][i]; /* Age at beginning of effective wave */
5659: ageend=agev[mw[mi][i]][i] + (dh[mi][i])*stepm/YEARM; /* Age at end of effective wave and at the end of transition */
5660: for(d=0; d<dh[mi][i]; d++){
5661: newm=savm;
5662: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM;
5663: cov[2]=agexact;
5664: if(nagesqr==1)
5665: cov[3]= agexact*agexact; /* Should be changed here */
5666: /* for (kk=1; kk<=cptcovage;kk++) { */
5667: /* if(!FixedV[Tvar[Tage[kk]]]) */
5668: /* cov[Tage[kk]+2+nagesqr]=covar[Tvar[Tage[kk]]][i]*agexact; /\* Tage[kk] gives the data-covariate associated with age *\/ */
5669: /* else */
5670: /* cov[Tage[kk]+2+nagesqr]=cotvar[mw[mi][i]][Tvar[Tage[kk]]][i]*agexact; /\* because cotvar starts now at first ncovcol+nqv+ (1 to nqtv) *\/ */
5671: /* } */
5672: for(ncovva=1, iposold=0; ncovva <= ncovta ; ncovva++){ /* Time varying covariates with age including individual from products, product is computed dynamically */
5673: itv=TvarAVVA[ncovva]; /* TvarVV={3, 1, 3} gives the name of each varying covariate, exploding product Vn*Vm into Vn and then Vm */
5674: ipos=TvarAVVAind[ncovva]; /* TvarVVind={2, 5, 5] gives the position in the model of the ncovv th varying covariate*/
5675: if(FixedV[itv]!=0){ /* Not a fixed covariate? Could be a fixed covariate of a product with a higher than ncovcol+nqv, itv */
5676: cotvarv=cotvar[mw[mi][i]][TvarAVVA[ncovva]][i]; /* because cotvar starts now at first ncovcol+nqv+ntv+nqtv (1 to nqtv) */
5677: }else{ /* fixed covariate */
5678: cotvarv=covar[itv][i]; /* Error: TvarFind gives the name, that is the true column of fixed covariates, but Tvar of the model */
5679: }
5680: if(ipos!=iposold){ /* Not a product or first of a product */
5681: cotvarvold=cotvarv;
5682: }else{ /* A second product */
5683: cotvarv=cotvarv*cotvarvold;
5684: }
5685: iposold=ipos;
5686: cov[ioffset+ipos]=cotvarv*agexact;
5687: /* For products */
5688: }
5689:
5690: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
5691: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
5692: savm=oldm;
5693: oldm=newm;
5694: } /* end mult */
5695:
5696: /*lli=log(out[s[mw[mi][i]][i]][s[mw[mi+1][i]][i]]);*/ /* Original formula */
5697: /* But now since version 0.9 we anticipate for bias at large stepm.
5698: * If stepm is larger than one month (smallest stepm) and if the exact delay
5699: * (in months) between two waves is not a multiple of stepm, we rounded to
5700: * the nearest (and in case of equal distance, to the lowest) interval but now
5701: * we keep into memory the bias bh[mi][i] and also the previous matrix product
5702: * (i.e to dh[mi][i]-1) saved in 'savm'. Then we inter(extra)polate the
5703: * probability in order to take into account the bias as a fraction of the way
5704: * from savm to out if bh is negative or even beyond if bh is positive. bh varies
5705: * -stepm/2 to stepm/2 .
5706: * For stepm=1 the results are the same as for previous versions of Imach.
5707: * For stepm > 1 the results are less biased than in previous versions.
5708: */
5709: s1=s[mw[mi][i]][i];
5710: s2=s[mw[mi+1][i]][i];
5711: bbh=(double)bh[mi][i]/(double)stepm;
5712: /* bias bh is positive if real duration
5713: * is higher than the multiple of stepm and negative otherwise.
5714: */
5715: /* lli= (savm[s1][s2]>1.e-8 ?(1.+bbh)*log(out[s1][s2])- bbh*log(savm[s1][s2]):log((1.+bbh)*out[s1][s2]));*/
5716: if( s2 > nlstate){
5717: /* i.e. if s2 is a death state and if the date of death is known
5718: then the contribution to the likelihood is the probability to
5719: die between last step unit time and current step unit time,
5720: which is also equal to probability to die before dh
5721: minus probability to die before dh-stepm .
5722: In version up to 0.92 likelihood was computed
5723: as if date of death was unknown. Death was treated as any other
5724: health state: the date of the interview describes the actual state
5725: and not the date of a change in health state. The former idea was
5726: to consider that at each interview the state was recorded
5727: (healthy, disable or death) and IMaCh was corrected; but when we
5728: introduced the exact date of death then we should have modified
5729: the contribution of an exact death to the likelihood. This new
5730: contribution is smaller and very dependent of the step unit
5731: stepm. It is no more the probability to die between last interview
5732: and month of death but the probability to survive from last
5733: interview up to one month before death multiplied by the
5734: probability to die within a month. Thanks to Chris
5735: Jackson for correcting this bug. Former versions increased
5736: mortality artificially. The bad side is that we add another loop
5737: which slows down the processing. The difference can be up to 10%
5738: lower mortality.
5739: */
5740: /* If, at the beginning of the maximization mostly, the
5741: cumulative probability or probability to be dead is
5742: constant (ie = 1) over time d, the difference is equal to
5743: 0. out[s1][3] = savm[s1][3]: probability, being at state
5744: s1 at precedent wave, to be dead a month before current
5745: wave is equal to probability, being at state s1 at
5746: precedent wave, to be dead at mont of the current
5747: wave. Then the observed probability (that this person died)
5748: is null according to current estimated parameter. In fact,
5749: it should be very low but not zero otherwise the log go to
5750: infinity.
5751: */
5752: /* #ifdef INFINITYORIGINAL */
5753: /* lli=log(out[s1][s2] - savm[s1][s2]); */
5754: /* #else */
5755: /* if ((out[s1][s2] - savm[s1][s2]) < mytinydouble) */
5756: /* lli=log(mytinydouble); */
5757: /* else */
5758: /* lli=log(out[s1][s2] - savm[s1][s2]); */
5759: /* #endif */
5760: lli=log(out[s1][s2] - savm[s1][s2]);
5761:
5762: } else if ( s2==-1 ) { /* alive */
5763: for (j=1,survp=0. ; j<=nlstate; j++)
5764: survp += (1.+bbh)*out[s1][j]- bbh*savm[s1][j];
5765: /*survp += out[s1][j]; */
5766: lli= log(survp);
5767: }
5768: /* else if (s2==-4) { */
5769: /* for (j=3,survp=0. ; j<=nlstate; j++) */
5770: /* survp += (1.+bbh)*out[s1][j]- bbh*savm[s1][j]; */
5771: /* lli= log(survp); */
5772: /* } */
5773: /* else if (s2==-5) { */
5774: /* for (j=1,survp=0. ; j<=2; j++) */
5775: /* survp += (1.+bbh)*out[s1][j]- bbh*savm[s1][j]; */
5776: /* lli= log(survp); */
5777: /* } */
5778: else{
5779: lli= log((1.+bbh)*out[s1][s2]- bbh*savm[s1][s2]); /* linear interpolation */
5780: /* 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 */
5781: }
5782: /*lli=(1.+bbh)*log(out[s1][s2])- bbh*log(savm[s1][s2]);*/
5783: /*if(lli ==000.0)*/
5784: /* printf("num[i], i=%d, 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); */
5785: ipmx +=1;
5786: sw += weight[i];
5787: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
5788: /* if (lli < log(mytinydouble)){ */
5789: /* 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); */
5790: /* 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]); */
5791: /* } */
5792: } /* end of wave */
5793: } /* end of individual */
5794: } else if(mle==2){
5795: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
5796: ioffset=2+nagesqr ;
5797: for (k=1; k<=ncovf;k++)
5798: cov[ioffset+TvarFind[k]]=covar[Tvar[TvarFind[k]]][i];
5799: for(mi=1; mi<= wav[i]-1; mi++){
5800: for(k=1; k <= ncovv ; k++){
5801: cov[ioffset+TvarVind[k]]=cotvar[mw[mi][i]][Tvar[TvarVind[k]]][i]; /* because cotvar starts now at first ncovcol+nqv+ (1 to nqtv) */
5802: }
5803: for (ii=1;ii<=nlstate+ndeath;ii++)
5804: for (j=1;j<=nlstate+ndeath;j++){
5805: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
5806: savm[ii][j]=(ii==j ? 1.0 : 0.0);
5807: }
5808: for(d=0; d<=dh[mi][i]; d++){
5809: newm=savm;
5810: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM;
5811: cov[2]=agexact;
5812: if(nagesqr==1)
5813: cov[3]= agexact*agexact;
5814: for (kk=1; kk<=cptcovage;kk++) {
5815: cov[Tage[kk]+2+nagesqr]=covar[Tvar[Tage[kk]]][i]*agexact;
5816: }
5817: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
5818: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
5819: savm=oldm;
5820: oldm=newm;
5821: } /* end mult */
5822:
5823: s1=s[mw[mi][i]][i];
5824: s2=s[mw[mi+1][i]][i];
5825: bbh=(double)bh[mi][i]/(double)stepm;
5826: 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 */
5827: ipmx +=1;
5828: sw += weight[i];
5829: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
5830: } /* end of wave */
5831: } /* end of individual */
5832: } else if(mle==3){ /* exponential inter-extrapolation */
5833: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
5834: for (k=1; k<=cptcovn;k++) cov[2+nagesqr+k]=covar[Tvar[k]][i];
5835: for(mi=1; mi<= wav[i]-1; mi++){
5836: for (ii=1;ii<=nlstate+ndeath;ii++)
5837: for (j=1;j<=nlstate+ndeath;j++){
5838: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
5839: savm[ii][j]=(ii==j ? 1.0 : 0.0);
5840: }
5841: for(d=0; d<dh[mi][i]; d++){
5842: newm=savm;
5843: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM;
5844: cov[2]=agexact;
5845: if(nagesqr==1)
5846: cov[3]= agexact*agexact;
5847: for (kk=1; kk<=cptcovage;kk++) {
5848: if(!FixedV[Tvar[Tage[kk]]])
5849: cov[Tage[kk]+2+nagesqr]=covar[Tvar[Tage[kk]]][i]*agexact; /* Tage[kk] gives the data-covariate associated with age */
5850: else
5851: cov[Tage[kk]+2+nagesqr]=cotvar[mw[mi][i]][Tvar[Tage[kk]]][i]*agexact; /* because cotvar starts now at first ncovcol+nqv+ (1 to nqtv) */
5852: }
5853: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
5854: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
5855: savm=oldm;
5856: oldm=newm;
5857: } /* end mult */
5858:
5859: s1=s[mw[mi][i]][i];
5860: s2=s[mw[mi+1][i]][i];
5861: bbh=(double)bh[mi][i]/(double)stepm;
5862: 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 */
5863: ipmx +=1;
5864: sw += weight[i];
5865: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
5866: } /* end of wave */
5867: } /* end of individual */
5868: }else if (mle==4){ /* ml=4 no inter-extrapolation */
5869: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
5870: for (k=1; k<=cptcovn;k++) cov[2+nagesqr+k]=covar[Tvar[k]][i];
5871: for(mi=1; mi<= wav[i]-1; mi++){
5872: for (ii=1;ii<=nlstate+ndeath;ii++)
5873: for (j=1;j<=nlstate+ndeath;j++){
5874: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
5875: savm[ii][j]=(ii==j ? 1.0 : 0.0);
5876: }
5877: for(d=0; d<dh[mi][i]; d++){
5878: newm=savm;
5879: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM;
5880: cov[2]=agexact;
5881: if(nagesqr==1)
5882: cov[3]= agexact*agexact;
5883: for (kk=1; kk<=cptcovage;kk++) {
5884: cov[Tage[kk]+2+nagesqr]=covar[Tvar[Tage[kk]]][i]*agexact;
5885: }
5886:
5887: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
5888: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
5889: savm=oldm;
5890: oldm=newm;
5891: } /* end mult */
5892:
5893: s1=s[mw[mi][i]][i];
5894: s2=s[mw[mi+1][i]][i];
5895: if( s2 > nlstate){
5896: lli=log(out[s1][s2] - savm[s1][s2]);
5897: } else if ( s2==-1 ) { /* alive */
5898: for (j=1,survp=0. ; j<=nlstate; j++)
5899: survp += out[s1][j];
5900: lli= log(survp);
5901: }else{
5902: lli=log(out[s[mw[mi][i]][i]][s[mw[mi+1][i]][i]]); /* Original formula */
5903: }
5904: ipmx +=1;
5905: sw += weight[i];
5906: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
5907: /* printf("num[i]=%09ld, 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",num[i],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])); */
5908: } /* end of wave */
5909: } /* end of individual */
5910: }else{ /* ml=5 no inter-extrapolation no jackson =0.8a */
5911: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
5912: for (k=1; k<=cptcovn;k++) cov[2+nagesqr+k]=covar[Tvar[k]][i];
5913: for(mi=1; mi<= wav[i]-1; mi++){
5914: for (ii=1;ii<=nlstate+ndeath;ii++)
5915: for (j=1;j<=nlstate+ndeath;j++){
5916: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
5917: savm[ii][j]=(ii==j ? 1.0 : 0.0);
5918: }
5919: for(d=0; d<dh[mi][i]; d++){
5920: newm=savm;
5921: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM;
5922: cov[2]=agexact;
5923: if(nagesqr==1)
5924: cov[3]= agexact*agexact;
5925: for (kk=1; kk<=cptcovage;kk++) {
5926: if(!FixedV[Tvar[Tage[kk]]])
5927: cov[Tage[kk]+2+nagesqr]=covar[Tvar[Tage[kk]]][i]*agexact; /* Tage[kk] gives the data-covariate associated with age */
5928: else
5929: cov[Tage[kk]+2+nagesqr]=cotvar[mw[mi][i]][Tvar[Tage[kk]]][i]*agexact; /* because cotvar starts now at first ncovcol+nqv+ (1 to nqtv) */
5930: }
5931:
5932: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
5933: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
5934: savm=oldm;
5935: oldm=newm;
5936: } /* end mult */
5937:
5938: s1=s[mw[mi][i]][i];
5939: s2=s[mw[mi+1][i]][i];
5940: lli=log(out[s[mw[mi][i]][i]][s[mw[mi+1][i]][i]]); /* Original formula */
5941: ipmx +=1;
5942: sw += weight[i];
5943: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
5944: /*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]);*/
5945: } /* end of wave */
5946: } /* end of individual */
5947: } /* End of if */
5948: for(k=1,l=0.; k<=nlstate; k++) l += ll[k];
5949: /* printf("l1=%f l2=%f ",ll[1],ll[2]); */
5950: l= l*ipmx/sw; /* To get the same order of magnitude as if weight=1 for every body */
5951: return -l;
5952: }
5953:
5954: /*************** log-likelihood *************/
5955: double funcone( double *x)
5956: {
5957: /* Same as func but slower because of a lot of printf and if */
5958: int i, ii, j, k, mi, d, kv=0, kf=0;
5959: int ioffset=0;
5960: int ipos=0,iposold=0,ncovv=0;
5961:
5962: double cotvarv, cotvarvold;
5963: double l, ll[NLSTATEMAX+1], cov[NCOVMAX+1];
5964: double **out;
5965: double lli; /* Individual log likelihood */
5966: double llt;
5967: int s1, s2;
5968: int iv=0, iqv=0, itv=0, iqtv=0 ; /* Index of varying covariate, fixed quantitative cov, time varying covariate, quantitative time varying covariate */
5969:
5970: double bbh, survp;
5971: double agexact;
5972: double agebegin, ageend;
5973: /*extern weight */
5974: /* We are differentiating ll according to initial status */
5975: /* for (i=1;i<=npar;i++) printf("%f ", x[i]);*/
5976: /*for(i=1;i<imx;i++)
5977: printf(" %d\n",s[4][i]);
5978: */
5979: cov[1]=1.;
5980:
5981: for(k=1; k<=nlstate; k++) ll[k]=0.;
5982: ioffset=0;
5983: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
5984: /* Computes the values of the ncovmodel covariates of the model
5985: depending if the covariates are fixed or varying (age dependent) and stores them in cov[]
5986: Then computes with function pmij which return a matrix p[i][j] giving the elementary probability
5987: to be observed in j being in i according to the model.
5988: */
5989: /* ioffset=2+nagesqr+cptcovage; */
5990: ioffset=2+nagesqr;
5991: /* Fixed */
5992: /* for (k=1; k<=cptcovn;k++) cov[2+nagesqr+k]=covar[Tvar[k]][i]; */
5993: /* for (k=1; k<=ncoveff;k++){ /\* Simple and product fixed Dummy covariates without age* products *\/ */
5994: for (kf=1; kf<=ncovf;kf++){ /* V2 + V3 + V4 Simple and product fixed covariates without age* products *//* Missing values are set to -1 but should be dropped */
5995: /* printf("Debug3 TvarFind[%d]=%d",kf, TvarFind[kf]); */
5996: /* printf(" Tvar[TvarFind[kf]]=%d", Tvar[TvarFind[kf]]); */
5997: /* printf(" i=%d covar[Tvar[TvarFind[kf]]][i]=%f\n",i,covar[Tvar[TvarFind[kf]]][i]); */
5998: 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)*/
5999: /* cov[ioffset+TvarFind[1]]=covar[Tvar[TvarFind[1]]][i]; */
6000: /* cov[2+6]=covar[Tvar[6]][i]; */
6001: /* cov[2+6]=covar[2][i]; V2 */
6002: /* cov[TvarFind[2]]=covar[Tvar[TvarFind[2]]][i]; */
6003: /* cov[2+7]=covar[Tvar[7]][i]; */
6004: /* cov[2+7]=covar[7][i]; V7=V1*V2 */
6005: /* cov[TvarFind[3]]=covar[Tvar[TvarFind[3]]][i]; */
6006: /* cov[2+9]=covar[Tvar[9]][i]; */
6007: /* cov[2+9]=covar[1][i]; V1 */
6008: }
6009: /* In model V2+V1*V4+age*V3+V3*V2 Tvar[1] is V2, Tvar[2=V1*V4]
6010: is 5, Tvar[3=age*V3] should not be computed because of age Tvar[4=V3*V2]=6
6011: has been calculated etc */
6012: /* For an individual i, wav[i] gives the number of effective waves */
6013: /* We compute the contribution to Likelihood of each effective transition
6014: mw[mi][i] is real wave of the mi th effectve wave */
6015: /* Then statuses are computed at each begin and end of an effective wave s1=s[ mw[mi][i] ][i];
6016: s2=s[mw[mi+1][i]][i];
6017: And the iv th varying covariate in the DATA is the cotvar[mw[mi+1][i]][ncovcol+nqv+iv][i]
6018: */
6019: /* This part may be useless now because everythin should be in covar */
6020: /* for (k=1; k<=nqfveff;k++){ /\* Simple and product fixed Quantitative covariates without age* products *\/ */
6021: /* 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?)*\/ */
6022: /* } */
6023: /* for(iqv=1; iqv <= nqfveff; iqv++){ /\* Quantitative fixed covariates *\/ */
6024: /* cov[++ioffset]=coqvar[Tvar[iqv]][i]; /\* Only V2 k=6 and V1*V2 7 *\/ */
6025: /* } */
6026:
6027:
6028: for(mi=1; mi<= wav[i]-1; mi++){ /* Varying with waves */
6029: /* Wave varying (but not age varying) *//* V1+V3+age*V1+age*V3+V1*V3 with V4 tv and V5 tvq k= 1 to 5 and extra at V(5+1)=6 for V1*V3 */
6030: /* for(k=1; k <= ncovv ; k++){ /\* Varying covariates (single and product but no age )*\/ */
6031: /* /\* cov[ioffset+TvarVind[k]]=cotvar[mw[mi][i]][Tvar[TvarVind[k]]][i]; *\/ */
6032: /* cov[ioffset+TvarVind[k]]=cotvar[mw[mi][i]][Tvar[TvarVind[k]]-ncovcol-nqv][i]; */
6033: /* } */
6034:
6035: /*# ID V1 V2 weight birth death 1st s1 V3 V4 V5 2nd s2 */
6036: /* model V1+V3+age*V1+age*V3+V1*V3 */
6037: /* Tvar={1, 3, 1, 3, 6}, the 6 comes from the fact that there are already V1, V2, V3, V4, V5 native covariates */
6038: /* TvarVV[1]=V3 (first time varying in the model equation, TvarVV[2]=V1 (in V1*V3) TvarVV[3]=3(V3) */
6039: /* We need the position of the time varying or product in the model */
6040: /* TvarVVind={2,5,5}, for V3 at position 2 and then the product V1*V3 is decomposed into V1 and V3 but at same position 5 */
6041: /* TvarVV gives the variable name */
6042: /* Other example V1 + V3 + V5 + age*V1 + age*V3 + age*V5 + V1*V3 + V3*V5 + V1*V5
6043: * k= 1 2 3 4 5 6 7 8 9
6044: * varying 1 2 3 4 5
6045: * ncovv 1 2 3 4 5 6 7 8
6046: * TvarVV[ncovv] V3 5 1 3 3 5 1 5
6047: * TvarVVind 2 3 7 7 8 8 9 9
6048: * TvarFind[k] 1 0 0 0 0 0 0 0 0
6049: */
6050: /* Other model ncovcol=5 nqv=0 ntv=3 nqtv=0 nlstate=3
6051: * V2 V3 V4 are fixed V6 V7 are timevarying so V8 and V5 are not in the model and product column will start at 9 Tvar[(v6*V2)6]=9
6052: * FixedV[ncovcol+qv+ntv+nqtv] V5
6053: * 3 V1 V2 V3 V4 V5 V6 V7 V8 V3*V2 V7*V2 V6*V3 V7*V3 V6*V4 V7*V4
6054: * 0 0 0 0 0 1 1 1 0, 0, 1,1, 1, 0, 1, 0, 1, 0, 1, 0}
6055: * 3 0 0 0 0 0 1 1 1 0, 1 1 1 1 1}
6056: * model= V2 + V3 + V4 + V6 + V7 + V6*V2 + V7*V2 + V6*V3 + V7*V3 + V6*V4 + V7*V4
6057: * +age*V2 +age*V3 +age*V4 +age*V6 + age*V7
6058: * +age*V6*V2 + age*V6*V3 +age*V7*V3 + age*V6*V4 +age*V7*V4
6059: * model2= V2 + V3 + V4 + V6 + V7 + V3*V2 + V7*V2 + V6*V3 + V7*V3 + V6*V4 + V7*V4
6060: * +age*V2 +age*V3 +age*V4 +age*V6 + age*V7
6061: * +age*V3*V2 + age*V6*V3 +age*V7*V3 + age*V6*V4 +age*V7*V4
6062: * model3= V2 + V3 + V4 + V6 + V7 + age*V3*V2 + V7*V2 + V6*V3 + V7*V3 + V6*V4 + V7*V4
6063: * +age*V2 +age*V3 +age*V4 +age*V6 + age*V7
6064: * +V3*V2 + age*V6*V3 +age*V7*V3 + age*V6*V4 +age*V7*V4
6065: * kmodel 1 2 3 4 5 6 7 8 9 10 11
6066: * 12 13 14 15 16
6067: * 17 18 19 20 21
6068: * Tvar[kmodel] 2 3 4 6 7 9 10 11 12 13 14
6069: * 2 3 4 6 7
6070: * 9 11 12 13 14
6071: * cptcovage=5+5 total of covariates with age
6072: * Tage[cptcovage] age*V2=12 13 14 15 16
6073: *1 17 18 19 20 21 gives the position in model of covariates associated with age
6074: *3 Tage[cptcovage] age*V3*V2=6
6075: *3 age*V2=12 13 14 15 16
6076: *3 age*V6*V3=18 19 20 21
6077: * Tvar[Tage[cptcovage]] Tvar[12]=2 3 4 6 Tvar[16]=7(age*V7)
6078: * Tvar[17]age*V6*V2=9 Tvar[18]age*V6*V3=11 age*V7*V3=12 age*V6*V4=13 Tvar[21]age*V7*V4=14
6079: * 2 Tvar[17]age*V3*V2=9 Tvar[18]age*V6*V3=11 age*V7*V3=12 age*V6*V4=13 Tvar[21]age*V7*V4=14
6080: * 3 Tvar[Tage[cptcovage]] Tvar[6]=9 Tvar[12]=2 3 4 6 Tvar[16]=7(age*V7)
6081: * 3 Tvar[18]age*V6*V3=11 age*V7*V3=12 age*V6*V4=13 Tvar[21]age*V7*V4=14
6082: * 3 Tage[cptcovage] age*V3*V2=6 age*V2=12 age*V3 13 14 15 16
6083: * age*V6*V3=18 19 20 21 gives the position in model of covariates associated with age
6084: * 3 Tvar[17]age*V3*V2=9 Tvar[18]age*V6*V3=11 age*V7*V3=12 age*V6*V4=13 Tvar[21]age*V7*V4=14
6085: * Tvar= {2, 3, 4, 6, 7,
6086: * 9, 10, 11, 12, 13, 14,
6087: * Tvar[12]=2, 3, 4, 6, 7,
6088: * Tvar[17]=9, 11, 12, 13, 14}
6089: * Typevar[1]@21 = {0, 0, 0, 0, 0,
6090: * 2, 2, 2, 2, 2, 2,
6091: * 3 3, 2, 2, 2, 2, 2,
6092: * 1, 1, 1, 1, 1,
6093: * 3, 3, 3, 3, 3}
6094: * 3 2, 3, 3, 3, 3}
6095: * p Tposprod[1]@21 {0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 0, 0, 0, 0, 0, 1, 3, 4, 5, 6} Id of the prod at position k in the model
6096: * p Tprod[1]@21 {6, 7, 8, 9, 10, 11, 0 <repeats 15 times>}
6097: * 3 Tposprod[1]@21 {0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 0, 0, 0, 0, 0, 1, 3, 4, 5, 6}
6098: * 3 Tprod[1]@21 {17, 7, 8, 9, 10, 11, 0 <repeats 15 times>}
6099: * cptcovprod=11 (6+5)
6100: * FixedV[Tvar[Tage[cptcovage]]]] FixedV[2]=0 FixedV[3]=0 0 1 (age*V7)Tvar[16]=1 FixedV[absolute] not [kmodel]
6101: * FixedV[Tvar[17]=FixedV[age*V6*V2]=FixedV[9]=1 1 1 1 1
6102: * 3 FixedV[Tvar[17]=FixedV[age*V3*V2]=FixedV[9]=0 [11]=1 1 1 1
6103: * FixedV[] V1=0 V2=0 V3=0 v4=0 V5=0 V6=1 V7=1 v8=1 OK then model dependent
6104: * 9=1 [V7*V2]=[10]=1 11=1 12=1 13=1 14=1
6105: * 3 9=0 [V7*V2]=[10]=1 11=1 12=1 13=1 14=1
6106: * cptcovdageprod=5 for gnuplot printing
6107: * cptcovprodvage=6
6108: * ncova=15 1 2 3 4 5
6109: * 6 7 8 9 10 11 12 13 14 15
6110: * TvarA 2 3 4 6 7
6111: * 6 2 6 7 7 3 6 4 7 4
6112: * TvaAind 12 12 13 13 14 14 15 15 16 16
6113: * ncovf 1 2 3
6114: * V6 V7 V6*V2 V7*V2 V6*V3 V7*V3 V6*V4 V7*V4
6115: * ncovvt=14 1 2 3 4 5 6 7 8 9 10 11 12 13 14
6116: * TvarVV[1]@14 = itv {V6=6, 7, V6*V2=6, 2, 7, 2, 6, 3, 7, 3, 6, 4, 7, 4}
6117: * TvarVVind[1]@14= {4, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11}
6118: * 3 ncovvt=12 V6 V7 V7*V2 V6*V3 V7*V3 V6*V4 V7*V4
6119: * 3 TvarVV[1]@12 = itv {6, 7, V7*V2=7, 2, 6, 3, 7, 3, 6, 4, 7, 4}
6120: * 3 1 2 3 4 5 6 7 8 9 10 11 12
6121: * TvarVVind[1]@12= {V6 is in k=4, 5, 7,(4isV2)=7, 8, 8, 9, 9, 10,10, 11,11}TvarVVind[12]=k=11
6122: * TvarV 6, 7, 9, 10, 11, 12, 13, 14
6123: * 3 cptcovprodvage=6
6124: * 3 ncovta=15 +age*V3*V2+age*V2+agev3+ageV4 +age*V6 + age*V7 + age*V6*V3 +age*V7*V3 + age*V6*V4 +age*V7*V4
6125: * 3 TvarAVVA[1]@15= itva 3 2 2 3 4 6 7 6 3 7 3 6 4 7 4
6126: * 3 ncovta 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
6127: *?TvarAVVAind[1]@15= V3 is in k=2 1 1 2 3 4 5 4,2 5,2, 4,3 5 3}TvarVVAind[]
6128: * TvarAVVAind[1]@15= V3 is in k=6 6 12 13 14 15 16 18 18 19,19, 20,20 21,21}TvarVVAind[]
6129: * 3 ncovvta=10 +age*V6 + age*V7 + age*V6*V3 +age*V7*V3 + age*V6*V4 +age*V7*V4
6130: * 3 we want to compute =cotvar[mw[mi][i]][TvarVVA[ncovva]][i] at position TvarVVAind[ncovva]
6131: * 3 TvarVVA[1]@10= itva 6 7 6 3 7 3 6 4 7 4
6132: * 3 ncovva 1 2 3 4 5 6 7 8 9 10
6133: * TvarVVAind[1]@10= V6 is in k=4 5 8,8 9, 9, 10,10 11 11}TvarVVAind[]
6134: * TvarVVAind[1]@10= 15 16 18,18 19,19, 20,20 21 21}TvarVVAind[]
6135: * TvarVA V3*V2=6 6 , 1, 2, 11, 12, 13, 14
6136: * TvarFind[1]@14= {1, 2, 3, 0 <repeats 12 times>}
6137: * Tvar[1]@21= {2, 3, 4, 6, 7, 9, 10, 11, 12, 13, 14,
6138: * 2, 3, 4, 6, 7,
6139: * 6, 8, 9, 10, 11}
6140: * TvarFind[itv] 0 0 0
6141: * FixedV[itv] 1 1 1 0 1 0 1 0 1 0 0
6142: *? FixedV[itv] 1 1 1 0 1 0 1 0 1 0 1 0 1 0
6143: * Tvar[TvarFind[ncovf]]=[1]=2 [2]=3 [4]=4
6144: * Tvar[TvarFind[itv]] [0]=? ?ncovv 1 à ncovvt]
6145: * Not a fixed cotvar[mw][itv][i] 6 7 6 2 7, 2, 6, 3, 7, 3, 6, 4, 7, 4}
6146: * fixed covar[itv] [6] [7] [6][2]
6147: */
6148:
6149: for(ncovv=1, iposold=0; ncovv <= ncovvt ; ncovv++){ /* V6 V7 V7*V2 V6*V3 V7*V3 V6*V4 V7*V4 Time varying covariates (single and extended product but no age) including individual from products, product is computed dynamically */
6150: itv=TvarVV[ncovv]; /* TvarVV={3, 1, 3} gives the name of each varying covariate, or fixed covariate of a varying product after exploding product Vn*Vm into Vn and then Vm */
6151: ipos=TvarVVind[ncovv]; /* TvarVVind={2, 5, 5] gives the position in the model of the ncovv th varying covariate*/
6152: /* if(TvarFind[itv]==0){ /\* Not a fixed covariate? Could be a fixed covariate of a product with a higher than ncovcol+nqv, itv *\/ */
6153: if(FixedV[itv]!=0){ /* Not a fixed covariate? Could be a fixed covariate of a product with a higher than ncovcol+nqv, itv */
6154: /* printf("DEBUG ncovv=%d, Varying TvarVV[ncovv]=%d\n",ncovv, TvarVV[ncovv]); */
6155: cotvarv=cotvar[mw[mi][i]][TvarVV[ncovv]][i]; /* because cotvar starts now at first ncovcol+nqv+ntv+nqtv (1 to nqtv) */
6156: /* printf("DEBUG Varying cov[ioffset+ipos=%d]=%g \n",ioffset+ipos,cotvarv); */
6157: }else{ /* fixed covariate */
6158: /* cotvarv=covar[Tvar[TvarFind[itv]]][i]; /\* Error: TvarFind gives the name, that is the true column of fixed covariates, but Tvar of the model *\/ */
6159: /* printf("DEBUG ncovv=%d, Fixed TvarVV[ncovv]=%d\n",ncovv, TvarVV[ncovv]); */
6160: cotvarv=covar[itv][i]; /* Good: In V6*V3, 3 is fixed at position of the data */
6161: /* printf("DEBUG Fixed cov[ioffset+ipos=%d]=%g \n",ioffset+ipos,cotvarv); */
6162: }
6163: if(ipos!=iposold){ /* Not a product or first of a product */
6164: cotvarvold=cotvarv;
6165: }else{ /* A second product */
6166: cotvarv=cotvarv*cotvarvold;
6167: }
6168: iposold=ipos;
6169: cov[ioffset+ipos]=cotvarv;
6170: /* printf("DEBUG Product cov[ioffset+ipos=%d] \n",ioffset+ipos); */
6171: /* For products */
6172: }
6173: /* for(itv=1; itv <= ntveff; itv++){ /\* Varying dummy covariates single *\/ */
6174: /* iv=TvarVDind[itv]; /\* iv, position in the model equation of time varying covariate itv *\/ */
6175: /* /\* "V1+V3+age*V1+age*V3+V1*V3" with V3 time varying *\/ */
6176: /* /\* 1 2 3 4 5 *\/ */
6177: /* /\*itv 1 *\/ */
6178: /* /\* TvarVInd[1]= 2 *\/ */
6179: /* /\* iv= Tvar[Tmodelind[itv]]-ncovcol-nqv; /\\* Counting the # varying covariate from 1 to ntveff *\\/ *\/ */
6180: /* /\* iv= Tvar[Tmodelind[ioffset-2-nagesqr-cptcovage+itv]]-ncovcol-nqv; *\/ */
6181: /* /\* cov[ioffset+iv]=cotvar[mw[mi][i]][iv][i]; *\/ */
6182: /* /\* k=ioffset-2-nagesqr-cptcovage+itv; /\\* position in simple model *\\/ *\/ */
6183: /* /\* cov[ioffset+iv]=cotvar[mw[mi][i]][TmodelInvind[itv]][i]; *\/ */
6184: /* cov[ioffset+iv]=cotvar[mw[mi][i]][itv][i]; */
6185: /* /\* printf(" i=%d,mi=%d,itv=%d,TmodelInvind[itv]=%d,cotvar[mw[mi][i]][itv][i]=%f\n", i, mi, itv, TvarVDind[itv],cotvar[mw[mi][i]][itv][i]); *\/ */
6186: /* } */
6187: /* for(iqtv=1; iqtv <= nqtveff; iqtv++){ /\* Varying quantitatives covariates *\/ */
6188: /* iv=TmodelInvQind[iqtv]; /\* Counting the # varying covariate from 1 to ntveff *\/ */
6189: /* /\* 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]); *\/ */
6190: /* cov[ioffset+ntveff+iqtv]=cotqvar[mw[mi][i]][TmodelInvQind[iqtv]][i]; */
6191: /* } */
6192: for (ii=1;ii<=nlstate+ndeath;ii++)
6193: for (j=1;j<=nlstate+ndeath;j++){
6194: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
6195: savm[ii][j]=(ii==j ? 1.0 : 0.0);
6196: }
6197:
6198: agebegin=agev[mw[mi][i]][i]; /* Age at beginning of effective wave */
6199: ageend=agev[mw[mi][i]][i] + (dh[mi][i])*stepm/YEARM; /* Age at end of effective wave and at the end of transition */
6200: for(d=0; d<dh[mi][i]; d++){ /* Delay between two effective waves */
6201: /* for(d=0; d<=0; d++){ /\* Delay between two effective waves Only one matrix to speed up*\/ */
6202: /*dh[m][i] or dh[mw[mi][i]][i] is the delay between two effective waves m=mw[mi][i]
6203: and mw[mi+1][i]. dh depends on stepm.*/
6204: newm=savm;
6205: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM; /* Here d is needed */
6206: cov[2]=agexact;
6207: if(nagesqr==1)
6208: cov[3]= agexact*agexact;
6209: for(ncovva=1, iposold=0; ncovva <= ncovta ; ncovva++){ /* Time varying covariates with age including individual from products, product is computed dynamically */
6210: itv=TvarAVVA[ncovva]; /* TvarVV={3, 1, 3} gives the name of each varying covariate, exploding product Vn*Vm into Vn and then Vm */
6211: ipos=TvarAVVAind[ncovva]; /* TvarVVind={2, 5, 5] gives the position in the model of the ncovv th varying covariate*/
6212: /* if(TvarFind[itv]==0){ /\* Not a fixed covariate? Could be a fixed covariate of a product with a higher than ncovcol+nqv, itv *\/ */
6213: if(FixedV[itv]!=0){ /* Not a fixed covariate? Could be a fixed covariate of a product with a higher than ncovcol+nqv, itv */
6214: /* printf("DEBUG ncovva=%d, Varying TvarAVVA[ncovva]=%d\n", ncovva, TvarAVVA[ncovva]); */
6215: cotvarv=cotvar[mw[mi][i]][TvarAVVA[ncovva]][i]; /* because cotvar starts now at first ncovcol+nqv+ntv+nqtv (1 to nqtv) */
6216: }else{ /* fixed covariate */
6217: /* cotvarv=covar[Tvar[TvarFind[itv]]][i]; /\* Error: TvarFind gives the name, that is the true column of fixed covariates, but Tvar of the model *\/ */
6218: /* printf("DEBUG ncovva=%d, Fixed TvarAVVA[ncovva]=%d\n", ncovva, TvarAVVA[ncovva]); */
6219: cotvarv=covar[itv][i]; /* Error: TvarFind gives the name, that is the true column of fixed covariates, but Tvar of the model */
6220: }
6221: if(ipos!=iposold){ /* Not a product or first of a product */
6222: cotvarvold=cotvarv;
6223: }else{ /* A second product */
6224: /* printf("DEBUG * \n"); */
6225: cotvarv=cotvarv*cotvarvold;
6226: }
6227: iposold=ipos;
6228: /* printf("DEBUG Product cov[ioffset+ipos=%d] \n",ioffset+ipos); */
6229: cov[ioffset+ipos]=cotvarv*agexact;
6230: /* For products */
6231: }
6232:
6233: /* printf("i=%d,mi=%d,d=%d,mw[mi][i]=%d\n",i, mi,d,mw[mi][i]); */
6234: /* savm=pmij(pmmij,cov,ncovmodel,x,nlstate); */
6235: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
6236: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
6237: /* out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath, */
6238: /* 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate)); */
6239: savm=oldm;
6240: oldm=newm;
6241: } /* end mult */
6242: /*lli=log(out[s[mw[mi][i]][i]][s[mw[mi+1][i]][i]]);*/ /* Original formula */
6243: /* But now since version 0.9 we anticipate for bias at large stepm.
6244: * If stepm is larger than one month (smallest stepm) and if the exact delay
6245: * (in months) between two waves is not a multiple of stepm, we rounded to
6246: * the nearest (and in case of equal distance, to the lowest) interval but now
6247: * we keep into memory the bias bh[mi][i] and also the previous matrix product
6248: * (i.e to dh[mi][i]-1) saved in 'savm'. Then we inter(extra)polate the
6249: * probability in order to take into account the bias as a fraction of the way
6250: * from savm to out if bh is negative or even beyond if bh is positive. bh varies
6251: * -stepm/2 to stepm/2 .
6252: * For stepm=1 the results are the same as for previous versions of Imach.
6253: * For stepm > 1 the results are less biased than in previous versions.
6254: */
6255: s1=s[mw[mi][i]][i];
6256: s2=s[mw[mi+1][i]][i];
6257: /* if(s2==-1){ */
6258: /* printf(" ERROR s1=%d, s2=%d i=%d \n", s1, s2, i); */
6259: /* /\* exit(1); *\/ */
6260: /* } */
6261: bbh=(double)bh[mi][i]/(double)stepm;
6262: /* bias is positive if real duration
6263: * is higher than the multiple of stepm and negative otherwise.
6264: */
6265: if( s2 > nlstate && (mle <5) ){ /* Jackson */
6266: lli=log(out[s1][s2] - savm[s1][s2]);
6267: } else if ( s2==-1 ) { /* alive */
6268: for (j=1,survp=0. ; j<=nlstate; j++)
6269: survp += (1.+bbh)*out[s1][j]- bbh*savm[s1][j];
6270: lli= log(survp);
6271: }else if (mle==1){
6272: lli= log((1.+bbh)*out[s1][s2]- bbh*savm[s1][s2]); /* linear interpolation */
6273: } else if(mle==2){
6274: 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 */
6275: } else if(mle==3){ /* exponential inter-extrapolation */
6276: 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 */
6277: } else if (mle==4){ /* mle=4 no inter-extrapolation */
6278: lli=log(out[s1][s2]); /* Original formula */
6279: } else{ /* mle=0 back to 1 */
6280: lli= log((1.+bbh)*out[s1][s2]- bbh*savm[s1][s2]); /* linear interpolation */
6281: /*lli=log(out[s1][s2]); */ /* Original formula */
6282: } /* End of if */
6283: ipmx +=1;
6284: sw += weight[i];
6285: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
6286: /* Printing covariates values for each contribution for checking */
6287: /* printf("num[i]=%09ld, 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",num[i],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])); */
6288: if(globpr){
6289: fprintf(ficresilk,"%09ld %6.1f %6.1f %6d %2d %2d %2d %2d %3d %15.6f %8.4f %8.3f\
6290: %11.6f %11.6f %11.6f ", \
6291: num[i], agebegin, ageend, i,s1,s2,mi,mw[mi][i],dh[mi][i],exp(lli),weight[i],weight[i]*gipmx/gsw,
6292: 2*weight[i]*lli,(s2==-1? -1: out[s1][s2]),(s2==-1? -1: savm[s1][s2]));
6293: /* printf("%09ld %6.1f %6.1f %6d %2d %2d %2d %2d %3d %15.6f %8.4f %8.3f\ */
6294: /* %11.6f %11.6f %11.6f ", \ */
6295: /* num[i], agebegin, ageend, i,s1,s2,mi,mw[mi][i],dh[mi][i],exp(lli),weight[i],weight[i]*gipmx/gsw, */
6296: /* 2*weight[i]*lli,(s2==-1? -1: out[s1][s2]),(s2==-1? -1: savm[s1][s2])); */
6297: for(k=1,llt=0.,l=0.; k<=nlstate; k++){
6298: llt +=ll[k]*gipmx/gsw;
6299: fprintf(ficresilk," %10.6f",-ll[k]*gipmx/gsw);
6300: /* printf(" %10.6f",-ll[k]*gipmx/gsw); */
6301: }
6302: fprintf(ficresilk," %10.6f ", -llt);
6303: /* printf(" %10.6f\n", -llt); */
6304: /* if(debugILK){ /\* debugILK is set by a #d in a comment line *\/ */
6305: /* fprintf(ficresilk,"%09ld ", num[i]); */ /* not necessary */
6306: for (kf=1; kf<=ncovf;kf++){ /* Simple and product fixed covariates without age* products *//* Missing values are set to -1 but should be dropped */
6307: fprintf(ficresilk," %g",covar[Tvar[TvarFind[kf]]][i]);
6308: }
6309: for(ncovv=1, iposold=0; ncovv <= ncovvt ; ncovv++){ /* Varying covariates (single and product but no age) including individual from products */
6310: ipos=TvarVVind[ncovv]; /* TvarVVind={2, 5, 5] gives the position in the model of the ncovv th varying covariate*/
6311: if(ipos!=iposold){ /* Not a product or first of a product */
6312: fprintf(ficresilk," %g",cov[ioffset+ipos]);
6313: /* printf(" %g",cov[ioffset+ipos]); */
6314: }else{
6315: fprintf(ficresilk,"*");
6316: /* printf("*"); */
6317: }
6318: iposold=ipos;
6319: }
6320: /* for (kk=1; kk<=cptcovage;kk++) { */
6321: /* if(!FixedV[Tvar[Tage[kk]]]){ */
6322: /* fprintf(ficresilk," %g*age",covar[Tvar[Tage[kk]]][i]); */
6323: /* /\* printf(" %g*age",covar[Tvar[Tage[kk]]][i]); *\/ */
6324: /* }else{ */
6325: /* fprintf(ficresilk," %g*age",cotvar[mw[mi][i]][Tvar[Tage[kk]]][i]);/\* because cotvar starts now at first ncovcol+nqv+ (1 to nqtv) *\/ */
6326: /* /\* printf(" %g*age",cotvar[mw[mi][i]][Tvar[Tage[kk]]][i]);/\\* because cotvar starts now at first ncovcol+nqv+ (1 to nqtv) *\\/ *\/ */
6327: /* } */
6328: /* } */
6329: for(ncovva=1, iposold=0; ncovva <= ncovta ; ncovva++){ /* Time varying covariates with age including individual from products, product is computed dynamically */
6330: itv=TvarAVVA[ncovva]; /* TvarVV={3, 1, 3} gives the name of each varying covariate, exploding product Vn*Vm into Vn and then Vm */
6331: ipos=TvarAVVAind[ncovva]; /* TvarVVind={2, 5, 5] gives the position in the model of the ncovv th varying covariate*/
6332: /* if(TvarFind[itv]==0){ /\* Not a fixed covariate? Could be a fixed covariate of a product with a higher than ncovcol+nqv, itv *\/ */
6333: if(FixedV[itv]!=0){ /* Not a fixed covariate? Could be a fixed covariate of a product with a higher than ncovcol+nqv, itv */
6334: /* printf("DEBUG ncovva=%d, Varying TvarAVVA[ncovva]=%d\n", ncovva, TvarAVVA[ncovva]); */
6335: cotvarv=cotvar[mw[mi][i]][TvarAVVA[ncovva]][i]; /* because cotvar starts now at first ncovcol+nqv+ntv+nqtv (1 to nqtv) */
6336: }else{ /* fixed covariate */
6337: /* cotvarv=covar[Tvar[TvarFind[itv]]][i]; /\* Error: TvarFind gives the name, that is the true column of fixed covariates, but Tvar of the model *\/ */
6338: /* printf("DEBUG ncovva=%d, Fixed TvarAVVA[ncovva]=%d\n", ncovva, TvarAVVA[ncovva]); */
6339: cotvarv=covar[itv][i]; /* Error: TvarFind gives the name, that is the true column of fixed covariates, but Tvar of the model */
6340: }
6341: if(ipos!=iposold){ /* Not a product or first of a product */
6342: cotvarvold=cotvarv;
6343: }else{ /* A second product */
6344: /* printf("DEBUG * \n"); */
6345: cotvarv=cotvarv*cotvarvold;
6346: }
6347: cotvarv=cotvarv*agexact;
6348: fprintf(ficresilk," %g*age",cotvarv);
6349: iposold=ipos;
6350: /* printf("DEBUG Product cov[ioffset+ipos=%d] \n",ioffset+ipos); */
6351: cov[ioffset+ipos]=cotvarv;
6352: /* For products */
6353: }
6354: /* printf("\n"); */
6355: /* } /\* End debugILK *\/ */
6356: fprintf(ficresilk,"\n");
6357: } /* End if globpr */
6358: } /* end of wave */
6359: } /* end of individual */
6360: for(k=1,l=0.; k<=nlstate; k++) l += ll[k];
6361: /* printf("l1=%f l2=%f ",ll[1],ll[2]); */
6362: l= l*ipmx/sw; /* To get the same order of magnitude as if weight=1 for every body */
6363: if(globpr==0){ /* First time we count the contributions and weights */
6364: gipmx=ipmx;
6365: gsw=sw;
6366: }
6367: return -l;
6368: }
6369:
6370:
6371: /*************** function likelione ***********/
6372: void likelione(FILE *ficres,double p[], int npar, int nlstate, int *globpri, long *ipmx, double *sw, double *fretone, double (*func)(double []))
6373: {
6374: /* This routine should help understanding what is done with
6375: the selection of individuals/waves and
6376: to check the exact contribution to the likelihood.
6377: Plotting could be done.
6378: */
6379: void pstamp(FILE *ficres);
6380: int k, kf, kk, kvar, ncovv, iposold, ipos;
6381:
6382: if(*globpri !=0){ /* Just counts and sums, no printings */
6383: strcpy(fileresilk,"ILK_");
6384: strcat(fileresilk,fileresu);
6385: if((ficresilk=fopen(fileresilk,"w"))==NULL) {
6386: printf("Problem with resultfile: %s\n", fileresilk);
6387: fprintf(ficlog,"Problem with resultfile: %s\n", fileresilk);
6388: }
6389: pstamp(ficresilk);fprintf(ficresilk,"# model=1+age+%s\n",model);
6390: 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");
6391: fprintf(ficresilk, "#num_i ageb agend i s1 s2 mi mw dh likeli weight %%weight 2wlli out sav ");
6392: /* i,s1,s2,mi,mw[mi][i],dh[mi][i],exp(lli),weight[i],2*weight[i]*lli,out[s1][s2],savm[s1][s2]); */
6393: for(k=1; k<=nlstate; k++)
6394: fprintf(ficresilk," -2*gipw/gsw*weight*ll[%d]++",k);
6395: fprintf(ficresilk," -2*gipw/gsw*weight*ll(total) ");
6396:
6397: /* if(debugILK){ /\* debugILK is set by a #d in a comment line *\/ */
6398: for(kf=1;kf <= ncovf; kf++){
6399: fprintf(ficresilk,"V%d",Tvar[TvarFind[kf]]);
6400: /* printf("V%d",Tvar[TvarFind[kf]]); */
6401: }
6402: for(ncovv=1, iposold=0; ncovv <= ncovvt ; ncovv++){
6403: ipos=TvarVVind[ncovv]; /* TvarVVind={2, 5, 5] gives the position in the model of the ncovv th varying covariate */
6404: if(ipos!=iposold){ /* Not a product or first of a product */
6405: /* printf(" %d",ipos); */
6406: fprintf(ficresilk," V%d",TvarVV[ncovv]);
6407: }else{
6408: /* printf("*"); */
6409: fprintf(ficresilk,"*");
6410: }
6411: iposold=ipos;
6412: }
6413: for (kk=1; kk<=cptcovage;kk++) {
6414: if(!FixedV[Tvar[Tage[kk]]]){
6415: /* printf(" %d*age(Fixed)",Tvar[Tage[kk]]); */
6416: fprintf(ficresilk," %d*age(Fixed)",Tvar[Tage[kk]]);
6417: }else{
6418: fprintf(ficresilk," %d*age(Varying)",Tvar[Tage[kk]]);/* because cotvar starts now at first ncovcol+nqv+ (1 to nqtv) */
6419: /* printf(" %d*age(Varying)",Tvar[Tage[kk]]);/\* because cotvar starts now at first ncovcol+nqv+ (1 to nqtv) *\/ */
6420: }
6421: }
6422: /* } /\* End if debugILK *\/ */
6423: /* printf("\n"); */
6424: fprintf(ficresilk,"\n");
6425: } /* End glogpri */
6426:
6427: *fretone=(*func)(p);
6428: if(*globpri !=0){
6429: fclose(ficresilk);
6430: if (mle ==0)
6431: fprintf(fichtm,"\n<br>File of contributions to the likelihood computed with initial parameters and mle = %d.",mle);
6432: else if(mle >=1)
6433: fprintf(fichtm,"\n<br>File of contributions to the likelihood computed with optimized parameters mle = %d.",mle);
6434: 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));
6435: fprintf(fichtm,"\n<br>Equation of the model: <b>model=1+age+%s</b><br>\n",model);
6436:
6437: 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> \
6438: <img src=\"%s-ori.png\">\n",subdirf2(optionfilefiname,"ILK_"),subdirf2(optionfilefiname,"ILK_"),subdirf2(optionfilefiname,"ILK_"));
6439: fprintf(fichtm,"<br>- and by state of destination <a href=\"%s-dest.png\">%s-dest.png</a><br> \
6440: <img src=\"%s-dest.png\">\n",subdirf2(optionfilefiname,"ILK_"),subdirf2(optionfilefiname,"ILK_"),subdirf2(optionfilefiname,"ILK_"));
6441:
6442: for (k=1; k<= nlstate ; k++) {
6443: 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>\n \
6444: <img src=\"%s-p%dj.png\">\n",k,k,subdirf2(optionfilefiname,"ILK_"),k,subdirf2(optionfilefiname,"ILK_"),k,subdirf2(optionfilefiname,"ILK_"),k);
6445: for(kf=1; kf <= ncovf; kf++){ /* For each simple dummy covariate of the model */
6446: kvar=Tvar[TvarFind[kf]]; /* variable */
6447: fprintf(fichtm,"<br>- Probability p<sub>%dj</sub> by origin %d and destination j with colored covariate V%d. Same dot size of all points but with a different color for transitions with dummy variable V%d=1 at beginning of transition (keeping former color for V%d=0): ",k,k,Tvar[TvarFind[kf]],Tvar[TvarFind[kf]],Tvar[TvarFind[kf]]);
6448: fprintf(fichtm,"<a href=\"%s-p%dj-%d.png\">%s-p%dj-%d.png</a><br>",subdirf2(optionfilefiname,"ILK_"),k,kvar,subdirf2(optionfilefiname,"ILK_"),k,kvar);
6449: fprintf(fichtm,"<img src=\"%s-p%dj-%d.png\">",subdirf2(optionfilefiname,"ILK_"),k,Tvar[TvarFind[kf]]);
6450: }
6451: for(ncovv=1, iposold=0; ncovv <= ncovvt ; ncovv++){ /* Loop on the time varying extended covariates (with extension of Vn*Vm */
6452: ipos=TvarVVind[ncovv]; /* TvarVVind={2, 5, 5] gives the position in the model of the ncovv th varying covariate */
6453: kvar=TvarVV[ncovv]; /* TvarVV={3, 1, 3} gives the name of each varying covariate */
6454: /* printf("DebugILK fichtm ncovv=%d, kvar=TvarVV[ncovv]=V%d, ipos=TvarVVind[ncovv]=%d, Dummy[ipos]=%d, Typevar[ipos]=%d\n", ncovv,kvar,ipos,Dummy[ipos],Typevar[ipos]); */
6455: if(ipos!=iposold){ /* Not a product or first of a product */
6456: /* fprintf(ficresilk," V%d",TvarVV[ncovv]); */
6457: /* printf(" DebugILK fichtm ipos=%d != iposold=%d\n", ipos, iposold); */
6458: if(Dummy[ipos]==0 && Typevar[ipos]==0){ /* Only if dummy time varying: Dummy(0, 1=quant singor prod without age,2 dummy*age, 3quant*age) Typevar (0 single, 1=*age,2=Vn*vm) */
6459: fprintf(fichtm,"<br>- Probability p<sub>%dj</sub> by origin %d and destination j with colored time varying dummy covariate V%d. Same dot size of all points but with a different color for transitions with dummy variable V%d=1 at beginning of transition (keeping former color for V%d=0): <a href=\"%s-p%dj.png\">%s-p%dj.png</a><br> \
6460: <img src=\"%s-p%dj-%d.png\">",k,k,kvar,kvar,kvar,subdirf2(optionfilefiname,"ILK_"),k,subdirf2(optionfilefiname,"ILK_"),k,subdirf2(optionfilefiname,"ILK_"),k,kvar);
6461: } /* End only for dummies time varying (single?) */
6462: }else{ /* Useless product */
6463: /* printf("*"); */
6464: /* fprintf(ficresilk,"*"); */
6465: }
6466: iposold=ipos;
6467: } /* For each time varying covariate */
6468: } /* End loop on states */
6469:
6470: /* if(debugILK){ */
6471: /* for(kf=1; kf <= ncovf; kf++){ /\* For each simple dummy covariate of the model *\/ */
6472: /* /\* kvar=Tvar[TvarFind[kf]]; *\/ /\* variable *\/ */
6473: /* for (k=1; k<= nlstate ; k++) { */
6474: /* fprintf(fichtm,"<br>- Probability p<sub>%dj</sub> by origin %d and destination j with colored covariate V%. Same dot size of all points but with a different color for transitions with dummy variable V%d=1 at beginning of transition (keeping former color for V%d=0): <a href=\"%s-p%dj.png\">%s-p%dj.png</a><br> \ */
6475: /* <img src=\"%s-p%dj-%d.png\">",k,k,Tvar[TvarFind[kf]],Tvar[TvarFind[kf]],Tvar[TvarFind[kf]],subdirf2(optionfilefiname,"ILK_"),k,subdirf2(optionfilefiname,"ILK_"),k,subdirf2(optionfilefiname,"ILK_"),k,Tvar[TvarFind[kf]]); */
6476: /* } */
6477: /* } */
6478: /* for(ncovv=1, iposold=0; ncovv <= ncovvt ; ncovv++){ /\* Loop on the time varying extended covariates (with extension of Vn*Vm *\/ */
6479: /* ipos=TvarVVind[ncovv]; /\* TvarVVind={2, 5, 5] gives the position in the model of the ncovv th varying covariate *\/ */
6480: /* kvar=TvarVV[ncovv]; /\* TvarVV={3, 1, 3} gives the name of each varying covariate *\/ */
6481: /* /\* printf("DebugILK fichtm ncovv=%d, kvar=TvarVV[ncovv]=V%d, ipos=TvarVVind[ncovv]=%d, Dummy[ipos]=%d, Typevar[ipos]=%d\n", ncovv,kvar,ipos,Dummy[ipos],Typevar[ipos]); *\/ */
6482: /* if(ipos!=iposold){ /\* Not a product or first of a product *\/ */
6483: /* /\* fprintf(ficresilk," V%d",TvarVV[ncovv]); *\/ */
6484: /* /\* printf(" DebugILK fichtm ipos=%d != iposold=%d\n", ipos, iposold); *\/ */
6485: /* if(Dummy[ipos]==0 && Typevar[ipos]==0){ /\* Only if dummy time varying: Dummy(0, 1=quant singor prod without age,2 dummy*age, 3quant*age) Typevar (0 single, 1=*age,2=Vn*vm) *\/ */
6486: /* for (k=1; k<= nlstate ; k++) { */
6487: /* fprintf(fichtm,"<br>- Probability p<sub>%dj</sub> by origin %d and destination j. Same dot size of all points but with a different color for transitions with dummy variable V%d=1 at beginning of transition (keeping former color for V%d=0): <a href=\"%s-p%dj.png\">%s-p%dj.png</a><br> \ */
6488: /* <img src=\"%s-p%dj-%d.png\">",k,k,kvar,kvar,subdirf2(optionfilefiname,"ILK_"),k,subdirf2(optionfilefiname,"ILK_"),k,subdirf2(optionfilefiname,"ILK_"),k,kvar); */
6489: /* } /\* End state *\/ */
6490: /* } /\* End only for dummies time varying (single?) *\/ */
6491: /* }else{ /\* Useless product *\/ */
6492: /* /\* printf("*"); *\/ */
6493: /* /\* fprintf(ficresilk,"*"); *\/ */
6494: /* } */
6495: /* iposold=ipos; */
6496: /* } /\* For each time varying covariate *\/ */
6497: /* }/\* End debugILK *\/ */
6498: fflush(fichtm);
6499: }/* End globpri */
6500: return;
6501: }
6502:
6503:
6504: /*********** Maximum Likelihood Estimation ***************/
6505:
6506: void mlikeli(FILE *ficres,double p[], int npar, int ncovmodel, int nlstate, double ftol, double (*func)(double []))
6507: {
6508: int i,j, jkk=0, iter=0;
6509: double **xi;
6510: /*double fret;*/
6511: /*double fretone;*/ /* Only one call to likelihood */
6512: /* char filerespow[FILENAMELENGTH];*/
6513:
6514: /*double * p1;*/ /* Shifted parameters from 0 instead of 1 */
6515: #ifdef NLOPT
6516: int creturn;
6517: nlopt_opt opt;
6518: /* double lb[9] = { -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL }; /\* lower bounds *\/ */
6519: double *lb;
6520: double minf; /* the minimum objective value, upon return */
6521:
6522: myfunc_data dinst, *d = &dinst;
6523: #endif
6524:
6525:
6526: xi=matrix(1,npar,1,npar);
6527: for (i=1;i<=npar;i++) /* Starting with canonical directions j=1,n xi[i=1,n][j] */
6528: for (j=1;j<=npar;j++)
6529: xi[i][j]=(i==j ? 1.0 : 0.0);
6530: printf("Powell-prax\n"); fprintf(ficlog,"Powell-prax\n");
6531: strcpy(filerespow,"POW_");
6532: strcat(filerespow,fileres);
6533: if((ficrespow=fopen(filerespow,"w"))==NULL) {
6534: printf("Problem with resultfile: %s\n", filerespow);
6535: fprintf(ficlog,"Problem with resultfile: %s\n", filerespow);
6536: }
6537: fprintf(ficrespow,"# Powell\n# iter -2*LL");
6538: for (i=1;i<=nlstate;i++)
6539: for(j=1;j<=nlstate+ndeath;j++)
6540: if(j!=i)fprintf(ficrespow," p%1d%1d",i,j);
6541: fprintf(ficrespow,"\n");
6542: #ifdef POWELL
6543: #ifdef LINMINORIGINAL
6544: #else /* LINMINORIGINAL */
6545:
6546: flatdir=ivector(1,npar);
6547: for (j=1;j<=npar;j++) flatdir[j]=0;
6548: #endif /*LINMINORIGINAL */
6549:
6550: #ifdef FLATSUP
6551: powell(p,xi,npar,ftol,&iter,&fret,flatdir,func);
6552: /* reorganizing p by suppressing flat directions */
6553: for(i=1, jk=1; i <=nlstate; i++){
6554: for(k=1; k <=(nlstate+ndeath); k++){
6555: if (k != i) {
6556: printf("%d%d flatdir[%d]=%d",i,k,jk, flatdir[jk]);
6557: if(flatdir[jk]==1){
6558: printf(" To be skipped %d%d flatdir[%d]=%d ",i,k,jk, flatdir[jk]);
6559: }
6560: for(j=1; j <=ncovmodel; j++){
6561: printf("%12.7f ",p[jk]);
6562: jk++;
6563: }
6564: printf("\n");
6565: }
6566: }
6567: }
6568: /* skipping */
6569: /* for(i=1, jk=1, jkk=1;(flatdir[jk]==0)&& (i <=nlstate); i++){ */
6570: for(i=1, jk=1, jkk=1;i <=nlstate; i++){
6571: for(k=1; k <=(nlstate+ndeath); k++){
6572: if (k != i) {
6573: printf("%d%d flatdir[%d]=%d",i,k,jk, flatdir[jk]);
6574: if(flatdir[jk]==1){
6575: printf(" To be skipped %d%d flatdir[%d]=%d jk=%d p[%d] ",i,k,jk, flatdir[jk],jk, jk);
6576: for(j=1; j <=ncovmodel; jk++,j++){
6577: printf(" p[%d]=%12.7f",jk, p[jk]);
6578: /*q[jjk]=p[jk];*/
6579: }
6580: }else{
6581: printf(" To be kept %d%d flatdir[%d]=%d jk=%d q[%d]=p[%d] ",i,k,jk, flatdir[jk],jk, jkk, jk);
6582: for(j=1; j <=ncovmodel; jk++,jkk++,j++){
6583: printf(" p[%d]=%12.7f=q[%d]",jk, p[jk],jkk);
6584: /*q[jjk]=p[jk];*/
6585: }
6586: }
6587: printf("\n");
6588: }
6589: fflush(stdout);
6590: }
6591: }
6592: powell(p,xi,npar,ftol,&iter,&fret,flatdir,func);
6593: #else /* FLATSUP */
6594: /* powell(p,xi,npar,ftol,&iter,&fret,func);*/
6595: /* praxis ( t0, h0, n, prin, x, beale_f ); */
6596: int prin=1;
6597: double h0=0.25;
6598: double macheps;
6599: double fmin;
6600: macheps=pow(16.0,-13.0);
6601: /* #include "praxis.h" */
6602: /* Be careful that praxis start at x[0] and powell start at p[1] */
6603: /* praxis ( ftol, h0, npar, prin, p, func ); */
6604: /* p1= (p+1); */ /* p *(p+1)@8 and p *(p1)@8 are equal p1[0]=p[1] */
6605: printf("Praxis Gegenfurtner \n");
6606: fprintf(ficlog, "Praxis Gegenfurtner\n");fflush(ficlog);
6607: /* praxis ( ftol, h0, npar, prin, p1, func ); */
6608: /* fmin = praxis(1.e-5,macheps, h, n, prin, x, func); */
6609: fmin = praxis(ftol,macheps, h0, npar, prin, p, func);
6610: printf("End Praxis\n");
6611: #endif /* FLATSUP */
6612:
6613: #ifdef LINMINORIGINAL
6614: #else
6615: free_ivector(flatdir,1,npar);
6616: #endif /* LINMINORIGINAL*/
6617: #endif /* POWELL */
6618:
6619: #ifdef NLOPT
6620: #ifdef NEWUOA
6621: opt = nlopt_create(NLOPT_LN_NEWUOA,npar);
6622: #else
6623: opt = nlopt_create(NLOPT_LN_BOBYQA,npar);
6624: #endif
6625: lb=vector(0,npar-1);
6626: for (i=0;i<npar;i++) lb[i]= -HUGE_VAL;
6627: nlopt_set_lower_bounds(opt, lb);
6628: nlopt_set_initial_step1(opt, 0.1);
6629:
6630: p1= (p+1); /* p *(p+1)@8 and p *(p1)@8 are equal p1[0]=p[1] */
6631: d->function = func;
6632: printf(" Func %.12lf \n",myfunc(npar,p1,NULL,d));
6633: nlopt_set_min_objective(opt, myfunc, d);
6634: nlopt_set_xtol_rel(opt, ftol);
6635: if ((creturn=nlopt_optimize(opt, p1, &minf)) < 0) {
6636: printf("nlopt failed! %d\n",creturn);
6637: }
6638: else {
6639: printf("found minimum after %d evaluations (NLOPT=%d)\n", countcallfunc ,NLOPT);
6640: printf("found minimum at f(%g,%g) = %0.10g\n", p[0], p[1], minf);
6641: iter=1; /* not equal */
6642: }
6643: nlopt_destroy(opt);
6644: #endif
6645: #ifdef FLATSUP
6646: /* npared = npar -flatd/ncovmodel; */
6647: /* xired= matrix(1,npared,1,npared); */
6648: /* paramred= ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel); */
6649: /* powell(pred,xired,npared,ftol,&iter,&fret,flatdir,func); */
6650: /* free_matrix(xire,1,npared,1,npared); */
6651: #else /* FLATSUP */
6652: #endif /* FLATSUP */
6653: free_matrix(xi,1,npar,1,npar);
6654: fclose(ficrespow);
6655: printf("\n#Number of iterations & function calls = %d & %d, -2 Log likelihood = %.12f\n",iter, countcallfunc,func(p));
6656: fprintf(ficlog,"\n#Number of iterations & function calls = %d & %d, -2 Log likelihood = %.12f\n",iter, countcallfunc,func(p));
6657: fprintf(ficres,"#Number of iterations & function calls = %d & %d, -2 Log likelihood = %.12f\n",iter, countcallfunc,func(p));
6658:
6659: }
6660:
6661: /**** Computes Hessian and covariance matrix ***/
6662: void hesscov(double **matcov, double **hess, double p[], int npar, double delti[], double ftolhess, double (*func)(double []))
6663: {
6664: double **a,**y,*x,pd;
6665: /* double **hess; */
6666: int i, j;
6667: int *indx;
6668:
6669: double hessii(double p[], double delta, int theta, double delti[],double (*func)(double []),int npar);
6670: double hessij(double p[], double **hess, double delti[], int i, int j,double (*func)(double []),int npar);
6671: void lubksb(double **a, int npar, int *indx, double b[]) ;
6672: void ludcmp(double **a, int npar, int *indx, double *d) ;
6673: double gompertz(double p[]);
6674: /* hess=matrix(1,npar,1,npar); */
6675:
6676: printf("\nCalculation of the hessian matrix. Wait...\n");
6677: fprintf(ficlog,"\nCalculation of the hessian matrix. Wait...\n");
6678: for (i=1;i<=npar;i++){
6679: printf("%d-",i);fflush(stdout);
6680: fprintf(ficlog,"%d-",i);fflush(ficlog);
6681:
6682: hess[i][i]=hessii(p,ftolhess,i,delti,func,npar);
6683:
6684: /* printf(" %f ",p[i]);
6685: printf(" %lf %lf %lf",hess[i][i],ftolhess,delti[i]);*/
6686: }
6687:
6688: for (i=1;i<=npar;i++) {
6689: for (j=1;j<=npar;j++) {
6690: if (j>i) {
6691: printf(".%d-%d",i,j);fflush(stdout);
6692: fprintf(ficlog,".%d-%d",i,j);fflush(ficlog);
6693: hess[i][j]=hessij(p,hess, delti,i,j,func,npar);
6694:
6695: hess[j][i]=hess[i][j];
6696: /*printf(" %lf ",hess[i][j]);*/
6697: }
6698: }
6699: }
6700: printf("\n");
6701: fprintf(ficlog,"\n");
6702:
6703: printf("\nInverting the hessian to get the covariance matrix. Wait...\n");
6704: fprintf(ficlog,"\nInverting the hessian to get the covariance matrix. Wait...\n");
6705:
6706: a=matrix(1,npar,1,npar);
6707: y=matrix(1,npar,1,npar);
6708: x=vector(1,npar);
6709: indx=ivector(1,npar);
6710: for (i=1;i<=npar;i++)
6711: for (j=1;j<=npar;j++) a[i][j]=hess[i][j];
6712: ludcmp(a,npar,indx,&pd);
6713:
6714: for (j=1;j<=npar;j++) {
6715: for (i=1;i<=npar;i++) x[i]=0;
6716: x[j]=1;
6717: lubksb(a,npar,indx,x);
6718: for (i=1;i<=npar;i++){
6719: matcov[i][j]=x[i];
6720: }
6721: }
6722:
6723: printf("\n#Hessian matrix#\n");
6724: fprintf(ficlog,"\n#Hessian matrix#\n");
6725: for (i=1;i<=npar;i++) {
6726: for (j=1;j<=npar;j++) {
6727: printf("%.6e ",hess[i][j]);
6728: fprintf(ficlog,"%.6e ",hess[i][j]);
6729: }
6730: printf("\n");
6731: fprintf(ficlog,"\n");
6732: }
6733:
6734: /* printf("\n#Covariance matrix#\n"); */
6735: /* fprintf(ficlog,"\n#Covariance matrix#\n"); */
6736: /* for (i=1;i<=npar;i++) { */
6737: /* for (j=1;j<=npar;j++) { */
6738: /* printf("%.6e ",matcov[i][j]); */
6739: /* fprintf(ficlog,"%.6e ",matcov[i][j]); */
6740: /* } */
6741: /* printf("\n"); */
6742: /* fprintf(ficlog,"\n"); */
6743: /* } */
6744:
6745: /* Recompute Inverse */
6746: /* for (i=1;i<=npar;i++) */
6747: /* for (j=1;j<=npar;j++) a[i][j]=matcov[i][j]; */
6748: /* ludcmp(a,npar,indx,&pd); */
6749:
6750: /* printf("\n#Hessian matrix recomputed#\n"); */
6751:
6752: /* for (j=1;j<=npar;j++) { */
6753: /* for (i=1;i<=npar;i++) x[i]=0; */
6754: /* x[j]=1; */
6755: /* lubksb(a,npar,indx,x); */
6756: /* for (i=1;i<=npar;i++){ */
6757: /* y[i][j]=x[i]; */
6758: /* printf("%.3e ",y[i][j]); */
6759: /* fprintf(ficlog,"%.3e ",y[i][j]); */
6760: /* } */
6761: /* printf("\n"); */
6762: /* fprintf(ficlog,"\n"); */
6763: /* } */
6764:
6765: /* Verifying the inverse matrix */
6766: #ifdef DEBUGHESS
6767: y=matprod2(y,hess,1,npar,1,npar,1,npar,matcov);
6768:
6769: printf("\n#Verification: multiplying the matrix of covariance by the Hessian matrix, should be unity:#\n");
6770: fprintf(ficlog,"\n#Verification: multiplying the matrix of covariance by the Hessian matrix. Should be unity:#\n");
6771:
6772: for (j=1;j<=npar;j++) {
6773: for (i=1;i<=npar;i++){
6774: printf("%.2f ",y[i][j]);
6775: fprintf(ficlog,"%.2f ",y[i][j]);
6776: }
6777: printf("\n");
6778: fprintf(ficlog,"\n");
6779: }
6780: #endif
6781:
6782: free_matrix(a,1,npar,1,npar);
6783: free_matrix(y,1,npar,1,npar);
6784: free_vector(x,1,npar);
6785: free_ivector(indx,1,npar);
6786: /* free_matrix(hess,1,npar,1,npar); */
6787:
6788:
6789: }
6790:
6791: /*************** hessian matrix ****************/
6792: double hessii(double x[], double delta, int theta, double delti[], double (*func)(double []), int npar)
6793: { /* Around values of x, computes the function func and returns the scales delti and hessian */
6794: int i;
6795: int l=1, lmax=20;
6796: double k1,k2, res, fx;
6797: double p2[MAXPARM+1]; /* identical to x */
6798: double delt=0.0001, delts, nkhi=10.,nkhif=1., khi=1.e-4;
6799: int k=0,kmax=10;
6800: double l1;
6801:
6802: fx=func(x);
6803: for (i=1;i<=npar;i++) p2[i]=x[i];
6804: for(l=0 ; l <=lmax; l++){ /* Enlarging the zone around the Maximum */
6805: l1=pow(10,l);
6806: delts=delt;
6807: for(k=1 ; k <kmax; k=k+1){
6808: delt = delta*(l1*k);
6809: p2[theta]=x[theta] +delt;
6810: k1=func(p2)-fx; /* Might be negative if too close to the theoretical maximum */
6811: p2[theta]=x[theta]-delt;
6812: k2=func(p2)-fx;
6813: /*res= (k1-2.0*fx+k2)/delt/delt; */
6814: res= (k1+k2)/delt/delt/2.; /* Divided by 2 because L and not 2*L */
6815:
6816: #ifdef DEBUGHESSII
6817: 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);
6818: 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);
6819: #endif
6820: /*if(fabs(k1-2.0*fx+k2) <1.e-13){ */
6821: if((k1 <khi/nkhi/2.) || (k2 <khi/nkhi/2.)){
6822: k=kmax;
6823: }
6824: else if((k1 >khi/nkhif) || (k2 >khi/nkhif)){ /* Keeps lastvalue before 3.84/2 KHI2 5% 1d.f. */
6825: k=kmax; l=lmax*10;
6826: }
6827: else if((k1 >khi/nkhi) || (k2 >khi/nkhi)){
6828: delts=delt;
6829: }
6830: } /* End loop k */
6831: }
6832: delti[theta]=delts;
6833: return res;
6834:
6835: }
6836:
6837: double hessij( double x[], double **hess, double delti[], int thetai,int thetaj,double (*func)(double []),int npar)
6838: {
6839: int i;
6840: int l=1, lmax=20;
6841: double k1,k2,k3,k4,res,fx;
6842: double p2[MAXPARM+1];
6843: int k, kmax=1;
6844: double v1, v2, cv12, lc1, lc2;
6845:
6846: int firstime=0;
6847:
6848: fx=func(x);
6849: for (k=1; k<=kmax; k=k+10) {
6850: for (i=1;i<=npar;i++) p2[i]=x[i];
6851: p2[thetai]=x[thetai]+delti[thetai]*k;
6852: p2[thetaj]=x[thetaj]+delti[thetaj]*k;
6853: k1=func(p2)-fx;
6854:
6855: p2[thetai]=x[thetai]+delti[thetai]*k;
6856: p2[thetaj]=x[thetaj]-delti[thetaj]*k;
6857: k2=func(p2)-fx;
6858:
6859: p2[thetai]=x[thetai]-delti[thetai]*k;
6860: p2[thetaj]=x[thetaj]+delti[thetaj]*k;
6861: k3=func(p2)-fx;
6862:
6863: p2[thetai]=x[thetai]-delti[thetai]*k;
6864: p2[thetaj]=x[thetaj]-delti[thetaj]*k;
6865: k4=func(p2)-fx;
6866: res=(k1-k2-k3+k4)/4.0/delti[thetai]/k/delti[thetaj]/k/2.; /* Because of L not 2*L */
6867: if(k1*k2*k3*k4 <0.){
6868: firstime=1;
6869: kmax=kmax+10;
6870: }
6871: if(kmax >=10 || firstime ==1){
6872: /* What are the thetai and thetaj? thetai/ncovmodel thetai=(thetai-thetai%ncovmodel)/ncovmodel +thetai%ncovmodel=(line,pos) */
6873: 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);
6874: 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);
6875: 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);
6876: 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);
6877: }
6878: #ifdef DEBUGHESSIJ
6879: v1=hess[thetai][thetai];
6880: v2=hess[thetaj][thetaj];
6881: cv12=res;
6882: /* Computing eigen value of Hessian matrix */
6883: lc1=((v1+v2)+sqrt((v1+v2)*(v1+v2) - 4*(v1*v2-cv12*cv12)))/2.;
6884: lc2=((v1+v2)-sqrt((v1+v2)*(v1+v2) - 4*(v1*v2-cv12*cv12)))/2.;
6885: if ((lc2 <0) || (lc1 <0) ){
6886: printf("Warning: sub Hessian matrix '%d%d' does not have positive eigen values \n",thetai,thetaj);
6887: fprintf(ficlog, "Warning: sub Hessian matrix '%d%d' does not have positive eigen values \n",thetai,thetaj);
6888: 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);
6889: 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);
6890: }
6891: #endif
6892: }
6893: return res;
6894: }
6895:
6896: /* Not done yet: Was supposed to fix if not exactly at the maximum */
6897: /* double hessij( double x[], double delti[], int thetai,int thetaj,double (*func)(double []),int npar) */
6898: /* { */
6899: /* int i; */
6900: /* int l=1, lmax=20; */
6901: /* double k1,k2,k3,k4,res,fx; */
6902: /* double p2[MAXPARM+1]; */
6903: /* double delt=0.0001, delts, nkhi=10.,nkhif=1., khi=1.e-4; */
6904: /* int k=0,kmax=10; */
6905: /* double l1; */
6906:
6907: /* fx=func(x); */
6908: /* for(l=0 ; l <=lmax; l++){ /\* Enlarging the zone around the Maximum *\/ */
6909: /* l1=pow(10,l); */
6910: /* delts=delt; */
6911: /* for(k=1 ; k <kmax; k=k+1){ */
6912: /* delt = delti*(l1*k); */
6913: /* for (i=1;i<=npar;i++) p2[i]=x[i]; */
6914: /* p2[thetai]=x[thetai]+delti[thetai]/k; */
6915: /* p2[thetaj]=x[thetaj]+delti[thetaj]/k; */
6916: /* k1=func(p2)-fx; */
6917:
6918: /* p2[thetai]=x[thetai]+delti[thetai]/k; */
6919: /* p2[thetaj]=x[thetaj]-delti[thetaj]/k; */
6920: /* k2=func(p2)-fx; */
6921:
6922: /* p2[thetai]=x[thetai]-delti[thetai]/k; */
6923: /* p2[thetaj]=x[thetaj]+delti[thetaj]/k; */
6924: /* k3=func(p2)-fx; */
6925:
6926: /* p2[thetai]=x[thetai]-delti[thetai]/k; */
6927: /* p2[thetaj]=x[thetaj]-delti[thetaj]/k; */
6928: /* k4=func(p2)-fx; */
6929: /* res=(k1-k2-k3+k4)/4.0/delti[thetai]*k/delti[thetaj]*k/2.; /\* Because of L not 2*L *\/ */
6930: /* #ifdef DEBUGHESSIJ */
6931: /* 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); */
6932: /* 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); */
6933: /* #endif */
6934: /* if((k1 <khi/nkhi/2.) || (k2 <khi/nkhi/2.)|| (k4 <khi/nkhi/2.)|| (k4 <khi/nkhi/2.)){ */
6935: /* k=kmax; */
6936: /* } */
6937: /* else if((k1 >khi/nkhif) || (k2 >khi/nkhif) || (k4 >khi/nkhif) || (k4 >khi/nkhif)){ /\* Keeps lastvalue before 3.84/2 KHI2 5% 1d.f. *\/ */
6938: /* k=kmax; l=lmax*10; */
6939: /* } */
6940: /* else if((k1 >khi/nkhi) || (k2 >khi/nkhi)){ */
6941: /* delts=delt; */
6942: /* } */
6943: /* } /\* End loop k *\/ */
6944: /* } */
6945: /* delti[theta]=delts; */
6946: /* return res; */
6947: /* } */
6948:
6949:
6950: /************** Inverse of matrix **************/
6951: void ludcmp(double **a, int n, int *indx, double *d)
6952: {
6953: int i,imax,j,k;
6954: double big,dum,sum,temp;
6955: double *vv;
6956:
6957: vv=vector(1,n);
6958: *d=1.0;
6959: for (i=1;i<=n;i++) {
6960: big=0.0;
6961: for (j=1;j<=n;j++)
6962: if ((temp=fabs(a[i][j])) > big) big=temp;
6963: if (big == 0.0){
6964: printf(" Singular Hessian matrix at row %d:\n",i);
6965: for (j=1;j<=n;j++) {
6966: printf(" a[%d][%d]=%f,",i,j,a[i][j]);
6967: fprintf(ficlog," a[%d][%d]=%f,",i,j,a[i][j]);
6968: }
6969: fflush(ficlog);
6970: fclose(ficlog);
6971: nrerror("Singular matrix in routine ludcmp");
6972: }
6973: vv[i]=1.0/big;
6974: }
6975: for (j=1;j<=n;j++) {
6976: for (i=1;i<j;i++) {
6977: sum=a[i][j];
6978: for (k=1;k<i;k++) sum -= a[i][k]*a[k][j];
6979: a[i][j]=sum;
6980: }
6981: big=0.0;
6982: for (i=j;i<=n;i++) {
6983: sum=a[i][j];
6984: for (k=1;k<j;k++)
6985: sum -= a[i][k]*a[k][j];
6986: a[i][j]=sum;
6987: if ( (dum=vv[i]*fabs(sum)) >= big) {
6988: big=dum;
6989: imax=i;
6990: }
6991: }
6992: if (j != imax) {
6993: for (k=1;k<=n;k++) {
6994: dum=a[imax][k];
6995: a[imax][k]=a[j][k];
6996: a[j][k]=dum;
6997: }
6998: *d = -(*d);
6999: vv[imax]=vv[j];
7000: }
7001: indx[j]=imax;
7002: if (a[j][j] == 0.0) a[j][j]=TINY;
7003: if (j != n) {
7004: dum=1.0/(a[j][j]);
7005: for (i=j+1;i<=n;i++) a[i][j] *= dum;
7006: }
7007: }
7008: free_vector(vv,1,n); /* Doesn't work */
7009: ;
7010: }
7011:
7012: void lubksb(double **a, int n, int *indx, double b[])
7013: {
7014: int i,ii=0,ip,j;
7015: double sum;
7016:
7017: for (i=1;i<=n;i++) {
7018: ip=indx[i];
7019: sum=b[ip];
7020: b[ip]=b[i];
7021: if (ii)
7022: for (j=ii;j<=i-1;j++) sum -= a[i][j]*b[j];
7023: else if (sum) ii=i;
7024: b[i]=sum;
7025: }
7026: for (i=n;i>=1;i--) {
7027: sum=b[i];
7028: for (j=i+1;j<=n;j++) sum -= a[i][j]*b[j];
7029: b[i]=sum/a[i][i];
7030: }
7031: }
7032:
7033: void pstamp(FILE *fichier)
7034: {
7035: fprintf(fichier,"# %s.%s\n#IMaCh version %s, %s\n#%s\n# %s", optionfilefiname,optionfilext,version,copyright, fullversion, strstart);
7036: }
7037:
7038: void date2dmy(double date,double *day, double *month, double *year){
7039: double yp=0., yp1=0., yp2=0.;
7040:
7041: yp1=modf(date,&yp);/* extracts integral of date in yp and
7042: fractional in yp1 */
7043: *year=yp;
7044: yp2=modf((yp1*12),&yp);
7045: *month=yp;
7046: yp1=modf((yp2*30.5),&yp);
7047: *day=yp;
7048: if(*day==0) *day=1;
7049: if(*month==0) *month=1;
7050: }
7051:
7052:
7053:
7054: /************ Frequencies ********************/
7055: void freqsummary(char fileres[], double p[], double pstart[], int iagemin, int iagemax, int **s, double **agev, int nlstate, int imx, \
7056: int *Tvaraff, int *invalidvarcomb, int **nbcode, int *ncodemax,double **mint,double **anint, char strstart[], \
7057: int firstpass, int lastpass, int stepm, int weightopt, char model[])
7058: { /* Some frequencies as well as proposing some starting values */
7059: /* Frequencies of any combination of dummy covariate used in the model equation */
7060: int i, m, jk, j1, bool, z1,j, nj, nl, k, iv, jj=0, s1=1, s2=1;
7061: int iind=0, iage=0;
7062: int mi; /* Effective wave */
7063: int first;
7064: double ***freq; /* Frequencies */
7065: 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 */
7066: 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);
7067: double *meanq, *stdq, *idq;
7068: double **meanqt;
7069: double *pp, **prop, *posprop, *pospropt;
7070: double pos=0., posproptt=0., pospropta=0., k2, dateintsum=0,k2cpt=0;
7071: char fileresp[FILENAMELENGTH], fileresphtm[FILENAMELENGTH], fileresphtmfr[FILENAMELENGTH];
7072: double agebegin, ageend;
7073:
7074: pp=vector(1,nlstate);
7075: prop=matrix(1,nlstate,iagemin-AGEMARGE,iagemax+4+AGEMARGE);
7076: posprop=vector(1,nlstate); /* Counting the number of transition starting from a live state per age */
7077: pospropt=vector(1,nlstate); /* Counting the number of transition starting from a live state */
7078: /* prop=matrix(1,nlstate,iagemin,iagemax+3); */
7079: meanq=vector(1,nqfveff); /* Number of Quantitative Fixed Variables Effective */
7080: stdq=vector(1,nqfveff); /* Number of Quantitative Fixed Variables Effective */
7081: idq=vector(1,nqfveff); /* Number of Quantitative Fixed Variables Effective */
7082: meanqt=matrix(1,lastpass,1,nqtveff);
7083: strcpy(fileresp,"P_");
7084: strcat(fileresp,fileresu);
7085: /*strcat(fileresphtm,fileresu);*/
7086: if((ficresp=fopen(fileresp,"w"))==NULL) {
7087: printf("Problem with prevalence resultfile: %s\n", fileresp);
7088: fprintf(ficlog,"Problem with prevalence resultfile: %s\n", fileresp);
7089: exit(0);
7090: }
7091:
7092: strcpy(fileresphtm,subdirfext(optionfilefiname,"PHTM_",".htm"));
7093: if((ficresphtm=fopen(fileresphtm,"w"))==NULL) {
7094: printf("Problem with prevalence HTM resultfile '%s' with errno='%s'\n",fileresphtm,strerror(errno));
7095: fprintf(ficlog,"Problem with prevalence HTM resultfile '%s' with errno='%s'\n",fileresphtm,strerror(errno));
7096: fflush(ficlog);
7097: exit(70);
7098: }
7099: else{
7100: fprintf(ficresphtm,"<html><head>\n<title>IMaCh PHTM_ %s</title></head>\n <body><font size=\"2\">%s <br> %s</font> \
7101: <hr size=\"2\" color=\"#EC5E5E\"> \n \
7102: Title=%s <br>Datafile=%s Firstpass=%d Lastpass=%d Stepm=%d Weight=%d Model=1+age+%s<br>\n",\
7103: fileresphtm,version,fullversion,title,datafile,firstpass,lastpass,stepm, weightopt, model);
7104: }
7105: 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);
7106:
7107: strcpy(fileresphtmfr,subdirfext(optionfilefiname,"PHTMFR_",".htm"));
7108: if((ficresphtmfr=fopen(fileresphtmfr,"w"))==NULL) {
7109: printf("Problem with frequency table HTM resultfile '%s' with errno='%s'\n",fileresphtmfr,strerror(errno));
7110: fprintf(ficlog,"Problem with frequency table HTM resultfile '%s' with errno='%s'\n",fileresphtmfr,strerror(errno));
7111: fflush(ficlog);
7112: exit(70);
7113: } else{
7114: fprintf(ficresphtmfr,"<html><head>\n<title>IMaCh PHTM_Frequency table %s</title></head>\n <body><font size=\"2\">%s <br> %s</font> \
7115: ,<hr size=\"2\" color=\"#EC5E5E\"> \n \
7116: Title=%s <br>Datafile=%s Firstpass=%d Lastpass=%d Stepm=%d Weight=%d Model=1+age+%s<br>\n",\
7117: fileresphtmfr,version,fullversion,title,datafile,firstpass,lastpass,stepm, weightopt, model);
7118: }
7119: 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);
7120:
7121: y= vector(iagemin-AGEMARGE,iagemax+4+AGEMARGE);
7122: x= vector(iagemin-AGEMARGE,iagemax+4+AGEMARGE);
7123: freq= ma3x(-5,nlstate+ndeath,-5,nlstate+ndeath,iagemin-AGEMARGE,iagemax+4+AGEMARGE);
7124: j1=0;
7125:
7126: /* j=ncoveff; /\* Only fixed dummy covariates *\/ */
7127: j=cptcoveff; /* Only simple dummy covariates used in the model */
7128: /* j=cptcovn; /\* Only dummy covariates of the model *\/ */
7129: if (cptcovn<1) {j=1;ncodemax[1]=1;}
7130:
7131:
7132: /* Detects if a combination j1 is empty: for a multinomial variable like 3 education levels:
7133: reference=low_education V1=0,V2=0
7134: med_educ V1=1 V2=0,
7135: high_educ V1=0 V2=1
7136: Then V1=1 and V2=1 is a noisy combination that we want to exclude for the list 2**cptcovn
7137: */
7138: dateintsum=0;
7139: k2cpt=0;
7140:
7141: if(cptcoveff == 0 )
7142: nl=1; /* Constant and age model only */
7143: else
7144: nl=2;
7145:
7146: /* if a constant only model, one pass to compute frequency tables and to write it on ficresp */
7147: /* Loop on nj=1 or 2 if dummy covariates j!=0
7148: * Loop on j1(1 to 2**cptcoveff) covariate combination
7149: * freq[s1][s2][iage] =0.
7150: * Loop on iind
7151: * ++freq[s1][s2][iage] weighted
7152: * end iind
7153: * if covariate and j!0
7154: * headers Variable on one line
7155: * endif cov j!=0
7156: * header of frequency table by age
7157: * Loop on age
7158: * pp[s1]+=freq[s1][s2][iage] weighted
7159: * pos+=freq[s1][s2][iage] weighted
7160: * Loop on s1 initial state
7161: * fprintf(ficresp
7162: * end s1
7163: * end age
7164: * if j!=0 computes starting values
7165: * end compute starting values
7166: * end j1
7167: * end nl
7168: */
7169: for (nj = 1; nj <= nl; nj++){ /* nj= 1 constant model, nl number of loops. */
7170: if(nj==1)
7171: j=0; /* First pass for the constant */
7172: else{
7173: j=cptcoveff; /* Other passes for the covariate values number of simple covariates in the model V2+V1 =2 (simple dummy fixed or time varying) */
7174: }
7175: first=1;
7176: 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 */
7177: posproptt=0.;
7178: /*printf("cptcovn=%d Tvaraff=%d", cptcovn,Tvaraff[1]);
7179: scanf("%d", i);*/
7180: for (i=-5; i<=nlstate+ndeath; i++)
7181: for (s2=-5; s2<=nlstate+ndeath; s2++)
7182: for(m=iagemin; m <= iagemax+3; m++)
7183: freq[i][s2][m]=0;
7184:
7185: for (i=1; i<=nlstate; i++) {
7186: for(m=iagemin; m <= iagemax+3; m++)
7187: prop[i][m]=0;
7188: posprop[i]=0;
7189: pospropt[i]=0;
7190: }
7191: for (z1=1; z1<= nqfveff; z1++) { /* zeroing for each combination j1 as well as for the total */
7192: idq[z1]=0.;
7193: meanq[z1]=0.;
7194: stdq[z1]=0.;
7195: }
7196: /* for (z1=1; z1<= nqtveff; z1++) { */
7197: /* for(m=1;m<=lastpass;m++){ */
7198: /* meanqt[m][z1]=0.; */
7199: /* } */
7200: /* } */
7201: /* dateintsum=0; */
7202: /* k2cpt=0; */
7203:
7204: /* For that combination of covariates j1 (V4=1 V3=0 for example), we count and print the frequencies in one pass */
7205: for (iind=1; iind<=imx; iind++) { /* For each individual iind */
7206: bool=1;
7207: if(j !=0){
7208: if(anyvaryingduminmodel==0){ /* If All fixed covariates */
7209: if (cptcoveff >0) { /* Filter is here: Must be looked at for model=V1+V2+V3+V4 */
7210: for (z1=1; z1<=cptcoveff; z1++) { /* loops on covariates in the model */
7211: /* if(Tvaraff[z1] ==-20){ */
7212: /* /\* sumnew+=cotvar[mw[mi][iind]][z1][iind]; *\/ */
7213: /* }else if(Tvaraff[z1] ==-10){ */
7214: /* /\* sumnew+=coqvar[z1][iind]; *\/ */
7215: /* }else */ /* TODO TODO codtabm(j1,z1) or codtabm(j1,Tvaraff[z1]]z1)*/
7216: /* 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); */
7217: if(Tvaraff[z1]<1 || Tvaraff[z1]>=NCOVMAX)
7218: printf("Error Tvaraff[z1]=%d<1 or >=%d, cptcoveff=%d model=1+age+%s\n",Tvaraff[z1],NCOVMAX, cptcoveff, model);
7219: if (covar[Tvaraff[z1]][iind]!= nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]){ /* for combination j1 of covariates */
7220: /* Tests if the value of the covariate z1 for this individual iind responded to combination j1 (V4=1 V3=0) */
7221: bool=0; /* bool should be equal to 1 to be selected, one covariate value failed */
7222: /* 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", */
7223: /* bool,i,z1, z1, Tvaraff[z1],i,covar[Tvaraff[z1]][i],j1,z1,codtabm(j1,z1),*/
7224: /* j1,z1,nbcode[Tvaraff[z1]][codtabm(j1,z1)],j1);*/
7225: /* For j1=7 in V1+V2+V3+V4 = 0 1 1 0 and codtabm(7,3)=1 and nbcde[3][?]=1*/
7226: } /* Onlyf fixed */
7227: } /* end z1 */
7228: } /* cptcoveff > 0 */
7229: } /* end any */
7230: }/* end j==0 */
7231: if (bool==1){ /* We selected an individual iind satisfying combination j1 (V4=1 V3=0) or all fixed covariates */
7232: /* for(m=firstpass; m<=lastpass; m++){ */
7233: for(mi=1; mi<wav[iind];mi++){ /* For each wave */
7234: m=mw[mi][iind];
7235: if(j!=0){
7236: if(anyvaryingduminmodel==1){ /* Some are varying covariates */
7237: for (z1=1; z1<=cptcoveff; z1++) {
7238: if( Fixed[Tmodelind[z1]]==1){
7239: /* iv= Tvar[Tmodelind[z1]]-ncovcol-nqv; /\* Good *\/ */
7240: iv= Tvar[Tmodelind[z1]]; /* Good *//* because cotvar starts now at first at ncovcol+nqv+ntv */
7241: if (cotvar[m][iv][iind]!= nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]) /* iv=1 to ntv, right modality. If covariate's
7242: value is -1, we don't select. It differs from the
7243: constant and age model which counts them. */
7244: bool=0; /* not selected */
7245: }else if( Fixed[Tmodelind[z1]]== 0) { /* fixed */
7246: /* i1=Tvaraff[z1]; */
7247: /* i2=TnsdVar[i1]; */
7248: /* i3=nbcode[i1][i2]; */
7249: /* i4=covar[i1][iind]; */
7250: /* if(i4 != i3){ */
7251: if (covar[Tvaraff[z1]][iind]!= nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]) { /* Bug valgrind */
7252: bool=0;
7253: }
7254: }
7255: }
7256: }/* Some are varying covariates, we tried to speed up if all fixed covariates in the model, avoiding waves loop */
7257: } /* end j==0 */
7258: /* bool =0 we keep that guy which corresponds to the combination of dummy values */
7259: if(bool==1){ /*Selected */
7260: /* dh[m][iind] or dh[mw[mi][iind]][iind] is the delay between two effective (mi) waves m=mw[mi][iind]
7261: and mw[mi+1][iind]. dh depends on stepm. */
7262: agebegin=agev[m][iind]; /* Age at beginning of wave before transition*/
7263: ageend=agev[m][iind]+(dh[m][iind])*stepm/YEARM; /* Age at end of wave and transition */
7264: if(m >=firstpass && m <=lastpass){
7265: k2=anint[m][iind]+(mint[m][iind]/12.);
7266: /*if ((k2>=dateprev1) && (k2<=dateprev2)) {*/
7267: if(agev[m][iind]==0) agev[m][iind]=iagemax+1; /* All ages equal to 0 are in iagemax+1 */
7268: if(agev[m][iind]==1) agev[m][iind]=iagemax+2; /* All ages equal to 1 are in iagemax+2 */
7269: if (s[m][iind]>0 && s[m][iind]<=nlstate) /* If status at wave m is known and a live state */
7270: prop[s[m][iind]][(int)agev[m][iind]] += weight[iind]; /* At age of beginning of transition, where status is known */
7271: if (m<lastpass) {
7272: /* if(s[m][iind]==4 && s[m+1][iind]==4) */
7273: /* 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]); */
7274: if(s[m][iind]==-1)
7275: 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.));
7276: freq[s[m][iind]][s[m+1][iind]][(int)agev[m][iind]] += weight[iind]; /* At age of beginning of transition, where status is known */
7277: for (z1=1; z1<= nqfveff; z1++) { /* Quantitative variables, calculating mean on known values only */
7278: if(!isnan(covar[ncovcol+z1][iind])){
7279: idq[z1]=idq[z1]+weight[iind];
7280: meanq[z1]+=covar[ncovcol+z1][iind]*weight[iind]; /* Computes mean of quantitative with selected filter */
7281: /* stdq[z1]+=covar[ncovcol+z1][iind]*covar[ncovcol+z1][iind]*weight[iind]*weight[iind]; *//*error*/
7282: stdq[z1]+=covar[ncovcol+z1][iind]*covar[ncovcol+z1][iind]*weight[iind]; /* *weight[iind];*/ /* Computes mean of quantitative with selected filter */
7283: }
7284: }
7285: /* if((int)agev[m][iind] == 55) */
7286: /* printf("j=%d, j1=%d Age %d, iind=%d, num=%09ld m=%d\n",j,j1,(int)agev[m][iind],iind, num[iind],m); */
7287: /* freq[s[m][iind]][s[m+1][iind]][(int)((agebegin+ageend)/2.)] += weight[iind]; */
7288: 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 */
7289: }
7290: } /* end if between passes */
7291: if ((agev[m][iind]>1) && (agev[m][iind]< (iagemax+3)) && (anint[m][iind]!=9999) && (mint[m][iind]!=99) && (j==0)) {
7292: dateintsum=dateintsum+k2; /* on all covariates ?*/
7293: k2cpt++;
7294: /* printf("iind=%ld dateintmean = %lf dateintsum=%lf k2cpt=%lf k2=%lf\n",iind, dateintsum/k2cpt, dateintsum,k2cpt, k2); */
7295: }
7296: }else{
7297: bool=1;
7298: }/* end bool 2 */
7299: } /* end m */
7300: /* for (z1=1; z1<= nqfveff; z1++) { /\* Quantitative variables, calculating mean *\/ */
7301: /* idq[z1]=idq[z1]+weight[iind]; */
7302: /* meanq[z1]+=covar[ncovcol+z1][iind]*weight[iind]; /\* Computes mean of quantitative with selected filter *\/ */
7303: /* stdq[z1]+=covar[ncovcol+z1][iind]*covar[ncovcol+z1][iind]*weight[iind]*weight[iind]; /\* *weight[iind];*\/ /\* Computes mean of quantitative with selected filter *\/ */
7304: /* } */
7305: } /* end bool */
7306: } /* end iind = 1 to imx */
7307: /* prop[s][age] is fed for any initial and valid live state as well as
7308: freq[s1][s2][age] at single age of beginning the transition, for a combination j1 */
7309:
7310:
7311: /* fprintf(ficresp, "#Count between %.lf/%.lf/%.lf and %.lf/%.lf/%.lf\n",jprev1, mprev1,anprev1,jprev2, mprev2,anprev2);*/
7312: if(cptcoveff==0 && nj==1) /* no covariate and first pass */
7313: pstamp(ficresp);
7314: if (cptcoveff>0 && j!=0){
7315: pstamp(ficresp);
7316: printf( "\n#********** Variable ");
7317: fprintf(ficresp, "\n#********** Variable ");
7318: fprintf(ficresphtm, "\n<br/><br/><h3>********** Variable ");
7319: fprintf(ficresphtmfr, "\n<br/><br/><h3>********** Variable ");
7320: fprintf(ficlog, "\n#********** Variable ");
7321: for (z1=1; z1<=cptcoveff; z1++){
7322: if(!FixedV[Tvaraff[z1]]){
7323: printf( "V%d(fixed)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]);
7324: fprintf(ficresp, "V%d(fixed)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]);
7325: fprintf(ficresphtm, "V%d(fixed)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]);
7326: fprintf(ficresphtmfr, "V%d(fixed)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]);
7327: fprintf(ficlog, "V%d(fixed)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]);
7328: }else{
7329: printf( "V%d(varying)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]);
7330: fprintf(ficresp, "V%d(varying)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]);
7331: fprintf(ficresphtm, "V%d(varying)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]);
7332: fprintf(ficresphtmfr, "V%d(varying)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]);
7333: fprintf(ficlog, "V%d(varying)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]);
7334: }
7335: }
7336: printf( "**********\n#");
7337: fprintf(ficresp, "**********\n#");
7338: fprintf(ficresphtm, "**********</h3>\n");
7339: fprintf(ficresphtmfr, "**********</h3>\n");
7340: fprintf(ficlog, "**********\n");
7341: }
7342: /*
7343: Printing means of quantitative variables if any
7344: */
7345: for (z1=1; z1<= nqfveff; z1++) {
7346: fprintf(ficlog,"Mean of fixed quantitative variable V%d on %.3g (weighted) individuals sum=%f", ncovcol+z1, idq[z1], meanq[z1]);
7347: fprintf(ficlog,", mean=%.3g\n",meanq[z1]/idq[z1]);
7348: if(weightopt==1){
7349: printf(" Weighted mean and standard deviation of");
7350: fprintf(ficlog," Weighted mean and standard deviation of");
7351: fprintf(ficresphtmfr," Weighted mean and standard deviation of");
7352: }
7353: /* mu = \frac{w x}{\sum w}
7354: var = \frac{\sum w (x-mu)^2}{\sum w} = \frac{w x^2}{\sum w} - mu^2
7355: */
7356: 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]));
7357: 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]));
7358: 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]));
7359: }
7360: /* for (z1=1; z1<= nqtveff; z1++) { */
7361: /* for(m=1;m<=lastpass;m++){ */
7362: /* fprintf(ficresphtmfr,"V quantitative id %d, pass id=%d, mean=%f<p>\n", z1, m, meanqt[m][z1]); */
7363: /* } */
7364: /* } */
7365:
7366: fprintf(ficresphtm,"<table style=\"text-align:center; border: 1px solid\">");
7367: if((cptcoveff==0 && nj==1)|| nj==2 ) /* no covariate and first pass */
7368: fprintf(ficresp, " Age");
7369: if(nj==2) for (z1=1; z1<=cptcoveff; z1++) {
7370: 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]]);
7371: fprintf(ficresp, " V%d=%d",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]);
7372: }
7373: for(i=1; i<=nlstate;i++) {
7374: if((cptcoveff==0 && nj==1)|| nj==2 ) fprintf(ficresp," Prev(%d) N(%d) N ",i,i);
7375: fprintf(ficresphtm, "<th>Age</th><th>Prev(%d)</th><th>N(%d)</th><th>N</th>",i,i);
7376: }
7377: if((cptcoveff==0 && nj==1)|| nj==2 ) fprintf(ficresp, "\n");
7378: fprintf(ficresphtm, "\n");
7379:
7380: /* Header of frequency table by age */
7381: fprintf(ficresphtmfr,"<table style=\"text-align:center; border: 1px solid\">");
7382: fprintf(ficresphtmfr,"<th>Age</th> ");
7383: for(s2=-1; s2 <=nlstate+ndeath; s2++){
7384: for(m=-1; m <=nlstate+ndeath; m++){
7385: if(s2!=0 && m!=0)
7386: fprintf(ficresphtmfr,"<th>%d%d</th> ",s2,m);
7387: }
7388: }
7389: fprintf(ficresphtmfr, "\n");
7390:
7391: /* For each age */
7392: for(iage=iagemin; iage <= iagemax+3; iage++){
7393: fprintf(ficresphtm,"<tr>");
7394: if(iage==iagemax+1){
7395: fprintf(ficlog,"1");
7396: fprintf(ficresphtmfr,"<tr><th>0</th> ");
7397: }else if(iage==iagemax+2){
7398: fprintf(ficlog,"0");
7399: fprintf(ficresphtmfr,"<tr><th>Unknown</th> ");
7400: }else if(iage==iagemax+3){
7401: fprintf(ficlog,"Total");
7402: fprintf(ficresphtmfr,"<tr><th>Total</th> ");
7403: }else{
7404: if(first==1){
7405: first=0;
7406: printf("See log file for details...\n");
7407: }
7408: fprintf(ficresphtmfr,"<tr><th>%d</th> ",iage);
7409: fprintf(ficlog,"Age %d", iage);
7410: }
7411: for(s1=1; s1 <=nlstate ; s1++){
7412: for(m=-1, pp[s1]=0; m <=nlstate+ndeath ; m++)
7413: pp[s1] += freq[s1][m][iage];
7414: }
7415: for(s1=1; s1 <=nlstate ; s1++){
7416: for(m=-1, pos=0; m <=0 ; m++)
7417: pos += freq[s1][m][iage];
7418: if(pp[s1]>=1.e-10){
7419: if(first==1){
7420: printf(" %d.=%.0f loss[%d]=%.1f%%",s1,pp[s1],s1,100*pos/pp[s1]);
7421: }
7422: fprintf(ficlog," %d.=%.0f loss[%d]=%.1f%%",s1,pp[s1],s1,100*pos/pp[s1]);
7423: }else{
7424: if(first==1)
7425: printf(" %d.=%.0f loss[%d]=NaNQ%%",s1,pp[s1],s1);
7426: fprintf(ficlog," %d.=%.0f loss[%d]=NaNQ%%",s1,pp[s1],s1);
7427: }
7428: }
7429:
7430: for(s1=1; s1 <=nlstate ; s1++){
7431: /* posprop[s1]=0; */
7432: for(m=0, pp[s1]=0; m <=nlstate+ndeath; m++)/* Summing on all ages */
7433: pp[s1] += freq[s1][m][iage];
7434: } /* pp[s1] is the total number of transitions starting from state s1 and any ending status until this age */
7435:
7436: for(s1=1,pos=0, pospropta=0.; s1 <=nlstate ; s1++){
7437: pos += pp[s1]; /* pos is the total number of transitions until this age */
7438: posprop[s1] += prop[s1][iage]; /* prop is the number of transitions from a live state
7439: from s1 at age iage prop[s[m][iind]][(int)agev[m][iind]] += weight[iind] */
7440: pospropta += prop[s1][iage]; /* prop is the number of transitions from a live state
7441: from s1 at age iage prop[s[m][iind]][(int)agev[m][iind]] += weight[iind] */
7442: }
7443:
7444: /* Writing ficresp */
7445: if(cptcoveff==0 && nj==1){ /* no covariate and first pass */
7446: if( iage <= iagemax){
7447: fprintf(ficresp," %d",iage);
7448: }
7449: }else if( nj==2){
7450: if( iage <= iagemax){
7451: fprintf(ficresp," %d",iage);
7452: for (z1=1; z1<=cptcoveff; z1++) fprintf(ficresp, " %d %d",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]);
7453: }
7454: }
7455: for(s1=1; s1 <=nlstate ; s1++){
7456: if(pos>=1.e-5){
7457: if(first==1)
7458: printf(" %d.=%.0f prev[%d]=%.1f%%",s1,pp[s1],s1,100*pp[s1]/pos);
7459: fprintf(ficlog," %d.=%.0f prev[%d]=%.1f%%",s1,pp[s1],s1,100*pp[s1]/pos);
7460: }else{
7461: if(first==1)
7462: printf(" %d.=%.0f prev[%d]=NaNQ%%",s1,pp[s1],s1);
7463: fprintf(ficlog," %d.=%.0f prev[%d]=NaNQ%%",s1,pp[s1],s1);
7464: }
7465: if( iage <= iagemax){
7466: if(pos>=1.e-5){
7467: if(cptcoveff==0 && nj==1){ /* no covariate and first pass */
7468: fprintf(ficresp," %.5f %.0f %.0f",prop[s1][iage]/pospropta, prop[s1][iage],pospropta);
7469: }else if( nj==2){
7470: fprintf(ficresp," %.5f %.0f %.0f",prop[s1][iage]/pospropta, prop[s1][iage],pospropta);
7471: }
7472: fprintf(ficresphtm,"<th>%d</th><td>%.5f</td><td>%.0f</td><td>%.0f</td>",iage,prop[s1][iage]/pospropta, prop[s1][iage],pospropta);
7473: /*probs[iage][s1][j1]= pp[s1]/pos;*/
7474: /*printf("\niage=%d s1=%d j1=%d %.5f %.0f %.0f %f",iage,s1,j1,pp[s1]/pos, pp[s1],pos,probs[iage][s1][j1]);*/
7475: } else{
7476: if((cptcoveff==0 && nj==1)|| nj==2 ) fprintf(ficresp," NaNq %.0f %.0f",prop[s1][iage],pospropta);
7477: fprintf(ficresphtm,"<th>%d</th><td>NaNq</td><td>%.0f</td><td>%.0f</td>",iage, prop[s1][iage],pospropta);
7478: }
7479: }
7480: pospropt[s1] +=posprop[s1];
7481: } /* end loop s1 */
7482: /* pospropt=0.; */
7483: for(s1=-1; s1 <=nlstate+ndeath; s1++){
7484: for(m=-1; m <=nlstate+ndeath; m++){
7485: if(freq[s1][m][iage] !=0 ) { /* minimizing output */
7486: if(first==1){
7487: printf(" %d%d=%.0f",s1,m,freq[s1][m][iage]);
7488: }
7489: /* printf(" %d%d=%.0f",s1,m,freq[s1][m][iage]); */
7490: fprintf(ficlog," %d%d=%.0f",s1,m,freq[s1][m][iage]);
7491: }
7492: if(s1!=0 && m!=0)
7493: fprintf(ficresphtmfr,"<td>%.0f</td> ",freq[s1][m][iage]);
7494: }
7495: } /* end loop s1 */
7496: posproptt=0.;
7497: for(s1=1; s1 <=nlstate; s1++){
7498: posproptt += pospropt[s1];
7499: }
7500: fprintf(ficresphtmfr,"</tr>\n ");
7501: fprintf(ficresphtm,"</tr>\n");
7502: if((cptcoveff==0 && nj==1)|| nj==2 ) {
7503: if(iage <= iagemax)
7504: fprintf(ficresp,"\n");
7505: }
7506: if(first==1)
7507: printf("Others in log...\n");
7508: fprintf(ficlog,"\n");
7509: } /* end loop age iage */
7510:
7511: fprintf(ficresphtm,"<tr><th>Tot</th>");
7512: for(s1=1; s1 <=nlstate ; s1++){
7513: if(posproptt < 1.e-5){
7514: fprintf(ficresphtm,"<td>Nanq</td><td>%.0f</td><td>%.0f</td>",pospropt[s1],posproptt);
7515: }else{
7516: fprintf(ficresphtm,"<td>%.5f</td><td>%.0f</td><td>%.0f</td>",pospropt[s1]/posproptt,pospropt[s1],posproptt);
7517: }
7518: }
7519: fprintf(ficresphtm,"</tr>\n");
7520: fprintf(ficresphtm,"</table>\n");
7521: fprintf(ficresphtmfr,"</table>\n");
7522: if(posproptt < 1.e-5){
7523: fprintf(ficresphtm,"\n <p><b> This combination (%d) is not valid and no result will be produced</b></p>",j1);
7524: fprintf(ficresphtmfr,"\n <p><b> This combination (%d) is not valid and no result will be produced</b></p>",j1);
7525: fprintf(ficlog,"# This combination (%d) is not valid and no result will be produced\n",j1);
7526: printf("# This combination (%d) is not valid and no result will be produced\n",j1);
7527: invalidvarcomb[j1]=1;
7528: }else{
7529: fprintf(ficresphtm,"\n <p> This combination (%d) is valid and result will be produced (or no resultline).</p>",j1);
7530: invalidvarcomb[j1]=0;
7531: }
7532: fprintf(ficresphtmfr,"</table>\n");
7533: fprintf(ficlog,"\n");
7534: if(j!=0){
7535: printf("#Freqsummary: Starting values for combination j1=%d:\n", j1);
7536: for(i=1,s1=1; i <=nlstate; i++){
7537: for(k=1; k <=(nlstate+ndeath); k++){
7538: if (k != i) {
7539: for(jj=1; jj <=ncovmodel; jj++){ /* For counting s1 */
7540: if(jj==1){ /* Constant case (in fact cste + age) */
7541: if(j1==1){ /* All dummy covariates to zero */
7542: freq[i][k][iagemax+4]=freq[i][k][iagemax+3]; /* Stores case 0 0 0 */
7543: freq[i][i][iagemax+4]=freq[i][i][iagemax+3]; /* Stores case 0 0 0 */
7544: printf("%d%d ",i,k);
7545: fprintf(ficlog,"%d%d ",i,k);
7546: 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]));
7547: 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]));
7548: pstart[s1]= log(freq[i][k][iagemax+3]/freq[i][i][iagemax+3]);
7549: }
7550: }else if((j1==1) && (jj==2 || nagesqr==1)){ /* age or age*age parameter without covariate V4*age (to be done later) */
7551: for(iage=iagemin; iage <= iagemax+3; iage++){
7552: x[iage]= (double)iage;
7553: y[iage]= log(freq[i][k][iage]/freq[i][i][iage]);
7554: /* printf("i=%d, k=%d, s1=%d, j1=%d, jj=%d, y[%d]=%f\n",i,k,s1,j1,jj, iage, y[iage]); */
7555: }
7556: /* Some are not finite, but linreg will ignore these ages */
7557: no=0;
7558: linreg(iagemin,iagemax,&no,x,y,&a,&b,&r, &sa, &sb ); /* y= a+b*x with standard errors */
7559: pstart[s1]=b;
7560: pstart[s1-1]=a;
7561: }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 */
7562: 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]);
7563: 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]);
7564: pstart[s1]= log((freq[i][k][iagemax+3]/freq[i][i][iagemax+3])/(freq[i][k][iagemax+4]/freq[i][i][iagemax+4]));
7565: printf("%d%d ",i,k);
7566: fprintf(ficlog,"%d%d ",i,k);
7567: 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]));
7568: }else{ /* Other cases, like quantitative fixed or varying covariates */
7569: ;
7570: }
7571: /* printf("%12.7f )", param[i][jj][k]); */
7572: /* fprintf(ficlog,"%12.7f )", param[i][jj][k]); */
7573: s1++;
7574: } /* end jj */
7575: } /* end k!= i */
7576: } /* end k */
7577: } /* end i, s1 */
7578: } /* end j !=0 */
7579: } /* end selected combination of covariate j1 */
7580: if(j==0){ /* We can estimate starting values from the occurences in each case */
7581: printf("#Freqsummary: Starting values for the constants:\n");
7582: fprintf(ficlog,"\n");
7583: for(i=1,s1=1; i <=nlstate; i++){
7584: for(k=1; k <=(nlstate+ndeath); k++){
7585: if (k != i) {
7586: printf("%d%d ",i,k);
7587: fprintf(ficlog,"%d%d ",i,k);
7588: for(jj=1; jj <=ncovmodel; jj++){
7589: pstart[s1]=p[s1]; /* Setting pstart to p values by default */
7590: if(jj==1){ /* Age has to be done */
7591: pstart[s1]= log(freq[i][k][iagemax+3]/freq[i][i][iagemax+3]);
7592: 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]));
7593: 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]));
7594: }
7595: /* printf("%12.7f )", param[i][jj][k]); */
7596: /* fprintf(ficlog,"%12.7f )", param[i][jj][k]); */
7597: s1++;
7598: }
7599: printf("\n");
7600: fprintf(ficlog,"\n");
7601: }
7602: }
7603: } /* end of state i */
7604: printf("#Freqsummary\n");
7605: fprintf(ficlog,"\n");
7606: for(s1=-1; s1 <=nlstate+ndeath; s1++){
7607: for(s2=-1; s2 <=nlstate+ndeath; s2++){
7608: /* param[i]|j][k]= freq[s1][s2][iagemax+3] */
7609: printf(" %d%d=%.0f",s1,s2,freq[s1][s2][iagemax+3]);
7610: fprintf(ficlog," %d%d=%.0f",s1,s2,freq[s1][s2][iagemax+3]);
7611: /* if(freq[s1][s2][iage] !=0 ) { /\* minimizing output *\/ */
7612: /* printf(" %d%d=%.0f",s1,s2,freq[s1][s2][iagemax+3]); */
7613: /* fprintf(ficlog," %d%d=%.0f",s1,s2,freq[s1][s2][iagemax+3]); */
7614: /* } */
7615: }
7616: } /* end loop s1 */
7617:
7618: printf("\n");
7619: fprintf(ficlog,"\n");
7620: } /* end j=0 */
7621: } /* end j */
7622:
7623: if(mle == -2){ /* We want to use these values as starting values */
7624: for(i=1, jk=1; i <=nlstate; i++){
7625: for(j=1; j <=nlstate+ndeath; j++){
7626: if(j!=i){
7627: /*ca[0]= k+'a'-1;ca[1]='\0';*/
7628: printf("%1d%1d",i,j);
7629: fprintf(ficparo,"%1d%1d",i,j);
7630: for(k=1; k<=ncovmodel;k++){
7631: /* printf(" %lf",param[i][j][k]); */
7632: /* fprintf(ficparo," %lf",param[i][j][k]); */
7633: p[jk]=pstart[jk];
7634: printf(" %f ",pstart[jk]);
7635: fprintf(ficparo," %f ",pstart[jk]);
7636: jk++;
7637: }
7638: printf("\n");
7639: fprintf(ficparo,"\n");
7640: }
7641: }
7642: }
7643: } /* end mle=-2 */
7644: dateintmean=dateintsum/k2cpt;
7645: date2dmy(dateintmean,&jintmean,&mintmean,&aintmean);
7646:
7647: fclose(ficresp);
7648: fclose(ficresphtm);
7649: fclose(ficresphtmfr);
7650: free_vector(idq,1,nqfveff);
7651: free_vector(meanq,1,nqfveff);
7652: free_vector(stdq,1,nqfveff);
7653: free_matrix(meanqt,1,lastpass,1,nqtveff);
7654: free_vector(x, iagemin-AGEMARGE, iagemax+4+AGEMARGE);
7655: free_vector(y, iagemin-AGEMARGE, iagemax+4+AGEMARGE);
7656: free_ma3x(freq,-5,nlstate+ndeath,-5,nlstate+ndeath, iagemin-AGEMARGE, iagemax+4+AGEMARGE);
7657: free_vector(pospropt,1,nlstate);
7658: free_vector(posprop,1,nlstate);
7659: free_matrix(prop,1,nlstate,iagemin-AGEMARGE, iagemax+4+AGEMARGE);
7660: free_vector(pp,1,nlstate);
7661: /* End of freqsummary */
7662: }
7663:
7664: /* Simple linear regression */
7665: int linreg(int ifi, int ila, int *no, const double x[], const double y[], double* a, double* b, double* r, double* sa, double * sb) {
7666:
7667: /* y=a+bx regression */
7668: double sumx = 0.0; /* sum of x */
7669: double sumx2 = 0.0; /* sum of x**2 */
7670: double sumxy = 0.0; /* sum of x * y */
7671: double sumy = 0.0; /* sum of y */
7672: double sumy2 = 0.0; /* sum of y**2 */
7673: double sume2 = 0.0; /* sum of square or residuals */
7674: double yhat;
7675:
7676: double denom=0;
7677: int i;
7678: int ne=*no;
7679:
7680: for ( i=ifi, ne=0;i<=ila;i++) {
7681: if(!isfinite(x[i]) || !isfinite(y[i])){
7682: /* printf(" x[%d]=%f, y[%d]=%f\n",i,x[i],i,y[i]); */
7683: continue;
7684: }
7685: ne=ne+1;
7686: sumx += x[i];
7687: sumx2 += x[i]*x[i];
7688: sumxy += x[i] * y[i];
7689: sumy += y[i];
7690: sumy2 += y[i]*y[i];
7691: denom = (ne * sumx2 - sumx*sumx);
7692: /* 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); */
7693: }
7694:
7695: denom = (ne * sumx2 - sumx*sumx);
7696: if (denom == 0) {
7697: // vertical, slope m is infinity
7698: *b = INFINITY;
7699: *a = 0;
7700: if (r) *r = 0;
7701: return 1;
7702: }
7703:
7704: *b = (ne * sumxy - sumx * sumy) / denom;
7705: *a = (sumy * sumx2 - sumx * sumxy) / denom;
7706: if (r!=NULL) {
7707: *r = (sumxy - sumx * sumy / ne) / /* compute correlation coeff */
7708: sqrt((sumx2 - sumx*sumx/ne) *
7709: (sumy2 - sumy*sumy/ne));
7710: }
7711: *no=ne;
7712: for ( i=ifi, ne=0;i<=ila;i++) {
7713: if(!isfinite(x[i]) || !isfinite(y[i])){
7714: /* printf(" x[%d]=%f, y[%d]=%f\n",i,x[i],i,y[i]); */
7715: continue;
7716: }
7717: ne=ne+1;
7718: yhat = y[i] - *a -*b* x[i];
7719: sume2 += yhat * yhat ;
7720:
7721: denom = (ne * sumx2 - sumx*sumx);
7722: /* 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); */
7723: }
7724: *sb = sqrt(sume2/(double)(ne-2)/(sumx2 - sumx * sumx /(double)ne));
7725: *sa= *sb * sqrt(sumx2/ne);
7726:
7727: return 0;
7728: }
7729:
7730: /************ Prevalence ********************/
7731: 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)
7732: {
7733: /* Compute observed prevalence between dateprev1 and dateprev2 by counting the number of people
7734: in each health status at the date of interview (if between dateprev1 and dateprev2).
7735: We still use firstpass and lastpass as another selection.
7736: */
7737:
7738: int i, m, jk, j1, bool, z1,j, iv;
7739: int mi; /* Effective wave */
7740: int iage;
7741: double agebegin; /*, ageend;*/
7742:
7743: double **prop;
7744: double posprop;
7745: double y2; /* in fractional years */
7746: int iagemin, iagemax;
7747: int first; /** to stop verbosity which is redirected to log file */
7748:
7749: iagemin= (int) agemin;
7750: iagemax= (int) agemax;
7751: /*pp=vector(1,nlstate);*/
7752: prop=matrix(1,nlstate,iagemin-AGEMARGE,iagemax+4+AGEMARGE);
7753: /* freq=ma3x(-1,nlstate+ndeath,-1,nlstate+ndeath,iagemin,iagemax+3);*/
7754: j1=0;
7755:
7756: /*j=cptcoveff;*/
7757: if (cptcovn<1) {j=1;ncodemax[1]=1;}
7758:
7759: first=0;
7760: for(j1=1; j1<= (int) pow(2,cptcoveff);j1++){ /* For each combination of simple dummy covariates */
7761: for (i=1; i<=nlstate; i++)
7762: for(iage=iagemin-AGEMARGE; iage <= iagemax+4+AGEMARGE; iage++)
7763: prop[i][iage]=0.0;
7764: printf("Prevalence combination of varying and fixed dummies %d\n",j1);
7765: /* fprintf(ficlog," V%d=%d ",Tvaraff[j1],nbcode[Tvaraff[j1]][codtabm(k,j1)]); */
7766: fprintf(ficlog,"Prevalence combination of varying and fixed dummies %d\n",j1);
7767:
7768: for (i=1; i<=imx; i++) { /* Each individual */
7769: bool=1;
7770: /* for(m=firstpass; m<=lastpass; m++){/\* Other selection (we can limit to certain interviews*\/ */
7771: for(mi=1; mi<wav[i];mi++){ /* For this wave too look where individual can be counted V4=0 V3=0 */
7772: m=mw[mi][i];
7773: /* Tmodelind[z1]=k is the position of the varying covariate in the model, but which # within 1 to ntv? */
7774: /* Tvar[Tmodelind[z1]] is the n of Vn; n-ncovcol-nqv is the first time varying covariate or iv */
7775: for (z1=1; z1<=cptcoveff; z1++){
7776: if( Fixed[Tmodelind[z1]]==1){
7777: iv= Tvar[Tmodelind[z1]];/* because cotvar starts now at first ncovcol+nqv+ (1 to nqtv) */
7778: if (cotvar[m][iv][i]!= nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]) /* iv=1 to ntv, right modality */
7779: bool=0;
7780: }else if( Fixed[Tmodelind[z1]]== 0) /* fixed */
7781: if (covar[Tvaraff[z1]][i]!= nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]) {
7782: bool=0;
7783: }
7784: }
7785: if(bool==1){ /* Otherwise we skip that wave/person */
7786: agebegin=agev[m][i]; /* Age at beginning of wave before transition*/
7787: /* ageend=agev[m][i]+(dh[m][i])*stepm/YEARM; /\* Age at end of wave and transition *\/ */
7788: if(m >=firstpass && m <=lastpass){
7789: y2=anint[m][i]+(mint[m][i]/12.); /* Fractional date in year */
7790: if ((y2>=dateprev1) && (y2<=dateprev2)) { /* Here is the main selection (fractional years) */
7791: if(agev[m][i]==0) agev[m][i]=iagemax+1;
7792: if(agev[m][i]==1) agev[m][i]=iagemax+2;
7793: if((int)agev[m][i] <iagemin-AGEMARGE || (int)agev[m][i] >iagemax+4+AGEMARGE){
7794: 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);
7795: exit(1);
7796: }
7797: if (s[m][i]>0 && s[m][i]<=nlstate) {
7798: /*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]]);*/
7799: prop[s[m][i]][(int)agev[m][i]] += weight[i];/* At age of beginning of transition, where status is known */
7800: prop[s[m][i]][iagemax+3] += weight[i];
7801: } /* end valid statuses */
7802: } /* end selection of dates */
7803: } /* end selection of waves */
7804: } /* end bool */
7805: } /* end wave */
7806: } /* end individual */
7807: for(i=iagemin; i <= iagemax+3; i++){
7808: for(jk=1,posprop=0; jk <=nlstate ; jk++) {
7809: posprop += prop[jk][i];
7810: }
7811:
7812: for(jk=1; jk <=nlstate ; jk++){
7813: if( i <= iagemax){
7814: if(posprop>=1.e-5){
7815: probs[i][jk][j1]= prop[jk][i]/posprop;
7816: } else{
7817: if(!first){
7818: first=1;
7819: 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]);
7820: }else{
7821: 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]);
7822: }
7823: }
7824: }
7825: }/* end jk */
7826: }/* end i */
7827: /*} *//* end i1 */
7828: } /* end j1 */
7829:
7830: /* free_ma3x(freq,-1,nlstate+ndeath,-1,nlstate+ndeath, iagemin, iagemax+3);*/
7831: /*free_vector(pp,1,nlstate);*/
7832: free_matrix(prop,1,nlstate, iagemin-AGEMARGE,iagemax+4+AGEMARGE);
7833: } /* End of prevalence */
7834:
7835: /************* Waves Concatenation ***************/
7836:
7837: 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)
7838: {
7839: /* Concatenates waves: wav[i] is the number of effective (useful waves in the sense that a non interview is useless) of individual i.
7840: Death is a valid wave (if date is known).
7841: mw[mi][i] is the mi (mi=1 to wav[i]) effective wave of individual i
7842: dh[m][i] or dh[mw[mi][i]][i] is the delay between two effective waves m=mw[mi][i]
7843: and mw[mi+1][i]. dh depends on stepm. s[m][i] exists for any wave from firstpass to lastpass
7844: */
7845:
7846: int i=0, mi=0, m=0, mli=0;
7847: /* int j, k=0,jk, ju, jl,jmin=1e+5, jmax=-1;
7848: double sum=0., jmean=0.;*/
7849: int first=0, firstwo=0, firsthree=0, firstfour=0, firstfiv=0;
7850: int j, k=0,jk, ju, jl;
7851: double sum=0.;
7852: first=0;
7853: firstwo=0;
7854: firsthree=0;
7855: firstfour=0;
7856: jmin=100000;
7857: jmax=-1;
7858: jmean=0.;
7859:
7860: /* Treating live states */
7861: for(i=1; i<=imx; i++){ /* For simple cases and if state is death */
7862: mi=0; /* First valid wave */
7863: mli=0; /* Last valid wave */
7864: m=firstpass; /* Loop on waves */
7865: while(s[m][i] <= nlstate){ /* a live state or unknown state */
7866: 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 */
7867: mli=m-1;/* mw[++mi][i]=m-1; */
7868: }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 */
7869: mw[++mi][i]=m; /* Valid wave: incrementing mi and updating mi; mw[mi] is the wave number of mi_th valid transition */
7870: mli=m;
7871: } /* else might be a useless wave -1 and mi is not incremented and mw[mi] not updated */
7872: if(m < lastpass){ /* m < lastpass, standard case */
7873: m++; /* mi gives the "effective" current wave, m the current wave, go to next wave by incrementing m */
7874: }
7875: else{ /* m = lastpass, eventual special issue with warning */
7876: #ifdef UNKNOWNSTATUSNOTCONTRIBUTING
7877: break;
7878: #else
7879: 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 */
7880: if(firsthree == 0){
7881: 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);
7882: firsthree=1;
7883: }else if(firsthree >=1 && firsthree < 10){
7884: 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);
7885: firsthree++;
7886: }else if(firsthree == 10){
7887: printf("Information, too many Information flags: no more reported to log either\n");
7888: fprintf(ficlog,"Information, too many Information flags: no more reported to log either\n");
7889: firsthree++;
7890: }else{
7891: firsthree++;
7892: }
7893: mw[++mi][i]=m; /* Valid transition with unknown status */
7894: mli=m;
7895: }
7896: if(s[m][i]==-2){ /* Vital status is really unknown */
7897: nbwarn++;
7898: if((int)anint[m][i] == 9999){ /* Has the vital status really been verified?not a transition */
7899: 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);
7900: 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);
7901: }
7902: break;
7903: }
7904: break;
7905: #endif
7906: }/* End m >= lastpass */
7907: }/* end while */
7908:
7909: /* mi is the last effective wave, m is lastpass, mw[j][i] gives the # of j-th effective wave for individual i */
7910: /* After last pass */
7911: /* Treating death states */
7912: if (s[m][i] > nlstate){ /* In a death state */
7913: /* if( mint[m][i]==mdc[m][i] && anint[m][i]==andc[m][i]){ /\* same date of death and date of interview *\/ */
7914: /* } */
7915: mi++; /* Death is another wave */
7916: /* if(mi==0) never been interviewed correctly before death */
7917: /* Only death is a correct wave */
7918: mw[mi][i]=m;
7919: } /* else not in a death state */
7920: #ifndef DISPATCHINGKNOWNDEATHAFTERLASTWAVE
7921: else if ((int) andc[i] != 9999) { /* Date of death is known */
7922: if ((int)anint[m][i]!= 9999) { /* date of last interview is known */
7923: 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 */
7924: nbwarn++;
7925: if(firstfiv==0){
7926: 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 );
7927: firstfiv=1;
7928: }else{
7929: 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 );
7930: }
7931: s[m][i]=nlstate+1; /* Fixing the status as death. Be careful if multiple death states */
7932: }else{ /* Month of Death occured afer last wave month, potential bias */
7933: nberr++;
7934: if(firstwo==0){
7935: 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 );
7936: firstwo=1;
7937: }
7938: 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 );
7939: }
7940: }else{ /* if date of interview is unknown */
7941: /* death is known but not confirmed by death status at any wave */
7942: if(firstfour==0){
7943: 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 );
7944: firstfour=1;
7945: }
7946: 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 );
7947: }
7948: } /* end if date of death is known */
7949: #endif
7950: wav[i]=mi; /* mi should be the last effective wave (or mli), */
7951: /* wav[i]=mw[mi][i]; */
7952: if(mi==0){
7953: nbwarn++;
7954: if(first==0){
7955: printf("Warning! No valid information for individual %ld line=%d (skipped) and may be others, see log file\n",num[i],i);
7956: first=1;
7957: }
7958: if(first==1){
7959: fprintf(ficlog,"Warning! No valid information for individual %ld line=%d (skipped)\n",num[i],i);
7960: }
7961: } /* end mi==0 */
7962: } /* End individuals */
7963: /* wav and mw are no more changed */
7964:
7965: printf("Information, you have to check %d informations which haven't been logged!\n",firsthree);
7966: fprintf(ficlog,"Information, you have to check %d informations which haven't been logged!\n",firsthree);
7967:
7968:
7969: for(i=1; i<=imx; i++){
7970: for(mi=1; mi<wav[i];mi++){
7971: if (stepm <=0)
7972: dh[mi][i]=1;
7973: else{
7974: if (s[mw[mi+1][i]][i] > nlstate) { /* A death, but what if date is unknown? */
7975: if (agedc[i] < 2*AGESUP) {
7976: j= rint(agedc[i]*12-agev[mw[mi][i]][i]*12);
7977: if(j==0) j=1; /* Survives at least one month after exam */
7978: else if(j<0){
7979: nberr++;
7980: printf("Error! Negative delay (%d to death) between waves %d and %d of individual %ld (around 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]);
7981: j=1; /* Temporary Dangerous patch */
7982: 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);
7983: fprintf(ficlog,"Error! Negative delay (%d to death) between waves %d and %d of individual %ld (around 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]);
7984: 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);
7985: }
7986: k=k+1;
7987: if (j >= jmax){
7988: jmax=j;
7989: ijmax=i;
7990: }
7991: if (j <= jmin){
7992: jmin=j;
7993: ijmin=i;
7994: }
7995: sum=sum+j;
7996: /*if (j<0) printf("j=%d num=%d \n",j,i);*/
7997: /* printf("%d %d %d %d\n", s[mw[mi][i]][i] ,s[mw[mi+1][i]][i],j,i);*/
7998: }
7999: }
8000: else{
8001: j= rint( (agev[mw[mi+1][i]][i]*12 - agev[mw[mi][i]][i]*12));
8002: /* 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]); */
8003:
8004: k=k+1;
8005: if (j >= jmax) {
8006: jmax=j;
8007: ijmax=i;
8008: }
8009: else if (j <= jmin){
8010: jmin=j;
8011: ijmin=i;
8012: }
8013: /* if (j<10) printf("j=%d jmin=%d num=%d ",j,jmin,i); */
8014: /*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]);*/
8015: if(j<0){
8016: nberr++;
8017: printf("Error! Negative delay (%d) between waves %d and %d of individual %ld (around 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]);
8018: fprintf(ficlog,"Error! Negative delay (%d) between waves %d and %d of individual %ld (around 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]);
8019: }
8020: sum=sum+j;
8021: }
8022: jk= j/stepm;
8023: jl= j -jk*stepm;
8024: ju= j -(jk+1)*stepm;
8025: if(mle <=1){ /* only if we use a the linear-interpoloation pseudo-likelihood */
8026: if(jl==0){
8027: dh[mi][i]=jk;
8028: bh[mi][i]=0;
8029: }else{ /* We want a negative bias in order to only have interpolation ie
8030: * to avoid the price of an extra matrix product in likelihood */
8031: dh[mi][i]=jk+1;
8032: bh[mi][i]=ju;
8033: }
8034: }else{
8035: if(jl <= -ju){
8036: dh[mi][i]=jk;
8037: bh[mi][i]=jl; /* bias is positive if real duration
8038: * is higher than the multiple of stepm and negative otherwise.
8039: */
8040: }
8041: else{
8042: dh[mi][i]=jk+1;
8043: bh[mi][i]=ju;
8044: }
8045: if(dh[mi][i]==0){
8046: dh[mi][i]=1; /* At least one step */
8047: bh[mi][i]=ju; /* At least one step */
8048: /* 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);*/
8049: }
8050: } /* end if mle */
8051: }
8052: } /* end wave */
8053: }
8054: jmean=sum/k;
8055: 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);
8056: 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);
8057: }
8058:
8059: /*********** Tricode ****************************/
8060: void tricode(int *cptcov, int *Tvar, int **nbcode, int imx, int *Ndum)
8061: {
8062: /**< Uses cptcovn+2*cptcovprod as the number of covariates */
8063: /* Tvar[i]=atoi(stre); find 'n' in Vn and stores in Tvar. If model=V2+V1 Tvar[1]=2 and Tvar[2]=1
8064: * Boring subroutine which should only output nbcode[Tvar[j]][k]
8065: * Tvar[5] in V2+V1+V3*age+V2*V4 is 4 (V4) even it is a time varying or quantitative variable
8066: * nbcode[Tvar[5]][1]= nbcode[4][1]=0, nbcode[4][2]=1 (usually);
8067: */
8068:
8069: int ij=1, k=0, j=0, i=0, maxncov=NCOVMAX;
8070: int modmaxcovj=0; /* Modality max of covariates j */
8071: int cptcode=0; /* Modality max of covariates j */
8072: int modmincovj=0; /* Modality min of covariates j */
8073:
8074:
8075: /* cptcoveff=0; */
8076: /* *cptcov=0; */
8077:
8078: for (k=1; k <= maxncov; k++) ncodemax[k]=0; /* Horrible constant again replaced by NCOVMAX */
8079: for (k=1; k <= maxncov; k++)
8080: for(j=1; j<=2; j++)
8081: nbcode[k][j]=0; /* Valgrind */
8082:
8083: /* Loop on covariates without age and products and no quantitative variable */
8084: 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 */
8085: for (j=-1; (j < maxncov); j++) Ndum[j]=0;
8086: /* printf("Testing k=%d, cptcovt=%d\n",k, cptcovt); */
8087: if(Dummy[k]==0 && Typevar[k] !=1 && Typevar[k] != 3 && Typevar[k] != 2){ /* Dummy covariate and not age product nor fixed product */
8088: switch(Fixed[k]) {
8089: case 0: /* Testing on fixed dummy covariate, simple or product of fixed */
8090: modmaxcovj=0;
8091: modmincovj=0;
8092: 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*/
8093: /* printf("Waiting for error tricode Tvar[%d]=%d i=%d (int)(covar[Tvar[k]][i]=%d\n",k,Tvar[k], i, (int)(covar[Tvar[k]][i])); */
8094: ij=(int)(covar[Tvar[k]][i]);
8095: /* ij=0 or 1 or -1. Value of the covariate Tvar[j] for individual i
8096: * If product of Vn*Vm, still boolean *:
8097: * If it was coded 1, 2, 3, 4 should be splitted into 3 boolean variables
8098: * 1 => 0 0 0, 2 => 0 0 1, 3 => 0 1 1, 4=1 0 0 */
8099: /* Finds for covariate j, n=Tvar[j] of Vn . ij is the
8100: modality of the nth covariate of individual i. */
8101: if (ij > modmaxcovj)
8102: modmaxcovj=ij;
8103: else if (ij < modmincovj)
8104: modmincovj=ij;
8105: if (ij <0 || ij >1 ){
8106: printf("ERROR, IMaCh doesn't treat covariate with missing values V%d=-1, individual %d will be skipped.\n",Tvar[k],i);
8107: fprintf(ficlog,"ERROR, currently IMaCh doesn't treat covariate with missing values V%d=-1, individual %d will be skipped.\n",Tvar[k],i);
8108: fflush(ficlog);
8109: exit(1);
8110: }
8111: if ((ij < -1) || (ij > NCOVMAX)){
8112: printf( "Error: minimal is less than -1 or maximal is bigger than %d. Exiting. \n", NCOVMAX );
8113: exit(1);
8114: }else
8115: Ndum[ij]++; /*counts and stores the occurence of this modality 0, 1, -1*/
8116: /* If coded 1, 2, 3 , counts the number of 1 Ndum[1], number of 2, Ndum[2], etc */
8117: /*printf("i=%d ij=%d Ndum[ij]=%d imx=%d",i,ij,Ndum[ij],imx);*/
8118: /* getting the maximum value of the modality of the covariate
8119: (should be 0 or 1 now) Tvar[j]. If V=sex and male is coded 0 and
8120: female ies 1, then modmaxcovj=1.
8121: */
8122: } /* end for loop on individuals i */
8123: printf(" Minimal and maximal values of %d th (fixed) covariate V%d: min=%d max=%d \n", k, Tvar[k], modmincovj, modmaxcovj);
8124: fprintf(ficlog," Minimal and maximal values of %d th (fixed) covariate V%d: min=%d max=%d \n", k, Tvar[k], modmincovj, modmaxcovj);
8125: cptcode=modmaxcovj;
8126: /* Ndum[0] = frequency of 0 for model-covariate j, Ndum[1] frequency of 1 etc. */
8127: /*for (i=0; i<=cptcode; i++) {*/
8128: for (j=modmincovj; j<=modmaxcovj; j++) { /* j=-1 ? 0 and 1*//* For each value j of the modality of model-cov k */
8129: printf("Frequencies of (fixed) covariate %d ie V%d with value %d: %d\n", k, Tvar[k], j, Ndum[j]);
8130: fprintf(ficlog, "Frequencies of (fixed) covariate %d ie V%d with value %d: %d\n", k, Tvar[k], j, Ndum[j]);
8131: if( Ndum[j] != 0 ){ /* Counts if nobody answered modality j ie empty modality, we skip it and reorder */
8132: if( j != -1){
8133: ncodemax[k]++; /* ncodemax[k]= Number of modalities of the k th
8134: covariate for which somebody answered excluding
8135: undefined. Usually 2: 0 and 1. */
8136: }
8137: ncodemaxwundef[k]++; /* ncodemax[j]= Number of modalities of the k th
8138: covariate for which somebody answered including
8139: undefined. Usually 3: -1, 0 and 1. */
8140: } /* In fact ncodemax[k]=2 (dichotom. variables only) but it could be more for
8141: * historical reasons: 3 if coded 1, 2, 3 and 4 and Ndum[2]=0 */
8142: } /* Ndum[-1] number of undefined modalities */
8143:
8144: /* j is a covariate, n=Tvar[j] of Vn; Fills nbcode */
8145: /* For covariate j, modalities could be 1, 2, 3, 4, 5, 6, 7. */
8146: /* If Ndum[1]=0, Ndum[2]=0, Ndum[3]= 635, Ndum[4]=0, Ndum[5]=0, Ndum[6]=27, Ndum[7]=125; */
8147: /* modmincovj=3; modmaxcovj = 7; */
8148: /* There are only 3 modalities non empty 3, 6, 7 (or 2 if 27 is too few) : ncodemax[j]=3; */
8149: /* which will be coded 0, 1, 2 which in binary on 2=3-1 digits are 0=00 1=01, 2=10; */
8150: /* defining two dummy variables: variables V1_1 and V1_2.*/
8151: /* nbcode[Tvar[j]][ij]=k; */
8152: /* nbcode[Tvar[j]][1]=0; */
8153: /* nbcode[Tvar[j]][2]=1; */
8154: /* nbcode[Tvar[j]][3]=2; */
8155: /* To be continued (not working yet). */
8156: ij=0; /* ij is similar to i but can jump over null modalities */
8157:
8158: /* 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*/
8159: /* Skipping the case of missing values by reducing nbcode to 0 and 1 and not -1, 0, 1 */
8160: /* model=V1+V2+V3, if V2=-1, 0 or 1, then nbcode[2][1]=0 and nbcode[2][2]=1 instead of
8161: * nbcode[2][1]=-1, nbcode[2][2]=0 and nbcode[2][3]=1 */
8162: /*, could be restored in the future */
8163: 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*/
8164: if (Ndum[i] == 0) { /* If nobody responded to this modality k */
8165: break;
8166: }
8167: ij++;
8168: 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*/
8169: cptcode = ij; /* New max modality for covar j */
8170: } /* end of loop on modality i=-1 to 1 or more */
8171: break;
8172: case 1: /* Testing on varying covariate, could be simple and
8173: * should look at waves or product of fixed *
8174: * varying. No time to test -1, assuming 0 and 1 only */
8175: ij=0;
8176: for(i=0; i<=1;i++){
8177: nbcode[Tvar[k]][++ij]=i;
8178: }
8179: break;
8180: default:
8181: break;
8182: } /* end switch */
8183: } /* end dummy test */
8184: if(Dummy[k]==1 && Typevar[k] !=1 && Typevar[k] !=3 && Fixed ==0){ /* Fixed Quantitative covariate and not age product */
8185: 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*/
8186: if(Tvar[k]<=0 || Tvar[k]>=NCOVMAX){
8187: printf("Error k=%d \n",k);
8188: exit(1);
8189: }
8190: if(isnan(covar[Tvar[k]][i])){
8191: printf("ERROR, IMaCh doesn't treat fixed quantitative covariate with missing values V%d=., individual %d will be skipped.\n",Tvar[k],i);
8192: fprintf(ficlog,"ERROR, currently IMaCh doesn't treat covariate with missing values V%d=., individual %d will be skipped.\n",Tvar[k],i);
8193: fflush(ficlog);
8194: exit(1);
8195: }
8196: }
8197: } /* end Quanti */
8198: } /* 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*/
8199:
8200: for (k=-1; k< maxncov; k++) Ndum[k]=0;
8201: /* Look at fixed dummy (single or product) covariates to check empty modalities */
8202: for (i=1; i<=ncovmodel-2-nagesqr; i++) { /* -2, cste and age and eventually age*age */
8203: /* Listing of all covariables in statement model to see if some covariates appear twice. For example, V1 appears twice in V1+V1*V2.*/
8204: 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 */
8205: 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 */
8206: /* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1, {2, 1, 1, 1, 2, 1, 1, 0, 0} */
8207: } /* V4+V3+V5, Ndum[1]@5={0, 0, 1, 1, 1} */
8208:
8209: ij=0;
8210: /* for (i=0; i<= maxncov-1; i++) { /\* modmaxcovj is unknown here. Only Ndum[2(V2),3(age*V3), 5(V3*V2) 6(V1*V4) *\/ */
8211: 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 */
8212: /* modmaxcovj is unknown here. Only Ndum[2(V2),3(age*V3), 5(V3*V2) 6(V1*V4) */
8213: /*printf("Ndum[%d]=%d\n",i, Ndum[i]);*/
8214: /* if((Ndum[i]!=0) && (i<=ncovcol)){ /\* Tvar[i] <= ncovmodel ? *\/ */
8215: if(Ndum[Tvar[k]]!=0 && Dummy[k] == 0 && Typevar[k]==0){ /* Only Dummy simple and non empty in the model */
8216: /* Typevar[k] =0 for simple covariate (dummy, quantitative, fixed or varying), 1 for age product, 2 for product */
8217: /* Dummy[k] 0=dummy (0 1), 1 quantitative (single or product without age), 2 dummy with age product, 3 quant with age product*/
8218: /* If product not in single variable we don't print results */
8219: /*printf("diff Ndum[%d]=%d\n",i, Ndum[i]);*/
8220: ++ij;/* V5 + V4 + V3 + V4*V3 + V5*age + V2 + V1*V2 + V1*age + V1, *//* V5 quanti, V2 quanti, V4, V3, V1 dummies */
8221: /* k= 1 2 3 4 5 6 7 8 9 */
8222: /* Tvar[k]= 5 4 3 6 5 2 7 1 1 */
8223: /* ij 1 2 3 */
8224: /* Tvaraff[ij]= 4 3 1 */
8225: /* Tmodelind[ij]=2 3 9 */
8226: /* TmodelInvind[ij]=2 1 1 */
8227: 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*/
8228: Tmodelind[ij]=k; /* Tmodelind: index in model of dummies Tmodelind[1]=2 V4: pos=2; V3: pos=3, V1=9 {2, 3, 9, ?, ?,} */
8229: 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 */
8230: if(Fixed[k]!=0)
8231: anyvaryingduminmodel=1;
8232: /* }else if((Ndum[i]!=0) && (i<=ncovcol+nqv)){ */
8233: /* Tvaraff[++ij]=-10; /\* Dont'n know how to treat quantitative variables yet *\/ */
8234: /* }else if((Ndum[i]!=0) && (i<=ncovcol+nqv+ntv)){ */
8235: /* Tvaraff[++ij]=i; /\*For printing (unclear) *\/ */
8236: /* }else if((Ndum[i]!=0) && (i<=ncovcol+nqv+ntv+nqtv)){ */
8237: /* Tvaraff[++ij]=-20; /\* Dont'n know how to treat quantitative variables yet *\/ */
8238: }
8239: } /* Tvaraff[1]@5 {3, 4, -20, 0, 0} Very strange */
8240: /* ij--; */
8241: /* cptcoveff=ij; /\*Number of total covariates*\/ */
8242: *cptcov=ij; /* cptcov= Number of total real effective simple dummies (fixed or time arying) effective (used as cptcoveff in other functions)
8243: * because they can be excluded from the model and real
8244: * if in the model but excluded because missing values, but how to get k from ij?*/
8245: for(j=ij+1; j<= cptcovt; j++){
8246: Tvaraff[j]=0;
8247: Tmodelind[j]=0;
8248: }
8249: for(j=ntveff+1; j<= cptcovt; j++){
8250: TmodelInvind[j]=0;
8251: }
8252: /* To be sorted */
8253: ;
8254: }
8255:
8256:
8257: /*********** Health Expectancies ****************/
8258:
8259: 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 )
8260:
8261: {
8262: /* Health expectancies, no variances */
8263: /* cij is the combination in the list of combination of dummy covariates */
8264: /* strstart is a string of time at start of computing */
8265: int i, j, nhstepm, hstepm, h, nstepm;
8266: int nhstepma, nstepma; /* Decreasing with age */
8267: double age, agelim, hf;
8268: double ***p3mat;
8269: double eip;
8270:
8271: /* pstamp(ficreseij); */
8272: fprintf(ficreseij,"# (a) Life expectancies by health status at initial age and (b) health expectancies by health status at initial age\n");
8273: fprintf(ficreseij,"# Age");
8274: for(i=1; i<=nlstate;i++){
8275: for(j=1; j<=nlstate;j++){
8276: fprintf(ficreseij," e%1d%1d ",i,j);
8277: }
8278: fprintf(ficreseij," e%1d. ",i);
8279: }
8280: fprintf(ficreseij,"\n");
8281:
8282:
8283: if(estepm < stepm){
8284: printf ("Problem %d lower than %d\n",estepm, stepm);
8285: }
8286: else hstepm=estepm;
8287: /* We compute the life expectancy from trapezoids spaced every estepm months
8288: * This is mainly to measure the difference between two models: for example
8289: * if stepm=24 months pijx are given only every 2 years and by summing them
8290: * we are calculating an estimate of the Life Expectancy assuming a linear
8291: * progression in between and thus overestimating or underestimating according
8292: * to the curvature of the survival function. If, for the same date, we
8293: * estimate the model with stepm=1 month, we can keep estepm to 24 months
8294: * to compare the new estimate of Life expectancy with the same linear
8295: * hypothesis. A more precise result, taking into account a more precise
8296: * curvature will be obtained if estepm is as small as stepm. */
8297:
8298: /* For example we decided to compute the life expectancy with the smallest unit */
8299: /* hstepm beeing the number of stepms, if hstepm=1 the length of hstepm is stepm.
8300: nhstepm is the number of hstepm from age to agelim
8301: nstepm is the number of stepm from age to agelin.
8302: Look at hpijx to understand the reason which relies in memory size consideration
8303: and note for a fixed period like estepm months */
8304: /* We decided (b) to get a life expectancy respecting the most precise curvature of the
8305: survival function given by stepm (the optimization length). Unfortunately it
8306: means that if the survival funtion is printed only each two years of age and if
8307: you sum them up and add 1 year (area under the trapezoids) you won't get the same
8308: results. So we changed our mind and took the option of the best precision.
8309: */
8310: hstepm=hstepm/stepm; /* Typically in stepm units, if stepm=6 & estepm=24 , = 24/6 months = 4 */
8311:
8312: agelim=AGESUP;
8313: /* If stepm=6 months */
8314: /* Computed by stepm unit matrices, product of hstepm matrices, stored
8315: in an array of nhstepm length: nhstepm=10, hstepm=4, stepm=6 months */
8316:
8317: /* nhstepm age range expressed in number of stepm */
8318: nstepm=(int) rint((agelim-bage)*YEARM/stepm); /* Biggest nstepm */
8319: /* Typically if 20 years nstepm = 20*12/6=40 stepm */
8320: /* if (stepm >= YEARM) hstepm=1;*/
8321: nhstepm = nstepm/hstepm;/* Expressed in hstepm, typically nhstepm=40/4=10 */
8322: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
8323:
8324: for (age=bage; age<=fage; age ++){
8325: nstepma=(int) rint((agelim-bage)*YEARM/stepm); /* Biggest nstepm */
8326: /* Typically if 20 years nstepm = 20*12/6=40 stepm */
8327: /* if (stepm >= YEARM) hstepm=1;*/
8328: nhstepma = nstepma/hstepm;/* Expressed in hstepm, typically nhstepma=40/4=10 */
8329:
8330: /* If stepm=6 months */
8331: /* Computed by stepm unit matrices, product of hstepma matrices, stored
8332: in an array of nhstepma length: nhstepma=10, hstepm=4, stepm=6 months */
8333: /* printf("HELLO evsij Entering hpxij age=%d cij=%d hstepm=%d x[1]=%f nres=%d\n",(int) age, cij, hstepm, x[1], nres); */
8334: hpxij(p3mat,nhstepma,age,hstepm,x,nlstate,stepm,oldm, savm, cij, nres);
8335:
8336: hf=hstepm*stepm/YEARM; /* Duration of hstepm expressed in year unit. */
8337:
8338: printf("%d|",(int)age);fflush(stdout);
8339: fprintf(ficlog,"%d|",(int)age);fflush(ficlog);
8340:
8341: /* Computing expectancies */
8342: for(i=1; i<=nlstate;i++)
8343: for(j=1; j<=nlstate;j++)
8344: for (h=0, eij[i][j][(int)age]=0; h<=nhstepm-1; h++){
8345: eij[i][j][(int)age] += (p3mat[i][j][h]+p3mat[i][j][h+1])/2.0*hf;
8346:
8347: /* 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]);*/
8348:
8349: }
8350:
8351: fprintf(ficreseij,"%3.0f",age );
8352: for(i=1; i<=nlstate;i++){
8353: eip=0;
8354: for(j=1; j<=nlstate;j++){
8355: eip +=eij[i][j][(int)age];
8356: fprintf(ficreseij,"%9.4f", eij[i][j][(int)age] );
8357: }
8358: fprintf(ficreseij,"%9.4f", eip );
8359: }
8360: fprintf(ficreseij,"\n");
8361:
8362: }
8363: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
8364: printf("\n");
8365: fprintf(ficlog,"\n");
8366:
8367: }
8368:
8369: 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 )
8370:
8371: {
8372: /* Covariances of health expectancies eij and of total life expectancies according
8373: to initial status i, ei. .
8374: */
8375: /* Very time consuming function, but already optimized with precov */
8376: int i, j, nhstepm, hstepm, h, nstepm, k, cptj, cptj2, i2, j2, ij, ji;
8377: int nhstepma, nstepma; /* Decreasing with age */
8378: double age, agelim, hf;
8379: double ***p3matp, ***p3matm, ***varhe;
8380: double **dnewm,**doldm;
8381: double *xp, *xm;
8382: double **gp, **gm;
8383: double ***gradg, ***trgradg;
8384: int theta;
8385:
8386: double eip, vip;
8387:
8388: varhe=ma3x(1,nlstate*nlstate,1,nlstate*nlstate,(int) bage, (int) fage);
8389: xp=vector(1,npar);
8390: xm=vector(1,npar);
8391: dnewm=matrix(1,nlstate*nlstate,1,npar);
8392: doldm=matrix(1,nlstate*nlstate,1,nlstate*nlstate);
8393:
8394: pstamp(ficresstdeij);
8395: fprintf(ficresstdeij,"# Health expectancies with standard errors\n");
8396: fprintf(ficresstdeij,"# Age");
8397: for(i=1; i<=nlstate;i++){
8398: for(j=1; j<=nlstate;j++)
8399: fprintf(ficresstdeij," e%1d%1d (SE)",i,j);
8400: fprintf(ficresstdeij," e%1d. ",i);
8401: }
8402: fprintf(ficresstdeij,"\n");
8403:
8404: pstamp(ficrescveij);
8405: fprintf(ficrescveij,"# Subdiagonal matrix of covariances of health expectancies by age: cov(eij,ekl)\n");
8406: fprintf(ficrescveij,"# Age");
8407: for(i=1; i<=nlstate;i++)
8408: for(j=1; j<=nlstate;j++){
8409: cptj= (j-1)*nlstate+i;
8410: for(i2=1; i2<=nlstate;i2++)
8411: for(j2=1; j2<=nlstate;j2++){
8412: cptj2= (j2-1)*nlstate+i2;
8413: if(cptj2 <= cptj)
8414: fprintf(ficrescveij," %1d%1d,%1d%1d",i,j,i2,j2);
8415: }
8416: }
8417: fprintf(ficrescveij,"\n");
8418:
8419: if(estepm < stepm){
8420: printf ("Problem %d lower than %d\n",estepm, stepm);
8421: }
8422: else hstepm=estepm;
8423: /* We compute the life expectancy from trapezoids spaced every estepm months
8424: * This is mainly to measure the difference between two models: for example
8425: * if stepm=24 months pijx are given only every 2 years and by summing them
8426: * we are calculating an estimate of the Life Expectancy assuming a linear
8427: * progression in between and thus overestimating or underestimating according
8428: * to the curvature of the survival function. If, for the same date, we
8429: * estimate the model with stepm=1 month, we can keep estepm to 24 months
8430: * to compare the new estimate of Life expectancy with the same linear
8431: * hypothesis. A more precise result, taking into account a more precise
8432: * curvature will be obtained if estepm is as small as stepm. */
8433:
8434: /* For example we decided to compute the life expectancy with the smallest unit */
8435: /* hstepm beeing the number of stepms, if hstepm=1 the length of hstepm is stepm.
8436: nhstepm is the number of hstepm from age to agelim
8437: nstepm is the number of stepm from age to agelin.
8438: Look at hpijx to understand the reason of that which relies in memory size
8439: and note for a fixed period like estepm months */
8440: /* We decided (b) to get a life expectancy respecting the most precise curvature of the
8441: survival function given by stepm (the optimization length). Unfortunately it
8442: means that if the survival funtion is printed only each two years of age and if
8443: you sum them up and add 1 year (area under the trapezoids) you won't get the same
8444: results. So we changed our mind and took the option of the best precision.
8445: */
8446: hstepm=hstepm/stepm; /* Typically in stepm units, if stepm=6 & estepm=24 , = 24/6 months = 4 */
8447:
8448: /* If stepm=6 months */
8449: /* nhstepm age range expressed in number of stepm */
8450: agelim=AGESUP;
8451: nstepm=(int) rint((agelim-bage)*YEARM/stepm);
8452: /* Typically if 20 years nstepm = 20*12/6=40 stepm */
8453: /* if (stepm >= YEARM) hstepm=1;*/
8454: nhstepm = nstepm/hstepm;/* Expressed in hstepm, typically nhstepm=40/4=10 */
8455:
8456: p3matp=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
8457: p3matm=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
8458: gradg=ma3x(0,nhstepm,1,npar,1,nlstate*nlstate);
8459: trgradg =ma3x(0,nhstepm,1,nlstate*nlstate,1,npar);
8460: gp=matrix(0,nhstepm,1,nlstate*nlstate);
8461: gm=matrix(0,nhstepm,1,nlstate*nlstate);
8462:
8463: for (age=bage; age<=fage; age ++){
8464: nstepma=(int) rint((agelim-bage)*YEARM/stepm); /* Biggest nstepm */
8465: /* Typically if 20 years nstepm = 20*12/6=40 stepm */
8466: /* if (stepm >= YEARM) hstepm=1;*/
8467: nhstepma = nstepma/hstepm;/* Expressed in hstepm, typically nhstepma=40/4=10 */
8468:
8469: /* If stepm=6 months */
8470: /* Computed by stepm unit matrices, product of hstepma matrices, stored
8471: in an array of nhstepma length: nhstepma=10, hstepm=4, stepm=6 months */
8472:
8473: hf=hstepm*stepm/YEARM; /* Duration of hstepm expressed in year unit. */
8474:
8475: /* Computing Variances of health expectancies */
8476: /* Gradient is computed with plus gp and minus gm. Code is duplicated in order to
8477: decrease memory allocation */
8478: for(theta=1; theta <=npar; theta++){
8479: for(i=1; i<=npar; i++){
8480: xp[i] = x[i] + (i==theta ?delti[theta]:0);
8481: xm[i] = x[i] - (i==theta ?delti[theta]:0);
8482: }
8483: hpxij(p3matp,nhstepm,age,hstepm,xp,nlstate,stepm,oldm,savm, cij, nres);
8484: hpxij(p3matm,nhstepm,age,hstepm,xm,nlstate,stepm,oldm,savm, cij, nres);
8485:
8486: for(j=1; j<= nlstate; j++){
8487: for(i=1; i<=nlstate; i++){
8488: for(h=0; h<=nhstepm-1; h++){
8489: gp[h][(j-1)*nlstate + i] = (p3matp[i][j][h]+p3matp[i][j][h+1])/2.;
8490: gm[h][(j-1)*nlstate + i] = (p3matm[i][j][h]+p3matm[i][j][h+1])/2.;
8491: }
8492: }
8493: }
8494:
8495: for(ij=1; ij<= nlstate*nlstate; ij++)
8496: for(h=0; h<=nhstepm-1; h++){
8497: gradg[h][theta][ij]= (gp[h][ij]-gm[h][ij])/2./delti[theta];
8498: }
8499: }/* End theta */
8500:
8501:
8502: for(h=0; h<=nhstepm-1; h++)
8503: for(j=1; j<=nlstate*nlstate;j++)
8504: for(theta=1; theta <=npar; theta++)
8505: trgradg[h][j][theta]=gradg[h][theta][j];
8506:
8507:
8508: for(ij=1;ij<=nlstate*nlstate;ij++)
8509: for(ji=1;ji<=nlstate*nlstate;ji++)
8510: varhe[ij][ji][(int)age] =0.;
8511:
8512: printf("%d|",(int)age);fflush(stdout);
8513: fprintf(ficlog,"%d|",(int)age);fflush(ficlog);
8514: for(h=0;h<=nhstepm-1;h++){
8515: for(k=0;k<=nhstepm-1;k++){
8516: matprod2(dnewm,trgradg[h],1,nlstate*nlstate,1,npar,1,npar,matcov);
8517: matprod2(doldm,dnewm,1,nlstate*nlstate,1,npar,1,nlstate*nlstate,gradg[k]);
8518: for(ij=1;ij<=nlstate*nlstate;ij++)
8519: for(ji=1;ji<=nlstate*nlstate;ji++)
8520: varhe[ij][ji][(int)age] += doldm[ij][ji]*hf*hf;
8521: }
8522: }
8523: /* if((int)age ==50){ */
8524: /* printf(" age=%d cij=%d nres=%d varhe[%d][%d]=%f ",(int)age, cij, nres, 1,2,varhe[1][2]); */
8525: /* } */
8526: /* Computing expectancies */
8527: hpxij(p3matm,nhstepm,age,hstepm,x,nlstate,stepm,oldm, savm, cij,nres);
8528: for(i=1; i<=nlstate;i++)
8529: for(j=1; j<=nlstate;j++)
8530: for (h=0, eij[i][j][(int)age]=0; h<=nhstepm-1; h++){
8531: eij[i][j][(int)age] += (p3matm[i][j][h]+p3matm[i][j][h+1])/2.0*hf;
8532:
8533: /* 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]);*/
8534:
8535: }
8536:
8537: /* Standard deviation of expectancies ij */
8538: fprintf(ficresstdeij,"%3.0f",age );
8539: for(i=1; i<=nlstate;i++){
8540: eip=0.;
8541: vip=0.;
8542: for(j=1; j<=nlstate;j++){
8543: eip += eij[i][j][(int)age];
8544: for(k=1; k<=nlstate;k++) /* Sum on j and k of cov(eij,eik) */
8545: vip += varhe[(j-1)*nlstate+i][(k-1)*nlstate+i][(int)age];
8546: fprintf(ficresstdeij," %9.4f (%.4f)", eij[i][j][(int)age], sqrt(varhe[(j-1)*nlstate+i][(j-1)*nlstate+i][(int)age]) );
8547: }
8548: fprintf(ficresstdeij," %9.4f (%.4f)", eip, sqrt(vip));
8549: }
8550: fprintf(ficresstdeij,"\n");
8551:
8552: /* Variance of expectancies ij */
8553: fprintf(ficrescveij,"%3.0f",age );
8554: for(i=1; i<=nlstate;i++)
8555: for(j=1; j<=nlstate;j++){
8556: cptj= (j-1)*nlstate+i;
8557: for(i2=1; i2<=nlstate;i2++)
8558: for(j2=1; j2<=nlstate;j2++){
8559: cptj2= (j2-1)*nlstate+i2;
8560: if(cptj2 <= cptj)
8561: fprintf(ficrescveij," %.4f", varhe[cptj][cptj2][(int)age]);
8562: }
8563: }
8564: fprintf(ficrescveij,"\n");
8565:
8566: }
8567: free_matrix(gm,0,nhstepm,1,nlstate*nlstate);
8568: free_matrix(gp,0,nhstepm,1,nlstate*nlstate);
8569: free_ma3x(gradg,0,nhstepm,1,npar,1,nlstate*nlstate);
8570: free_ma3x(trgradg,0,nhstepm,1,nlstate*nlstate,1,npar);
8571: free_ma3x(p3matm,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
8572: free_ma3x(p3matp,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
8573: printf("\n");
8574: fprintf(ficlog,"\n");
8575:
8576: free_vector(xm,1,npar);
8577: free_vector(xp,1,npar);
8578: free_matrix(dnewm,1,nlstate*nlstate,1,npar);
8579: free_matrix(doldm,1,nlstate*nlstate,1,nlstate*nlstate);
8580: free_ma3x(varhe,1,nlstate*nlstate,1,nlstate*nlstate,(int) bage, (int)fage);
8581: }
8582:
8583: /************ Variance ******************/
8584: 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)
8585: {
8586: /** Computes the matrix of variance covariance of health expectancies e.j= sum_i w_i e_ij where w_i depends of popbased,
8587: * either cross-sectional or implied.
8588: * return vareij[i][j][(int)age]=cov(e.i,e.j)=sum_h sum_k trgrad(h_p.i) V(theta) grad(k_p.k) Equation 20
8589: * double **prevalim(double **prlim, int nlstate, double *xp, double age, double **oldm, double ** savm,double ftolpl);
8590: * double **newm;
8591: * int movingaverage(double ***probs, double bage,double fage, double ***mobaverage, int mobilav)
8592: */
8593:
8594: /* int movingaverage(); */
8595: double **dnewm,**doldm;
8596: double **dnewmp,**doldmp;
8597: int i, j, nhstepm, hstepm, h, nstepm ;
8598: int first=0;
8599: int k;
8600: double *xp;
8601: double **gp, **gm; /**< for var eij */
8602: double ***gradg, ***trgradg; /**< for var eij */
8603: double **gradgp, **trgradgp; /**< for var p point j */
8604: double *gpp, *gmp; /**< for var p point j */
8605: double **varppt; /**< for var e.. nlstate+1 to nlstate+ndeath */
8606: double ***p3mat;
8607: double age,agelim, hf;
8608: /* double ***mobaverage; */
8609: int theta;
8610: char digit[4];
8611: char digitp[25];
8612:
8613: char fileresprobmorprev[FILENAMELENGTH];
8614:
8615: if(popbased==1){
8616: if(mobilav!=0)
8617: strcpy(digitp,"-POPULBASED-MOBILAV_");
8618: else strcpy(digitp,"-POPULBASED-NOMOBIL_");
8619: }
8620: else
8621: strcpy(digitp,"-STABLBASED_");
8622:
8623: /* if (mobilav!=0) { */
8624: /* mobaverage= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
8625: /* if (movingaverage(probs, bage, fage, mobaverage,mobilav)!=0){ */
8626: /* fprintf(ficlog," Error in movingaverage mobilav=%d\n",mobilav); */
8627: /* printf(" Error in movingaverage mobilav=%d\n",mobilav); */
8628: /* } */
8629: /* } */
8630:
8631: strcpy(fileresprobmorprev,"PRMORPREV-");
8632: sprintf(digit,"%-d",ij);
8633: /*printf("DIGIT=%s, ij=%d ijr=%-d|\n",digit, ij,ij);*/
8634: strcat(fileresprobmorprev,digit); /* Tvar to be done */
8635: strcat(fileresprobmorprev,digitp); /* Popbased or not, mobilav or not */
8636: strcat(fileresprobmorprev,fileresu);
8637: if((ficresprobmorprev=fopen(fileresprobmorprev,"w"))==NULL) {
8638: printf("Problem with resultfile: %s\n", fileresprobmorprev);
8639: fprintf(ficlog,"Problem with resultfile: %s\n", fileresprobmorprev);
8640: }
8641: printf("Computing total mortality p.j=w1*p1j+w2*p2j+..: result on file '%s' \n",fileresprobmorprev);
8642: fprintf(ficlog,"Computing total mortality p.j=w1*p1j+w2*p2j+..: result on file '%s' \n",fileresprobmorprev);
8643: pstamp(ficresprobmorprev);
8644: 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);
8645: fprintf(ficresprobmorprev,"# Selected quantitative variables and dummies");
8646:
8647: /* We use TinvDoQresult[nres][resultmodel[nres][j] we sort according to the equation model and the resultline: it is a choice */
8648: /* for (j=1; j<= nsq; j++){ /\* For each selected (single) quantitative value *\/ /\* To be done*\/ */
8649: /* fprintf(ficresprobmorprev," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][resultmodel[nres][j]]); */
8650: /* } */
8651: for (j=1; j<= cptcovs; j++){ /* For each selected (single) quantitative value */ /* To be done*/
8652: /* fprintf(ficresprobmorprev," V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]); */
8653: fprintf(ficresprobmorprev," V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
8654: }
8655: /* for(j=1;j<=cptcoveff;j++) */
8656: /* fprintf(ficresprobmorprev," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(ij,TnsdVar[Tvaraff[j]])]); */
8657: fprintf(ficresprobmorprev,"\n");
8658:
8659: fprintf(ficresprobmorprev,"# Age cov=%-d",ij);
8660: for(j=nlstate+1; j<=(nlstate+ndeath);j++){
8661: fprintf(ficresprobmorprev," p.%-d SE",j);
8662: for(i=1; i<=nlstate;i++)
8663: fprintf(ficresprobmorprev," w%1d p%-d%-d",i,i,j);
8664: }
8665: fprintf(ficresprobmorprev,"\n");
8666:
8667: fprintf(ficgp,"\n# Routine varevsij");
8668: fprintf(ficgp,"\nunset title \n");
8669: /* fprintf(fichtm, "#Local time at start: %s", strstart);*/
8670: 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");
8671: fprintf(fichtm,"\n<br>%s <br>\n",digitp);
8672:
8673: varppt = matrix(nlstate+1,nlstate+ndeath,nlstate+1,nlstate+ndeath); /* In fact, currently a double */
8674: pstamp(ficresvij);
8675: fprintf(ficresvij,"# Variance and covariance of health expectancies e.j \n# (weighted average of eij where weights are ");
8676: if(popbased==1)
8677: 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);
8678: else
8679: fprintf(ficresvij,"the age specific period (stable) prevalences in each health state \n");
8680: fprintf(ficresvij,"# Age");
8681: for(i=1; i<=nlstate;i++)
8682: for(j=1; j<=nlstate;j++)
8683: fprintf(ficresvij," Cov(e.%1d, e.%1d)",i,j);
8684: fprintf(ficresvij,"\n");
8685:
8686: xp=vector(1,npar);
8687: dnewm=matrix(1,nlstate,1,npar);
8688: doldm=matrix(1,nlstate,1,nlstate);
8689: dnewmp= matrix(nlstate+1,nlstate+ndeath,1,npar);
8690: doldmp= matrix(nlstate+1,nlstate+ndeath,nlstate+1,nlstate+ndeath);
8691:
8692: gradgp=matrix(1,npar,nlstate+1,nlstate+ndeath);
8693: gpp=vector(nlstate+1,nlstate+ndeath);
8694: gmp=vector(nlstate+1,nlstate+ndeath);
8695: trgradgp =matrix(nlstate+1,nlstate+ndeath,1,npar); /* mu or p point j*/
8696:
8697: if(estepm < stepm){
8698: printf ("Problem %d lower than %d\n",estepm, stepm);
8699: }
8700: else hstepm=estepm;
8701: /* For example we decided to compute the life expectancy with the smallest unit */
8702: /* hstepm beeing the number of stepms, if hstepm=1 the length of hstepm is stepm.
8703: nhstepm is the number of hstepm from age to agelim
8704: nstepm is the number of stepm from age to agelim.
8705: Look at function hpijx to understand why because of memory size limitations,
8706: we decided (b) to get a life expectancy respecting the most precise curvature of the
8707: survival function given by stepm (the optimization length). Unfortunately it
8708: means that if the survival funtion is printed every two years of age and if
8709: you sum them up and add 1 year (area under the trapezoids) you won't get the same
8710: results. So we changed our mind and took the option of the best precision.
8711: */
8712: hstepm=hstepm/stepm; /* Typically in stepm units, if stepm=6 & estepm=24 , = 24/6 months = 4 */
8713: agelim = AGESUP;
8714: for (age=bage; age<=fage; age ++){ /* If stepm=6 months */
8715: nstepm=(int) rint((agelim-age)*YEARM/stepm); /* Typically 20 years = 20*12/6=40 */
8716: nhstepm = nstepm/hstepm;/* Expressed in hstepm, typically nhstepm=40/4=10 */
8717: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
8718: gradg=ma3x(0,nhstepm,1,npar,1,nlstate);
8719: gp=matrix(0,nhstepm,1,nlstate);
8720: gm=matrix(0,nhstepm,1,nlstate);
8721:
8722:
8723: for(theta=1; theta <=npar; theta++){
8724: for(i=1; i<=npar; i++){ /* Computes gradient x + delta*/
8725: xp[i] = x[i] + (i==theta ?delti[theta]:0);
8726: }
8727: /**< Computes the prevalence limit with parameter theta shifted of delta up to ftolpl precision and
8728: * returns into prlim .
8729: */
8730: prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp,ij, nres);
8731:
8732: /* If popbased = 1 we use crossection prevalences. Previous step is useless but prlim is created */
8733: if (popbased==1) {
8734: if(mobilav ==0){
8735: for(i=1; i<=nlstate;i++)
8736: prlim[i][i]=probs[(int)age][i][ij];
8737: }else{ /* mobilav */
8738: for(i=1; i<=nlstate;i++)
8739: prlim[i][i]=mobaverage[(int)age][i][ij];
8740: }
8741: }
8742: /**< Computes the shifted plus (gp) transition matrix \f$ {}{h}_p^{ij}x\f$ at horizon h.
8743: */
8744: hpxij(p3mat,nhstepm,age,hstepm,xp,nlstate,stepm,oldm,savm, ij,nres); /* Returns p3mat[i][j][h] for h=0 to nhstepm */
8745: /**< And for each alive state j, sums over i \f$ w^i_x {}{h}_p^{ij}x\f$, which are the probability
8746: * at horizon h in state j including mortality.
8747: */
8748: for(j=1; j<= nlstate; j++){
8749: for(h=0; h<=nhstepm; h++){
8750: for(i=1, gp[h][j]=0.;i<=nlstate;i++)
8751: gp[h][j] += prlim[i][i]*p3mat[i][j][h]; /* gp[h][j]= w_i h_pij */
8752: }
8753: }
8754: /* Next for computing shifted+ probability of death (h=1 means
8755: computed over hstepm matrices product = hstepm*stepm months)
8756: as a weighted average of prlim(i) * p(i,j) p.3=w1*p13 + w2*p23 .
8757: */
8758: for(j=nlstate+1;j<=nlstate+ndeath;j++){ /* Currently only once for theta plus p.3(age) Sum_i wi pi3*/
8759: for(i=1,gpp[j]=0.; i<= nlstate; i++)
8760: gpp[j] += prlim[i][i]*p3mat[i][j][1];
8761: }
8762:
8763: /* Again with minus shift */
8764:
8765: for(i=1; i<=npar; i++) /* Computes gradient x - delta */
8766: xp[i] = x[i] - (i==theta ?delti[theta]:0);
8767:
8768: prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp, ij, nres);
8769:
8770: if (popbased==1) {
8771: if(mobilav ==0){
8772: for(i=1; i<=nlstate;i++)
8773: prlim[i][i]=probs[(int)age][i][ij];
8774: }else{ /* mobilav */
8775: for(i=1; i<=nlstate;i++)
8776: prlim[i][i]=mobaverage[(int)age][i][ij];
8777: }
8778: }
8779:
8780: hpxij(p3mat,nhstepm,age,hstepm,xp,nlstate,stepm,oldm,savm, ij,nres); /* Still minus */
8781:
8782: for(j=1; j<= nlstate; j++){ /* gm[h][j]= Sum_i of wi * pij = h_p.j */
8783: for(h=0; h<=nhstepm; h++){
8784: for(i=1, gm[h][j]=0.;i<=nlstate;i++)
8785: gm[h][j] += prlim[i][i]*p3mat[i][j][h];
8786: }
8787: }
8788: /* This for computing probability of death (h=1 means
8789: computed over hstepm matrices product = hstepm*stepm months)
8790: as a weighted average of prlim. j is death. gmp[3]=sum_i w_i*p_i3=p.3 minus theta
8791: */
8792: for(j=nlstate+1;j<=nlstate+ndeath;j++){ /* Currently only once theta_minus p.3=Sum_i wi pi3*/
8793: for(i=1,gmp[j]=0.; i<= nlstate; i++)
8794: gmp[j] += prlim[i][i]*p3mat[i][j][1];
8795: }
8796: /* end shifting computations */
8797:
8798: /**< Computing gradient of p.j matrix at horizon h and still for one parameter of vector theta
8799: * equation 31 and 32
8800: */
8801: for(j=1; j<= nlstate; j++) /* computes grad p.j(x, over each h) where p.j is Sum_i w_i*pij(x over h)
8802: * equation 24 */
8803: for(h=0; h<=nhstepm; h++){
8804: gradg[h][theta][j]= (gp[h][j]-gm[h][j])/2./delti[theta];
8805: }
8806: /**< Gradient of overall mortality p.3 (or p.death)
8807: */
8808: for(j=nlstate+1; j<= nlstate+ndeath; j++){ /* computes grad of p.3 from wi+pi3 grad p.3 (theta) */
8809: gradgp[theta][j]= (gpp[j]-gmp[j])/2./delti[theta];
8810: }
8811:
8812: } /* End theta */
8813:
8814: /* We got the gradient matrix for each theta and each state j of gradg(h]theta][j)=grad(_hp.j(theta) */
8815: trgradg =ma3x(0,nhstepm,1,nlstate,1,npar);
8816:
8817: for(h=0; h<=nhstepm; h++) /* veij */ /* computes the transposed of grad (_hp.j(theta)*/
8818: for(j=1; j<=nlstate;j++)
8819: for(theta=1; theta <=npar; theta++)
8820: trgradg[h][j][theta]=gradg[h][theta][j];
8821:
8822: for(j=nlstate+1; j<=nlstate+ndeath;j++) /* computes transposed of grad p.3 (theta)*/
8823: for(theta=1; theta <=npar; theta++)
8824: trgradgp[j][theta]=gradgp[theta][j];
8825: /**< as well as its transposed matrix
8826: */
8827:
8828: hf=hstepm*stepm/YEARM; /* Duration of hstepm expressed in year unit. */
8829: for(i=1;i<=nlstate;i++)
8830: for(j=1;j<=nlstate;j++)
8831: vareij[i][j][(int)age] =0.;
8832:
8833: /* Computing trgradg by matcov by gradg at age and summing over h
8834: * and k (nhstepm) formula 32 of article
8835: * Lievre-Brouard-Heathcote so that for each j, computes the cov(e.j,e.k) (formula 31).
8836: * for given h and k computes trgradg[h](i,j) matcov (theta) gradg(k)(i,j) into vareij[i][j] which is
8837: cov(e.i,e.j) and sums on h and k
8838: * including the covariances.
8839: */
8840:
8841: for(h=0;h<=nhstepm;h++){
8842: for(k=0;k<=nhstepm;k++){
8843: matprod2(dnewm,trgradg[h],1,nlstate,1,npar,1,npar,matcov);
8844: matprod2(doldm,dnewm,1,nlstate,1,npar,1,nlstate,gradg[k]);
8845: for(i=1;i<=nlstate;i++)
8846: for(j=1;j<=nlstate;j++)
8847: vareij[i][j][(int)age] += doldm[i][j]*hf*hf; /* This is vareij=sum_h sum_k trgrad(h_pij) V(theta) grad(k_pij)
8848: including the covariances of e.j */
8849: }
8850: }
8851:
8852: /* Mortality: pptj is p.3 or p.death = trgradgp by cov by gradgp, variance of
8853: * p.3=1-p..=1-sum i p.i overall mortality computed directly because
8854: * we compute the grad (wix pijx) instead of grad (pijx),even if
8855: * wix is independent of theta.
8856: */
8857: matprod2(dnewmp,trgradgp,nlstate+1,nlstate+ndeath,1,npar,1,npar,matcov);
8858: matprod2(doldmp,dnewmp,nlstate+1,nlstate+ndeath,1,npar,nlstate+1,nlstate+ndeath,gradgp);
8859: for(j=nlstate+1;j<=nlstate+ndeath;j++)
8860: for(i=nlstate+1;i<=nlstate+ndeath;i++)
8861: varppt[j][i]=doldmp[j][i]; /* This is the variance of p.3 */
8862: /* end ppptj */
8863: /* x centered again */
8864:
8865: prevalim(prlim,nlstate,x,age,oldm,savm,ftolpl,ncvyearp,ij, nres);
8866:
8867: if (popbased==1) {
8868: if(mobilav ==0){
8869: for(i=1; i<=nlstate;i++)
8870: prlim[i][i]=probs[(int)age][i][ij];
8871: }else{ /* mobilav */
8872: for(i=1; i<=nlstate;i++)
8873: prlim[i][i]=mobaverage[(int)age][i][ij];
8874: }
8875: }
8876:
8877: /* This for computing probability of death (h=1 means
8878: computed over hstepm (estepm) matrices product = hstepm*stepm months)
8879: as a weighted average of prlim.
8880: */
8881: hpxij(p3mat,nhstepm,age,hstepm,x,nlstate,stepm,oldm,savm, ij, nres);
8882: for(j=nlstate+1;j<=nlstate+ndeath;j++){
8883: for(i=1,gmp[j]=0.;i<= nlstate; i++)
8884: gmp[j] += prlim[i][i]*p3mat[i][j][1]; /* gmp[j] is p.3 */
8885: }
8886: /* end probability of death */
8887:
8888: fprintf(ficresprobmorprev,"%3d %d ",(int) age, ij);
8889: for(j=nlstate+1; j<=(nlstate+ndeath);j++){
8890: fprintf(ficresprobmorprev," %11.3e %11.3e",gmp[j], sqrt(varppt[j][j]));/* p.3 (STD p.3) */
8891: for(i=1; i<=nlstate;i++){
8892: fprintf(ficresprobmorprev," %11.3e %11.3e ",prlim[i][i],p3mat[i][j][1]); /* wi, pi3 */
8893: }
8894: }
8895: fprintf(ficresprobmorprev,"\n");
8896:
8897: fprintf(ficresvij,"%.0f ",age );
8898: for(i=1; i<=nlstate;i++)
8899: for(j=1; j<=nlstate;j++){
8900: fprintf(ficresvij," %.4f", vareij[i][j][(int)age]);
8901: }
8902: fprintf(ficresvij,"\n");
8903: free_matrix(gp,0,nhstepm,1,nlstate);
8904: free_matrix(gm,0,nhstepm,1,nlstate);
8905: free_ma3x(gradg,0,nhstepm,1,npar,1,nlstate);
8906: free_ma3x(trgradg,0,nhstepm,1,nlstate,1,npar);
8907: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
8908: } /* End age */
8909: free_vector(gpp,nlstate+1,nlstate+ndeath);
8910: free_vector(gmp,nlstate+1,nlstate+ndeath);
8911: free_matrix(gradgp,1,npar,nlstate+1,nlstate+ndeath);
8912: free_matrix(trgradgp,nlstate+1,nlstate+ndeath,1,npar); /* mu or p point j*/
8913: /* fprintf(ficgp,"\nunset parametric;unset label; set ter png small size 320, 240"); */
8914: fprintf(ficgp,"\nunset parametric;unset label; set ter svg size 640, 480");
8915: /* for(j=nlstate+1; j<= nlstate+ndeath; j++){ *//* Only the first actually */
8916: fprintf(ficgp,"\n set log y; unset log x;set xlabel \"Age\"; set ylabel \"Force of mortality (year-1)\";");
8917: fprintf(ficgp,"\nset out \"%s%s.svg\";",subdirf3(optionfilefiname,"VARMUPTJGR-",digitp),digit);
8918: /* fprintf(ficgp,"\n plot \"%s\" u 1:($3*%6.3f) not w l 1 ",fileresprobmorprev,YEARM/estepm); */
8919: /* fprintf(ficgp,"\n replot \"%s\" u 1:(($3+1.96*$4)*%6.3f) t \"95\%% interval\" w l 2 ",fileresprobmorprev,YEARM/estepm); */
8920: /* fprintf(ficgp,"\n replot \"%s\" u 1:(($3-1.96*$4)*%6.3f) not w l 2 ",fileresprobmorprev,YEARM/estepm); */
8921: fprintf(ficgp,"\n plot \"%s\" u 1:($3) not w l lt 1 ",subdirf(fileresprobmorprev));
8922: fprintf(ficgp,"\n replot \"%s\" u 1:(($3+1.96*$4)) t \"95%% interval\" w l lt 2 ",subdirf(fileresprobmorprev));
8923: fprintf(ficgp,"\n replot \"%s\" u 1:(($3-1.96*$4)) not w l lt 2 ",subdirf(fileresprobmorprev));
8924: fprintf(fichtm,"\n<br> File (multiple files are possible if covariates are present): <A href=\"%s\">%s</a>\n",subdirf(fileresprobmorprev),subdirf(fileresprobmorprev));
8925: 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);
8926: /* 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);
8927: */
8928: /* fprintf(ficgp,"\nset out \"varmuptjgr%s%s%s.svg\";replot;",digitp,optionfilefiname,digit); */
8929: fprintf(ficgp,"\nset out;\nset out \"%s%s.svg\";replot;set out;\n",subdirf3(optionfilefiname,"VARMUPTJGR-",digitp),digit);
8930:
8931: free_vector(xp,1,npar);
8932: free_matrix(doldm,1,nlstate,1,nlstate);
8933: free_matrix(dnewm,1,nlstate,1,npar);
8934: free_matrix(doldmp,nlstate+1,nlstate+ndeath,nlstate+1,nlstate+ndeath);
8935: free_matrix(dnewmp,nlstate+1,nlstate+ndeath,1,npar);
8936: free_matrix(varppt,nlstate+1,nlstate+ndeath,nlstate+1,nlstate+ndeath);
8937: /* if (mobilav!=0) free_ma3x(mobaverage,1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
8938: fclose(ficresprobmorprev);
8939: fflush(ficgp);
8940: fflush(fichtm);
8941: } /* end varevsij */
8942:
8943: /************ Variance of prevlim ******************/
8944: 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)
8945: {
8946: /* Variance of prevalence limit for each state ij using current parameters x[] and estimates of neighbourhood give by delti*/
8947: /* double **prevalim(double **prlim, int nlstate, double *xp, double age, double **oldm, double **savm,double ftolpl);*/
8948:
8949: double **dnewmpar,**doldm;
8950: int i, j, nhstepm, hstepm;
8951: double *xp;
8952: double *gp, *gm;
8953: double **gradg, **trgradg;
8954: double **mgm, **mgp;
8955: double age,agelim;
8956: int theta;
8957:
8958: pstamp(ficresvpl);
8959: fprintf(ficresvpl,"# Standard deviation of period (forward stable) prevalences \n");
8960: fprintf(ficresvpl,"# Age ");
8961: if(nresult >=1)
8962: fprintf(ficresvpl," Result# ");
8963: for(i=1; i<=nlstate;i++)
8964: fprintf(ficresvpl," %1d-%1d",i,i);
8965: fprintf(ficresvpl,"\n");
8966:
8967: xp=vector(1,npar);
8968: dnewmpar=matrix(1,nlstate,1,npar);
8969: doldm=matrix(1,nlstate,1,nlstate);
8970:
8971: hstepm=1*YEARM; /* Every year of age */
8972: hstepm=hstepm/stepm; /* Typically in stepm units, if j= 2 years, = 2/6 months = 4 */
8973: agelim = AGESUP;
8974: for (age=bage; age<=fage; age ++){ /* If stepm=6 months */
8975: nhstepm=(int) rint((agelim-age)*YEARM/stepm); /* Typically 20 years = 20*12/6=40 */
8976: if (stepm >= YEARM) hstepm=1;
8977: nhstepm = nhstepm/hstepm; /* Typically 40/4=10 */
8978: gradg=matrix(1,npar,1,nlstate);
8979: mgp=matrix(1,npar,1,nlstate);
8980: mgm=matrix(1,npar,1,nlstate);
8981: gp=vector(1,nlstate);
8982: gm=vector(1,nlstate);
8983:
8984: for(theta=1; theta <=npar; theta++){
8985: for(i=1; i<=npar; i++){ /* Computes gradient */
8986: xp[i] = x[i] + (i==theta ?delti[theta]:0);
8987: }
8988: /* if((int)age==79 ||(int)age== 80 ||(int)age== 81 ) */
8989: /* prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp,ij,nres); */
8990: /* else */
8991: prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp,ij,nres);
8992: for(i=1;i<=nlstate;i++){
8993: gp[i] = prlim[i][i];
8994: mgp[theta][i] = prlim[i][i];
8995: }
8996: for(i=1; i<=npar; i++) /* Computes gradient */
8997: xp[i] = x[i] - (i==theta ?delti[theta]:0);
8998: /* if((int)age==79 ||(int)age== 80 ||(int)age== 81 ) */
8999: /* prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp,ij,nres); */
9000: /* else */
9001: prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp,ij,nres);
9002: for(i=1;i<=nlstate;i++){
9003: gm[i] = prlim[i][i];
9004: mgm[theta][i] = prlim[i][i];
9005: }
9006: for(i=1;i<=nlstate;i++)
9007: gradg[theta][i]= (gp[i]-gm[i])/2./delti[theta];
9008: /* gradg[theta][2]= -gradg[theta][1]; */ /* For testing if nlstate=2 */
9009: } /* End theta */
9010:
9011: trgradg =matrix(1,nlstate,1,npar);
9012:
9013: for(j=1; j<=nlstate;j++)
9014: for(theta=1; theta <=npar; theta++)
9015: trgradg[j][theta]=gradg[theta][j];
9016: /* if((int)age==79 ||(int)age== 80 ||(int)age== 81 ){ */
9017: /* printf("\nmgm mgp %d ",(int)age); */
9018: /* for(j=1; j<=nlstate;j++){ */
9019: /* printf(" %d ",j); */
9020: /* for(theta=1; theta <=npar; theta++) */
9021: /* printf(" %d %lf %lf",theta,mgm[theta][j],mgp[theta][j]); */
9022: /* printf("\n "); */
9023: /* } */
9024: /* } */
9025: /* if((int)age==79 ||(int)age== 80 ||(int)age== 81 ){ */
9026: /* printf("\n gradg %d ",(int)age); */
9027: /* for(j=1; j<=nlstate;j++){ */
9028: /* printf("%d ",j); */
9029: /* for(theta=1; theta <=npar; theta++) */
9030: /* printf("%d %lf ",theta,gradg[theta][j]); */
9031: /* printf("\n "); */
9032: /* } */
9033: /* } */
9034:
9035: for(i=1;i<=nlstate;i++)
9036: varpl[i][(int)age] =0.;
9037: if((int)age==79 ||(int)age== 80 ||(int)age== 81){
9038: matprod2(dnewmpar,trgradg,1,nlstate,1,npar,1,npar,matcov);
9039: matprod2(doldm,dnewmpar,1,nlstate,1,npar,1,nlstate,gradg);
9040: }else{
9041: matprod2(dnewmpar,trgradg,1,nlstate,1,npar,1,npar,matcov);
9042: matprod2(doldm,dnewmpar,1,nlstate,1,npar,1,nlstate,gradg);
9043: }
9044: for(i=1;i<=nlstate;i++)
9045: varpl[i][(int)age] = doldm[i][i]; /* Covariances are useless */
9046:
9047: fprintf(ficresvpl,"%.0f ",age );
9048: if(nresult >=1)
9049: fprintf(ficresvpl,"%d ",nres );
9050: for(i=1; i<=nlstate;i++){
9051: fprintf(ficresvpl," %.5f (%.5f)",prlim[i][i],sqrt(varpl[i][(int)age]));
9052: /* for(j=1;j<=nlstate;j++) */
9053: /* fprintf(ficresvpl," %d %.5f ",j,prlim[j][i]); */
9054: }
9055: fprintf(ficresvpl,"\n");
9056: free_vector(gp,1,nlstate);
9057: free_vector(gm,1,nlstate);
9058: free_matrix(mgm,1,npar,1,nlstate);
9059: free_matrix(mgp,1,npar,1,nlstate);
9060: free_matrix(gradg,1,npar,1,nlstate);
9061: free_matrix(trgradg,1,nlstate,1,npar);
9062: } /* End age */
9063:
9064: free_vector(xp,1,npar);
9065: free_matrix(doldm,1,nlstate,1,npar);
9066: free_matrix(dnewmpar,1,nlstate,1,nlstate);
9067:
9068: }
9069:
9070:
9071: /************ Variance of backprevalence limit ******************/
9072: 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)
9073: {
9074: /* Variance of backward prevalence limit for each state ij using current parameters x[] and estimates of neighbourhood give by delti*/
9075: /* double **prevalim(double **prlim, int nlstate, double *xp, double age, double **oldm, double **savm,double ftolpl);*/
9076:
9077: double **dnewmpar,**doldm;
9078: int i, j, nhstepm, hstepm;
9079: double *xp;
9080: double *gp, *gm;
9081: double **gradg, **trgradg;
9082: double **mgm, **mgp;
9083: double age,agelim;
9084: int theta;
9085:
9086: pstamp(ficresvbl);
9087: fprintf(ficresvbl,"# Standard deviation of back (stable) prevalences \n");
9088: fprintf(ficresvbl,"# Age ");
9089: if(nresult >=1)
9090: fprintf(ficresvbl," Result# ");
9091: for(i=1; i<=nlstate;i++)
9092: fprintf(ficresvbl," %1d-%1d",i,i);
9093: fprintf(ficresvbl,"\n");
9094:
9095: xp=vector(1,npar);
9096: dnewmpar=matrix(1,nlstate,1,npar);
9097: doldm=matrix(1,nlstate,1,nlstate);
9098:
9099: hstepm=1*YEARM; /* Every year of age */
9100: hstepm=hstepm/stepm; /* Typically in stepm units, if j= 2 years, = 2/6 months = 4 */
9101: agelim = AGEINF;
9102: for (age=fage; age>=bage; age --){ /* If stepm=6 months */
9103: nhstepm=(int) rint((age-agelim)*YEARM/stepm); /* Typically 20 years = 20*12/6=40 */
9104: if (stepm >= YEARM) hstepm=1;
9105: nhstepm = nhstepm/hstepm; /* Typically 40/4=10 */
9106: gradg=matrix(1,npar,1,nlstate);
9107: mgp=matrix(1,npar,1,nlstate);
9108: mgm=matrix(1,npar,1,nlstate);
9109: gp=vector(1,nlstate);
9110: gm=vector(1,nlstate);
9111:
9112: for(theta=1; theta <=npar; theta++){
9113: for(i=1; i<=npar; i++){ /* Computes gradient */
9114: xp[i] = x[i] + (i==theta ?delti[theta]:0);
9115: }
9116: if(mobilavproj > 0 )
9117: bprevalim(bprlim, mobaverage,nlstate,xp,age,ftolpl,ncvyearp,ij,nres);
9118: else
9119: bprevalim(bprlim, mobaverage,nlstate,xp,age,ftolpl,ncvyearp,ij,nres);
9120: for(i=1;i<=nlstate;i++){
9121: gp[i] = bprlim[i][i];
9122: mgp[theta][i] = bprlim[i][i];
9123: }
9124: for(i=1; i<=npar; i++) /* Computes gradient */
9125: xp[i] = x[i] - (i==theta ?delti[theta]:0);
9126: if(mobilavproj > 0 )
9127: bprevalim(bprlim, mobaverage,nlstate,xp,age,ftolpl,ncvyearp,ij,nres);
9128: else
9129: bprevalim(bprlim, mobaverage,nlstate,xp,age,ftolpl,ncvyearp,ij,nres);
9130: for(i=1;i<=nlstate;i++){
9131: gm[i] = bprlim[i][i];
9132: mgm[theta][i] = bprlim[i][i];
9133: }
9134: for(i=1;i<=nlstate;i++)
9135: gradg[theta][i]= (gp[i]-gm[i])/2./delti[theta];
9136: /* gradg[theta][2]= -gradg[theta][1]; */ /* For testing if nlstate=2 */
9137: } /* End theta */
9138:
9139: trgradg =matrix(1,nlstate,1,npar);
9140:
9141: for(j=1; j<=nlstate;j++)
9142: for(theta=1; theta <=npar; theta++)
9143: trgradg[j][theta]=gradg[theta][j];
9144: /* if((int)age==79 ||(int)age== 80 ||(int)age== 81 ){ */
9145: /* printf("\nmgm mgp %d ",(int)age); */
9146: /* for(j=1; j<=nlstate;j++){ */
9147: /* printf(" %d ",j); */
9148: /* for(theta=1; theta <=npar; theta++) */
9149: /* printf(" %d %lf %lf",theta,mgm[theta][j],mgp[theta][j]); */
9150: /* printf("\n "); */
9151: /* } */
9152: /* } */
9153: /* if((int)age==79 ||(int)age== 80 ||(int)age== 81 ){ */
9154: /* printf("\n gradg %d ",(int)age); */
9155: /* for(j=1; j<=nlstate;j++){ */
9156: /* printf("%d ",j); */
9157: /* for(theta=1; theta <=npar; theta++) */
9158: /* printf("%d %lf ",theta,gradg[theta][j]); */
9159: /* printf("\n "); */
9160: /* } */
9161: /* } */
9162:
9163: for(i=1;i<=nlstate;i++)
9164: varbpl[i][(int)age] =0.;
9165: if((int)age==79 ||(int)age== 80 ||(int)age== 81){
9166: matprod2(dnewmpar,trgradg,1,nlstate,1,npar,1,npar,matcov);
9167: matprod2(doldm,dnewmpar,1,nlstate,1,npar,1,nlstate,gradg);
9168: }else{
9169: matprod2(dnewmpar,trgradg,1,nlstate,1,npar,1,npar,matcov);
9170: matprod2(doldm,dnewmpar,1,nlstate,1,npar,1,nlstate,gradg);
9171: }
9172: for(i=1;i<=nlstate;i++)
9173: varbpl[i][(int)age] = doldm[i][i]; /* Covariances are useless */
9174:
9175: fprintf(ficresvbl,"%.0f ",age );
9176: if(nresult >=1)
9177: fprintf(ficresvbl,"%d ",nres );
9178: for(i=1; i<=nlstate;i++)
9179: fprintf(ficresvbl," %.5f (%.5f)",bprlim[i][i],sqrt(varbpl[i][(int)age]));
9180: fprintf(ficresvbl,"\n");
9181: free_vector(gp,1,nlstate);
9182: free_vector(gm,1,nlstate);
9183: free_matrix(mgm,1,npar,1,nlstate);
9184: free_matrix(mgp,1,npar,1,nlstate);
9185: free_matrix(gradg,1,npar,1,nlstate);
9186: free_matrix(trgradg,1,nlstate,1,npar);
9187: } /* End age */
9188:
9189: free_vector(xp,1,npar);
9190: free_matrix(doldm,1,nlstate,1,npar);
9191: free_matrix(dnewmpar,1,nlstate,1,nlstate);
9192:
9193: }
9194:
9195: /************ Variance of one-step probabilities ******************/
9196: 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[])
9197: {
9198: int i, j=0, k1, l1, tj;
9199: int k2, l2, j1, z1;
9200: int k=0, l;
9201: int first=1, first1, first2;
9202: int nres=0; /* New */
9203: double cv12, mu1, mu2, lc1, lc2, v12, v21, v11, v22,v1,v2, c12, tnalp;
9204: double **dnewm,**doldm;
9205: double *xp;
9206: double *gp, *gm;
9207: double **gradg, **trgradg;
9208: double **mu;
9209: double age, cov[NCOVMAX+1];
9210: double std=2.0; /* Number of standard deviation wide of confidence ellipsoids */
9211: int theta;
9212: char fileresprob[FILENAMELENGTH];
9213: char fileresprobcov[FILENAMELENGTH];
9214: char fileresprobcor[FILENAMELENGTH];
9215: double ***varpij;
9216:
9217: strcpy(fileresprob,"PROB_");
9218: strcat(fileresprob,fileresu);
9219: if((ficresprob=fopen(fileresprob,"w"))==NULL) {
9220: printf("Problem with resultfile: %s\n", fileresprob);
9221: fprintf(ficlog,"Problem with resultfile: %s\n", fileresprob);
9222: }
9223: strcpy(fileresprobcov,"PROBCOV_");
9224: strcat(fileresprobcov,fileresu);
9225: if((ficresprobcov=fopen(fileresprobcov,"w"))==NULL) {
9226: printf("Problem with resultfile: %s\n", fileresprobcov);
9227: fprintf(ficlog,"Problem with resultfile: %s\n", fileresprobcov);
9228: }
9229: strcpy(fileresprobcor,"PROBCOR_");
9230: strcat(fileresprobcor,fileresu);
9231: if((ficresprobcor=fopen(fileresprobcor,"w"))==NULL) {
9232: printf("Problem with resultfile: %s\n", fileresprobcor);
9233: fprintf(ficlog,"Problem with resultfile: %s\n", fileresprobcor);
9234: }
9235: printf("Computing standard deviation of one-step probabilities: result on file '%s' \n",fileresprob);
9236: fprintf(ficlog,"Computing standard deviation of one-step probabilities: result on file '%s' \n",fileresprob);
9237: printf("Computing matrix of variance covariance of one-step probabilities: result on file '%s' \n",fileresprobcov);
9238: fprintf(ficlog,"Computing matrix of variance covariance of one-step probabilities: result on file '%s' \n",fileresprobcov);
9239: printf("and correlation matrix of one-step probabilities: result on file '%s' \n",fileresprobcor);
9240: fprintf(ficlog,"and correlation matrix of one-step probabilities: result on file '%s' \n",fileresprobcor);
9241: pstamp(ficresprob);
9242: fprintf(ficresprob,"#One-step probabilities and stand. devi in ()\n");
9243: fprintf(ficresprob,"# Age");
9244: pstamp(ficresprobcov);
9245: fprintf(ficresprobcov,"#One-step probabilities and covariance matrix\n");
9246: fprintf(ficresprobcov,"# Age");
9247: pstamp(ficresprobcor);
9248: fprintf(ficresprobcor,"#One-step probabilities and correlation matrix\n");
9249: fprintf(ficresprobcor,"# Age");
9250:
9251:
9252: for(i=1; i<=nlstate;i++)
9253: for(j=1; j<=(nlstate+ndeath);j++){
9254: fprintf(ficresprob," p%1d-%1d (SE)",i,j);
9255: fprintf(ficresprobcov," p%1d-%1d ",i,j);
9256: fprintf(ficresprobcor," p%1d-%1d ",i,j);
9257: }
9258: /* fprintf(ficresprob,"\n");
9259: fprintf(ficresprobcov,"\n");
9260: fprintf(ficresprobcor,"\n");
9261: */
9262: xp=vector(1,npar);
9263: dnewm=matrix(1,(nlstate)*(nlstate+ndeath),1,npar);
9264: doldm=matrix(1,(nlstate)*(nlstate+ndeath),1,(nlstate)*(nlstate+ndeath));
9265: mu=matrix(1,(nlstate)*(nlstate+ndeath), (int) bage, (int)fage);
9266: varpij=ma3x(1,nlstate*(nlstate+ndeath),1,nlstate*(nlstate+ndeath),(int) bage, (int) fage);
9267: first=1;
9268: fprintf(ficgp,"\n# Routine varprob");
9269: fprintf(fichtm,"\n<li><h4> Computing and drawing one step probabilities with their confidence intervals</h4></li>\n");
9270: fprintf(fichtm,"\n");
9271:
9272: 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);
9273: 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);
9274: fprintf(fichtmcov,"\nEllipsoids of confidence centered on point (p<inf>ij</inf>, p<inf>kl</inf>) are estimated \
9275: and drawn. It helps understanding how is the covariance between two incidences.\
9276: They are expressed in year<sup>-1</sup> in order to be less dependent of stepm.<br>\n");
9277: 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. \
9278: It can be understood this way: if pij and pkl where uncorrelated the (2x2) matrix of covariance \
9279: would have been (1/(var pij), 0 , 0, 1/(var pkl)), and the confidence interval would be 2 \
9280: standard deviations wide on each axis. <br>\
9281: Now, if both incidences are correlated (usual case) we diagonalised the inverse of the covariance matrix\
9282: and made the appropriate rotation to look at the uncorrelated principal directions.<br>\
9283: To be simple, these graphs help to understand the significativity of each parameter in relation to a second other one.<br> \n");
9284:
9285: cov[1]=1;
9286: /* tj=cptcoveff; */
9287: tj = (int) pow(2,cptcoveff);
9288: if (cptcovn<1) {tj=1;ncodemax[1]=1;}
9289: j1=0;
9290:
9291: for(nres=1;nres <=nresult; nres++){ /* For each resultline */
9292: for(j1=1; j1<=tj;j1++){ /* For any combination of dummy covariates, fixed and varying */
9293: /* 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); */
9294: if(tj != 1 && TKresult[nres]!= j1)
9295: continue;
9296:
9297: /* for(j1=1; j1<=tj;j1++){ /\* For each valid combination of covariates or only once*\/ */
9298: /* for(nres=1;nres <=1; nres++){ /\* For each resultline *\/ */
9299: /* /\* for(nres=1;nres <=nresult; nres++){ /\\* For each resultline *\\/ *\/ */
9300: if (cptcovn>0) {
9301: fprintf(ficresprob, "\n#********** Variable ");
9302: fprintf(ficresprobcov, "\n#********** Variable ");
9303: fprintf(ficgp, "\n#********** Variable ");
9304: fprintf(fichtmcov, "\n<hr size=\"2\" color=\"#EC5E5E\">********** Variable ");
9305: fprintf(ficresprobcor, "\n#********** Variable ");
9306:
9307: /* Including quantitative variables of the resultline to be done */
9308: for (z1=1; z1<=cptcovs; z1++){ /* Loop on each variable of this resultline */
9309: /* printf("Varprob modelresult[%d][%d]=%d model=1+age+%s \n",nres, z1, modelresult[nres][z1], model); */
9310: fprintf(ficlog,"Varprob modelresult[%d][%d]=%d model=1+age+%s \n",nres, z1, modelresult[nres][z1], model);
9311: /* fprintf(ficlog,"Varprob modelresult[%d][%d]=%d model=1+age+%s resultline[%d]=%s \n",nres, z1, modelresult[nres][z1], model, nres, resultline[nres]); */
9312: if(Dummy[modelresult[nres][z1]]==0){/* Dummy variable of the variable in position modelresult in the model corresponding to z1 in resultline */
9313: if(Fixed[modelresult[nres][z1]]==0){ /* Fixed referenced to model equation */
9314: 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 */
9315: 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 */
9316: 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 */
9317: 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 */
9318: 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 */
9319: fprintf(ficresprob,"fixed ");
9320: fprintf(ficresprobcov,"fixed ");
9321: fprintf(ficgp,"fixed ");
9322: fprintf(fichtmcov,"fixed ");
9323: fprintf(ficresprobcor,"fixed ");
9324: }else{
9325: fprintf(ficresprob,"varyi ");
9326: fprintf(ficresprobcov,"varyi ");
9327: fprintf(ficgp,"varyi ");
9328: fprintf(fichtmcov,"varyi ");
9329: fprintf(ficresprobcor,"varyi ");
9330: }
9331: }else if(Dummy[modelresult[nres][z1]]==1){ /* Quanti variable */
9332: /* For each selected (single) quantitative value */
9333: fprintf(ficresprob," V%d=%lg ",Tvqresult[nres][z1],Tqresult[nres][z1]);
9334: if(Fixed[modelresult[nres][z1]]==0){ /* Fixed */
9335: fprintf(ficresprob,"fixed ");
9336: fprintf(ficresprobcov,"fixed ");
9337: fprintf(ficgp,"fixed ");
9338: fprintf(fichtmcov,"fixed ");
9339: fprintf(ficresprobcor,"fixed ");
9340: }else{
9341: fprintf(ficresprob,"varyi ");
9342: fprintf(ficresprobcov,"varyi ");
9343: fprintf(ficgp,"varyi ");
9344: fprintf(fichtmcov,"varyi ");
9345: fprintf(ficresprobcor,"varyi ");
9346: }
9347: }else{
9348: 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 */
9349: 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 */
9350: exit(1);
9351: }
9352: } /* End loop on variable of this resultline */
9353: /* for (z1=1; z1<=cptcoveff; z1++) fprintf(ficresprob, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]); */
9354: fprintf(ficresprob, "**********\n#\n");
9355: fprintf(ficresprobcov, "**********\n#\n");
9356: fprintf(ficgp, "**********\n#\n");
9357: fprintf(fichtmcov, "**********\n<hr size=\"2\" color=\"#EC5E5E\">");
9358: fprintf(ficresprobcor, "**********\n#");
9359: if(invalidvarcomb[j1]){
9360: fprintf(ficgp,"\n#Combination (%d) ignored because no cases \n",j1);
9361: fprintf(fichtmcov,"\n<h3>Combination (%d) ignored because no cases </h3>\n",j1);
9362: continue;
9363: }
9364: }
9365: gradg=matrix(1,npar,1,(nlstate)*(nlstate+ndeath));
9366: trgradg=matrix(1,(nlstate)*(nlstate+ndeath),1,npar);
9367: gp=vector(1,(nlstate)*(nlstate+ndeath));
9368: gm=vector(1,(nlstate)*(nlstate+ndeath));
9369: for (age=bage; age<=fage; age ++){ /* Fo each age we feed the model equation with covariates, using precov as in hpxij() ? */
9370: cov[2]=age;
9371: if(nagesqr==1)
9372: cov[3]= age*age;
9373: /* New code end of combination but for each resultline */
9374: for(k1=1;k1<=cptcovt;k1++){ /* loop on model equation (including products) */
9375: if(Typevar[k1]==1 || Typevar[k1] ==3){ /* A product with age */
9376: cov[2+nagesqr+k1]=precov[nres][k1]*cov[2];
9377: }else{
9378: cov[2+nagesqr+k1]=precov[nres][k1];
9379: }
9380: }/* End of loop on model equation */
9381: /* Old code */
9382: /* /\* for (k=1; k<=cptcovn;k++) { *\/ */
9383: /* /\* cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(j1,k)]; *\/ */
9384: /* for (k=1; k<=nsd;k++) { /\* For single dummy covariates only *\/ */
9385: /* /\* Here comes the value of the covariate 'j1' after renumbering k with single dummy covariates *\/ */
9386: /* cov[2+nagesqr+TvarsDind[k]]=nbcode[TvarsD[k]][codtabm(j1,TnsdVar[TvarsD[k]])]; */
9387: /* /\*cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(j1,Tvar[k])];*\//\* j1 1 2 3 4 */
9388: /* * 1 1 1 1 1 */
9389: /* * 2 2 1 1 1 */
9390: /* * 3 1 2 1 1 */
9391: /* *\/ */
9392: /* /\* nbcode[1][1]=0 nbcode[1][2]=1;*\/ */
9393: /* } */
9394: /* /\* V2+V1+V4+V3*age Tvar[4]=3 ; V1+V2*age Tvar[2]=2; V1+V1*age Tvar[2]=1, Tage[1]=2 *\/ */
9395: /* /\* ) p nbcode[Tvar[Tage[k]]][(1 & (ij-1) >> (k-1))+1] *\/ */
9396: /* /\*for (k=1; k<=cptcovage;k++) cov[2+Tage[k]]=cov[2+Tage[k]]*cov[2]; *\/ */
9397: /* for (k=1; k<=cptcovage;k++){ /\* For product with age *\/ */
9398: /* if(Dummy[Tage[k]]==2){ /\* dummy with age *\/ */
9399: /* cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(j1,TnsdVar[Tvar[Tage[k]]])]*cov[2]; */
9400: /* /\* cov[++k1]=nbcode[Tvar[Tage[k]]][codtabm(ij,k)]*cov[2]; *\/ */
9401: /* } else if(Dummy[Tage[k]]==3){ /\* quantitative with age *\/ */
9402: /* 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]); */
9403: /* /\* cov[2+nagesqr+Tage[k]]=meanq[k]/idq[k]*cov[2];/\\* Using the mean of quantitative variable Tvar[Tage[k]] /\\* Tqresult[nres][k]; *\\/ *\/ */
9404: /* /\* exit(1); *\/ */
9405: /* /\* cov[++k1]=Tqresult[nres][k]; *\/ */
9406: /* } */
9407: /* /\* cov[2+Tage[k]+nagesqr]=nbcode[Tvar[Tage[k]]][codtabm(ij,k)]*cov[2]; *\/ */
9408: /* } */
9409: /* for (k=1; k<=cptcovprod;k++){/\* For product without age *\/ */
9410: /* if(Dummy[Tvard[k][1]]==0){ */
9411: /* if(Dummy[Tvard[k][2]]==0){ */
9412: /* 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]])]; */
9413: /* /\* cov[++k1]=nbcode[Tvard[k][1]][codtabm(ij,k)] * nbcode[Tvard[k][2]][codtabm(ij,k)]; *\/ */
9414: /* }else{ /\* Should we use the mean of the quantitative variables? *\/ */
9415: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(j1,TnsdVar[Tvard[k][1]])] * Tqresult[nres][resultmodel[nres][k]]; */
9416: /* /\* cov[++k1]=nbcode[Tvard[k][1]][codtabm(ij,k)] * Tqresult[nres][k]; *\/ */
9417: /* } */
9418: /* }else{ */
9419: /* if(Dummy[Tvard[k][2]]==0){ */
9420: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][2]][codtabm(j1,TnsdVar[Tvard[k][2]])] * Tqinvresult[nres][TnsdVar[Tvard[k][1]]]; */
9421: /* /\* cov[++k1]=nbcode[Tvard[k][2]][codtabm(ij,k)] * Tqinvresult[nres][Tvard[k][1]]; *\/ */
9422: /* }else{ */
9423: /* cov[2+nagesqr+Tprod[k]]=Tqinvresult[nres][TnsdVar[Tvard[k][1]]]* Tqinvresult[nres][TnsdVar[Tvard[k][2]]]; */
9424: /* /\* cov[++k1]=Tqinvresult[nres][Tvard[k][1]]* Tqinvresult[nres][Tvard[k][2]]; *\/ */
9425: /* } */
9426: /* } */
9427: /* /\* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)]*nbcode[Tvard[k][2]][codtabm(ij,k)]; *\/ */
9428: /* } */
9429: /* For each age and combination of dummy covariates we slightly move the parameters of delti in order to get the gradient*/
9430: for(theta=1; theta <=npar; theta++){
9431: for(i=1; i<=npar; i++)
9432: xp[i] = x[i] + (i==theta ?delti[theta]:(double)0);
9433:
9434: pmij(pmmij,cov,ncovmodel,xp,nlstate);
9435:
9436: k=0;
9437: for(i=1; i<= (nlstate); i++){
9438: for(j=1; j<=(nlstate+ndeath);j++){
9439: k=k+1;
9440: gp[k]=pmmij[i][j];
9441: }
9442: }
9443:
9444: for(i=1; i<=npar; i++)
9445: xp[i] = x[i] - (i==theta ?delti[theta]:(double)0);
9446:
9447: pmij(pmmij,cov,ncovmodel,xp,nlstate);
9448: k=0;
9449: for(i=1; i<=(nlstate); i++){
9450: for(j=1; j<=(nlstate+ndeath);j++){
9451: k=k+1;
9452: gm[k]=pmmij[i][j];
9453: }
9454: }
9455:
9456: for(i=1; i<= (nlstate)*(nlstate+ndeath); i++)
9457: gradg[theta][i]=(gp[i]-gm[i])/(double)2./delti[theta];
9458: }
9459:
9460: for(j=1; j<=(nlstate)*(nlstate+ndeath);j++)
9461: for(theta=1; theta <=npar; theta++)
9462: trgradg[j][theta]=gradg[theta][j];
9463:
9464: matprod2(dnewm,trgradg,1,(nlstate)*(nlstate+ndeath),1,npar,1,npar,matcov);
9465: matprod2(doldm,dnewm,1,(nlstate)*(nlstate+ndeath),1,npar,1,(nlstate)*(nlstate+ndeath),gradg);
9466:
9467: pmij(pmmij,cov,ncovmodel,x,nlstate);
9468:
9469: k=0;
9470: for(i=1; i<=(nlstate); i++){
9471: for(j=1; j<=(nlstate+ndeath);j++){
9472: k=k+1;
9473: mu[k][(int) age]=pmmij[i][j];
9474: }
9475: }
9476: for(i=1;i<=(nlstate)*(nlstate+ndeath);i++)
9477: for(j=1;j<=(nlstate)*(nlstate+ndeath);j++)
9478: varpij[i][j][(int)age] = doldm[i][j];
9479:
9480: /*printf("\n%d ",(int)age);
9481: for (i=1; i<=(nlstate)*(nlstate+ndeath);i++){
9482: printf("%e [%e ;%e] ",gm[i],gm[i]-2*sqrt(doldm[i][i]),gm[i]+2*sqrt(doldm[i][i]));
9483: fprintf(ficlog,"%e [%e ;%e] ",gm[i],gm[i]-2*sqrt(doldm[i][i]),gm[i]+2*sqrt(doldm[i][i]));
9484: }*/
9485:
9486: fprintf(ficresprob,"\n%d ",(int)age);
9487: fprintf(ficresprobcov,"\n%d ",(int)age);
9488: fprintf(ficresprobcor,"\n%d ",(int)age);
9489:
9490: for (i=1; i<=(nlstate)*(nlstate+ndeath);i++)
9491: fprintf(ficresprob,"%11.3e (%11.3e) ",mu[i][(int) age],sqrt(varpij[i][i][(int)age]));
9492: for (i=1; i<=(nlstate)*(nlstate+ndeath);i++){
9493: fprintf(ficresprobcov,"%11.3e ",mu[i][(int) age]);
9494: fprintf(ficresprobcor,"%11.3e ",mu[i][(int) age]);
9495: }
9496: i=0;
9497: for (k=1; k<=(nlstate);k++){
9498: for (l=1; l<=(nlstate+ndeath);l++){
9499: i++;
9500: fprintf(ficresprobcov,"\n%d %d-%d",(int)age,k,l);
9501: fprintf(ficresprobcor,"\n%d %d-%d",(int)age,k,l);
9502: for (j=1; j<=i;j++){
9503: /* printf(" k=%d l=%d i=%d j=%d\n",k,l,i,j);fflush(stdout); */
9504: fprintf(ficresprobcov," %11.3e",varpij[i][j][(int)age]);
9505: fprintf(ficresprobcor," %11.3e",varpij[i][j][(int) age]/sqrt(varpij[i][i][(int) age])/sqrt(varpij[j][j][(int)age]));
9506: }
9507: }
9508: }/* end of loop for state */
9509: } /* end of loop for age */
9510: free_vector(gp,1,(nlstate+ndeath)*(nlstate+ndeath));
9511: free_vector(gm,1,(nlstate+ndeath)*(nlstate+ndeath));
9512: free_matrix(trgradg,1,(nlstate+ndeath)*(nlstate+ndeath),1,npar);
9513: free_matrix(gradg,1,(nlstate+ndeath)*(nlstate+ndeath),1,npar);
9514:
9515: /* Confidence intervalle of pij */
9516: /*
9517: fprintf(ficgp,"\nunset parametric;unset label");
9518: fprintf(ficgp,"\nset log y;unset log x; set xlabel \"Age\";set ylabel \"probability (year-1)\"");
9519: fprintf(ficgp,"\nset ter png small\nset size 0.65,0.65");
9520: 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);
9521: fprintf(fichtm,"\n<br><img src=\"pijgr%s.png\"> ",optionfilefiname);
9522: fprintf(ficgp,"\nset out \"pijgr%s.png\"",optionfilefiname);
9523: fprintf(ficgp,"\nplot \"%s\" every :::%d::%d u 1:2 \"\%%lf",k1,k2,xfilevarprob);
9524: */
9525:
9526: /* Drawing ellipsoids of confidence of two variables p(k1-l1,k2-l2)*/
9527: first1=1;first2=2;
9528: for (k2=1; k2<=(nlstate);k2++){
9529: for (l2=1; l2<=(nlstate+ndeath);l2++){
9530: if(l2==k2) continue;
9531: j=(k2-1)*(nlstate+ndeath)+l2;
9532: for (k1=1; k1<=(nlstate);k1++){
9533: for (l1=1; l1<=(nlstate+ndeath);l1++){
9534: if(l1==k1) continue;
9535: i=(k1-1)*(nlstate+ndeath)+l1;
9536: if(i<=j) continue;
9537: for (age=bage; age<=fage; age ++){
9538: if ((int)age %5==0){
9539: v1=varpij[i][i][(int)age]/stepm*YEARM/stepm*YEARM;
9540: v2=varpij[j][j][(int)age]/stepm*YEARM/stepm*YEARM;
9541: cv12=varpij[i][j][(int)age]/stepm*YEARM/stepm*YEARM;
9542: mu1=mu[i][(int) age]/stepm*YEARM ;
9543: mu2=mu[j][(int) age]/stepm*YEARM;
9544: c12=cv12/sqrt(v1*v2);
9545: /* Computing eigen value of matrix of covariance */
9546: lc1=((v1+v2)+sqrt((v1+v2)*(v1+v2) - 4*(v1*v2-cv12*cv12)))/2.;
9547: lc2=((v1+v2)-sqrt((v1+v2)*(v1+v2) - 4*(v1*v2-cv12*cv12)))/2.;
9548: if ((lc2 <0) || (lc1 <0) ){
9549: if(first2==1){
9550: first1=0;
9551: 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);
9552: }
9553: 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);
9554: /* lc1=fabs(lc1); */ /* If we want to have them positive */
9555: /* lc2=fabs(lc2); */
9556: }
9557:
9558: /* Eigen vectors */
9559: if(1+(v1-lc1)*(v1-lc1)/cv12/cv12 <1.e-5){
9560: printf(" Error sqrt of a negative number: %lf\n",1+(v1-lc1)*(v1-lc1)/cv12/cv12);
9561: fprintf(ficlog," Error sqrt of a negative number: %lf\n",1+(v1-lc1)*(v1-lc1)/cv12/cv12);
9562: v11=(1./sqrt(fabs(1+(v1-lc1)*(v1-lc1)/cv12/cv12)));
9563: }else
9564: v11=(1./sqrt(1+(v1-lc1)*(v1-lc1)/cv12/cv12));
9565: /*v21=sqrt(1.-v11*v11); *//* error */
9566: v21=(lc1-v1)/cv12*v11;
9567: v12=-v21;
9568: v22=v11;
9569: tnalp=v21/v11;
9570: if(first1==1){
9571: first1=0;
9572: 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);
9573: }
9574: 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);
9575: /*printf(fignu*/
9576: /* mu1+ v11*lc1*cost + v12*lc2*sin(t) */
9577: /* mu2+ v21*lc1*cost + v22*lc2*sin(t) */
9578: if(first==1){
9579: first=0;
9580: fprintf(ficgp,"\n# Ellipsoids of confidence\n#\n");
9581: fprintf(ficgp,"\nset parametric;unset label");
9582: 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);
9583: fprintf(ficgp,"\nset ter svg size 640, 480");
9584: fprintf(fichtmcov,"\n<p><br>Ellipsoids of confidence cov(p%1d%1d,p%1d%1d) expressed in year<sup>-1</sup>\
9585: :<a href=\"%s_%d%1d%1d-%1d%1d.svg\"> \
9586: %s_%d%1d%1d-%1d%1d.svg</A>, ",k1,l1,k2,l2,\
9587: subdirf2(optionfilefiname,"VARPIJGR_"), j1,k1,l1,k2,l2, \
9588: subdirf2(optionfilefiname,"VARPIJGR_"), j1,k1,l1,k2,l2);
9589: fprintf(fichtmcov,"\n<br><img src=\"%s_%d%1d%1d-%1d%1d.svg\"> ",subdirf2(optionfilefiname,"VARPIJGR_"), j1,k1,l1,k2,l2);
9590: fprintf(fichtmcov,"\n<br> Correlation at age %d (%.3f),",(int) age, c12);
9591: fprintf(ficgp,"\nset out \"%s_%d%1d%1d-%1d%1d.svg\"",subdirf2(optionfilefiname,"VARPIJGR_"), j1,k1,l1,k2,l2);
9592: fprintf(ficgp,"\nset label \"%d\" at %11.3e,%11.3e center",(int) age, mu1,mu2);
9593: fprintf(ficgp,"\n# Age %d, p%1d%1d - p%1d%1d",(int) age, k1,l1,k2,l2);
9594: 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", \
9595: mu1,std,v11,sqrt(fabs(lc1)),v12,sqrt(fabs(lc2)), \
9596: mu2,std,v21,sqrt(fabs(lc1)),v22,sqrt(fabs(lc2))); /* For gnuplot only */
9597: }else{
9598: first=0;
9599: fprintf(fichtmcov," %d (%.3f),",(int) age, c12);
9600: fprintf(ficgp,"\n# Age %d, p%1d%1d - p%1d%1d",(int) age, k1,l1,k2,l2);
9601: fprintf(ficgp,"\nset label \"%d\" at %11.3e,%11.3e center",(int) age, mu1,mu2);
9602: 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", \
9603: mu1,std,v11,sqrt(lc1),v12,sqrt(fabs(lc2)), \
9604: mu2,std,v21,sqrt(lc1),v22,sqrt(fabs(lc2)));
9605: }/* if first */
9606: } /* age mod 5 */
9607: } /* end loop age */
9608: fprintf(ficgp,"\nset out;\nset out \"%s_%d%1d%1d-%1d%1d.svg\";replot;set out;",subdirf2(optionfilefiname,"VARPIJGR_"), j1,k1,l1,k2,l2);
9609: first=1;
9610: } /*l12 */
9611: } /* k12 */
9612: } /*l1 */
9613: }/* k1 */
9614: } /* loop on combination of covariates j1 */
9615: } /* loop on nres */
9616: free_ma3x(varpij,1,nlstate,1,nlstate+ndeath,(int) bage, (int)fage);
9617: free_matrix(mu,1,(nlstate+ndeath)*(nlstate+ndeath),(int) bage, (int)fage);
9618: free_matrix(doldm,1,(nlstate)*(nlstate+ndeath),1,(nlstate)*(nlstate+ndeath));
9619: free_matrix(dnewm,1,(nlstate)*(nlstate+ndeath),1,npar);
9620: free_vector(xp,1,npar);
9621: fclose(ficresprob);
9622: fclose(ficresprobcov);
9623: fclose(ficresprobcor);
9624: fflush(ficgp);
9625: fflush(fichtmcov);
9626: }
9627:
9628:
9629: /******************* Printing html file ***********/
9630: void printinghtml(char fileresu[], char title[], char datafile[], int firstpass, \
9631: int lastpass, int stepm, int weightopt, char model[],\
9632: int imx,int jmin, int jmax, double jmeanint,char rfileres[],\
9633: int popforecast, int mobilav, int prevfcast, int mobilavproj, int prevbcast, int estepm , \
9634: double jprev1, double mprev1,double anprev1, double dateprev1, double dateprojd, double dateback1, \
9635: double jprev2, double mprev2,double anprev2, double dateprev2, double dateprojf, double dateback2){
9636: int jj1, k1, cpt, nres;
9637: /* In fact some results are already printed in fichtm which is open */
9638: fprintf(fichtm,"<ul><li><a href='#firstorder'>Result files (first order: no variance)</a>\n \
9639: <li><a href='#secondorder'>Result files (second order (variance)</a>\n \
9640: </ul>");
9641: /* fprintf(fichtm,"<ul><li> model=1+age+%s\n \ */
9642: /* </ul>", model); */
9643: fprintf(fichtm,"<ul><li><h4><a name='firstorder'>Result files (first order: no variance)</a></h4>\n");
9644: 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",
9645: jprev1, mprev1,anprev1,jprev2, mprev2,anprev2,subdirfext3(optionfilefiname,"PHTMFR_",".htm"),subdirfext3(optionfilefiname,"PHTMFR_",".htm"));
9646: 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) ",
9647: jprev1, mprev1,anprev1,jprev2, mprev2,anprev2,subdirfext3(optionfilefiname,"PHTM_",".htm"),subdirfext3(optionfilefiname,"PHTM_",".htm"));
9648: fprintf(fichtm,", <a href=\"%s\">%s</a> (text file) <br>\n",subdirf2(fileresu,"P_"),subdirf2(fileresu,"P_"));
9649: fprintf(fichtm,"\
9650: - Estimated transition probabilities over %d (stepm) months: <a href=\"%s\">%s</a><br>\n ",
9651: stepm,subdirf2(fileresu,"PIJ_"),subdirf2(fileresu,"PIJ_"));
9652: fprintf(fichtm,"\
9653: - Estimated back transition probabilities over %d (stepm) months: <a href=\"%s\">%s</a><br>\n ",
9654: stepm,subdirf2(fileresu,"PIJB_"),subdirf2(fileresu,"PIJB_"));
9655: fprintf(fichtm,"\
9656: - Period (forward) prevalence in each health state: <a href=\"%s\">%s</a> <br>\n",
9657: subdirf2(fileresu,"PL_"),subdirf2(fileresu,"PL_"));
9658: fprintf(fichtm,"\
9659: - Backward prevalence in each health state: <a href=\"%s\">%s</a> <br>\n",
9660: subdirf2(fileresu,"PLB_"),subdirf2(fileresu,"PLB_"));
9661: fprintf(fichtm,"\
9662: - (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): \
9663: <a href=\"%s\">%s</a> <br>\n",
9664: estepm,subdirf2(fileresu,"E_"),subdirf2(fileresu,"E_"));
9665: if(prevfcast==1){
9666: fprintf(fichtm,"\
9667: - Prevalence projections by age and states: \
9668: <a href=\"%s\">%s</a> <br>\n</li>", subdirf2(fileresu,"F_"),subdirf2(fileresu,"F_"));
9669: }
9670:
9671:
9672: m=pow(2,cptcoveff);
9673: if (cptcovn < 1) {m=1;ncodemax[1]=1;}
9674:
9675: fprintf(fichtm," \n<ul><li><b>Graphs (first order)</b></li><p>");
9676:
9677: jj1=0;
9678:
9679: fprintf(fichtm," \n<ul>");
9680: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
9681: /* k1=nres; */
9682: k1=TKresult[nres];
9683: if(TKresult[nres]==0)k1=1; /* To be checked for no result */
9684: /* for(k1=1; k1<=m;k1++){ /\* For each combination of covariate *\/ */
9685: /* if(m != 1 && TKresult[nres]!= k1) */
9686: /* continue; */
9687: jj1++;
9688: if (cptcovn > 0) {
9689: fprintf(fichtm,"\n<li><a size=\"1\" color=\"#EC5E5E\" href=\"#rescov");
9690: for (cpt=1; cpt<=cptcovs;cpt++){ /**< cptcovs number of SIMPLE covariates in the model V2+V1 =2 (dummy or quantit or time varying) */
9691: fprintf(fichtm,"_V%d=%lg_",Tvresult[nres][cpt],TinvDoQresult[nres][Tvresult[nres][cpt]]);
9692: }
9693: /* for (cpt=1; cpt<=cptcoveff;cpt++){ */
9694: /* fprintf(fichtm,"_V%d=%d_",Tvresult[nres][cpt],(int)Tresult[nres][cpt]); */
9695: /* } */
9696: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
9697: /* fprintf(fichtm,"_V%d=%f_",Tvqresult[nres][k4],Tqresult[nres][k4]); */
9698: /* } */
9699: fprintf(fichtm,"\">");
9700:
9701: /* if(nqfveff+nqtveff 0) */ /* Test to be done */
9702: fprintf(fichtm,"************ Results for covariates");
9703: for (cpt=1; cpt<=cptcovs;cpt++){
9704: fprintf(fichtm," V%d=%lg ",Tvresult[nres][cpt],TinvDoQresult[nres][Tvresult[nres][cpt]]);
9705: }
9706: /* fprintf(fichtm,"************ Results for covariates"); */
9707: /* for (cpt=1; cpt<=cptcoveff;cpt++){ */
9708: /* fprintf(fichtm," V%d=%d ",Tvresult[nres][cpt],(int)Tresult[nres][cpt]); */
9709: /* } */
9710: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
9711: /* fprintf(fichtm," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); */
9712: /* } */
9713: if(invalidvarcomb[k1]){
9714: fprintf(fichtm," Warning Combination (%d) ignored because no cases ",k1);
9715: continue;
9716: }
9717: fprintf(fichtm,"</a></li>");
9718: } /* cptcovn >0 */
9719: }
9720: fprintf(fichtm," \n</ul>");
9721:
9722: jj1=0;
9723:
9724: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
9725: /* k1=nres; */
9726: k1=TKresult[nres];
9727: if(TKresult[nres]==0) k1=1; /* To be checked for noresult */
9728: /* for(k1=1; k1<=m;k1++){ /\* For each combination of covariate *\/ */
9729: /* if(m != 1 && TKresult[nres]!= k1) */
9730: /* continue; */
9731:
9732: /* for(i1=1; i1<=ncodemax[k1];i1++){ */
9733: jj1++;
9734: if (cptcovn > 0) {
9735: fprintf(fichtm,"\n<p><a name=\"rescov");
9736: for (cpt=1; cpt<=cptcovs;cpt++){
9737: fprintf(fichtm,"_V%d=%lg_",Tvresult[nres][cpt],TinvDoQresult[nres][Tvresult[nres][cpt]]);
9738: }
9739: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
9740: /* fprintf(fichtm,"_V%d=%f_",Tvqresult[nres][k4],Tqresult[nres][k4]); */
9741: /* } */
9742: fprintf(fichtm,"\"</a>");
9743:
9744: fprintf(fichtm,"<hr size=\"2\" color=\"#EC5E5E\">************ Results for covariates");
9745: for (cpt=1; cpt<=cptcovs;cpt++){
9746: fprintf(fichtm," V%d=%lg ",Tvresult[nres][cpt],TinvDoQresult[nres][Tvresult[nres][cpt]]);
9747: printf(" V%d=%lg ",Tvresult[nres][cpt],TinvDoQresult[nres][Tvresult[nres][cpt]]);
9748: /* fprintf(fichtm," V%d=%d ",Tvaraff[cpt],nbcode[Tvaraff[cpt]][codtabm(jj1,cpt)]); */
9749: /* printf(" V%d=%d ",Tvaraff[cpt],nbcode[Tvaraff[cpt]][codtabm(jj1,cpt)]);fflush(stdout); */
9750: }
9751: /* if(nqfveff+nqtveff 0) */ /* Test to be done */
9752: fprintf(fichtm," (model=1+age+%s) ************\n<hr size=\"2\" color=\"#EC5E5E\">",model);
9753: if(invalidvarcomb[k1]){
9754: fprintf(fichtm,"\n<h3>Combination (%d) ignored because no cases </h3>\n",k1);
9755: printf("\nCombination (%d) ignored because no cases \n",k1);
9756: continue;
9757: }
9758: }
9759: /* aij, bij */
9760: 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> \
9761: <img src=\"%s_%d-1-%d.svg\">",model,subdirf2(optionfilefiname,"PE_"),k1,nres,subdirf2(optionfilefiname,"PE_"),k1,nres,subdirf2(optionfilefiname,"PE_"),k1,nres);
9762: /* Pij */
9763: 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> \
9764: <img src=\"%s_%d-2-%d.svg\">",stepm,subdirf2(optionfilefiname,"PE_"),k1,nres,subdirf2(optionfilefiname,"PE_"),k1,nres,subdirf2(optionfilefiname,"PE_"),k1,nres);
9765: /* Quasi-incidences */
9766: fprintf(fichtm,"<br>\n- I<sub>ij</sub> or Conditional probabilities to be observed in state j being in state i %d (stepm) months\
9767: before but expressed in per year i.e. quasi incidences if stepm is small and probabilities too, \
9768: incidence (rates) are the limit when h tends to zero of the ratio of the probability <sub>h</sub>P<sub>ij</sub> \
9769: 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> \
9770: <img src=\"%s_%d-3-%d.svg\">",stepm,subdirf2(optionfilefiname,"PE_"),k1,nres,subdirf2(optionfilefiname,"PE_"),k1,nres,subdirf2(optionfilefiname,"PE_"),k1,nres);
9771: /* Survival functions (period) in state j */
9772: for(cpt=1; cpt<=nlstate;cpt++){
9773: 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. Mean times spent in state (or Life Expectancy or Health Expectancy etc.) are the areas under each curve. <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);
9774: fprintf(fichtm," (data from text file <a href=\"%s.txt\">%s.txt</a>)\n<br>",subdirf2(optionfilefiname,"PIJ_"),subdirf2(optionfilefiname,"PIJ_"));
9775: fprintf(fichtm,"<img src=\"%s_%d-%d-%d.svg\">",subdirf2(optionfilefiname,"LIJ_"),cpt,k1,nres);
9776: }
9777: /* State specific survival functions (period) */
9778: for(cpt=1; cpt<=nlstate;cpt++){
9779: fprintf(fichtm,"<br>\n- Survival functions in state %d and in any other live state (total).\
9780: And probability to be observed in various states (up to %d) being in state %d at different ages. Mean times spent in state (or Life Expectancy or Health Expectancy etc.) are the areas under each curve. \
9781: <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);
9782: fprintf(fichtm," (data from text file <a href=\"%s.txt\">%s.txt</a>)\n<br>",subdirf2(optionfilefiname,"PIJ_"),subdirf2(optionfilefiname,"PIJ_"));
9783: fprintf(fichtm,"<img src=\"%s_%d-%d-%d.svg\">",subdirf2(optionfilefiname,"LIJT_"),cpt,k1,nres);
9784: }
9785: /* Period (forward stable) prevalence in each health state */
9786: for(cpt=1; cpt<=nlstate;cpt++){
9787: 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 alive 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);
9788: fprintf(fichtm," (data from text file <a href=\"%s.txt\">%s.txt</a>)\n<br>",subdirf2(optionfilefiname,"PIJ_"),subdirf2(optionfilefiname,"PIJ_"));
9789: fprintf(fichtm,"<img src=\"%s_%d-%d-%d.svg\">" ,subdirf2(optionfilefiname,"P_"),cpt,k1,nres);
9790: }
9791: if(prevbcast==1){
9792: /* Backward prevalence in each health state */
9793: for(cpt=1; cpt<=nlstate;cpt++){
9794: 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);
9795: fprintf(fichtm," (data from text file <a href=\"%s.txt\">%s.txt</a>)\n<br>",subdirf2(optionfilefiname,"PIJB_"),subdirf2(optionfilefiname,"PIJB_"));
9796: fprintf(fichtm,"<img src=\"%s_%d-%d-%d.svg\">" ,subdirf2(optionfilefiname,"PB_"),cpt,k1,nres);
9797: }
9798: }
9799: if(prevfcast==1){
9800: /* Projection of prevalence up to period (forward stable) prevalence in each health state */
9801: for(cpt=1; cpt<=nlstate;cpt++){
9802: 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);
9803: fprintf(fichtm," (data from text file <a href=\"%s.txt\">%s.txt</a>)\n<br>",subdirf2(optionfilefiname,"F_"),subdirf2(optionfilefiname,"F_"));
9804: fprintf(fichtm,"<img src=\"%s_%d-%d-%d.svg\">",
9805: subdirf2(optionfilefiname,"PROJ_"),cpt,k1,nres);
9806: }
9807: }
9808: if(prevbcast==1){
9809: /* Back projection of prevalence up to stable (mixed) back-prevalence in each health state */
9810: for(cpt=1; cpt<=nlstate;cpt++){
9811: fprintf(fichtm,"<br>\n- Back projection of cross-sectional prevalence (estimated with cases observed from %.1f to %.1f and mobil_average=%d), \
9812: 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 \
9813: 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) \
9814: 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);
9815: fprintf(fichtm," (data from text file <a href=\"%s.txt\">%s.txt</a>)\n<br>",subdirf2(optionfilefiname,"FB_"),subdirf2(optionfilefiname,"FB_"));
9816: fprintf(fichtm," <img src=\"%s_%d-%d-%d.svg\">", subdirf2(optionfilefiname,"PROJB_"),cpt,k1,nres);
9817: }
9818: }
9819:
9820: for(cpt=1; cpt<=nlstate;cpt++) {
9821: 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);
9822: fprintf(fichtm," (data from text file <a href=\"%s.txt\"> %s.txt</a>)\n<br>",subdirf2(optionfilefiname,"E_"),subdirf2(optionfilefiname,"E_"));
9823: fprintf(fichtm,"<img src=\"%s_%d-%d-%d.svg\">", subdirf2(optionfilefiname,"EXP_"),cpt,k1,nres );
9824: }
9825: /* } /\* end i1 *\/ */
9826: }/* End k1=nres */
9827: fprintf(fichtm,"</ul>");
9828:
9829: fprintf(fichtm,"\
9830: \n<br><li><h4> <a name='secondorder'>Result files (second order: variances)</a></h4>\n\
9831: - Parameter file with estimated parameters and covariance matrix: <a href=\"%s\">%s</a> <br> \
9832: - 95%% confidence intervals and Wald tests of the estimated parameters are in the log file if optimization has been done (mle != 0).<br> \
9833: But because parameters are usually highly correlated (a higher incidence of disability \
9834: and a higher incidence of recovery can give very close observed transition) it might \
9835: be very useful to look not only at linear confidence intervals estimated from the \
9836: variances but at the covariance matrix. And instead of looking at the estimated coefficients \
9837: (parameters) of the logistic regression, it might be more meaningful to visualize the \
9838: covariance matrix of the one-step probabilities. \
9839: See page 'Matrix of variance-covariance of one-step probabilities' below. \n", rfileres,rfileres);
9840:
9841: fprintf(fichtm," - Standard deviation of one-step probabilities: <a href=\"%s\">%s</a> <br>\n",
9842: subdirf2(fileresu,"PROB_"),subdirf2(fileresu,"PROB_"));
9843: fprintf(fichtm,"\
9844: - Variance-covariance of one-step probabilities: <a href=\"%s\">%s</a> <br>\n",
9845: subdirf2(fileresu,"PROBCOV_"),subdirf2(fileresu,"PROBCOV_"));
9846:
9847: fprintf(fichtm,"\
9848: - Correlation matrix of one-step probabilities: <a href=\"%s\">%s</a> <br>\n",
9849: subdirf2(fileresu,"PROBCOR_"),subdirf2(fileresu,"PROBCOR_"));
9850: fprintf(fichtm,"\
9851: - 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): \
9852: <a href=\"%s\">%s</a> <br>\n</li>",
9853: estepm,subdirf2(fileresu,"CVE_"),subdirf2(fileresu,"CVE_"));
9854: fprintf(fichtm,"\
9855: - (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): \
9856: <a href=\"%s\">%s</a> <br>\n</li>",
9857: estepm,subdirf2(fileresu,"STDE_"),subdirf2(fileresu,"STDE_"));
9858: fprintf(fichtm,"\
9859: - 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",
9860: estepm, subdirf2(fileresu,"V_"),subdirf2(fileresu,"V_"));
9861: fprintf(fichtm,"\
9862: - 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",
9863: estepm, subdirf2(fileresu,"T_"),subdirf2(fileresu,"T_"));
9864: fprintf(fichtm,"\
9865: - Standard deviation of forward (period) prevalences: <a href=\"%s\">%s</a> <br>\n",\
9866: subdirf2(fileresu,"VPL_"),subdirf2(fileresu,"VPL_"));
9867:
9868: /* if(popforecast==1) fprintf(fichtm,"\n */
9869: /* - Prevalences forecasting: <a href=\"f%s\">f%s</a> <br>\n */
9870: /* - Population forecasting (if popforecast=1): <a href=\"pop%s\">pop%s</a> <br>\n */
9871: /* <br>",fileres,fileres,fileres,fileres); */
9872: /* else */
9873: /* 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); */
9874: fflush(fichtm);
9875:
9876: m=pow(2,cptcoveff);
9877: if (cptcovn < 1) {m=1;ncodemax[1]=1;}
9878:
9879: fprintf(fichtm," <ul><li><b>Graphs (second order)</b></li><p>");
9880:
9881: jj1=0;
9882:
9883: fprintf(fichtm," \n<ul>");
9884: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
9885: /* k1=nres; */
9886: k1=TKresult[nres];
9887: /* for(k1=1; k1<=m;k1++){ /\* For each combination of covariate *\/ */
9888: /* if(m != 1 && TKresult[nres]!= k1) */
9889: /* continue; */
9890: jj1++;
9891: if (cptcovn > 0) {
9892: fprintf(fichtm,"\n<li><a size=\"1\" color=\"#EC5E5E\" href=\"#rescovsecond");
9893: for (cpt=1; cpt<=cptcovs;cpt++){
9894: fprintf(fichtm,"_V%d=%lg_",Tvresult[nres][cpt],TinvDoQresult[nres][Tvresult[nres][cpt]]);
9895: }
9896: fprintf(fichtm,"\">");
9897:
9898: /* if(nqfveff+nqtveff 0) */ /* Test to be done */
9899: fprintf(fichtm,"************ Results for covariates");
9900: for (cpt=1; cpt<=cptcovs;cpt++){
9901: fprintf(fichtm," V%d=%lg ",Tvresult[nres][cpt],TinvDoQresult[nres][Tvresult[nres][cpt]]);
9902: }
9903: if(invalidvarcomb[k1]){
9904: fprintf(fichtm," Warning Combination (%d) ignored because no cases ",k1);
9905: continue;
9906: }
9907: fprintf(fichtm,"</a></li>");
9908: } /* cptcovn >0 */
9909: } /* End nres */
9910: fprintf(fichtm," \n</ul>");
9911:
9912: jj1=0;
9913:
9914: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
9915: /* k1=nres; */
9916: k1=TKresult[nres];
9917: if(TKresult[nres]==0) k1=1; /* To be checked for noresult */
9918: /* for(k1=1; k1<=m;k1++){ */
9919: /* if(m != 1 && TKresult[nres]!= k1) */
9920: /* continue; */
9921: /* for(i1=1; i1<=ncodemax[k1];i1++){ */
9922: jj1++;
9923: if (cptcovn > 0) {
9924: fprintf(fichtm,"\n<p><a name=\"rescovsecond");
9925: for (cpt=1; cpt<=cptcovs;cpt++){
9926: fprintf(fichtm,"_V%d=%lg_",Tvresult[nres][cpt],TinvDoQresult[nres][Tvresult[nres][cpt]]);
9927: }
9928: fprintf(fichtm,"\"</a>");
9929:
9930: fprintf(fichtm,"<hr size=\"2\" color=\"#EC5E5E\">************ Results for covariates");
9931: for (cpt=1; cpt<=cptcovs;cpt++){ /**< cptcoveff number of variables */
9932: fprintf(fichtm," V%d=%lg ",Tvresult[nres][cpt],TinvDoQresult[nres][Tvresult[nres][cpt]]);
9933: printf(" V%d=%lg ",Tvresult[nres][cpt],TinvDoQresult[nres][Tvresult[nres][cpt]]);
9934: /* fprintf(fichtm," V%d=%d ",Tvaraff[cpt],nbcode[Tvaraff[cpt]][codtabm(jj1,cpt)]); */
9935: }
9936:
9937: fprintf(fichtm," (model=1+age+%s) ************\n<hr size=\"2\" color=\"#EC5E5E\">",model);
9938:
9939: if(invalidvarcomb[k1]){
9940: fprintf(fichtm,"\n<h4>Combination (%d) ignored because no cases </h4>\n",k1);
9941: continue;
9942: }
9943: } /* If cptcovn >0 */
9944: for(cpt=1; cpt<=nlstate;cpt++) {
9945: fprintf(fichtm,"\n<br>- Observed (cross-sectional with mov_average=%d) and period (incidence based) \
9946: 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);
9947: fprintf(fichtm," (data from text file <a href=\"%s\">%s</a>)\n <br>",subdirf2(fileresu,"VPL_"),subdirf2(fileresu,"VPL_"));
9948: fprintf(fichtm,"<img src=\"%s_%d-%d-%d.svg\">",subdirf2(optionfilefiname,"V_"), cpt,k1,nres);
9949: }
9950: fprintf(fichtm,"\n<br>- Total life expectancy by age and \
9951: health expectancies in each live state (1 to %d) with confidence intervals \
9952: on left y-scale as well as proportions of time spent in each live state \
9953: (with confidence intervals) on right y-scale 0 to 100%%.\
9954: If popbased=1 the smooth (due to the model) \
9955: true period expectancies (those weighted with period prevalences are also\
9956: drawn in addition to the population based expectancies computed using\
9957: 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);
9958: fprintf(fichtm," (data from text file <a href=\"%s.txt\">%s.txt</a>) \n<br>",subdirf2(optionfilefiname,"T_"),subdirf2(optionfilefiname,"T_"));
9959: fprintf(fichtm,"<img src=\"%s_%d-%d.svg\">",subdirf2(optionfilefiname,"E_"),k1,nres);
9960: /* } /\* end i1 *\/ */
9961: }/* End nres */
9962: fprintf(fichtm,"</ul>");
9963: fflush(fichtm);
9964: }
9965:
9966: /******************* Gnuplot file **************/
9967: 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){
9968:
9969: char dirfileres[256],optfileres[256];
9970: char gplotcondition[256], gplotlabel[256];
9971: int cpt=0,k1=0,i=0,k=0,j=0,jk=0,k2=0,k3=0,k4=0,kf=0,kvar=0,kk=0,ipos=0,iposold=0,ij=0, ijp=0, l=0;
9972: int lv=0, vlv=0, kl=0;
9973: int ng=0;
9974: int vpopbased;
9975: int ioffset; /* variable offset for columns */
9976: int iyearc=1; /* variable column for year of projection */
9977: int iagec=1; /* variable column for age of projection */
9978: int nres=0; /* Index of resultline */
9979: int istart=1; /* For starting graphs in projections */
9980:
9981: /* if((ficgp=fopen(optionfilegnuplot,"a"))==NULL) { */
9982: /* printf("Problem with file %s",optionfilegnuplot); */
9983: /* fprintf(ficlog,"Problem with file %s",optionfilegnuplot); */
9984: /* } */
9985:
9986: /*#ifdef windows */
9987: fprintf(ficgp,"cd \"%s\" \n",pathc);
9988: /*#endif */
9989: m=pow(2,cptcoveff);
9990:
9991: /* diagram of the model */
9992: fprintf(ficgp,"\n#Diagram of the model \n");
9993: fprintf(ficgp,"\ndelta=0.03;delta2=0.07;unset arrow;\n");
9994: fprintf(ficgp,"yoff=(%d > 2? 0:1);\n",nlstate);
9995: 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);
9996:
9997: fprintf(ficgp,"\n#Centripete arrows (turning in other direction (1-i) instead of (i-1)) \nset for [i=1:%d] for [j=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,nlstate);
9998: fprintf(ficgp,"\n#show arrow\nunset label\n");
9999: 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);
10000: fprintf(ficgp,"\nset label %d+1 sprintf(\"State %%d\",%d+1) center at 0.,0. font \"helvetica, 16\" tc rgbcolor \"red\"\n",nlstate,nlstate);
10001: fprintf(ficgp,"\n#show label\nunset border;unset xtics; unset ytics;\n");
10002: fprintf(ficgp,"\n\nset ter svg size 640, 480;set out \"%s_.svg\" \n",subdirf2(optionfilefiname,"D_"));
10003: fprintf(ficgp,"unset log y; plot [-1.2:1.2][yoff-1.2:1.2] 1/0 not; set out;reset;\n");
10004:
10005: /* Contribution to likelihood */
10006: /* Plot the probability implied in the likelihood */
10007: fprintf(ficgp,"\n# Contributions to the Likelihood, mle >=1. For mle=4 no interpolation, pure matrix products.\n#\n");
10008: fprintf(ficgp,"\n set log y; unset log x;set xlabel \"Age\"; set ylabel \"Likelihood (-2Log(L))\";");
10009: /* fprintf(ficgp,"\nset ter svg size 640, 480"); */ /* Too big for svg */
10010: fprintf(ficgp,"\nset ter pngcairo size 640, 480");
10011: /* nice for mle=4 plot by number of matrix products.
10012: replot "rrtest1/toto.txt" u 2:($4 == 1 && $5==2 ? $9 : 1/0):5 t "p12" with point lc 1 */
10013: /* replot exp(p1+p2*x)/(1+exp(p1+p2*x)+exp(p3+p4*x)+exp(p5+p6*x)) t "p12(x)" */
10014: /* fprintf(ficgp,"\nset out \"%s.svg\";",subdirf2(optionfilefiname,"ILK_")); */
10015: fprintf(ficgp,"\nset out \"%s-dest.png\";",subdirf2(optionfilefiname,"ILK_"));
10016: 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));
10017: fprintf(ficgp,"\nset out \"%s-ori.png\";",subdirf2(optionfilefiname,"ILK_"));
10018: 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));
10019: for (i=1; i<= nlstate ; i ++) {
10020: fprintf(ficgp,"\nset out \"%s-p%dj.png\";set ylabel \"Probability for each individual/wave\";",subdirf2(optionfilefiname,"ILK_"),i);
10021: fprintf(ficgp,"unset log;\n# plot weighted, mean weight should have point size of 0.5\n plot \"%s\"",subdirf(fileresilk));
10022: 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);
10023: for (j=2; j<= nlstate+ndeath ; j ++) {
10024: 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);
10025: }
10026: fprintf(ficgp,";\nset out; unset ylabel;\n");
10027: }
10028: /* 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 */
10029: /* fprintf(ficgp,"\nset log y;plot \"%s\" u 2:(-$11):3 t \"All sample, all transitions\" with dots lc variable",subdirf(fileresilk)); */
10030: /* fprintf(ficgp,"\nreplot \"%s\" u 2:($3 <= 3 ? -$11 : 1/0):3 t \"First 3 individuals\" with line lc variable", subdirf(fileresilk)); */
10031: fprintf(ficgp,"\nset out;unset log\n");
10032: /* fprintf(ficgp,"\nset out \"%s.svg\"; replot; set out; # bug gnuplot",subdirf2(optionfilefiname,"ILK_")); */
10033:
10034: /* Plot the probability implied in the likelihood by covariate value */
10035: fprintf(ficgp,"\nset ter pngcairo size 640, 480");
10036: /* if(debugILK==1){ */
10037: for(kf=1; kf <= ncovf; kf++){ /* For each simple dummy covariate of the model */
10038: kvar=Tvar[TvarFind[kf]]; /* variable name */
10039: /* k=18+Tvar[TvarFind[kf]];/\*offset because there are 18 columns in the ILK_ file but could be placed else where *\/ */
10040: /* k=18+kf;/\*offset because there are 18 columns in the ILK_ file *\/ */
10041: /* k=19+kf;/\*offset because there are 19 columns in the ILK_ file *\/ */
10042: k=16+nlstate+kf;/*offset because there are 19 columns in the ILK_ file, first cov Vn on col 21 with 4 living states */
10043: for (i=1; i<= nlstate ; i ++) {
10044: fprintf(ficgp,"\nset out \"%s-p%dj-%d.png\";set ylabel \"Probability for each individual/wave\";",subdirf2(optionfilefiname,"ILK_"),i,kvar);
10045: fprintf(ficgp,"unset log;\n# For each simple dummy covariate of the model \n plot \"%s\"",subdirf(fileresilk));
10046: if(gnuplotversion >=5.2){ /* Former gnuplot versions do not have variable pointsize!! */
10047: fprintf(ficgp," u 2:($5 == %d && $6==%d ? $10 : 1/0):($%d==0 ? 7 : 9):($%d==0 ? $6 : $6+4) t \"p%d%d V%d\" with points pt variable ps 0.4 lc variable \\\n",i,1,k,k,i,1,kvar);
10048: for (j=2; j<= nlstate+ndeath ; j ++) {
10049: fprintf(ficgp,",\\\n \"\" u 2:($5 == %d && $6==%d ? $10 : 1/0):($%d==0 ? 7 : 9):($%d==0 ? $6 : $6+4) t \"p%d%d V%d\" with points pt variable ps 0.4 lc variable ",i,j,k,k,i,j,kvar);
10050: }
10051: }else{
10052: fprintf(ficgp," u 2:($5 == %d && $6==%d ? $10 : 1/0):($%d==0 ? $6 : $6+4) t \"p%d%d V%d\" with points pt 7 ps 0.4 lc variable \\\n",i,1,k,i,1,kvar);
10053: for (j=2; j<= nlstate+ndeath ; j ++) {
10054: fprintf(ficgp,",\\\n \"\" u 2:($5 == %d && $6==%d ? $10 : 1/0):($%d==0 ? $6 : $6+4) t \"p%d%d V%d\" with points pt 7 ps 0.4 lc variable ",i,j,k,i,j,kvar);
10055: }
10056: }
10057: fprintf(ficgp,";\nset out; unset ylabel;\n");
10058: }
10059: } /* End of each covariate dummy */
10060: for(ncovv=1, iposold=0, kk=0; ncovv <= ncovvt ; ncovv++){
10061: /* Other example V1 + V3 + V5 + age*V1 + age*V3 + age*V5 + V1*V3 + V3*V5 + V1*V5
10062: * kmodel = 1 2 3 4 5 6 7 8 9
10063: * varying 1 2 3 4 5
10064: * ncovv 1 2 3 4 5 6 7 8
10065: * TvarVV[ncovv] V3 5 1 3 3 5 1 5
10066: * TvarVVind[ncovv]=kmodel 2 3 7 7 8 8 9 9
10067: * TvarFind[kmodel] 1 0 0 0 0 0 0 0 0
10068: * kdata ncovcol=[V1 V2] nqv=0 ntv=[V3 V4] nqtv=V5
10069: * Dummy[kmodel] 0 0 1 2 2 3 1 1 1
10070: */
10071: ipos=TvarVVind[ncovv]; /* TvarVVind={2, 5, 5] gives the position in the model of the ncovv th varying covariate */
10072: kvar=TvarVV[ncovv]; /* TvarVV={3, 1, 3} gives the name of each varying covariate */
10073: /* printf("DebugILK ficgp ncovv=%d, kvar=TvarVV[ncovv]=%d, ipos=TvarVVind[ncovv]=%d, Dummy[ipos]=%d, Typevar[ipos]=%d\n", ncovv,kvar,ipos,Dummy[ipos],Typevar[ipos]); */
10074: if(ipos!=iposold){ /* Not a product or first of a product */
10075: /* printf(" %d",ipos); */
10076: /* fprintf(ficresilk," V%d",TvarVV[ncovv]); */
10077: /* printf(" DebugILK ficgp suite ipos=%d != iposold=%d\n", ipos, iposold); */
10078: kk++; /* Position of the ncovv column in ILK_ */
10079: k=18+ncovf+kk; /*offset because there are 18 columns in the ILK_ file plus ncovf fixed covariate */
10080: if(Dummy[ipos]==0 && Typevar[ipos]==0){ /* Only if dummy time varying: Dummy(0, 1=quant singor prod without age,2 dummy*age, 3quant*age) Typevar (0 single, 1=*age,2=Vn*vm) */
10081: for (i=1; i<= nlstate ; i ++) {
10082: fprintf(ficgp,"\nset out \"%s-p%dj-%d.png\";set ylabel \"Probability for each individual/wave\";",subdirf2(optionfilefiname,"ILK_"),i,kvar);
10083: fprintf(ficgp,"unset log;\n# For each simple dummy covariate of the model \n plot \"%s\"",subdirf(fileresilk));
10084:
10085: /* printf("Before DebugILK gnuplotversion=%g >=5.2\n",gnuplotversion); */
10086: if(gnuplotversion >=5.2){ /* Former gnuplot versions do not have variable pointsize!! */
10087: /* printf("DebugILK gnuplotversion=%g >=5.2\n",gnuplotversion); */
10088: fprintf(ficgp," u 2:($5 == %d && $6==%d ? $10 : 1/0):($%d==0 ? 7 : 9):($%d==0 ? $6 : $6+4) t \"p%d%d V%d\" with points pt variable ps 0.4 lc variable \\\n",i,1,k,k,i,1,kvar);
10089: for (j=2; j<= nlstate+ndeath ; j ++) {
10090: fprintf(ficgp,",\\\n \"\" u 2:($5 == %d && $6==%d ? $10 : 1/0):($%d==0 ? 7 : 9):($%d==0 ? $6 : $6+4) t \"p%d%d V%d\" with points pt variable ps 0.4 lc variable ",i,j,k,k,i,j,kvar);
10091: }
10092: }else{
10093: /* printf("DebugILK gnuplotversion=%g <5.2\n",gnuplotversion); */
10094: fprintf(ficgp," u 2:($5 == %d && $6==%d ? $10 : 1/0):($%d==0 ? $6 : $6+4) t \"p%d%d V%d\" with points pt 7 ps 0.4 lc variable \\\n",i,1,k,i,1,kvar);
10095: for (j=2; j<= nlstate+ndeath ; j ++) {
10096: fprintf(ficgp,",\\\n \"\" u 2:($5 == %d && $6==%d ? $10 : 1/0):($%d==0 ? $6 : $6+4) t \"p%d%d V%d\" with points pt 7 ps 0.4 lc variable ",i,j,k,i,j,kvar);
10097: }
10098: }
10099: fprintf(ficgp,";\nset out; unset ylabel;\n");
10100: }
10101: }/* End if dummy varying */
10102: }else{ /*Product */
10103: /* printf("*"); */
10104: /* fprintf(ficresilk,"*"); */
10105: }
10106: iposold=ipos;
10107: } /* For each time varying covariate */
10108: /* } /\* debugILK==1 *\/ */
10109: /* 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 */
10110: /* fprintf(ficgp,"\nset log y;plot \"%s\" u 2:(-$11):3 t \"All sample, all transitions\" with dots lc variable",subdirf(fileresilk)); */
10111: /* fprintf(ficgp,"\nreplot \"%s\" u 2:($3 <= 3 ? -$11 : 1/0):3 t \"First 3 individuals\" with line lc variable", subdirf(fileresilk)); */
10112: fprintf(ficgp,"\nset out;unset log\n");
10113: /* fprintf(ficgp,"\nset out \"%s.svg\"; replot; set out; # bug gnuplot",subdirf2(optionfilefiname,"ILK_")); */
10114:
10115:
10116:
10117: strcpy(dirfileres,optionfilefiname);
10118: strcpy(optfileres,"vpl");
10119: /* 1eme*/
10120: for (cpt=1; cpt<= nlstate ; cpt ++){ /* For each live state */
10121: /* for (k1=1; k1<= m ; k1 ++){ /\* For each valid combination of covariate *\/ */
10122: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
10123: k1=TKresult[nres];
10124: if(TKresult[nres]==0) k1=1; /* To be checked for noresult */
10125: /* plot [100000000000000000000:-100000000000000000000] "mysbiaspar/vplrmysbiaspar.txt to check */
10126: /* if(m != 1 && TKresult[nres]!= k1) */
10127: /* continue; */
10128: /* We are interested in selected combination by the resultline */
10129: /* printf("\n# 1st: Period (stable) prevalence with CI: 'VPL_' files and live state =%d ", cpt); */
10130: fprintf(ficgp,"\n# 1st: Forward (stable period) prevalence with CI: 'VPL_' files and live state =%d ", cpt);
10131: strcpy(gplotlabel,"(");
10132: for (k=1; k<=cptcovs; k++){ /* For each covariate k get corresponding value lv for combination k1 */
10133: fprintf(ficgp," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
10134: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
10135:
10136: /* for (k=1; k<=cptcoveff; k++){ /\* For each covariate k get corresponding value lv for combination k1 *\/ */
10137: /* /\* lv= decodtabm(k1,k,cptcoveff); /\\* Should be the value of the covariate corresponding to k1 combination *\\/ *\/ */
10138: /* lv=codtabm(k1,TnsdVar[Tvaraff[k]]); */
10139: /* /\* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 *\/ */
10140: /* /\* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 *\/ */
10141: /* /\* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 *\/ */
10142: /* vlv= nbcode[Tvaraff[k]][lv]; /\* vlv is the value of the covariate lv, 0 or 1 *\/ */
10143: /* /\* For each combination of covariate k1 (V1=1, V3=0), we printed the current covariate k and its value vlv *\/ */
10144: /* /\* printf(" V%d=%d ",Tvaraff[k],vlv); *\/ */
10145: /* fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv); */
10146: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv); */
10147: /* } */
10148: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
10149: /* /\* printf(" V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); *\/ */
10150: /* fprintf(ficgp," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); */
10151: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); */
10152: }
10153: strcpy(gplotlabel+strlen(gplotlabel),")");
10154: /* printf("\n#\n"); */
10155: fprintf(ficgp,"\n#\n");
10156: if(invalidvarcomb[k1]){
10157: /*k1=k1-1;*/ /* To be checked */
10158: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
10159: continue;
10160: }
10161:
10162: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" \n",subdirf2(optionfilefiname,"V_"),cpt,k1,nres);
10163: fprintf(ficgp,"\n#set out \"V_%s_%d-%d-%d.svg\" \n",optionfilefiname,cpt,k1,nres);
10164: /* fprintf(ficgp,"set label \"Alive state %d %s\" at graph 0.98,0.5 center rotate font \"Helvetica,12\"\n",cpt,gplotlabel); */
10165: fprintf(ficgp,"set title \"Alive state %d %s model=1+age+%s\" font \"Helvetica,12\"\n",cpt,gplotlabel,model);
10166: 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);
10167: /* 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); */
10168: /* k1-1 error should be nres-1*/
10169: for (i=1; i<= nlstate ; i ++) {
10170: if (i==cpt) fprintf(ficgp," %%lf (%%lf)");
10171: else fprintf(ficgp," %%*lf (%%*lf)");
10172: }
10173: 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);
10174: for (i=1; i<= nlstate ; i ++) {
10175: if (i==cpt) fprintf(ficgp," %%lf (%%lf)");
10176: else fprintf(ficgp," %%*lf (%%*lf)");
10177: }
10178: 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);
10179: for (i=1; i<= nlstate ; i ++) {
10180: if (i==cpt) fprintf(ficgp," %%lf (%%lf)");
10181: else fprintf(ficgp," %%*lf (%%*lf)");
10182: }
10183: /* 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)); */
10184:
10185: fprintf(ficgp,"\" t\"\" w l lt 1,\"%s\" u 1:((",subdirf2(fileresu,"P_"));
10186: if(cptcoveff ==0){
10187: fprintf(ficgp,"$%d)) t 'Observed prevalence in state %d' with line lt 3", 2+3*(cpt-1), cpt );
10188: }else{
10189: kl=0;
10190: for (k=1; k<=cptcoveff; k++){ /* For each combination of covariate */
10191: /* lv= decodtabm(k1,k,cptcoveff); /\* Should be the covariate value corresponding to k1 combination and kth covariate *\/ */
10192: lv=codtabm(k1,TnsdVar[Tvaraff[k]]);
10193: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
10194: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
10195: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
10196: vlv= nbcode[Tvaraff[k]][lv];
10197: kl++;
10198: /* 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 *\/ */
10199: /*6+(cpt-1)*(nlstate+1)+1+(i-1)+(nlstate+1)*nlstate; 6+(1-1)*(2+1)+1+(1-1) +(2+1)*2=13 */
10200: /*6+1+(i-1)+(nlstate+1)*nlstate; 6+1+(1-1) +(2+1)*2=13 */
10201: /* '' 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*/
10202: if(k==cptcoveff){
10203: 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], \
10204: 2+cptcoveff*2+3*(cpt-1), cpt ); /* 4 or 6 ?*/
10205: }else{
10206: fprintf(ficgp,"$%d==%d && $%d==%d && ",kl+1, Tvaraff[k],kl+1+1,nbcode[Tvaraff[k]][lv]);
10207: kl++;
10208: }
10209: } /* end covariate */
10210: } /* end if no covariate */
10211:
10212: if(prevbcast==1){ /* We need to get the corresponding values of the covariates involved in this combination k1 */
10213: /* 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); */
10214: fprintf(ficgp,",\"%s\" u 1:((",subdirf2(fileresu,"PLB_")); /* Age is in 1, nres in 2 to be fixed */
10215: if(cptcoveff ==0){
10216: fprintf(ficgp,"$%d)) t 'Backward prevalence in state %d' with line lt 3", 2+(cpt-1), cpt );
10217: }else{
10218: kl=0;
10219: for (k=1; k<=cptcoveff; k++){ /* For each combination of covariate */
10220: /* lv= decodtabm(k1,k,cptcoveff); /\* Should be the covariate value corresponding to k1 combination and kth covariate *\/ */
10221: lv=codtabm(k1,TnsdVar[Tvaraff[k]]);
10222: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
10223: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
10224: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
10225: /* vlv= nbcode[Tvaraff[k]][lv]; */
10226: vlv= nbcode[Tvaraff[k]][codtabm(k1,TnsdVar[Tvaraff[k]])];
10227: kl++;
10228: /* 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 *\/ */
10229: /*6+(cpt-1)*(nlstate+1)+1+(i-1)+(nlstate+1)*nlstate; 6+(1-1)*(2+1)+1+(1-1) +(2+1)*2=13 */
10230: /*6+1+(i-1)+(nlstate+1)*nlstate; 6+1+(1-1) +(2+1)*2=13 */
10231: /* '' 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*/
10232: if(k==cptcoveff){
10233: 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], \
10234: 2+cptcoveff*2+(cpt-1), cpt ); /* 4 or 6 ?*/
10235: }else{
10236: fprintf(ficgp,"$%d==%d && $%d==%d && ",kl+1, Tvaraff[k],kl+1+1,nbcode[Tvaraff[k]][codtabm(k1,TnsdVar[Tvaraff[k]])]);
10237: kl++;
10238: }
10239: } /* end covariate */
10240: } /* end if no covariate */
10241: if(prevbcast == 1){
10242: fprintf(ficgp,", \"%s\" every :::%d::%d u 1:($2==%d ? $3:1/0) \"%%lf %%lf",subdirf2(fileresu,"VBL_"),nres-1,nres-1,nres);
10243: /* k1-1 error should be nres-1*/
10244: for (i=1; i<= nlstate ; i ++) {
10245: if (i==cpt) fprintf(ficgp," %%lf (%%lf)");
10246: else fprintf(ficgp," %%*lf (%%*lf)");
10247: }
10248: 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);
10249: for (i=1; i<= nlstate ; i ++) {
10250: if (i==cpt) fprintf(ficgp," %%lf (%%lf)");
10251: else fprintf(ficgp," %%*lf (%%*lf)");
10252: }
10253: 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);
10254: for (i=1; i<= nlstate ; i ++) {
10255: if (i==cpt) fprintf(ficgp," %%lf (%%lf)");
10256: else fprintf(ficgp," %%*lf (%%*lf)");
10257: }
10258: fprintf(ficgp,"\" t\"\" w l lt 4");
10259: } /* end if backprojcast */
10260: } /* end if prevbcast */
10261: /* fprintf(ficgp,"\nset out ;unset label;\n"); */
10262: fprintf(ficgp,"\nset out ;unset title;\n");
10263: } /* nres */
10264: /* } /\* k1 *\/ */
10265: } /* cpt */
10266:
10267:
10268: /*2 eme*/
10269: /* for (k1=1; k1<= m ; k1 ++){ */
10270: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
10271: k1=TKresult[nres];
10272: if(TKresult[nres]==0) k1=1; /* To be checked for noresult */
10273: /* if(m != 1 && TKresult[nres]!= k1) */
10274: /* continue; */
10275: fprintf(ficgp,"\n# 2nd: Total life expectancy with CI: 't' files ");
10276: strcpy(gplotlabel,"(");
10277: for (k=1; k<=cptcovs; k++){ /* For each covariate k get corresponding value lv for combination k1 */
10278: fprintf(ficgp," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
10279: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
10280: /* for (k=1; k<=cptcoveff; k++){ /\* For each covariate and each value *\/ */
10281: /* /\* lv= decodtabm(k1,k,cptcoveff); /\\* Should be the covariate number corresponding to k1 combination *\\/ *\/ */
10282: /* lv=codtabm(k1,TnsdVar[Tvaraff[k]]); */
10283: /* /\* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 *\/ */
10284: /* /\* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 *\/ */
10285: /* /\* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 *\/ */
10286: /* /\* vlv= nbcode[Tvaraff[k]][lv]; *\/ */
10287: /* vlv= nbcode[Tvaraff[k]][codtabm(k1,TnsdVar[Tvaraff[k]])]; */
10288: /* fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv); */
10289: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv); */
10290: /* } */
10291: /* /\* for(k=1; k <= ncovds; k++){ *\/ */
10292: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
10293: /* printf(" V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); */
10294: /* fprintf(ficgp," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); */
10295: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); */
10296: }
10297: strcpy(gplotlabel+strlen(gplotlabel),")");
10298: fprintf(ficgp,"\n#\n");
10299: if(invalidvarcomb[k1]){
10300: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
10301: continue;
10302: }
10303:
10304: fprintf(ficgp,"\nset out \"%s_%d-%d.svg\" \n",subdirf2(optionfilefiname,"E_"),k1,nres);
10305: for(vpopbased=0; vpopbased <= popbased; vpopbased++){ /* Done for vpopbased=0 and vpopbased=1 if popbased==1*/
10306: fprintf(ficgp,"\nset label \"popbased %d %s\" at graph 0.98,0.5 center rotate font \"Helvetica,12\"\n",vpopbased,gplotlabel);
10307: if(vpopbased==0){
10308: fprintf(ficgp,"set ylabel \"Years\" \nset ter svg size 640, 480\nunset ytics; unset y2tics; set ytics nomirror; set y2tics 0,10,100;set y2range [0:100];\nplot [%.f:%.f] ",ageminpar,fage);
10309: }else
10310: fprintf(ficgp,"\nreplot ");
10311: for (i=1; i<= nlstate+1 ; i ++) { /* For state i-1=0 is LE, while i-1=1 to nlstate are origin state */
10312: k=2*i;
10313: 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); /* for fixed variables age, popbased, mobilav */
10314: for (j=1; j<= nlstate+1 ; j ++) { /* e.. e.1 e.2 again j-1 is the state of end, wlim_i eij*/
10315: if (j==i) fprintf(ficgp," %%lf (%%lf)"); /* We want to read e.. i=1,j=1, e.1 i=2,j=2, e.2 i=3,j=3 */
10316: else fprintf(ficgp," %%*lf (%%*lf)"); /* skipping that field with a star */
10317: }
10318: if (i== 1) fprintf(ficgp,"\" t\"TLE\" w l lt %d, \\\n",i);
10319: else fprintf(ficgp,"\" t\"LE in state (%d)\" w l lt %d, \\\n",i-1,i+1); /* state=i-1=1 to nlstate */
10320: 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);
10321: for (j=1; j<= nlstate+1 ; j ++) {
10322: if (j==i) fprintf(ficgp," %%lf (%%lf)");
10323: else fprintf(ficgp," %%*lf (%%*lf)");
10324: }
10325: fprintf(ficgp,"\" t\"\" w l lt 0,");
10326: 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);
10327: for (j=1; j<= nlstate+1 ; j ++) {
10328: if (j==i) fprintf(ficgp," %%lf (%%lf)");
10329: else fprintf(ficgp," %%*lf (%%*lf)");
10330: }
10331: if (i== (nlstate+1)) fprintf(ficgp,"\" t\"\" w l lt 0,\\\n"); /* ,\\\n added for th percentage graphs */
10332: else fprintf(ficgp,"\" t\"\" w l lt 0,\\\n");
10333: } /* state */
10334: /* again for the percentag spent in state i-1=1 to i-1=nlstate */
10335: for (i=2; i<= nlstate+1 ; i ++) { /* For state i-1=0 is LE, while i-1=1 to nlstate are origin state */
10336: k=2*i;
10337: fprintf(ficgp,"\"%s\" every :::%d::%d u 1:($2==%d && ($4)<=1 && ($4)>=0 ?($4)*100. : 1/0) \"%%lf %%lf %%lf",subdirf2(fileresu,"T_"),nres-1,nres-1, vpopbased); /* for fixed variables age, popbased, mobilav */
10338: for (j=1; j<= nlstate ; j ++)
10339: fprintf(ficgp," %%*lf (%%*lf)"); /* Skipping TLE and LE to read %LE only */
10340: for (j=1; j<= nlstate+1 ; j ++) { /* e.. e.1 e.2 again j-1 is the state of end, wlim_i eij*/
10341: if (j==i) fprintf(ficgp," %%lf (%%lf)"); /* We want to read e.. i=1,j=1, e.1 i=2,j=2, e.2 i=3,j=3 */
10342: else fprintf(ficgp," %%*lf (%%*lf)"); /* skipping that field with a star */
10343: }
10344: if (i== 1) fprintf(ficgp,"\" t\"%%TLE\" w l lt %d axis x1y2, \\\n",i); /* Not used */
10345: else fprintf(ficgp,"\" t\"%%LE in state (%d)\" w l lw 2 lt %d axis x1y2, \\\n",i-1,i+1); /* state=i-1=1 to nlstate */
10346: fprintf(ficgp,"\"%s\" every :::%d::%d u 1:($2==%d && ($4-$5*2)<=1 && ($4-$5*2)>=0? ($4-$5*2)*100. : 1/0) \"%%lf %%lf %%lf",subdirf2(fileresu,"T_"),nres-1,nres-1,vpopbased);
10347: for (j=1; j<= nlstate ; j ++)
10348: fprintf(ficgp," %%*lf (%%*lf)"); /* Skipping TLE and LE to read %LE only */
10349: for (j=1; j<= nlstate+1 ; j ++) {
10350: if (j==i) fprintf(ficgp," %%lf (%%lf)");
10351: else fprintf(ficgp," %%*lf (%%*lf)");
10352: }
10353: fprintf(ficgp,"\" t\"\" w l lt 0 axis x1y2,");
10354: fprintf(ficgp,"\"%s\" every :::%d::%d u 1:($2==%d && ($4+$5*2)<=1 && ($4+$5*2)>=0 ? ($4+$5*2)*100. : 1/0) \"%%lf %%lf %%lf",subdirf2(fileresu,"T_"),nres-1,nres-1,vpopbased);
10355: for (j=1; j<= nlstate ; j ++)
10356: fprintf(ficgp," %%*lf (%%*lf)"); /* Skipping TLE and LE to read %LE only */
10357: for (j=1; j<= nlstate+1 ; j ++) {
10358: if (j==i) fprintf(ficgp," %%lf (%%lf)");
10359: else fprintf(ficgp," %%*lf (%%*lf)");
10360: }
10361: if (i== (nlstate+1)) fprintf(ficgp,"\" t\"\" w l lt 0 axis x1y2");
10362: else fprintf(ficgp,"\" t\"\" w l lt 0 axis x1y2,\\\n");
10363: } /* state for percent */
10364: } /* vpopbased */
10365: fprintf(ficgp,"\nset out;set out \"%s_%d-%d.svg\"; replot; set out; unset label;\n",subdirf2(optionfilefiname,"E_"),k1,nres); /* Buggy gnuplot */
10366: } /* end nres */
10367: /* } /\* k1 end 2 eme*\/ */
10368:
10369:
10370: /*3eme*/
10371: /* for (k1=1; k1<= m ; k1 ++){ */
10372: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
10373: k1=TKresult[nres];
10374: if(TKresult[nres]==0) k1=1; /* To be checked for noresult */
10375: /* if(m != 1 && TKresult[nres]!= k1) */
10376: /* continue; */
10377:
10378: for (cpt=1; cpt<= nlstate ; cpt ++) { /* Fragile no verification of covariate values */
10379: fprintf(ficgp,"\n\n# 3d: Life expectancy with EXP_ files: combination=%d state=%d",k1, cpt);
10380: strcpy(gplotlabel,"(");
10381: for (k=1; k<=cptcovs; k++){ /* For each covariate k get corresponding value lv for combination k1 */
10382: fprintf(ficgp," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
10383: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
10384: /* for (k=1; k<=cptcoveff; k++){ /\* For each covariate and each value *\/ */
10385: /* /\* lv= decodtabm(k1,k,cptcoveff); /\\* Should be the covariate number corresponding to k1 combination *\\/ *\/ */
10386: /* lv= codtabm(k1,TnsdVar[Tvaraff[k]]); /\* Should be the covariate value corresponding to combination k1 and covariate k *\/ */
10387: /* /\* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 *\/ */
10388: /* /\* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 *\/ */
10389: /* /\* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 *\/ */
10390: /* /\* vlv= nbcode[Tvaraff[k]][lv]; *\/ */
10391: /* vlv= nbcode[Tvaraff[k]][codtabm(k1,TnsdVar[Tvaraff[k]])]; */
10392: /* fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv); */
10393: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv); */
10394: /* } */
10395: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
10396: /* fprintf(ficgp," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][resultmodel[nres][k4]]); */
10397: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][resultmodel[nres][k4]]); */
10398: }
10399: strcpy(gplotlabel+strlen(gplotlabel),")");
10400: fprintf(ficgp,"\n#\n");
10401: if(invalidvarcomb[k1]){
10402: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
10403: continue;
10404: }
10405:
10406: /* k=2+nlstate*(2*cpt-2); */
10407: k=2+(nlstate+1)*(cpt-1);
10408: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" \n",subdirf2(optionfilefiname,"EXP_"),cpt,k1,nres);
10409: fprintf(ficgp,"set label \"%s\" at graph 0.98,0.5 center rotate font \"Helvetica,12\"\n",gplotlabel);
10410: fprintf(ficgp,"set ter svg size 640, 480\n\
10411: 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);
10412: /*fprintf(ficgp,",\"e%s\" every :::%d::%d u 1:($%d-2*$%d) \"\%%lf ",fileres,k1-1,k1-1,k,k+1);
10413: for (i=1; i<= nlstate*2 ; i ++) fprintf(ficgp,"\%%lf (\%%lf) ");
10414: fprintf(ficgp,"\" t \"e%d1\" w l",cpt);
10415: fprintf(ficgp,",\"e%s\" every :::%d::%d u 1:($%d+2*$%d) \"\%%lf ",fileres,k1-1,k1-1,k,k+1);
10416: for (i=1; i<= nlstate*2 ; i ++) fprintf(ficgp,"\%%lf (\%%lf) ");
10417: fprintf(ficgp,"\" t \"e%d1\" w l",cpt);
10418:
10419: */
10420: for (i=1; i< nlstate ; i ++) {
10421: 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);
10422: /* 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);*/
10423:
10424: }
10425: fprintf(ficgp," ,\"%s\" every :::%d::%d u 1:%d t \"e%d.\" w l",subdirf2(fileresu,"E_"),nres-1,nres-1,k+nlstate,cpt);
10426: }
10427: fprintf(ficgp,"\nunset label;\n");
10428: } /* end nres */
10429: /* } /\* end kl 3eme *\/ */
10430:
10431: /* 4eme */
10432: /* Survival functions (period) from state i in state j by initial state i */
10433: /* for (k1=1; k1<=m; k1++){ /\* For each covariate and each value *\/ */
10434: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
10435: k1=TKresult[nres];
10436: if(TKresult[nres]==0) k1=1; /* To be checked for noresult */
10437: /* if(m != 1 && TKresult[nres]!= k1) */
10438: /* continue; */
10439: for (cpt=1; cpt<=nlstate ; cpt ++) { /* For each life state cpt*/
10440: strcpy(gplotlabel,"(");
10441: fprintf(ficgp,"\n#\n#\n# Survival functions in state %d : 'LIJ_' files, cov=%d state=%d", cpt, k1, cpt);
10442: for (k=1; k<=cptcovs; k++){ /* For each covariate k get corresponding value lv for combination k1 */
10443: fprintf(ficgp," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
10444: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
10445: /* for (k=1; k<=cptcoveff; k++){ /\* For each covariate and each value *\/ */
10446: /* lv=codtabm(k1,TnsdVar[Tvaraff[k]]); */
10447: /* /\* lv= decodtabm(k1,k,cptcoveff); /\\* Should be the covariate number corresponding to k1 combination *\\/ *\/ */
10448: /* /\* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 *\/ */
10449: /* /\* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 *\/ */
10450: /* /\* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 *\/ */
10451: /* /\* vlv= nbcode[Tvaraff[k]][lv]; *\/ */
10452: /* vlv= nbcode[Tvaraff[k]][codtabm(k1,TnsdVar[Tvaraff[k]])]; */
10453: /* fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv); */
10454: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv); */
10455: /* } */
10456: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
10457: /* fprintf(ficgp," V%d=%f ",Tvqresult[nres][resultmodel[nres][k4]],Tqresult[nres][resultmodel[nres][k4]]); */
10458: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][resultmodel[nres][k4]],Tqresult[nres][resultmodel[nres][k4]]); */
10459: }
10460: strcpy(gplotlabel+strlen(gplotlabel),")");
10461: fprintf(ficgp,"\n#\n");
10462: if(invalidvarcomb[k1]){
10463: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
10464: continue;
10465: }
10466:
10467: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" \n",subdirf2(optionfilefiname,"LIJ_"),cpt,k1,nres);
10468: fprintf(ficgp,"set label \"Alive state %d %s\" at graph 0.98,0.5 center rotate font \"Helvetica,12\"\n",cpt,gplotlabel);
10469: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability to be alive\" \n\
10470: set ter svg size 640, 480\nunset log y\nplot [%.f:%.f] ", ageminpar, agemaxpar);
10471: k=3;
10472: for (i=1; i<= nlstate ; i ++){
10473: if(i==1){
10474: fprintf(ficgp,"\"%s\"",subdirf2(fileresu,"PIJ_"));
10475: }else{
10476: fprintf(ficgp,", '' ");
10477: }
10478: l=(nlstate+ndeath)*(i-1)+1;
10479: fprintf(ficgp," u ($1==%d ? ($3):1/0):($%d/($%d",k1,k+l+(cpt-1),k+l);
10480: for (j=2; j<= nlstate+ndeath ; j ++)
10481: fprintf(ficgp,"+$%d",k+l+j-1);
10482: fprintf(ficgp,")) t \"l(%d,%d)\" w l",i,cpt);
10483: } /* nlstate */
10484: fprintf(ficgp,"\nset out; unset label;\n");
10485: } /* end cpt state*/
10486: } /* end nres */
10487: /* } /\* end covariate k1 *\/ */
10488:
10489: /* 5eme */
10490: /* Survival functions (period) from state i in state j by final state j */
10491: /* for (k1=1; k1<= m ; k1++){ /\* For each covariate combination if any *\/ */
10492: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
10493: k1=TKresult[nres];
10494: if(TKresult[nres]==0) k1=1; /* To be checked for noresult */
10495: /* if(m != 1 && TKresult[nres]!= k1) */
10496: /* continue; */
10497: for (cpt=1; cpt<=nlstate ; cpt ++) { /* For each inital state */
10498: strcpy(gplotlabel,"(");
10499: 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);
10500: for (k=1; k<=cptcovs; k++){ /* For each covariate k get corresponding value lv for combination k1 */
10501: fprintf(ficgp," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
10502: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
10503: /* for (k=1; k<=cptcoveff; k++){ /\* For each covariate and each value *\/ */
10504: /* lv=codtabm(k1,TnsdVar[Tvaraff[k]]); */
10505: /* /\* lv= decodtabm(k1,k,cptcoveff); /\\* Should be the covariate number corresponding to k1 combination *\\/ *\/ */
10506: /* /\* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 *\/ */
10507: /* /\* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 *\/ */
10508: /* /\* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 *\/ */
10509: /* /\* vlv= nbcode[Tvaraff[k]][lv]; *\/ */
10510: /* vlv= nbcode[Tvaraff[k]][codtabm(k1,TnsdVar[Tvaraff[k]])]; */
10511: /* fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv); */
10512: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv); */
10513: /* } */
10514: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
10515: /* fprintf(ficgp," V%d=%f ",Tvqresult[nres][resultmodel[nres][k4]],Tqresult[nres][resultmodel[nres][k4]]); */
10516: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][resultmodel[nres][k4]],Tqresult[nres][resultmodel[nres][k4]]); */
10517: }
10518: strcpy(gplotlabel+strlen(gplotlabel),")");
10519: fprintf(ficgp,"\n#\n");
10520: if(invalidvarcomb[k1]){
10521: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
10522: continue;
10523: }
10524:
10525: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" \n",subdirf2(optionfilefiname,"LIJT_"),cpt,k1,nres);
10526: fprintf(ficgp,"set label \"Alive state %d %s\" at graph 0.98,0.5 center rotate font \"Helvetica,12\"\n",cpt,gplotlabel);
10527: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability to be alive\" \n\
10528: set ter svg size 640, 480\nunset log y\nplot [%.f:%.f] ", ageminpar, agemaxpar);
10529: k=3;
10530: for (j=1; j<= nlstate ; j ++){ /* Lived in state j */
10531: if(j==1)
10532: fprintf(ficgp,"\"%s\"",subdirf2(fileresu,"PIJ_"));
10533: else
10534: fprintf(ficgp,", '' ");
10535: l=(nlstate+ndeath)*(cpt-1) +j;
10536: fprintf(ficgp," u (($1==%d && (floor($2)%%5 == 0)) ? ($3):1/0):($%d",k1,k+l);
10537: /* for (i=2; i<= nlstate+ndeath ; i ++) */
10538: /* fprintf(ficgp,"+$%d",k+l+i-1); */
10539: fprintf(ficgp,") t \"l(%d,%d)\" w l",cpt,j);
10540: } /* nlstate */
10541: fprintf(ficgp,", '' ");
10542: fprintf(ficgp," u (($1==%d && (floor($2)%%5 == 0)) ? ($3):1/0):(",k1);
10543: for (j=1; j<= nlstate ; j ++){ /* Lived in state j */
10544: l=(nlstate+ndeath)*(cpt-1) +j;
10545: if(j < nlstate)
10546: fprintf(ficgp,"$%d +",k+l);
10547: else
10548: fprintf(ficgp,"$%d) t\"l(%d,.)\" w l",k+l,cpt);
10549: }
10550: fprintf(ficgp,"\nset out; unset label;\n");
10551: } /* end cpt state*/
10552: /* } /\* end covariate *\/ */
10553: } /* end nres */
10554:
10555: /* 6eme */
10556: /* CV preval stable (period) for each covariate */
10557: /* for (k1=1; k1<= m ; k1 ++) /\* For each covariate combination if any *\/ */
10558: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
10559: k1=TKresult[nres];
10560: if(TKresult[nres]==0) k1=1; /* To be checked for noresult */
10561: /* if(m != 1 && TKresult[nres]!= k1) */
10562: /* continue; */
10563: for (cpt=1; cpt<=nlstate ; cpt ++) { /* For each life state of arrival */
10564: strcpy(gplotlabel,"(");
10565: fprintf(ficgp,"\n#\n#\n#CV preval stable (forward): 'pij' files, covariatecombination#=%d state=%d",k1, cpt);
10566: for (k=1; k<=cptcovs; k++){ /* For each covariate k get corresponding value lv for combination k1 */
10567: fprintf(ficgp," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
10568: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
10569: /* for (k=1; k<=cptcoveff; k++){ /\* For each covariate and each value *\/ */
10570: /* /\* lv= decodtabm(k1,k,cptcoveff); /\\* Should be the covariate number corresponding to k1 combination *\\/ *\/ */
10571: /* lv=codtabm(k1,TnsdVar[Tvaraff[k]]); */
10572: /* /\* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 *\/ */
10573: /* /\* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 *\/ */
10574: /* /\* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 *\/ */
10575: /* /\* vlv= nbcode[Tvaraff[k]][lv]; *\/ */
10576: /* vlv= nbcode[Tvaraff[k]][codtabm(k1,TnsdVar[Tvaraff[k]])]; */
10577: /* fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv); */
10578: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv); */
10579: /* } */
10580: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
10581: /* fprintf(ficgp," V%d=%f ",Tvqresult[nres][resultmodel[nres][k4]],Tqresult[nres][resultmodel[nres][k4]]); */
10582: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][resultmodel[nres][k4]],Tqresult[nres][resultmodel[nres][k4]]); */
10583: }
10584: strcpy(gplotlabel+strlen(gplotlabel),")");
10585: fprintf(ficgp,"\n#\n");
10586: if(invalidvarcomb[k1]){
10587: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
10588: continue;
10589: }
10590:
10591: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" \n",subdirf2(optionfilefiname,"P_"),cpt,k1,nres);
10592: fprintf(ficgp,"set label \"Alive state %d %s\" at graph 0.98,0.5 center rotate font \"Helvetica,12\"\n",cpt,gplotlabel);
10593: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability\" \n\
10594: set ter svg size 640, 480\nunset log y\nplot [%.f:%.f] ", ageminpar, agemaxpar);
10595: k=3; /* Offset */
10596: for (i=1; i<= nlstate ; i ++){ /* State of origin */
10597: if(i==1)
10598: fprintf(ficgp,"\"%s\"",subdirf2(fileresu,"PIJ_"));
10599: else
10600: fprintf(ficgp,", '' ");
10601: l=(nlstate+ndeath)*(i-1)+1; /* 1, 1+ nlstate+ndeath, 1+2*(nlstate+ndeath) */
10602: fprintf(ficgp," u ($1==%d ? ($3):1/0):($%d/($%d",k1,k+l+(cpt-1),k+l);
10603: for (j=2; j<= nlstate ; j ++)
10604: fprintf(ficgp,"+$%d",k+l+j-1);
10605: fprintf(ficgp,")) t \"prev(%d,%d)\" w l",i,cpt);
10606: } /* nlstate */
10607: fprintf(ficgp,"\nset out; unset label;\n");
10608: } /* end cpt state*/
10609: } /* end covariate */
10610:
10611:
10612: /* 7eme */
10613: if(prevbcast == 1){
10614: /* CV backward prevalence for each covariate */
10615: /* for (k1=1; k1<= m ; k1 ++) /\* For each covariate combination if any *\/ */
10616: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
10617: k1=TKresult[nres];
10618: if(TKresult[nres]==0) k1=1; /* To be checked for noresult */
10619: /* if(m != 1 && TKresult[nres]!= k1) */
10620: /* continue; */
10621: for (cpt=1; cpt<=nlstate ; cpt ++) { /* For each life origin state */
10622: strcpy(gplotlabel,"(");
10623: fprintf(ficgp,"\n#\n#\n#CV Backward stable prevalence: 'pijb' files, covariatecombination#=%d state=%d",k1, cpt);
10624: for (k=1; k<=cptcovs; k++){ /* For each covariate k get corresponding value lv for combination k1 */
10625: fprintf(ficgp," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
10626: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
10627: /* for (k=1; k<=cptcoveff; k++){ /\* For each covariate and each value *\/ */
10628: /* /\* lv= decodtabm(k1,k,cptcoveff); /\\* Should be the covariate number corresponding to k1 combination *\\/ *\/ */
10629: /* lv=codtabm(k1,TnsdVar[Tvaraff[k]]); */
10630: /* /\* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 *\/ */
10631: /* /\* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 *\/ */
10632: /* /\* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 *\/ */
10633: /* /\* vlv= nbcode[Tvaraff[k]][lv]; *\/ */
10634: /* vlv= nbcode[Tvaraff[k]][codtabm(k1,TnsdVar[Tvaraff[k]])]; */
10635: /* fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv); */
10636: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv); */
10637: /* } */
10638: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
10639: /* fprintf(ficgp," V%d=%f ",Tvqresult[nres][resultmodel[nres][k4]],Tqresult[nres][resultmodel[nres][k4]]); */
10640: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][resultmodel[nres][k4]],Tqresult[nres][resultmodel[nres][k4]]); */
10641: }
10642: strcpy(gplotlabel+strlen(gplotlabel),")");
10643: fprintf(ficgp,"\n#\n");
10644: if(invalidvarcomb[k1]){
10645: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
10646: continue;
10647: }
10648:
10649: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" \n",subdirf2(optionfilefiname,"PB_"),cpt,k1,nres);
10650: fprintf(ficgp,"set label \"Origin alive state %d %s\" at graph 0.98,0.5 center rotate font \"Helvetica,12\"\n",cpt,gplotlabel);
10651: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability\" \n\
10652: set ter svg size 640, 480\nunset log y\nplot [%.f:%.f] ", ageminpar, agemaxpar);
10653: k=3; /* Offset */
10654: for (i=1; i<= nlstate ; i ++){ /* State of arrival */
10655: if(i==1)
10656: fprintf(ficgp,"\"%s\"",subdirf2(fileresu,"PIJB_"));
10657: else
10658: fprintf(ficgp,", '' ");
10659: /* l=(nlstate+ndeath)*(i-1)+1; */
10660: l=(nlstate+ndeath)*(cpt-1)+1; /* fixed for i; cpt=1 1, cpt=2 1+ nlstate+ndeath, 1+2*(nlstate+ndeath) */
10661: /* fprintf(ficgp," u ($1==%d ? ($3):1/0):($%d/($%d",k1,k+l+(cpt-1),k+l); /\* a vérifier *\/ */
10662: /* fprintf(ficgp," u ($1==%d ? ($3):1/0):($%d/($%d",k1,k+l+(cpt-1),k+l+(cpt-1)+i-1); /\* a vérifier *\/ */
10663: fprintf(ficgp," u ($1==%d ? ($3):1/0):($%d",k1,k+l+i-1); /* To be verified */
10664: /* for (j=2; j<= nlstate ; j ++) */
10665: /* fprintf(ficgp,"+$%d",k+l+j-1); */
10666: /* /\* fprintf(ficgp,"+$%d",k+l+j-1); *\/ */
10667: fprintf(ficgp,") t \"bprev(%d,%d)\" w l",cpt,i);
10668: } /* nlstate */
10669: fprintf(ficgp,"\nset out; unset label;\n");
10670: } /* end cpt state*/
10671: } /* end covariate */
10672: } /* End if prevbcast */
10673:
10674: /* 8eme */
10675: if(prevfcast==1){
10676: /* Projection from cross-sectional to forward stable (period) prevalence for each covariate */
10677:
10678: /* for (k1=1; k1<= m ; k1 ++) /\* For each covariate combination if any *\/ */
10679: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
10680: k1=TKresult[nres];
10681: if(TKresult[nres]==0) k1=1; /* To be checked for noresult */
10682: /* if(m != 1 && TKresult[nres]!= k1) */
10683: /* continue; */
10684: for (cpt=1; cpt<=nlstate ; cpt ++) { /* For each life state */
10685: strcpy(gplotlabel,"(");
10686: fprintf(ficgp,"\n#\n#\n#Projection of prevalence to forward stable prevalence (period): 'PROJ_' files, covariatecombination#=%d state=%d",k1, cpt);
10687: for (k=1; k<=cptcovs; k++){ /* For each covariate k get corresponding value lv for combination k1 */
10688: fprintf(ficgp," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
10689: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
10690: /* for (k=1; k<=cptcoveff; k++){ /\* For each correspondig covariate value *\/ */
10691: /* /\* lv= decodtabm(k1,k,cptcoveff); /\\* Should be the covariate value corresponding to k1 combination and kth covariate *\\/ *\/ */
10692: /* lv=codtabm(k1,TnsdVar[Tvaraff[k]]); */
10693: /* /\* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 *\/ */
10694: /* /\* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 *\/ */
10695: /* /\* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 *\/ */
10696: /* /\* vlv= nbcode[Tvaraff[k]][lv]; *\/ */
10697: /* vlv= nbcode[Tvaraff[k]][codtabm(k1,TnsdVar[Tvaraff[k]])]; */
10698: /* fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv); */
10699: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv); */
10700: /* } */
10701: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
10702: /* fprintf(ficgp," V%d=%f ",Tvqresult[nres][resultmodel[nres][k4]],Tqresult[nres][resultmodel[nres][k4]]); */
10703: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][resultmodel[nres][k4]],Tqresult[nres][resultmodel[nres][k4]]); */
10704: }
10705: strcpy(gplotlabel+strlen(gplotlabel),")");
10706: fprintf(ficgp,"\n#\n");
10707: if(invalidvarcomb[k1]){
10708: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
10709: continue;
10710: }
10711:
10712: fprintf(ficgp,"# hpijx=probability over h years, hp.jx is weighted by observed prev\n ");
10713: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" \n",subdirf2(optionfilefiname,"PROJ_"),cpt,k1,nres);
10714: fprintf(ficgp,"set label \"Alive state %d %s\" at graph 0.98,0.5 center rotate font \"Helvetica,12\"\n",cpt,gplotlabel);
10715: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Prevalence\" \n\
10716: set ter svg size 640, 480\nunset log y\nplot [%.f:%.f] ", ageminpar, agemaxpar);
10717:
10718: /* for (i=1; i<= nlstate+1 ; i ++){ /\* nlstate +1 p11 p21 p.1 *\/ */
10719: istart=nlstate+1; /* Could be one if by state, but nlstate+1 is w.i projection only */
10720: /*istart=1;*/ /* Could be one if by state, but nlstate+1 is w.i projection only */
10721: for (i=istart; i<= nlstate+1 ; i ++){ /* nlstate +1 p11 p21 p.1 */
10722: /*# V1 = 1 V2 = 0 yearproj age p11 p21 p.1 p12 p22 p.2 p13 p23 p.3*/
10723: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
10724: /*# yearproj age p11 p21 p.1 p12 p22 p.2 p13 p23 p.3*/
10725: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
10726: if(i==istart){
10727: fprintf(ficgp,"\"%s\"",subdirf2(fileresu,"F_"));
10728: }else{
10729: fprintf(ficgp,",\\\n '' ");
10730: }
10731: if(cptcoveff ==0){ /* No covariate */
10732: ioffset=2; /* Age is in 2 */
10733: /*# yearproj age p11 p21 p31 p.1 p12 p22 p32 p.2 p13 p23 p33 p.3 p14 p24 p34 p.4*/
10734: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 */
10735: /*# V1 = 1 yearproj age p11 p21 p31 p.1 p12 p22 p32 p.2 p13 p23 p33 p.3 p14 p24 p34 p.4*/
10736: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 */
10737: fprintf(ficgp," u %d:(", ioffset);
10738: if(i==nlstate+1){
10739: fprintf(ficgp," $%d/(1.-$%d)):1 t 'pw.%d' with line lc variable ", \
10740: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset+1+(i-1)+(nlstate+1)*nlstate,cpt );
10741: fprintf(ficgp,",\\\n '' ");
10742: fprintf(ficgp," u %d:(",ioffset);
10743: fprintf(ficgp," (($1-$2) == %d ) ? $%d/(1.-$%d) : 1/0):1 with labels center not ", \
10744: offyear, \
10745: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset+1+(i-1)+(nlstate+1)*nlstate );
10746: }else
10747: fprintf(ficgp," $%d/(1.-$%d)) t 'p%d%d' with line ", \
10748: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset+1+(i-1)+(nlstate+1)*nlstate,i,cpt );
10749: }else{ /* more than 2 covariates */
10750: ioffset=2*cptcoveff+2; /* Age is in 4 or 6 or etc.*/
10751: /*# V1 = 1 V2 = 0 yearproj age p11 p21 p.1 p12 p22 p.2 p13 p23 p.3*/
10752: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
10753: iyearc=ioffset-1;
10754: iagec=ioffset;
10755: fprintf(ficgp," u %d:(",ioffset);
10756: kl=0;
10757: strcpy(gplotcondition,"(");
10758: /* for (k=1; k<=cptcoveff; k++){ /\* For each covariate writing the chain of conditions *\/ */
10759: /* lv= decodtabm(k1,k,cptcoveff); /\* Should be the covariate value corresponding to combination k1 and covariate k *\/ */
10760: for (k=1; k<=cptcovs; k++){ /* For each covariate k get corresponding value lv for combination k1 */
10761: /* lv=codtabm(k1,TnsdVar[Tvaraff[k]]); */
10762: lv=Tvresult[nres][k];
10763: vlv=TinvDoQresult[nres][Tvresult[nres][k]];
10764: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
10765: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
10766: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
10767: /* vlv= nbcode[Tvaraff[k]][lv]; /\* Value of the modality of Tvaraff[k] *\/ */
10768: /* vlv= nbcode[Tvaraff[k]][codtabm(k1,TnsdVar[Tvaraff[k]])]; */
10769: kl++;
10770: /* sprintf(gplotcondition+strlen(gplotcondition),"$%d==%d && $%d==%d " ,kl,Tvaraff[k], kl+1, nbcode[Tvaraff[k]][lv]); */
10771: sprintf(gplotcondition+strlen(gplotcondition),"$%d==%d && $%d==%d " ,kl,lv, kl+1, vlv );
10772: kl++;
10773: if(k <cptcovs && cptcovs>1)
10774: sprintf(gplotcondition+strlen(gplotcondition)," && ");
10775: }
10776: strcpy(gplotcondition+strlen(gplotcondition),")");
10777: /* 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 *\/ */
10778: /*6+(cpt-1)*(nlstate+1)+1+(i-1)+(nlstate+1)*nlstate; 6+(1-1)*(2+1)+1+(1-1) +(2+1)*2=13 */
10779: /*6+1+(i-1)+(nlstate+1)*nlstate; 6+1+(1-1) +(2+1)*2=13 */
10780: /* '' 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*/
10781: if(i==nlstate+1){
10782: fprintf(ficgp,"%s ? $%d/(1.-$%d) : 1/0):%d t 'p.%d' with line lc variable", gplotcondition, \
10783: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset+1+(i-1)+(nlstate+1)*nlstate,iyearc, cpt );
10784: fprintf(ficgp,",\\\n '' ");
10785: fprintf(ficgp," u %d:(",iagec);
10786: fprintf(ficgp,"%s && (($%d-$%d) == %d ) ? $%d/(1.-$%d) : 1/0):%d with labels center not ", gplotcondition, \
10787: iyearc, iagec, offyear, \
10788: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset+1+(i-1)+(nlstate+1)*nlstate, iyearc );
10789: /* '' u 6:(($1==1 && $2==0 && $3==2 && $4==0) && (($5-$6) == 1947) ? $10/(1.-$22) : 1/0):5 with labels center boxed not*/
10790: }else{
10791: fprintf(ficgp,"%s ? $%d/(1.-$%d) : 1/0) t 'p%d%d' with line ", gplotcondition, \
10792: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset +1+(i-1)+(nlstate+1)*nlstate,i,cpt );
10793: }
10794: } /* end if covariate */
10795: } /* nlstate */
10796: fprintf(ficgp,"\nset out; unset label;\n");
10797: } /* end cpt state*/
10798: } /* end covariate */
10799: } /* End if prevfcast */
10800:
10801: if(prevbcast==1){
10802: /* Back projection from cross-sectional to stable (mixed) for each covariate */
10803:
10804: /* for (k1=1; k1<= m ; k1 ++) /\* For each covariate combination if any *\/ */
10805: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
10806: k1=TKresult[nres];
10807: if(TKresult[nres]==0) k1=1; /* To be checked for noresult */
10808: /* if(m != 1 && TKresult[nres]!= k1) */
10809: /* continue; */
10810: for (cpt=1; cpt<=nlstate ; cpt ++) { /* For each life state */
10811: strcpy(gplotlabel,"(");
10812: fprintf(ficgp,"\n#\n#\n#Back projection of prevalence to stable (mixed) back prevalence: 'BPROJ_' files, covariatecombination#=%d originstate=%d",k1, cpt);
10813: for (k=1; k<=cptcovs; k++){ /* For each covariate k get corresponding value lv for combination k1 */
10814: fprintf(ficgp," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
10815: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
10816: /* for (k=1; k<=cptcoveff; k++){ /\* For each correspondig covariate value *\/ */
10817: /* /\* lv= decodtabm(k1,k,cptcoveff); /\\* Should be the covariate value corresponding to k1 combination and kth covariate *\\/ *\/ */
10818: /* lv= codtabm(k1,TnsdVar[Tvaraff[k]]); /\* Should be the covariate value corresponding to combination k1 and covariate k *\/ */
10819: /* /\* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 *\/ */
10820: /* /\* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 *\/ */
10821: /* /\* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 *\/ */
10822: /* /\* vlv= nbcode[Tvaraff[k]][lv]; *\/ */
10823: /* vlv= nbcode[Tvaraff[k]][codtabm(k1,TnsdVar[Tvaraff[k]])]; */
10824: /* fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv); */
10825: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv); */
10826: /* } */
10827: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
10828: /* fprintf(ficgp," V%d=%f ",Tvqresult[nres][resultmodel[nres][k4]],Tqresult[nres][resultmodel[nres][k4]]); */
10829: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][resultmodel[nres][k4]],Tqresult[nres][resultmodel[nres][k4]]); */
10830: }
10831: strcpy(gplotlabel+strlen(gplotlabel),")");
10832: fprintf(ficgp,"\n#\n");
10833: if(invalidvarcomb[k1]){
10834: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
10835: continue;
10836: }
10837:
10838: fprintf(ficgp,"# hbijx=backprobability over h years, hb.jx is weighted by observed prev at destination state\n ");
10839: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" \n",subdirf2(optionfilefiname,"PROJB_"),cpt,k1,nres);
10840: fprintf(ficgp,"set label \"Origin alive state %d %s\" at graph 0.98,0.5 center rotate font \"Helvetica,12\"\n",cpt,gplotlabel);
10841: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Prevalence\" \n\
10842: set ter svg size 640, 480\nunset log y\nplot [%.f:%.f] ", ageminpar, agemaxpar);
10843:
10844: /* for (i=1; i<= nlstate+1 ; i ++){ /\* nlstate +1 p11 p21 p.1 *\/ */
10845: istart=nlstate+1; /* Could be one if by state, but nlstate+1 is w.i projection only */
10846: /*istart=1;*/ /* Could be one if by state, but nlstate+1 is w.i projection only */
10847: for (i=istart; i<= nlstate+1 ; i ++){ /* nlstate +1 p11 p21 p.1 */
10848: /*# V1 = 1 V2 = 0 yearproj age p11 p21 p.1 p12 p22 p.2 p13 p23 p.3*/
10849: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
10850: /*# yearproj age p11 p21 p.1 p12 p22 p.2 p13 p23 p.3*/
10851: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
10852: if(i==istart){
10853: fprintf(ficgp,"\"%s\"",subdirf2(fileresu,"FB_"));
10854: }else{
10855: fprintf(ficgp,",\\\n '' ");
10856: }
10857: /* if(cptcoveff ==0){ /\* No covariate *\/ */
10858: if(cptcovs ==0){ /* No covariate */
10859: ioffset=2; /* Age is in 2 */
10860: /*# yearproj age p11 p21 p31 p.1 p12 p22 p32 p.2 p13 p23 p33 p.3 p14 p24 p34 p.4*/
10861: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 */
10862: /*# V1 = 1 yearproj age p11 p21 p31 p.1 p12 p22 p32 p.2 p13 p23 p33 p.3 p14 p24 p34 p.4*/
10863: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 */
10864: fprintf(ficgp," u %d:(", ioffset);
10865: if(i==nlstate+1){
10866: fprintf(ficgp," $%d/(1.-$%d)):1 t 'bw%d' with line lc variable ", \
10867: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset+1+(i-1)+(nlstate+1)*nlstate,cpt );
10868: fprintf(ficgp,",\\\n '' ");
10869: fprintf(ficgp," u %d:(",ioffset);
10870: fprintf(ficgp," (($1-$2) == %d ) ? $%d : 1/0):1 with labels center not ", \
10871: offbyear, \
10872: ioffset+(cpt-1)*(nlstate+1)+1+(i-1) );
10873: }else
10874: fprintf(ficgp," $%d/(1.-$%d)) t 'b%d%d' with line ", \
10875: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset+1+(i-1)+(nlstate+1)*nlstate,cpt,i );
10876: }else{ /* more than 2 covariates */
10877: ioffset=2*cptcoveff+2; /* Age is in 4 or 6 or etc.*/
10878: /*# V1 = 1 V2 = 0 yearproj age p11 p21 p.1 p12 p22 p.2 p13 p23 p.3*/
10879: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
10880: iyearc=ioffset-1;
10881: iagec=ioffset;
10882: fprintf(ficgp," u %d:(",ioffset);
10883: kl=0;
10884: strcpy(gplotcondition,"(");
10885: for (k=1; k<=cptcovs; k++){ /* For each covariate k of the resultline, get corresponding value lv for combination k1 */
10886: if(Dummy[modelresult[nres][k]]==0){ /* To be verified */
10887: /* for (k=1; k<=cptcoveff; k++){ /\* For each covariate writing the chain of conditions *\/ */
10888: /* lv= decodtabm(k1,k,cptcoveff); /\* Should be the covariate value corresponding to combination k1 and covariate k *\/ */
10889: /* lv= codtabm(k1,TnsdVar[Tvaraff[k]]); /\* Should be the covariate value corresponding to combination k1 and covariate k *\/ */
10890: lv=Tvresult[nres][k];
10891: vlv=TinvDoQresult[nres][Tvresult[nres][k]];
10892: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
10893: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
10894: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
10895: /* vlv= nbcode[Tvaraff[k]][lv]; /\* Value of the modality of Tvaraff[k] *\/ */
10896: /* vlv= nbcode[Tvaraff[k]][codtabm(k1,TnsdVar[Tvaraff[k]])]; */
10897: kl++;
10898: /* sprintf(gplotcondition+strlen(gplotcondition),"$%d==%d && $%d==%d " ,kl,Tvaraff[k], kl+1, nbcode[Tvaraff[k]][lv]); */
10899: sprintf(gplotcondition+strlen(gplotcondition),"$%d==%d && $%d==%lg " ,kl,Tvresult[nres][k], kl+1,TinvDoQresult[nres][Tvresult[nres][k]]);
10900: kl++;
10901: if(k <cptcovs && cptcovs>1)
10902: sprintf(gplotcondition+strlen(gplotcondition)," && ");
10903: }
10904: }
10905: strcpy(gplotcondition+strlen(gplotcondition),")");
10906: /* 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 *\/ */
10907: /*6+(cpt-1)*(nlstate+1)+1+(i-1)+(nlstate+1)*nlstate; 6+(1-1)*(2+1)+1+(1-1) +(2+1)*2=13 */
10908: /*6+1+(i-1)+(nlstate+1)*nlstate; 6+1+(1-1) +(2+1)*2=13 */
10909: /* '' 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*/
10910: if(i==nlstate+1){
10911: fprintf(ficgp,"%s ? $%d : 1/0):%d t 'bw%d' with line lc variable", gplotcondition, \
10912: ioffset+(cpt-1)*(nlstate+1)+1+(i-1),iyearc,cpt );
10913: fprintf(ficgp,",\\\n '' ");
10914: fprintf(ficgp," u %d:(",iagec);
10915: /* fprintf(ficgp,"%s && (($5-$6) == %d ) ? $%d/(1.-$%d) : 1/0):5 with labels center not ", gplotcondition, \ */
10916: fprintf(ficgp,"%s && (($%d-$%d) == %d ) ? $%d : 1/0):%d with labels center not ", gplotcondition, \
10917: iyearc,iagec,offbyear, \
10918: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), iyearc );
10919: /* '' u 6:(($1==1 && $2==0 && $3==2 && $4==0) && (($5-$6) == 1947) ? $10/(1.-$22) : 1/0):5 with labels center boxed not*/
10920: }else{
10921: /* fprintf(ficgp,"%s ? $%d/(1.-$%d) : 1/0) t 'p%d%d' with line ", gplotcondition, \ */
10922: fprintf(ficgp,"%s ? $%d : 1/0) t 'b%d%d' with line ", gplotcondition, \
10923: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), cpt,i );
10924: }
10925: } /* end if covariate */
10926: } /* nlstate */
10927: fprintf(ficgp,"\nset out; unset label;\n");
10928: } /* end cpt state*/
10929: } /* end covariate */
10930: } /* End if prevbcast */
10931:
10932:
10933: /* 9eme writing MLE parameters */
10934: fprintf(ficgp,"\n##############\n#9eme MLE estimated parameters\n#############\n");
10935: for(i=1,jk=1; i <=nlstate; i++){
10936: fprintf(ficgp,"# initial state %d\n",i);
10937: for(k=1; k <=(nlstate+ndeath); k++){
10938: if (k != i) {
10939: fprintf(ficgp,"# current state %d\n",k);
10940: for(j=1; j <=ncovmodel; j++){
10941: fprintf(ficgp,"p%d=%f; ",jk,p[jk]);
10942: jk++;
10943: }
10944: fprintf(ficgp,"\n");
10945: }
10946: }
10947: }
10948: fprintf(ficgp,"##############\n#\n");
10949:
10950: /*goto avoid;*/
10951: /* 10eme Graphics of probabilities or incidences using written MLE parameters */
10952: fprintf(ficgp,"\n##############\n#10eme Graphics of probabilities or incidences\n#############\n");
10953: fprintf(ficgp,"# logi(p12/p11)=a12+b12*age+c12age*age+d12*V1+e12*V1*age\n");
10954: fprintf(ficgp,"# logi(p12/p11)=p1 +p2*age +p3*age*age+ p4*V1+ p5*V1*age\n");
10955: fprintf(ficgp,"# logi(p13/p11)=a13+b13*age+c13age*age+d13*V1+e13*V1*age\n");
10956: fprintf(ficgp,"# logi(p13/p11)=p6 +p7*age +p8*age*age+ p9*V1+ p10*V1*age\n");
10957: fprintf(ficgp,"# p12+p13+p14+p11=1=p11(1+exp(a12+b12*age+c12age*age+d12*V1+e12*V1*age)\n");
10958: fprintf(ficgp,"# +exp(a13+b13*age+c13age*age+d13*V1+e13*V1*age)+...)\n");
10959: fprintf(ficgp,"# p11=1/(1+exp(a12+b12*age+c12age*age+d12*V1+e12*V1*age)\n");
10960: fprintf(ficgp,"# +exp(a13+b13*age+c13age*age+d13*V1+e13*V1*age)+...)\n");
10961: fprintf(ficgp,"# p12=exp(a12+b12*age+c12age*age+d12*V1+e12*V1*age)/\n");
10962: fprintf(ficgp,"# (1+exp(a12+b12*age+c12age*age+d12*V1+e12*V1*age)\n");
10963: fprintf(ficgp,"# +exp(a13+b13*age+c13age*age+d13*V1+e13*V1*age))\n");
10964: fprintf(ficgp,"# +exp(a14+b14*age+c14age*age+d14*V1+e14*V1*age)+...)\n");
10965: fprintf(ficgp,"#\n");
10966: for(ng=1; ng<=3;ng++){ /* Number of graphics: first is logit, 2nd is probabilities, third is incidences per year*/
10967: fprintf(ficgp,"#Number of graphics: first is logit, 2nd is probabilities, third is incidences per year\n");
10968: fprintf(ficgp,"#model=1+age+%s \n",model);
10969: fprintf(ficgp,"# Type of graphic ng=%d\n",ng);
10970: /* fprintf(ficgp,"# k1=1 to 2^%d=%d\n",cptcoveff,m);/\* to be checked *\/ */
10971: fprintf(ficgp,"# k1=1 to 2^%d=%d\n",cptcovs,m);/* to be checked */
10972: /* for(k1=1; k1 <=m; k1++) /\* For each combination of covariate *\/ */
10973: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
10974: /* k1=nres; */
10975: k1=TKresult[nres];
10976: if(TKresult[nres]==0) k1=1; /* To be checked for noresult */
10977: fprintf(ficgp,"\n\n# Resultline k1=%d ",k1);
10978: strcpy(gplotlabel,"(");
10979: /*sprintf(gplotlabel+strlen(gplotlabel)," Dummy combination %d ",k1);*/
10980: for (k=1; k<=cptcovs; k++){ /**< cptcovs number of SIMPLE covariates in the model V2+V1 =2 (dummy or quantit or time varying) */
10981: /* for each resultline nres, and position k, Tvresult[nres][k] gives the name of the variable and
10982: TinvDoQresult[nres][Tvresult[nres][k]] gives its value double or integer) */
10983: fprintf(ficgp," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
10984: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
10985: }
10986: /* if(m != 1 && TKresult[nres]!= k1) */
10987: /* continue; */
10988: /* fprintf(ficgp,"\n\n# Combination of dummy k1=%d which is ",k1); */
10989: /* strcpy(gplotlabel,"("); */
10990: /* /\*sprintf(gplotlabel+strlen(gplotlabel)," Dummy combination %d ",k1);*\/ */
10991: /* for (k=1; k<=cptcoveff; k++){ /\* For each correspondig covariate value *\/ */
10992: /* /\* lv= decodtabm(k1,k,cptcoveff); /\\* Should be the covariate value corresponding to k1 combination and kth covariate *\\/ *\/ */
10993: /* lv= codtabm(k1,TnsdVar[Tvaraff[k]]); /\* Should be the covariate value corresponding to combination k1 and covariate k *\/ */
10994: /* /\* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 *\/ */
10995: /* /\* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 *\/ */
10996: /* /\* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 *\/ */
10997: /* /\* vlv= nbcode[Tvaraff[k]][lv]; *\/ */
10998: /* vlv= nbcode[Tvaraff[k]][codtabm(k1,TnsdVar[Tvaraff[k]])]; */
10999: /* fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv); */
11000: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv); */
11001: /* } */
11002: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
11003: /* fprintf(ficgp," V%d=%f ",Tvqresult[nres][resultmodel[nres][k4]],Tqresult[nres][resultmodel[nres][k4]]); */
11004: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][resultmodel[nres][k4]],Tqresult[nres][resultmodel[nres][k4]]); */
11005: /* } */
11006: strcpy(gplotlabel+strlen(gplotlabel),")");
11007: fprintf(ficgp,"\n#\n");
11008: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" ",subdirf2(optionfilefiname,"PE_"),k1,ng,nres);
11009: fprintf(ficgp,"\nset key outside ");
11010: /* fprintf(ficgp,"\nset label \"%s\" at graph 1.2,0.5 center rotate font \"Helvetica,12\"\n",gplotlabel); */
11011: fprintf(ficgp,"\nset title \"%s\" font \"Helvetica,12\"\n",gplotlabel);
11012: fprintf(ficgp,"\nset ter svg size 640, 480 ");
11013: if (ng==1){
11014: fprintf(ficgp,"\nset ylabel \"Value of the logit of the model\"\n"); /* exp(a12+b12*x) could be nice */
11015: fprintf(ficgp,"\nunset log y");
11016: }else if (ng==2){
11017: fprintf(ficgp,"\nset ylabel \"Probability\"\n");
11018: fprintf(ficgp,"\nset log y");
11019: }else if (ng==3){
11020: fprintf(ficgp,"\nset ylabel \"Quasi-incidence per year\"\n");
11021: fprintf(ficgp,"\nset log y");
11022: }else
11023: fprintf(ficgp,"\nunset title ");
11024: fprintf(ficgp,"\nplot [%.f:%.f] ",ageminpar,agemaxpar);
11025: i=1;
11026: for(k2=1; k2<=nlstate; k2++) {
11027: k3=i;
11028: for(k=1; k<=(nlstate+ndeath); k++) {
11029: if (k != k2){
11030: switch( ng) {
11031: case 1:
11032: if(nagesqr==0)
11033: fprintf(ficgp," p%d+p%d*x",i,i+1);
11034: else /* nagesqr =1 */
11035: fprintf(ficgp," p%d+p%d*x+p%d*x*x",i,i+1,i+1+nagesqr);
11036: break;
11037: case 2: /* ng=2 */
11038: if(nagesqr==0)
11039: fprintf(ficgp," exp(p%d+p%d*x",i,i+1);
11040: else /* nagesqr =1 */
11041: fprintf(ficgp," exp(p%d+p%d*x+p%d*x*x",i,i+1,i+1+nagesqr);
11042: break;
11043: case 3:
11044: if(nagesqr==0)
11045: fprintf(ficgp," %f*exp(p%d+p%d*x",YEARM/stepm,i,i+1);
11046: else /* nagesqr =1 */
11047: fprintf(ficgp," %f*exp(p%d+p%d*x+p%d*x*x",YEARM/stepm,i,i+1,i+1+nagesqr);
11048: break;
11049: }
11050: ij=1;/* To be checked else nbcode[0][0] wrong */
11051: ijp=1; /* product no age */
11052: /* for(j=3; j <=ncovmodel-nagesqr; j++) { */
11053: for(j=1; j <=cptcovt; j++) { /* For each covariate of the simplified model */
11054: /* printf("Tage[%d]=%d, j=%d\n", ij, Tage[ij], j); */
11055: switch(Typevar[j]){
11056: case 1:
11057: if(cptcovage >0){ /* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1, 2 V5 and V1 */
11058: if(j==Tage[ij]) { /* Product by age To be looked at!!*//* Bug valgrind */
11059: if(ij <=cptcovage) { /* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1, 2 V5 and V1 */
11060: if(DummyV[j]==0){/* Bug valgrind */
11061: fprintf(ficgp,"+p%d*%d*x",i+j+2+nagesqr-1,Tinvresult[nres][Tvar[j]]);;
11062: }else{ /* quantitative */
11063: fprintf(ficgp,"+p%d*%f*x",i+j+2+nagesqr-1,Tqinvresult[nres][Tvar[j]]); /* Tqinvresult in decoderesult */
11064: /* fprintf(ficgp,"+p%d*%d*x",i+j+nagesqr-1,nbcode[Tvar[j-2]][codtabm(k1,Tvar[j-2])]); */
11065: }
11066: ij++;
11067: }
11068: }
11069: }
11070: break;
11071: case 2:
11072: if(cptcovprod >0){
11073: if(j==Tprod[ijp]) { /* */
11074: /* printf("Tprod[%d]=%d, j=%d\n", ij, Tprod[ijp], j); */
11075: if(ijp <=cptcovprod) { /* Product */
11076: if(DummyV[Tvard[ijp][1]]==0){/* Vn is dummy */
11077: if(DummyV[Tvard[ijp][2]]==0){/* Vn and Vm are dummy */
11078: /* 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)]); */
11079: fprintf(ficgp,"+p%d*%d*%d",i+j+2+nagesqr-1,Tinvresult[nres][Tvard[ijp][1]],Tinvresult[nres][Tvard[ijp][2]]);
11080: }else{ /* Vn is dummy and Vm is quanti */
11081: /* fprintf(ficgp,"+p%d*%d*%f",i+j+2+nagesqr-1,nbcode[Tvard[ijp][1]][codtabm(k1,j)],Tqinvresult[nres][Tvard[ijp][2]]); */
11082: fprintf(ficgp,"+p%d*%d*%f",i+j+2+nagesqr-1,Tinvresult[nres][Tvard[ijp][1]],Tqinvresult[nres][Tvard[ijp][2]]);
11083: }
11084: }else{ /* Vn*Vm Vn is quanti */
11085: if(DummyV[Tvard[ijp][2]]==0){
11086: fprintf(ficgp,"+p%d*%d*%f",i+j+2+nagesqr-1,Tinvresult[nres][Tvard[ijp][2]],Tqinvresult[nres][Tvard[ijp][1]]);
11087: }else{ /* Both quanti */
11088: fprintf(ficgp,"+p%d*%f*%f",i+j+2+nagesqr-1,Tqinvresult[nres][Tvard[ijp][1]],Tqinvresult[nres][Tvard[ijp][2]]);
11089: }
11090: }
11091: ijp++;
11092: }
11093: } /* end Tprod */
11094: }
11095: break;
11096: case 3:
11097: if(cptcovdageprod >0){
11098: /* if(j==Tprod[ijp]) { */ /* not necessary */
11099: /* printf("Tprod[%d]=%d, j=%d\n", ij, Tprod[ijp], j); */
11100: if(ijp <=cptcovprod) { /* Product Vn*Vm and age*VN*Vm*/
11101: if(DummyV[Tvardk[ijp][1]]==0){/* Vn is dummy */
11102: if(DummyV[Tvardk[ijp][2]]==0){/* Vn and Vm are dummy */
11103: /* 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)]); */
11104: fprintf(ficgp,"+p%d*%d*%d*x",i+j+2+nagesqr-1,Tinvresult[nres][Tvard[ijp][1]],Tinvresult[nres][Tvard[ijp][2]]);
11105: }else{ /* Vn is dummy and Vm is quanti */
11106: /* fprintf(ficgp,"+p%d*%d*%f",i+j+2+nagesqr-1,nbcode[Tvard[ijp][1]][codtabm(k1,j)],Tqinvresult[nres][Tvard[ijp][2]]); */
11107: fprintf(ficgp,"+p%d*%d*%f*x",i+j+2+nagesqr-1,Tinvresult[nres][Tvardk[ijp][1]],Tqinvresult[nres][Tvardk[ijp][2]]);
11108: }
11109: }else{ /* age* Vn*Vm Vn is quanti HERE */
11110: if(DummyV[Tvard[ijp][2]]==0){
11111: fprintf(ficgp,"+p%d*%d*%f*x",i+j+2+nagesqr-1,Tinvresult[nres][Tvardk[ijp][2]],Tqinvresult[nres][Tvardk[ijp][1]]);
11112: }else{ /* Both quanti */
11113: fprintf(ficgp,"+p%d*%f*%f*x",i+j+2+nagesqr-1,Tqinvresult[nres][Tvardk[ijp][1]],Tqinvresult[nres][Tvardk[ijp][2]]);
11114: }
11115: }
11116: ijp++;
11117: }
11118: /* } */ /* end Tprod */
11119: }
11120: break;
11121: case 0:
11122: /* simple covariate */
11123: /* fprintf(ficgp,"+p%d*%d",i+j+2+nagesqr-1,nbcode[Tvar[j]][codtabm(k1,j)]); /\* Valgrind bug nbcode *\/ */
11124: if(Dummy[j]==0){
11125: fprintf(ficgp,"+p%d*%d",i+j+2+nagesqr-1,Tinvresult[nres][Tvar[j]]); /* */
11126: }else{ /* quantitative */
11127: fprintf(ficgp,"+p%d*%f",i+j+2+nagesqr-1,Tqinvresult[nres][Tvar[j]]); /* */
11128: /* fprintf(ficgp,"+p%d*%d*x",i+j+nagesqr-1,nbcode[Tvar[j-2]][codtabm(k1,Tvar[j-2])]); */
11129: }
11130: /* end simple */
11131: break;
11132: default:
11133: break;
11134: } /* end switch */
11135: } /* end j */
11136: }else{ /* k=k2 */
11137: if(ng !=1 ){ /* For logit formula of log p11 is more difficult to get */
11138: fprintf(ficgp," (1.");i=i-ncovmodel;
11139: }else
11140: i=i-ncovmodel;
11141: }
11142:
11143: if(ng != 1){
11144: fprintf(ficgp,")/(1");
11145:
11146: for(cpt=1; cpt <=nlstate; cpt++){
11147: if(nagesqr==0)
11148: fprintf(ficgp,"+exp(p%d+p%d*x",k3+(cpt-1)*ncovmodel,k3+(cpt-1)*ncovmodel+1);
11149: else /* nagesqr =1 */
11150: 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);
11151:
11152: ij=1;
11153: ijp=1;
11154: /* for(j=3; j <=ncovmodel-nagesqr; j++){ */
11155: for(j=1; j <=cptcovt; j++) { /* For each covariate of the simplified model */
11156: switch(Typevar[j]){
11157: case 1:
11158: if(cptcovage >0){
11159: if(j==Tage[ij]) { /* Bug valgrind */
11160: if(ij <=cptcovage) { /* Bug valgrind */
11161: if(DummyV[j]==0){/* Bug valgrind */
11162: /* fprintf(ficgp,"+p%d*%d*x",k3+(cpt-1)*ncovmodel+1+j-2+nagesqr,nbcode[Tvar[j-2]][codtabm(k1,j-2)]); */
11163: /* fprintf(ficgp,"+p%d*%d*x",k3+(cpt-1)*ncovmodel+1+j+nagesqr,nbcode[Tvar[j]][codtabm(k1,j)]); */
11164: fprintf(ficgp,"+p%d*%d*x",k3+(cpt-1)*ncovmodel+1+j+nagesqr,Tinvresult[nres][Tvar[j]]);
11165: /* fprintf(ficgp,"+p%d*%d*x",i+j+2+nagesqr-1,Tinvresult[nres][Tvar[j]]);; */
11166: /* fprintf(ficgp,"+p%d*%d*x",k3+(cpt-1)*ncovmodel+1+j-2+nagesqr,nbcode[Tvar[j-2]][codtabm(k1,Tvar[j-2])]); */
11167: }else{ /* quantitative */
11168: /* fprintf(ficgp,"+p%d*%f*x",i+j+2+nagesqr-1,Tqinvresult[nres][Tvar[j]]); /\* Tqinvresult in decoderesult *\/ */
11169: fprintf(ficgp,"+p%d*%f*x",k3+(cpt-1)*ncovmodel+1+j+nagesqr,Tqinvresult[nres][Tvar[j]]); /* Tqinvresult in decoderesult */
11170: /* fprintf(ficgp,"+p%d*%f*x",i+j+2+nagesqr-1,Tqinvresult[nres][Tvar[j]]); /\* Tqinvresult in decoderesult *\/ */
11171: /* fprintf(ficgp,"+p%d*%d*x",i+j+nagesqr-1,nbcode[Tvar[j-2]][codtabm(k1,Tvar[j-2])]); */
11172: }
11173: ij++;
11174: }
11175: }
11176: }
11177: break;
11178: case 2:
11179: if(cptcovprod >0){
11180: if(j==Tprod[ijp]) { /* */
11181: /* printf("Tprod[%d]=%d, j=%d\n", ij, Tprod[ijp], j); */
11182: if(ijp <=cptcovprod) { /* Product */
11183: if(DummyV[Tvard[ijp][1]]==0){/* Vn is dummy */
11184: if(DummyV[Tvard[ijp][2]]==0){/* Vn and Vm are dummy */
11185: /* 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)]); */
11186: fprintf(ficgp,"+p%d*%d*%d",k3+(cpt-1)*ncovmodel+1+j+nagesqr,Tinvresult[nres][Tvard[ijp][1]],Tinvresult[nres][Tvard[ijp][2]]);
11187: /* fprintf(ficgp,"+p%d*%d*%d",i+j+2+nagesqr-1,Tinvresult[nres][Tvard[ijp][1]],Tinvresult[nres][Tvard[ijp][2]]); */
11188: }else{ /* Vn is dummy and Vm is quanti */
11189: /* fprintf(ficgp,"+p%d*%d*%f",i+j+2+nagesqr-1,nbcode[Tvard[ijp][1]][codtabm(k1,j)],Tqinvresult[nres][Tvard[ijp][2]]); */
11190: fprintf(ficgp,"+p%d*%d*%f",k3+(cpt-1)*ncovmodel+1+j+nagesqr,Tinvresult[nres][Tvard[ijp][1]],Tqinvresult[nres][Tvard[ijp][2]]);
11191: /* fprintf(ficgp,"+p%d*%d*%f",i+j+2+nagesqr-1,Tinvresult[nres][Tvard[ijp][1]],Tqinvresult[nres][Tvard[ijp][2]]); */
11192: }
11193: }else{ /* Vn*Vm Vn is quanti */
11194: if(DummyV[Tvard[ijp][2]]==0){
11195: fprintf(ficgp,"+p%d*%d*%f",k3+(cpt-1)*ncovmodel+1+j+nagesqr,Tinvresult[nres][Tvard[ijp][2]],Tqinvresult[nres][Tvard[ijp][1]]);
11196: /* fprintf(ficgp,"+p%d*%d*%f",i+j+2+nagesqr-1,Tinvresult[nres][Tvard[ijp][2]],Tqinvresult[nres][Tvard[ijp][1]]); */
11197: }else{ /* Both quanti */
11198: fprintf(ficgp,"+p%d*%f*%f",k3+(cpt-1)*ncovmodel+1+j+nagesqr,Tqinvresult[nres][Tvard[ijp][1]],Tqinvresult[nres][Tvard[ijp][2]]);
11199: /* fprintf(ficgp,"+p%d*%f*%f",i+j+2+nagesqr-1,Tqinvresult[nres][Tvard[ijp][1]],Tqinvresult[nres][Tvard[ijp][2]]); */
11200: }
11201: }
11202: ijp++;
11203: }
11204: } /* end Tprod */
11205: } /* end if */
11206: break;
11207: case 3:
11208: if(cptcovdageprod >0){
11209: /* if(j==Tprod[ijp]) { /\* *\/ */
11210: /* printf("Tprod[%d]=%d, j=%d\n", ij, Tprod[ijp], j); */
11211: if(ijp <=cptcovprod) { /* Product */
11212: if(DummyV[Tvardk[ijp][1]]==0){/* Vn is dummy */
11213: if(DummyV[Tvardk[ijp][2]]==0){/* Vn and Vm are dummy */
11214: /* 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)]); */
11215: fprintf(ficgp,"+p%d*%d*%d*x",k3+(cpt-1)*ncovmodel+1+j+nagesqr,Tinvresult[nres][Tvardk[ijp][1]],Tinvresult[nres][Tvardk[ijp][2]]);
11216: /* fprintf(ficgp,"+p%d*%d*%d",i+j+2+nagesqr-1,Tinvresult[nres][Tvard[ijp][1]],Tinvresult[nres][Tvard[ijp][2]]); */
11217: }else{ /* Vn is dummy and Vm is quanti */
11218: /* fprintf(ficgp,"+p%d*%d*%f",i+j+2+nagesqr-1,nbcode[Tvard[ijp][1]][codtabm(k1,j)],Tqinvresult[nres][Tvard[ijp][2]]); */
11219: fprintf(ficgp,"+p%d*%d*%f*x",k3+(cpt-1)*ncovmodel+1+j+nagesqr,Tinvresult[nres][Tvardk[ijp][1]],Tqinvresult[nres][Tvardk[ijp][2]]);
11220: /* fprintf(ficgp,"+p%d*%d*%f*x",i+j+2+nagesqr-1,Tinvresult[nres][Tvard[ijp][1]],Tqinvresult[nres][Tvard[ijp][2]]); */
11221: }
11222: }else{ /* Vn*Vm Vn is quanti */
11223: if(DummyV[Tvardk[ijp][2]]==0){
11224: fprintf(ficgp,"+p%d*%d*%f",k3+(cpt-1)*ncovmodel+1+j+nagesqr,Tinvresult[nres][Tvardk[ijp][2]],Tqinvresult[nres][Tvardk[ijp][1]]);
11225: /* fprintf(ficgp,"+p%d*%d*%f*x",i+j+2+nagesqr-1,Tinvresult[nres][Tvard[ijp][2]],Tqinvresult[nres][Tvard[ijp][1]]); */
11226: }else{ /* Both quanti */
11227: fprintf(ficgp,"+p%d*%f*%f",k3+(cpt-1)*ncovmodel+1+j+nagesqr,Tqinvresult[nres][Tvardk[ijp][1]],Tqinvresult[nres][Tvardk[ijp][2]]);
11228: /* fprintf(ficgp,"+p%d*%f*%f*x",i+j+2+nagesqr-1,Tqinvresult[nres][Tvard[ijp][1]],Tqinvresult[nres][Tvard[ijp][2]]); */
11229: }
11230: }
11231: ijp++;
11232: }
11233: /* } /\* end Tprod *\/ */
11234: } /* end if */
11235: break;
11236: case 0:
11237: /* simple covariate */
11238: /* fprintf(ficgp,"+p%d*%d",i+j+2+nagesqr-1,nbcode[Tvar[j]][codtabm(k1,j)]); /\* Valgrind bug nbcode *\/ */
11239: if(Dummy[j]==0){
11240: /* fprintf(ficgp,"+p%d*%d",i+j+2+nagesqr-1,Tinvresult[nres][Tvar[j]]); /\* *\/ */
11241: fprintf(ficgp,"+p%d*%d",k3+(cpt-1)*ncovmodel+1+j+nagesqr,Tinvresult[nres][Tvar[j]]); /* */
11242: /* fprintf(ficgp,"+p%d*%d",i+j+2+nagesqr-1,Tinvresult[nres][Tvar[j]]); /\* *\/ */
11243: }else{ /* quantitative */
11244: fprintf(ficgp,"+p%d*%f",k3+(cpt-1)*ncovmodel+1+j+nagesqr,Tqinvresult[nres][Tvar[j]]); /* */
11245: /* fprintf(ficgp,"+p%d*%f",i+j+2+nagesqr-1,Tqinvresult[nres][Tvar[j]]); /\* *\/ */
11246: /* fprintf(ficgp,"+p%d*%d*x",i+j+nagesqr-1,nbcode[Tvar[j-2]][codtabm(k1,Tvar[j-2])]); */
11247: }
11248: /* end simple */
11249: /* fprintf(ficgp,"+p%d*%d",k3+(cpt-1)*ncovmodel+1+j-2+nagesqr,nbcode[Tvar[j-2]][codtabm(k1,j-2)]);/\* Valgrind bug nbcode *\/ */
11250: break;
11251: default:
11252: break;
11253: } /* end switch */
11254: }
11255: fprintf(ficgp,")");
11256: }
11257: fprintf(ficgp,")");
11258: if(ng ==2)
11259: 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);
11260: else /* ng= 3 */
11261: 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);
11262: }else{ /* end ng <> 1 */
11263: if( k !=k2) /* logit p11 is hard to draw */
11264: 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);
11265: }
11266: if ((k+k2)!= (nlstate*2+ndeath) && ng != 1)
11267: fprintf(ficgp,",");
11268: if (ng == 1 && k!=k2 && (k+k2)!= (nlstate*2+ndeath))
11269: fprintf(ficgp,",");
11270: i=i+ncovmodel;
11271: } /* end k */
11272: } /* end k2 */
11273: /* fprintf(ficgp,"\n set out; unset label;set key default;\n"); */
11274: fprintf(ficgp,"\n set out; unset title;set key default;\n");
11275: } /* end resultline */
11276: } /* end ng */
11277: /* avoid: */
11278: fflush(ficgp);
11279: } /* end gnuplot */
11280:
11281:
11282: /*************** Moving average **************/
11283: /* int movingaverage(double ***probs, double bage, double fage, double ***mobaverage, int mobilav, double bageout, double fageout){ */
11284: int movingaverage(double ***probs, double bage, double fage, double ***mobaverage, int mobilav){
11285:
11286: int i, cpt, cptcod;
11287: int modcovmax =1;
11288: int mobilavrange, mob;
11289: int iage=0;
11290: int firstA1=0, firstA2=0;
11291:
11292: double sum=0., sumr=0.;
11293: double age;
11294: double *sumnewp, *sumnewm, *sumnewmr;
11295: double *agemingood, *agemaxgood;
11296: double *agemingoodr, *agemaxgoodr;
11297:
11298:
11299: /* modcovmax=2*cptcoveff; Max number of modalities. We suppose */
11300: /* a covariate has 2 modalities, should be equal to ncovcombmax */
11301:
11302: sumnewp = vector(1,ncovcombmax);
11303: sumnewm = vector(1,ncovcombmax);
11304: sumnewmr = vector(1,ncovcombmax);
11305: agemingood = vector(1,ncovcombmax);
11306: agemingoodr = vector(1,ncovcombmax);
11307: agemaxgood = vector(1,ncovcombmax);
11308: agemaxgoodr = vector(1,ncovcombmax);
11309:
11310: for (cptcod=1;cptcod<=ncovcombmax;cptcod++){
11311: sumnewm[cptcod]=0.; sumnewmr[cptcod]=0.;
11312: sumnewp[cptcod]=0.;
11313: agemingood[cptcod]=0, agemingoodr[cptcod]=0;
11314: agemaxgood[cptcod]=0, agemaxgoodr[cptcod]=0;
11315: }
11316: if (cptcovn<1) ncovcombmax=1; /* At least 1 pass */
11317:
11318: if(mobilav==-1 || mobilav==1||mobilav ==3 ||mobilav==5 ||mobilav== 7){
11319: if(mobilav==1 || mobilav==-1) mobilavrange=5; /* default */
11320: else mobilavrange=mobilav;
11321: for (age=bage; age<=fage; age++)
11322: for (i=1; i<=nlstate;i++)
11323: for (cptcod=1;cptcod<=ncovcombmax;cptcod++)
11324: mobaverage[(int)age][i][cptcod]=probs[(int)age][i][cptcod];
11325: /* We keep the original values on the extreme ages bage, fage and for
11326: fage+1 and bage-1 we use a 3 terms moving average; for fage+2 bage+2
11327: we use a 5 terms etc. until the borders are no more concerned.
11328: */
11329: for (mob=3;mob <=mobilavrange;mob=mob+2){
11330: for (age=bage+(mob-1)/2; age<=fage-(mob-1)/2; age++){
11331: for (cptcod=1;cptcod<=ncovcombmax;cptcod++){
11332: sumnewm[cptcod]=0.;
11333: for (i=1; i<=nlstate;i++){
11334: mobaverage[(int)age][i][cptcod] =probs[(int)age][i][cptcod];
11335: for (cpt=1;cpt<=(mob-1)/2;cpt++){
11336: mobaverage[(int)age][i][cptcod] +=probs[(int)age-cpt][i][cptcod];
11337: mobaverage[(int)age][i][cptcod] +=probs[(int)age+cpt][i][cptcod];
11338: }
11339: mobaverage[(int)age][i][cptcod]=mobaverage[(int)age][i][cptcod]/mob;
11340: sumnewm[cptcod]+=mobaverage[(int)age][i][cptcod];
11341: } /* end i */
11342: if(sumnewm[cptcod] >1.e-3) mobaverage[(int)age][i][cptcod]=mobaverage[(int)age][i][cptcod]/sumnewm[cptcod]; /* Rescaling to sum one */
11343: } /* end cptcod */
11344: }/* end age */
11345: }/* end mob */
11346: }else{
11347: printf("Error internal in movingaverage, mobilav=%d.\n",mobilav);
11348: return -1;
11349: }
11350:
11351: for (cptcod=1;cptcod<=ncovcombmax;cptcod++){ /* for each combination */
11352: /* for (age=bage+(mob-1)/2; age<=fage-(mob-1)/2; age++){ */
11353: if(invalidvarcomb[cptcod]){
11354: printf("\nCombination (%d) ignored because no cases \n",cptcod);
11355: continue;
11356: }
11357:
11358: for (age=fage-(mob-1)/2; age>=bage+(mob-1)/2; age--){ /*looking for the youngest and oldest good age */
11359: sumnewm[cptcod]=0.;
11360: sumnewmr[cptcod]=0.;
11361: for (i=1; i<=nlstate;i++){
11362: sumnewm[cptcod]+=mobaverage[(int)age][i][cptcod];
11363: sumnewmr[cptcod]+=probs[(int)age][i][cptcod];
11364: }
11365: if(fabs(sumnewmr[cptcod] - 1.) <= 1.e-3) { /* good without smoothing */
11366: agemingoodr[cptcod]=age;
11367: }
11368: if(fabs(sumnewm[cptcod] - 1.) <= 1.e-3) { /* good */
11369: agemingood[cptcod]=age;
11370: }
11371: } /* age */
11372: for (age=bage+(mob-1)/2; age<=fage-(mob-1)/2; age++){ /*looking for the youngest and oldest good age */
11373: sumnewm[cptcod]=0.;
11374: sumnewmr[cptcod]=0.;
11375: for (i=1; i<=nlstate;i++){
11376: sumnewm[cptcod]+=mobaverage[(int)age][i][cptcod];
11377: sumnewmr[cptcod]+=probs[(int)age][i][cptcod];
11378: }
11379: if(fabs(sumnewmr[cptcod] - 1.) <= 1.e-3) { /* good without smoothing */
11380: agemaxgoodr[cptcod]=age;
11381: }
11382: if(fabs(sumnewm[cptcod] - 1.) <= 1.e-3) { /* good */
11383: agemaxgood[cptcod]=age;
11384: }
11385: } /* age */
11386: /* Thus we have agemingood and agemaxgood as well as goodr for raw (preobs) */
11387: /* but they will change */
11388: firstA1=0;firstA2=0;
11389: for (age=fage-(mob-1)/2; age>=bage; age--){/* From oldest to youngest, filling up to the youngest */
11390: sumnewm[cptcod]=0.;
11391: sumnewmr[cptcod]=0.;
11392: for (i=1; i<=nlstate;i++){
11393: sumnewm[cptcod]+=mobaverage[(int)age][i][cptcod];
11394: sumnewmr[cptcod]+=probs[(int)age][i][cptcod];
11395: }
11396: if(mobilav==-1){ /* Forcing raw ages if good else agemingood */
11397: if(fabs(sumnewmr[cptcod] - 1.) <= 1.e-3) { /* good without smoothing */
11398: agemaxgoodr[cptcod]=age; /* age min */
11399: for (i=1; i<=nlstate;i++)
11400: mobaverage[(int)age][i][cptcod]=probs[(int)age][i][cptcod];
11401: }else{ /* bad we change the value with the values of good ages */
11402: for (i=1; i<=nlstate;i++){
11403: mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemaxgoodr[cptcod]][i][cptcod];
11404: } /* i */
11405: } /* end bad */
11406: }else{
11407: if(fabs(sumnewm[cptcod] - 1.) <= 1.e-3) { /* good */
11408: agemaxgood[cptcod]=age;
11409: }else{ /* bad we change the value with the values of good ages */
11410: for (i=1; i<=nlstate;i++){
11411: mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemaxgood[cptcod]][i][cptcod];
11412: } /* i */
11413: } /* end bad */
11414: }/* end else */
11415: sum=0.;sumr=0.;
11416: for (i=1; i<=nlstate;i++){
11417: sum+=mobaverage[(int)age][i][cptcod];
11418: sumr+=probs[(int)age][i][cptcod];
11419: }
11420: if(fabs(sum - 1.) > 1.e-3) { /* bad */
11421: if(!firstA1){
11422: firstA1=1;
11423: 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);
11424: }
11425: 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);
11426: } /* end bad */
11427: /* else{ /\* We found some ages summing to one, we will smooth the oldest *\/ */
11428: if(fabs(sumr - 1.) > 1.e-3) { /* bad */
11429: if(!firstA2){
11430: firstA2=1;
11431: 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);
11432: }
11433: 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);
11434: } /* end bad */
11435: }/* age */
11436:
11437: for (age=bage+(mob-1)/2; age<=fage; age++){/* From youngest, finding the oldest wrong */
11438: sumnewm[cptcod]=0.;
11439: sumnewmr[cptcod]=0.;
11440: for (i=1; i<=nlstate;i++){
11441: sumnewm[cptcod]+=mobaverage[(int)age][i][cptcod];
11442: sumnewmr[cptcod]+=probs[(int)age][i][cptcod];
11443: }
11444: if(mobilav==-1){ /* Forcing raw ages if good else agemingood */
11445: if(fabs(sumnewmr[cptcod] - 1.) <= 1.e-3) { /* good */
11446: agemingoodr[cptcod]=age;
11447: for (i=1; i<=nlstate;i++)
11448: mobaverage[(int)age][i][cptcod]=probs[(int)age][i][cptcod];
11449: }else{ /* bad we change the value with the values of good ages */
11450: for (i=1; i<=nlstate;i++){
11451: mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemingoodr[cptcod]][i][cptcod];
11452: } /* i */
11453: } /* end bad */
11454: }else{
11455: if(fabs(sumnewm[cptcod] - 1.) <= 1.e-3) { /* good */
11456: agemingood[cptcod]=age;
11457: }else{ /* bad */
11458: for (i=1; i<=nlstate;i++){
11459: mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemingood[cptcod]][i][cptcod];
11460: } /* i */
11461: } /* end bad */
11462: }/* end else */
11463: sum=0.;sumr=0.;
11464: for (i=1; i<=nlstate;i++){
11465: sum+=mobaverage[(int)age][i][cptcod];
11466: sumr+=mobaverage[(int)age][i][cptcod];
11467: }
11468: if(fabs(sum - 1.) > 1.e-3) { /* bad */
11469: 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);
11470: } /* end bad */
11471: /* else{ /\* We found some ages summing to one, we will smooth the oldest *\/ */
11472: if(fabs(sumr - 1.) > 1.e-3) { /* bad */
11473: 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);
11474: } /* end bad */
11475: }/* age */
11476:
11477:
11478: for (age=bage; age<=fage; age++){
11479: /* printf("%d %d ", cptcod, (int)age); */
11480: sumnewp[cptcod]=0.;
11481: sumnewm[cptcod]=0.;
11482: for (i=1; i<=nlstate;i++){
11483: sumnewp[cptcod]+=probs[(int)age][i][cptcod];
11484: sumnewm[cptcod]+=mobaverage[(int)age][i][cptcod];
11485: /* printf("%.4f %.4f ",probs[(int)age][i][cptcod], mobaverage[(int)age][i][cptcod]); */
11486: }
11487: /* printf("%.4f %.4f \n",sumnewp[cptcod], sumnewm[cptcod]); */
11488: }
11489: /* printf("\n"); */
11490: /* } */
11491:
11492: /* brutal averaging */
11493: /* for (i=1; i<=nlstate;i++){ */
11494: /* for (age=1; age<=bage; age++){ */
11495: /* mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemingood[cptcod]][i][cptcod]; */
11496: /* /\* printf("age=%d i=%d cptcod=%d mobaverage=%.4f \n",(int)age,i, cptcod, mobaverage[(int)age][i][cptcod]); *\/ */
11497: /* } */
11498: /* for (age=fage; age<=AGESUP; age++){ */
11499: /* mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemaxgood[cptcod]][i][cptcod]; */
11500: /* /\* printf("age=%d i=%d cptcod=%d mobaverage=%.4f \n",(int)age,i, cptcod, mobaverage[(int)age][i][cptcod]); *\/ */
11501: /* } */
11502: /* } /\* end i status *\/ */
11503: /* for (i=nlstate+1; i<=nlstate+ndeath;i++){ */
11504: /* for (age=1; age<=AGESUP; age++){ */
11505: /* /\*printf("i=%d, age=%d, cptcod=%d\n",i, (int)age, cptcod);*\/ */
11506: /* mobaverage[(int)age][i][cptcod]=0.; */
11507: /* } */
11508: /* } */
11509: }/* end cptcod */
11510: free_vector(agemaxgoodr,1, ncovcombmax);
11511: free_vector(agemaxgood,1, ncovcombmax);
11512: free_vector(agemingood,1, ncovcombmax);
11513: free_vector(agemingoodr,1, ncovcombmax);
11514: free_vector(sumnewmr,1, ncovcombmax);
11515: free_vector(sumnewm,1, ncovcombmax);
11516: free_vector(sumnewp,1, ncovcombmax);
11517: return 0;
11518: }/* End movingaverage */
11519:
11520:
11521:
11522: /************** Forecasting ******************/
11523: /* 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)*/
11524: 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){
11525: /* dateintemean, mean date of interviews
11526: dateprojd, year, month, day of starting projection
11527: dateprojf date of end of projection;year of end of projection (same day and month as proj1).
11528: agemin, agemax range of age
11529: dateprev1 dateprev2 range of dates during which prevalence is computed
11530: */
11531: /* double anprojd, mprojd, jprojd; */
11532: /* double anprojf, mprojf, jprojf; */
11533: int yearp, stepsize, hstepm, nhstepm, j, k, i, h, nres=0;
11534: double agec; /* generic age */
11535: double agelim, ppij;
11536: /*double *popcount;*/
11537: double ***p3mat;
11538: /* double ***mobaverage; */
11539: char fileresf[FILENAMELENGTH];
11540:
11541: agelim=AGESUP;
11542: /* Compute observed prevalence between dateprev1 and dateprev2 by counting the number of people
11543: in each health status at the date of interview (if between dateprev1 and dateprev2).
11544: We still use firstpass and lastpass as another selection.
11545: */
11546: /* freqsummary(fileres, agemin, agemax, s, agev, nlstate, imx,Tvaraff,nbcode, ncodemax,mint,anint,strstart,\ */
11547: /* firstpass, lastpass, stepm, weightopt, model); */
11548:
11549: strcpy(fileresf,"F_");
11550: strcat(fileresf,fileresu);
11551: if((ficresf=fopen(fileresf,"w"))==NULL) {
11552: printf("Problem with forecast resultfile: %s\n", fileresf);
11553: fprintf(ficlog,"Problem with forecast resultfile: %s\n", fileresf);
11554: }
11555: printf("\nComputing forecasting: result on file '%s', please wait... \n", fileresf);
11556: fprintf(ficlog,"\nComputing forecasting: result on file '%s', please wait... \n", fileresf);
11557:
11558: if (cptcoveff==0) ncodemax[cptcoveff]=1;
11559:
11560:
11561: stepsize=(int) (stepm+YEARM-1)/YEARM;
11562: if (stepm<=12) stepsize=1;
11563: if(estepm < stepm){
11564: printf ("Problem %d lower than %d\n",estepm, stepm);
11565: }
11566: else{
11567: hstepm=estepm;
11568: }
11569: if(estepm > stepm){ /* Yes every two year */
11570: stepsize=2;
11571: }
11572: hstepm=hstepm/stepm;
11573:
11574:
11575: /* yp1=modf(dateintmean,&yp);/\* extracts integral of datemean in yp and */
11576: /* fractional in yp1 *\/ */
11577: /* aintmean=yp; */
11578: /* yp2=modf((yp1*12),&yp); */
11579: /* mintmean=yp; */
11580: /* yp1=modf((yp2*30.5),&yp); */
11581: /* jintmean=yp; */
11582: /* if(jintmean==0) jintmean=1; */
11583: /* if(mintmean==0) mintmean=1; */
11584:
11585:
11586: /* date2dmy(dateintmean,&jintmean,&mintmean,&aintmean); */
11587: /* date2dmy(dateprojd,&jprojd, &mprojd, &anprojd); */
11588: /* date2dmy(dateprojf,&jprojf, &mprojf, &anprojf); */
11589: /* i1=pow(2,cptcoveff); */
11590: /* if (cptcovn < 1){i1=1;} */
11591:
11592: fprintf(ficresf,"# Mean day of interviews %.lf/%.lf/%.lf (%.2f) between %.2f and %.2f \n",jintmean,mintmean,aintmean,dateintmean,dateprev1,dateprev2);
11593:
11594: fprintf(ficresf,"#****** Routine prevforecast **\n");
11595:
11596: /* if (h==(int)(YEARM*yearp)){ */
11597: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
11598: k=TKresult[nres];
11599: if(TKresult[nres]==0) k=1; /* To be checked for noresult */
11600: /* 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) *\/ */
11601: /* if(i1 != 1 && TKresult[nres]!= k) */
11602: /* continue; */
11603: /* if(invalidvarcomb[k]){ */
11604: /* printf("\nCombination (%d) projection ignored because no cases \n",k); */
11605: /* continue; */
11606: /* } */
11607: fprintf(ficresf,"\n#****** hpijx=probability over h years, hp.jx is weighted by observed prev \n#");
11608: for(j=1;j<=cptcovs;j++){
11609: /* for(j=1;j<=cptcoveff;j++) { */
11610: /* /\* fprintf(ficresf," V%d (=) %d",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,Tvaraff[j])]); *\/ */
11611: /* fprintf(ficresf," V%d (=) %d",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
11612: /* } */
11613: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
11614: /* fprintf(ficresf," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); */
11615: /* } */
11616: fprintf(ficresf," V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
11617: }
11618:
11619: fprintf(ficresf," yearproj age");
11620: for(j=1; j<=nlstate+ndeath;j++){
11621: for(i=1; i<=nlstate;i++)
11622: fprintf(ficresf," p%d%d",i,j);
11623: fprintf(ficresf," wp.%d",j);
11624: }
11625: for (yearp=0; yearp<=(anprojf-anprojd);yearp +=stepsize) {
11626: fprintf(ficresf,"\n");
11627: fprintf(ficresf,"\n# Forecasting at date %.lf/%.lf/%.lf ",jprojd,mprojd,anprojd+yearp);
11628: /* for (agec=fage; agec>=(ageminpar-1); agec--){ */
11629: for (agec=fage; agec>=(bage); agec--){
11630: nhstepm=(int) rint((agelim-agec)*YEARM/stepm);
11631: nhstepm = nhstepm/hstepm;
11632: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
11633: oldm=oldms;savm=savms;
11634: /* We compute pii at age agec over nhstepm);*/
11635: hpxij(p3mat,nhstepm,agec,hstepm,p,nlstate,stepm,oldm,savm, k,nres);
11636: /* Then we print p3mat for h corresponding to the right agec+h*stepms=yearp */
11637: for (h=0; h<=nhstepm; h++){
11638: if (h*hstepm/YEARM*stepm ==yearp) {
11639: break;
11640: }
11641: }
11642: fprintf(ficresf,"\n");
11643: /* for(j=1;j<=cptcoveff;j++) */
11644: for(j=1;j<=cptcovs;j++)
11645: fprintf(ficresf,"%d %lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
11646: /* fprintf(ficresf,"%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,Tvaraff[j])]); /\* Tvaraff not correct *\/ */
11647: /* fprintf(ficresf,"%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); /\* TnsdVar[Tvaraff] correct *\/ */
11648: fprintf(ficresf,"%.f %.f ",anprojd+yearp,agec+h*hstepm/YEARM*stepm);
11649:
11650: for(j=1; j<=nlstate+ndeath;j++) {
11651: ppij=0.;
11652: for(i=1; i<=nlstate;i++) {
11653: if (mobilav>=1)
11654: ppij=ppij+p3mat[i][j][h]*prev[(int)agec][i][k];
11655: else { /* even if mobilav==-1 we use mobaverage, probs may not sums to 1 */
11656: ppij=ppij+p3mat[i][j][h]*probs[(int)(agec)][i][k];
11657: }
11658: fprintf(ficresf," %.3f", p3mat[i][j][h]);
11659: } /* end i */
11660: fprintf(ficresf," %.3f", ppij);
11661: }/* end j */
11662: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
11663: } /* end agec */
11664: /* diffyear=(int) anproj1+yearp-ageminpar-1; */
11665: /*printf("Prevforecast %d+%d-%d=diffyear=%d\n",(int) anproj1, (int)yearp,(int)ageminpar,(int) anproj1-(int)ageminpar);*/
11666: } /* end yearp */
11667: } /* end k */
11668:
11669: fclose(ficresf);
11670: printf("End of Computing forecasting \n");
11671: fprintf(ficlog,"End of Computing forecasting\n");
11672:
11673: }
11674:
11675: /************** Back Forecasting ******************/
11676: /* 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){ */
11677: 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){
11678: /* back1, year, month, day of starting backprojection
11679: agemin, agemax range of age
11680: dateprev1 dateprev2 range of dates during which prevalence is computed
11681: anback2 year of end of backprojection (same day and month as back1).
11682: prevacurrent and prev are prevalences.
11683: */
11684: int yearp, stepsize, hstepm, nhstepm, j, k, i, h, nres=0;
11685: double agec; /* generic age */
11686: double agelim, ppij, ppi; /* ,jintmean,mintmean,aintmean;*/
11687: /*double *popcount;*/
11688: double ***p3mat;
11689: /* double ***mobaverage; */
11690: char fileresfb[FILENAMELENGTH];
11691:
11692: agelim=AGEINF;
11693: /* Compute observed prevalence between dateprev1 and dateprev2 by counting the number of people
11694: in each health status at the date of interview (if between dateprev1 and dateprev2).
11695: We still use firstpass and lastpass as another selection.
11696: */
11697: /* freqsummary(fileres, agemin, agemax, s, agev, nlstate, imx,Tvaraff,nbcode, ncodemax,mint,anint,strstart,\ */
11698: /* firstpass, lastpass, stepm, weightopt, model); */
11699:
11700: /*Do we need to compute prevalence again?*/
11701:
11702: /* prevalence(probs, ageminpar, agemax, s, agev, nlstate, imx, Tvar, nbcode, ncodemax, mint, anint, dateprev1, dateprev2, firstpass, lastpass); */
11703:
11704: strcpy(fileresfb,"FB_");
11705: strcat(fileresfb,fileresu);
11706: if((ficresfb=fopen(fileresfb,"w"))==NULL) {
11707: printf("Problem with back forecast resultfile: %s\n", fileresfb);
11708: fprintf(ficlog,"Problem with back forecast resultfile: %s\n", fileresfb);
11709: }
11710: printf("\nComputing back forecasting: result on file '%s', please wait... \n", fileresfb);
11711: fprintf(ficlog,"\nComputing back forecasting: result on file '%s', please wait... \n", fileresfb);
11712:
11713: if (cptcoveff==0) ncodemax[cptcoveff]=1;
11714:
11715:
11716: stepsize=(int) (stepm+YEARM-1)/YEARM;
11717: if (stepm<=12) stepsize=1;
11718: if(estepm < stepm){
11719: printf ("Problem %d lower than %d\n",estepm, stepm);
11720: }
11721: else{
11722: hstepm=estepm;
11723: }
11724: if(estepm >= stepm){ /* Yes every two year */
11725: stepsize=2;
11726: }
11727:
11728: hstepm=hstepm/stepm;
11729: /* yp1=modf(dateintmean,&yp);/\* extracts integral of datemean in yp and */
11730: /* fractional in yp1 *\/ */
11731: /* aintmean=yp; */
11732: /* yp2=modf((yp1*12),&yp); */
11733: /* mintmean=yp; */
11734: /* yp1=modf((yp2*30.5),&yp); */
11735: /* jintmean=yp; */
11736: /* if(jintmean==0) jintmean=1; */
11737: /* if(mintmean==0) jintmean=1; */
11738:
11739: /* i1=pow(2,cptcoveff); */
11740: /* if (cptcovn < 1){i1=1;} */
11741:
11742: fprintf(ficresfb,"# Mean day of interviews %.lf/%.lf/%.lf (%.2f) between %.2f and %.2f \n",jintmean,mintmean,aintmean,dateintmean,dateprev1,dateprev2);
11743: printf("# Mean day of interviews %.lf/%.lf/%.lf (%.2f) between %.2f and %.2f \n",jintmean,mintmean,aintmean,dateintmean,dateprev1,dateprev2);
11744:
11745: fprintf(ficresfb,"#****** Routine prevbackforecast **\n");
11746:
11747: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
11748: k=TKresult[nres];
11749: if(TKresult[nres]==0) k=1; /* To be checked for noresult */
11750: /* for(k=1; k<=i1;k++){ */
11751: /* if(i1 != 1 && TKresult[nres]!= k) */
11752: /* continue; */
11753: /* if(invalidvarcomb[k]){ */
11754: /* printf("\nCombination (%d) projection ignored because no cases \n",k); */
11755: /* continue; */
11756: /* } */
11757: fprintf(ficresfb,"\n#****** hbijx=probability over h years, hb.jx is weighted by observed prev \n#");
11758: for(j=1;j<=cptcovs;j++){
11759: /* for(j=1;j<=cptcoveff;j++) { */
11760: /* fprintf(ficresfb," V%d (=) %d",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
11761: /* } */
11762: fprintf(ficresfb," V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
11763: }
11764: /* fprintf(ficrespij,"******\n"); */
11765: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
11766: /* fprintf(ficresfb," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); */
11767: /* } */
11768: fprintf(ficresfb," yearbproj age");
11769: for(j=1; j<=nlstate+ndeath;j++){
11770: for(i=1; i<=nlstate;i++)
11771: fprintf(ficresfb," b%d%d",i,j);
11772: fprintf(ficresfb," b.%d",j);
11773: }
11774: for (yearp=0; yearp>=(anbackf-anbackd);yearp -=stepsize) {
11775: /* for (yearp=0; yearp<=(anproj2-anproj1);yearp +=stepsize) { */
11776: fprintf(ficresfb,"\n");
11777: fprintf(ficresfb,"\n# Back Forecasting at date %.lf/%.lf/%.lf ",jbackd,mbackd,anbackd+yearp);
11778: /* printf("\n# Back Forecasting at date %.lf/%.lf/%.lf ",jback1,mback1,anback1+yearp); */
11779: /* for (agec=bage; agec<=agemax-1; agec++){ /\* testing *\/ */
11780: for (agec=bage; agec<=fage; agec++){ /* testing */
11781: /* We compute bij at age agec over nhstepm, nhstepm decreases when agec increases because of agemax;*/
11782: nhstepm=(int) (agec-agelim) *YEARM/stepm;/* nhstepm=(int) rint((agec-agelim)*YEARM/stepm);*/
11783: nhstepm = nhstepm/hstepm;
11784: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
11785: oldm=oldms;savm=savms;
11786: /* computes hbxij at age agec over 1 to nhstepm */
11787: /* printf("####prevbackforecast debug agec=%.2f nhstepm=%d\n",agec, nhstepm);fflush(stdout); */
11788: hbxij(p3mat,nhstepm,agec,hstepm,p,prevacurrent,nlstate,stepm, k, nres);
11789: /* hpxij(p3mat,nhstepm,agec,hstepm,p, nlstate,stepm,oldm,savm, k,nres); */
11790: /* Then we print p3mat for h corresponding to the right agec+h*stepms=yearp */
11791: /* printf(" agec=%.2f\n",agec);fflush(stdout); */
11792: for (h=0; h<=nhstepm; h++){
11793: if (h*hstepm/YEARM*stepm ==-yearp) {
11794: break;
11795: }
11796: }
11797: fprintf(ficresfb,"\n");
11798: /* for(j=1;j<=cptcoveff;j++) */
11799: for(j=1;j<=cptcovs;j++)
11800: fprintf(ficresfb,"%d %lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
11801: /* fprintf(ficresfb,"%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
11802: fprintf(ficresfb,"%.f %.f ",anbackd+yearp,agec-h*hstepm/YEARM*stepm);
11803: for(i=1; i<=nlstate+ndeath;i++) {
11804: ppij=0.;ppi=0.;
11805: for(j=1; j<=nlstate;j++) {
11806: /* if (mobilav==1) */
11807: ppij=ppij+p3mat[i][j][h]*prevacurrent[(int)agec][j][k];
11808: ppi=ppi+prevacurrent[(int)agec][j][k];
11809: /* ppij=ppij+p3mat[i][j][h]*mobaverage[(int)agec][j][k]; */
11810: /* ppi=ppi+mobaverage[(int)agec][j][k]; */
11811: /* else { */
11812: /* ppij=ppij+p3mat[i][j][h]*probs[(int)(agec)][i][k]; */
11813: /* } */
11814: fprintf(ficresfb," %.3f", p3mat[i][j][h]);
11815: } /* end j */
11816: if(ppi <0.99){
11817: printf("Error in prevbackforecast, prevalence doesn't sum to 1 for state %d: %3f\n",i, ppi);
11818: fprintf(ficlog,"Error in prevbackforecast, prevalence doesn't sum to 1 for state %d: %3f\n",i, ppi);
11819: }
11820: fprintf(ficresfb," %.3f", ppij);
11821: }/* end j */
11822: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
11823: } /* end agec */
11824: } /* end yearp */
11825: } /* end k */
11826:
11827: /* if (mobilav!=0) free_ma3x(mobaverage,1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
11828:
11829: fclose(ficresfb);
11830: printf("End of Computing Back forecasting \n");
11831: fprintf(ficlog,"End of Computing Back forecasting\n");
11832:
11833: }
11834:
11835: /* Variance of prevalence limit: varprlim */
11836: 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){
11837: /*------- Variance of forward period (stable) prevalence------*/
11838:
11839: char fileresvpl[FILENAMELENGTH];
11840: FILE *ficresvpl;
11841: double **oldm, **savm;
11842: double **varpl; /* Variances of prevalence limits by age */
11843: int i1, k, nres, j ;
11844:
11845: strcpy(fileresvpl,"VPL_");
11846: strcat(fileresvpl,fileresu);
11847: if((ficresvpl=fopen(fileresvpl,"w"))==NULL) {
11848: printf("Problem with variance of forward period (stable) prevalence resultfile: %s\n", fileresvpl);
11849: exit(0);
11850: }
11851: printf("Computing Variance-covariance of forward period (stable) prevalence: file '%s' ...", fileresvpl);fflush(stdout);
11852: fprintf(ficlog, "Computing Variance-covariance of forward period (stable) prevalence: file '%s' ...", fileresvpl);fflush(ficlog);
11853:
11854: /*for(cptcov=1,k=0;cptcov<=i1;cptcov++){
11855: for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){*/
11856:
11857: i1=pow(2,cptcoveff);
11858: if (cptcovn < 1){i1=1;}
11859:
11860: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
11861: k=TKresult[nres];
11862: if(TKresult[nres]==0) k=1; /* To be checked for noresult */
11863: /* for(k=1; k<=i1;k++){ /\* We find the combination equivalent to result line values of dummies *\/ */
11864: if(i1 != 1 && TKresult[nres]!= k)
11865: continue;
11866: fprintf(ficresvpl,"\n#****** ");
11867: printf("\n#****** ");
11868: fprintf(ficlog,"\n#****** ");
11869: for(j=1;j<=cptcovs;j++) {
11870: fprintf(ficresvpl,"V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
11871: fprintf(ficlog,"V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
11872: printf("V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
11873: /* fprintf(ficlog,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
11874: /* printf("V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
11875: }
11876: /* for (j=1; j<= nsq; j++){ /\* For each selected (single) quantitative value *\/ */
11877: /* printf(" V%d=%f ",Tvqresult[nres][j],Tqresult[nres][resultmodel[nres][j]]); */
11878: /* fprintf(ficresvpl," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][resultmodel[nres][j]]); */
11879: /* fprintf(ficlog," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][resultmodel[nres][j]]); */
11880: /* } */
11881: fprintf(ficresvpl,"******\n");
11882: printf("******\n");
11883: fprintf(ficlog,"******\n");
11884:
11885: varpl=matrix(1,nlstate,(int) bage, (int) fage);
11886: oldm=oldms;savm=savms;
11887: varprevlim(fileresvpl, ficresvpl, varpl, matcov, p, delti, nlstate, stepm, (int) bage, (int) fage, oldm, savm, prlim, ftolpl, ncvyearp, k, strstart, nres);
11888: free_matrix(varpl,1,nlstate,(int) bage, (int)fage);
11889: /*}*/
11890: }
11891:
11892: fclose(ficresvpl);
11893: printf("done variance-covariance of forward period prevalence\n");fflush(stdout);
11894: fprintf(ficlog,"done variance-covariance of forward period prevalence\n");fflush(ficlog);
11895:
11896: }
11897: /* Variance of back prevalence: varbprlim */
11898: 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){
11899: /*------- Variance of back (stable) prevalence------*/
11900:
11901: char fileresvbl[FILENAMELENGTH];
11902: FILE *ficresvbl;
11903:
11904: double **oldm, **savm;
11905: double **varbpl; /* Variances of back prevalence limits by age */
11906: int i1, k, nres, j ;
11907:
11908: strcpy(fileresvbl,"VBL_");
11909: strcat(fileresvbl,fileresu);
11910: if((ficresvbl=fopen(fileresvbl,"w"))==NULL) {
11911: printf("Problem with variance of back (stable) prevalence resultfile: %s\n", fileresvbl);
11912: exit(0);
11913: }
11914: printf("Computing Variance-covariance of back (stable) prevalence: file '%s' ...", fileresvbl);fflush(stdout);
11915: fprintf(ficlog, "Computing Variance-covariance of back (stable) prevalence: file '%s' ...", fileresvbl);fflush(ficlog);
11916:
11917:
11918: i1=pow(2,cptcoveff);
11919: if (cptcovn < 1){i1=1;}
11920:
11921: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
11922: k=TKresult[nres];
11923: if(TKresult[nres]==0) k=1; /* To be checked for noresult */
11924: /* for(k=1; k<=i1;k++){ */
11925: /* if(i1 != 1 && TKresult[nres]!= k) */
11926: /* continue; */
11927: fprintf(ficresvbl,"\n#****** ");
11928: printf("\n#****** ");
11929: fprintf(ficlog,"\n#****** ");
11930: for (j=1; j<= cptcovs; j++){ /* For each selected (single) quantitative value */
11931: printf(" V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][resultmodel[nres][j]]);
11932: fprintf(ficresvbl," V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][resultmodel[nres][j]]);
11933: fprintf(ficlog," V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][resultmodel[nres][j]]);
11934: /* for(j=1;j<=cptcoveff;j++) { */
11935: /* fprintf(ficresvbl,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
11936: /* fprintf(ficlog,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
11937: /* printf("V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
11938: /* } */
11939: /* for (j=1; j<= nsq; j++){ /\* For each selected (single) quantitative value *\/ */
11940: /* printf(" V%d=%f ",Tvqresult[nres][j],Tqresult[nres][resultmodel[nres][j]]); */
11941: /* fprintf(ficresvbl," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][resultmodel[nres][j]]); */
11942: /* fprintf(ficlog," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][resultmodel[nres][j]]); */
11943: }
11944: fprintf(ficresvbl,"******\n");
11945: printf("******\n");
11946: fprintf(ficlog,"******\n");
11947:
11948: varbpl=matrix(1,nlstate,(int) bage, (int) fage);
11949: oldm=oldms;savm=savms;
11950:
11951: varbrevlim(fileresvbl, ficresvbl, varbpl, matcov, p, delti, nlstate, stepm, (int) bage, (int) fage, oldm, savm, bprlim, ftolpl, mobilavproj, ncvyearp, k, strstart, nres);
11952: free_matrix(varbpl,1,nlstate,(int) bage, (int)fage);
11953: /*}*/
11954: }
11955:
11956: fclose(ficresvbl);
11957: printf("done variance-covariance of back prevalence\n");fflush(stdout);
11958: fprintf(ficlog,"done variance-covariance of back prevalence\n");fflush(ficlog);
11959:
11960: } /* End of varbprlim */
11961:
11962: /************** Forecasting *****not tested NB*************/
11963: /* 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){ */
11964:
11965: /* int cpt, stepsize, hstepm, nhstepm, j,k,c, cptcod, i,h; */
11966: /* int *popage; */
11967: /* double calagedatem, agelim, kk1, kk2; */
11968: /* double *popeffectif,*popcount; */
11969: /* double ***p3mat,***tabpop,***tabpopprev; */
11970: /* /\* double ***mobaverage; *\/ */
11971: /* char filerespop[FILENAMELENGTH]; */
11972:
11973: /* tabpop= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
11974: /* tabpopprev= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
11975: /* agelim=AGESUP; */
11976: /* calagedatem=(anpyram+mpyram/12.+jpyram/365.-dateintmean)*YEARM; */
11977:
11978: /* prevalence(probs, ageminpar, agemax, s, agev, nlstate, imx, Tvar, nbcode, ncodemax, mint, anint, dateprev1, dateprev2, firstpass, lastpass); */
11979:
11980:
11981: /* strcpy(filerespop,"POP_"); */
11982: /* strcat(filerespop,fileresu); */
11983: /* if((ficrespop=fopen(filerespop,"w"))==NULL) { */
11984: /* printf("Problem with forecast resultfile: %s\n", filerespop); */
11985: /* fprintf(ficlog,"Problem with forecast resultfile: %s\n", filerespop); */
11986: /* } */
11987: /* printf("Computing forecasting: result on file '%s' \n", filerespop); */
11988: /* fprintf(ficlog,"Computing forecasting: result on file '%s' \n", filerespop); */
11989:
11990: /* if (cptcoveff==0) ncodemax[cptcoveff]=1; */
11991:
11992: /* /\* if (mobilav!=0) { *\/ */
11993: /* /\* mobaverage= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX); *\/ */
11994: /* /\* if (movingaverage(probs, ageminpar, fage, mobaverage,mobilav)!=0){ *\/ */
11995: /* /\* fprintf(ficlog," Error in movingaverage mobilav=%d\n",mobilav); *\/ */
11996: /* /\* printf(" Error in movingaverage mobilav=%d\n",mobilav); *\/ */
11997: /* /\* } *\/ */
11998: /* /\* } *\/ */
11999:
12000: /* stepsize=(int) (stepm+YEARM-1)/YEARM; */
12001: /* if (stepm<=12) stepsize=1; */
12002:
12003: /* agelim=AGESUP; */
12004:
12005: /* hstepm=1; */
12006: /* hstepm=hstepm/stepm; */
12007:
12008: /* if (popforecast==1) { */
12009: /* if((ficpop=fopen(popfile,"r"))==NULL) { */
12010: /* printf("Problem with population file : %s\n",popfile);exit(0); */
12011: /* fprintf(ficlog,"Problem with population file : %s\n",popfile);exit(0); */
12012: /* } */
12013: /* popage=ivector(0,AGESUP); */
12014: /* popeffectif=vector(0,AGESUP); */
12015: /* popcount=vector(0,AGESUP); */
12016:
12017: /* i=1; */
12018: /* while ((c=fscanf(ficpop,"%d %lf\n",&popage[i],&popcount[i])) != EOF) i=i+1; */
12019:
12020: /* imx=i; */
12021: /* for (i=1; i<imx;i++) popeffectif[popage[i]]=popcount[i]; */
12022: /* } */
12023:
12024: /* for(cptcov=1,k=0;cptcov<=i2;cptcov++){ */
12025: /* for(cptcod=1;cptcod<=ncodemax[cptcoveff];cptcod++){ */
12026: /* k=k+1; */
12027: /* fprintf(ficrespop,"\n#******"); */
12028: /* for(j=1;j<=cptcoveff;j++) { */
12029: /* fprintf(ficrespop," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]); */
12030: /* } */
12031: /* fprintf(ficrespop,"******\n"); */
12032: /* fprintf(ficrespop,"# Age"); */
12033: /* for(j=1; j<=nlstate+ndeath;j++) fprintf(ficrespop," P.%d",j); */
12034: /* if (popforecast==1) fprintf(ficrespop," [Population]"); */
12035:
12036: /* for (cpt=0; cpt<=0;cpt++) { */
12037: /* fprintf(ficrespop,"\n\n# Forecasting at date %.lf/%.lf/%.lf ",jpyram,mpyram,anpyram+cpt); */
12038:
12039: /* for (agedeb=(fage-((int)calagedatem %12/12.)); agedeb>=(ageminpar-((int)calagedatem %12)/12.); agedeb--){ */
12040: /* nhstepm=(int) rint((agelim-agedeb)*YEARM/stepm); */
12041: /* nhstepm = nhstepm/hstepm; */
12042:
12043: /* p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); */
12044: /* oldm=oldms;savm=savms; */
12045: /* hpxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k); */
12046:
12047: /* for (h=0; h<=nhstepm; h++){ */
12048: /* if (h==(int) (calagedatem+YEARM*cpt)) { */
12049: /* fprintf(ficrespop,"\n %3.f ",agedeb+h*hstepm/YEARM*stepm); */
12050: /* } */
12051: /* for(j=1; j<=nlstate+ndeath;j++) { */
12052: /* kk1=0.;kk2=0; */
12053: /* for(i=1; i<=nlstate;i++) { */
12054: /* if (mobilav==1) */
12055: /* kk1=kk1+p3mat[i][j][h]*mobaverage[(int)agedeb+1][i][cptcod]; */
12056: /* else { */
12057: /* kk1=kk1+p3mat[i][j][h]*probs[(int)(agedeb+1)][i][cptcod]; */
12058: /* } */
12059: /* } */
12060: /* if (h==(int)(calagedatem+12*cpt)){ */
12061: /* tabpop[(int)(agedeb)][j][cptcod]=kk1; */
12062: /* /\*fprintf(ficrespop," %.3f", kk1); */
12063: /* if (popforecast==1) fprintf(ficrespop," [%.f]", kk1*popeffectif[(int)agedeb+1]);*\/ */
12064: /* } */
12065: /* } */
12066: /* for(i=1; i<=nlstate;i++){ */
12067: /* kk1=0.; */
12068: /* for(j=1; j<=nlstate;j++){ */
12069: /* kk1= kk1+tabpop[(int)(agedeb)][j][cptcod]; */
12070: /* } */
12071: /* tabpopprev[(int)(agedeb)][i][cptcod]=tabpop[(int)(agedeb)][i][cptcod]/kk1*popeffectif[(int)(agedeb+(calagedatem+12*cpt)*hstepm/YEARM*stepm-1)]; */
12072: /* } */
12073:
12074: /* if (h==(int)(calagedatem+12*cpt)) */
12075: /* for(j=1; j<=nlstate;j++) */
12076: /* fprintf(ficrespop," %15.2f",tabpopprev[(int)(agedeb+1)][j][cptcod]); */
12077: /* } */
12078: /* free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); */
12079: /* } */
12080: /* } */
12081:
12082: /* /\******\/ */
12083:
12084: /* for (cpt=1; cpt<=(anpyram1-anpyram);cpt++) { */
12085: /* fprintf(ficrespop,"\n\n# Forecasting at date %.lf/%.lf/%.lf ",jpyram,mpyram,anpyram+cpt); */
12086: /* for (agedeb=(fage-((int)calagedatem %12/12.)); agedeb>=(ageminpar-((int)calagedatem %12)/12.); agedeb--){ */
12087: /* nhstepm=(int) rint((agelim-agedeb)*YEARM/stepm); */
12088: /* nhstepm = nhstepm/hstepm; */
12089:
12090: /* p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); */
12091: /* oldm=oldms;savm=savms; */
12092: /* hpxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k); */
12093: /* for (h=0; h<=nhstepm; h++){ */
12094: /* if (h==(int) (calagedatem+YEARM*cpt)) { */
12095: /* fprintf(ficresf,"\n %3.f ",agedeb+h*hstepm/YEARM*stepm); */
12096: /* } */
12097: /* for(j=1; j<=nlstate+ndeath;j++) { */
12098: /* kk1=0.;kk2=0; */
12099: /* for(i=1; i<=nlstate;i++) { */
12100: /* kk1=kk1+p3mat[i][j][h]*tabpopprev[(int)agedeb+1][i][cptcod]; */
12101: /* } */
12102: /* if (h==(int)(calagedatem+12*cpt)) fprintf(ficresf," %15.2f", kk1); */
12103: /* } */
12104: /* } */
12105: /* free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); */
12106: /* } */
12107: /* } */
12108: /* } */
12109: /* } */
12110:
12111: /* /\* if (mobilav!=0) free_ma3x(mobaverage,1, AGESUP,1,NCOVMAX, 1,NCOVMAX); *\/ */
12112:
12113: /* if (popforecast==1) { */
12114: /* free_ivector(popage,0,AGESUP); */
12115: /* free_vector(popeffectif,0,AGESUP); */
12116: /* free_vector(popcount,0,AGESUP); */
12117: /* } */
12118: /* free_ma3x(tabpop,1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
12119: /* free_ma3x(tabpopprev,1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
12120: /* fclose(ficrespop); */
12121: /* } /\* End of popforecast *\/ */
12122:
12123: int fileappend(FILE *fichier, char *optionfich)
12124: {
12125: if((fichier=fopen(optionfich,"a"))==NULL) {
12126: printf("Problem with file: %s\n", optionfich);
12127: fprintf(ficlog,"Problem with file: %s\n", optionfich);
12128: return (0);
12129: }
12130: fflush(fichier);
12131: return (1);
12132: }
12133:
12134:
12135: /**************** function prwizard **********************/
12136: void prwizard(int ncovmodel, int nlstate, int ndeath, char model[], FILE *ficparo)
12137: {
12138:
12139: /* Wizard to print covariance matrix template */
12140:
12141: char ca[32], cb[32];
12142: int i,j, k, li, lj, lk, ll, jj, npar, itimes;
12143: int numlinepar;
12144:
12145: printf("# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n");
12146: fprintf(ficparo,"# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n");
12147: for(i=1; i <=nlstate; i++){
12148: jj=0;
12149: for(j=1; j <=nlstate+ndeath; j++){
12150: if(j==i) continue;
12151: jj++;
12152: /*ca[0]= k+'a'-1;ca[1]='\0';*/
12153: printf("%1d%1d",i,j);
12154: fprintf(ficparo,"%1d%1d",i,j);
12155: for(k=1; k<=ncovmodel;k++){
12156: /* printf(" %lf",param[i][j][k]); */
12157: /* fprintf(ficparo," %lf",param[i][j][k]); */
12158: printf(" 0.");
12159: fprintf(ficparo," 0.");
12160: }
12161: printf("\n");
12162: fprintf(ficparo,"\n");
12163: }
12164: }
12165: printf("# Scales (for hessian or gradient estimation)\n");
12166: fprintf(ficparo,"# Scales (for hessian or gradient estimation)\n");
12167: npar= (nlstate+ndeath-1)*nlstate*ncovmodel; /* Number of parameters*/
12168: for(i=1; i <=nlstate; i++){
12169: jj=0;
12170: for(j=1; j <=nlstate+ndeath; j++){
12171: if(j==i) continue;
12172: jj++;
12173: fprintf(ficparo,"%1d%1d",i,j);
12174: printf("%1d%1d",i,j);
12175: fflush(stdout);
12176: for(k=1; k<=ncovmodel;k++){
12177: /* printf(" %le",delti3[i][j][k]); */
12178: /* fprintf(ficparo," %le",delti3[i][j][k]); */
12179: printf(" 0.");
12180: fprintf(ficparo," 0.");
12181: }
12182: numlinepar++;
12183: printf("\n");
12184: fprintf(ficparo,"\n");
12185: }
12186: }
12187: printf("# Covariance matrix\n");
12188: /* # 121 Var(a12)\n\ */
12189: /* # 122 Cov(b12,a12) Var(b12)\n\ */
12190: /* # 131 Cov(a13,a12) Cov(a13,b12, Var(a13)\n\ */
12191: /* # 132 Cov(b13,a12) Cov(b13,b12, Cov(b13,a13) Var(b13)\n\ */
12192: /* # 212 Cov(a21,a12) Cov(a21,b12, Cov(a21,a13) Cov(a21,b13) Var(a21)\n\ */
12193: /* # 212 Cov(b21,a12) Cov(b21,b12, Cov(b21,a13) Cov(b21,b13) Cov(b21,a21) Var(b21)\n\ */
12194: /* # 232 Cov(a23,a12) Cov(a23,b12, Cov(a23,a13) Cov(a23,b13) Cov(a23,a21) Cov(a23,b21) Var(a23)\n\ */
12195: /* # 232 Cov(b23,a12) Cov(b23,b12) ... Var (b23)\n" */
12196: fflush(stdout);
12197: fprintf(ficparo,"# Covariance matrix\n");
12198: /* # 121 Var(a12)\n\ */
12199: /* # 122 Cov(b12,a12) Var(b12)\n\ */
12200: /* # ...\n\ */
12201: /* # 232 Cov(b23,a12) Cov(b23,b12) ... Var (b23)\n" */
12202:
12203: for(itimes=1;itimes<=2;itimes++){
12204: jj=0;
12205: for(i=1; i <=nlstate; i++){
12206: for(j=1; j <=nlstate+ndeath; j++){
12207: if(j==i) continue;
12208: for(k=1; k<=ncovmodel;k++){
12209: jj++;
12210: ca[0]= k+'a'-1;ca[1]='\0';
12211: if(itimes==1){
12212: printf("#%1d%1d%d",i,j,k);
12213: fprintf(ficparo,"#%1d%1d%d",i,j,k);
12214: }else{
12215: printf("%1d%1d%d",i,j,k);
12216: fprintf(ficparo,"%1d%1d%d",i,j,k);
12217: /* printf(" %.5le",matcov[i][j]); */
12218: }
12219: ll=0;
12220: for(li=1;li <=nlstate; li++){
12221: for(lj=1;lj <=nlstate+ndeath; lj++){
12222: if(lj==li) continue;
12223: for(lk=1;lk<=ncovmodel;lk++){
12224: ll++;
12225: if(ll<=jj){
12226: cb[0]= lk +'a'-1;cb[1]='\0';
12227: if(ll<jj){
12228: if(itimes==1){
12229: printf(" Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj);
12230: fprintf(ficparo," Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj);
12231: }else{
12232: printf(" 0.");
12233: fprintf(ficparo," 0.");
12234: }
12235: }else{
12236: if(itimes==1){
12237: printf(" Var(%s%1d%1d)",ca,i,j);
12238: fprintf(ficparo," Var(%s%1d%1d)",ca,i,j);
12239: }else{
12240: printf(" 0.");
12241: fprintf(ficparo," 0.");
12242: }
12243: }
12244: }
12245: } /* end lk */
12246: } /* end lj */
12247: } /* end li */
12248: printf("\n");
12249: fprintf(ficparo,"\n");
12250: numlinepar++;
12251: } /* end k*/
12252: } /*end j */
12253: } /* end i */
12254: } /* end itimes */
12255:
12256: } /* end of prwizard */
12257: /******************* Gompertz Likelihood ******************************/
12258: double gompertz(double x[])
12259: {
12260: double A=0.0,B=0.,L=0.0,sump=0.,num=0.;
12261: int i,n=0; /* n is the size of the sample */
12262:
12263: for (i=1;i<=imx ; i++) {
12264: sump=sump+weight[i];
12265: /* sump=sump+1;*/
12266: num=num+1;
12267: }
12268: L=0.0;
12269: /* agegomp=AGEGOMP; */
12270: /* for (i=0; i<=imx; i++)
12271: 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]);*/
12272:
12273: for (i=1;i<=imx ; i++) {
12274: /* mu(a)=mu(agecomp)*exp(teta*(age-agegomp))
12275: mu(a)=x[1]*exp(x[2]*(age-agegomp)); x[1] and x[2] are per year.
12276: * L= Product mu(agedeces)exp(-\int_ageexam^agedc mu(u) du ) for a death between agedc (in month)
12277: * and agedc +1 month, cens[i]=0: log(x[1]/YEARM)
12278: * +
12279: * exp(-\int_ageexam^agecens mu(u) du ) when censored, cens[i]=1
12280: */
12281: if (wav[i] > 1 || agedc[i] < AGESUP) {
12282: if (cens[i] == 1){
12283: A=-x[1]/(x[2])*(exp(x[2]*(agecens[i]-agegomp))-exp(x[2]*(ageexmed[i]-agegomp)));
12284: } else if (cens[i] == 0){
12285: A=-x[1]/(x[2])*(exp(x[2]*(agedc[i]-agegomp))-exp(x[2]*(ageexmed[i]-agegomp)))
12286: +log(x[1]/YEARM) +x[2]*(agedc[i]-agegomp)+log(YEARM);
12287: } else
12288: printf("Gompertz cens[%d] neither 1 nor 0\n",i);
12289: /*if (wav[i] > 1 && agecens[i] > 15) {*/ /* ??? */
12290: L=L+A*weight[i];
12291: /* 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]);*/
12292: }
12293: }
12294:
12295: /*printf("x1=%2.9f x2=%2.9f x3=%2.9f L=%f\n",x[1],x[2],x[3],L);*/
12296:
12297: return -2*L*num/sump;
12298: }
12299:
12300: #ifdef GSL
12301: /******************* Gompertz_f Likelihood ******************************/
12302: double gompertz_f(const gsl_vector *v, void *params)
12303: {
12304: double A=0.,B=0.,LL=0.0,sump=0.,num=0.;
12305: double *x= (double *) v->data;
12306: int i,n=0; /* n is the size of the sample */
12307:
12308: for (i=0;i<=imx-1 ; i++) {
12309: sump=sump+weight[i];
12310: /* sump=sump+1;*/
12311: num=num+1;
12312: }
12313:
12314:
12315: /* for (i=0; i<=imx; i++)
12316: 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]);*/
12317: printf("x[0]=%lf x[1]=%lf\n",x[0],x[1]);
12318: for (i=1;i<=imx ; i++)
12319: {
12320: if (cens[i] == 1 && wav[i]>1)
12321: A=-x[0]/(x[1])*(exp(x[1]*(agecens[i]-agegomp))-exp(x[1]*(ageexmed[i]-agegomp)));
12322:
12323: if (cens[i] == 0 && wav[i]>1)
12324: A=-x[0]/(x[1])*(exp(x[1]*(agedc[i]-agegomp))-exp(x[1]*(ageexmed[i]-agegomp)))
12325: +log(x[0]/YEARM)+x[1]*(agedc[i]-agegomp)+log(YEARM);
12326:
12327: /*if (wav[i] > 1 && agecens[i] > 15) {*/ /* ??? */
12328: if (wav[i] > 1 ) { /* ??? */
12329: LL=LL+A*weight[i];
12330: /* 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]);*/
12331: }
12332: }
12333:
12334: /*printf("x1=%2.9f x2=%2.9f x3=%2.9f L=%f\n",x[1],x[2],x[3],L);*/
12335: printf("x[0]=%lf x[1]=%lf -2*LL*num/sump=%lf\n",x[0],x[1],-2*LL*num/sump);
12336:
12337: return -2*LL*num/sump;
12338: }
12339: #endif
12340:
12341: /******************* Printing html file ***********/
12342: void printinghtmlmort(char fileresu[], char title[], char datafile[], int firstpass, \
12343: int lastpass, int stepm, int weightopt, char model[],\
12344: int imx, double p[],double **matcov,double agemortsup){
12345: int i,k;
12346:
12347: fprintf(fichtm,"<ul><li><h4>Result files </h4>\n Force of mortality. Parameters of the Gompertz fit (with confidence interval in brackets):<br>");
12348: fprintf(fichtm," mu(age) =%lf*exp(%lf*(age-%d)) per year<br><br>",p[1],p[2],agegomp);
12349: for (i=1;i<=2;i++)
12350: 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]));
12351: fprintf(fichtm,"<br><br><img src=\"graphmort.svg\">");
12352: fprintf(fichtm,"</ul>");
12353:
12354: fprintf(fichtm,"<ul><li><h4>Life table</h4>\n <br>");
12355:
12356: 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>");
12357:
12358: for (k=agegomp;k<(agemortsup-2);k++)
12359: 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]);
12360:
12361:
12362: fflush(fichtm);
12363: }
12364:
12365: /******************* Gnuplot file **************/
12366: void printinggnuplotmort(char fileresu[], char optionfilefiname[], double ageminpar, double agemaxpar, double fage , char pathc[], double p[]){
12367:
12368: char dirfileres[132],optfileres[132];
12369:
12370: /*int ng;*/
12371:
12372:
12373: /*#ifdef windows */
12374: fprintf(ficgp,"cd \"%s\" \n",pathc);
12375: /*#endif */
12376:
12377:
12378: strcpy(dirfileres,optionfilefiname);
12379: strcpy(optfileres,"vpl");
12380: fprintf(ficgp,"set out \"graphmort.svg\"\n ");
12381: fprintf(ficgp,"set xlabel \"Age\"\n set ylabel \"Force of mortality (per year)\" \n ");
12382: fprintf(ficgp, "set ter svg size 640, 480\n set log y\n");
12383: /* fprintf(ficgp, "set size 0.65,0.65\n"); */
12384: fprintf(ficgp,"plot [%d:100] %lf*exp(%lf*(x-%d))",agegomp,p[1],p[2],agegomp);
12385:
12386: }
12387:
12388: int readdata(char datafile[], int firstobs, int lastobs, int *imax)
12389: {
12390:
12391: /*-------- data file ----------*/
12392: FILE *fic;
12393: char dummy[]=" ";
12394: int i = 0, j = 0, n = 0, iv = 0;/* , v;*/
12395: int lstra;
12396: int linei, month, year,iout;
12397: int noffset=0; /* This is the offset if BOM data file */
12398: char line[MAXLINE], linetmp[MAXLINE];
12399: char stra[MAXLINE], strb[MAXLINE];
12400: char *stratrunc;
12401:
12402: /* DummyV=ivector(-1,NCOVMAX); /\* 1 to 3 *\/ */
12403: /* FixedV=ivector(-1,NCOVMAX); /\* 1 to 3 *\/ */
12404:
12405: ncovcolt=ncovcol+nqv+ntv+nqtv; /* total of covariates in the data, not in the model equation */
12406:
12407: if((fic=fopen(datafile,"r"))==NULL) {
12408: printf("Problem while opening datafile: %s with errno='%s'\n", datafile,strerror(errno));fflush(stdout);
12409: fprintf(ficlog,"Problem while opening datafile: %s with errno='%s'\n", datafile,strerror(errno));fflush(ficlog);return 1;
12410: }
12411:
12412: /* Is it a BOM UTF-8 Windows file? */
12413: /* First data line */
12414: linei=0;
12415: while(fgets(line, MAXLINE, fic)) {
12416: noffset=0;
12417: if( line[0] == (char)0xEF && line[1] == (char)0xBB) /* EF BB BF */
12418: {
12419: noffset=noffset+3;
12420: printf("# Data file '%s' is an UTF8 BOM file, please convert to UTF8 or ascii file and rerun.\n",datafile);fflush(stdout);
12421: fprintf(ficlog,"# Data file '%s' is an UTF8 BOM file, please convert to UTF8 or ascii file and rerun.\n",datafile);
12422: fflush(ficlog); return 1;
12423: }
12424: /* else if( line[0] == (char)0xFE && line[1] == (char)0xFF)*/
12425: else if( line[0] == (char)0xFF && line[1] == (char)0xFE)
12426: {
12427: noffset=noffset+2;
12428: 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);
12429: 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);
12430: fflush(ficlog); return 1;
12431: }
12432: else if( line[0] == 0 && line[1] == 0)
12433: {
12434: if( line[2] == (char)0xFE && line[3] == (char)0xFF){
12435: noffset=noffset+4;
12436: 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);
12437: 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);
12438: fflush(ficlog); return 1;
12439: }
12440: } else{
12441: ;/*printf(" Not a BOM file\n");*/
12442: }
12443: /* If line starts with a # it is a comment */
12444: if (line[noffset] == '#') {
12445: linei=linei+1;
12446: break;
12447: }else{
12448: break;
12449: }
12450: }
12451: fclose(fic);
12452: if((fic=fopen(datafile,"r"))==NULL) {
12453: printf("Problem while opening datafile: %s with errno='%s'\n", datafile,strerror(errno));fflush(stdout);
12454: fprintf(ficlog,"Problem while opening datafile: %s with errno='%s'\n", datafile,strerror(errno));fflush(ficlog);return 1;
12455: }
12456: /* Not a Bom file */
12457:
12458: i=1;
12459: while ((fgets(line, MAXLINE, fic) != NULL) &&((i >= firstobs) && (i <=lastobs))) {
12460: linei=linei+1;
12461: for(j=strlen(line); j>=0;j--){ /* Untabifies line */
12462: if(line[j] == '\t')
12463: line[j] = ' ';
12464: }
12465: for(j=strlen(line)-1; (line[j]==' ')||(line[j]==10)||(line[j]==13);j--){
12466: ;
12467: };
12468: line[j+1]=0; /* Trims blanks at end of line */
12469: if(line[0]=='#'){
12470: fprintf(ficlog,"Comment line\n%s\n",line);
12471: printf("Comment line\n%s\n",line);
12472: continue;
12473: }
12474: trimbb(linetmp,line); /* Trims multiple blanks in line */
12475: strcpy(line, linetmp);
12476:
12477: /* Loops on waves */
12478: for (j=maxwav;j>=1;j--){
12479: for (iv=nqtv;iv>=1;iv--){ /* Loop on time varying quantitative variables */
12480: cutv(stra, strb, line, ' ');
12481: if(strb[0]=='.') { /* Missing value */
12482: lval=-1;
12483: cotqvar[j][iv][i]=-1; /* 0.0/0.0 */
12484: cotvar[j][ncovcol+nqv+ntv+iv][i]=-1; /* For performance reasons */
12485: if(isalpha(strb[1])) { /* .m or .d Really Missing value */
12486: 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);
12487: 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);
12488: return 1;
12489: }
12490: }else{
12491: errno=0;
12492: /* what_kind_of_number(strb); */
12493: dval=strtod(strb,&endptr);
12494: /* if( strb[0]=='\0' || (*endptr != '\0')){ */
12495: /* if(strb != endptr && *endptr == '\0') */
12496: /* dval=dlval; */
12497: /* if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN)) */
12498: if( strb[0]=='\0' || (*endptr != '\0')){
12499: 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);
12500: 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);
12501: return 1;
12502: }
12503: cotqvar[j][iv][i]=dval;
12504: cotvar[j][ncovcol+nqv+ntv+iv][i]=dval; /* because cotvar starts now at first ntv */
12505: }
12506: strcpy(line,stra);
12507: }/* end loop ntqv */
12508:
12509: for (iv=ntv;iv>=1;iv--){ /* Loop on time varying dummies */
12510: cutv(stra, strb, line, ' ');
12511: if(strb[0]=='.') { /* Missing value */
12512: lval=-1;
12513: }else{
12514: errno=0;
12515: lval=strtol(strb,&endptr,10);
12516: /* if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN))*/
12517: if( strb[0]=='\0' || (*endptr != '\0')){
12518: 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);
12519: 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);
12520: return 1;
12521: }
12522: }
12523: if(lval <-1 || lval >1){
12524: printf("Error reading data around '%ld' at line number %d for individual %d, '%s'\n \
12525: Should be a value of %d(nth) covariate of wave %d (0 should be the value for the reference and 1\n \
12526: for the alternative. IMaCh does not build design variables automatically, do it yourself.\n \
12527: For example, for multinomial values like 1, 2 and 3,\n \
12528: build V1=0 V2=0 for the reference value (1),\n \
12529: V1=1 V2=0 for (2) \n \
12530: and V1=0 V2=1 for (3). V1=1 V2=1 should not exist and the corresponding\n \
12531: output of IMaCh is often meaningless.\n \
12532: Exiting.\n",lval,linei, i,line,iv,j);
12533: fprintf(ficlog,"Error reading data around '%ld' at line number %d for individual %d, '%s'\n \
12534: Should be a value of %d(nth) covariate of wave %d (0 should be the value for the reference and 1\n \
12535: for the alternative. IMaCh does not build design variables automatically, do it yourself.\n \
12536: For example, for multinomial values like 1, 2 and 3,\n \
12537: build V1=0 V2=0 for the reference value (1),\n \
12538: V1=1 V2=0 for (2) \n \
12539: and V1=0 V2=1 for (3). V1=1 V2=1 should not exist and the corresponding\n \
12540: output of IMaCh is often meaningless.\n \
12541: Exiting.\n",lval,linei, i,line,iv,j);fflush(ficlog);
12542: return 1;
12543: }
12544: cotvar[j][ncovcol+nqv+iv][i]=(double)(lval);
12545: strcpy(line,stra);
12546: }/* end loop ntv */
12547:
12548: /* Statuses at wave */
12549: cutv(stra, strb, line, ' ');
12550: if(strb[0]=='.') { /* Missing value */
12551: lval=-1;
12552: }else{
12553: errno=0;
12554: lval=strtol(strb,&endptr,10);
12555: /* if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN))*/
12556: if( strb[0]=='\0' || (*endptr != '\0' )){
12557: 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);
12558: 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);
12559: return 1;
12560: }else if( lval==0 || lval > nlstate+ndeath){
12561: printf("Error in data around '%s' at line number %d for individual %d, '%s'\n Should be a state at wave %d. A state should be 1 to %d and not %ld.\n Fix your data file '%s'! Exiting.\n", strb, linei,i,line,j,nlstate+ndeath, lval, datafile);fflush(stdout);
12562: fprintf(ficlog,"Error in data around '%s' at line number %d for individual %d, '%s'\n Should be a state at wave %d. A state should be 1 to %d and not %ld.\n Fix your data file '%s'! Exiting.\n", strb, linei,i,line,j,nlstate+ndeath, lval, datafile); fflush(ficlog);
12563: return 1;
12564: }
12565: }
12566:
12567: s[j][i]=lval;
12568:
12569: /* Date of Interview */
12570: strcpy(line,stra);
12571: cutv(stra, strb,line,' ');
12572: if( (iout=sscanf(strb,"%d/%d",&month, &year)) != 0){
12573: }
12574: else if( (iout=sscanf(strb,"%s.",dummy)) != 0){
12575: month=99;
12576: year=9999;
12577: }else{
12578: 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);
12579: 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);
12580: return 1;
12581: }
12582: anint[j][i]= (double) year;
12583: mint[j][i]= (double)month;
12584: /* if( (int)anint[j][i]+ (int)(mint[j][i])/12. < (int) (moisnais[i]/12.+annais[i])){ */
12585: /* 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]); */
12586: /* 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]); */
12587: /* } */
12588: strcpy(line,stra);
12589: } /* End loop on waves */
12590:
12591: /* Date of death */
12592: cutv(stra, strb,line,' ');
12593: if( (iout=sscanf(strb,"%d/%d",&month, &year)) != 0){
12594: }
12595: else if( (iout=sscanf(strb,"%s.",dummy)) != 0){
12596: month=99;
12597: year=9999;
12598: }else{
12599: 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);
12600: 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);
12601: return 1;
12602: }
12603: andc[i]=(double) year;
12604: moisdc[i]=(double) month;
12605: strcpy(line,stra);
12606:
12607: /* Date of birth */
12608: cutv(stra, strb,line,' ');
12609: if( (iout=sscanf(strb,"%d/%d",&month, &year)) != 0){
12610: }
12611: else if( (iout=sscanf(strb,"%s.", dummy)) != 0){
12612: month=99;
12613: year=9999;
12614: }else{
12615: 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);
12616: 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);
12617: return 1;
12618: }
12619: if (year==9999) {
12620: 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);
12621: 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);
12622: return 1;
12623:
12624: }
12625: annais[i]=(double)(year);
12626: moisnais[i]=(double)(month);
12627: for (j=1;j<=maxwav;j++){
12628: if( (int)anint[j][i]+ (int)(mint[j][i])/12. < (int) (moisnais[i]/12.+annais[i])){
12629: 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]);
12630: 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]);
12631: }
12632: }
12633:
12634: strcpy(line,stra);
12635:
12636: /* Sample weight */
12637: cutv(stra, strb,line,' ');
12638: errno=0;
12639: dval=strtod(strb,&endptr);
12640: if( strb[0]=='\0' || (*endptr != '\0')){
12641: printf("Error reading data around '%f' at line number %d, \"%s\" for individual %d\nShould be a weight. Exiting.\n",dval, i,line,linei);
12642: 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);
12643: fflush(ficlog);
12644: return 1;
12645: }
12646: weight[i]=dval;
12647: strcpy(line,stra);
12648:
12649: for (iv=nqv;iv>=1;iv--){ /* Loop on fixed quantitative variables */
12650: cutv(stra, strb, line, ' ');
12651: if(strb[0]=='.') { /* Missing value */
12652: lval=-1;
12653: coqvar[iv][i]=NAN;
12654: covar[ncovcol+iv][i]=NAN; /* including qvar in standard covar for performance reasons */
12655: }else{
12656: errno=0;
12657: /* what_kind_of_number(strb); */
12658: dval=strtod(strb,&endptr);
12659: /* if(strb != endptr && *endptr == '\0') */
12660: /* dval=dlval; */
12661: /* if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN)) */
12662: if( strb[0]=='\0' || (*endptr != '\0')){
12663: 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);
12664: 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);
12665: return 1;
12666: }
12667: coqvar[iv][i]=dval;
12668: covar[ncovcol+iv][i]=dval; /* including qvar in standard covar for performance reasons */
12669: }
12670: strcpy(line,stra);
12671: }/* end loop nqv */
12672:
12673: /* Covariate values */
12674: for (j=ncovcol;j>=1;j--){
12675: cutv(stra, strb,line,' ');
12676: if(strb[0]=='.') { /* Missing covariate value */
12677: lval=-1;
12678: }else{
12679: errno=0;
12680: lval=strtol(strb,&endptr,10);
12681: if( strb[0]=='\0' || (*endptr != '\0')){
12682: 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);
12683: 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);
12684: return 1;
12685: }
12686: }
12687: if(lval <-1 || lval >1){
12688: printf("Error reading data around '%ld' at line number %d for individual %d, '%s'\n \
12689: Should be a value of %d(nth) covariate (0 should be the value for the reference and 1\n \
12690: for the alternative. IMaCh does not build design variables automatically, do it yourself.\n \
12691: For example, for multinomial values like 1, 2 and 3,\n \
12692: build V1=0 V2=0 for the reference value (1),\n \
12693: V1=1 V2=0 for (2) \n \
12694: and V1=0 V2=1 for (3). V1=1 V2=1 should not exist and the corresponding\n \
12695: output of IMaCh is often meaningless.\n \
12696: Exiting.\n",lval,linei, i,line,j);
12697: fprintf(ficlog,"Error reading data around '%ld' at line number %d for individual %d, '%s'\n \
12698: Should be a value of %d(nth) covariate (0 should be the value for the reference and 1\n \
12699: for the alternative. IMaCh does not build design variables automatically, do it yourself.\n \
12700: For example, for multinomial values like 1, 2 and 3,\n \
12701: build V1=0 V2=0 for the reference value (1),\n \
12702: V1=1 V2=0 for (2) \n \
12703: and V1=0 V2=1 for (3). V1=1 V2=1 should not exist and the corresponding\n \
12704: output of IMaCh is often meaningless.\n \
12705: Exiting.\n",lval,linei, i,line,j);fflush(ficlog);
12706: return 1;
12707: }
12708: covar[j][i]=(double)(lval);
12709: strcpy(line,stra);
12710: }
12711: lstra=strlen(stra);
12712:
12713: if(lstra > 9){ /* More than 2**32 or max of what printf can write with %ld */
12714: stratrunc = &(stra[lstra-9]);
12715: num[i]=atol(stratrunc);
12716: }
12717: else
12718: num[i]=atol(stra);
12719: /*if((s[2][i]==2) && (s[3][i]==-1)&&(s[4][i]==9)){
12720: 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;}*/
12721:
12722: i=i+1;
12723: } /* End loop reading data */
12724:
12725: *imax=i-1; /* Number of individuals */
12726: fclose(fic);
12727:
12728: return (0);
12729: /* endread: */
12730: printf("Exiting readdata: ");
12731: fclose(fic);
12732: return (1);
12733: }
12734:
12735: void removefirstspace(char **stri){/*, char stro[]) {*/
12736: char *p1 = *stri, *p2 = *stri;
12737: while (*p2 == ' ')
12738: p2++;
12739: /* while ((*p1++ = *p2++) !=0) */
12740: /* ; */
12741: /* do */
12742: /* while (*p2 == ' ') */
12743: /* p2++; */
12744: /* while (*p1++ == *p2++); */
12745: *stri=p2;
12746: }
12747:
12748: int decoderesult( char resultline[], int nres)
12749: /**< This routine decode one result line and returns the combination # of dummy covariates only **/
12750: {
12751: int j=0, k=0, k1=0, k2=0, k3=0, k4=0, match=0, k2q=0, k3q=0, k4q=0;
12752: char resultsav[MAXLINE];
12753: /* int resultmodel[MAXLINE]; */
12754: /* int modelresult[MAXLINE]; */
12755: char stra[80], strb[80], strc[80], strd[80],stre[80];
12756:
12757: removefirstspace(&resultline);
12758: printf("decoderesult:%s\n",resultline);
12759:
12760: strcpy(resultsav,resultline);
12761: /* printf("Decoderesult resultsav=\"%s\" resultline=\"%s\"\n", resultsav, resultline); */
12762: if (strlen(resultsav) >1){
12763: j=nbocc(resultsav,'='); /**< j=Number of covariate values'=' in this resultline */
12764: }
12765: if(j == 0 && cptcovs== 0){ /* Resultline but no = and no covariate in the model */
12766: TKresult[nres]=0; /* Combination for the nresult and the model */
12767: return (0);
12768: }
12769: if( j != cptcovs ){ /* Be careful if a variable is in a product but not single */
12770: 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, 1+age+%s.\n",j, cptcovs, model);fflush(ficlog);
12771: 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, 1+age+%s.\n",j, cptcovs, model);fflush(stdout);
12772: if(j==0)
12773: return 1;
12774: }
12775: for(k=1; k<=j;k++){ /* Loop on any covariate of the RESULT LINE */
12776: if(nbocc(resultsav,'=') >1){
12777: 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" */
12778: /* If resultsav= "V4= 1 V5=25.1 V3=0" with a blank then strb="V4=" and stra="1 V5=25.1 V3=0" */
12779: cutl(strc,strd,strb,'='); /* strb:"V4=1" strc="1" strd="V4" */
12780: /* If a blank, then strc="V4=" and strd='\0' */
12781: if(strc[0]=='\0'){
12782: printf("Error in resultline, probably a blank after the \"%s\", \"result:%s\", stra=\"%s\" resultsav=\"%s\"\n",strb,resultline, stra, resultsav);
12783: fprintf(ficlog,"Error in resultline, probably a blank after the \"V%s=\", resultline=%s\n",strb,resultline);
12784: return 1;
12785: }
12786: }else
12787: cutl(strc,strd,resultsav,'=');
12788: Tvalsel[k]=atof(strc); /* 1 */ /* Tvalsel of k is the float value of the kth covariate appearing in this result line */
12789:
12790: cutl(strc,stre,strd,'V'); /* strd='V4' strc=4 stre='V' */;
12791: Tvarsel[k]=atoi(strc); /* 4 */ /* Tvarsel is the id of the kth covariate in the result line Tvarsel[1] in "V4=1.." is 4.*/
12792: /* Typevarsel[k]=1; /\* 1 for age product *\/ */
12793: /* cptcovsel++; */
12794: if (nbocc(stra,'=') >0)
12795: strcpy(resultsav,stra); /* and analyzes it */
12796: }
12797: /* Checking for missing or useless values in comparison of current model needs */
12798: /* Feeds resultmodel[nres][k1]=k2 for k1th product covariate with age in the model equation fed by the index k2 of the resutline*/
12799: for(k1=1; k1<= cptcovt ;k1++){ /* Loop on MODEL LINE V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
12800: if(Typevar[k1]==0){ /* Single covariate in model */
12801: /* 0 for simple covariate (dummy, quantitative, fixed or varying), 1 for age product, 2 for product */
12802: match=0;
12803: for(k2=1; k2 <=j;k2++){/* Loop on resultline. In result line V4=1 V5=24.1 V3=1 V2=8 V1=0 */
12804: if(Tvar[k1]==Tvarsel[k2]) {/* Tvar is coming from the model, Tvarsel from the result. Tvar[1]=5 == Tvarsel[2]=5 */
12805: modelresult[nres][k2]=k1;/* modelresult[2]=1 modelresult[1]=2 modelresult[3]=3 modelresult[6]=4 modelresult[9]=5 */
12806: match=1; /* modelresult of k2 variable of resultline is identical to k1 variable of the model good */
12807: break;
12808: }
12809: }
12810: if(match == 0){
12811: 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]);
12812: 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);
12813: return 1;
12814: }
12815: }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*/
12816: /* We feed resultmodel[k1]=k2; */
12817: match=0;
12818: 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 */
12819: if(Tvar[k1]==Tvarsel[k2]) {/* Tvar is coming from the model, Tvarsel from the result. Tvar[1]=5 == Tvarsel[2]=5 */
12820: 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 */
12821: resultmodel[nres][k1]=k2; /* Added here */
12822: /* printf("Decoderesult first modelresult[k2=%d]=%d (k1) V%d*AGE\n",k2,k1,Tvar[k1]); */
12823: match=1; /* modelresult of k2 variable of resultline is identical to k1 variable of the model good */
12824: break;
12825: }
12826: }
12827: if(match == 0){
12828: 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]);
12829: 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]);
12830: return 1;
12831: }
12832: }else if(Typevar[k1]==2 || Typevar[k1]==3){ /* Product with or without age. We want to get the position in the resultline of the product in the model line*/
12833: /* resultmodel[nres][of such a Vn * Vm product k1] is not unique, so can't exist, we feed Tvard[k1][1] and [2] */
12834: match=0;
12835: /* 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]); */
12836: for(k2=1; k2 <=j;k2++){/* Loop on resultline. In result line V4=1 V5=24.1 V3=1 V2=8 V1=0 */
12837: if(Tvardk[k1][1]==Tvarsel[k2]) {/* Tvardk is coming from the model, Tvarsel from the result. Tvar[1]=5 == Tvarsel[2]=5 */
12838: /* modelresult[k2]=k1; */
12839: /* printf("Decoderesult first Product modelresult[k2=%d]=%d (k1) V%d * \n",k2,k1,Tvarsel[k2]); */
12840: match=1; /* modelresult of k2 variable of resultline is identical to k1 variable of the model good */
12841: }
12842: }
12843: if(match == 0){
12844: printf("Error in result line (Product without age first variable or double product with age): V%d is missing in result: %s according to model=1+age+%s\n",Tvardk[k1][1], resultline, model);
12845: fprintf(ficlog,"Error in result line (Product without age first variable or double product with age): V%d is missing in result: %s according to model=1+age+%s\n",Tvardk[k1][1], resultline, model);
12846: return 1;
12847: }
12848: match=0;
12849: for(k2=1; k2 <=j;k2++){/* Loop on resultline. In result line V4=1 V5=24.1 V3=1 V2=8 V1=0 */
12850: if(Tvardk[k1][2]==Tvarsel[k2]) {/* Tvardk is coming from the model, Tvarsel from the result. Tvar[1]=5 == Tvarsel[2]=5 */
12851: /* modelresult[k2]=k1;*/
12852: /* printf("Decoderesult second Product modelresult[k2=%d]=%d (k1) * V%d \n ",k2,k1,Tvarsel[k2]); */
12853: match=1; /* modelresult of k2 variable of resultline is identical to k1 variable of the model good */
12854: break;
12855: }
12856: }
12857: if(match == 0){
12858: printf("Error in result line (Product without age second variable or double product with age): V%d is missing in result: %s according to model=1+age+%s\n",Tvardk[k1][2], resultline, model);
12859: fprintf(ficlog,"Error in result line (Product without age second variable or double product with age): V%d is missing in result : %s according to model=1+age+%s\n",Tvardk[k1][2], resultline, model);
12860: return 1;
12861: }
12862: }/* End of testing */
12863: }/* End loop cptcovt */
12864: /* Checking for missing or useless values in comparison of current model needs */
12865: /* Feeds resultmodel[nres][k1]=k2 for single covariate (k1) in the model equation */
12866: 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)
12867: * Loop on resultline variables: result line V4=1 V5=24.1 V3=1 V2=8 V1=0 */
12868: match=0;
12869: for(k1=1; k1<= cptcovt ;k1++){ /* loop on model: model line V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
12870: if(Typevar[k1]==0){ /* Single only */
12871: if(Tvar[k1]==Tvarsel[k2]) { /* Tvar[2]=4 == Tvarsel[1]=4 What if a product? */
12872: 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 */
12873: 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 */
12874: ++match;
12875: }
12876: }
12877: }
12878: if(match == 0){
12879: printf("Error in result line: variable V%d is missing in model; result: %s, model=1+age+%s\n",Tvarsel[k2], resultline, model);
12880: fprintf(ficlog,"Error in result line: variable V%d is missing in model; result: %s, model=1+age+%s\n",Tvarsel[k2], resultline, model);
12881: return 1;
12882: }else if(match > 1){
12883: printf("Error in result line: %d doubled; result: %s, model=1+age+%s\n",k2, resultline, model);
12884: fprintf(ficlog,"Error in result line: %d doubled; result: %s, model=1+age+%s\n",k2, resultline, model);
12885: return 1;
12886: }
12887: }
12888: /* cptcovres=j /\* Number of variables in the resultline is equal to cptcovs and thus useless *\/ */
12889: /* We need to deduce which combination number is chosen and save quantitative values */
12890: /* model line V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
12891: /* nres=1st result line: V4=1 V5=25.1 V3=0 V2=8 V1=1 */
12892: /* 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*/
12893: /* nres=2nd result line: V4=1 V5=24.1 V3=1 V2=8 V1=0 */
12894: /* should give a combination of dummy V4=1, V3=1, V1=0 => V4*2**(0) + V3*2**(1) + V1*2**(2) = 3 + (1offset) = 4*/
12895: /* 1 0 0 0 */
12896: /* 2 1 0 0 */
12897: /* 3 0 1 0 */
12898: /* 4 1 1 0 */ /* V4=1, V3=1, V1=0 (nres=2)*/
12899: /* 5 0 0 1 */
12900: /* 6 1 0 1 */ /* V4=1, V3=0, V1=1 (nres=1)*/
12901: /* 7 0 1 1 */
12902: /* 8 1 1 1 */
12903: /* V(Tvresult)=Tresult V4=1 V3=0 V1=1 Tresult[nres=1][2]=0 */
12904: /* V(Tvqresult)=Tqresult V5=25.1 V2=8 Tqresult[nres=1][1]=25.1 */
12905: /* V5*age V5 known which value for nres? */
12906: /* Tqinvresult[2]=8 Tqinvresult[1]=25.1 */
12907: 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.
12908: * loop on position k1 in the MODEL LINE */
12909: /* k counting number of combination of single dummies in the equation model */
12910: /* k4 counting single dummies in the equation model */
12911: /* k4q counting single quantitatives in the equation model */
12912: if( Dummy[k1]==0 && Typevar[k1]==0 ){ /* Dummy and Single, fixed or timevarying, k1 is sorting according to MODEL, but k3 to resultline */
12913: /* 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) */
12914: /* modelresult[k3]=k1: k3th position in the result line corresponds to the k1 position in the model line (doesn't work with products)*/
12915: /* Value in the (current nres) resultline of the variable at the k1th position in the model equation resultmodel[nres][k1]= k3 */
12916: /* resultmodel[nres][k1]=k3: k1th position in the model correspond to the k3 position in the resultline */
12917: /* k3 is the position in the nres result line of the k1th variable of the model equation */
12918: /* Tvarsel[k3]: Name of the variable at the k3th position in the result line. */
12919: /* Tvalsel[k3]: Value of the variable at the k3th position in the result line. */
12920: /* Tresult[nres][result_position]= value of the dummy variable at the result_position in the nres resultline */
12921: /* Tvresult[nres][result_position]= name of the dummy variable at the result_position in the nres resultline */
12922: /* Tinvresult[nres][Name of a dummy variable]= value of the variable in the result line */
12923: /* TinvDoQresult[nres][Name of a Dummy or Q variable]= value of the variable in the result line */
12924: k3= resultmodel[nres][k1]; /* From position k1 in model get position k3 in result line */
12925: /* nres=1 k1=2 resultmodel[2(V4)] = 1=k3 ; k1=3 resultmodel[3(V3)] = 2=k3*/
12926: 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)*/
12927: k+=Tvalsel[k3]*pow(2,k4); /* nres=1 k1=2 Tvalsel[1]=1 (V4=1); k1=3 k3=2 Tvalsel[2]=0 (V3=0) */
12928: TinvDoQresult[nres][(int)Tvarsel[k3]]=Tvalsel[k3]; /* TinvDoQresult[nres][Name]=Value; stores the value into the name of the variable. */
12929: /* Tinvresult[nres][4]=1 */
12930: /* Tresult[nres][k4+1]=Tvalsel[k3];/\* Tresult[nres=2][1]=1(V4=1) Tresult[nres=2][2]=0(V3=0) *\/ */
12931: Tresult[nres][k3]=Tvalsel[k3];/* Tresult[nres=2][1]=1(V4=1) Tresult[nres=2][2]=0(V3=0) */
12932: /* Tvresult[nres][k4+1]=(int)Tvarsel[k3];/\* Tvresult[nres][1]=4 Tvresult[nres][3]=1 *\/ */
12933: Tvresult[nres][k3]=(int)Tvarsel[k3];/* Tvresult[nres][1]=4 Tvresult[nres][3]=1 */
12934: Tinvresult[nres][(int)Tvarsel[k3]]=Tvalsel[k3]; /* Tinvresult[nres][4]=1 */
12935: precov[nres][k1]=Tvalsel[k3]; /* Value from resultline of the variable at the k1 position in the model */
12936: /* 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); */
12937: k4++;;
12938: }else if( Dummy[k1]==1 && Typevar[k1]==0 ){ /* Quantitative and single */
12939: /* Tqresult[nres][result_position]= value of the variable at the result_position in the nres resultline */
12940: /* Tvqresult[nres][result_position]= id of the variable at the result_position in the nres resultline */
12941: /* Tqinvresult[nres][Name of a quantitative variable]= value of the variable in the result line */
12942: k3q= resultmodel[nres][k1]; /* resultmodel[1(V5)] = 5 =k3q */
12943: k2q=(int)Tvarsel[k3q]; /* Name of variable at k3q th position in the resultline */
12944: /* Tvarsel[resultmodel[1]]= Tvarsel[1] = 4=k2 */
12945: /* Tqresult[nres][k4q+1]=Tvalsel[k3q]; /\* Tqresult[nres][1]=25.1 *\/ */
12946: /* Tvresult[nres][k4q+1]=(int)Tvarsel[k3q];/\* Tvresult[nres][1]=4 Tvresult[nres][3]=1 *\/ */
12947: /* Tvqresult[nres][k4q+1]=(int)Tvarsel[k3q]; /\* Tvqresult[nres][1]=5 *\/ */
12948: Tqresult[nres][k3q]=Tvalsel[k3q]; /* Tqresult[nres][1]=25.1 */
12949: Tvresult[nres][k3q]=(int)Tvarsel[k3q];/* Tvresult[nres][1]=4 Tvresult[nres][3]=1 */
12950: Tvqresult[nres][k3q]=(int)Tvarsel[k3q]; /* Tvqresult[nres][1]=5 */
12951: Tqinvresult[nres][(int)Tvarsel[k3q]]=Tvalsel[k3q]; /* Tqinvresult[nres][5]=25.1 */
12952: TinvDoQresult[nres][(int)Tvarsel[k3q]]=Tvalsel[k3q]; /* Tqinvresult[nres][5]=25.1 */
12953: precov[nres][k1]=Tvalsel[k3q];
12954: /* 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]); */
12955: k4q++;;
12956: }else if( Dummy[k1]==2 ){ /* For dummy with age product "V2+V3+V4+V6+V7+V6*V2+V7*V2+V6*V3+V7*V3+V6*V4+V7*V4+age*V2+age*V3+age*V4+age*V6+age*V7+age*V6*V2+age*V6*V3+age*V7*V3+age*V6*V4+age*V7*V4\r"*/
12957: /* Tvar[k1]; */ /* Age variable */ /* 17 age*V6*V2 ?*/
12958: /* Wrong we want the value of variable name Tvar[k1] */
12959: if(Typevar[k1]==2 || Typevar[k1]==3 ){ /* For product quant or dummy (with or without age) */
12960: precov[nres][k1]=TinvDoQresult[nres][Tvardk[k1][1]] * TinvDoQresult[nres][Tvardk[k1][2]];
12961: /* 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]]); */
12962: }else{
12963: k3= resultmodel[nres][k1]; /* nres=1 k1=2 resultmodel[2(V4)] = 1=k3 ; k1=3 resultmodel[3(V3)] = 2=k3*/
12964: 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)*/
12965: TinvDoQresult[nres][(int)Tvarsel[k3]]=Tvalsel[k3]; /* TinvDoQresult[nres][4]=1 */
12966: precov[nres][k1]=Tvalsel[k3];
12967: }
12968: /* 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]); */
12969: }else if( Dummy[k1]==3 ){ /* For quant with age product */
12970: if(Typevar[k1]==2 || Typevar[k1]==3 ){ /* For product quant or dummy (with or without age) */
12971: precov[nres][k1]=TinvDoQresult[nres][Tvardk[k1][1]] * TinvDoQresult[nres][Tvardk[k1][2]];
12972: /* 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]]); */
12973: }else{
12974: k3q= resultmodel[nres][k1]; /* resultmodel[1(V5)] = 25.1=k3q */
12975: k2q=(int)Tvarsel[k3q]; /* Tvarsel[resultmodel[1]]= Tvarsel[1] = 4=k2 */
12976: TinvDoQresult[nres][(int)Tvarsel[k3q]]=Tvalsel[k3q]; /* TinvDoQresult[nres][5]=25.1 */
12977: precov[nres][k1]=Tvalsel[k3q];
12978: }
12979: /* 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]); */
12980: }else if(Typevar[k1]==2 || Typevar[k1]==3 ){ /* For product quant or dummy (with or without age) */
12981: precov[nres][k1]=TinvDoQresult[nres][Tvardk[k1][1]] * TinvDoQresult[nres][Tvardk[k1][2]];
12982: /* 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]]); */
12983: }else{
12984: printf("Error Decoderesult probably a product Dummy[%d]==%d && Typevar[%d]==%d\n", k1, Dummy[k1], k1, Typevar[k1]);
12985: fprintf(ficlog,"Error Decoderesult probably a product Dummy[%d]==%d && Typevar[%d]==%d\n", k1, Dummy[k1], k1, Typevar[k1]);
12986: }
12987: }
12988:
12989: TKresult[nres]=++k; /* Number of combinations of dummies for the nresult and the model =Tvalsel[k3]*pow(2,k4) + 1*/
12990: return (0);
12991: }
12992:
12993: int decodemodel( char model[], int lastobs)
12994: /**< This routine decodes the model and returns:
12995: * Model V1+V2+V3+V8+V7*V8+V5*V6+V8*age+V3*age+age*age
12996: * - nagesqr = 1 if age*age in the model, otherwise 0.
12997: * - cptcovt total number of covariates of the model nbocc(+)+1 = 8 excepting constant and age and age*age
12998: * - cptcovn or number of covariates k of the models excluding age*products =6 and age*age
12999: * - cptcovage number of covariates with age*products =2
13000: * - cptcovs number of simple covariates
13001: * ncovcolt=ncovcol+nqv+ntv+nqtv total of covariates in the data, not in the model equation
13002: * - 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
13003: * which is a new column after the 9 (ncovcol+nqv+ntv+nqtv) variables.
13004: * - if k is a product Vn*Vm, covar[k][i] is filled with correct values for each individual
13005: * - Tprod[l] gives the kth covariates of the product Vn*Vm l=1 to cptcovprod-cptcovage
13006: * Tprod[1]@2 {5, 6}: position of first product V7*V8 is 5, and second V5*V6 is 6.
13007: * - Tvard[k] p Tvard[1][1]@4 {7, 8, 5, 6} for V7*V8 and V5*V6 .
13008: */
13009: /* V2+V1+V4+V3*age Tvar[4]=3 ; V1+V2*age Tvar[2]=2; V1+V1*age Tvar[2]=1, Tage[1]=2 */
13010: {
13011: int i, j, k, ks;/* , v;*/
13012: int n,m;
13013: int j1, k1, k11, k12, k2, k3, k4;
13014: char modelsav[300];
13015: char stra[300], strb[300], strc[300], strd[300],stre[300],strf[300];
13016: char *strpt;
13017: int **existcomb;
13018:
13019: existcomb=imatrix(1,NCOVMAX,1,NCOVMAX);
13020: for(i=1;i<=NCOVMAX;i++)
13021: for(j=1;j<=NCOVMAX;j++)
13022: existcomb[i][j]=0;
13023:
13024: /*removespace(model);*/
13025: if (strlen(model) >1){ /* If there is at least 1 covariate */
13026: j=0, j1=0, k1=0, k12=0, k2=-1, ks=0, cptcovn=0;
13027: if (strstr(model,"AGE") !=0){
13028: printf("Error. AGE must be in lower case 'age' model=1+age+%s. ",model);
13029: fprintf(ficlog,"Error. AGE must be in lower case model=1+age+%s. ",model);fflush(ficlog);
13030: return 1;
13031: }
13032: if (strstr(model,"v") !=0){
13033: printf("Error. 'v' must be in upper case 'V' model=1+age+%s ",model);
13034: fprintf(ficlog,"Error. 'v' must be in upper case model=1+age+%s ",model);fflush(ficlog);
13035: return 1;
13036: }
13037: strcpy(modelsav,model);
13038: if ((strpt=strstr(model,"age*age")) !=0){
13039: printf(" strpt=%s, model=1+age+%s\n",strpt, model);
13040: if(strpt != model){
13041: printf("Error in model: 'model=1+age+%s'; 'age*age' should in first place before other covariates\n \
13042: 'model=1+age+age*age+V1.' or 'model=1+age+age*age+V1+V1*age.', please swap as well as \n \
13043: corresponding column of parameters.\n",model);
13044: fprintf(ficlog,"Error in model: 'model=1+age+%s'; 'age*age' should in first place before other covariates\n \
13045: 'model=1+age+age*age+V1.' or 'model=1+age+age*age+V1+V1*age.', please swap as well as \n \
13046: corresponding column of parameters.\n",model); fflush(ficlog);
13047: return 1;
13048: }
13049: nagesqr=1;
13050: if (strstr(model,"+age*age") !=0)
13051: substrchaine(modelsav, model, "+age*age");
13052: else if (strstr(model,"age*age+") !=0)
13053: substrchaine(modelsav, model, "age*age+");
13054: else
13055: substrchaine(modelsav, model, "age*age");
13056: }else
13057: nagesqr=0;
13058: if (strlen(modelsav) >1){ /* V2 +V3 +V4 +V6 +V7 +V6*V2 +V7*V2 +V6*V3 +V7*V3 +V6*V4 +V7*V4 +age*V2 +age*V3 +age*V4 +age*V6 +age*V7 +age*V6*V2 +V7*V2 +age*V6*V3 +age*V7*V3 +age*V6*V4 +age*V7*V4 */
13059: j=nbocc(modelsav,'+'); /**< j=Number of '+' */
13060: j1=nbocc(modelsav,'*'); /**< j1=Number of '*' */
13061: cptcovs=0; /**< Number of simple covariates V1 +V1*age +V3 +V3*V4 +age*age => V1 + V3 =4+1-3=2 Wrong */
13062: cptcovt= j+1; /* Number of total covariates in the model, not including
13063: * cst, age and age*age
13064: * V1+V1*age+ V3 + V3*V4+age*age=> 3+1=4*/
13065: /* including age products which are counted in cptcovage.
13066: * but the covariates which are products must be treated
13067: * separately: ncovn=4- 2=2 (V1+V3). */
13068: cptcovprod=0; /**< Number of products V1*V2 +v3*age = 2 */
13069: cptcovdageprod=0; /* Number of doouble products with age age*Vn*VM or Vn*age*Vm or Vn*Vm*age */
13070: cptcovprodnoage=0; /**< Number of covariate products without age: V3*V4 =1 */
13071: cptcovprodage=0;
13072: /* cptcovprodage=nboccstr(modelsav,"age");*/
13073:
13074: /* Design
13075: * V1 V2 V3 V4 V5 V6 V7 V8 V9 Weight
13076: * < ncovcol=8 >
13077: * Model V2 + V1 + V3*age + V3 + V5*V6 + V7*V8 + V8*age + V8
13078: * k= 1 2 3 4 5 6 7 8
13079: * cptcovn number of covariates (not including constant and age ) = # of + plus 1 = 7+1=8
13080: * covar[k,i], are for fixed covariates, value of kth covariate if not including age for individual i:
13081: * covar[1][i]= (V1), covar[4][i]=(V4), covar[8][i]=(V8)
13082: * Tvar[k] # of the kth covariate: Tvar[1]=2 Tvar[2]=1 Tvar[4]=3 Tvar[8]=8
13083: * if multiplied by age: V3*age Tvar[3=V3*age]=3 (V3) Tvar[7]=8 and
13084: * Tage[++cptcovage]=k
13085: * if products, new covar are created after ncovcol + nqv (quanti fixed) with k1
13086: * Tvar[k]=ncovcol+k1; # of the kth covariate product: Tvar[5]=ncovcol+1=10 Tvar[6]=ncovcol+1=11
13087: * Tprod[k1]=k; Tprod[1]=5 Tprod[2]= 6; gives the position of the k1th product
13088: * 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
13089: * Tvar[cptcovn+k2]=Tvard[k1][1];Tvar[cptcovn+k2+1]=Tvard[k1][2];
13090: * Tvar[8+1]=5;Tvar[8+2]=6;Tvar[8+3]=7;Tvar[8+4]=8 inverted
13091: * V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11
13092: * < ncovcol=8 8 fixed covariate. Additional starts at 9 (V5*V6) and 10(V7*V8) >
13093: * Model V2 + V1 + V3*age + V3 + V5*V6 + V7*V8 + V8*age + V8 d1 d1 d2 d2
13094: * k= 1 2 3 4 5 6 7 8 9 10 11 12
13095: * Tvard[k]= 2 1 3 3 10 11 8 8 5 6 7 8
13096: * p Tvar[1]@12={2, 1, 3, 3, 9, 10, 8, 8}
13097: * p Tprod[1]@2={ 6, 5}
13098: *p Tvard[1][1]@4= {7, 8, 5, 6}
13099: * covar[k][i]= V2 V1 ? V3 V5*V6? V7*V8? ? V8
13100: * cov[Tage[kk]+2]=covar[Tvar[Tage[kk]]][i]*cov[2];
13101: *How to reorganize? Tvars(orted)
13102: * Model V1 + V2 + V3 + V8 + V5*V6 + V7*V8 + V3*age + V8*age
13103: * Tvars {2, 1, 3, 3, 11, 10, 8, 8, 7, 8, 5, 6}
13104: * {2, 1, 4, 8, 5, 6, 3, 7}
13105: * Struct []
13106: */
13107:
13108: /* This loop fills the array Tvar from the string 'model'.*/
13109: /* j is the number of + signs in the model V1+V2+V3 j=2 i=3 to 1 */
13110: /* modelsav=V2+V1+V4+age*V3 strb=age*V3 stra=V2+V1+V4 */
13111: /* k=4 (age*V3) Tvar[k=4]= 3 (from V3) Tage[cptcovage=1]=4 */
13112: /* k=3 V4 Tvar[k=3]= 4 (from V4) */
13113: /* k=2 V1 Tvar[k=2]= 1 (from V1) */
13114: /* k=1 Tvar[1]=2 (from V2) */
13115: /* k=5 Tvar[5] */
13116: /* for (k=1; k<=cptcovn;k++) { */
13117: /* cov[2+k]=nbcode[Tvar[k]][codtabm(ij,Tvar[k])]; */
13118: /* } */
13119: /* for (k=1; k<=cptcovage;k++) cov[2+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,Tvar[Tage[k])]]*cov[2]; */
13120: /*
13121: * Treating invertedly V2+V1+V3*age+V2*V4 is as if written V2*V4 +V3*age + V1 + V2 */
13122: for(k=cptcovt; k>=1;k--){ /**< Number of covariates not including constant and age, neither age*age*/
13123: Tvar[k]=0; Tprod[k]=0; Tposprod[k]=0;
13124: }
13125: cptcovage=0;
13126:
13127: /* First loop in order to calculate */
13128: /* for age*VN*Vm
13129: * Provides, Typevar[k], Tage[cptcovage], existcomb[n][m], FixedV[ncovcolt+k12]
13130: * Tprod[k1]=k Tposprod[k]=k1; Tvard[k1][1] =m;
13131: */
13132: /* Needs FixedV[Tvardk[k][1]] */
13133: /* For others:
13134: * Sets Typevar[k];
13135: * Tvar[k]=ncovcol+nqv+ntv+nqtv+k11;
13136: * Tposprod[k]=k11;
13137: * Tprod[k11]=k;
13138: * Tvardk[k][1] =m;
13139: * Needs FixedV[Tvardk[k][1]] == 0
13140: */
13141:
13142: for(k=1; k<=cptcovt;k++){ /* Loop on total covariates of the model line */
13143: cutl(stra,strb,modelsav,'+'); /* keeps in strb after the first '+' cutl from left to right
13144: 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" */
13145: if (nbocc(modelsav,'+')==0)
13146: strcpy(strb,modelsav); /* and analyzes it */
13147: /* printf("i=%d a=%s b=%s sav=%s\n",i, stra,strb,modelsav);*/
13148: /*scanf("%d",i);*/
13149: if (strchr(strb,'*')) { /**< Model includes a product V2+V1+V5*age+ V4+V3*age strb=V3*age OR double product with age strb=age*V6*V2 or V6*V2*age or V6*age*V2 */
13150: cutl(strc,strd,strb,'*'); /**< k=1 strd*strc Vm*Vn: strb=V3*age(input) strc=age strd=V3 ; V3*V2 strc=V2, strd=V3 OR strb=age*V6*V2 strc=V6*V2 strd=age OR c=V2*age OR c=age*V2 */
13151: if(strchr(strc,'*')) { /**< Model with age and DOUBLE product: allowed since 0.99r44, strc=V6*V2 or V2*age or age*V2, strd=age or V6 or V6 */
13152: Typevar[k]=3; /* 3 for age and double product age*Vn*Vm varying of fixed */
13153: if(strstr(strc,"age")!=0) { /* It means that strc=V2*age or age*V2 and thus that strd=Vn */
13154: cutl(stre,strf,strc,'*') ; /* strf=age or Vm, stre=Vm or age. If strc=V6*V2 then strf=V6 and stre=V2 */
13155: strcpy(strc,strb); /* save strb(=age*Vn*Vm) into strc */
13156: /* We want strb=Vn*Vm */
13157: if(strcmp(strf,"age")==0){ /* strf is "age" so that stre=Vm =V2 . */
13158: strcpy(strb,strd);
13159: strcat(strb,"*");
13160: strcat(strb,stre);
13161: }else{ /* strf=Vm If strf=V6 then stre=V2 */
13162: strcpy(strb,strf);
13163: strcat(strb,"*");
13164: strcat(strb,stre);
13165: strcpy(strd,strb); /* in order for strd to not be "age" for next test (will be Vn*Vm */
13166: }
13167: /* printf("DEBUG FIXED k=%d, Tage[k]=%d, Tvar[Tage[k]=%d,FixedV[Tvar[Tage[k]]]=%d\n",k,Tage[k],Tvar[Tage[k]],FixedV[Tvar[Tage[k]]]); */
13168: /* FixedV[Tvar[Tage[k]]]=0; /\* HERY not sure if V7*V4*age Fixed might not exist yet*\/ */
13169: }else{ /* strc=Vn*Vm (and strd=age) and should be strb=Vn*Vm but want to keep original strb double product */
13170: strcpy(stre,strb); /* save full b in stre */
13171: strcpy(strb,strc); /* save short c in new short b for next block strb=Vn*Vm*/
13172: strcpy(strf,strc); /* save short c in new short f */
13173: cutl(strc,strd,strf,'*'); /* We get strd=Vn and strc=Vm for next block (strb=Vn*Vm)*/
13174: /* strcpy(strc,stre);*/ /* save full e in c for future */
13175: }
13176: cptcovdageprod++; /* double product with age Which product is it? */
13177: /* strcpy(strb,strc); /\* strb was age*V6*V2 or V6*V2*age or V6*age*V2 IS now V6*V2 or V2*age or age*V2 *\/ */
13178: /* cutl(strc,strd,strb,'*'); /\* strd= V6 or V2 or age and strc= V2 or age or V2 *\/ */
13179: cutl(stre,strb,strc,'V'); /* strc= Vn, stre is n; strb=V3*V2 stre=3 strc=*/
13180: n=atoi(stre);
13181: cutl(strc,strb,strd,'V'); /* strd was Vm, strc is m */
13182: m=atoi(strc);
13183: cptcovage++; /* Counts the number of covariates which include age as a product */
13184: Tage[cptcovage]=k; /* For age*V3*V2 gives the position in model of covariates associated with age Tage[1]=6 HERY too*/
13185: if(existcomb[n][m] == 0){
13186: /* r /home/brouard/Documents/Recherches/REVES/Zachary/Zach-2022/Feinuo_Sun/Feinuo-threeway/femV12V15_3wayintNBe.imach */
13187: printf("Warning in model combination V%d*V%d should exist in the model before adding V%d*V%d*age !\n",n,m,n,m);
13188: fprintf(ficlog,"Warning in model combination V%d*V%d should exist in the model before adding V%d*V%d*age !\n",n,m,n,m);
13189: fflush(ficlog);
13190: k1++; /* The combination Vn*Vm will be in the model so we create it at k1 */
13191: k12++;
13192: existcomb[n][m]=k1;
13193: existcomb[m][n]=k1;
13194: Tvar[k]=ncovcol+nqv+ntv+nqtv+k1;
13195: Tprod[k1]=k; /* Tprod[1]=3(=V1*V4) for V2+V1+V1*V4+age*V3+V3*V2+ age*V6*V3 Gives the k position of the k1 double product Vn*Vm or age*Vn*Vm*/
13196: Tposprod[k]=k1; /* Tposprod[3]=1, Tposprod[2]=5 Gives the k1 double product Vn*Vm or age*Vn*Vm at the k position */
13197: Tvard[k1][1] =m; /* m 1 for V1*/
13198: Tvardk[k][1] =m; /* m 1 for V1*/
13199: Tvard[k1][2] =n; /* n 4 for V4*/
13200: Tvardk[k][2] =n; /* n 4 for V4*/
13201: /* Tvar[Tage[cptcovage]]=k1;*/ /* Tvar[6=age*V3*V2]=9 (new fixed covariate) */ /* We don't know about Fixed yet HERE */
13202: if( FixedV[Tvardk[k][1]] == 0 && FixedV[Tvardk[k][2]] == 0){ /* If the product is a fixed covariate then we feed the new column with Vn*Vm */
13203: for (i=1; i<=lastobs;i++){/* For fixed product */
13204: /* Computes the new covariate which is a product of
13205: covar[n][i]* covar[m][i] and stores it at ncovol+k1 May not be defined */
13206: covar[ncovcolt+k1][i]=covar[atoi(stre)][i]*covar[atoi(strc)][i];
13207: }
13208: cptcovprodage++; /* Counting the number of fixed covariate with age */
13209: FixedV[ncovcolt+k12]=0; /* We expand Vn*Vm */
13210: k12++;
13211: FixedV[ncovcolt+k12]=0;
13212: }else{ /*End of FixedV */
13213: cptcovprodvage++; /* Counting the number of varying covariate with age */
13214: FixedV[ncovcolt+k12]=1; /* We expand Vn*Vm */
13215: k12++;
13216: FixedV[ncovcolt+k12]=1;
13217: }
13218: }else{ /* k1 Vn*Vm already exists */
13219: k11=existcomb[n][m];
13220: Tposprod[k]=k11; /* OK */
13221: Tvar[k]=Tvar[Tprod[k11]]; /* HERY */
13222: Tvardk[k][1]=m;
13223: Tvardk[k][2]=n;
13224: if( FixedV[Tvardk[k][1]] == 0 && FixedV[Tvardk[k][2]] == 0){ /* If the product is a fixed covariate then we feed the new column with Vn*Vm */
13225: /*cptcovage++;*/ /* Counts the number of covariates which include age as a product */
13226: cptcovprodage++; /* Counting the number of fixed covariate with age */
13227: /*Tage[cptcovage]=k;*/ /* For age*V3*V2 Tage[1]=V3*V3=9 HERY too*/
13228: Tvar[Tage[cptcovage]]=k1;
13229: FixedV[ncovcolt+k12]=0; /* We expand Vn*Vm */
13230: k12++;
13231: FixedV[ncovcolt+k12]=0;
13232: }else{ /* Already exists but time varying (and age) */
13233: /*cptcovage++;*/ /* Counts the number of covariates which include age as a product */
13234: /*Tage[cptcovage]=k;*/ /* For age*V3*V2 Tage[1]=V3*V3=9 HERY too*/
13235: /* Tvar[Tage[cptcovage]]=k1; */
13236: cptcovprodvage++;
13237: FixedV[ncovcolt+k12]=1; /* We expand Vn*Vm */
13238: k12++;
13239: FixedV[ncovcolt+k12]=1;
13240: }
13241: }
13242: /* Tage[cptcovage]=k; /\* V2+V1+V4+V3*age Tvar[4]=3, Tage[1] = 4 or V1+V1*age Tvar[2]=1, Tage[1]=2 *\/ */
13243: /* Tvar[k]=k11; /\* HERY *\/ */
13244: } else {/* simple product strb=age*Vn so that c=Vn and d=age, or strb=Vn*age so that c=age and d=Vn, or b=Vn*Vm so that c=Vm and d=Vn */
13245: cptcovprod++;
13246: if (strcmp(strc,"age")==0) { /**< Model includes age: strb= Vn*age c=age d=Vn*/
13247: /* covar is not filled and then is empty */
13248: cutl(stre,strb,strd,'V'); /* strd=V3(input): stre="3" */
13249: 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 */
13250: Typevar[k]=1; /* 1 for age product */
13251: cptcovage++; /* Counts the number of covariates which include age as a product */
13252: Tage[cptcovage]=k; /* V2+V1+V4+V3*age Tvar[4]=3, Tage[1] = 4 or V1+V1*age Tvar[2]=1, Tage[1]=2 */
13253: if( FixedV[Tvar[k]] == 0){
13254: cptcovprodage++; /* Counting the number of fixed covariate with age */
13255: }else{
13256: cptcovprodvage++; /* Counting the number of fixedvarying covariate with age */
13257: }
13258: /*printf("stre=%s ", stre);*/
13259: } else if (strcmp(strd,"age")==0) { /* strb= age*Vn c=Vn */
13260: cutl(stre,strb,strc,'V');
13261: Tvar[k]=atoi(stre);
13262: Typevar[k]=1; /* 1 for age product */
13263: cptcovage++;
13264: Tage[cptcovage]=k;
13265: if( FixedV[Tvar[k]] == 0){
13266: cptcovprodage++; /* Counting the number of fixed covariate with age */
13267: }else{
13268: cptcovprodvage++; /* Counting the number of fixedvarying covariate with age */
13269: }
13270: }else{ /* for product Vn*Vm */
13271: Typevar[k]=2; /* 2 for product Vn*Vm */
13272: cutl(stre,strb,strc,'V'); /* strc= Vn, stre is n; strb=V3*V2 stre=3 strc=*/
13273: n=atoi(stre);
13274: cutl(strc,strb,strd,'V'); /* strd was Vm, strc is m */
13275: m=atoi(strc);
13276: k1++;
13277: cptcovprodnoage++;
13278: if(existcomb[n][m] != 0 || existcomb[m][n] != 0){
13279: printf("Warning in model combination V%d*V%d already exists in the model in position k1=%d!\n",n,m,existcomb[n][m]);
13280: fprintf(ficlog,"Warning in model combination V%d*V%d already exists in the model in position k1=%d!\n",n,m,existcomb[n][m]);
13281: fflush(ficlog);
13282: k11=existcomb[n][m];
13283: Tvar[k]=ncovcol+nqv+ntv+nqtv+k11;
13284: Tposprod[k]=k11;
13285: Tprod[k11]=k;
13286: Tvardk[k][1] =m; /* m 1 for V1*/
13287: /* Tvard[k11][1] =m; /\* n 4 for V4*\/ */
13288: Tvardk[k][2] =n; /* n 4 for V4*/
13289: /* Tvard[k11][2] =n; /\* n 4 for V4*\/ */
13290: }else{ /* combination Vn*Vm doesn't exist we create it (no age)*/
13291: existcomb[n][m]=k1;
13292: existcomb[m][n]=k1;
13293: Tvar[k]=ncovcol+nqv+ntv+nqtv+k1; /* ncovcolt+k1; For model-covariate k tells which data-covariate to use but
13294: because this model-covariate is a construction we invent a new column
13295: which is after existing variables ncovcol+nqv+ntv+nqtv + k1
13296: If already ncovcol=4 and model= V2 + V1 + V1*V4 + age*V3 + V3*V2
13297: thus after V4 we invent V5 and V6 because age*V3 will be computed in 4
13298: Tvar[3=V1*V4]=4+1=5 Tvar[5=V3*V2]=4 + 2= 6, Tvar[4=age*V3]=3 etc */
13299: /* Please remark that the new variables are model dependent */
13300: /* If we have 4 variable but the model uses only 3, like in
13301: * model= V1 + age*V1 + V2 + V3 + age*V2 + age*V3 + V1*V2 + V1*V3
13302: * k= 1 2 3 4 5 6 7 8
13303: * Tvar[k]=1 1 2 3 2 3 (5 6) (and not 4 5 because of V4 missing)
13304: * Tage[kk] [1]= 2 [2]=5 [3]=6 kk=1 to cptcovage=3
13305: * Tvar[Tage[kk]][1]=2 [2]=2 [3]=3
13306: */
13307: /* We need to feed some variables like TvarVV, but later on next loop because of ncovv (k2) is not correct */
13308: Tprod[k1]=k; /* Tprod[1]=3(=V1*V4) for V2+V1+V1*V4+age*V3+V3*V2 +V6*V2*age */
13309: Tposprod[k]=k1; /* Tposprod[3]=1, Tposprod[2]=5 */
13310: Tvard[k1][1] =m; /* m 1 for V1*/
13311: Tvardk[k][1] =m; /* m 1 for V1*/
13312: Tvard[k1][2] =n; /* n 4 for V4*/
13313: Tvardk[k][2] =n; /* n 4 for V4*/
13314: k2=k2+2; /* k2 is initialize to -1, We want to store the n and m in Vn*Vm at the end of Tvar */
13315: /* Tvar[cptcovt+k2]=Tvard[k1][1]; /\* Tvar[(cptcovt=4+k2=1)=5]= 1 (V1) *\/ */
13316: /* Tvar[cptcovt+k2+1]=Tvard[k1][2]; /\* Tvar[(cptcovt=4+(k2=1)+1)=6]= 4 (V4) *\/ */
13317: /*ncovcol=4 and model=V2+V1+V1*V4+age*V3+V3*V2, Tvar[3]=5, Tvar[4]=6, cptcovt=5 */
13318: /* 1 2 3 4 5 | Tvar[5+1)=1, Tvar[7]=2 */
13319: if( FixedV[Tvardk[k][1]] == 0 && FixedV[Tvardk[k][2]] == 0){ /* If the product is a fixed covariate then we feed the new column with Vn*Vm */
13320: for (i=1; i<=lastobs;i++){/* For fixed product */
13321: /* Computes the new covariate which is a product of
13322: covar[n][i]* covar[m][i] and stores it at ncovol+k1 May not be defined */
13323: covar[ncovcolt+k1][i]=covar[atoi(stre)][i]*covar[atoi(strc)][i];
13324: }
13325: /* TvarVV[k2]=n; */
13326: /* FixedV[ncovcolt+k2]=0; /\* or FixedV[Tvar[k]]=0; FixedV[TvarVV[ncovv]]=0 HERE *\/ */
13327: /* TvarVV[k2+1]=m; */
13328: /* FixedV[ncovcolt+k2]=0; /\* or FixedV[Tvar[k]]=0; FixedV[TvarVV[ncovv]]=0 HERE *\/ */
13329: }else{ /* not FixedV */
13330: /* TvarVV[k2]=n; */
13331: /* FixedV[ncovcolt+k2]=0; /\* or FixedV[Tvar[k]]=0; FixedV[TvarVV[ncovv]]=0 HERE *\/ */
13332: /* TvarVV[k2+1]=m; */
13333: /* FixedV[ncovcolt+k2]=0; /\* or FixedV[Tvar[k]]=0; FixedV[TvarVV[ncovv]]=0 HERE *\/ */
13334: }
13335: } /* End of creation of Vn*Vm if not created by age*Vn*Vm earlier */
13336: } /* End of product Vn*Vm */
13337: } /* End of age*double product or simple product */
13338: }else { /* not a product */
13339: /*printf("d=%s c=%s b=%s\n", strd,strc,strb);*/
13340: /* scanf("%d",i);*/
13341: cutl(strd,strc,strb,'V');
13342: ks++; /**< Number of simple covariates dummy or quantitative, fixe or varying */
13343: cptcovn++; /** V4+V3+V5: V4 and V3 timevarying dummy covariates, V5 timevarying quantitative */
13344: Tvar[k]=atoi(strd);
13345: Typevar[k]=0; /* 0 for simple covariates */
13346: }
13347: strcpy(modelsav,stra); /* modelsav=V2+V1+V4 stra=V2+V1+V4 */
13348: /*printf("a=%s b=%s sav=%s\n", stra,strb,modelsav);
13349: scanf("%d",i);*/
13350: } /* end of loop + on total covariates */
13351:
13352:
13353: } /* end if strlen(modelsave == 0) age*age might exist */
13354: } /* end if strlen(model == 0) */
13355: cptcovs=cptcovt - cptcovdageprod - cptcovprod;/**< Number of simple covariates V1 +V1*age +V3 +V3*V4 +age*age + age*v4*V3=> V1 + V3 =4+1-3=2 */
13356:
13357: /*The number n of Vn is stored in Tvar. cptcovage =number of age covariate. Tage gives the position of age. cptcovprod= number of products.
13358: If model=V1+V1*age then Tvar[1]=1 Tvar[2]=1 cptcovage=1 Tage[1]=2 cptcovprod=0*/
13359:
13360: /* printf("tvar1=%d tvar2=%d tvar3=%d cptcovage=%d Tage=%d",Tvar[1],Tvar[2],Tvar[3],cptcovage,Tage[1]);
13361: printf("cptcovprod=%d ", cptcovprod);
13362: fprintf(ficlog,"cptcovprod=%d ", cptcovprod);
13363: scanf("%d ",i);*/
13364:
13365:
13366: /* Until here, decodemodel knows only the grammar (simple, product, age*) of the model but not what kind
13367: of variable (dummy vs quantitative, fixed vs time varying) is behind. But we know the # of each. */
13368: /* ncovcol= 1, nqv=1 | ntv=2, nqtv= 1 = 5 possible variables data: 2 fixed 3, varying
13369: model= V5 + V4 +V3 + V4*V3 + V5*age + V2 + V1*V2 + V1*age + V5*age, V1 is not used saving its place
13370: k = 1 2 3 4 5 6 7 8 9
13371: Tvar[k]= 5 4 3 1+1+2+1+1=6 5 2 7 1 5
13372: Typevar[k]= 0 0 0 2 1 0 2 1 0
13373: Fixed[k] 1 1 1 1 3 0 0 or 2 2 3
13374: Dummy[k] 1 0 0 0 3 1 1 2 3
13375: Tmodelind[combination of covar]=k;
13376: */
13377: /* Dispatching between quantitative and time varying covariates */
13378: /* If Tvar[k] >ncovcol it is a product */
13379: /* Tvar[k] is the value n of Vn with n varying for 1 to nvcol, or p Vp=Vn*Vm for product */
13380: /* Computing effective variables, ie used by the model, that is from the cptcovt variables */
13381: printf("Model=1+age+%s\n\
13382: Typevar: 0 for simple covariate (dummy, quantitative, fixed or varying), 1 for age product, 2 for product, 3 for double product with age \n\
13383: Fixed[k] 0=fixed (product or simple), 1 varying, 2 fixed with age product, 3 varying with age product \n\
13384: 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);
13385: fprintf(ficlog,"Model=1+age+%s\n\
13386: Typevar: 0 for simple covariate (dummy, quantitative, fixed or varying), 1 for age product, 2 for product, 3 for double product with age \n\
13387: Fixed[k] 0=fixed (product or simple), 1 varying, 2 fixed with age product, 3 varying with age product \n\
13388: 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);
13389: for(k=-1;k<=NCOVMAX; k++){ Fixed[k]=0; Dummy[k]=0;}
13390: for(k=1;k<=NCOVMAX; k++){TvarFind[k]=0; TvarVind[k]=0;}
13391:
13392:
13393: /* Second loop for calculating Fixed[k], Dummy[k]*/
13394:
13395:
13396: for(k=1, ncovf=0, nsd=0, nsq=0, ncovv=0,ncovva=0,ncovvta=0, ncova=0, ncoveff=0, nqfveff=0, ntveff=0, nqtveff=0, ncovvt=0;k<=cptcovt; k++){ /* or cptocvt loop on k from model */
13397: if (Tvar[k] <=ncovcol && Typevar[k]==0 ){ /* Simple fixed dummy (<=ncovcol) covariates */
13398: Fixed[k]= 0;
13399: Dummy[k]= 0;
13400: ncoveff++;
13401: ncovf++;
13402: nsd++;
13403: modell[k].maintype= FTYPE;
13404: TvarsD[nsd]=Tvar[k];
13405: TvarsDind[nsd]=k;
13406: TnsdVar[Tvar[k]]=nsd;
13407: TvarF[ncovf]=Tvar[k];
13408: TvarFind[ncovf]=k;
13409: TvarFD[ncoveff]=Tvar[k]; /* TvarFD[1]=V1 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
13410: TvarFDind[ncoveff]=k; /* TvarFDind[1]=9 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
13411: /* }else if( Tvar[k] <=ncovcol && Typevar[k]==2){ /\* Product of fixed dummy (<=ncovcol) covariates For a fixed product k is higher than ncovcol *\/ */
13412: }else if( Tvar[k] <=ncovcol+nqv && Typevar[k]==0){/* Remind that product Vn*Vm are added in k Only simple fixed quantitative variable */
13413: Fixed[k]= 0;
13414: Dummy[k]= 1;
13415: nqfveff++;
13416: modell[k].maintype= FTYPE;
13417: modell[k].subtype= FQ;
13418: nsq++;
13419: TvarsQ[nsq]=Tvar[k]; /* Gives the variable name (extended to products) of first nsq simple quantitative covariates (fixed or time vary see below */
13420: TvarsQind[nsq]=k; /* Gives the position in the model equation of the first nsq simple quantitative covariates (fixed or time vary) */
13421: ncovf++;
13422: TvarF[ncovf]=Tvar[k];
13423: TvarFind[ncovf]=k;
13424: 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 */
13425: 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 */
13426: }else if( Tvar[k] <=ncovcol+nqv+ntv && Typevar[k]==0){/* Only simple time varying dummy variables */
13427: /*# ID V1 V2 weight birth death 1st s1 V3 V4 V5 2nd s2 */
13428: /* model V1+V3+age*V1+age*V3+V1*V3 */
13429: /* Tvar={1, 3, 1, 3, 6}, the 6 comes from the fact that there are already V1, V2, V3, V4, V5 native covariates */
13430: ncovvt++;
13431: TvarVV[ncovvt]=Tvar[k]; /* TvarVV[1]=V3 (first time varying in the model equation */
13432: TvarVVind[ncovvt]=k; /* TvarVVind[1]=2 (second position in the model equation */
13433:
13434: Fixed[k]= 1;
13435: Dummy[k]= 0;
13436: ntveff++; /* Only simple time varying dummy variable */
13437: modell[k].maintype= VTYPE;
13438: modell[k].subtype= VD;
13439: nsd++;
13440: TvarsD[nsd]=Tvar[k];
13441: TvarsDind[nsd]=k;
13442: TnsdVar[Tvar[k]]=nsd; /* To be verified */
13443: ncovv++; /* Only simple time varying variables */
13444: TvarV[ncovv]=Tvar[k];
13445: 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 */
13446: 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 */
13447: 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 */
13448: 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);
13449: printf("Quasi TmodelInvind[%d]=%d\n",k,Tvar[k]- ncovcol-nqv);
13450: }else if( Tvar[k] <=ncovcol+nqv+ntv+nqtv && Typevar[k]==0){ /* Only simple time varying quantitative variable V5*/
13451: /*# ID V1 V2 weight birth death 1st s1 V3 V4 V5 2nd s2 */
13452: /* model V1+V3+age*V1+age*V3+V1*V3 */
13453: /* Tvar={1, 3, 1, 3, 6}, the 6 comes from the fact that there are already V1, V2, V3, V4, V5 native covariates */
13454: ncovvt++;
13455: TvarVV[ncovvt]=Tvar[k]; /* TvarVV[1]=V3 (first time varying in the model equation */
13456: TvarVVind[ncovvt]=k; /* TvarVV[1]=V3 (first time varying in the model equation */
13457:
13458: Fixed[k]= 1;
13459: Dummy[k]= 1;
13460: nqtveff++;
13461: modell[k].maintype= VTYPE;
13462: modell[k].subtype= VQ;
13463: ncovv++; /* Only simple time varying variables */
13464: nsq++;
13465: 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) */
13466: 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) */
13467: TvarV[ncovv]=Tvar[k];
13468: TvarVind[ncovv]=k; /* TvarVind[1]=1 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ /* Any time varying singele */
13469: 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 */
13470: 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 */
13471: TmodelInvQind[nqtveff]=Tvar[k]- ncovcol-nqv-ntv;/* Only simple time varying quantitative variable */
13472: /* Tmodeliqind[k]=nqtveff;/\* Only simple time varying quantitative variable *\/ */
13473: /* printf("Quasi TmodelQind[%d]=%d,Tvar[TmodelQind[%d]]=V%d, ncovcol=%d, nqv=%d, ntv=%Ad,Tvar[k]- ncovcol-nqv-ntv=%d\n",nqtveff,k,nqtveff,Tvar[k], ncovcol, nqv, ntv, Tvar[k]- ncovcol-nqv-ntv); */
13474: /* printf("Quasi TmodelInvQind[%d]=%d\n",k,Tvar[k]- ncovcol-nqv-ntv); */
13475: }else if (Typevar[k] == 1) { /* product with age */
13476: ncova++;
13477: TvarA[ncova]=Tvar[k];
13478: TvarAind[ncova]=k;
13479: /** Fixed[k] 0=fixed, 1 varying, 2 fixed with age product, 3 varying with age product */
13480: /** Dummy[k] 0=dummy (0 1), 1 quantitative (single or product without age), 2 dummy with age product, 3 quant with age product */
13481: if (Tvar[k] <=ncovcol ){ /* Product age with fixed dummy covariatee */
13482: Fixed[k]= 2;
13483: Dummy[k]= 2;
13484: modell[k].maintype= ATYPE;
13485: modell[k].subtype= APFD;
13486: ncovta++;
13487: TvarAVVA[ncovta]=Tvar[k]; /* (2)age*V3 */
13488: TvarAVVAind[ncovta]=k;
13489: /* ncoveff++; */
13490: }else if( Tvar[k] <=ncovcol+nqv) { /* Remind that product Vn*Vm are added in k*/
13491: Fixed[k]= 2;
13492: Dummy[k]= 3;
13493: modell[k].maintype= ATYPE;
13494: modell[k].subtype= APFQ; /* Product age * fixed quantitative */
13495: ncovta++;
13496: TvarAVVA[ncovta]=Tvar[k]; /* */
13497: TvarAVVAind[ncovta]=k;
13498: /* nqfveff++; /\* Only simple fixed quantitative variable *\/ */
13499: }else if( Tvar[k] <=ncovcol+nqv+ntv ){
13500: Fixed[k]= 3;
13501: Dummy[k]= 2;
13502: modell[k].maintype= ATYPE;
13503: modell[k].subtype= APVD; /* Product age * varying dummy */
13504: ncovva++;
13505: TvarVVA[ncovva]=Tvar[k]; /* (1)+age*V6 + (2)age*V7 */
13506: TvarVVAind[ncovva]=k;
13507: ncovta++;
13508: TvarAVVA[ncovta]=Tvar[k]; /* */
13509: TvarAVVAind[ncovta]=k;
13510: /* ntveff++; /\* Only simple time varying dummy variable *\/ */
13511: }else if( Tvar[k] <=ncovcol+nqv+ntv+nqtv){
13512: Fixed[k]= 3;
13513: Dummy[k]= 3;
13514: modell[k].maintype= ATYPE;
13515: modell[k].subtype= APVQ; /* Product age * varying quantitative */
13516: ncovva++;
13517: TvarVVA[ncovva]=Tvar[k]; /* */
13518: TvarVVAind[ncovva]=k;
13519: ncovta++;
13520: TvarAVVA[ncovta]=Tvar[k]; /* (1)+age*V6 + (2)age*V7 */
13521: TvarAVVAind[ncovta]=k;
13522: /* nqtveff++;/\* Only simple time varying quantitative variable *\/ */
13523: }
13524: }else if( Tposprod[k]>0 && Typevar[k]==2){ /* Detects if fixed product no age Vm*Vn */
13525: printf("MEMORY ERRORR k=%d Tposprod[k]=%d, Typevar[k]=%d\n ",k, Tposprod[k], Typevar[k]);
13526: if(FixedV[Tvardk[k][1]] == 0 && FixedV[Tvardk[k][2]] == 0){ /* Needs a fixed product Product of fixed dummy (<=ncovcol) covariates For a fixed product k is higher than ncovcol V3*V2 */
13527: printf("MEMORY ERRORR k=%d Tvardk[k][1]=%d, Tvardk[k][2]=%d, FixedV[Tvardk[k][1]]=%d,FixedV[Tvardk[k][2]]=%d\n ",k,Tvardk[k][1],Tvardk[k][2],FixedV[Tvardk[k][1]],FixedV[Tvardk[k][2]]);
13528: Fixed[k]= 0;
13529: Dummy[k]= 0;
13530: ncoveff++;
13531: ncovf++;
13532: /* ncovv++; */
13533: /* TvarVV[ncovv]=Tvardk[k][1]; */
13534: /* FixedV[ncovcolt+ncovv]=0; /\* or FixedV[TvarVV[ncovv]]=0 HERE *\/ */
13535: /* ncovv++; */
13536: /* TvarVV[ncovv]=Tvardk[k][2]; */
13537: /* FixedV[ncovcolt+ncovv]=0; /\* or FixedV[TvarVV[ncovv]]=0 HERE *\/ */
13538: modell[k].maintype= FTYPE;
13539: TvarF[ncovf]=Tvar[k];
13540: /* TnsdVar[Tvar[k]]=nsd; */ /* To be done */
13541: TvarFind[ncovf]=k;
13542: TvarFD[ncoveff]=Tvar[k]; /* TvarFD[1]=V1 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
13543: TvarFDind[ncoveff]=k; /* TvarFDind[1]=9 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
13544: }else{/* product varying Vn * Vm without age, V1+V3+age*V1+age*V3+V1*V3 looking at V1*V3, Typevar={0, 0, 1, 1, 2}, k=5, V1 is fixed, V3 is timevary, V5 is a product */
13545: /*# ID V1 V2 weight birth death 1st s1 V3 V4 V5 2nd s2 */
13546: /* model V1+V3+age*V1+age*V3+V1*V3 + V1*V3*age*/
13547: /* Tvar={1, 3, 1, 3, 6, 6}, the 6 comes from the fact that there are already V1, V2, V3, V4, V5 native covariates */
13548: k1=Tposprod[k]; /* Position in the products of product k, Tposprod={0, 0, 0, 0, 1, 1} k1=1 first product but second time varying because of V3 */
13549: ncovvt++;
13550: TvarVV[ncovvt]=Tvard[k1][1]; /* TvarVV[2]=V1 (because TvarVV[1] was V3, first time varying covariates */
13551: TvarVVind[ncovvt]=k; /* TvarVVind[2]=5 (because TvarVVind[2] was V1*V3 at position 5 */
13552: ncovvt++;
13553: TvarVV[ncovvt]=Tvard[k1][2]; /* TvarVV[3]=V3 */
13554: TvarVVind[ncovvt]=k; /* TvarVVind[2]=5 (because TvarVVind[2] was V1*V3 at position 5 */
13555:
13556: /** Fixed[k] 0=fixed, 1 varying, 2 fixed with age product, 3 varying with age product */
13557: /** Dummy[k] 0=dummy (0 1), 1 quantitative (single or product without age), 2 dummy with age product, 3 quant with age product */
13558:
13559: if(Tvard[k1][1] <=ncovcol){ /* Vn is dummy fixed, (Tvard[1][1]=V1), (Tvard[1][1]=V3 time varying) */
13560: if(Tvard[k1][2] <=ncovcol){ /* Vm is dummy fixed */
13561: Fixed[k]= 1;
13562: Dummy[k]= 0;
13563: modell[k].maintype= FTYPE;
13564: modell[k].subtype= FPDD; /* Product fixed dummy * fixed dummy */
13565: ncovf++; /* Fixed variables without age */
13566: TvarF[ncovf]=Tvar[k];
13567: TvarFind[ncovf]=k;
13568: }else if(Tvard[k1][2] <=ncovcol+nqv){ /* Vm is quanti fixed */
13569: Fixed[k]= 0; /* Fixed product */
13570: Dummy[k]= 1;
13571: modell[k].maintype= FTYPE;
13572: modell[k].subtype= FPDQ; /* Product fixed dummy * fixed quantitative */
13573: ncovf++; /* Varying variables without age */
13574: TvarF[ncovf]=Tvar[k];
13575: TvarFind[ncovf]=k;
13576: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv){ /* Vm is a time varying dummy covariate */
13577: Fixed[k]= 1;
13578: Dummy[k]= 0;
13579: modell[k].maintype= VTYPE;
13580: modell[k].subtype= VPDD; /* Product fixed dummy * varying dummy */
13581: ncovv++; /* Varying variables without age */
13582: TvarV[ncovv]=Tvar[k]; /* TvarV[1]=Tvar[5]=5 because there is a V4 */
13583: TvarVind[ncovv]=k;/* TvarVind[1]=5 */
13584: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv+nqtv){ /* Vm is a time varying quantitative covariate */
13585: Fixed[k]= 1;
13586: Dummy[k]= 1;
13587: modell[k].maintype= VTYPE;
13588: modell[k].subtype= VPDQ; /* Product fixed dummy * varying quantitative */
13589: ncovv++; /* Varying variables without age */
13590: TvarV[ncovv]=Tvar[k];
13591: TvarVind[ncovv]=k;
13592: }
13593: }else if(Tvard[k1][1] <=ncovcol+nqv){ /* Vn is fixed quanti */
13594: if(Tvard[k1][2] <=ncovcol){ /* Vm is fixed dummy */
13595: Fixed[k]= 0; /* Fixed product */
13596: Dummy[k]= 1;
13597: modell[k].maintype= FTYPE;
13598: modell[k].subtype= FPDQ; /* Product fixed quantitative * fixed dummy */
13599: ncovf++; /* Fixed variables without age */
13600: TvarF[ncovf]=Tvar[k];
13601: TvarFind[ncovf]=k;
13602: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv){ /* Vm is time varying */
13603: Fixed[k]= 1;
13604: Dummy[k]= 1;
13605: modell[k].maintype= VTYPE;
13606: modell[k].subtype= VPDQ; /* Product fixed quantitative * varying dummy */
13607: ncovv++; /* Varying variables without age */
13608: TvarV[ncovv]=Tvar[k];
13609: TvarVind[ncovv]=k;
13610: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv+nqtv){ /* Vm is time varying quanti */
13611: Fixed[k]= 1;
13612: Dummy[k]= 1;
13613: modell[k].maintype= VTYPE;
13614: modell[k].subtype= VPQQ; /* Product fixed quantitative * varying quantitative */
13615: ncovv++; /* Varying variables without age */
13616: TvarV[ncovv]=Tvar[k];
13617: TvarVind[ncovv]=k;
13618: ncovv++; /* Varying variables without age */
13619: TvarV[ncovv]=Tvar[k];
13620: TvarVind[ncovv]=k;
13621: }
13622: }else if(Tvard[k1][1] <=ncovcol+nqv+ntv){ /* Vn is time varying dummy */
13623: if(Tvard[k1][2] <=ncovcol){
13624: Fixed[k]= 1;
13625: Dummy[k]= 1;
13626: modell[k].maintype= VTYPE;
13627: modell[k].subtype= VPDD; /* Product time varying dummy * fixed dummy */
13628: ncovv++; /* Varying variables without age */
13629: TvarV[ncovv]=Tvar[k];
13630: TvarVind[ncovv]=k;
13631: }else if(Tvard[k1][2] <=ncovcol+nqv){
13632: Fixed[k]= 1;
13633: Dummy[k]= 1;
13634: modell[k].maintype= VTYPE;
13635: modell[k].subtype= VPDQ; /* Product time varying dummy * fixed quantitative */
13636: ncovv++; /* Varying variables without age */
13637: TvarV[ncovv]=Tvar[k];
13638: TvarVind[ncovv]=k;
13639: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv){
13640: Fixed[k]= 1;
13641: Dummy[k]= 0;
13642: modell[k].maintype= VTYPE;
13643: modell[k].subtype= VPDD; /* Product time varying dummy * time varying dummy */
13644: ncovv++; /* Varying variables without age */
13645: TvarV[ncovv]=Tvar[k];
13646: TvarVind[ncovv]=k;
13647: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv+nqtv){
13648: Fixed[k]= 1;
13649: Dummy[k]= 1;
13650: modell[k].maintype= VTYPE;
13651: modell[k].subtype= VPDQ; /* Product time varying dummy * time varying quantitative */
13652: ncovv++; /* Varying variables without age */
13653: TvarV[ncovv]=Tvar[k];
13654: TvarVind[ncovv]=k;
13655: }
13656: }else if(Tvard[k1][1] <=ncovcol+nqv+ntv+nqtv){ /* Vn is time varying quanti */
13657: if(Tvard[k1][2] <=ncovcol){
13658: Fixed[k]= 1;
13659: Dummy[k]= 1;
13660: modell[k].maintype= VTYPE;
13661: modell[k].subtype= VPDQ; /* Product time varying quantitative * fixed dummy */
13662: ncovv++; /* Varying variables without age */
13663: TvarV[ncovv]=Tvar[k];
13664: TvarVind[ncovv]=k;
13665: }else if(Tvard[k1][2] <=ncovcol+nqv){
13666: Fixed[k]= 1;
13667: Dummy[k]= 1;
13668: modell[k].maintype= VTYPE;
13669: modell[k].subtype= VPQQ; /* Product time varying quantitative * fixed quantitative */
13670: ncovv++; /* Varying variables without age */
13671: TvarV[ncovv]=Tvar[k];
13672: TvarVind[ncovv]=k;
13673: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv){
13674: Fixed[k]= 1;
13675: Dummy[k]= 1;
13676: modell[k].maintype= VTYPE;
13677: modell[k].subtype= VPDQ; /* Product time varying quantitative * time varying dummy */
13678: ncovv++; /* Varying variables without age */
13679: TvarV[ncovv]=Tvar[k];
13680: TvarVind[ncovv]=k;
13681: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv+nqtv){
13682: Fixed[k]= 1;
13683: Dummy[k]= 1;
13684: modell[k].maintype= VTYPE;
13685: modell[k].subtype= VPQQ; /* Product time varying quantitative * time varying quantitative */
13686: ncovv++; /* Varying variables without age */
13687: TvarV[ncovv]=Tvar[k];
13688: TvarVind[ncovv]=k;
13689: }
13690: }else{
13691: printf("Error unknown type of covariate: Tvard[%d][1]=%d,Tvard[%d][2]=%d\n",k1,Tvard[k1][1],k1,Tvard[k1][2]);
13692: fprintf(ficlog,"Error unknown type of covariate: Tvard[%d][1]=%d,Tvard[%d][2]=%d\n",k1,Tvard[k1][1],k1,Tvard[k1][2]);
13693: } /*end k1*/
13694: }
13695: }else if(Typevar[k] == 3){ /* product Vn * Vm with age, V1+V3+age*V1+age*V3+V1*V3 looking at V1*V3, Typevar={0, 0, 1, 1, 2}, k=5, V1 is fixed, V3 is timevary, V5 is a product */
13696: /*# ID V1 V2 weight birth death 1st s1 V3 V4 V5 2nd s2 */
13697: /* model V1+V3+age*V1+age*V3+V1*V3 + V1*V3*age*/
13698: /* Tvar={1, 3, 1, 3, 6, 6}, the 6 comes from the fact that there are already V1, V2, V3, V4, V5 native covariates */
13699: k1=Tposprod[k]; /* Position in the products of product k, Tposprod={0, 0, 0, 0, 1, 1} k1=1 first product but second time varying because of V3 */
13700: ncova++;
13701: TvarA[ncova]=Tvard[k1][1]; /* TvarVV[2]=V1 (because TvarVV[1] was V3, first time varying covariates */
13702: TvarAind[ncova]=k; /* TvarVVind[2]=5 (because TvarVVind[2] was V1*V3 at position 5 */
13703: ncova++;
13704: TvarA[ncova]=Tvard[k1][2]; /* TvarVV[3]=V3 */
13705: TvarAind[ncova]=k; /* TvarVVind[2]=5 (because TvarVVind[2] was V1*V3 at position 5 */
13706:
13707: /** Fixed[k] 0=fixed, 1 varying, 2 fixed with age product, 3 varying with age product */
13708: /** Dummy[k] 0=dummy (0 1), 1 quantitative (single or product without age), 2 dummy with age product, 3 quant with age product */
13709: if( FixedV[Tvardk[k][1]] == 0 && FixedV[Tvardk[k][2]] == 0){
13710: ncovta++;
13711: TvarAVVA[ncovta]=Tvard[k1][1]; /* age*V6*V3 +age*V7*V3 + age*V6*V4 +age*V7*V4 */
13712: TvarAVVAind[ncovta]=k;
13713: ncovta++;
13714: TvarAVVA[ncovta]=Tvard[k1][2]; /* age*V6*V3 +age*V7*V3 + age*V6*V4 +age*V7*V4 */
13715: TvarAVVAind[ncovta]=k;
13716: }else{
13717: ncovva++; /* HERY reached */
13718: TvarVVA[ncovva]=Tvard[k1][1]; /* age*V6*V3 +age*V7*V3 + age*V6*V4 +age*V7*V4 */
13719: TvarVVAind[ncovva]=k;
13720: ncovva++;
13721: TvarVVA[ncovva]=Tvard[k1][2]; /* */
13722: TvarVVAind[ncovva]=k;
13723: ncovta++;
13724: TvarAVVA[ncovta]=Tvard[k1][1]; /* age*V6*V3 +age*V7*V3 + age*V6*V4 +age*V7*V4 */
13725: TvarAVVAind[ncovta]=k;
13726: ncovta++;
13727: TvarAVVA[ncovta]=Tvard[k1][2]; /* age*V6*V3 +age*V7*V3 + age*V6*V4 +age*V7*V4 */
13728: TvarAVVAind[ncovta]=k;
13729: }
13730: if(Tvard[k1][1] <=ncovcol){ /* Vn is dummy fixed, (Tvard[1][1]=V1), (Tvard[1][1]=V3 time varying) */
13731: if(Tvard[k1][2] <=ncovcol){ /* Vm is dummy fixed */
13732: Fixed[k]= 2;
13733: Dummy[k]= 2;
13734: modell[k].maintype= FTYPE;
13735: modell[k].subtype= FPDD; /* Product fixed dummy * fixed dummy */
13736: /* TvarF[ncova]=Tvar[k]; /\* Problem to solve *\/ */
13737: /* TvarFind[ncova]=k; */
13738: }else if(Tvard[k1][2] <=ncovcol+nqv){ /* Vm is quanti fixed */
13739: Fixed[k]= 2; /* Fixed product */
13740: Dummy[k]= 3;
13741: modell[k].maintype= FTYPE;
13742: modell[k].subtype= FPDQ; /* Product fixed dummy * fixed quantitative */
13743: /* TvarF[ncova]=Tvar[k]; */
13744: /* TvarFind[ncova]=k; */
13745: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv){ /* Vm is a time varying dummy covariate */
13746: Fixed[k]= 3;
13747: Dummy[k]= 2;
13748: modell[k].maintype= VTYPE;
13749: modell[k].subtype= VPDD; /* Product fixed dummy * varying dummy */
13750: TvarV[ncova]=Tvar[k]; /* TvarV[1]=Tvar[5]=5 because there is a V4 */
13751: TvarVind[ncova]=k;/* TvarVind[1]=5 */
13752: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv+nqtv){ /* Vm is a time varying quantitative covariate */
13753: Fixed[k]= 3;
13754: Dummy[k]= 3;
13755: modell[k].maintype= VTYPE;
13756: modell[k].subtype= VPDQ; /* Product fixed dummy * varying quantitative */
13757: /* ncovv++; /\* Varying variables without age *\/ */
13758: /* TvarV[ncovv]=Tvar[k]; */
13759: /* TvarVind[ncovv]=k; */
13760: }
13761: }else if(Tvard[k1][1] <=ncovcol+nqv){ /* Vn is fixed quanti */
13762: if(Tvard[k1][2] <=ncovcol){ /* Vm is fixed dummy */
13763: Fixed[k]= 2; /* Fixed product */
13764: Dummy[k]= 2;
13765: modell[k].maintype= FTYPE;
13766: modell[k].subtype= FPDQ; /* Product fixed quantitative * fixed dummy */
13767: /* ncova++; /\* Fixed variables with age *\/ */
13768: /* TvarF[ncovf]=Tvar[k]; */
13769: /* TvarFind[ncovf]=k; */
13770: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv){ /* Vm is time varying */
13771: Fixed[k]= 2;
13772: Dummy[k]= 3;
13773: modell[k].maintype= VTYPE;
13774: modell[k].subtype= VPDQ; /* Product fixed quantitative * varying dummy */
13775: /* ncova++; /\* Varying variables with age *\/ */
13776: /* TvarV[ncova]=Tvar[k]; */
13777: /* TvarVind[ncova]=k; */
13778: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv+nqtv){ /* Vm is time varying quanti */
13779: Fixed[k]= 3;
13780: Dummy[k]= 2;
13781: modell[k].maintype= VTYPE;
13782: modell[k].subtype= VPQQ; /* Product fixed quantitative * varying quantitative */
13783: ncova++; /* Varying variables without age */
13784: TvarV[ncova]=Tvar[k];
13785: TvarVind[ncova]=k;
13786: /* ncova++; /\* Varying variables without age *\/ */
13787: /* TvarV[ncova]=Tvar[k]; */
13788: /* TvarVind[ncova]=k; */
13789: }
13790: }else if(Tvard[k1][1] <=ncovcol+nqv+ntv){ /* Vn is time varying dummy */
13791: if(Tvard[k1][2] <=ncovcol){
13792: Fixed[k]= 2;
13793: Dummy[k]= 2;
13794: modell[k].maintype= VTYPE;
13795: modell[k].subtype= VPDD; /* Product time varying dummy * fixed dummy */
13796: /* ncova++; /\* Varying variables with age *\/ */
13797: /* TvarV[ncova]=Tvar[k]; */
13798: /* TvarVind[ncova]=k; */
13799: }else if(Tvard[k1][2] <=ncovcol+nqv){
13800: Fixed[k]= 2;
13801: Dummy[k]= 3;
13802: modell[k].maintype= VTYPE;
13803: modell[k].subtype= VPDQ; /* Product time varying dummy * fixed quantitative */
13804: /* ncova++; /\* Varying variables with age *\/ */
13805: /* TvarV[ncova]=Tvar[k]; */
13806: /* TvarVind[ncova]=k; */
13807: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv){
13808: Fixed[k]= 3;
13809: Dummy[k]= 2;
13810: modell[k].maintype= VTYPE;
13811: modell[k].subtype= VPDD; /* Product time varying dummy * time varying dummy */
13812: /* ncova++; /\* Varying variables with age *\/ */
13813: /* TvarV[ncova]=Tvar[k]; */
13814: /* TvarVind[ncova]=k; */
13815: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv+nqtv){
13816: Fixed[k]= 3;
13817: Dummy[k]= 3;
13818: modell[k].maintype= VTYPE;
13819: modell[k].subtype= VPDQ; /* Product time varying dummy * time varying quantitative */
13820: /* ncova++; /\* Varying variables with age *\/ */
13821: /* TvarV[ncova]=Tvar[k]; */
13822: /* TvarVind[ncova]=k; */
13823: }
13824: }else if(Tvard[k1][1] <=ncovcol+nqv+ntv+nqtv){ /* Vn is time varying quanti */
13825: if(Tvard[k1][2] <=ncovcol){
13826: Fixed[k]= 2;
13827: Dummy[k]= 2;
13828: modell[k].maintype= VTYPE;
13829: modell[k].subtype= VPDQ; /* Product time varying quantitative * fixed dummy */
13830: /* ncova++; /\* Varying variables with age *\/ */
13831: /* TvarV[ncova]=Tvar[k]; */
13832: /* TvarVind[ncova]=k; */
13833: }else if(Tvard[k1][2] <=ncovcol+nqv){
13834: Fixed[k]= 2;
13835: Dummy[k]= 3;
13836: modell[k].maintype= VTYPE;
13837: modell[k].subtype= VPQQ; /* Product time varying quantitative * fixed quantitative */
13838: /* ncova++; /\* Varying variables with age *\/ */
13839: /* TvarV[ncova]=Tvar[k]; */
13840: /* TvarVind[ncova]=k; */
13841: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv){
13842: Fixed[k]= 3;
13843: Dummy[k]= 2;
13844: modell[k].maintype= VTYPE;
13845: modell[k].subtype= VPDQ; /* Product time varying quantitative * time varying dummy */
13846: /* ncova++; /\* Varying variables with age *\/ */
13847: /* TvarV[ncova]=Tvar[k]; */
13848: /* TvarVind[ncova]=k; */
13849: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv+nqtv){
13850: Fixed[k]= 3;
13851: Dummy[k]= 3;
13852: modell[k].maintype= VTYPE;
13853: modell[k].subtype= VPQQ; /* Product time varying quantitative * time varying quantitative */
13854: /* ncova++; /\* Varying variables with age *\/ */
13855: /* TvarV[ncova]=Tvar[k]; */
13856: /* TvarVind[ncova]=k; */
13857: }
13858: }else{
13859: printf("Error unknown type of covariate: Tvard[%d][1]=%d,Tvard[%d][2]=%d\n",k1,Tvard[k1][1],k1,Tvard[k1][2]);
13860: fprintf(ficlog,"Error unknown type of covariate: Tvard[%d][1]=%d,Tvard[%d][2]=%d\n",k1,Tvard[k1][1],k1,Tvard[k1][2]);
13861: } /*end k1*/
13862: } else{
13863: printf("Error, current version can't treat for performance reasons, Tvar[%d]=%d, Typevar[%d]=%d\n", k, Tvar[k], k, Typevar[k]);
13864: fprintf(ficlog,"Error, current version can't treat for performance reasons, Tvar[%d]=%d, Typevar[%d]=%d\n", k, Tvar[k], k, Typevar[k]);
13865: }
13866: /* 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]); */
13867: /* printf(" modell[%d].maintype=%d, modell[%d].subtype=%d\n",k,modell[k].maintype,k,modell[k].subtype); */
13868: 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]);
13869: }
13870: ncovvta=ncovva;
13871: /* Searching for doublons in the model */
13872: for(k1=1; k1<= cptcovt;k1++){
13873: for(k2=1; k2 <k1;k2++){
13874: /* if((Typevar[k1]==Typevar[k2]) && (Fixed[Tvar[k1]]==Fixed[Tvar[k2]]) && (Dummy[Tvar[k1]]==Dummy[Tvar[k2]] )){ */
13875: if((Typevar[k1]==Typevar[k2]) && (Fixed[k1]==Fixed[k2]) && (Dummy[k1]==Dummy[k2] )){
13876: if((Typevar[k1] == 0 || Typevar[k1] == 1)){ /* Simple or age product */
13877: if(Tvar[k1]==Tvar[k2]){
13878: 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]);
13879: 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);
13880: return(1);
13881: }
13882: }else if (Typevar[k1] ==2){
13883: k3=Tposprod[k1];
13884: k4=Tposprod[k2];
13885: 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])) ){
13886: 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]]);
13887: 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);
13888: return(1);
13889: }
13890: }
13891: }
13892: }
13893: }
13894: printf("ncoveff=%d, nqfveff=%d, ntveff=%d, nqtveff=%d, cptcovn=%d\n",ncoveff,nqfveff,ntveff,nqtveff,cptcovn);
13895: fprintf(ficlog,"ncoveff=%d, nqfveff=%d, ntveff=%d, nqtveff=%d, cptcovn=%d\n",ncoveff,nqfveff,ntveff,nqtveff,cptcovn);
13896: printf("ncovf=%d, ncovv=%d, ncova=%d, nsd=%d, nsq=%d\n",ncovf,ncovv,ncova,nsd,nsq);
13897: fprintf(ficlog,"ncovf=%d, ncovv=%d, ncova=%d, nsd=%d, nsq=%d\n",ncovf,ncovv,ncova,nsd, nsq);
13898:
13899: free_imatrix(existcomb,1,NCOVMAX,1,NCOVMAX);
13900: return (0); /* with covar[new additional covariate if product] and Tage if age */
13901: /*endread:*/
13902: printf("Exiting decodemodel: ");
13903: return (1);
13904: }
13905:
13906: int calandcheckages(int imx, int maxwav, double *agemin, double *agemax, int *nberr, int *nbwarn )
13907: {/* Check ages at death */
13908: int i, m;
13909: int firstone=0;
13910:
13911: for (i=1; i<=imx; i++) {
13912: for(m=2; (m<= maxwav); m++) {
13913: if (((int)mint[m][i]== 99) && (s[m][i] <= nlstate)){
13914: anint[m][i]=9999;
13915: if (s[m][i] != -2) /* Keeping initial status of unknown vital status */
13916: s[m][i]=-1;
13917: }
13918: if((int)moisdc[i]==99 && (int)andc[i]==9999 && s[m][i]>nlstate){
13919: *nberr = *nberr + 1;
13920: if(firstone == 0){
13921: firstone=1;
13922: 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);
13923: }
13924: 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);
13925: s[m][i]=-1; /* Droping the death status */
13926: }
13927: if((int)moisdc[i]==99 && (int)andc[i]!=9999 && s[m][i]>nlstate){
13928: (*nberr)++;
13929: 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);
13930: 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);
13931: s[m][i]=-2; /* We prefer to skip it (and to skip it in version 0.8a1 too */
13932: }
13933: }
13934: }
13935:
13936: for (i=1; i<=imx; i++) {
13937: agedc[i]=(moisdc[i]/12.+andc[i])-(moisnais[i]/12.+annais[i]);
13938: for(m=firstpass; (m<= lastpass); m++){
13939: 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 */
13940: if (s[m][i] >= nlstate+1) {
13941: if(agedc[i]>0){
13942: if((int)moisdc[i]!=99 && (int)andc[i]!=9999){
13943: agev[m][i]=agedc[i];
13944: /*if(moisdc[i]==99 && andc[i]==9999) s[m][i]=-1;*/
13945: }else {
13946: if ((int)andc[i]!=9999){
13947: nbwarn++;
13948: printf("Warning negative age at death: %ld line:%d\n",num[i],i);
13949: fprintf(ficlog,"Warning negative age at death: %ld line:%d\n",num[i],i);
13950: agev[m][i]=-1;
13951: }
13952: }
13953: } /* agedc > 0 */
13954: } /* end if */
13955: else if(s[m][i] !=9){ /* Standard case, age in fractional
13956: years but with the precision of a month */
13957: agev[m][i]=(mint[m][i]/12.+1./24.+anint[m][i])-(moisnais[i]/12.+1./24.+annais[i]);
13958: if((int)mint[m][i]==99 || (int)anint[m][i]==9999)
13959: agev[m][i]=1;
13960: else if(agev[m][i] < *agemin){
13961: *agemin=agev[m][i];
13962: printf(" Min anint[%d][%d]=%.2f annais[%d]=%.2f, agemin=%.2f\n",m,i,anint[m][i], i,annais[i], *agemin);
13963: }
13964: else if(agev[m][i] >*agemax){
13965: *agemax=agev[m][i];
13966: /* printf(" Max anint[%d][%d]=%.0f annais[%d]=%.0f, agemax=%.2f\n",m,i,anint[m][i], i,annais[i], *agemax);*/
13967: }
13968: /*agev[m][i]=anint[m][i]-annais[i];*/
13969: /* agev[m][i] = age[i]+2*m;*/
13970: } /* en if 9*/
13971: else { /* =9 */
13972: /* printf("Debug num[%d]=%ld s[%d][%d]=%d\n",i,num[i], m,i, s[m][i]); */
13973: agev[m][i]=1;
13974: s[m][i]=-1;
13975: }
13976: }
13977: else if(s[m][i]==0) /*= 0 Unknown */
13978: agev[m][i]=1;
13979: else{
13980: printf("Warning, num[%d]=%ld, s[%d][%d]=%d\n", i, num[i], m, i,s[m][i]);
13981: fprintf(ficlog, "Warning, num[%d]=%ld, s[%d][%d]=%d\n", i, num[i], m, i,s[m][i]);
13982: agev[m][i]=0;
13983: }
13984: } /* End for lastpass */
13985: }
13986:
13987: for (i=1; i<=imx; i++) {
13988: for(m=firstpass; (m<=lastpass); m++){
13989: if (s[m][i] > (nlstate+ndeath)) {
13990: (*nberr)++;
13991: 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);
13992: 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);
13993: return 1;
13994: }
13995: }
13996: }
13997:
13998: /*for (i=1; i<=imx; i++){
13999: for (m=firstpass; (m<lastpass); m++){
14000: printf("%ld %d %.lf %d %d\n", num[i],(covar[1][i]),agev[m][i],s[m][i],s[m+1][i]);
14001: }
14002:
14003: }*/
14004:
14005:
14006: printf("Total number of individuals= %d, Agemin = %.2f, Agemax= %.2f\n\n", imx, *agemin, *agemax);
14007: fprintf(ficlog,"Total number of individuals= %d, Agemin = %.2f, Agemax= %.2f\n\n", imx, *agemin, *agemax);
14008:
14009: return (0);
14010: /* endread:*/
14011: printf("Exiting calandcheckages: ");
14012: return (1);
14013: }
14014:
14015: #if defined(_MSC_VER)
14016: /*printf("Visual C++ compiler: %s \n;", _MSC_FULL_VER);*/
14017: /*fprintf(ficlog, "Visual C++ compiler: %s \n;", _MSC_FULL_VER);*/
14018: //#include "stdafx.h"
14019: //#include <stdio.h>
14020: //#include <tchar.h>
14021: //#include <windows.h>
14022: //#include <iostream>
14023: typedef BOOL(WINAPI *LPFN_ISWOW64PROCESS) (HANDLE, PBOOL);
14024:
14025: LPFN_ISWOW64PROCESS fnIsWow64Process;
14026:
14027: BOOL IsWow64()
14028: {
14029: BOOL bIsWow64 = FALSE;
14030:
14031: //typedef BOOL (APIENTRY *LPFN_ISWOW64PROCESS)
14032: // (HANDLE, PBOOL);
14033:
14034: //LPFN_ISWOW64PROCESS fnIsWow64Process;
14035:
14036: HMODULE module = GetModuleHandle(_T("kernel32"));
14037: const char funcName[] = "IsWow64Process";
14038: fnIsWow64Process = (LPFN_ISWOW64PROCESS)
14039: GetProcAddress(module, funcName);
14040:
14041: if (NULL != fnIsWow64Process)
14042: {
14043: if (!fnIsWow64Process(GetCurrentProcess(),
14044: &bIsWow64))
14045: //throw std::exception("Unknown error");
14046: printf("Unknown error\n");
14047: }
14048: return bIsWow64 != FALSE;
14049: }
14050: #endif
14051:
14052: void syscompilerinfo(int logged)
14053: {
14054: #include <stdint.h>
14055:
14056: /* #include "syscompilerinfo.h"*/
14057: /* command line Intel compiler 32bit windows, XP compatible:*/
14058: /* /GS /W3 /Gy
14059: /Zc:wchar_t /Zi /O2 /Fd"Release\vc120.pdb" /D "WIN32" /D "NDEBUG" /D
14060: "_CONSOLE" /D "_LIB" /D "_USING_V110_SDK71_" /D "_UNICODE" /D
14061: "UNICODE" /Qipo /Zc:forScope /Gd /Oi /MT /Fa"Release\" /EHsc /nologo
14062: /Fo"Release\" /Qprof-dir "Release\" /Fp"Release\IMaCh.pch"
14063: */
14064: /* 64 bits */
14065: /*
14066: /GS /W3 /Gy
14067: /Zc:wchar_t /Zi /O2 /Fd"x64\Release\vc120.pdb" /D "WIN32" /D "NDEBUG"
14068: /D "_CONSOLE" /D "_LIB" /D "_UNICODE" /D "UNICODE" /Qipo /Zc:forScope
14069: /Oi /MD /Fa"x64\Release\" /EHsc /nologo /Fo"x64\Release\" /Qprof-dir
14070: "x64\Release\" /Fp"x64\Release\IMaCh.pch" */
14071: /* Optimization are useless and O3 is slower than O2 */
14072: /*
14073: /GS /W3 /Gy /Zc:wchar_t /Zi /O3 /Fd"x64\Release\vc120.pdb" /D "WIN32"
14074: /D "NDEBUG" /D "_CONSOLE" /D "_LIB" /D "_UNICODE" /D "UNICODE" /Qipo
14075: /Zc:forScope /Oi /MD /Fa"x64\Release\" /EHsc /nologo /Qparallel
14076: /Fo"x64\Release\" /Qprof-dir "x64\Release\" /Fp"x64\Release\IMaCh.pch"
14077: */
14078: /* Link is */ /* /OUT:"visual studio
14079: 2013\Projects\IMaCh\Release\IMaCh.exe" /MANIFEST /NXCOMPAT
14080: /PDB:"visual studio
14081: 2013\Projects\IMaCh\Release\IMaCh.pdb" /DYNAMICBASE
14082: "kernel32.lib" "user32.lib" "gdi32.lib" "winspool.lib"
14083: "comdlg32.lib" "advapi32.lib" "shell32.lib" "ole32.lib"
14084: "oleaut32.lib" "uuid.lib" "odbc32.lib" "odbccp32.lib"
14085: /MACHINE:X86 /OPT:REF /SAFESEH /INCREMENTAL:NO
14086: /SUBSYSTEM:CONSOLE",5.01" /MANIFESTUAC:"level='asInvoker'
14087: uiAccess='false'"
14088: /ManifestFile:"Release\IMaCh.exe.intermediate.manifest" /OPT:ICF
14089: /NOLOGO /TLBID:1
14090: */
14091:
14092:
14093: #if defined __INTEL_COMPILER
14094: #if defined(__GNUC__)
14095: struct utsname sysInfo; /* For Intel on Linux and OS/X */
14096: #endif
14097: #elif defined(__GNUC__)
14098: #ifndef __APPLE__
14099: #include <gnu/libc-version.h> /* Only on gnu */
14100: #endif
14101: struct utsname sysInfo;
14102: int cross = CROSS;
14103: if (cross){
14104: printf("Cross-");
14105: if(logged) fprintf(ficlog, "Cross-");
14106: }
14107: #endif
14108:
14109: printf("Compiled with:");if(logged)fprintf(ficlog,"Compiled with:");
14110: #if defined(__clang__)
14111: printf(" Clang/LLVM");if(logged)fprintf(ficlog," Clang/LLVM"); /* Clang/LLVM. ---------------------------------------------- */
14112: #endif
14113: #if defined(__ICC) || defined(__INTEL_COMPILER)
14114: printf(" Intel ICC/ICPC");if(logged)fprintf(ficlog," Intel ICC/ICPC");/* Intel ICC/ICPC. ------------------------------------------ */
14115: #endif
14116: #if defined(__GNUC__) || defined(__GNUG__)
14117: printf(" GNU GCC/G++");if(logged)fprintf(ficlog," GNU GCC/G++");/* GNU GCC/G++. --------------------------------------------- */
14118: #endif
14119: #if defined(__HP_cc) || defined(__HP_aCC)
14120: printf(" Hewlett-Packard C/aC++");if(logged)fprintf(fcilog," Hewlett-Packard C/aC++"); /* Hewlett-Packard C/aC++. ---------------------------------- */
14121: #endif
14122: #if defined(__IBMC__) || defined(__IBMCPP__)
14123: printf(" IBM XL C/C++"); if(logged) fprintf(ficlog," IBM XL C/C++");/* IBM XL C/C++. -------------------------------------------- */
14124: #endif
14125: #if defined(_MSC_VER)
14126: printf(" Microsoft Visual Studio");if(logged)fprintf(ficlog," Microsoft Visual Studio");/* Microsoft Visual Studio. --------------------------------- */
14127: #endif
14128: #if defined(__PGI)
14129: printf(" Portland Group PGCC/PGCPP");if(logged) fprintf(ficlog," Portland Group PGCC/PGCPP");/* Portland Group PGCC/PGCPP. ------------------------------- */
14130: #endif
14131: #if defined(__SUNPRO_C) || defined(__SUNPRO_CC)
14132: printf(" Oracle Solaris Studio");if(logged)fprintf(ficlog," Oracle Solaris Studio\n");/* Oracle Solaris Studio. ----------------------------------- */
14133: #endif
14134: printf(" for "); if (logged) fprintf(ficlog, " for ");
14135:
14136: // http://stackoverflow.com/questions/4605842/how-to-identify-platform-compiler-from-preprocessor-macros
14137: #ifdef _WIN32 // note the underscore: without it, it's not msdn official!
14138: // Windows (x64 and x86)
14139: printf("Windows (x64 and x86) ");if(logged) fprintf(ficlog,"Windows (x64 and x86) ");
14140: #elif __unix__ // all unices, not all compilers
14141: // Unix
14142: printf("Unix ");if(logged) fprintf(ficlog,"Unix ");
14143: #elif __linux__
14144: // linux
14145: printf("linux ");if(logged) fprintf(ficlog,"linux ");
14146: #elif __APPLE__
14147: // Mac OS, not sure if this is covered by __posix__ and/or __unix__ though..
14148: printf("Mac OS ");if(logged) fprintf(ficlog,"Mac OS ");
14149: #endif
14150:
14151: /* __MINGW32__ */
14152: /* __CYGWIN__ */
14153: /* __MINGW64__ */
14154: // http://msdn.microsoft.com/en-us/library/b0084kay.aspx
14155: /* _MSC_VER //the Visual C++ compiler is 17.00.51106.1, the _MSC_VER macro evaluates to 1700. Type cl /? */
14156: /* _MSC_FULL_VER //the Visual C++ compiler is 15.00.20706.01, the _MSC_FULL_VER macro evaluates to 150020706 */
14157: /* _WIN64 // Defined for applications for Win64. */
14158: /* _M_X64 // Defined for compilations that target x64 processors. */
14159: /* _DEBUG // Defined when you compile with /LDd, /MDd, and /MTd. */
14160:
14161: #if UINTPTR_MAX == 0xffffffff
14162: printf(" 32-bit"); if(logged) fprintf(ficlog," 32-bit");/* 32-bit */
14163: #elif UINTPTR_MAX == 0xffffffffffffffff
14164: printf(" 64-bit"); if(logged) fprintf(ficlog," 64-bit");/* 64-bit */
14165: #else
14166: printf(" wtf-bit"); if(logged) fprintf(ficlog," wtf-bit");/* wtf */
14167: #endif
14168:
14169: #if defined(__GNUC__)
14170: # if defined(__GNUC_PATCHLEVEL__)
14171: # define __GNUC_VERSION__ (__GNUC__ * 10000 \
14172: + __GNUC_MINOR__ * 100 \
14173: + __GNUC_PATCHLEVEL__)
14174: # else
14175: # define __GNUC_VERSION__ (__GNUC__ * 10000 \
14176: + __GNUC_MINOR__ * 100)
14177: # endif
14178: printf(" using GNU C version %d.\n", __GNUC_VERSION__);
14179: if(logged) fprintf(ficlog, " using GNU C version %d.\n", __GNUC_VERSION__);
14180:
14181: if (uname(&sysInfo) != -1) {
14182: printf("Running on: %s %s %s %s %s\n",sysInfo.sysname, sysInfo.nodename, sysInfo.release, sysInfo.version, sysInfo.machine);
14183: if(logged) fprintf(ficlog,"Running on: %s %s %s %s %s\n ",sysInfo.sysname, sysInfo.nodename, sysInfo.release, sysInfo.version, sysInfo.machine);
14184: }
14185: else
14186: perror("uname() error");
14187: //#ifndef __INTEL_COMPILER
14188: #if !defined (__INTEL_COMPILER) && !defined(__APPLE__)
14189: printf("GNU libc version: %s\n", gnu_get_libc_version());
14190: if(logged) fprintf(ficlog,"GNU libc version: %s\n", gnu_get_libc_version());
14191: #endif
14192: #endif
14193:
14194: // void main ()
14195: // {
14196: #if defined(_MSC_VER)
14197: if (IsWow64()){
14198: printf("\nThe program (probably compiled for 32bit) is running under WOW64 (64bit) emulation.\n");
14199: if (logged) fprintf(ficlog, "\nThe program (probably compiled for 32bit) is running under WOW64 (64bit) emulation.\n");
14200: }
14201: else{
14202: printf("\nThe program is not running under WOW64 (i.e probably on a 64bit Windows).\n");
14203: if (logged) fprintf(ficlog, "\nThe programm is not running under WOW64 (i.e probably on a 64bit Windows).\n");
14204: }
14205: // printf("\nPress Enter to continue...");
14206: // getchar();
14207: // }
14208:
14209: #endif
14210:
14211:
14212: }
14213:
14214: int prevalence_limit(double *p, double **prlim, double ageminpar, double agemaxpar, double ftolpl, int *ncvyearp){
14215: /*--------------- Prevalence limit (forward period or forward stable prevalence) --------------*/
14216: /* Computes the prevalence limit for each combination of the dummy covariates */
14217: int i, j, k, i1, k4=0, nres=0 ;
14218: /* double ftolpl = 1.e-10; */
14219: double age, agebase, agelim;
14220: double tot;
14221:
14222: strcpy(filerespl,"PL_");
14223: strcat(filerespl,fileresu);
14224: if((ficrespl=fopen(filerespl,"w"))==NULL) {
14225: printf("Problem with forward period (stable) prevalence resultfile: %s\n", filerespl);return 1;
14226: fprintf(ficlog,"Problem with forward period (stable) prevalence resultfile: %s\n", filerespl);return 1;
14227: }
14228: printf("\nComputing forward period (stable) prevalence: result on file '%s' \n", filerespl);
14229: fprintf(ficlog,"\nComputing forward period (stable) prevalence: result on file '%s' \n", filerespl);
14230: pstamp(ficrespl);
14231: fprintf(ficrespl,"# Forward period (stable) prevalence. Precision given by ftolpl=%g \n", ftolpl);
14232: fprintf(ficrespl,"#Age ");
14233: for(i=1; i<=nlstate;i++) fprintf(ficrespl,"%d-%d ",i,i);
14234: fprintf(ficrespl,"\n");
14235:
14236: /* prlim=matrix(1,nlstate,1,nlstate);*/ /* back in main */
14237:
14238: agebase=ageminpar;
14239: agelim=agemaxpar;
14240:
14241: /* i1=pow(2,ncoveff); */
14242: i1=pow(2,cptcoveff); /* Number of combination of dummy covariates */
14243: if (cptcovn < 1){i1=1;}
14244:
14245: /* for(k=1; k<=i1;k++){ /\* For each combination k of dummy covariates in the model *\/ */
14246: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
14247: k=TKresult[nres];
14248: if(TKresult[nres]==0) k=1; /* To be checked for noresult */
14249: /* if(i1 != 1 && TKresult[nres]!= k) /\* We found the combination k corresponding to the resultline value of dummies *\/ */
14250: /* continue; */
14251:
14252: /* for(cptcov=1,k=0;cptcov<=i1;cptcov++){ */
14253: /* for(cptcov=1,k=0;cptcov<=1;cptcov++){ */
14254: //for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){
14255: /* k=k+1; */
14256: /* to clean */
14257: /*printf("cptcov=%d cptcod=%d codtab=%d\n",cptcov, cptcod,codtabm(cptcod,cptcov));*/
14258: fprintf(ficrespl,"#******");
14259: printf("#******");
14260: fprintf(ficlog,"#******");
14261: 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) */
14262: /* fprintf(ficrespl," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,Tvaraff[j])]); /\* Here problem for varying dummy*\/ */
14263: /* printf(" V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
14264: /* fprintf(ficlog," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
14265: fprintf(ficrespl," V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
14266: printf(" V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
14267: fprintf(ficlog," V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
14268: }
14269: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
14270: /* printf(" V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); */
14271: /* fprintf(ficrespl," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); */
14272: /* fprintf(ficlog," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); */
14273: /* } */
14274: fprintf(ficrespl,"******\n");
14275: printf("******\n");
14276: fprintf(ficlog,"******\n");
14277: if(invalidvarcomb[k]){
14278: printf("\nCombination (%d) ignored because no case \n",k);
14279: fprintf(ficrespl,"#Combination (%d) ignored because no case \n",k);
14280: fprintf(ficlog,"\nCombination (%d) ignored because no case \n",k);
14281: continue;
14282: }
14283:
14284: fprintf(ficrespl,"#Age ");
14285: /* for(j=1;j<=cptcoveff;j++) { */
14286: /* fprintf(ficrespl,"V%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
14287: /* } */
14288: for(j=1;j<=cptcovs;j++) { /* New the quanti variable is added */
14289: fprintf(ficrespl,"V%d %lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
14290: }
14291: for(i=1; i<=nlstate;i++) fprintf(ficrespl," %d-%d ",i,i);
14292: fprintf(ficrespl,"Total Years_to_converge\n");
14293:
14294: for (age=agebase; age<=agelim; age++){
14295: /* for (age=agebase; age<=agebase; age++){ */
14296: /**< Computes the prevalence limit in each live state at age x and for covariate combination (k and) nres */
14297: prevalim(prlim, nlstate, p, age, oldm, savm, ftolpl, ncvyearp, k, nres); /* Nicely done */
14298: fprintf(ficrespl,"%.0f ",age );
14299: /* for(j=1;j<=cptcoveff;j++) */
14300: /* fprintf(ficrespl,"%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
14301: for(j=1;j<=cptcovs;j++)
14302: fprintf(ficrespl,"%d %lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
14303: tot=0.;
14304: for(i=1; i<=nlstate;i++){
14305: tot += prlim[i][i];
14306: fprintf(ficrespl," %.5f", prlim[i][i]);
14307: }
14308: fprintf(ficrespl," %.3f %d\n", tot, *ncvyearp);
14309: } /* Age */
14310: /* was end of cptcod */
14311: } /* nres */
14312: /* } /\* for each combination *\/ */
14313: return 0;
14314: }
14315:
14316: 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){
14317: /*--------------- Back Prevalence limit (backward stable prevalence) --------------*/
14318:
14319: /* Computes the back prevalence limit for any combination of covariate values
14320: * at any age between ageminpar and agemaxpar
14321: */
14322: int i, j, k, i1, nres=0 ;
14323: /* double ftolpl = 1.e-10; */
14324: double age, agebase, agelim;
14325: double tot;
14326: /* double ***mobaverage; */
14327: /* double **dnewm, **doldm, **dsavm; /\* for use *\/ */
14328:
14329: strcpy(fileresplb,"PLB_");
14330: strcat(fileresplb,fileresu);
14331: if((ficresplb=fopen(fileresplb,"w"))==NULL) {
14332: printf("Problem with backward prevalence resultfile: %s\n", fileresplb);return 1;
14333: fprintf(ficlog,"Problem with backward prevalence resultfile: %s\n", fileresplb);return 1;
14334: }
14335: printf("Computing backward prevalence: result on file '%s' \n", fileresplb);
14336: fprintf(ficlog,"Computing backward prevalence: result on file '%s' \n", fileresplb);
14337: pstamp(ficresplb);
14338: fprintf(ficresplb,"# Backward prevalence. Precision given by ftolpl=%g \n", ftolpl);
14339: fprintf(ficresplb,"#Age ");
14340: for(i=1; i<=nlstate;i++) fprintf(ficresplb,"%d-%d ",i,i);
14341: fprintf(ficresplb,"\n");
14342:
14343:
14344: /* prlim=matrix(1,nlstate,1,nlstate);*/ /* back in main */
14345:
14346: agebase=ageminpar;
14347: agelim=agemaxpar;
14348:
14349:
14350: i1=pow(2,cptcoveff);
14351: if (cptcovn < 1){i1=1;}
14352:
14353: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
14354: /* for(k=1; k<=i1;k++){ /\* For any combination of dummy covariates, fixed and varying *\/ */
14355: k=TKresult[nres];
14356: if(TKresult[nres]==0) k=1; /* To be checked for noresult */
14357: /* if(i1 != 1 && TKresult[nres]!= k) */
14358: /* continue; */
14359: /* /\*printf("cptcov=%d cptcod=%d codtab=%d\n",cptcov, cptcod,codtabm(cptcod,cptcov));*\/ */
14360: fprintf(ficresplb,"#******");
14361: printf("#******");
14362: fprintf(ficlog,"#******");
14363: 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) */
14364: printf(" V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
14365: fprintf(ficresplb," V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
14366: fprintf(ficlog," V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
14367: }
14368: /* for(j=1;j<=cptcoveff ;j++) {/\* all covariates *\/ */
14369: /* fprintf(ficresplb," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
14370: /* printf(" V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
14371: /* fprintf(ficlog," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
14372: /* } */
14373: /* for (j=1; j<= nsq; j++){ /\* For each selected (single) quantitative value *\/ */
14374: /* printf(" V%d=%f ",Tvqresult[nres][j],Tqresult[nres][resultmodel[nres][j]]); */
14375: /* fprintf(ficresplb," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][resultmodel[nres][j]]); */
14376: /* fprintf(ficlog," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][resultmodel[nres][j]]); */
14377: /* } */
14378: fprintf(ficresplb,"******\n");
14379: printf("******\n");
14380: fprintf(ficlog,"******\n");
14381: if(invalidvarcomb[k]){
14382: printf("\nCombination (%d) ignored because no cases \n",k);
14383: fprintf(ficresplb,"#Combination (%d) ignored because no cases \n",k);
14384: fprintf(ficlog,"\nCombination (%d) ignored because no cases \n",k);
14385: continue;
14386: }
14387:
14388: fprintf(ficresplb,"#Age ");
14389: for(j=1;j<=cptcovs;j++) {
14390: fprintf(ficresplb,"V%d %lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
14391: }
14392: for(i=1; i<=nlstate;i++) fprintf(ficresplb," %d-%d ",i,i);
14393: fprintf(ficresplb,"Total Years_to_converge\n");
14394:
14395:
14396: for (age=agebase; age<=agelim; age++){
14397: /* for (age=agebase; age<=agebase; age++){ */
14398: if(mobilavproj > 0){
14399: /* bprevalim(bprlim, mobaverage, nlstate, p, age, ageminpar, agemaxpar, oldm, savm, doldm, dsavm, ftolpl, ncvyearp, k); */
14400: /* bprevalim(bprlim, mobaverage, nlstate, p, age, oldm, savm, dnewm, doldm, dsavm, ftolpl, ncvyearp, k); */
14401: bprevalim(bprlim, mobaverage, nlstate, p, age, ftolpl, ncvyearp, k, nres);
14402: }else if (mobilavproj == 0){
14403: 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);
14404: 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);
14405: exit(1);
14406: }else{
14407: /* bprevalim(bprlim, probs, nlstate, p, age, oldm, savm, dnewm, doldm, dsavm, ftolpl, ncvyearp, k); */
14408: bprevalim(bprlim, probs, nlstate, p, age, ftolpl, ncvyearp, k,nres);
14409: /* printf("TOTOT\n"); */
14410: /* exit(1); */
14411: }
14412: fprintf(ficresplb,"%.0f ",age );
14413: for(j=1;j<=cptcovs;j++)
14414: fprintf(ficresplb,"%d %lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
14415: tot=0.;
14416: for(i=1; i<=nlstate;i++){
14417: tot += bprlim[i][i];
14418: fprintf(ficresplb," %.5f", bprlim[i][i]);
14419: }
14420: fprintf(ficresplb," %.3f %d\n", tot, *ncvyearp);
14421: } /* Age */
14422: /* was end of cptcod */
14423: /*fprintf(ficresplb,"\n");*/ /* Seems to be necessary for gnuplot only if two result lines and no covariate. */
14424: /* } /\* end of any combination *\/ */
14425: } /* end of nres */
14426: /* hBijx(p, bage, fage); */
14427: /* fclose(ficrespijb); */
14428:
14429: return 0;
14430: }
14431:
14432: int hPijx(double *p, int bage, int fage){
14433: /*------------- h Pij x at various ages ------------*/
14434: /* to be optimized with precov */
14435: int stepsize;
14436: int agelim;
14437: int hstepm;
14438: int nhstepm;
14439: int h, i, i1, j, k, nres=0;
14440:
14441: double agedeb;
14442: double ***p3mat;
14443:
14444: strcpy(filerespij,"PIJ_"); strcat(filerespij,fileresu);
14445: if((ficrespij=fopen(filerespij,"w"))==NULL) {
14446: printf("Problem with Pij resultfile: %s\n", filerespij); return 1;
14447: fprintf(ficlog,"Problem with Pij resultfile: %s\n", filerespij); return 1;
14448: }
14449: printf("Computing pij: result on file '%s' \n", filerespij);
14450: fprintf(ficlog,"Computing pij: result on file '%s' \n", filerespij);
14451:
14452: stepsize=(int) (stepm+YEARM-1)/YEARM;
14453: /*if (stepm<=24) stepsize=2;*/
14454:
14455: agelim=AGESUP;
14456: hstepm=stepsize*YEARM; /* Every year of age */
14457: hstepm=hstepm/stepm; /* Typically 2 years, = 2/6 months = 4 */
14458:
14459: /* hstepm=1; aff par mois*/
14460: pstamp(ficrespij);
14461: fprintf(ficrespij,"#****** h Pij x Probability to be in state j at age x+h being in i at x ");
14462: i1= pow(2,cptcoveff);
14463: /* for(cptcov=1,k=0;cptcov<=i1;cptcov++){ */
14464: /* /\*for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){*\/ */
14465: /* k=k+1; */
14466: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
14467: k=TKresult[nres];
14468: if(TKresult[nres]==0) k=1; /* To be checked for noresult */
14469: /* for(k=1; k<=i1;k++){ */
14470: /* if(i1 != 1 && TKresult[nres]!= k) */
14471: /* continue; */
14472: fprintf(ficrespij,"\n#****** ");
14473: for(j=1;j<=cptcovs;j++){
14474: fprintf(ficrespij," V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
14475: /* fprintf(ficrespij,"@wV%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
14476: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
14477: /* printf(" V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); */
14478: /* fprintf(ficrespij," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); */
14479: }
14480: fprintf(ficrespij,"******\n");
14481:
14482: for (agedeb=fage; agedeb>=bage; agedeb--){ /* If stepm=6 months */
14483: nhstepm=(int) rint((agelim-agedeb)*YEARM/stepm); /* Typically 20 years = 20*12/6=40 */
14484: nhstepm = nhstepm/hstepm; /* Typically 40/4=10 */
14485:
14486: /* nhstepm=nhstepm*YEARM; aff par mois*/
14487:
14488: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
14489: oldm=oldms;savm=savms;
14490: hpxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k, nres);
14491: fprintf(ficrespij,"# Cov Agex agex+h hpijx with i,j=");
14492: for(i=1; i<=nlstate;i++)
14493: for(j=1; j<=nlstate+ndeath;j++)
14494: fprintf(ficrespij," %1d-%1d",i,j);
14495: fprintf(ficrespij,"\n");
14496: for (h=0; h<=nhstepm; h++){
14497: /*agedebphstep = agedeb + h*hstepm/YEARM*stepm;*/
14498: fprintf(ficrespij,"%d %3.f %3.f",k, agedeb, agedeb + h*hstepm/YEARM*stepm );
14499: for(i=1; i<=nlstate;i++)
14500: for(j=1; j<=nlstate+ndeath;j++)
14501: fprintf(ficrespij," %.5f", p3mat[i][j][h]);
14502: fprintf(ficrespij,"\n");
14503: }
14504: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
14505: fprintf(ficrespij,"\n");
14506: }
14507: }
14508: /*}*/
14509: return 0;
14510: }
14511:
14512: int hBijx(double *p, int bage, int fage, double ***prevacurrent){
14513: /*------------- h Bij x at various ages ------------*/
14514: /* To be optimized with precov */
14515: int stepsize;
14516: /* int agelim; */
14517: int ageminl;
14518: int hstepm;
14519: int nhstepm;
14520: int h, i, i1, j, k, nres;
14521:
14522: double agedeb;
14523: double ***p3mat;
14524:
14525: strcpy(filerespijb,"PIJB_"); strcat(filerespijb,fileresu);
14526: if((ficrespijb=fopen(filerespijb,"w"))==NULL) {
14527: printf("Problem with Pij back resultfile: %s\n", filerespijb); return 1;
14528: fprintf(ficlog,"Problem with Pij back resultfile: %s\n", filerespijb); return 1;
14529: }
14530: printf("Computing pij back: result on file '%s' \n", filerespijb);
14531: fprintf(ficlog,"Computing pij back: result on file '%s' \n", filerespijb);
14532:
14533: stepsize=(int) (stepm+YEARM-1)/YEARM;
14534: /*if (stepm<=24) stepsize=2;*/
14535:
14536: /* agelim=AGESUP; */
14537: ageminl=AGEINF; /* was 30 */
14538: hstepm=stepsize*YEARM; /* Every year of age */
14539: hstepm=hstepm/stepm; /* Typically 2 years, = 2/6 months = 4 */
14540:
14541: /* hstepm=1; aff par mois*/
14542: pstamp(ficrespijb);
14543: fprintf(ficrespijb,"#****** h Bij x Back probability to be in state i at age x-h being in j at x: B1j+B2j+...=1 ");
14544: i1= pow(2,cptcoveff);
14545: /* for(cptcov=1,k=0;cptcov<=i1;cptcov++){ */
14546: /* /\*for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){*\/ */
14547: /* k=k+1; */
14548: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
14549: k=TKresult[nres];
14550: if(TKresult[nres]==0) k=1; /* To be checked for noresult */
14551: /* for(k=1; k<=i1;k++){ /\* For any combination of dummy covariates, fixed and varying *\/ */
14552: /* if(i1 != 1 && TKresult[nres]!= k) */
14553: /* continue; */
14554: fprintf(ficrespijb,"\n#****** ");
14555: for(j=1;j<=cptcovs;j++){
14556: fprintf(ficrespijb," V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
14557: /* for(j=1;j<=cptcoveff;j++) */
14558: /* fprintf(ficrespijb,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
14559: /* for (j=1; j<= nsq; j++){ /\* For each selected (single) quantitative value *\/ */
14560: /* fprintf(ficrespijb," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][resultmodel[nres][j]]); */
14561: }
14562: fprintf(ficrespijb,"******\n");
14563: if(invalidvarcomb[k]){ /* Is it necessary here? */
14564: fprintf(ficrespijb,"\n#Combination (%d) ignored because no cases \n",k);
14565: continue;
14566: }
14567:
14568: /* for (agedeb=fage; agedeb>=bage; agedeb--){ /\* If stepm=6 months *\/ */
14569: for (agedeb=bage; agedeb<=fage; agedeb++){ /* If stepm=6 months and estepm=24 (2 years) */
14570: /* nhstepm=(int) rint((agelim-agedeb)*YEARM/stepm); /\* Typically 20 years = 20*12/6=40 *\/ */
14571: 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 */
14572: nhstepm = nhstepm/hstepm; /* Typically 40/4=10, because estepm=24 stepm=6 => hstepm=24/6=4 or 28*/
14573:
14574: /* nhstepm=nhstepm*YEARM; aff par mois*/
14575:
14576: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); /* We can't have it at an upper level because of nhstepm */
14577: /* and memory limitations if stepm is small */
14578:
14579: /* oldm=oldms;savm=savms; */
14580: /* hbxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k); */
14581: hbxij(p3mat,nhstepm,agedeb,hstepm,p,prevacurrent,nlstate,stepm, k, nres);/* Bug valgrind */
14582: /* hbxij(p3mat,nhstepm,agedeb,hstepm,p,prevacurrent,nlstate,stepm,oldm,savm, dnewm, doldm, dsavm, k); */
14583: fprintf(ficrespijb,"# Cov Agex agex-h hbijx with i,j=");
14584: for(i=1; i<=nlstate;i++)
14585: for(j=1; j<=nlstate+ndeath;j++)
14586: fprintf(ficrespijb," %1d-%1d",i,j);
14587: fprintf(ficrespijb,"\n");
14588: for (h=0; h<=nhstepm; h++){
14589: /*agedebphstep = agedeb + h*hstepm/YEARM*stepm;*/
14590: fprintf(ficrespijb,"%d %3.f %3.f",k, agedeb, agedeb - h*hstepm/YEARM*stepm );
14591: /* fprintf(ficrespijb,"%d %3.f %3.f",k, agedeb, agedeb + h*hstepm/YEARM*stepm ); */
14592: for(i=1; i<=nlstate;i++)
14593: for(j=1; j<=nlstate+ndeath;j++)
14594: fprintf(ficrespijb," %.5f", p3mat[i][j][h]);/* Bug valgrind */
14595: fprintf(ficrespijb,"\n");
14596: }
14597: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
14598: fprintf(ficrespijb,"\n");
14599: } /* end age deb */
14600: /* } /\* end combination *\/ */
14601: } /* end nres */
14602: return 0;
14603: } /* hBijx */
14604:
14605:
14606: /***********************************************/
14607: /**************** Main Program *****************/
14608: /***********************************************/
14609:
14610: int main(int argc, char *argv[])
14611: {
14612: #ifdef GSL
14613: const gsl_multimin_fminimizer_type *T;
14614: size_t iteri = 0, it;
14615: int rval = GSL_CONTINUE;
14616: int status = GSL_SUCCESS;
14617: double ssval;
14618: #endif
14619: int movingaverage(double ***probs, double bage,double fage, double ***mobaverage, int mobilav);
14620: int i,j, k, iter=0,m,size=100, cptcod; /* Suppressing because nobs */
14621: /* int i,j, k, n=MAXN,iter=0,m,size=100, cptcod; */
14622: int ncvyear=0; /* Number of years needed for the period prevalence to converge */
14623: int jj, ll, li, lj, lk;
14624: int numlinepar=0; /* Current linenumber of parameter file */
14625: int num_filled;
14626: int itimes;
14627: int NDIM=2;
14628: int vpopbased=0;
14629: int nres=0;
14630: int endishere=0;
14631: int noffset=0;
14632: int ncurrv=0; /* Temporary variable */
14633:
14634: char ca[32], cb[32];
14635: /* FILE *fichtm; *//* Html File */
14636: /* FILE *ficgp;*/ /*Gnuplot File */
14637: struct stat info;
14638: double agedeb=0.;
14639:
14640: double ageminpar=AGEOVERFLOW,agemin=AGEOVERFLOW, agemaxpar=-AGEOVERFLOW, agemax=-AGEOVERFLOW;
14641: double ageminout=-AGEOVERFLOW,agemaxout=AGEOVERFLOW; /* Smaller Age range redefined after movingaverage */
14642:
14643: double stdpercent; /* for computing the std error of percent e.i: e.i/e.. */
14644: double fret;
14645: double dum=0.; /* Dummy variable */
14646: /* double*** p3mat;*/
14647: /* double ***mobaverage; */
14648: double wald;
14649:
14650: char line[MAXLINE], linetmp[MAXLINE];
14651: char path[MAXLINE],pathc[MAXLINE],pathcd[MAXLINE],pathtot[MAXLINE];
14652:
14653: char modeltemp[MAXLINE];
14654: char resultline[MAXLINE], resultlineori[MAXLINE];
14655:
14656: char pathr[MAXLINE], pathimach[MAXLINE];
14657: char *tok, *val; /* pathtot */
14658: /* int firstobs=1, lastobs=10; /\* nobs = lastobs-firstobs declared globally ;*\/ */
14659: int c, h; /* c2; */
14660: int jl=0;
14661: int i1, j1, jk, stepsize=0;
14662: int count=0;
14663:
14664: int *tab;
14665: int mobilavproj=0 , prevfcast=0 ; /* moving average of prev, If prevfcast=1 prevalence projection */
14666: /* double anprojd, mprojd, jprojd; /\* For eventual projections *\/ */
14667: /* double anprojf, mprojf, jprojf; */
14668: /* double jintmean,mintmean,aintmean; */
14669: int prvforecast = 0; /* Might be 1 (date of beginning of projection is a choice or 2 is the dateintmean */
14670: int prvbackcast = 0; /* Might be 1 (date of beginning of projection is a choice or 2 is the dateintmean */
14671: double yrfproj= 10.0; /* Number of years of forward projections */
14672: double yrbproj= 10.0; /* Number of years of backward projections */
14673: int prevbcast=0; /* defined as global for mlikeli and mle, replacing backcast */
14674: int mobilav=0,popforecast=0;
14675: int hstepm=0, nhstepm=0;
14676: int agemortsup;
14677: float sumlpop=0.;
14678: double jprev1=1, mprev1=1,anprev1=2000,jprev2=1, mprev2=1,anprev2=2000;
14679: double jpyram=1, mpyram=1,anpyram=2000,jpyram1=1, mpyram1=1,anpyram1=2000;
14680:
14681: double bage=0, fage=110., age, agelim=0., agebase=0.;
14682: double ftolpl=FTOL;
14683: double **prlim;
14684: double **bprlim;
14685: double ***param; /* Matrix of parameters, param[i][j][k] param=ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel)
14686: state of origin, state of destination including death, for each covariate: constante, age, and V1 V2 etc. */
14687: double ***paramstart; /* Matrix of starting parameter values */
14688: double *p, *pstart; /* p=param[1][1] pstart is for starting values guessed by freqsummary */
14689: double **matcov; /* Matrix of covariance */
14690: double **hess; /* Hessian matrix */
14691: double ***delti3; /* Scale */
14692: double *delti; /* Scale */
14693: double ***eij, ***vareij;
14694: //double **varpl; /* Variances of prevalence limits by age */
14695:
14696: double *epj, vepp;
14697:
14698: double dateprev1, dateprev2;
14699: double jproj1=1,mproj1=1,anproj1=2000,jproj2=1,mproj2=1,anproj2=2000, dateproj1=0, dateproj2=0, dateprojd=0, dateprojf=0;
14700: double jback1=1,mback1=1,anback1=2000,jback2=1,mback2=1,anback2=2000, dateback1=0, dateback2=0, datebackd=0, datebackf=0;
14701:
14702:
14703: double **ximort;
14704: char *alph[]={"a","a","b","c","d","e"}, str[4]="1234";
14705: int *dcwave;
14706:
14707: char z[1]="c";
14708:
14709: /*char *strt;*/
14710: char strtend[80];
14711:
14712:
14713: /* setlocale (LC_ALL, ""); */
14714: /* bindtextdomain (PACKAGE, LOCALEDIR); */
14715: /* textdomain (PACKAGE); */
14716: /* setlocale (LC_CTYPE, ""); */
14717: /* setlocale (LC_MESSAGES, ""); */
14718:
14719: /* gettimeofday(&start_time, (struct timezone*)0); */ /* at first time */
14720: rstart_time = time(NULL);
14721: /* (void) gettimeofday(&start_time,&tzp);*/
14722: start_time = *localtime(&rstart_time);
14723: curr_time=start_time;
14724: /*tml = *localtime(&start_time.tm_sec);*/
14725: /* strcpy(strstart,asctime(&tml)); */
14726: strcpy(strstart,asctime(&start_time));
14727:
14728: /* printf("Localtime (at start)=%s",strstart); */
14729: /* tp.tm_sec = tp.tm_sec +86400; */
14730: /* tm = *localtime(&start_time.tm_sec); */
14731: /* tmg.tm_year=tmg.tm_year +dsign*dyear; */
14732: /* tmg.tm_mon=tmg.tm_mon +dsign*dmonth; */
14733: /* tmg.tm_hour=tmg.tm_hour + 1; */
14734: /* tp.tm_sec = mktime(&tmg); */
14735: /* strt=asctime(&tmg); */
14736: /* printf("Time(after) =%s",strstart); */
14737: /* (void) time (&time_value);
14738: * printf("time=%d,t-=%d\n",time_value,time_value-86400);
14739: * tm = *localtime(&time_value);
14740: * strstart=asctime(&tm);
14741: * printf("tim_value=%d,asctime=%s\n",time_value,strstart);
14742: */
14743:
14744: nberr=0; /* Number of errors and warnings */
14745: nbwarn=0;
14746: #ifdef WIN32
14747: _getcwd(pathcd, size);
14748: #else
14749: getcwd(pathcd, size);
14750: #endif
14751: syscompilerinfo(0);
14752: printf("\nIMaCh prax version %s, %s\n%s",version, copyright, fullversion);
14753: if(argc <=1){
14754: printf("\nEnter the parameter file name: ");
14755: if(!fgets(pathr,FILENAMELENGTH,stdin)){
14756: printf("ERROR Empty parameter file name\n");
14757: goto end;
14758: }
14759: i=strlen(pathr);
14760: if(pathr[i-1]=='\n')
14761: pathr[i-1]='\0';
14762: i=strlen(pathr);
14763: if(i >= 1 && pathr[i-1]==' ') {/* This may happen when dragging on oS/X! */
14764: pathr[i-1]='\0';
14765: }
14766: i=strlen(pathr);
14767: if( i==0 ){
14768: printf("ERROR Empty parameter file name\n");
14769: goto end;
14770: }
14771: for (tok = pathr; tok != NULL; ){
14772: printf("Pathr |%s|\n",pathr);
14773: while ((val = strsep(&tok, "\"" )) != NULL && *val == '\0');
14774: printf("val= |%s| pathr=%s\n",val,pathr);
14775: strcpy (pathtot, val);
14776: if(pathr[0] == '\0') break; /* Dirty */
14777: }
14778: }
14779: else if (argc<=2){
14780: strcpy(pathtot,argv[1]);
14781: }
14782: else{
14783: strcpy(pathtot,argv[1]);
14784: strcpy(z,argv[2]);
14785: printf("\nargv[2]=%s z=%c\n",argv[2],z[0]);
14786: }
14787: /*if(getcwd(pathcd, MAXLINE)!= NULL)printf ("Error pathcd\n");*/
14788: /*cygwin_split_path(pathtot,path,optionfile);
14789: printf("pathtot=%s, path=%s, optionfile=%s\n",pathtot,path,optionfile);*/
14790: /* cutv(path,optionfile,pathtot,'\\');*/
14791:
14792: /* Split argv[0], imach program to get pathimach */
14793: printf("\nargv[0]=%s argv[1]=%s, \n",argv[0],argv[1]);
14794: split(argv[0],pathimach,optionfile,optionfilext,optionfilefiname);
14795: printf("\nargv[0]=%s pathimach=%s, \noptionfile=%s \noptionfilext=%s \noptionfilefiname=%s\n",argv[0],pathimach,optionfile,optionfilext,optionfilefiname);
14796: /* strcpy(pathimach,argv[0]); */
14797: /* Split argv[1]=pathtot, parameter file name to get path, optionfile, extension and name */
14798: split(pathtot,path,optionfile,optionfilext,optionfilefiname);
14799: printf("\npathtot=%s,\npath=%s,\noptionfile=%s \noptionfilext=%s \noptionfilefiname=%s\n",pathtot,path,optionfile,optionfilext,optionfilefiname);
14800: #ifdef WIN32
14801: _chdir(path); /* Can be a relative path */
14802: if(_getcwd(pathcd,MAXLINE) > 0) /* So pathcd is the full path */
14803: #else
14804: chdir(path); /* Can be a relative path */
14805: if (getcwd(pathcd, MAXLINE) > 0) /* So pathcd is the full path */
14806: #endif
14807: printf("Current directory %s!\n",pathcd);
14808: strcpy(command,"mkdir ");
14809: strcat(command,optionfilefiname);
14810: if((outcmd=system(command)) != 0){
14811: printf("Directory already exists (or can't create it) %s%s, err=%d\n",path,optionfilefiname,outcmd);
14812: /* fprintf(ficlog,"Problem creating directory %s%s\n",path,optionfilefiname); */
14813: /* fclose(ficlog); */
14814: /* exit(1); */
14815: }
14816: /* if((imk=mkdir(optionfilefiname))<0){ */
14817: /* perror("mkdir"); */
14818: /* } */
14819:
14820: /*-------- arguments in the command line --------*/
14821:
14822: /* Main Log file */
14823: strcat(filelog, optionfilefiname);
14824: strcat(filelog,".log"); /* */
14825: if((ficlog=fopen(filelog,"w"))==NULL) {
14826: printf("Problem with logfile %s\n",filelog);
14827: goto end;
14828: }
14829: fprintf(ficlog,"Log filename:%s\n",filelog);
14830: fprintf(ficlog,"Version %s %s",version,fullversion);
14831: fprintf(ficlog,"\nEnter the parameter file name: \n");
14832: fprintf(ficlog,"pathimach=%s\npathtot=%s\n\
14833: path=%s \n\
14834: optionfile=%s\n\
14835: optionfilext=%s\n\
14836: optionfilefiname='%s'\n",pathimach,pathtot,path,optionfile,optionfilext,optionfilefiname);
14837:
14838: syscompilerinfo(1);
14839:
14840: printf("Local time (at start):%s",strstart);
14841: fprintf(ficlog,"Local time (at start): %s",strstart);
14842: fflush(ficlog);
14843: /* (void) gettimeofday(&curr_time,&tzp); */
14844: /* printf("Elapsed time %d\n", asc_diff_time(curr_time.tm_sec-start_time.tm_sec,tmpout)); */
14845:
14846: /* */
14847: strcpy(fileres,"r");
14848: strcat(fileres, optionfilefiname);
14849: strcat(fileresu, optionfilefiname); /* Without r in front */
14850: strcat(fileres,".txt"); /* Other files have txt extension */
14851: strcat(fileresu,".txt"); /* Other files have txt extension */
14852:
14853: /* Main ---------arguments file --------*/
14854:
14855: if((ficpar=fopen(optionfile,"r"))==NULL) {
14856: printf("Problem with optionfile '%s' with errno='%s'\n",optionfile,strerror(errno));
14857: fprintf(ficlog,"Problem with optionfile '%s' with errno='%s'\n",optionfile,strerror(errno));
14858: fflush(ficlog);
14859: /* goto end; */
14860: exit(70);
14861: }
14862:
14863: strcpy(filereso,"o");
14864: strcat(filereso,fileresu);
14865: if((ficparo=fopen(filereso,"w"))==NULL) { /* opened on subdirectory */
14866: printf("Problem with Output resultfile: %s\n", filereso);
14867: fprintf(ficlog,"Problem with Output resultfile: %s\n", filereso);
14868: fflush(ficlog);
14869: goto end;
14870: }
14871: /*-------- Rewriting parameter file ----------*/
14872: strcpy(rfileres,"r"); /* "Rparameterfile */
14873: strcat(rfileres,optionfilefiname); /* Parameter file first name */
14874: strcat(rfileres,"."); /* */
14875: strcat(rfileres,optionfilext); /* Other files have txt extension */
14876: if((ficres =fopen(rfileres,"w"))==NULL) {
14877: printf("Problem writing new parameter file: %s\n", rfileres);goto end;
14878: fprintf(ficlog,"Problem writing new parameter file: %s\n", rfileres);goto end;
14879: fflush(ficlog);
14880: goto end;
14881: }
14882: fprintf(ficres,"#IMaCh %s\n",version);
14883:
14884:
14885: /* Reads comments: lines beginning with '#' */
14886: numlinepar=0;
14887: /* Is it a BOM UTF-8 Windows file? */
14888: /* First parameter line */
14889: while(fgets(line, MAXLINE, ficpar)) {
14890: noffset=0;
14891: if( line[0] == (char)0xEF && line[1] == (char)0xBB) /* EF BB BF */
14892: {
14893: noffset=noffset+3;
14894: printf("# File is an UTF8 Bom.\n"); // 0xBF
14895: }
14896: /* else if( line[0] == (char)0xFE && line[1] == (char)0xFF)*/
14897: else if( line[0] == (char)0xFF && line[1] == (char)0xFE)
14898: {
14899: noffset=noffset+2;
14900: printf("# File is an UTF16BE BOM file\n");
14901: }
14902: else if( line[0] == 0 && line[1] == 0)
14903: {
14904: if( line[2] == (char)0xFE && line[3] == (char)0xFF){
14905: noffset=noffset+4;
14906: printf("# File is an UTF16BE BOM file\n");
14907: }
14908: } else{
14909: ;/*printf(" Not a BOM file\n");*/
14910: }
14911:
14912: /* If line starts with a # it is a comment */
14913: if (line[noffset] == '#') {
14914: numlinepar++;
14915: fputs(line,stdout);
14916: fputs(line,ficparo);
14917: fputs(line,ficres);
14918: fputs(line,ficlog);
14919: continue;
14920: }else
14921: break;
14922: }
14923: if((num_filled=sscanf(line,"title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\n", \
14924: title, datafile, &lastobs, &firstpass,&lastpass)) !=EOF){
14925: if (num_filled != 5) {
14926: printf("Should be 5 parameters\n");
14927: fprintf(ficlog,"Should be 5 parameters\n");
14928: }
14929: numlinepar++;
14930: printf("title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\n", title, datafile, lastobs, firstpass,lastpass);
14931: fprintf(ficparo,"title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\n", title, datafile, lastobs, firstpass,lastpass);
14932: fprintf(ficres,"title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\n", title, datafile, lastobs, firstpass,lastpass);
14933: fprintf(ficlog,"title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\n", title, datafile, lastobs, firstpass,lastpass);
14934: }
14935: /* Second parameter line */
14936: while(fgets(line, MAXLINE, ficpar)) {
14937: /* while(fscanf(ficpar,"%[^\n]", line)) { */
14938: /* If line starts with a # it is a comment. Strangely fgets reads the EOL and fputs doesn't */
14939: if (line[0] == '#') {
14940: numlinepar++;
14941: printf("%s",line);
14942: fprintf(ficres,"%s",line);
14943: fprintf(ficparo,"%s",line);
14944: fprintf(ficlog,"%s",line);
14945: continue;
14946: }else
14947: break;
14948: }
14949: 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", \
14950: &ftol, &stepm, &ncovcol, &nqv, &ntv, &nqtv, &nlstate, &ndeath, &maxwav, &mle, &weightopt)) !=EOF){
14951: if (num_filled != 11) {
14952: 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");
14953: printf("but line=%s\n",line);
14954: 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");
14955: fprintf(ficlog,"but line=%s\n",line);
14956: }
14957: if( lastpass > maxwav){
14958: printf("Error (lastpass = %d) > (maxwav = %d)\n",lastpass, maxwav);
14959: fprintf(ficlog,"Error (lastpass = %d) > (maxwav = %d)\n",lastpass, maxwav);
14960: fflush(ficlog);
14961: goto end;
14962: }
14963: 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);
14964: 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);
14965: 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);
14966: 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);
14967: }
14968: /* ftolpl=6*ftol*1.e5; /\* 6.e-3 make convergences in less than 80 loops for the prevalence limit *\/ */
14969: /*ftolpl=6.e-4; *//* 6.e-3 make convergences in less than 80 loops for the prevalence limit */
14970: /* Third parameter line */
14971: while(fgets(line, MAXLINE, ficpar)) {
14972: /* If line starts with a # it is a comment */
14973: if (line[0] == '#') {
14974: numlinepar++;
14975: printf("%s",line);
14976: fprintf(ficres,"%s",line);
14977: fprintf(ficparo,"%s",line);
14978: fprintf(ficlog,"%s",line);
14979: continue;
14980: }else
14981: break;
14982: }
14983: if((num_filled=sscanf(line,"model=%[^.\n]", model)) !=EOF){ /* Every character after model but dot and return */
14984: if (num_filled != 1){
14985: printf("ERROR %d: Model should be at minimum 'model=1+age+' instead of '%s'\n",num_filled, line);
14986: fprintf(ficlog,"ERROR %d: Model should be at minimum 'model=1+age+' instead of '%s'\n",num_filled, line);
14987: model[0]='\0';
14988: goto end;
14989: }else{
14990: trimbtab(linetmp,line); /* Trims multiple blanks in line */
14991: strcpy(line, linetmp);
14992: }
14993: }
14994: if((num_filled=sscanf(line,"model=1+age%[^.\n]", model)) !=EOF){ /* Every character after 1+age but dot and return */
14995: if (num_filled != 1){
14996: printf("ERROR %d: Model should be at minimum 'model=1+age+' instead of '%s'\n",num_filled, line);
14997: fprintf(ficlog,"ERROR %d: Model should be at minimum 'model=1+age+' instead of '%s'\n",num_filled, line);
14998: model[0]='\0';
14999: goto end;
15000: }
15001: else{
15002: if (model[0]=='+'){
15003: for(i=1; i<=strlen(model);i++)
15004: modeltemp[i-1]=model[i];
15005: strcpy(model,modeltemp);
15006: }
15007: }
15008: /* printf(" model=1+age%s modeltemp= %s, model=1+age+%s\n",model, modeltemp, model);fflush(stdout); */
15009: printf("model=1+age+%s\n",model);fflush(stdout);
15010: fprintf(ficparo,"model=1+age+%s\n",model);fflush(stdout);
15011: fprintf(ficres,"model=1+age+%s\n",model);fflush(stdout);
15012: fprintf(ficlog,"model=1+age+%s\n",model);fflush(stdout);
15013: }
15014: /* 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); */
15015: /* numlinepar=numlinepar+3; /\* In general *\/ */
15016: /* 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); */
15017: /* 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); */
15018: /* 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); */
15019: fflush(ficlog);
15020: /* if(model[0]=='#'|| model[0]== '\0'){ */
15021: if(model[0]=='#'){
15022: printf("Error in 'model' line: model should start with 'model=1+age+' and end without space \n \
15023: 'model=1+age+' or 'model=1+age+V1.' or 'model=1+age+age*age+V1+V1*age' or \n \
15024: 'model=1+age+V1+V2' or 'model=1+age+V1+V2+V1*V2' etc. \n"); \
15025: if(mle != -1){
15026: 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");
15027: exit(1);
15028: }
15029: }
15030: while((c=getc(ficpar))=='#' && c!= EOF){
15031: ungetc(c,ficpar);
15032: fgets(line, MAXLINE, ficpar);
15033: numlinepar++;
15034: if(line[1]=='q'){ /* This #q will quit imach (the answer is q) */
15035: z[0]=line[1];
15036: }else if(line[1]=='d'){ /* For debugging individual values of covariates in ficresilk */
15037: debugILK=1;printf("DebugILK\n");
15038: }
15039: /* printf("****line [1] = %c \n",line[1]); */
15040: fputs(line, stdout);
15041: //puts(line);
15042: fputs(line,ficparo);
15043: fputs(line,ficlog);
15044: }
15045: ungetc(c,ficpar);
15046:
15047:
15048: covar=matrix(0,NCOVMAX,firstobs,lastobs); /**< used in readdata */
15049: if(nqv>=1)coqvar=matrix(1,nqv,firstobs,lastobs); /**< Fixed quantitative covariate */
15050: if(nqtv>=1)cotqvar=ma3x(1,maxwav,1,nqtv,firstobs,lastobs); /**< Time varying quantitative covariate */
15051: /* if(ntv+nqtv>=1)cotvar=ma3x(1,maxwav,1,ntv+nqtv,firstobs,lastobs); /\**< Time varying covariate (dummy and quantitative)*\/ */
15052: if(ntv+nqtv>=1)cotvar=ma3x(1,maxwav,ncovcol+nqv+1,ncovcol+nqv+ntv+nqtv,firstobs,lastobs); /**< Might be better */
15053: cptcovn=0; /*Number of covariates, i.e. number of '+' in model statement plus one, indepently of n in Vn*/
15054: /* v1+v2+v3+v2*v4+v5*age makes cptcovn = 5
15055: v1+v2*age+v2*v3 makes cptcovn = 3
15056: */
15057: if (strlen(model)>1)
15058: 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*/
15059: else
15060: ncovmodel=2; /* Constant and age */
15061: nforce= (nlstate+ndeath-1)*nlstate; /* Number of forces ij from state i to j */
15062: npar= nforce*ncovmodel; /* Number of parameters like aij*/
15063: if(npar >MAXPARM || nlstate >NLSTATEMAX || ndeath >NDEATHMAX || ncovmodel>NCOVMAX){
15064: 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);
15065: 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);
15066: fflush(stdout);
15067: fclose (ficlog);
15068: goto end;
15069: }
15070: delti3= ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel);
15071: delti=delti3[1][1];
15072: /*delti=vector(1,npar); *//* Scale of each paramater (output from hesscov)*/
15073: if(mle==-1){ /* Print a wizard for help writing covariance matrix */
15074: /* We could also provide initial parameters values giving by simple logistic regression
15075: * only one way, that is without matrix product. We will have nlstate maximizations */
15076: /* for(i=1;i<nlstate;i++){ */
15077: /* /\*reducing xi for 1 to npar to 1 to ncovmodel; *\/ */
15078: /* mlikeli(ficres,p, ncovmodel, ncovmodel, nlstate, ftol, funcnoprod); */
15079: /* } */
15080: prwizard(ncovmodel, nlstate, ndeath, model, ficparo);
15081: printf(" You chose mle=-1, look at file %s for a template of covariance matrix \n",filereso);
15082: fprintf(ficlog," You chose mle=-1, look at file %s for a template of covariance matrix \n",filereso);
15083: free_ma3x(delti3,1,nlstate,1, nlstate+ndeath-1,1,ncovmodel);
15084: fclose (ficparo);
15085: fclose (ficlog);
15086: goto end;
15087: exit(0);
15088: } else if(mle==-5) { /* Main Wizard */
15089: prwizard(ncovmodel, nlstate, ndeath, model, ficparo);
15090: printf(" You chose mle=-3, look at file %s for a template of covariance matrix \n",filereso);
15091: fprintf(ficlog," You chose mle=-3, look at file %s for a template of covariance matrix \n",filereso);
15092: param= ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel);
15093: matcov=matrix(1,npar,1,npar);
15094: hess=matrix(1,npar,1,npar);
15095: } else{ /* Begin of mle != -1 or -5 */
15096: /* Read guessed parameters */
15097: /* Reads comments: lines beginning with '#' */
15098: while((c=getc(ficpar))=='#' && c!= EOF){
15099: ungetc(c,ficpar);
15100: fgets(line, MAXLINE, ficpar);
15101: numlinepar++;
15102: fputs(line,stdout);
15103: fputs(line,ficparo);
15104: fputs(line,ficlog);
15105: }
15106: ungetc(c,ficpar);
15107:
15108: param= ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel);
15109: paramstart= ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel);
15110: for(i=1; i <=nlstate; i++){
15111: j=0;
15112: for(jj=1; jj <=nlstate+ndeath; jj++){
15113: if(jj==i) continue;
15114: j++;
15115: while((c=getc(ficpar))=='#' && c!= EOF){
15116: ungetc(c,ficpar);
15117: fgets(line, MAXLINE, ficpar);
15118: numlinepar++;
15119: fputs(line,stdout);
15120: fputs(line,ficparo);
15121: fputs(line,ficlog);
15122: }
15123: ungetc(c,ficpar);
15124: fscanf(ficpar,"%1d%1d",&i1,&j1);
15125: if ((i1 != i) || (j1 != jj)){
15126: printf("Error in line parameters number %d, %1d%1d instead of %1d%1d \n \
15127: It might be a problem of design; if ncovcol and the model are correct\n \
15128: run imach with mle=-1 to get a correct template of the parameter file.\n",numlinepar, i,j, i1, j1);
15129: exit(1);
15130: }
15131: fprintf(ficparo,"%1d%1d",i1,j1);
15132: if(mle==1)
15133: printf("%1d%1d",i,jj);
15134: fprintf(ficlog,"%1d%1d",i,jj);
15135: for(k=1; k<=ncovmodel;k++){
15136: fscanf(ficpar," %lf",¶m[i][j][k]);
15137: if(mle==1){
15138: printf(" %lf",param[i][j][k]);
15139: fprintf(ficlog," %lf",param[i][j][k]);
15140: }
15141: else
15142: fprintf(ficlog," %lf",param[i][j][k]);
15143: fprintf(ficparo," %lf",param[i][j][k]);
15144: }
15145: fscanf(ficpar,"\n");
15146: numlinepar++;
15147: if(mle==1)
15148: printf("\n");
15149: fprintf(ficlog,"\n");
15150: fprintf(ficparo,"\n");
15151: }
15152: }
15153: fflush(ficlog);
15154:
15155: /* Reads parameters values */
15156: p=param[1][1];
15157: pstart=paramstart[1][1];
15158:
15159: /* Reads comments: lines beginning with '#' */
15160: while((c=getc(ficpar))=='#' && c!= EOF){
15161: ungetc(c,ficpar);
15162: fgets(line, MAXLINE, ficpar);
15163: numlinepar++;
15164: fputs(line,stdout);
15165: fputs(line,ficparo);
15166: fputs(line,ficlog);
15167: }
15168: ungetc(c,ficpar);
15169:
15170: for(i=1; i <=nlstate; i++){
15171: for(j=1; j <=nlstate+ndeath-1; j++){
15172: fscanf(ficpar,"%1d%1d",&i1,&j1);
15173: if ( (i1-i) * (j1-j) != 0){
15174: printf("Error in line parameters number %d, %1d%1d instead of %1d%1d \n",numlinepar, i,j, i1, j1);
15175: exit(1);
15176: }
15177: printf("%1d%1d",i,j);
15178: fprintf(ficparo,"%1d%1d",i1,j1);
15179: fprintf(ficlog,"%1d%1d",i1,j1);
15180: for(k=1; k<=ncovmodel;k++){
15181: fscanf(ficpar,"%le",&delti3[i][j][k]);
15182: printf(" %le",delti3[i][j][k]);
15183: fprintf(ficparo," %le",delti3[i][j][k]);
15184: fprintf(ficlog," %le",delti3[i][j][k]);
15185: }
15186: fscanf(ficpar,"\n");
15187: numlinepar++;
15188: printf("\n");
15189: fprintf(ficparo,"\n");
15190: fprintf(ficlog,"\n");
15191: }
15192: }
15193: fflush(ficlog);
15194:
15195: /* Reads covariance matrix */
15196: delti=delti3[1][1];
15197:
15198:
15199: /* free_ma3x(delti3,1,nlstate,1,nlstate+ndeath-1,1,ncovmodel); */ /* Hasn't to to freed here otherwise delti is no more allocated */
15200:
15201: /* Reads comments: lines beginning with '#' */
15202: while((c=getc(ficpar))=='#' && c!= EOF){
15203: ungetc(c,ficpar);
15204: fgets(line, MAXLINE, ficpar);
15205: numlinepar++;
15206: fputs(line,stdout);
15207: fputs(line,ficparo);
15208: fputs(line,ficlog);
15209: }
15210: ungetc(c,ficpar);
15211:
15212: matcov=matrix(1,npar,1,npar);
15213: hess=matrix(1,npar,1,npar);
15214: for(i=1; i <=npar; i++)
15215: for(j=1; j <=npar; j++) matcov[i][j]=0.;
15216:
15217: /* Scans npar lines */
15218: for(i=1; i <=npar; i++){
15219: count=fscanf(ficpar,"%1d%1d%d",&i1,&j1,&jk);
15220: if(count != 3){
15221: printf("Error! Error in parameter file %s at line %d after line starting with %1d%1d%1d\n\
15222: This is probably because your covariance matrix doesn't \n contain exactly %d lines corresponding to your model line '1+age+%s'.\n\
15223: Please run with mle=-1 to get a correct covariance matrix.\n",optionfile,numlinepar, i1,j1,jk, npar, model);
15224: fprintf(ficlog,"Error! Error in parameter file %s at line %d after line starting with %1d%1d%1d\n\
15225: This is probably because your covariance matrix doesn't \n contain exactly %d lines corresponding to your model line '1+age+%s'.\n\
15226: Please run with mle=-1 to get a correct covariance matrix.\n",optionfile,numlinepar, i1,j1,jk, npar, model);
15227: exit(1);
15228: }else{
15229: if(mle==1)
15230: printf("%1d%1d%d",i1,j1,jk);
15231: }
15232: fprintf(ficlog,"%1d%1d%d",i1,j1,jk);
15233: fprintf(ficparo,"%1d%1d%d",i1,j1,jk);
15234: for(j=1; j <=i; j++){
15235: fscanf(ficpar," %le",&matcov[i][j]);
15236: if(mle==1){
15237: printf(" %.5le",matcov[i][j]);
15238: }
15239: fprintf(ficlog," %.5le",matcov[i][j]);
15240: fprintf(ficparo," %.5le",matcov[i][j]);
15241: }
15242: fscanf(ficpar,"\n");
15243: numlinepar++;
15244: if(mle==1)
15245: printf("\n");
15246: fprintf(ficlog,"\n");
15247: fprintf(ficparo,"\n");
15248: }
15249: /* End of read covariance matrix npar lines */
15250: for(i=1; i <=npar; i++)
15251: for(j=i+1;j<=npar;j++)
15252: matcov[i][j]=matcov[j][i];
15253:
15254: if(mle==1)
15255: printf("\n");
15256: fprintf(ficlog,"\n");
15257:
15258: fflush(ficlog);
15259:
15260: } /* End of mle != -3 */
15261:
15262: /* Main data
15263: */
15264: nobs=lastobs-firstobs+1; /* was = lastobs;*/
15265: /* num=lvector(1,n); */
15266: /* moisnais=vector(1,n); */
15267: /* annais=vector(1,n); */
15268: /* moisdc=vector(1,n); */
15269: /* andc=vector(1,n); */
15270: /* weight=vector(1,n); */
15271: /* agedc=vector(1,n); */
15272: /* cod=ivector(1,n); */
15273: /* for(i=1;i<=n;i++){ */
15274: num=lvector(firstobs,lastobs);
15275: moisnais=vector(firstobs,lastobs);
15276: annais=vector(firstobs,lastobs);
15277: moisdc=vector(firstobs,lastobs);
15278: andc=vector(firstobs,lastobs);
15279: weight=vector(firstobs,lastobs);
15280: agedc=vector(firstobs,lastobs);
15281: cod=ivector(firstobs,lastobs);
15282: for(i=firstobs;i<=lastobs;i++){
15283: num[i]=0;
15284: moisnais[i]=0;
15285: annais[i]=0;
15286: moisdc[i]=0;
15287: andc[i]=0;
15288: agedc[i]=0;
15289: cod[i]=0;
15290: weight[i]=1.0; /* Equal weights, 1 by default */
15291: }
15292: mint=matrix(1,maxwav,firstobs,lastobs);
15293: anint=matrix(1,maxwav,firstobs,lastobs);
15294: s=imatrix(1,maxwav+1,firstobs,lastobs); /* s[i][j] health state for wave i and individual j */
15295: /* printf("BUG ncovmodel=%d NCOVMAX=%d 2**ncovmodel=%f BUG\n",ncovmodel,NCOVMAX,pow(2,ncovmodel)); */
15296: tab=ivector(1,NCOVMAX);
15297: ncodemax=ivector(1,NCOVMAX); /* Number of code per covariate; if O and 1 only, 2**ncov; V1+V2+V3+V4=>16 */
15298: ncodemaxwundef=ivector(1,NCOVMAX); /* Number of code per covariate; if - 1 O and 1 only, 2**ncov; V1+V2+V3+V4=>16 */
15299:
15300: /* Reads data from file datafile */
15301: if (readdata(datafile, firstobs, lastobs, &imx)==1)
15302: goto end;
15303:
15304: /* Calculation of the number of parameters from char model */
15305: /* modelsav=V2+V1+V4+age*V3 strb=age*V3 stra=V2+V1+V4
15306: k=4 (age*V3) Tvar[k=4]= 3 (from V3) Tag[cptcovage=1]=4
15307: k=3 V4 Tvar[k=3]= 4 (from V4)
15308: k=2 V1 Tvar[k=2]= 1 (from V1)
15309: k=1 Tvar[1]=2 (from V2)
15310: */
15311:
15312: Tvar=ivector(1,NCOVMAX); /* Was 15 changed to NCOVMAX. */
15313: TvarsDind=ivector(1,NCOVMAX); /* */
15314: TnsdVar=ivector(1,NCOVMAX); /* */
15315: /* for(i=1; i<=NCOVMAX;i++) TnsdVar[i]=3; */
15316: TvarsD=ivector(1,NCOVMAX); /* */
15317: TvarsQind=ivector(1,NCOVMAX); /* */
15318: TvarsQ=ivector(1,NCOVMAX); /* */
15319: TvarF=ivector(1,NCOVMAX); /* */
15320: TvarFind=ivector(1,NCOVMAX); /* */
15321: TvarV=ivector(1,NCOVMAX); /* */
15322: TvarVind=ivector(1,NCOVMAX); /* */
15323: TvarA=ivector(1,NCOVMAX); /* */
15324: TvarAind=ivector(1,NCOVMAX); /* */
15325: TvarFD=ivector(1,NCOVMAX); /* */
15326: TvarFDind=ivector(1,NCOVMAX); /* */
15327: TvarFQ=ivector(1,NCOVMAX); /* */
15328: TvarFQind=ivector(1,NCOVMAX); /* */
15329: TvarVD=ivector(1,NCOVMAX); /* */
15330: TvarVDind=ivector(1,NCOVMAX); /* */
15331: TvarVQ=ivector(1,NCOVMAX); /* */
15332: TvarVQind=ivector(1,NCOVMAX); /* */
15333: TvarVV=ivector(1,NCOVMAX); /* */
15334: TvarVVind=ivector(1,NCOVMAX); /* */
15335: TvarVVA=ivector(1,NCOVMAX); /* */
15336: TvarVVAind=ivector(1,NCOVMAX); /* */
15337: TvarAVVA=ivector(1,NCOVMAX); /* */
15338: TvarAVVAind=ivector(1,NCOVMAX); /* */
15339:
15340: Tvalsel=vector(1,NCOVMAX); /* */
15341: Tvarsel=ivector(1,NCOVMAX); /* */
15342: Typevar=ivector(-1,NCOVMAX); /* -1 to 2 */
15343: Fixed=ivector(-1,NCOVMAX); /* -1 to 3 */
15344: Dummy=ivector(-1,NCOVMAX); /* -1 to 3 */
15345: DummyV=ivector(-1,NCOVMAX); /* 1 to 3 */
15346: FixedV=ivector(-1,NCOVMAX); /* 1 to 3 */
15347:
15348: /* V2+V1+V4+age*V3 is a model with 4 covariates (3 plus signs).
15349: For each model-covariate stores the data-covariate id. Tvar[1]=2, Tvar[2]=1, Tvar[3]=4,
15350: Tvar[4=age*V3] is 3 and 'age' is recorded in Tage.
15351: */
15352: /* For model-covariate k tells which data-covariate to use but
15353: because this model-covariate is a construction we invent a new column
15354: ncovcol + k1
15355: If already ncovcol=4 and model=V2+V1+V1*V4+age*V3
15356: Tvar[3=V1*V4]=4+1 etc */
15357: Tprod=ivector(1,NCOVMAX); /* Gives the k position of the k1 product */
15358: Tposprod=ivector(1,NCOVMAX); /* Gives the k1 product from the k position */
15359: /* Tprod[k1=1]=3(=V1*V4) for V2+V1+V1*V4+age*V3
15360: if V2+V1+V1*V4+age*V3+V3*V2 TProd[k1=2]=5 (V3*V2)
15361: Tposprod[k]=k1 , Tposprod[3]=1, Tposprod[5]=2
15362: */
15363: Tvaraff=ivector(1,NCOVMAX); /* Unclear */
15364: 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
15365: * For V3*V2 (in V2+V1+V1*V4+age*V3+V3*V2), V3*V2 position is 2nd.
15366: * Tvard[k1=2][1]=3 (V3) Tvard[k1=2][2]=2(V2) */
15367: Tvardk=imatrix(0,NCOVMAX,1,2);
15368: Tage=ivector(1,NCOVMAX); /* Gives the covariate id of covariates associated with age: V2 + V1 + age*V4 + V3*age
15369: 4 covariates (3 plus signs)
15370: Tage[1=V3*age]= 4; Tage[2=age*V4] = 3
15371: */
15372: for(i=1;i<NCOVMAX;i++)
15373: Tage[i]=0;
15374: Tmodelind=ivector(1,NCOVMAX);/** gives the k model position of an
15375: * individual dummy, fixed or varying:
15376: * Tmodelind[Tvaraff[3]]=9,Tvaraff[1]@9={4,
15377: * 3, 1, 0, 0, 0, 0, 0, 0},
15378: * model=V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 ,
15379: * V1 df, V2 qf, V3 & V4 dv, V5 qv
15380: * Tmodelind[1]@9={9,0,3,2,}*/
15381: TmodelInvind=ivector(1,NCOVMAX); /* TmodelInvind=Tvar[k]- ncovcol-nqv={5-2-1=2,*/
15382: TmodelInvQind=ivector(1,NCOVMAX);/** gives the k model position of an
15383: * individual quantitative, fixed or varying:
15384: * Tmodelqind[1]=1,Tvaraff[1]@9={4,
15385: * 3, 1, 0, 0, 0, 0, 0, 0},
15386: * model=V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1*/
15387:
15388: /* Probably useless zeroes */
15389: for(i=1;i<NCOVMAX;i++){
15390: DummyV[i]=0;
15391: FixedV[i]=0;
15392: }
15393:
15394: for(i=1; i <=ncovcol;i++){
15395: DummyV[i]=0;
15396: FixedV[i]=0;
15397: }
15398: for(i=ncovcol+1; i <=ncovcol+nqv;i++){
15399: DummyV[i]=1;
15400: FixedV[i]=0;
15401: }
15402: for(i=ncovcol+nqv+1; i <=ncovcol+nqv+ntv;i++){
15403: DummyV[i]=0;
15404: FixedV[i]=1;
15405: }
15406: for(i=ncovcol+nqv+ntv+1; i <=ncovcol+nqv+ntv+nqtv;i++){
15407: DummyV[i]=1;
15408: FixedV[i]=1;
15409: }
15410: for(i=1; i <=ncovcol+nqv+ntv+nqtv;i++){
15411: printf("Covariate type in the data: V%d, DummyV(V%d)=%d, FixedV(V%d)=%d\n",i,i,DummyV[i],i,FixedV[i]);
15412: fprintf(ficlog,"Covariate type in the data: V%d, DummyV(V%d)=%d, FixedV(V%d)=%d\n",i,i,DummyV[i],i,FixedV[i]);
15413: }
15414:
15415:
15416:
15417: /* Main decodemodel */
15418:
15419:
15420: if(decodemodel(model, lastobs) == 1) /* In order to get Tvar[k] V4+V3+V5 p Tvar[1]@3 = {4, 3, 5}*/
15421: goto end;
15422:
15423: if((double)(lastobs-imx)/(double)imx > 1.10){
15424: nbwarn++;
15425: 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);
15426: 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);
15427: }
15428: /* if(mle==1){*/
15429: if (weightopt != 1) { /* Maximisation without weights. We can have weights different from 1 but want no weight*/
15430: for(i=1;i<=imx;i++) weight[i]=1.0; /* changed to imx */
15431: }
15432:
15433: /*-calculation of age at interview from date of interview and age at death -*/
15434: agev=matrix(1,maxwav,1,imx);
15435:
15436: if(calandcheckages(imx, maxwav, &agemin, &agemax, &nberr, &nbwarn) == 1)
15437: goto end;
15438:
15439:
15440: agegomp=(int)agemin;
15441: free_vector(moisnais,firstobs,lastobs);
15442: free_vector(annais,firstobs,lastobs);
15443: /* free_matrix(mint,1,maxwav,1,n);
15444: free_matrix(anint,1,maxwav,1,n);*/
15445: /* free_vector(moisdc,1,n); */
15446: /* free_vector(andc,1,n); */
15447: /* */
15448:
15449: wav=ivector(1,imx);
15450: /* dh=imatrix(1,lastpass-firstpass+1,1,imx); */
15451: /* bh=imatrix(1,lastpass-firstpass+1,1,imx); */
15452: /* mw=imatrix(1,lastpass-firstpass+1,1,imx); */
15453: 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.*/
15454: bh=imatrix(1,lastpass-firstpass+2,1,imx);
15455: mw=imatrix(1,lastpass-firstpass+2,1,imx);
15456:
15457: /* Concatenates waves */
15458: /* Concatenates waves: wav[i] is the number of effective (useful waves) of individual i.
15459: Death is a valid wave (if date is known).
15460: mw[mi][i] is the number of (mi=1 to wav[i]) effective wave out of mi of individual i
15461: dh[m][i] or dh[mw[mi][i]][i] is the delay between two effective waves m=mw[mi][i]
15462: and mw[mi+1][i]. dh depends on stepm.
15463: */
15464:
15465: concatwav(wav, dh, bh, mw, s, agedc, agev, firstpass, lastpass, imx, nlstate, stepm);
15466: /* Concatenates waves */
15467:
15468: free_vector(moisdc,firstobs,lastobs);
15469: free_vector(andc,firstobs,lastobs);
15470:
15471: /* Routine tricode is to calculate cptcoveff (real number of unique covariates) and to associate covariable number and modality */
15472: nbcode=imatrix(0,NCOVMAX,0,NCOVMAX);
15473: ncodemax[1]=1;
15474: Ndum =ivector(-1,NCOVMAX);
15475: cptcoveff=0;
15476: if (ncovmodel-nagesqr > 2 ){ /* That is if covariate other than cst, age and age*age */
15477: tricode(&cptcoveff,Tvar,nbcode,imx, Ndum); /**< Fills nbcode[Tvar[j]][l]; as well as calculate cptcoveff or number of total effective dummy covariates*/
15478: }
15479:
15480: ncovcombmax=pow(2,cptcoveff);
15481: invalidvarcomb=ivector(0, ncovcombmax);
15482: for(i=0;i<ncovcombmax;i++)
15483: invalidvarcomb[i]=0;
15484:
15485: /* Nbcode gives the value of the lth modality (currently 1 to 2) of jth covariate, in
15486: V2+V1*age, there are 3 covariates Tvar[2]=1 (V1).*/
15487: /* 1 to ncodemax[j] which is the maximum value of this jth covariate */
15488:
15489: /* codtab=imatrix(1,100,1,10);*/ /* codtab[h,k]=( (h-1) - mod(k-1,2**(k-1) )/2**(k-1) */
15490: /*printf(" codtab[1,1],codtab[100,10]=%d,%d\n", codtab[1][1],codtabm(100,10));*/
15491: /* codtab gives the value 1 or 2 of the hth combination of k covariates (1 or 2).*/
15492: /* nbcode[Tvaraff[j]][codtabm(h,j)]) : if there are only 2 modalities for a covariate j,
15493: * codtabm(h,j) gives its value classified at position h and nbcode gives how it is coded
15494: * (currently 0 or 1) in the data.
15495: * In a loop on h=1 to 2**k, and a loop on j (=1 to k), we get the value of
15496: * corresponding modality (h,j).
15497: */
15498:
15499: h=0;
15500: /*if (cptcovn > 0) */
15501: m=pow(2,cptcoveff);
15502:
15503: /**< codtab(h,k) k = codtab[h,k]=( (h-1) - mod(k-1,2**(k-1) )/2**(k-1) + 1
15504: * For k=4 covariates, h goes from 1 to m=2**k
15505: * codtabm(h,k)= (1 & (h-1) >> (k-1)) + 1;
15506: * #define codtabm(h,k) (1 & (h-1) >> (k-1))+1
15507: * h\k 1 2 3 4 * h-1\k-1 4 3 2 1
15508: *______________________________ *______________________
15509: * 1 i=1 1 i=1 1 i=1 1 i=1 1 * 0 0 0 0 0
15510: * 2 2 1 1 1 * 1 0 0 0 1
15511: * 3 i=2 1 2 1 1 * 2 0 0 1 0
15512: * 4 2 2 1 1 * 3 0 0 1 1
15513: * 5 i=3 1 i=2 1 2 1 * 4 0 1 0 0
15514: * 6 2 1 2 1 * 5 0 1 0 1
15515: * 7 i=4 1 2 2 1 * 6 0 1 1 0
15516: * 8 2 2 2 1 * 7 0 1 1 1
15517: * 9 i=5 1 i=3 1 i=2 1 2 * 8 1 0 0 0
15518: * 10 2 1 1 2 * 9 1 0 0 1
15519: * 11 i=6 1 2 1 2 * 10 1 0 1 0
15520: * 12 2 2 1 2 * 11 1 0 1 1
15521: * 13 i=7 1 i=4 1 2 2 * 12 1 1 0 0
15522: * 14 2 1 2 2 * 13 1 1 0 1
15523: * 15 i=8 1 2 2 2 * 14 1 1 1 0
15524: * 16 2 2 2 2 * 15 1 1 1 1
15525: */
15526: /* How to do the opposite? From combination h (=1 to 2**k) how to get the value on the covariates? */
15527: /* from h=5 and m, we get then number of covariates k=log(m)/log(2)=4
15528: * and the value of each covariate?
15529: * V1=1, V2=1, V3=2, V4=1 ?
15530: * h-1=4 and 4 is 0100 or reverse 0010, and +1 is 1121 ok.
15531: * h=6, 6-1=5, 5 is 0101, 1010, 2121, V1=2nd, V2=1st, V3=2nd, V4=1st.
15532: * In order to get the real value in the data, we use nbcode
15533: * nbcode[Tvar[3][2nd]]=1 and nbcode[Tvar[4][1]]=0
15534: * We are keeping this crazy system in order to be able (in the future?)
15535: * to have more than 2 values (0 or 1) for a covariate.
15536: * #define codtabm(h,k) (1 & (h-1) >> (k-1))+1
15537: * h=6, k=2? h-1=5=0101, reverse 1010, +1=2121, k=2nd position: value is 1: codtabm(6,2)=1
15538: * bbbbbbbb
15539: * 76543210
15540: * h-1 00000101 (6-1=5)
15541: *(h-1)>>(k-1)= 00000010 >> (2-1) = 1 right shift
15542: * &
15543: * 1 00000001 (1)
15544: * 00000000 = 1 & ((h-1) >> (k-1))
15545: * +1= 00000001 =1
15546: *
15547: * h=14, k=3 => h'=h-1=13, k'=k-1=2
15548: * h' 1101 =2^3+2^2+0x2^1+2^0
15549: * >>k' 11
15550: * & 00000001
15551: * = 00000001
15552: * +1 = 00000010=2 = codtabm(14,3)
15553: * Reverse h=6 and m=16?
15554: * cptcoveff=log(16)/log(2)=4 covariate: 6-1=5=0101 reversed=1010 +1=2121 =>V1=2, V2=1, V3=2, V4=1.
15555: * for (j=1 to cptcoveff) Vj=decodtabm(j,h,cptcoveff)
15556: * decodtabm(h,j,cptcoveff)= (((h-1) >> (j-1)) & 1) +1
15557: * decodtabm(h,j,cptcoveff)= (h <= (1<<cptcoveff)?(((h-1) >> (j-1)) & 1) +1 : -1)
15558: * V3=decodtabm(14,3,2**4)=2
15559: * h'=13 1101 =2^3+2^2+0x2^1+2^0
15560: *(h-1) >> (j-1) 0011 =13 >> 2
15561: * &1 000000001
15562: * = 000000001
15563: * +1= 000000010 =2
15564: * 2211
15565: * V1=1+1, V2=0+1, V3=1+1, V4=1+1
15566: * V3=2
15567: * codtabm and decodtabm are identical
15568: */
15569:
15570:
15571: free_ivector(Ndum,-1,NCOVMAX);
15572:
15573:
15574:
15575: /* Initialisation of ----------- gnuplot -------------*/
15576: strcpy(optionfilegnuplot,optionfilefiname);
15577: if(mle==-3)
15578: strcat(optionfilegnuplot,"-MORT_");
15579: strcat(optionfilegnuplot,".gp");
15580:
15581: if((ficgp=fopen(optionfilegnuplot,"w"))==NULL) {
15582: printf("Problem with file %s",optionfilegnuplot);
15583: }
15584: else{
15585: fprintf(ficgp,"\n# IMaCh-%s\n", version);
15586: fprintf(ficgp,"# %s\n", optionfilegnuplot);
15587: //fprintf(ficgp,"set missing 'NaNq'\n");
15588: fprintf(ficgp,"set datafile missing 'NaNq'\n");
15589: }
15590: /* fclose(ficgp);*/
15591:
15592:
15593: /* Initialisation of --------- index.htm --------*/
15594:
15595: strcpy(optionfilehtm,optionfilefiname); /* Main html file */
15596: if(mle==-3)
15597: strcat(optionfilehtm,"-MORT_");
15598: strcat(optionfilehtm,".htm");
15599: if((fichtm=fopen(optionfilehtm,"w"))==NULL) {
15600: printf("Problem with %s \n",optionfilehtm);
15601: exit(0);
15602: }
15603:
15604: strcpy(optionfilehtmcov,optionfilefiname); /* Only for matrix of covariance */
15605: strcat(optionfilehtmcov,"-cov.htm");
15606: if((fichtmcov=fopen(optionfilehtmcov,"w"))==NULL) {
15607: printf("Problem with %s \n",optionfilehtmcov), exit(0);
15608: }
15609: else{
15610: fprintf(fichtmcov,"<html><head>\n<title>IMaCh Cov %s</title></head>\n <body><font size=\"2\">%s <br> %s</font> \
15611: <hr size=\"2\" color=\"#EC5E5E\"> \n\
15612: Title=%s <br>Datafile=%s Firstpass=%d Lastpass=%d Stepm=%d Weight=%d Model=1+age+%s<br>\n",\
15613: optionfilehtmcov,version,fullversion,title,datafile,firstpass,lastpass,stepm, weightopt, model);
15614: }
15615:
15616: fprintf(fichtm,"<html><head>\n<meta charset=\"utf-8\"/><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n\
15617: <title>IMaCh %s</title></head>\n\
15618: <body><font size=\"7\"><a href=http:/euroreves.ined.fr/imach>IMaCh for Interpolated Markov Chain</a> </font><br>\n\
15619: <font size=\"3\">Sponsored by Copyright (C) 2002-2015 <a href=http://www.ined.fr>INED</a>\
15620: -EUROREVES-Institut de longévité-2013-2022-Japan Society for the Promotion of Sciences 日本学術振興会 \
15621: (<a href=https://www.jsps.go.jp/english/e-grants/>Grant-in-Aid for Scientific Research 25293121</a>) - \
15622: <a href=https://software.intel.com/en-us>Intel Software 2015-2018</a></font><br> \n", optionfilehtm);
15623:
15624: fprintf(fichtm,"<hr size=\"2\" color=\"#EC5E5E\"> \n\
15625: <font size=\"2\">IMaCh-%s <br> %s</font> \
15626: <hr size=\"2\" color=\"#EC5E5E\"> \n\
15627: 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\
15628: \n\
15629: <hr size=\"2\" color=\"#EC5E5E\">\
15630: <ul><li><h4>Parameter files</h4>\n\
15631: - Parameter file: <a href=\"%s.%s\">%s.%s</a><br>\n\
15632: - Copy of the parameter file: <a href=\"o%s\">o%s</a><br>\n\
15633: - Log file of the run: <a href=\"%s\">%s</a><br>\n\
15634: - Gnuplot file name: <a href=\"%s\">%s</a><br>\n\
15635: - Date and time at start: %s</ul>\n",\
15636: version,fullversion,optionfilehtm,optionfilehtm,title,datafile,datafile,firstpass,lastpass,stepm, weightopt, model, \
15637: optionfilefiname,optionfilext,optionfilefiname,optionfilext,\
15638: fileres,fileres,\
15639: filelog,filelog,optionfilegnuplot,optionfilegnuplot,strstart);
15640: fflush(fichtm);
15641:
15642: strcpy(pathr,path);
15643: strcat(pathr,optionfilefiname);
15644: #ifdef WIN32
15645: _chdir(optionfilefiname); /* Move to directory named optionfile */
15646: #else
15647: chdir(optionfilefiname); /* Move to directory named optionfile */
15648: #endif
15649:
15650:
15651: /* Calculates basic frequencies. Computes observed prevalence at single age
15652: and for any valid combination of covariates
15653: and prints on file fileres'p'. */
15654: freqsummary(fileres, p, pstart, (double)agemin, agemax, s, agev, nlstate, imx, Tvaraff, invalidvarcomb, nbcode, ncodemax,mint,anint,strstart, \
15655: firstpass, lastpass, stepm, weightopt, model);
15656:
15657: fprintf(fichtm,"\n");
15658: 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",\
15659: ftol, stepm);
15660: fprintf(fichtm,"\n<li>Number of fixed dummy covariates: ncovcol=%d ", ncovcol);
15661: ncurrv=1;
15662: for(i=ncurrv; i <=ncovcol; i++) fprintf(fichtm,"V%d ", i);
15663: fprintf(fichtm,"\n<li> Number of fixed quantitative variables: nqv=%d ", nqv);
15664: ncurrv=i;
15665: for(i=ncurrv; i <=ncurrv-1+nqv; i++) fprintf(fichtm,"V%d ", i);
15666: fprintf(fichtm,"\n<li> Number of time varying (wave varying) dummy covariates: ntv=%d ", ntv);
15667: ncurrv=i;
15668: for(i=ncurrv; i <=ncurrv-1+ntv; i++) fprintf(fichtm,"V%d ", i);
15669: fprintf(fichtm,"\n<li>Number of time varying quantitative covariates: nqtv=%d ", nqtv);
15670: ncurrv=i;
15671: for(i=ncurrv; i <=ncurrv-1+nqtv; i++) fprintf(fichtm,"V%d ", i);
15672: 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", \
15673: nlstate, ndeath, maxwav, mle, weightopt);
15674:
15675: fprintf(fichtm,"<h4> Diagram of states <a href=\"%s_.svg\">%s_.svg</a></h4> \n\
15676: <img src=\"%s_.svg\">", subdirf2(optionfilefiname,"D_"),subdirf2(optionfilefiname,"D_"),subdirf2(optionfilefiname,"D_"));
15677:
15678:
15679: fprintf(fichtm,"\n<h4>Some descriptive statistics </h4>\n<br>Number of (used) observations=%d <br>\n\
15680: Youngest age at first (selected) pass %.2f, oldest age %.2f<br>\n\
15681: Interval (in months) between two waves: Min=%d Max=%d Mean=%.2lf<br>\n",\
15682: imx,agemin,agemax,jmin,jmax,jmean);
15683: pmmij= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
15684: oldms= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
15685: newms= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
15686: savms= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
15687: oldm=oldms; newm=newms; savm=savms; /* Keeps fixed addresses to free */
15688:
15689: /* For Powell, parameters are in a vector p[] starting at p[1]
15690: so we point p on param[1][1] so that p[1] maps on param[1][1][1] */
15691: p=param[1][1]; /* *(*(*(param +1)+1)+0) */
15692:
15693: globpr=0; /* To get the number ipmx of contributions and the sum of weights*/
15694: /* For mortality only */
15695: if (mle==-3){
15696: ximort=matrix(1,NDIM,1,NDIM);
15697: for(i=1;i<=NDIM;i++)
15698: for(j=1;j<=NDIM;j++)
15699: ximort[i][j]=0.;
15700: /* ximort=gsl_matrix_alloc(1,NDIM,1,NDIM); */
15701: cens=ivector(firstobs,lastobs);
15702: ageexmed=vector(firstobs,lastobs);
15703: agecens=vector(firstobs,lastobs);
15704: dcwave=ivector(firstobs,lastobs);
15705:
15706: for (i=1; i<=imx; i++){
15707: dcwave[i]=-1;
15708: for (m=firstpass; m<=lastpass; m++)
15709: if (s[m][i]>nlstate) {
15710: dcwave[i]=m;
15711: /* printf("i=%d j=%d s=%d dcwave=%d\n",i,j, s[j][i],dcwave[i]);*/
15712: break;
15713: }
15714: }
15715:
15716: for (i=1; i<=imx; i++) {
15717: if (wav[i]>0){
15718: ageexmed[i]=agev[mw[1][i]][i];
15719: j=wav[i];
15720: agecens[i]=1.;
15721:
15722: if (ageexmed[i]> 1 && wav[i] > 0){
15723: agecens[i]=agev[mw[j][i]][i];
15724: cens[i]= 1;
15725: }else if (ageexmed[i]< 1)
15726: cens[i]= -1;
15727: if (agedc[i]< AGESUP && agedc[i]>1 && dcwave[i]>firstpass && dcwave[i]<=lastpass)
15728: cens[i]=0 ;
15729: }
15730: else cens[i]=-1;
15731: }
15732:
15733: for (i=1;i<=NDIM;i++) {
15734: for (j=1;j<=NDIM;j++)
15735: ximort[i][j]=(i == j ? 1.0 : 0.0);
15736: }
15737:
15738: p[1]=0.0268; p[NDIM]=0.083;
15739: /* printf("%lf %lf", p[1], p[2]); */
15740:
15741:
15742: #ifdef GSL
15743: printf("GSL optimization\n"); fprintf(ficlog,"Powell\n");
15744: #else
15745: printf("Powell-mort\n"); fprintf(ficlog,"Powell-mort\n");
15746: #endif
15747: strcpy(filerespow,"POW-MORT_");
15748: strcat(filerespow,fileresu);
15749: if((ficrespow=fopen(filerespow,"w"))==NULL) {
15750: printf("Problem with resultfile: %s\n", filerespow);
15751: fprintf(ficlog,"Problem with resultfile: %s\n", filerespow);
15752: }
15753: #ifdef GSL
15754: fprintf(ficrespow,"# GSL optimization\n# iter -2*LL");
15755: #else
15756: fprintf(ficrespow,"# Powell\n# iter -2*LL");
15757: #endif
15758: /* for (i=1;i<=nlstate;i++)
15759: for(j=1;j<=nlstate+ndeath;j++)
15760: if(j!=i)fprintf(ficrespow," p%1d%1d",i,j);
15761: */
15762: fprintf(ficrespow,"\n");
15763: #ifdef GSL
15764: /* gsl starts here */
15765: T = gsl_multimin_fminimizer_nmsimplex;
15766: gsl_multimin_fminimizer *sfm = NULL;
15767: gsl_vector *ss, *x;
15768: gsl_multimin_function minex_func;
15769:
15770: /* Initial vertex size vector */
15771: ss = gsl_vector_alloc (NDIM);
15772:
15773: if (ss == NULL){
15774: GSL_ERROR_VAL ("failed to allocate space for ss", GSL_ENOMEM, 0);
15775: }
15776: /* Set all step sizes to 1 */
15777: gsl_vector_set_all (ss, 0.001);
15778:
15779: /* Starting point */
15780:
15781: x = gsl_vector_alloc (NDIM);
15782:
15783: if (x == NULL){
15784: gsl_vector_free(ss);
15785: GSL_ERROR_VAL ("failed to allocate space for x", GSL_ENOMEM, 0);
15786: }
15787:
15788: /* Initialize method and iterate */
15789: /* p[1]=0.0268; p[NDIM]=0.083; */
15790: /* gsl_vector_set(x, 0, 0.0268); */
15791: /* gsl_vector_set(x, 1, 0.083); */
15792: gsl_vector_set(x, 0, p[1]);
15793: gsl_vector_set(x, 1, p[2]);
15794:
15795: minex_func.f = &gompertz_f;
15796: minex_func.n = NDIM;
15797: minex_func.params = (void *)&p; /* ??? */
15798:
15799: sfm = gsl_multimin_fminimizer_alloc (T, NDIM);
15800: gsl_multimin_fminimizer_set (sfm, &minex_func, x, ss);
15801:
15802: printf("Iterations beginning .....\n\n");
15803: printf("Iter. # Intercept Slope -Log Likelihood Simplex size\n");
15804:
15805: iteri=0;
15806: while (rval == GSL_CONTINUE){
15807: iteri++;
15808: status = gsl_multimin_fminimizer_iterate(sfm);
15809:
15810: if (status) printf("error: %s\n", gsl_strerror (status));
15811: fflush(0);
15812:
15813: if (status)
15814: break;
15815:
15816: rval = gsl_multimin_test_size (gsl_multimin_fminimizer_size (sfm), 1e-6);
15817: ssval = gsl_multimin_fminimizer_size (sfm);
15818:
15819: if (rval == GSL_SUCCESS)
15820: printf ("converged to a local maximum at\n");
15821:
15822: printf("%5d ", iteri);
15823: for (it = 0; it < NDIM; it++){
15824: printf ("%10.5f ", gsl_vector_get (sfm->x, it));
15825: }
15826: printf("f() = %-10.5f ssize = %.7f\n", sfm->fval, ssval);
15827: }
15828:
15829: printf("\n\n Please note: Program should be run many times with varying starting points to detemine global maximum\n\n");
15830:
15831: gsl_vector_free(x); /* initial values */
15832: gsl_vector_free(ss); /* inital step size */
15833: for (it=0; it<NDIM; it++){
15834: p[it+1]=gsl_vector_get(sfm->x,it);
15835: fprintf(ficrespow," %.12lf", p[it]);
15836: }
15837: gsl_multimin_fminimizer_free (sfm); /* p *(sfm.x.data) et p *(sfm.x.data+1) */
15838: #endif
15839: #ifdef POWELL
15840: #ifdef LINMINORIGINAL
15841: #else /* LINMINORIGINAL */
15842:
15843: flatdir=ivector(1,npar);
15844: for (j=1;j<=npar;j++) flatdir[j]=0;
15845: #endif /*LINMINORIGINAL */
15846: powell(p,ximort,NDIM,ftol,&iter,&fret,gompertz);
15847: #endif
15848: fclose(ficrespow);
15849: #ifdef LINMINORIGINAL
15850: #else
15851: free_ivector(flatdir,1,npar);
15852: #endif /* LINMINORIGINAL*/
15853:
15854: hesscov(matcov, hess, p, NDIM, delti, 1e-4, gompertz);
15855:
15856: for(i=1; i <=NDIM; i++)
15857: for(j=i+1;j<=NDIM;j++)
15858: matcov[i][j]=matcov[j][i];
15859:
15860: printf("\nCovariance matrix\n ");
15861: fprintf(ficlog,"\nCovariance matrix\n ");
15862: for(i=1; i <=NDIM; i++) {
15863: for(j=1;j<=NDIM;j++){
15864: printf("%f ",matcov[i][j]);
15865: fprintf(ficlog,"%f ",matcov[i][j]);
15866: }
15867: printf("\n "); fprintf(ficlog,"\n ");
15868: }
15869:
15870: printf("iter=%d MLE=%f Eq=%lf*exp(%lf*(age-%d))\n",iter,-gompertz(p),p[1],p[2],agegomp);
15871: for (i=1;i<=NDIM;i++) {
15872: printf("%f [%f ; %f]\n",p[i],p[i]-2*sqrt(matcov[i][i]),p[i]+2*sqrt(matcov[i][i]));
15873: fprintf(ficlog,"%f [%f ; %f]\n",p[i],p[i]-2*sqrt(matcov[i][i]),p[i]+2*sqrt(matcov[i][i]));
15874: }
15875: lsurv=vector(agegomp,AGESUP);
15876: lpop=vector(agegomp,AGESUP);
15877: tpop=vector(agegomp,AGESUP);
15878: lsurv[agegomp]=100000;
15879:
15880: for (k=agegomp;k<=AGESUP;k++) {
15881: agemortsup=k;
15882: if (p[1]*exp(p[2]*(k-agegomp))>1) break;
15883: }
15884:
15885: for (k=agegomp;k<agemortsup;k++)
15886: lsurv[k+1]=lsurv[k]-lsurv[k]*(p[1]*exp(p[2]*(k-agegomp)));
15887:
15888: for (k=agegomp;k<agemortsup;k++){
15889: lpop[k]=(lsurv[k]+lsurv[k+1])/2.;
15890: sumlpop=sumlpop+lpop[k];
15891: }
15892:
15893: tpop[agegomp]=sumlpop;
15894: for (k=agegomp;k<(agemortsup-3);k++){
15895: /* tpop[k+1]=2;*/
15896: tpop[k+1]=tpop[k]-lpop[k];
15897: }
15898:
15899:
15900: printf("\nAge lx qx dx Lx Tx e(x)\n");
15901: for (k=agegomp;k<(agemortsup-2);k++)
15902: 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]);
15903:
15904:
15905: replace_back_to_slash(pathc,pathcd); /* Even gnuplot wants a / */
15906: ageminpar=50;
15907: agemaxpar=100;
15908: if(ageminpar == AGEOVERFLOW ||agemaxpar == AGEOVERFLOW){
15909: printf("Warning! Error in gnuplot file with ageminpar %f or agemaxpar %f overflow\n\
15910: This is probably because your parameter file doesn't \n contain the exact number of lines (or columns) corresponding to your model line.\n\
15911: Please run with mle=-1 to get a correct covariance matrix.\n",ageminpar,agemaxpar);
15912: fprintf(ficlog,"Warning! Error in gnuplot file with ageminpar %f or agemaxpar %f overflow\n\
15913: This is probably because your parameter file doesn't \n contain the exact number of lines (or columns) corresponding to your model line.\n\
15914: Please run with mle=-1 to get a correct covariance matrix.\n",ageminpar,agemaxpar);
15915: }else{
15916: printf("Warning! ageminpar %f and agemaxpar %f have been fixed because for simplification until it is fixed...\n\n",ageminpar,agemaxpar);
15917: fprintf(ficlog,"Warning! ageminpar %f and agemaxpar %f have been fixed because for simplification until it is fixed...\n\n",ageminpar,agemaxpar);
15918: printinggnuplotmort(fileresu, optionfilefiname,ageminpar,agemaxpar,fage, pathc,p);
15919: }
15920: printinghtmlmort(fileresu,title,datafile, firstpass, lastpass, \
15921: stepm, weightopt,\
15922: model,imx,p,matcov,agemortsup);
15923:
15924: free_vector(lsurv,agegomp,AGESUP);
15925: free_vector(lpop,agegomp,AGESUP);
15926: free_vector(tpop,agegomp,AGESUP);
15927: free_matrix(ximort,1,NDIM,1,NDIM);
15928: free_ivector(dcwave,firstobs,lastobs);
15929: free_vector(agecens,firstobs,lastobs);
15930: free_vector(ageexmed,firstobs,lastobs);
15931: free_ivector(cens,firstobs,lastobs);
15932: #ifdef GSL
15933: #endif
15934: } /* Endof if mle==-3 mortality only */
15935: /* Standard */
15936: else{ /* For mle !=- 3, could be 0 or 1 or 4 etc. */
15937: globpr=0;/* Computes sum of likelihood for globpr=1 and funcone */
15938: /* Computes likelihood for initial parameters, uses funcone to compute gpimx and gsw */
15939: likelione(ficres, p, npar, nlstate, &globpr, &ipmx, &sw, &fretone, funcone); /* Prints the contributions to the likelihood */
15940: printf("First Likeli=%12.6f ipmx=%ld sw=%12.6f",fretone,ipmx,sw);
15941: for (k=1; k<=npar;k++)
15942: printf(" %d %8.5f",k,p[k]);
15943: printf("\n");
15944: if(mle>=1){ /* Could be 1 or 2, Real Maximization */
15945: /* mlikeli uses func not funcone */
15946: /* for(i=1;i<nlstate;i++){ */
15947: /* /\*reducing xi for 1 to npar to 1 to ncovmodel; *\/ */
15948: /* mlikeli(ficres,p, ncovmodel, ncovmodel, nlstate, ftol, funcnoprod); */
15949: /* } */
15950: mlikeli(ficres,p, npar, ncovmodel, nlstate, ftol, func);
15951: }
15952: if(mle==0) {/* No optimization, will print the likelihoods for the datafile */
15953: globpr=0;/* Computes sum of likelihood for globpr=1 and funcone */
15954: /* Computes likelihood for initial parameters, uses funcone to compute gpimx and gsw */
15955: likelione(ficres, p, npar, nlstate, &globpr, &ipmx, &sw, &fretone, funcone); /* Prints the contributions to the likelihood */
15956: }
15957: globpr=1; /* again, to print the individual contributions using computed gpimx and gsw */
15958: likelione(ficres, p, npar, nlstate, &globpr, &ipmx, &sw, &fretone, funcone); /* Prints the contributions to the likelihood */
15959: printf("Second Likeli=%12.6f ipmx=%ld sw=%12.6f",fretone,ipmx,sw);
15960: /* exit(0); */
15961: for (k=1; k<=npar;k++)
15962: printf(" %d %8.5f",k,p[k]);
15963: printf("\n");
15964:
15965: /*--------- results files --------------*/
15966: /* 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); */
15967:
15968:
15969: fprintf(ficres,"# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n");
15970: printf("# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n"); /* Printing model equation */
15971: fprintf(ficlog,"# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n");
15972:
15973: printf("#model= 1 + age ");
15974: fprintf(ficres,"#model= 1 + age ");
15975: fprintf(ficlog,"#model= 1 + age ");
15976: fprintf(fichtm,"\n<ul><li> model=1+age+%s\n \
15977: </ul>", model);
15978:
15979: fprintf(fichtm,"\n<table style=\"text-align:center; border: 1px solid\">\n");
15980: fprintf(fichtm, "<tr><th>Model=</th><th>1</th><th>+ age</th>");
15981: if(nagesqr==1){
15982: printf(" + age*age ");
15983: fprintf(ficres," + age*age ");
15984: fprintf(ficlog," + age*age ");
15985: fprintf(fichtm, "<th>+ age*age</th>");
15986: }
15987: for(j=1;j <=ncovmodel-2;j++){
15988: if(Typevar[j]==0) {
15989: printf(" + V%d ",Tvar[j]);
15990: fprintf(ficres," + V%d ",Tvar[j]);
15991: fprintf(ficlog," + V%d ",Tvar[j]);
15992: fprintf(fichtm, "<th>+ V%d</th>",Tvar[j]);
15993: }else if(Typevar[j]==1) {
15994: printf(" + V%d*age ",Tvar[j]);
15995: fprintf(ficres," + V%d*age ",Tvar[j]);
15996: fprintf(ficlog," + V%d*age ",Tvar[j]);
15997: fprintf(fichtm, "<th>+ V%d*age</th>",Tvar[j]);
15998: }else if(Typevar[j]==2) {
15999: printf(" + V%d*V%d ",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
16000: fprintf(ficres," + V%d*V%d ",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
16001: fprintf(ficlog," + V%d*V%d ",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
16002: fprintf(fichtm, "<th>+ V%d*V%d</th>",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
16003: }else if(Typevar[j]==3) { /* TO VERIFY */
16004: printf(" + V%d*V%d*age ",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
16005: fprintf(ficres," + V%d*V%d*age ",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
16006: fprintf(ficlog," + V%d*V%d*age ",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
16007: fprintf(fichtm, "<th>+ V%d*V%d*age</th>",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
16008: }
16009: }
16010: printf("\n");
16011: fprintf(ficres,"\n");
16012: fprintf(ficlog,"\n");
16013: fprintf(fichtm, "</tr>");
16014: fprintf(fichtm, "\n");
16015:
16016:
16017: for(i=1,jk=1; i <=nlstate; i++){
16018: for(k=1; k <=(nlstate+ndeath); k++){
16019: if (k != i) {
16020: fprintf(fichtm, "<tr>");
16021: printf("%d%d ",i,k);
16022: fprintf(ficlog,"%d%d ",i,k);
16023: fprintf(ficres,"%1d%1d ",i,k);
16024: fprintf(fichtm, "<td>%1d%1d</td>",i,k);
16025: for(j=1; j <=ncovmodel; j++){
16026: printf("%12.7f ",p[jk]);
16027: fprintf(ficlog,"%12.7f ",p[jk]);
16028: fprintf(ficres,"%12.7f ",p[jk]);
16029: fprintf(fichtm, "<td>%12.7f</td>",p[jk]);
16030: jk++;
16031: }
16032: printf("\n");
16033: fprintf(ficlog,"\n");
16034: fprintf(ficres,"\n");
16035: fprintf(fichtm, "</tr>\n");
16036: }
16037: }
16038: }
16039: /* fprintf(fichtm,"</tr>\n"); */
16040: fprintf(fichtm,"</table>\n");
16041: fprintf(fichtm, "\n");
16042:
16043: if(mle != 0){
16044: /* Computing hessian and covariance matrix only at a peak of the Likelihood, that is after optimization */
16045: ftolhess=ftol; /* Usually correct */
16046: hesscov(matcov, hess, p, npar, delti, ftolhess, func);
16047: 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");
16048: 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");
16049: 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);
16050: fprintf(fichtm,"\n<table style=\"text-align:center; border: 1px solid\">");
16051: fprintf(fichtm, "\n<tr><th>Model=</th><th>1</th><th>+ age</th>");
16052: if(nagesqr==1){
16053: printf(" + age*age ");
16054: fprintf(ficres," + age*age ");
16055: fprintf(ficlog," + age*age ");
16056: fprintf(fichtm, "<th>+ age*age</th>");
16057: }
16058: for(j=1;j <=ncovmodel-2;j++){
16059: if(Typevar[j]==0) {
16060: printf(" + V%d ",Tvar[j]);
16061: fprintf(fichtm, "<th>+ V%d</th>",Tvar[j]);
16062: }else if(Typevar[j]==1) {
16063: printf(" + V%d*age ",Tvar[j]);
16064: fprintf(fichtm, "<th>+ V%d*age</th>",Tvar[j]);
16065: }else if(Typevar[j]==2) {
16066: fprintf(fichtm, "<th>+ V%d*V%d</th>",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
16067: }else if(Typevar[j]==3) { /* TO VERIFY */
16068: fprintf(fichtm, "<th>+ V%d*V%d*age</th>",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
16069: }
16070: }
16071: fprintf(fichtm, "</tr>\n");
16072:
16073: for(i=1,jk=1; i <=nlstate; i++){
16074: for(k=1; k <=(nlstate+ndeath); k++){
16075: if (k != i) {
16076: fprintf(fichtm, "<tr valign=top>");
16077: printf("%d%d ",i,k);
16078: fprintf(ficlog,"%d%d ",i,k);
16079: fprintf(fichtm, "<td>%1d%1d</td>",i,k);
16080: for(j=1; j <=ncovmodel; j++){
16081: wald=p[jk]/sqrt(matcov[jk][jk]);
16082: 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]));
16083: 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]));
16084: if(fabs(wald) > 1.96){
16085: fprintf(fichtm, "<td><b>%12.7f</b></br> (%12.7f)</br>",p[jk],sqrt(matcov[jk][jk]));
16086: }else{
16087: fprintf(fichtm, "<td>%12.7f (%12.7f)</br>",p[jk],sqrt(matcov[jk][jk]));
16088: }
16089: fprintf(fichtm,"W=%8.3f</br>",wald);
16090: fprintf(fichtm,"[%12.7f;%12.7f]</br></td>", p[jk]-1.96*sqrt(matcov[jk][jk]),p[jk]+1.96*sqrt(matcov[jk][jk]));
16091: jk++;
16092: }
16093: printf("\n");
16094: fprintf(ficlog,"\n");
16095: fprintf(fichtm, "</tr>\n");
16096: }
16097: }
16098: }
16099: } /* end of hesscov and Wald tests */
16100: fprintf(fichtm,"</table>\n");
16101:
16102: /* */
16103: fprintf(ficres,"# Scales (for hessian or gradient estimation)\n");
16104: printf("# Scales (for hessian or gradient estimation)\n");
16105: fprintf(ficlog,"# Scales (for hessian or gradient estimation)\n");
16106: for(i=1,jk=1; i <=nlstate; i++){
16107: for(j=1; j <=nlstate+ndeath; j++){
16108: if (j!=i) {
16109: fprintf(ficres,"%1d%1d",i,j);
16110: printf("%1d%1d",i,j);
16111: fprintf(ficlog,"%1d%1d",i,j);
16112: for(k=1; k<=ncovmodel;k++){
16113: printf(" %.5e",delti[jk]);
16114: fprintf(ficlog," %.5e",delti[jk]);
16115: fprintf(ficres," %.5e",delti[jk]);
16116: jk++;
16117: }
16118: printf("\n");
16119: fprintf(ficlog,"\n");
16120: fprintf(ficres,"\n");
16121: }
16122: }
16123: }
16124:
16125: 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");
16126: if(mle >= 1) /* Too big for the screen */
16127: 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");
16128: 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");
16129: /* # 121 Var(a12)\n\ */
16130: /* # 122 Cov(b12,a12) Var(b12)\n\ */
16131: /* # 131 Cov(a13,a12) Cov(a13,b12, Var(a13)\n\ */
16132: /* # 132 Cov(b13,a12) Cov(b13,b12, Cov(b13,a13) Var(b13)\n\ */
16133: /* # 212 Cov(a21,a12) Cov(a21,b12, Cov(a21,a13) Cov(a21,b13) Var(a21)\n\ */
16134: /* # 212 Cov(b21,a12) Cov(b21,b12, Cov(b21,a13) Cov(b21,b13) Cov(b21,a21) Var(b21)\n\ */
16135: /* # 232 Cov(a23,a12) Cov(a23,b12, Cov(a23,a13) Cov(a23,b13) Cov(a23,a21) Cov(a23,b21) Var(a23)\n\ */
16136: /* # 232 Cov(b23,a12) Cov(b23,b12) ... Var (b23)\n" */
16137:
16138:
16139: /* Just to have a covariance matrix which will be more understandable
16140: even is we still don't want to manage dictionary of variables
16141: */
16142: for(itimes=1;itimes<=2;itimes++){
16143: jj=0;
16144: for(i=1; i <=nlstate; i++){
16145: for(j=1; j <=nlstate+ndeath; j++){
16146: if(j==i) continue;
16147: for(k=1; k<=ncovmodel;k++){
16148: jj++;
16149: ca[0]= k+'a'-1;ca[1]='\0';
16150: if(itimes==1){
16151: if(mle>=1)
16152: printf("#%1d%1d%d",i,j,k);
16153: fprintf(ficlog,"#%1d%1d%d",i,j,k);
16154: fprintf(ficres,"#%1d%1d%d",i,j,k);
16155: }else{
16156: if(mle>=1)
16157: printf("%1d%1d%d",i,j,k);
16158: fprintf(ficlog,"%1d%1d%d",i,j,k);
16159: fprintf(ficres,"%1d%1d%d",i,j,k);
16160: }
16161: ll=0;
16162: for(li=1;li <=nlstate; li++){
16163: for(lj=1;lj <=nlstate+ndeath; lj++){
16164: if(lj==li) continue;
16165: for(lk=1;lk<=ncovmodel;lk++){
16166: ll++;
16167: if(ll<=jj){
16168: cb[0]= lk +'a'-1;cb[1]='\0';
16169: if(ll<jj){
16170: if(itimes==1){
16171: if(mle>=1)
16172: printf(" Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj);
16173: fprintf(ficlog," Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj);
16174: fprintf(ficres," Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj);
16175: }else{
16176: if(mle>=1)
16177: printf(" %.5e",matcov[jj][ll]);
16178: fprintf(ficlog," %.5e",matcov[jj][ll]);
16179: fprintf(ficres," %.5e",matcov[jj][ll]);
16180: }
16181: }else{
16182: if(itimes==1){
16183: if(mle>=1)
16184: printf(" Var(%s%1d%1d)",ca,i,j);
16185: fprintf(ficlog," Var(%s%1d%1d)",ca,i,j);
16186: fprintf(ficres," Var(%s%1d%1d)",ca,i,j);
16187: }else{
16188: if(mle>=1)
16189: printf(" %.7e",matcov[jj][ll]);
16190: fprintf(ficlog," %.7e",matcov[jj][ll]);
16191: fprintf(ficres," %.7e",matcov[jj][ll]);
16192: }
16193: }
16194: }
16195: } /* end lk */
16196: } /* end lj */
16197: } /* end li */
16198: if(mle>=1)
16199: printf("\n");
16200: fprintf(ficlog,"\n");
16201: fprintf(ficres,"\n");
16202: numlinepar++;
16203: } /* end k*/
16204: } /*end j */
16205: } /* end i */
16206: } /* end itimes */
16207:
16208: fflush(ficlog);
16209: fflush(ficres);
16210: while(fgets(line, MAXLINE, ficpar)) {
16211: /* If line starts with a # it is a comment */
16212: if (line[0] == '#') {
16213: numlinepar++;
16214: fputs(line,stdout);
16215: fputs(line,ficparo);
16216: fputs(line,ficlog);
16217: fputs(line,ficres);
16218: continue;
16219: }else
16220: break;
16221: }
16222:
16223: /* while((c=getc(ficpar))=='#' && c!= EOF){ */
16224: /* ungetc(c,ficpar); */
16225: /* fgets(line, MAXLINE, ficpar); */
16226: /* fputs(line,stdout); */
16227: /* fputs(line,ficparo); */
16228: /* } */
16229: /* ungetc(c,ficpar); */
16230:
16231: estepm=0;
16232: 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){
16233:
16234: if (num_filled != 6) {
16235: 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);
16236: 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);
16237: goto end;
16238: }
16239: printf("agemin=%lf agemax=%lf bage=%lf fage=%lf estepm=%d ftolpl=%lf\n",ageminpar,agemaxpar, bage, fage, estepm, ftolpl);
16240: }
16241: /* ftolpl=6*ftol*1.e5; /\* 6.e-3 make convergences in less than 80 loops for the prevalence limit *\/ */
16242: /*ftolpl=6.e-4;*/ /* 6.e-3 make convergences in less than 80 loops for the prevalence limit */
16243:
16244: /* fscanf(ficpar,"agemin=%lf agemax=%lf bage=%lf fage=%lf estepm=%d ftolpl=%\n",&ageminpar,&agemaxpar, &bage, &fage, &estepm); */
16245: if (estepm==0 || estepm < stepm) estepm=stepm;
16246: if (fage <= 2) {
16247: bage = ageminpar;
16248: fage = agemaxpar;
16249: }
16250:
16251: fprintf(ficres,"# agemin agemax for life expectancy, bage fage (if mle==0 ie no data nor Max likelihood).\n");
16252: fprintf(ficres,"agemin=%.0f agemax=%.0f bage=%.0f fage=%.0f estepm=%d ftolpl=%e\n",ageminpar,agemaxpar,bage,fage, estepm, ftolpl);
16253: fprintf(ficparo,"agemin=%.0f agemax=%.0f bage=%.0f fage=%.0f estepm=%d, ftolpl=%e\n",ageminpar,agemaxpar,bage,fage, estepm, ftolpl);
16254:
16255: /* Other stuffs, more or less useful */
16256: while(fgets(line, MAXLINE, ficpar)) {
16257: /* If line starts with a # it is a comment */
16258: if (line[0] == '#') {
16259: numlinepar++;
16260: fputs(line,stdout);
16261: fputs(line,ficparo);
16262: fputs(line,ficlog);
16263: fputs(line,ficres);
16264: continue;
16265: }else
16266: break;
16267: }
16268:
16269: 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){
16270:
16271: if (num_filled != 7) {
16272: 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);
16273: 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);
16274: goto end;
16275: }
16276: printf("begin-prev-date=%.lf/%.lf/%.lf end-prev-date=%.lf/%.lf/%.lf mov_average=%d\n",jprev1, mprev1,anprev1,jprev2, mprev2,anprev2,mobilav);
16277: 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);
16278: 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);
16279: 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);
16280: }
16281:
16282: while(fgets(line, MAXLINE, ficpar)) {
16283: /* If line starts with a # it is a comment */
16284: if (line[0] == '#') {
16285: numlinepar++;
16286: fputs(line,stdout);
16287: fputs(line,ficparo);
16288: fputs(line,ficlog);
16289: fputs(line,ficres);
16290: continue;
16291: }else
16292: break;
16293: }
16294:
16295:
16296: dateprev1=anprev1+(mprev1-1)/12.+(jprev1-1)/365.;
16297: dateprev2=anprev2+(mprev2-1)/12.+(jprev2-1)/365.;
16298:
16299: if((num_filled=sscanf(line,"pop_based=%d\n",&popbased)) !=EOF){
16300: if (num_filled != 1) {
16301: 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);
16302: 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);
16303: goto end;
16304: }
16305: printf("pop_based=%d\n",popbased);
16306: fprintf(ficlog,"pop_based=%d\n",popbased);
16307: fprintf(ficparo,"pop_based=%d\n",popbased);
16308: fprintf(ficres,"pop_based=%d\n",popbased);
16309: }
16310:
16311: /* Results */
16312: /* Value of covariate in each resultine will be computed (if product) and sorted according to model rank */
16313: /* It is precov[] because we need the varying age in order to compute the real cov[] of the model equation */
16314: precov=matrix(1,MAXRESULTLINESPONE,1,NCOVMAX+1);
16315: endishere=0;
16316: nresult=0;
16317: parameterline=0;
16318: do{
16319: if(!fgets(line, MAXLINE, ficpar)){
16320: endishere=1;
16321: parameterline=15;
16322: }else if (line[0] == '#') {
16323: /* If line starts with a # it is a comment */
16324: numlinepar++;
16325: fputs(line,stdout);
16326: fputs(line,ficparo);
16327: fputs(line,ficlog);
16328: fputs(line,ficres);
16329: continue;
16330: }else if(sscanf(line,"prevforecast=%[^\n]\n",modeltemp))
16331: parameterline=11;
16332: else if(sscanf(line,"prevbackcast=%[^\n]\n",modeltemp))
16333: parameterline=12;
16334: else if(sscanf(line,"result:%[^\n]\n",modeltemp)){
16335: parameterline=13;
16336: }
16337: else{
16338: parameterline=14;
16339: }
16340: switch (parameterline){ /* =0 only if only comments */
16341: case 11:
16342: 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)){
16343: 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);
16344: 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);
16345: 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);
16346: 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);
16347: /* day and month of proj2 are not used but only year anproj2.*/
16348: dateproj1=anproj1+(mproj1-1)/12.+(jproj1-1)/365.;
16349: dateproj2=anproj2+(mproj2-1)/12.+(jproj2-1)/365.;
16350: prvforecast = 1;
16351: }
16352: else if((num_filled=sscanf(line,"prevforecast=%d yearsfproj=%lf mobil_average=%d\n",&prevfcast,&yrfproj,&mobilavproj)) !=EOF){/* && (num_filled == 3))*/
16353: printf("prevforecast=%d yearsfproj=%.2lf mobil_average=%d\n",prevfcast,yrfproj,mobilavproj);
16354: fprintf(ficlog,"prevforecast=%d yearsfproj=%.2lf mobil_average=%d\n",prevfcast,yrfproj,mobilavproj);
16355: fprintf(ficres,"prevforecast=%d yearsfproj=%.2lf mobil_average=%d\n",prevfcast,yrfproj,mobilavproj);
16356: prvforecast = 2;
16357: }
16358: else {
16359: 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);
16360: 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);
16361: goto end;
16362: }
16363: break;
16364: case 12:
16365: 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)){
16366: 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);
16367: 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);
16368: 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);
16369: 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);
16370: /* day and month of back2 are not used but only year anback2.*/
16371: dateback1=anback1+(mback1-1)/12.+(jback1-1)/365.;
16372: dateback2=anback2+(mback2-1)/12.+(jback2-1)/365.;
16373: prvbackcast = 1;
16374: }
16375: else if((num_filled=sscanf(line,"prevbackcast=%d yearsbproj=%lf mobil_average=%d\n",&prevbcast,&yrbproj,&mobilavproj)) ==3){/* && (num_filled == 3))*/
16376: printf("prevbackcast=%d yearsbproj=%.2lf mobil_average=%d\n",prevbcast,yrbproj,mobilavproj);
16377: fprintf(ficlog,"prevbackcast=%d yearsbproj=%.2lf mobil_average=%d\n",prevbcast,yrbproj,mobilavproj);
16378: fprintf(ficres,"prevbackcast=%d yearsbproj=%.2lf mobil_average=%d\n",prevbcast,yrbproj,mobilavproj);
16379: prvbackcast = 2;
16380: }
16381: else {
16382: 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);
16383: 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);
16384: goto end;
16385: }
16386: break;
16387: case 13:
16388: num_filled=sscanf(line,"result:%[^\n]\n",resultlineori);
16389: nresult++; /* Sum of resultlines */
16390: /* printf("Result %d: result:%s\n",nresult, resultlineori); */
16391: /* removefirstspace(&resultlineori); */
16392:
16393: if(strstr(resultlineori,"v") !=0){
16394: printf("Error. 'v' must be in upper case 'V' result: %s ",resultlineori);
16395: fprintf(ficlog,"Error. 'v' must be in upper case result: %s ",resultlineori);fflush(ficlog);
16396: return 1;
16397: }
16398: trimbb(resultline, resultlineori); /* Suppressing double blank in the resultline */
16399: /* printf("Decoderesult resultline=\"%s\" resultlineori=\"%s\"\n", resultline, resultlineori); */
16400: if(nresult > MAXRESULTLINESPONE-1){
16401: 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);
16402: 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);
16403: goto end;
16404: }
16405:
16406: if(!decoderesult(resultline, nresult)){ /* Fills TKresult[nresult] combination and Tresult[nresult][k4+1] combination values */
16407: fprintf(ficparo,"result: %s\n",resultline);
16408: fprintf(ficres,"result: %s\n",resultline);
16409: fprintf(ficlog,"result: %s\n",resultline);
16410: } else
16411: goto end;
16412: break;
16413: case 14:
16414: printf("Error: Unknown command '%s'\n",line);
16415: fprintf(ficlog,"Error: Unknown command '%s'\n",line);
16416: if(line[0] == ' ' || line[0] == '\n'){
16417: printf("It should not be an empty line '%s'\n",line);
16418: fprintf(ficlog,"It should not be an empty line '%s'\n",line);
16419: }
16420: if(ncovmodel >=2 && nresult==0 ){
16421: printf("ERROR: no result lines! It should be at minimum 'result: V2=0 V1=1 or result:.' %s\n",line);
16422: fprintf(ficlog,"ERROR: no result lines! It should be at minimum 'result: V2=0 V1=1 or result:.' %s\n",line);
16423: }
16424: /* goto end; */
16425: break;
16426: case 15:
16427: printf("End of resultlines.\n");
16428: fprintf(ficlog,"End of resultlines.\n");
16429: break;
16430: default: /* parameterline =0 */
16431: nresult=1;
16432: decoderesult(".",nresult ); /* No covariate */
16433: } /* End switch parameterline */
16434: }while(endishere==0); /* End do */
16435:
16436: /* freqsummary(fileres, agemin, agemax, s, agev, nlstate, imx,Tvaraff,nbcode, ncodemax,mint,anint); */
16437: /* ,dateprev1,dateprev2,jprev1, mprev1,anprev1,jprev2, mprev2,anprev2); */
16438:
16439: replace_back_to_slash(pathc,pathcd); /* Even gnuplot wants a / */
16440: if(ageminpar == AGEOVERFLOW ||agemaxpar == -AGEOVERFLOW){
16441: printf("Warning! Error in gnuplot file with ageminpar %f or agemaxpar %f overflow\n\
16442: This is probably because your parameter file doesn't \n contain the exact number of lines (or columns) corresponding to your model line.\n\
16443: Please run with mle=-1 to get a correct covariance matrix.\n",ageminpar,agemaxpar);
16444: fprintf(ficlog,"Warning! Error in gnuplot file with ageminpar %f or agemaxpar %f overflow\n\
16445: This is probably because your parameter file doesn't \n contain the exact number of lines (or columns) corresponding to your model line.\n\
16446: Please run with mle=-1 to get a correct covariance matrix.\n",ageminpar,agemaxpar);
16447: }else{
16448: /* printinggnuplot(fileresu, optionfilefiname,ageminpar,agemaxpar,fage, prevfcast, backcast, pathc,p, (int)anproj1-(int)agemin, (int)anback1-(int)agemax+1); */
16449: /* It seems that anprojd which is computed from the mean year at interview which is known yet because of freqsummary */
16450: /* date2dmy(dateintmean,&jintmean,&mintmean,&aintmean); */ /* Done in freqsummary */
16451: if(prvforecast==1){
16452: dateprojd=(jproj1+12*mproj1+365*anproj1)/365;
16453: jprojd=jproj1;
16454: mprojd=mproj1;
16455: anprojd=anproj1;
16456: dateprojf=(jproj2+12*mproj2+365*anproj2)/365;
16457: jprojf=jproj2;
16458: mprojf=mproj2;
16459: anprojf=anproj2;
16460: } else if(prvforecast == 2){
16461: dateprojd=dateintmean;
16462: date2dmy(dateprojd,&jprojd, &mprojd, &anprojd);
16463: dateprojf=dateintmean+yrfproj;
16464: date2dmy(dateprojf,&jprojf, &mprojf, &anprojf);
16465: }
16466: if(prvbackcast==1){
16467: datebackd=(jback1+12*mback1+365*anback1)/365;
16468: jbackd=jback1;
16469: mbackd=mback1;
16470: anbackd=anback1;
16471: datebackf=(jback2+12*mback2+365*anback2)/365;
16472: jbackf=jback2;
16473: mbackf=mback2;
16474: anbackf=anback2;
16475: } else if(prvbackcast == 2){
16476: datebackd=dateintmean;
16477: date2dmy(datebackd,&jbackd, &mbackd, &anbackd);
16478: datebackf=dateintmean-yrbproj;
16479: date2dmy(datebackf,&jbackf, &mbackf, &anbackf);
16480: }
16481:
16482: printinggnuplot(fileresu, optionfilefiname,ageminpar,agemaxpar,bage, fage, prevfcast, prevbcast, pathc,p, (int)anprojd-bage, (int)anbackd-fage);/* HERE valgrind Tvard*/
16483: }
16484: printinghtml(fileresu,title,datafile, firstpass, lastpass, stepm, weightopt, \
16485: model,imx,jmin,jmax,jmean,rfileres,popforecast,mobilav,prevfcast,mobilavproj,prevbcast, estepm, \
16486: jprev1,mprev1,anprev1,dateprev1, dateprojd, datebackd,jprev2,mprev2,anprev2,dateprev2,dateprojf, datebackf);
16487:
16488: /*------------ free_vector -------------*/
16489: /* chdir(path); */
16490:
16491: /* free_ivector(wav,1,imx); */ /* Moved after last prevalence call */
16492: /* free_imatrix(dh,1,lastpass-firstpass+2,1,imx); */
16493: /* free_imatrix(bh,1,lastpass-firstpass+2,1,imx); */
16494: /* free_imatrix(mw,1,lastpass-firstpass+2,1,imx); */
16495: free_lvector(num,firstobs,lastobs);
16496: free_vector(agedc,firstobs,lastobs);
16497: /*free_matrix(covar,0,NCOVMAX,1,n);*/
16498: /*free_matrix(covar,1,NCOVMAX,1,n);*/
16499: fclose(ficparo);
16500: fclose(ficres);
16501:
16502:
16503: /* Other results (useful)*/
16504:
16505:
16506: /*--------------- Prevalence limit (period or stable prevalence) --------------*/
16507: /*#include "prevlim.h"*/ /* Use ficrespl, ficlog */
16508: prlim=matrix(1,nlstate,1,nlstate);
16509: /* Computes the prevalence limit for each combination k of the dummy covariates by calling prevalim(k) */
16510: prevalence_limit(p, prlim, ageminpar, agemaxpar, ftolpl, &ncvyear);
16511: fclose(ficrespl);
16512:
16513: /*------------- h Pij x at various ages ------------*/
16514: /*#include "hpijx.h"*/
16515: /** 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?*/
16516: /* calls hpxij with combination k */
16517: hPijx(p, bage, fage);
16518: fclose(ficrespij);
16519:
16520: /* ncovcombmax= pow(2,cptcoveff); */
16521: /*-------------- Variance of one-step probabilities for a combination ij or for nres ?---*/
16522: k=1;
16523: varprob(optionfilefiname, matcov, p, delti, nlstate, bage, fage,k,Tvar,nbcode, ncodemax,strstart);
16524:
16525: /* Prevalence for each covariate combination in probs[age][status][cov] */
16526: probs= ma3x(AGEINF,AGESUP,1,nlstate+ndeath, 1,ncovcombmax);
16527: for(i=AGEINF;i<=AGESUP;i++)
16528: for(j=1;j<=nlstate+ndeath;j++) /* ndeath is useless but a necessity to be compared with mobaverages */
16529: for(k=1;k<=ncovcombmax;k++)
16530: probs[i][j][k]=0.;
16531: prevalence(probs, ageminpar, agemaxpar, s, agev, nlstate, imx, Tvar, nbcode,
16532: ncodemax, mint, anint, dateprev1, dateprev2, firstpass, lastpass);
16533: if (mobilav!=0 ||mobilavproj !=0 ) {
16534: mobaverages= ma3x(AGEINF, AGESUP,1,nlstate+ndeath, 1,ncovcombmax);
16535: for(i=AGEINF;i<=AGESUP;i++)
16536: for(j=1;j<=nlstate+ndeath;j++)
16537: for(k=1;k<=ncovcombmax;k++)
16538: mobaverages[i][j][k]=0.;
16539: mobaverage=mobaverages;
16540: if (mobilav!=0) {
16541: printf("Movingaveraging observed prevalence\n");
16542: fprintf(ficlog,"Movingaveraging observed prevalence\n");
16543: if (movingaverage(probs, ageminpar, agemaxpar, mobaverage, mobilav)!=0){
16544: fprintf(ficlog," Error in movingaverage mobilav=%d\n",mobilav);
16545: printf(" Error in movingaverage mobilav=%d\n",mobilav);
16546: }
16547: } else if (mobilavproj !=0) {
16548: printf("Movingaveraging projected observed prevalence\n");
16549: fprintf(ficlog,"Movingaveraging projected observed prevalence\n");
16550: if (movingaverage(probs, ageminpar, agemaxpar, mobaverage, mobilavproj)!=0){
16551: fprintf(ficlog," Error in movingaverage mobilavproj=%d\n",mobilavproj);
16552: printf(" Error in movingaverage mobilavproj=%d\n",mobilavproj);
16553: }
16554: }else{
16555: printf("Internal error moving average\n");
16556: fflush(stdout);
16557: exit(1);
16558: }
16559: }/* end if moving average */
16560:
16561: /*---------- Forecasting ------------------*/
16562: if(prevfcast==1){
16563: /* /\* if(stepm ==1){*\/ */
16564: /* /\* anproj1, mproj1, jproj1 either read explicitly or yrfproj *\/ */
16565: /*This done previously after freqsummary.*/
16566: /* dateprojd=(jproj1+12*mproj1+365*anproj1)/365; */
16567: /* dateprojf=(jproj2+12*mproj2+365*anproj2)/365; */
16568:
16569: /* } else if (prvforecast==2){ */
16570: /* /\* if(stepm ==1){*\/ */
16571: /* /\* anproj1, mproj1, jproj1 either read explicitly or yrfproj *\/ */
16572: /* } */
16573: /*prevforecast(fileresu, dateintmean, anproj1, mproj1, jproj1, agemin, agemax, dateprev1, dateprev2, mobilavproj, mobaverage, bage, fage, firstpass, lastpass, anproj2, p, cptcoveff);*/
16574: prevforecast(fileresu,dateintmean, dateprojd, dateprojf, agemin, agemax, dateprev1, dateprev2, mobilavproj, mobaverage, bage, fage, firstpass, lastpass, p, cptcoveff);
16575: }
16576:
16577: /* Prevbcasting */
16578: if(prevbcast==1){
16579: ddnewms=matrix(1,nlstate+ndeath,1,nlstate+ndeath);
16580: ddoldms=matrix(1,nlstate+ndeath,1,nlstate+ndeath);
16581: ddsavms=matrix(1,nlstate+ndeath,1,nlstate+ndeath);
16582:
16583: /*--------------- Back Prevalence limit (period or stable prevalence) --------------*/
16584:
16585: bprlim=matrix(1,nlstate,1,nlstate);
16586:
16587: back_prevalence_limit(p, bprlim, ageminpar, agemaxpar, ftolpl, &ncvyear, dateprev1, dateprev2, firstpass, lastpass, mobilavproj);
16588: fclose(ficresplb);
16589:
16590: hBijx(p, bage, fage, mobaverage);
16591: fclose(ficrespijb);
16592:
16593: /* /\* prevbackforecast(fileresu, mobaverage, anback1, mback1, jback1, agemin, agemax, dateprev1, dateprev2, *\/ */
16594: /* /\* mobilavproj, bage, fage, firstpass, lastpass, anback2, p, cptcoveff); *\/ */
16595: /* prevbackforecast(fileresu, mobaverage, anback1, mback1, jback1, agemin, agemax, dateprev1, dateprev2, */
16596: /* mobilavproj, bage, fage, firstpass, lastpass, anback2, p, cptcoveff); */
16597: prevbackforecast(fileresu, mobaverage, dateintmean, dateprojd, dateprojf, agemin, agemax, dateprev1, dateprev2,
16598: mobilavproj, bage, fage, firstpass, lastpass, p, cptcoveff);
16599:
16600:
16601: varbprlim(fileresu, nresult, mobaverage, mobilavproj, bage, fage, bprlim, &ncvyear, ftolpl, p, matcov, delti, stepm, cptcoveff);
16602:
16603:
16604: free_matrix(bprlim,1,nlstate,1,nlstate); /*here or after loop ? */
16605: free_matrix(ddnewms, 1, nlstate+ndeath, 1, nlstate+ndeath);
16606: free_matrix(ddsavms, 1, nlstate+ndeath, 1, nlstate+ndeath);
16607: free_matrix(ddoldms, 1, nlstate+ndeath, 1, nlstate+ndeath);
16608: } /* end Prevbcasting */
16609:
16610:
16611: /* ------ Other prevalence ratios------------ */
16612:
16613: free_ivector(wav,1,imx);
16614: free_imatrix(dh,1,lastpass-firstpass+2,1,imx);
16615: free_imatrix(bh,1,lastpass-firstpass+2,1,imx);
16616: free_imatrix(mw,1,lastpass-firstpass+2,1,imx);
16617:
16618:
16619: /*---------- Health expectancies, no variances ------------*/
16620:
16621: strcpy(filerese,"E_");
16622: strcat(filerese,fileresu);
16623: if((ficreseij=fopen(filerese,"w"))==NULL) {
16624: printf("Problem with Health Exp. resultfile: %s\n", filerese); exit(0);
16625: fprintf(ficlog,"Problem with Health Exp. resultfile: %s\n", filerese); exit(0);
16626: }
16627: printf("Computing Health Expectancies: result on file '%s' ...", filerese);fflush(stdout);
16628: fprintf(ficlog,"Computing Health Expectancies: result on file '%s' ...", filerese);fflush(ficlog);
16629:
16630: pstamp(ficreseij);
16631:
16632: /* i1=pow(2,cptcoveff); /\* Number of combination of dummy covariates *\/ */
16633: /* if (cptcovn < 1){i1=1;} */
16634:
16635: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
16636: /* for(k=1; k<=i1;k++){ /\* For any combination of dummy covariates, fixed and varying *\/ */
16637: /* if(i1 != 1 && TKresult[nres]!= k) */
16638: /* continue; */
16639: fprintf(ficreseij,"\n#****** ");
16640: printf("\n#****** ");
16641: for(j=1;j<=cptcovs;j++){
16642: /* for(j=1;j<=cptcoveff;j++) { */
16643: /* fprintf(ficreseij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
16644: fprintf(ficreseij," V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
16645: printf(" V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
16646: /* printf("V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
16647: }
16648: for (j=1; j<= nsq; j++){ /* For each selected (single) quantitative value */
16649: printf(" V%d=%lg ",TvarsQ[j], TinvDoQresult[nres][TvarsQ[j]]); /* TvarsQ[j] gives the name of the jth quantitative (fixed or time v) */
16650: fprintf(ficreseij,"V%d=%lg ",TvarsQ[j], TinvDoQresult[nres][TvarsQ[j]]);
16651: }
16652: fprintf(ficreseij,"******\n");
16653: printf("******\n");
16654:
16655: eij=ma3x(1,nlstate,1,nlstate,(int) bage, (int) fage);
16656: oldm=oldms;savm=savms;
16657: /* printf("HELLO Entering evsij bage=%d fage=%d k=%d estepm=%d nres=%d\n",(int) bage, (int)fage, k, estepm, nres); */
16658: evsij(eij, p, nlstate, stepm, (int) bage, (int)fage, oldm, savm, k, estepm, strstart, nres);
16659:
16660: free_ma3x(eij,1,nlstate,1,nlstate,(int) bage, (int)fage);
16661: }
16662: fclose(ficreseij);
16663: printf("done evsij\n");fflush(stdout);
16664: fprintf(ficlog,"done evsij\n");fflush(ficlog);
16665:
16666:
16667: /*---------- State-specific expectancies and variances ------------*/
16668: /* Should be moved in a function */
16669: strcpy(filerest,"T_");
16670: strcat(filerest,fileresu);
16671: if((ficrest=fopen(filerest,"w"))==NULL) {
16672: printf("Problem with total LE resultfile: %s\n", filerest);goto end;
16673: fprintf(ficlog,"Problem with total LE resultfile: %s\n", filerest);goto end;
16674: }
16675: printf("Computing Total Life expectancies with their standard errors: file '%s' ...\n", filerest); fflush(stdout);
16676: fprintf(ficlog,"Computing Total Life expectancies with their standard errors: file '%s' ...\n", filerest); fflush(ficlog);
16677: strcpy(fileresstde,"STDE_");
16678: strcat(fileresstde,fileresu);
16679: if((ficresstdeij=fopen(fileresstde,"w"))==NULL) {
16680: printf("Problem with State specific Exp. and std errors resultfile: %s\n", fileresstde); exit(0);
16681: fprintf(ficlog,"Problem with State specific Exp. and std errors resultfile: %s\n", fileresstde); exit(0);
16682: }
16683: printf(" Computing State-specific Expectancies and standard errors: result on file '%s' \n", fileresstde);
16684: fprintf(ficlog," Computing State-specific Expectancies and standard errors: result on file '%s' \n", fileresstde);
16685:
16686: strcpy(filerescve,"CVE_");
16687: strcat(filerescve,fileresu);
16688: if((ficrescveij=fopen(filerescve,"w"))==NULL) {
16689: printf("Problem with Covar. State-specific Exp. resultfile: %s\n", filerescve); exit(0);
16690: fprintf(ficlog,"Problem with Covar. State-specific Exp. resultfile: %s\n", filerescve); exit(0);
16691: }
16692: printf(" Computing Covar. of State-specific Expectancies: result on file '%s' \n", filerescve);
16693: fprintf(ficlog," Computing Covar. of State-specific Expectancies: result on file '%s' \n", filerescve);
16694:
16695: strcpy(fileresv,"V_");
16696: strcat(fileresv,fileresu);
16697: if((ficresvij=fopen(fileresv,"w"))==NULL) {
16698: printf("Problem with variance resultfile: %s\n", fileresv);exit(0);
16699: fprintf(ficlog,"Problem with variance resultfile: %s\n", fileresv);exit(0);
16700: }
16701: printf(" Computing Variance-covariance of State-specific Expectancies: file '%s' ... ", fileresv);fflush(stdout);
16702: fprintf(ficlog," Computing Variance-covariance of State-specific Expectancies: file '%s' ... ", fileresv);fflush(ficlog);
16703:
16704: i1=pow(2,cptcoveff); /* Number of combination of dummy covariates */
16705: if (cptcovn < 1){i1=1;}
16706:
16707: for(nres=1; nres <= nresult; nres++) /* For each resultline, find the combination and output results according to the values of dummies and then quanti. */
16708: for(k=1; k<=i1;k++){ /* For any combination of dummy covariates, fixed and varying. For each nres and each value at position k
16709: * we know Tresult[nres][result_position]= value of the dummy variable at the result_position in the nres resultline
16710: * Tvqresult[nres][result_position]= id of the variable at the result_position in the nres resultline
16711: * and Tqresult[nres][result_position]= value of the variable at the result_position in the nres resultline */
16712: /* */
16713: if(i1 != 1 && TKresult[nres]!= k) /* TKresult[nres] is the combination of this nres resultline. All the i1 combinations are not output */
16714: continue;
16715: printf("\n# model=1+age+%s \n#****** Result for:", model); /* HERE model is empty */
16716: fprintf(ficrest,"\n# model=1+age+%s \n#****** Result for:", model);
16717: fprintf(ficlog,"\n# model=1+age+%s \n#****** Result for:", model);
16718: /* It might not be a good idea to mix dummies and quantitative */
16719: /* 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 *\/ */
16720: 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 */
16721: /* printf("V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); /\* Output by variables in the resultline *\/ */
16722: /* Tvaraff[j] is the name of the dummy variable in position j in the equation model:
16723: * Tvaraff[1]@9={4, 3, 0, 0, 0, 0, 0, 0, 0}, in model=V5+V4+V3+V4*V3+V5*age
16724: * (V5 is quanti) V4 and V3 are dummies
16725: * TnsdVar[4] is the position 1 and TnsdVar[3]=2 in codtabm(k,l)(V4 V3)=V4 V3
16726: * l=1 l=2
16727: * k=1 1 1 0 0
16728: * k=2 2 1 1 0
16729: * k=3 [1] [2] 0 1
16730: * k=4 2 2 1 1
16731: * If nres=1 result: V3=1 V4=0 then k=3 and outputs
16732: * If nres=2 result: V4=1 V3=0 then k=2 and outputs
16733: * nres=1 =>k=3 j=1 V4= nbcode[4][codtabm(3,1)=1)=0; j=2 V3= nbcode[3][codtabm(3,2)=2]=1
16734: * nres=2 =>k=2 j=1 V4= nbcode[4][codtabm(2,1)=2)=1; j=2 V3= nbcode[3][codtabm(2,2)=1]=0
16735: */
16736: /* Tvresult[nres][j] Name of the variable at position j in this resultline */
16737: /* Tresult[nres][j] Value of this variable at position j could be a float if quantitative */
16738: /* We give up with the combinations!! */
16739: /* if(debugILK) */
16740: /* 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 *\/ */
16741:
16742: if(Dummy[modelresult[nres][j]]==0){/* Dummy variable of the variable in position modelresult in the model corresponding to j in resultline */
16743: /* 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 *\/ */ /* TinvDoQresult[nres][Name of the variable] */
16744: printf("V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]); /* Output of each value for the combination TKresult[nres], ordered by the covariate values in the resultline */
16745: fprintf(ficlog,"V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]); /* Output of each value for the combination TKresult[nres], ordere by the covariate values in the resultline */
16746: fprintf(ficrest,"V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]); /* Output of each value for the combination TKresult[nres], ordere by the covariate values in the resultline */
16747: if(Fixed[modelresult[nres][j]]==0){ /* Fixed */
16748: printf("fixed ");fprintf(ficlog,"fixed ");fprintf(ficrest,"fixed ");
16749: }else{
16750: printf("varyi ");fprintf(ficlog,"varyi ");fprintf(ficrest,"varyi ");
16751: }
16752: /* fprintf(ficrest,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
16753: /* fprintf(ficlog,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
16754: }else if(Dummy[modelresult[nres][j]]==1){ /* Quanti variable */
16755: /* For each selected (single) quantitative value */
16756: printf(" V%d=%lg ",Tvqresult[nres][j],Tqresult[nres][j]);
16757: fprintf(ficlog," V%d=%lg ",Tvqresult[nres][j],Tqresult[nres][j]);
16758: fprintf(ficrest," V%d=%lg ",Tvqresult[nres][j],Tqresult[nres][j]);
16759: if(Fixed[modelresult[nres][j]]==0){ /* Fixed */
16760: printf("fixed ");fprintf(ficlog,"fixed ");fprintf(ficrest,"fixed ");
16761: }else{
16762: printf("varyi ");fprintf(ficlog,"varyi ");fprintf(ficrest,"varyi ");
16763: }
16764: }else{
16765: 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 */
16766: 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 */
16767: exit(1);
16768: }
16769: } /* End loop for each variable in the resultline */
16770: /* for (j=1; j<= nsq; j++){ /\* For each selected (single) quantitative value *\/ */
16771: /* printf(" V%d=%f ",Tvqresult[nres][j],Tqresult[nres][resultmodel[nres][j]]); /\* Wrong j is not in the equation model *\/ */
16772: /* fprintf(ficrest," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][resultmodel[nres][j]]); */
16773: /* fprintf(ficlog," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][resultmodel[nres][j]]); */
16774: /* } */
16775: fprintf(ficrest,"******\n");
16776: fprintf(ficlog,"******\n");
16777: printf("******\n");
16778:
16779: fprintf(ficresstdeij,"\n#****** ");
16780: fprintf(ficrescveij,"\n#****** ");
16781: /* It could have been: for(j=1;j<=cptcoveff;j++) {printf("V=%d=%lg",Tvresult[nres][cpt],TinvDoQresult[nres][Tvresult[nres][cpt]]);} */
16782: /* But it won't be sorted and depends on how the resultline is ordered */
16783: for(j=1;j<=cptcoveff;j++) {
16784: fprintf(ficresstdeij,"V%d=%d ",Tvresult[nres][j],Tresult[nres][j]);
16785: /* fprintf(ficresstdeij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
16786: /* fprintf(ficrescveij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
16787: }
16788: for (j=1; j<= nsq; j++){ /* For each selected (single) quantitative value, TvarsQind gives the position of a quantitative in model equation */
16789: fprintf(ficresstdeij," V%d=%lg ",Tvar[TvarsQind[j]],Tqresult[nres][resultmodel[nres][TvarsQind[j]]]);
16790: fprintf(ficrescveij," V%d=%lg ",Tvar[TvarsQind[j]],Tqresult[nres][resultmodel[nres][TvarsQind[j]]]);
16791: }
16792: fprintf(ficresstdeij,"******\n");
16793: fprintf(ficrescveij,"******\n");
16794:
16795: fprintf(ficresvij,"\n#****** ");
16796: /* pstamp(ficresvij); */
16797: for(j=1;j<=cptcoveff;j++)
16798: fprintf(ficresvij,"V%d=%d ",Tvresult[nres][j],Tresult[nres][j]);
16799: /* fprintf(ficresvij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[TnsdVar[Tvaraff[j]]])]); */
16800: for (j=1; j<= nsq; j++){ /* For each selected (single) quantitative value */
16801: /* fprintf(ficresvij," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]); /\* To solve *\/ */
16802: fprintf(ficresvij," V%d=%lg ",Tvqresult[nres][j],Tqresult[nres][resultmodel[nres][j]]); /* Solved */
16803: }
16804: fprintf(ficresvij,"******\n");
16805:
16806: eij=ma3x(1,nlstate,1,nlstate,(int) bage, (int) fage);
16807: oldm=oldms;savm=savms;
16808: printf(" cvevsij ");
16809: fprintf(ficlog, " cvevsij ");
16810: cvevsij(eij, p, nlstate, stepm, (int) bage, (int)fage, oldm, savm, k, estepm, delti, matcov, strstart, nres);
16811: printf(" end cvevsij \n ");
16812: fprintf(ficlog, " end cvevsij \n ");
16813:
16814: /*
16815: */
16816: /* goto endfree; */
16817:
16818: vareij=ma3x(1,nlstate,1,nlstate,(int) bage, (int) fage);
16819: pstamp(ficrest);
16820:
16821: epj=vector(1,nlstate+1);
16822: for(vpopbased=0; vpopbased <= popbased; vpopbased++){ /* Done for vpopbased=0 and vpopbased=1 if popbased==1*/
16823: oldm=oldms;savm=savms; /* ZZ Segmentation fault */
16824: cptcod= 0; /* To be deleted */
16825: printf("varevsij vpopbased=%d popbased=%d \n",vpopbased,popbased);
16826: fprintf(ficlog, "varevsij vpopbased=%d popbased=%d \n",vpopbased,popbased);
16827: /* Call to varevsij to get cov(e.i, e.j)= vareij[i][j][(int)age]=sum_h sum_k trgrad(h_p.i) V(theta) grad(k_p.k) Equation 20 */
16828: /* Depending of popbased which changes the prevalences, either cross-sectional or period */
16829: 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 */
16830: fprintf(ficrest,"# Total life expectancy with std error and decomposition into time to be expected in each state\n\
16831: # (these are weighted average of eij where weights are ");
16832: if(vpopbased==1)
16833: 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);
16834: else
16835: fprintf(ficrest,"the age specific forward period (stable) prevalences in each state) \n");
16836: fprintf(ficrest,"# with proportions of time spent in each state with standard error (on the right of the table.\n ");
16837: fprintf(ficrest,"# Age popbased mobilav e.. (std) "); /* Adding covariate values? */
16838: for (i=1;i<=nlstate;i++) fprintf(ficrest,"e.%d (std) ",i);
16839: for (i=1;i<=nlstate;i++) fprintf(ficrest," %% e.%d/e.. (std) ",i);
16840: fprintf(ficrest,"\n");
16841: /* printf("Which p?\n"); for(i=1;i<=npar;i++)printf("p[i=%d]=%lf,",i,p[i]);printf("\n"); */
16842: printf("Computing age specific forward period (stable) prevalences in each health state \n");
16843: fprintf(ficlog,"Computing age specific forward period (stable) prevalences in each health state \n");
16844: for(age=bage; age <=fage ;age++){
16845: prevalim(prlim, nlstate, p, age, oldm, savm, ftolpl, &ncvyear, k, nres); /*ZZ Is it the correct prevalim */
16846: if (vpopbased==1) {
16847: if(mobilav ==0){
16848: for(i=1; i<=nlstate;i++)
16849: prlim[i][i]=probs[(int)age][i][k];
16850: }else{ /* mobilav */
16851: for(i=1; i<=nlstate;i++)
16852: prlim[i][i]=mobaverage[(int)age][i][k];
16853: }
16854: }
16855:
16856: fprintf(ficrest," %4.0f %d %d",age, vpopbased, mobilav);
16857: /* fprintf(ficrest," %4.0f %d %d %d %d",age, vpopbased, mobilav,Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]); */ /* to be done */
16858: /* printf(" age %4.0f ",age); */
16859: for(j=1, epj[nlstate+1]=0.;j <=nlstate;j++){
16860: for(i=1, epj[j]=0.;i <=nlstate;i++) {
16861: epj[j] += prlim[i][i]*eij[i][j][(int)age];
16862: /*ZZZ printf("%lf %lf ", prlim[i][i] ,eij[i][j][(int)age]);*/
16863: /* printf("%lf %lf ", prlim[i][i] ,eij[i][j][(int)age]); */
16864: }
16865: epj[nlstate+1] +=epj[j]; /* epp=sum_j epj = sum_j sum_i w_i e_ij */
16866: }
16867: /* printf(" age %4.0f \n",age); */
16868:
16869: for(i=1, vepp=0.;i <=nlstate;i++) /* Variance of total life expectancy e.. */
16870: for(j=1;j <=nlstate;j++)
16871: vepp += vareij[i][j][(int)age]; /* sum_i sum_j cov(e.i, e.j) = var(e..) */
16872: fprintf(ficrest," %7.3f (%7.3f)", epj[nlstate+1],sqrt(vepp));
16873: /* vareij[i][j] is the covariance cov(e.i, e.j) and vareij[j][j] is the variance of e.j */
16874: for(j=1;j <=nlstate;j++){
16875: fprintf(ficrest," %7.3f (%7.3f)", epj[j],sqrt(vareij[j][j][(int)age]));
16876: }
16877: /* And proportion of time spent in state j */
16878: /* $$ E[r(X,Y)-E(r(X,Y))]^2=[\frac{1}{\mu_y} -\frac{\mu_x}{{\mu_y}^2}]' Var(X,Y)[\frac{1}{\mu_y} -\frac{\mu_x}{{\mu_y}^2}]$$ */
16879: /* \frac{\mu_x^2}{\mu_y^2} ( \frac{\sigma^2_x}{\mu_x^2}-2\frac{\sigma_{xy}}{\mu_x\mu_y} +\frac{\sigma^2_y}{\mu_y^2}) */
16880: /* \frac{e_{.i}^2}{e_{..}^2} ( \frac{\Var e_{.i}}{e_{.i}^2}-2\frac{\Var e_{.i} + \sum_{j\ne i} \Cov e_{.j},e_{.i}}{e_{.i}e_{..}} +\frac{\Var e_{..}}{e_{..}^2})*/
16881: /*\mu_x = epj[j], \sigma^2_x = vareij[j][j][(int)age] and \mu_y=epj[nlstate+1], \sigma^2_y=vepp \sigmaxy= */
16882: /* vareij[j][j][(int)age]/epj[nlstate+1]^2 + vepp/epj[nlstate+1]^4 */
16883: for(j=1;j <=nlstate;j++){
16884: /* fprintf(ficrest," %7.3f (%7.3f)", epj[j]/epj[nlstate+1], sqrt( vareij[j][j][(int)age]/epj[j]/epj[j] + vepp/epj[j]/epj[j]/epj[j]/epj[j] )); */
16885: /* fprintf(ficrest," %7.3f (%7.3f)", epj[j]/epj[nlstate+1], sqrt( vareij[j][j][(int)age]/epj[j]/epj[j] + vepp/epj[j]/epj[j]/epj[j]/epj[j] )); */
16886:
16887: for(i=1,stdpercent=0.;i<=nlstate;i++){ /* Computing cov(e..,e.j)=cov(sum_i e.i,e.j)=sum_i cov(e.i, e.j) */
16888: stdpercent += vareij[i][j][(int)age];
16889: }
16890: stdpercent= epj[j]*epj[j]/epj[nlstate+1]/epj[nlstate+1]* (vareij[j][j][(int)age]/epj[j]/epj[j]-2.*stdpercent/epj[j]/epj[nlstate+1]+ vepp/epj[nlstate+1]/epj[nlstate+1]);
16891: /* stdpercent= epj[j]*epj[j]/epj[nlstate+1]/epj[nlstate+1]*(vareij[j][j][(int)age]/epj[j]/epj[j] + vepp/epj[nlstate+1]/epj[nlstate+1]); */ /* Without covariance */
16892: /* fprintf(ficrest," %7.3f (%7.3f)", epj[j]/epj[nlstate+1], sqrt( vareij[j][j][(int)age]/epj[nlstate+1]/epj[nlstate+1] + epj[j]*epj[j]*vepp/epj[nlstate+1]/epj[nlstate+1]/epj[nlstate+1]/epj[nlstate+1] )); */
16893: fprintf(ficrest," %7.3f (%7.3f)", epj[j]/epj[nlstate+1], sqrt(stdpercent));
16894: }
16895: fprintf(ficrest,"\n");
16896: }
16897: } /* End vpopbased */
16898: free_vector(epj,1,nlstate+1);
16899: free_ma3x(eij,1,nlstate,1,nlstate,(int) bage, (int)fage);
16900: free_ma3x(vareij,1,nlstate,1,nlstate,(int) bage, (int)fage);
16901: printf("done selection\n");fflush(stdout);
16902: fprintf(ficlog,"done selection\n");fflush(ficlog);
16903:
16904: } /* End k selection or end covariate selection for nres */
16905:
16906: printf("done State-specific expectancies\n");fflush(stdout);
16907: fprintf(ficlog,"done State-specific expectancies\n");fflush(ficlog);
16908:
16909: /* variance-covariance of forward period prevalence */
16910: varprlim(fileresu, nresult, mobaverage, mobilavproj, bage, fage, prlim, &ncvyear, ftolpl, p, matcov, delti, stepm, cptcoveff);
16911:
16912:
16913: free_vector(weight,firstobs,lastobs);
16914: free_imatrix(Tvardk,0,NCOVMAX,1,2);
16915: free_imatrix(Tvard,1,NCOVMAX,1,2);
16916: free_imatrix(s,1,maxwav+1,firstobs,lastobs);
16917: free_matrix(anint,1,maxwav,firstobs,lastobs);
16918: free_matrix(mint,1,maxwav,firstobs,lastobs);
16919: free_ivector(cod,firstobs,lastobs);
16920: free_ivector(tab,1,NCOVMAX);
16921: fclose(ficresstdeij);
16922: fclose(ficrescveij);
16923: fclose(ficresvij);
16924: fclose(ficrest);
16925: fclose(ficpar);
16926:
16927:
16928: /*---------- End : free ----------------*/
16929: if (mobilav!=0 ||mobilavproj !=0)
16930: free_ma3x(mobaverages,AGEINF, AGESUP,1,nlstate+ndeath, 1,ncovcombmax); /* We need to have a squared matrix with prevalence of the dead! */
16931: free_ma3x(probs,AGEINF,AGESUP,1,nlstate+ndeath, 1,ncovcombmax);
16932: free_matrix(prlim,1,nlstate,1,nlstate); /*here or after loop ? */
16933: free_matrix(pmmij,1,nlstate+ndeath,1,nlstate+ndeath);
16934: } /* mle==-3 arrives here for freeing */
16935: /* endfree:*/
16936: if(mle!=-3) free_matrix(precov, 1,MAXRESULTLINESPONE,1,NCOVMAX+1); /* Could be elsewhere ?*/
16937: free_matrix(oldms, 1,nlstate+ndeath,1,nlstate+ndeath);
16938: free_matrix(newms, 1,nlstate+ndeath,1,nlstate+ndeath);
16939: free_matrix(savms, 1,nlstate+ndeath,1,nlstate+ndeath);
16940: /* if(ntv+nqtv>=1)free_ma3x(cotvar,1,maxwav,1,ntv+nqtv,firstobs,lastobs); */
16941: if(ntv+nqtv>=1)free_ma3x(cotvar,1,maxwav,ncovcol+nqv+1,ncovcol+nqv+ntv+nqtv,firstobs,lastobs);
16942: if(nqtv>=1)free_ma3x(cotqvar,1,maxwav,1,nqtv,firstobs,lastobs);
16943: if(nqv>=1)free_matrix(coqvar,1,nqv,firstobs,lastobs);
16944: free_matrix(covar,0,NCOVMAX,firstobs,lastobs);
16945: free_matrix(matcov,1,npar,1,npar);
16946: free_matrix(hess,1,npar,1,npar);
16947: /*free_vector(delti,1,npar);*/
16948: free_ma3x(delti3,1,nlstate,1, nlstate+ndeath-1,1,ncovmodel);
16949: free_matrix(agev,1,maxwav,1,imx);
16950: free_ma3x(paramstart,1,nlstate,1, nlstate+ndeath-1,1,ncovmodel);
16951: free_ma3x(param,1,nlstate,1, nlstate+ndeath-1,1,ncovmodel);
16952:
16953: free_ivector(ncodemax,1,NCOVMAX);
16954: free_ivector(ncodemaxwundef,1,NCOVMAX);
16955: free_ivector(Dummy,-1,NCOVMAX);
16956: free_ivector(Fixed,-1,NCOVMAX);
16957: free_ivector(DummyV,-1,NCOVMAX);
16958: free_ivector(FixedV,-1,NCOVMAX);
16959: free_ivector(Typevar,-1,NCOVMAX);
16960: free_ivector(Tvar,1,NCOVMAX);
16961: free_ivector(TvarsQ,1,NCOVMAX);
16962: free_ivector(TvarsQind,1,NCOVMAX);
16963: free_ivector(TvarsD,1,NCOVMAX);
16964: free_ivector(TnsdVar,1,NCOVMAX);
16965: free_ivector(TvarsDind,1,NCOVMAX);
16966: free_ivector(TvarFD,1,NCOVMAX);
16967: free_ivector(TvarFDind,1,NCOVMAX);
16968: free_ivector(TvarF,1,NCOVMAX);
16969: free_ivector(TvarFind,1,NCOVMAX);
16970: free_ivector(TvarV,1,NCOVMAX);
16971: free_ivector(TvarVind,1,NCOVMAX);
16972: free_ivector(TvarA,1,NCOVMAX);
16973: free_ivector(TvarAind,1,NCOVMAX);
16974: free_ivector(TvarFQ,1,NCOVMAX);
16975: free_ivector(TvarFQind,1,NCOVMAX);
16976: free_ivector(TvarVD,1,NCOVMAX);
16977: free_ivector(TvarVDind,1,NCOVMAX);
16978: free_ivector(TvarVQ,1,NCOVMAX);
16979: free_ivector(TvarVQind,1,NCOVMAX);
16980: free_ivector(TvarAVVA,1,NCOVMAX);
16981: free_ivector(TvarAVVAind,1,NCOVMAX);
16982: free_ivector(TvarVVA,1,NCOVMAX);
16983: free_ivector(TvarVVAind,1,NCOVMAX);
16984: free_ivector(TvarVV,1,NCOVMAX);
16985: free_ivector(TvarVVind,1,NCOVMAX);
16986:
16987: free_ivector(Tvarsel,1,NCOVMAX);
16988: free_vector(Tvalsel,1,NCOVMAX);
16989: free_ivector(Tposprod,1,NCOVMAX);
16990: free_ivector(Tprod,1,NCOVMAX);
16991: free_ivector(Tvaraff,1,NCOVMAX);
16992: free_ivector(invalidvarcomb,0,ncovcombmax);
16993: free_ivector(Tage,1,NCOVMAX);
16994: free_ivector(Tmodelind,1,NCOVMAX);
16995: free_ivector(TmodelInvind,1,NCOVMAX);
16996: free_ivector(TmodelInvQind,1,NCOVMAX);
16997:
16998: /* free_matrix(precov, 1,MAXRESULTLINESPONE,1,NCOVMAX+1); /\* Could be elsewhere ?*\/ */
16999:
17000: free_imatrix(nbcode,0,NCOVMAX,0,NCOVMAX);
17001: /* free_imatrix(codtab,1,100,1,10); */
17002: fflush(fichtm);
17003: fflush(ficgp);
17004:
17005:
17006: if((nberr >0) || (nbwarn>0)){
17007: printf("End of Imach with %d errors and/or %d warnings. Please look at the log file for details.\n",nberr,nbwarn);
17008: fprintf(ficlog,"End of Imach with %d errors and/or warnings %d. Please look at the log file for details.\n",nberr,nbwarn);
17009: }else{
17010: printf("End of Imach\n");
17011: fprintf(ficlog,"End of Imach\n");
17012: }
17013: printf("See log file on %s\n",filelog);
17014: /* gettimeofday(&end_time, (struct timezone*)0);*/ /* after time */
17015: /*(void) gettimeofday(&end_time,&tzp);*/
17016: rend_time = time(NULL);
17017: end_time = *localtime(&rend_time);
17018: /* tml = *localtime(&end_time.tm_sec); */
17019: strcpy(strtend,asctime(&end_time));
17020: printf("Local time at start %s\nLocal time at end %s",strstart, strtend);
17021: fprintf(ficlog,"Local time at start %s\nLocal time at end %s\n",strstart, strtend);
17022: printf("Total time used %s\n", asc_diff_time(rend_time -rstart_time,tmpout));
17023:
17024: printf("Total time was %.0lf Sec.\n", difftime(rend_time,rstart_time));
17025: fprintf(ficlog,"Total time used %s\n", asc_diff_time(rend_time -rstart_time,tmpout));
17026: fprintf(ficlog,"Total time was %.0lf Sec.\n", difftime(rend_time,rstart_time));
17027: /* printf("Total time was %d uSec.\n", total_usecs);*/
17028: /* if(fileappend(fichtm,optionfilehtm)){ */
17029: fprintf(fichtm,"<br>Local time at start %s<br>Local time at end %s<br>\n</body></html>",strstart, strtend);
17030: fclose(fichtm);
17031: fprintf(fichtmcov,"<br>Local time at start %s<br>Local time at end %s<br>\n</body></html>",strstart, strtend);
17032: fclose(fichtmcov);
17033: fclose(ficgp);
17034: fclose(ficlog);
17035: /*------ End -----------*/
17036:
17037:
17038: /* Executes gnuplot */
17039:
17040: printf("Before Current directory %s!\n",pathcd);
17041: #ifdef WIN32
17042: if (_chdir(pathcd) != 0)
17043: printf("Can't move to directory %s!\n",path);
17044: if(_getcwd(pathcd,MAXLINE) > 0)
17045: #else
17046: if(chdir(pathcd) != 0)
17047: printf("Can't move to directory %s!\n", path);
17048: if (getcwd(pathcd, MAXLINE) > 0)
17049: #endif
17050: printf("Current directory %s!\n",pathcd);
17051: /*strcat(plotcmd,CHARSEPARATOR);*/
17052: sprintf(plotcmd,"gnuplot");
17053: #ifdef _WIN32
17054: sprintf(plotcmd,"\"%sgnuplot.exe\"",pathimach);
17055: #endif
17056: if(!stat(plotcmd,&info)){
17057: printf("Error or gnuplot program not found: '%s'\n",plotcmd);fflush(stdout);
17058: if(!stat(getenv("GNUPLOTBIN"),&info)){
17059: printf("Error or gnuplot program not found: '%s' Environment GNUPLOTBIN not set.\n",plotcmd);fflush(stdout);
17060: }else
17061: strcpy(pplotcmd,plotcmd);
17062: #ifdef __unix
17063: strcpy(plotcmd,GNUPLOTPROGRAM);
17064: if(!stat(plotcmd,&info)){
17065: printf("Error gnuplot program not found: '%s'\n",plotcmd);fflush(stdout);
17066: }else
17067: strcpy(pplotcmd,plotcmd);
17068: #endif
17069: }else
17070: strcpy(pplotcmd,plotcmd);
17071:
17072: sprintf(plotcmd,"%s %s",pplotcmd, optionfilegnuplot);
17073: printf("Starting graphs with: '%s'\n",plotcmd);fflush(stdout);
17074: strcpy(pplotcmd,plotcmd);
17075:
17076: if((outcmd=system(plotcmd)) != 0){
17077: printf("Error in gnuplot, command might not be in your path: '%s', err=%d\n", plotcmd, outcmd);
17078: printf("\n Trying if gnuplot resides on the same directory that IMaCh\n");
17079: sprintf(plotcmd,"%sgnuplot %s", pathimach, optionfilegnuplot);
17080: if((outcmd=system(plotcmd)) != 0){
17081: printf("\n Still a problem with gnuplot command %s, err=%d\n", plotcmd, outcmd);
17082: strcpy(plotcmd,pplotcmd);
17083: }
17084: }
17085: printf(" Successful, please wait...");
17086: while (z[0] != 'q') {
17087: /* chdir(path); */
17088: printf("\nType e to edit results with your browser, g to graph again and q for exit: ");
17089: scanf("%s",z);
17090: /* if (z[0] == 'c') system("./imach"); */
17091: if (z[0] == 'e') {
17092: #ifdef __APPLE__
17093: sprintf(pplotcmd, "open %s", optionfilehtm);
17094: #elif __linux
17095: sprintf(pplotcmd, "xdg-open %s", optionfilehtm);
17096: #else
17097: sprintf(pplotcmd, "%s", optionfilehtm);
17098: #endif
17099: printf("Starting browser with: %s",pplotcmd);fflush(stdout);
17100: system(pplotcmd);
17101: }
17102: else if (z[0] == 'g') system(plotcmd);
17103: else if (z[0] == 'q') exit(0);
17104: }
17105: end:
17106: while (z[0] != 'q') {
17107: printf("\nType q for exiting: "); fflush(stdout);
17108: scanf("%s",z);
17109: }
17110: printf("End\n");
17111: exit(0);
17112: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>