Annotation of imach/src/imach.c, revision 1.231
1.231 ! brouard 1: /* $Id: imach.c,v 1.230 2016/08/22 06:55:53 brouard Exp $
1.126 brouard 2: $State: Exp $
1.163 brouard 3: $Log: imach.c,v $
1.231 ! brouard 4: Revision 1.230 2016/08/22 06:55:53 brouard
! 5: Summary: Not working
! 6:
1.230 brouard 7: Revision 1.229 2016/07/23 09:45:53 brouard
8: Summary: Completing for func too
9:
1.229 brouard 10: Revision 1.228 2016/07/22 17:45:30 brouard
11: Summary: Fixing some arrays, still debugging
12:
1.227 brouard 13: Revision 1.226 2016/07/12 18:42:34 brouard
14: Summary: temp
15:
1.226 brouard 16: Revision 1.225 2016/07/12 08:40:03 brouard
17: Summary: saving but not running
18:
1.225 brouard 19: Revision 1.224 2016/07/01 13:16:01 brouard
20: Summary: Fixes
21:
1.224 brouard 22: Revision 1.223 2016/02/19 09:23:35 brouard
23: Summary: temporary
24:
1.223 brouard 25: Revision 1.222 2016/02/17 08:14:50 brouard
26: Summary: Probably last 0.98 stable version 0.98r6
27:
1.222 brouard 28: Revision 1.221 2016/02/15 23:35:36 brouard
29: Summary: minor bug
30:
1.220 brouard 31: Revision 1.219 2016/02/15 00:48:12 brouard
32: *** empty log message ***
33:
1.219 brouard 34: Revision 1.218 2016/02/12 11:29:23 brouard
35: Summary: 0.99 Back projections
36:
1.218 brouard 37: Revision 1.217 2015/12/23 17:18:31 brouard
38: Summary: Experimental backcast
39:
1.217 brouard 40: Revision 1.216 2015/12/18 17:32:11 brouard
41: Summary: 0.98r4 Warning and status=-2
42:
43: Version 0.98r4 is now:
44: - displaying an error when status is -1, date of interview unknown and date of death known;
45: - permitting a status -2 when the vital status is unknown at a known date of right truncation.
46: Older changes concerning s=-2, dating from 2005 have been supersed.
47:
1.216 brouard 48: Revision 1.215 2015/12/16 08:52:24 brouard
49: Summary: 0.98r4 working
50:
1.215 brouard 51: Revision 1.214 2015/12/16 06:57:54 brouard
52: Summary: temporary not working
53:
1.214 brouard 54: Revision 1.213 2015/12/11 18:22:17 brouard
55: Summary: 0.98r4
56:
1.213 brouard 57: Revision 1.212 2015/11/21 12:47:24 brouard
58: Summary: minor typo
59:
1.212 brouard 60: Revision 1.211 2015/11/21 12:41:11 brouard
61: Summary: 0.98r3 with some graph of projected cross-sectional
62:
63: Author: Nicolas Brouard
64:
1.211 brouard 65: Revision 1.210 2015/11/18 17:41:20 brouard
66: Summary: Start working on projected prevalences
67:
1.210 brouard 68: Revision 1.209 2015/11/17 22:12:03 brouard
69: Summary: Adding ftolpl parameter
70: Author: N Brouard
71:
72: We had difficulties to get smoothed confidence intervals. It was due
73: to the period prevalence which wasn't computed accurately. The inner
74: parameter ftolpl is now an outer parameter of the .imach parameter
75: file after estepm. If ftolpl is small 1.e-4 and estepm too,
76: computation are long.
77:
1.209 brouard 78: Revision 1.208 2015/11/17 14:31:57 brouard
79: Summary: temporary
80:
1.208 brouard 81: Revision 1.207 2015/10/27 17:36:57 brouard
82: *** empty log message ***
83:
1.207 brouard 84: Revision 1.206 2015/10/24 07:14:11 brouard
85: *** empty log message ***
86:
1.206 brouard 87: Revision 1.205 2015/10/23 15:50:53 brouard
88: Summary: 0.98r3 some clarification for graphs on likelihood contributions
89:
1.205 brouard 90: Revision 1.204 2015/10/01 16:20:26 brouard
91: Summary: Some new graphs of contribution to likelihood
92:
1.204 brouard 93: Revision 1.203 2015/09/30 17:45:14 brouard
94: Summary: looking at better estimation of the hessian
95:
96: Also a better criteria for convergence to the period prevalence And
97: therefore adding the number of years needed to converge. (The
98: prevalence in any alive state shold sum to one
99:
1.203 brouard 100: Revision 1.202 2015/09/22 19:45:16 brouard
101: Summary: Adding some overall graph on contribution to likelihood. Might change
102:
1.202 brouard 103: Revision 1.201 2015/09/15 17:34:58 brouard
104: Summary: 0.98r0
105:
106: - Some new graphs like suvival functions
107: - Some bugs fixed like model=1+age+V2.
108:
1.201 brouard 109: Revision 1.200 2015/09/09 16:53:55 brouard
110: Summary: Big bug thanks to Flavia
111:
112: Even model=1+age+V2. did not work anymore
113:
1.200 brouard 114: Revision 1.199 2015/09/07 14:09:23 brouard
115: Summary: 0.98q6 changing default small png format for graph to vectorized svg.
116:
1.199 brouard 117: Revision 1.198 2015/09/03 07:14:39 brouard
118: Summary: 0.98q5 Flavia
119:
1.198 brouard 120: Revision 1.197 2015/09/01 18:24:39 brouard
121: *** empty log message ***
122:
1.197 brouard 123: Revision 1.196 2015/08/18 23:17:52 brouard
124: Summary: 0.98q5
125:
1.196 brouard 126: Revision 1.195 2015/08/18 16:28:39 brouard
127: Summary: Adding a hack for testing purpose
128:
129: After reading the title, ftol and model lines, if the comment line has
130: a q, starting with #q, the answer at the end of the run is quit. It
131: permits to run test files in batch with ctest. The former workaround was
132: $ echo q | imach foo.imach
133:
1.195 brouard 134: Revision 1.194 2015/08/18 13:32:00 brouard
135: Summary: Adding error when the covariance matrix doesn't contain the exact number of lines required by the model line.
136:
1.194 brouard 137: Revision 1.193 2015/08/04 07:17:42 brouard
138: Summary: 0.98q4
139:
1.193 brouard 140: Revision 1.192 2015/07/16 16:49:02 brouard
141: Summary: Fixing some outputs
142:
1.192 brouard 143: Revision 1.191 2015/07/14 10:00:33 brouard
144: Summary: Some fixes
145:
1.191 brouard 146: Revision 1.190 2015/05/05 08:51:13 brouard
147: Summary: Adding digits in output parameters (7 digits instead of 6)
148:
149: Fix 1+age+.
150:
1.190 brouard 151: Revision 1.189 2015/04/30 14:45:16 brouard
152: Summary: 0.98q2
153:
1.189 brouard 154: Revision 1.188 2015/04/30 08:27:53 brouard
155: *** empty log message ***
156:
1.188 brouard 157: Revision 1.187 2015/04/29 09:11:15 brouard
158: *** empty log message ***
159:
1.187 brouard 160: Revision 1.186 2015/04/23 12:01:52 brouard
161: Summary: V1*age is working now, version 0.98q1
162:
163: Some codes had been disabled in order to simplify and Vn*age was
164: working in the optimization phase, ie, giving correct MLE parameters,
165: but, as usual, outputs were not correct and program core dumped.
166:
1.186 brouard 167: Revision 1.185 2015/03/11 13:26:42 brouard
168: Summary: Inclusion of compile and links command line for Intel Compiler
169:
1.185 brouard 170: Revision 1.184 2015/03/11 11:52:39 brouard
171: Summary: Back from Windows 8. Intel Compiler
172:
1.184 brouard 173: Revision 1.183 2015/03/10 20:34:32 brouard
174: Summary: 0.98q0, trying with directest, mnbrak fixed
175:
176: We use directest instead of original Powell test; probably no
177: incidence on the results, but better justifications;
178: We fixed Numerical Recipes mnbrak routine which was wrong and gave
179: wrong results.
180:
1.183 brouard 181: Revision 1.182 2015/02/12 08:19:57 brouard
182: Summary: Trying to keep directest which seems simpler and more general
183: Author: Nicolas Brouard
184:
1.182 brouard 185: Revision 1.181 2015/02/11 23:22:24 brouard
186: Summary: Comments on Powell added
187:
188: Author:
189:
1.181 brouard 190: Revision 1.180 2015/02/11 17:33:45 brouard
191: Summary: Finishing move from main to function (hpijx and prevalence_limit)
192:
1.180 brouard 193: Revision 1.179 2015/01/04 09:57:06 brouard
194: Summary: back to OS/X
195:
1.179 brouard 196: Revision 1.178 2015/01/04 09:35:48 brouard
197: *** empty log message ***
198:
1.178 brouard 199: Revision 1.177 2015/01/03 18:40:56 brouard
200: Summary: Still testing ilc32 on OSX
201:
1.177 brouard 202: Revision 1.176 2015/01/03 16:45:04 brouard
203: *** empty log message ***
204:
1.176 brouard 205: Revision 1.175 2015/01/03 16:33:42 brouard
206: *** empty log message ***
207:
1.175 brouard 208: Revision 1.174 2015/01/03 16:15:49 brouard
209: Summary: Still in cross-compilation
210:
1.174 brouard 211: Revision 1.173 2015/01/03 12:06:26 brouard
212: Summary: trying to detect cross-compilation
213:
1.173 brouard 214: Revision 1.172 2014/12/27 12:07:47 brouard
215: Summary: Back from Visual Studio and Intel, options for compiling for Windows XP
216:
1.172 brouard 217: Revision 1.171 2014/12/23 13:26:59 brouard
218: Summary: Back from Visual C
219:
220: Still problem with utsname.h on Windows
221:
1.171 brouard 222: Revision 1.170 2014/12/23 11:17:12 brouard
223: Summary: Cleaning some \%% back to %%
224:
225: The escape was mandatory for a specific compiler (which one?), but too many warnings.
226:
1.170 brouard 227: Revision 1.169 2014/12/22 23:08:31 brouard
228: Summary: 0.98p
229:
230: Outputs some informations on compiler used, OS etc. Testing on different platforms.
231:
1.169 brouard 232: Revision 1.168 2014/12/22 15:17:42 brouard
1.170 brouard 233: Summary: update
1.169 brouard 234:
1.168 brouard 235: Revision 1.167 2014/12/22 13:50:56 brouard
236: Summary: Testing uname and compiler version and if compiled 32 or 64
237:
238: Testing on Linux 64
239:
1.167 brouard 240: Revision 1.166 2014/12/22 11:40:47 brouard
241: *** empty log message ***
242:
1.166 brouard 243: Revision 1.165 2014/12/16 11:20:36 brouard
244: Summary: After compiling on Visual C
245:
246: * imach.c (Module): Merging 1.61 to 1.162
247:
1.165 brouard 248: Revision 1.164 2014/12/16 10:52:11 brouard
249: Summary: Merging with Visual C after suppressing some warnings for unused variables. Also fixing Saito's bug 0.98Xn
250:
251: * imach.c (Module): Merging 1.61 to 1.162
252:
1.164 brouard 253: Revision 1.163 2014/12/16 10:30:11 brouard
254: * imach.c (Module): Merging 1.61 to 1.162
255:
1.163 brouard 256: Revision 1.162 2014/09/25 11:43:39 brouard
257: Summary: temporary backup 0.99!
258:
1.162 brouard 259: Revision 1.1 2014/09/16 11:06:58 brouard
260: Summary: With some code (wrong) for nlopt
261:
262: Author:
263:
264: Revision 1.161 2014/09/15 20:41:41 brouard
265: Summary: Problem with macro SQR on Intel compiler
266:
1.161 brouard 267: Revision 1.160 2014/09/02 09:24:05 brouard
268: *** empty log message ***
269:
1.160 brouard 270: Revision 1.159 2014/09/01 10:34:10 brouard
271: Summary: WIN32
272: Author: Brouard
273:
1.159 brouard 274: Revision 1.158 2014/08/27 17:11:51 brouard
275: *** empty log message ***
276:
1.158 brouard 277: Revision 1.157 2014/08/27 16:26:55 brouard
278: Summary: Preparing windows Visual studio version
279: Author: Brouard
280:
281: In order to compile on Visual studio, time.h is now correct and time_t
282: and tm struct should be used. difftime should be used but sometimes I
283: just make the differences in raw time format (time(&now).
284: Trying to suppress #ifdef LINUX
285: Add xdg-open for __linux in order to open default browser.
286:
1.157 brouard 287: Revision 1.156 2014/08/25 20:10:10 brouard
288: *** empty log message ***
289:
1.156 brouard 290: Revision 1.155 2014/08/25 18:32:34 brouard
291: Summary: New compile, minor changes
292: Author: Brouard
293:
1.155 brouard 294: Revision 1.154 2014/06/20 17:32:08 brouard
295: Summary: Outputs now all graphs of convergence to period prevalence
296:
1.154 brouard 297: Revision 1.153 2014/06/20 16:45:46 brouard
298: Summary: If 3 live state, convergence to period prevalence on same graph
299: Author: Brouard
300:
1.153 brouard 301: Revision 1.152 2014/06/18 17:54:09 brouard
302: Summary: open browser, use gnuplot on same dir than imach if not found in the path
303:
1.152 brouard 304: Revision 1.151 2014/06/18 16:43:30 brouard
305: *** empty log message ***
306:
1.151 brouard 307: Revision 1.150 2014/06/18 16:42:35 brouard
308: Summary: If gnuplot is not in the path try on same directory than imach binary (OSX)
309: Author: brouard
310:
1.150 brouard 311: Revision 1.149 2014/06/18 15:51:14 brouard
312: Summary: Some fixes in parameter files errors
313: Author: Nicolas Brouard
314:
1.149 brouard 315: Revision 1.148 2014/06/17 17:38:48 brouard
316: Summary: Nothing new
317: Author: Brouard
318:
319: Just a new packaging for OS/X version 0.98nS
320:
1.148 brouard 321: Revision 1.147 2014/06/16 10:33:11 brouard
322: *** empty log message ***
323:
1.147 brouard 324: Revision 1.146 2014/06/16 10:20:28 brouard
325: Summary: Merge
326: Author: Brouard
327:
328: Merge, before building revised version.
329:
1.146 brouard 330: Revision 1.145 2014/06/10 21:23:15 brouard
331: Summary: Debugging with valgrind
332: Author: Nicolas Brouard
333:
334: Lot of changes in order to output the results with some covariates
335: After the Edimburgh REVES conference 2014, it seems mandatory to
336: improve the code.
337: No more memory valgrind error but a lot has to be done in order to
338: continue the work of splitting the code into subroutines.
339: Also, decodemodel has been improved. Tricode is still not
340: optimal. nbcode should be improved. Documentation has been added in
341: the source code.
342:
1.144 brouard 343: Revision 1.143 2014/01/26 09:45:38 brouard
344: Summary: Version 0.98nR (to be improved, but gives same optimization results as 0.98k. Nice, promising
345:
346: * imach.c (Module): Trying to merge old staffs together while being at Tokyo. Not tested...
347: (Module): Version 0.98nR Running ok, but output format still only works for three covariates.
348:
1.143 brouard 349: Revision 1.142 2014/01/26 03:57:36 brouard
350: Summary: gnuplot changed plot w l 1 has to be changed to plot w l lt 2
351:
352: * imach.c (Module): Trying to merge old staffs together while being at Tokyo. Not tested...
353:
1.142 brouard 354: Revision 1.141 2014/01/26 02:42:01 brouard
355: * imach.c (Module): Trying to merge old staffs together while being at Tokyo. Not tested...
356:
1.141 brouard 357: Revision 1.140 2011/09/02 10:37:54 brouard
358: Summary: times.h is ok with mingw32 now.
359:
1.140 brouard 360: Revision 1.139 2010/06/14 07:50:17 brouard
361: After the theft of my laptop, I probably lost some lines of codes which were not uploaded to the CVS tree.
362: I remember having already fixed agemin agemax which are pointers now but not cvs saved.
363:
1.139 brouard 364: Revision 1.138 2010/04/30 18:19:40 brouard
365: *** empty log message ***
366:
1.138 brouard 367: Revision 1.137 2010/04/29 18:11:38 brouard
368: (Module): Checking covariates for more complex models
369: than V1+V2. A lot of change to be done. Unstable.
370:
1.137 brouard 371: Revision 1.136 2010/04/26 20:30:53 brouard
372: (Module): merging some libgsl code. Fixing computation
373: of likelione (using inter/intrapolation if mle = 0) in order to
374: get same likelihood as if mle=1.
375: Some cleaning of code and comments added.
376:
1.136 brouard 377: Revision 1.135 2009/10/29 15:33:14 brouard
378: (Module): Now imach stops if date of birth, at least year of birth, is not given. Some cleaning of the code.
379:
1.135 brouard 380: Revision 1.134 2009/10/29 13:18:53 brouard
381: (Module): Now imach stops if date of birth, at least year of birth, is not given. Some cleaning of the code.
382:
1.134 brouard 383: Revision 1.133 2009/07/06 10:21:25 brouard
384: just nforces
385:
1.133 brouard 386: Revision 1.132 2009/07/06 08:22:05 brouard
387: Many tings
388:
1.132 brouard 389: Revision 1.131 2009/06/20 16:22:47 brouard
390: Some dimensions resccaled
391:
1.131 brouard 392: Revision 1.130 2009/05/26 06:44:34 brouard
393: (Module): Max Covariate is now set to 20 instead of 8. A
394: lot of cleaning with variables initialized to 0. Trying to make
395: V2+V3*age+V1+V4 strb=V3*age+V1+V4 working better.
396:
1.130 brouard 397: Revision 1.129 2007/08/31 13:49:27 lievre
398: Modification of the way of exiting when the covariate is not binary in order to see on the window the error message before exiting
399:
1.129 lievre 400: Revision 1.128 2006/06/30 13:02:05 brouard
401: (Module): Clarifications on computing e.j
402:
1.128 brouard 403: Revision 1.127 2006/04/28 18:11:50 brouard
404: (Module): Yes the sum of survivors was wrong since
405: imach-114 because nhstepm was no more computed in the age
406: loop. Now we define nhstepma in the age loop.
407: (Module): In order to speed up (in case of numerous covariates) we
408: compute health expectancies (without variances) in a first step
409: and then all the health expectancies with variances or standard
410: deviation (needs data from the Hessian matrices) which slows the
411: computation.
412: In the future we should be able to stop the program is only health
413: expectancies and graph are needed without standard deviations.
414:
1.127 brouard 415: Revision 1.126 2006/04/28 17:23:28 brouard
416: (Module): Yes the sum of survivors was wrong since
417: imach-114 because nhstepm was no more computed in the age
418: loop. Now we define nhstepma in the age loop.
419: Version 0.98h
420:
1.126 brouard 421: Revision 1.125 2006/04/04 15:20:31 lievre
422: Errors in calculation of health expectancies. Age was not initialized.
423: Forecasting file added.
424:
425: Revision 1.124 2006/03/22 17:13:53 lievre
426: Parameters are printed with %lf instead of %f (more numbers after the comma).
427: The log-likelihood is printed in the log file
428:
429: Revision 1.123 2006/03/20 10:52:43 brouard
430: * imach.c (Module): <title> changed, corresponds to .htm file
431: name. <head> headers where missing.
432:
433: * imach.c (Module): Weights can have a decimal point as for
434: English (a comma might work with a correct LC_NUMERIC environment,
435: otherwise the weight is truncated).
436: Modification of warning when the covariates values are not 0 or
437: 1.
438: Version 0.98g
439:
440: Revision 1.122 2006/03/20 09:45:41 brouard
441: (Module): Weights can have a decimal point as for
442: English (a comma might work with a correct LC_NUMERIC environment,
443: otherwise the weight is truncated).
444: Modification of warning when the covariates values are not 0 or
445: 1.
446: Version 0.98g
447:
448: Revision 1.121 2006/03/16 17:45:01 lievre
449: * imach.c (Module): Comments concerning covariates added
450:
451: * imach.c (Module): refinements in the computation of lli if
452: status=-2 in order to have more reliable computation if stepm is
453: not 1 month. Version 0.98f
454:
455: Revision 1.120 2006/03/16 15:10:38 lievre
456: (Module): refinements in the computation of lli if
457: status=-2 in order to have more reliable computation if stepm is
458: not 1 month. Version 0.98f
459:
460: Revision 1.119 2006/03/15 17:42:26 brouard
461: (Module): Bug if status = -2, the loglikelihood was
462: computed as likelihood omitting the logarithm. Version O.98e
463:
464: Revision 1.118 2006/03/14 18:20:07 brouard
465: (Module): varevsij Comments added explaining the second
466: table of variances if popbased=1 .
467: (Module): Covariances of eij, ekl added, graphs fixed, new html link.
468: (Module): Function pstamp added
469: (Module): Version 0.98d
470:
471: Revision 1.117 2006/03/14 17:16:22 brouard
472: (Module): varevsij Comments added explaining the second
473: table of variances if popbased=1 .
474: (Module): Covariances of eij, ekl added, graphs fixed, new html link.
475: (Module): Function pstamp added
476: (Module): Version 0.98d
477:
478: Revision 1.116 2006/03/06 10:29:27 brouard
479: (Module): Variance-covariance wrong links and
480: varian-covariance of ej. is needed (Saito).
481:
482: Revision 1.115 2006/02/27 12:17:45 brouard
483: (Module): One freematrix added in mlikeli! 0.98c
484:
485: Revision 1.114 2006/02/26 12:57:58 brouard
486: (Module): Some improvements in processing parameter
487: filename with strsep.
488:
489: Revision 1.113 2006/02/24 14:20:24 brouard
490: (Module): Memory leaks checks with valgrind and:
491: datafile was not closed, some imatrix were not freed and on matrix
492: allocation too.
493:
494: Revision 1.112 2006/01/30 09:55:26 brouard
495: (Module): Back to gnuplot.exe instead of wgnuplot.exe
496:
497: Revision 1.111 2006/01/25 20:38:18 brouard
498: (Module): Lots of cleaning and bugs added (Gompertz)
499: (Module): Comments can be added in data file. Missing date values
500: can be a simple dot '.'.
501:
502: Revision 1.110 2006/01/25 00:51:50 brouard
503: (Module): Lots of cleaning and bugs added (Gompertz)
504:
505: Revision 1.109 2006/01/24 19:37:15 brouard
506: (Module): Comments (lines starting with a #) are allowed in data.
507:
508: Revision 1.108 2006/01/19 18:05:42 lievre
509: Gnuplot problem appeared...
510: To be fixed
511:
512: Revision 1.107 2006/01/19 16:20:37 brouard
513: Test existence of gnuplot in imach path
514:
515: Revision 1.106 2006/01/19 13:24:36 brouard
516: Some cleaning and links added in html output
517:
518: Revision 1.105 2006/01/05 20:23:19 lievre
519: *** empty log message ***
520:
521: Revision 1.104 2005/09/30 16:11:43 lievre
522: (Module): sump fixed, loop imx fixed, and simplifications.
523: (Module): If the status is missing at the last wave but we know
524: that the person is alive, then we can code his/her status as -2
525: (instead of missing=-1 in earlier versions) and his/her
526: contributions to the likelihood is 1 - Prob of dying from last
527: health status (= 1-p13= p11+p12 in the easiest case of somebody in
528: the healthy state at last known wave). Version is 0.98
529:
530: Revision 1.103 2005/09/30 15:54:49 lievre
531: (Module): sump fixed, loop imx fixed, and simplifications.
532:
533: Revision 1.102 2004/09/15 17:31:30 brouard
534: Add the possibility to read data file including tab characters.
535:
536: Revision 1.101 2004/09/15 10:38:38 brouard
537: Fix on curr_time
538:
539: Revision 1.100 2004/07/12 18:29:06 brouard
540: Add version for Mac OS X. Just define UNIX in Makefile
541:
542: Revision 1.99 2004/06/05 08:57:40 brouard
543: *** empty log message ***
544:
545: Revision 1.98 2004/05/16 15:05:56 brouard
546: New version 0.97 . First attempt to estimate force of mortality
547: directly from the data i.e. without the need of knowing the health
548: state at each age, but using a Gompertz model: log u =a + b*age .
549: This is the basic analysis of mortality and should be done before any
550: other analysis, in order to test if the mortality estimated from the
551: cross-longitudinal survey is different from the mortality estimated
552: from other sources like vital statistic data.
553:
554: The same imach parameter file can be used but the option for mle should be -3.
555:
1.133 brouard 556: Agnès, who wrote this part of the code, tried to keep most of the
1.126 brouard 557: former routines in order to include the new code within the former code.
558:
559: The output is very simple: only an estimate of the intercept and of
560: the slope with 95% confident intervals.
561:
562: Current limitations:
563: A) Even if you enter covariates, i.e. with the
564: model= V1+V2 equation for example, the programm does only estimate a unique global model without covariates.
565: B) There is no computation of Life Expectancy nor Life Table.
566:
567: Revision 1.97 2004/02/20 13:25:42 lievre
568: Version 0.96d. Population forecasting command line is (temporarily)
569: suppressed.
570:
571: Revision 1.96 2003/07/15 15:38:55 brouard
572: * imach.c (Repository): Errors in subdirf, 2, 3 while printing tmpout is
573: rewritten within the same printf. Workaround: many printfs.
574:
575: Revision 1.95 2003/07/08 07:54:34 brouard
576: * imach.c (Repository):
577: (Repository): Using imachwizard code to output a more meaningful covariance
578: matrix (cov(a12,c31) instead of numbers.
579:
580: Revision 1.94 2003/06/27 13:00:02 brouard
581: Just cleaning
582:
583: Revision 1.93 2003/06/25 16:33:55 brouard
584: (Module): On windows (cygwin) function asctime_r doesn't
585: exist so I changed back to asctime which exists.
586: (Module): Version 0.96b
587:
588: Revision 1.92 2003/06/25 16:30:45 brouard
589: (Module): On windows (cygwin) function asctime_r doesn't
590: exist so I changed back to asctime which exists.
591:
592: Revision 1.91 2003/06/25 15:30:29 brouard
593: * imach.c (Repository): Duplicated warning errors corrected.
594: (Repository): Elapsed time after each iteration is now output. It
595: helps to forecast when convergence will be reached. Elapsed time
596: is stamped in powell. We created a new html file for the graphs
597: concerning matrix of covariance. It has extension -cov.htm.
598:
599: Revision 1.90 2003/06/24 12:34:15 brouard
600: (Module): Some bugs corrected for windows. Also, when
601: mle=-1 a template is output in file "or"mypar.txt with the design
602: of the covariance matrix to be input.
603:
604: Revision 1.89 2003/06/24 12:30:52 brouard
605: (Module): Some bugs corrected for windows. Also, when
606: mle=-1 a template is output in file "or"mypar.txt with the design
607: of the covariance matrix to be input.
608:
609: Revision 1.88 2003/06/23 17:54:56 brouard
610: * 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.
611:
612: Revision 1.87 2003/06/18 12:26:01 brouard
613: Version 0.96
614:
615: Revision 1.86 2003/06/17 20:04:08 brouard
616: (Module): Change position of html and gnuplot routines and added
617: routine fileappend.
618:
619: Revision 1.85 2003/06/17 13:12:43 brouard
620: * imach.c (Repository): Check when date of death was earlier that
621: current date of interview. It may happen when the death was just
622: prior to the death. In this case, dh was negative and likelihood
623: was wrong (infinity). We still send an "Error" but patch by
624: assuming that the date of death was just one stepm after the
625: interview.
626: (Repository): Because some people have very long ID (first column)
627: we changed int to long in num[] and we added a new lvector for
628: memory allocation. But we also truncated to 8 characters (left
629: truncation)
630: (Repository): No more line truncation errors.
631:
632: Revision 1.84 2003/06/13 21:44:43 brouard
633: * imach.c (Repository): Replace "freqsummary" at a correct
634: place. It differs from routine "prevalence" which may be called
635: many times. Probs is memory consuming and must be used with
636: parcimony.
637: Version 0.95a3 (should output exactly the same maximization than 0.8a2)
638:
639: Revision 1.83 2003/06/10 13:39:11 lievre
640: *** empty log message ***
641:
642: Revision 1.82 2003/06/05 15:57:20 brouard
643: Add log in imach.c and fullversion number is now printed.
644:
645: */
646: /*
647: Interpolated Markov Chain
648:
649: Short summary of the programme:
650:
1.227 brouard 651: This program computes Healthy Life Expectancies or State-specific
652: (if states aren't health statuses) Expectancies from
653: cross-longitudinal data. Cross-longitudinal data consist in:
654:
655: -1- a first survey ("cross") where individuals from different ages
656: are interviewed on their health status or degree of disability (in
657: the case of a health survey which is our main interest)
658:
659: -2- at least a second wave of interviews ("longitudinal") which
660: measure each change (if any) in individual health status. Health
661: expectancies are computed from the time spent in each health state
662: according to a model. More health states you consider, more time is
663: necessary to reach the Maximum Likelihood of the parameters involved
664: in the model. The simplest model is the multinomial logistic model
665: where pij is the probability to be observed in state j at the second
666: wave conditional to be observed in state i at the first
667: wave. Therefore the model is: log(pij/pii)= aij + bij*age+ cij*sex +
668: etc , where 'age' is age and 'sex' is a covariate. If you want to
669: have a more complex model than "constant and age", you should modify
670: the program where the markup *Covariates have to be included here
671: again* invites you to do it. More covariates you add, slower the
1.126 brouard 672: convergence.
673:
674: The advantage of this computer programme, compared to a simple
675: multinomial logistic model, is clear when the delay between waves is not
676: identical for each individual. Also, if a individual missed an
677: intermediate interview, the information is lost, but taken into
678: account using an interpolation or extrapolation.
679:
680: hPijx is the probability to be observed in state i at age x+h
681: conditional to the observed state i at age x. The delay 'h' can be
682: split into an exact number (nh*stepm) of unobserved intermediate
683: states. This elementary transition (by month, quarter,
684: semester or year) is modelled as a multinomial logistic. The hPx
685: matrix is simply the matrix product of nh*stepm elementary matrices
686: and the contribution of each individual to the likelihood is simply
687: hPijx.
688:
689: Also this programme outputs the covariance matrix of the parameters but also
1.218 brouard 690: of the life expectancies. It also computes the period (stable) prevalence.
691:
692: Back prevalence and projections:
1.227 brouard 693:
694: - back_prevalence_limit(double *p, double **bprlim, double ageminpar,
695: double agemaxpar, double ftolpl, int *ncvyearp, double
696: dateprev1,double dateprev2, int firstpass, int lastpass, int
697: mobilavproj)
698:
699: Computes the back prevalence limit for any combination of
700: covariate values k at any age between ageminpar and agemaxpar and
701: returns it in **bprlim. In the loops,
702:
703: - **bprevalim(**bprlim, ***mobaverage, nlstate, *p, age, **oldm,
704: **savm, **dnewm, **doldm, **dsavm, ftolpl, ncvyearp, k);
705:
706: - hBijx Back Probability to be in state i at age x-h being in j at x
1.218 brouard 707: Computes for any combination of covariates k and any age between bage and fage
708: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
709: oldm=oldms;savm=savms;
1.227 brouard 710:
711: - hbxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k);
1.218 brouard 712: Computes the transition matrix starting at age 'age' over
713: 'nhstepm*hstepm*stepm' months (i.e. until
714: age (in years) age+nhstepm*hstepm*stepm/12) by multiplying
1.227 brouard 715: nhstepm*hstepm matrices.
716:
717: Returns p3mat[i][j][h] after calling
718: p3mat[i][j][h]=matprod2(newm,
719: bmij(pmmij,cov,ncovmodel,x,nlstate,prevacurrent, dnewm, doldm,
720: dsavm,ij),\ 1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath,
721: oldm);
1.226 brouard 722:
723: Important routines
724:
725: - func (or funcone), computes logit (pij) distinguishing
726: o fixed variables (single or product dummies or quantitative);
727: o varying variables by:
728: (1) wave (single, product dummies, quantitative),
729: (2) by age (can be month) age (done), age*age (done), age*Vn where Vn can be:
730: % fixed dummy (treated) or quantitative (not done because time-consuming);
731: % varying dummy (not done) or quantitative (not done);
732: - Tricode which tests the modality of dummy variables (in order to warn with wrong or empty modalities)
733: and returns the number of efficient covariates cptcoveff and modalities nbcode[Tvar[k]][1]= 0 and nbcode[Tvar[k]][2]= 1 usually.
734: - printinghtml which outputs results like life expectancy in and from a state for a combination of modalities of dummy variables
735: o There are 2*cptcoveff combinations of (0,1) for cptcoveff variables. Outputting only combinations with people, éliminating 1 1 if
736: race White (0 0), Black vs White (1 0), Hispanic (0 1) and 1 1 being meaningless.
1.218 brouard 737:
1.226 brouard 738:
739:
1.133 brouard 740: Authors: Nicolas Brouard (brouard@ined.fr) and Agnès Lièvre (lievre@ined.fr).
741: Institut national d'études démographiques, Paris.
1.126 brouard 742: This software have been partly granted by Euro-REVES, a concerted action
743: from the European Union.
744: It is copyrighted identically to a GNU software product, ie programme and
745: software can be distributed freely for non commercial use. Latest version
746: can be accessed at http://euroreves.ined.fr/imach .
747:
748: Help to debug: LD_PRELOAD=/usr/local/lib/libnjamd.so ./imach foo.imach
749: or better on gdb : set env LD_PRELOAD=/usr/local/lib/libnjamd.so
750:
751: **********************************************************************/
752: /*
753: main
754: read parameterfile
755: read datafile
756: concatwav
757: freqsummary
758: if (mle >= 1)
759: mlikeli
760: print results files
761: if mle==1
762: computes hessian
763: read end of parameter file: agemin, agemax, bage, fage, estepm
764: begin-prev-date,...
765: open gnuplot file
766: open html file
1.145 brouard 767: period (stable) prevalence | pl_nom 1-1 2-2 etc by covariate
768: for age prevalim() | #****** V1=0 V2=1 V3=1 V4=0 ******
769: | 65 1 0 2 1 3 1 4 0 0.96326 0.03674
770: freexexit2 possible for memory heap.
771:
772: h Pij x | pij_nom ficrestpij
773: # Cov Agex agex+h hpijx with i,j= 1-1 1-2 1-3 2-1 2-2 2-3
774: 1 85 85 1.00000 0.00000 0.00000 0.00000 1.00000 0.00000
775: 1 85 86 0.68299 0.22291 0.09410 0.71093 0.00000 0.28907
776:
777: 1 65 99 0.00364 0.00322 0.99314 0.00350 0.00310 0.99340
778: 1 65 100 0.00214 0.00204 0.99581 0.00206 0.00196 0.99597
779: variance of p one-step probabilities varprob | prob_nom ficresprob #One-step probabilities and stand. devi in ()
780: Standard deviation of one-step probabilities | probcor_nom ficresprobcor #One-step probabilities and correlation matrix
781: Matrix of variance covariance of one-step probabilities | probcov_nom ficresprobcov #One-step probabilities and covariance matrix
782:
1.126 brouard 783: forecasting if prevfcast==1 prevforecast call prevalence()
784: health expectancies
785: Variance-covariance of DFLE
786: prevalence()
787: movingaverage()
788: varevsij()
789: if popbased==1 varevsij(,popbased)
790: total life expectancies
791: Variance of period (stable) prevalence
792: end
793: */
794:
1.187 brouard 795: /* #define DEBUG */
796: /* #define DEBUGBRENT */
1.203 brouard 797: /* #define DEBUGLINMIN */
798: /* #define DEBUGHESS */
799: #define DEBUGHESSIJ
1.224 brouard 800: /* #define LINMINORIGINAL /\* Don't use loop on scale in linmin (accepting nan) *\/ */
1.165 brouard 801: #define POWELL /* Instead of NLOPT */
1.224 brouard 802: #define POWELLNOF3INFF1TEST /* Skip test */
1.186 brouard 803: /* #define POWELLORIGINAL /\* Don't use Directest to decide new direction but original Powell test *\/ */
804: /* #define MNBRAKORIGINAL /\* Don't use mnbrak fix *\/ */
1.126 brouard 805:
806: #include <math.h>
807: #include <stdio.h>
808: #include <stdlib.h>
809: #include <string.h>
1.226 brouard 810: #include <ctype.h>
1.159 brouard 811:
812: #ifdef _WIN32
813: #include <io.h>
1.172 brouard 814: #include <windows.h>
815: #include <tchar.h>
1.159 brouard 816: #else
1.126 brouard 817: #include <unistd.h>
1.159 brouard 818: #endif
1.126 brouard 819:
820: #include <limits.h>
821: #include <sys/types.h>
1.171 brouard 822:
823: #if defined(__GNUC__)
824: #include <sys/utsname.h> /* Doesn't work on Windows */
825: #endif
826:
1.126 brouard 827: #include <sys/stat.h>
828: #include <errno.h>
1.159 brouard 829: /* extern int errno; */
1.126 brouard 830:
1.157 brouard 831: /* #ifdef LINUX */
832: /* #include <time.h> */
833: /* #include "timeval.h" */
834: /* #else */
835: /* #include <sys/time.h> */
836: /* #endif */
837:
1.126 brouard 838: #include <time.h>
839:
1.136 brouard 840: #ifdef GSL
841: #include <gsl/gsl_errno.h>
842: #include <gsl/gsl_multimin.h>
843: #endif
844:
1.167 brouard 845:
1.162 brouard 846: #ifdef NLOPT
847: #include <nlopt.h>
848: typedef struct {
849: double (* function)(double [] );
850: } myfunc_data ;
851: #endif
852:
1.126 brouard 853: /* #include <libintl.h> */
854: /* #define _(String) gettext (String) */
855:
1.141 brouard 856: #define MAXLINE 1024 /* Was 256. Overflow with 312 with 2 states and 4 covariates. Should be ok */
1.126 brouard 857:
858: #define GNUPLOTPROGRAM "gnuplot"
859: /*#define GNUPLOTPROGRAM "..\\gp37mgw\\wgnuplot"*/
860: #define FILENAMELENGTH 132
861:
862: #define GLOCK_ERROR_NOPATH -1 /* empty path */
863: #define GLOCK_ERROR_GETCWD -2 /* cannot get cwd */
864:
1.144 brouard 865: #define MAXPARM 128 /**< Maximum number of parameters for the optimization */
866: #define NPARMAX 64 /**< (nlstate+ndeath-1)*nlstate*ncovmodel */
1.126 brouard 867:
868: #define NINTERVMAX 8
1.144 brouard 869: #define NLSTATEMAX 8 /**< Maximum number of live states (for func) */
870: #define NDEATHMAX 8 /**< Maximum number of dead states (for func) */
871: #define NCOVMAX 20 /**< Maximum number of covariates, including generated covariates V1*V2 */
1.197 brouard 872: #define codtabm(h,k) (1 & (h-1) >> (k-1))+1
1.211 brouard 873: /*#define decodtabm(h,k,cptcoveff)= (h <= (1<<cptcoveff)?(((h-1) >> (k-1)) & 1) +1 : -1)*/
874: #define decodtabm(h,k,cptcoveff) (((h-1) >> (k-1)) & 1) +1
1.126 brouard 875: #define MAXN 20000
1.144 brouard 876: #define YEARM 12. /**< Number of months per year */
1.218 brouard 877: /* #define AGESUP 130 */
878: #define AGESUP 150
879: #define AGEMARGE 25 /* Marge for agemin and agemax for(iage=agemin-AGEMARGE; iage <= agemax+3+AGEMARGE; iage++) */
1.126 brouard 880: #define AGEBASE 40
1.194 brouard 881: #define AGEOVERFLOW 1.e20
1.164 brouard 882: #define AGEGOMP 10 /**< Minimal age for Gompertz adjustment */
1.157 brouard 883: #ifdef _WIN32
884: #define DIRSEPARATOR '\\'
885: #define CHARSEPARATOR "\\"
886: #define ODIRSEPARATOR '/'
887: #else
1.126 brouard 888: #define DIRSEPARATOR '/'
889: #define CHARSEPARATOR "/"
890: #define ODIRSEPARATOR '\\'
891: #endif
892:
1.231 ! brouard 893: /* $Id: imach.c,v 1.230 2016/08/22 06:55:53 brouard Exp $ */
1.126 brouard 894: /* $State: Exp $ */
1.196 brouard 895: #include "version.h"
896: char version[]=__IMACH_VERSION__;
1.224 brouard 897: char copyright[]="February 2016,INED-EUROREVES-Institut de longevite-Japan Society for the Promotion of Science (Grant-in-Aid for Scientific Research 25293121), Intel Software 2015-2018";
1.231 ! brouard 898: char fullversion[]="$Revision: 1.230 $ $Date: 2016/08/22 06:55:53 $";
1.126 brouard 899: char strstart[80];
900: char optionfilext[10], optionfilefiname[FILENAMELENGTH];
1.130 brouard 901: int erreur=0, nberr=0, nbwarn=0; /* Error number, number of errors number of warnings */
1.187 brouard 902: int nagesqr=0, nforce=0; /* nagesqr=1 if model is including age*age, number of forces */
1.145 brouard 903: /* Number of covariates model=V2+V1+ V3*age+V2*V4 */
904: int cptcovn=0; /**< cptcovn number of covariates added in the model (excepting constant and age and age*product) */
905: int cptcovt=0; /**< cptcovt number of covariates added in the model (excepting constant and age) */
1.225 brouard 906: int cptcovs=0; /**< cptcovs number of simple covariates in the model V2+V1 =2 */
907: int cptcovsnq=0; /**< cptcovsnq number of simple covariates in the model but non quantitative V2+V1 =2 */
1.145 brouard 908: int cptcovage=0; /**< Number of covariates with age: V3*age only =1 */
909: int cptcovprodnoage=0; /**< Number of covariate products without age */
910: int cptcoveff=0; /* Total number of covariates to vary for printing results */
1.224 brouard 911: int ncoveff=0; /* Total number of effective covariates in the model */
1.225 brouard 912: int nqfveff=0; /**< nqfveff Number of Quantitative Fixed Variables Effective */
1.224 brouard 913: int ntveff=0; /**< ntveff number of effective time varying variables */
914: int nqtveff=0; /**< ntqveff number of effective time varying quantitative variables */
1.145 brouard 915: int cptcov=0; /* Working variable */
1.218 brouard 916: int ncovcombmax=NCOVMAX; /* Maximum calculated number of covariate combination = pow(2, cptcoveff) */
1.126 brouard 917: int npar=NPARMAX;
918: int nlstate=2; /* Number of live states */
919: int ndeath=1; /* Number of dead states */
1.130 brouard 920: int ncovmodel=0, ncovcol=0; /* Total number of covariables including constant a12*1 +b12*x ncovmodel=2 */
1.223 brouard 921: int nqv=0, ntv=0, nqtv=0; /* Total number of quantitative variables, time variable (dummy), quantitative and time variable */
1.126 brouard 922: int popbased=0;
923:
924: int *wav; /* Number of waves for this individuual 0 is possible */
1.130 brouard 925: int maxwav=0; /* Maxim number of waves */
926: int jmin=0, jmax=0; /* min, max spacing between 2 waves */
927: int ijmin=0, ijmax=0; /* Individuals having jmin and jmax */
928: int gipmx=0, gsw=0; /* Global variables on the number of contributions
1.126 brouard 929: to the likelihood and the sum of weights (done by funcone)*/
1.130 brouard 930: int mle=1, weightopt=0;
1.126 brouard 931: int **mw; /* mw[mi][i] is number of the mi wave for this individual */
932: int **dh; /* dh[mi][i] is number of steps between mi,mi+1 for this individual */
933: int **bh; /* bh[mi][i] is the bias (+ or -) for this individual if the delay between
934: * wave mi and wave mi+1 is not an exact multiple of stepm. */
1.162 brouard 935: int countcallfunc=0; /* Count the number of calls to func */
1.230 brouard 936: int selected(int kvar); /* Is covariate kvar selected for printing results */
937:
1.130 brouard 938: double jmean=1; /* Mean space between 2 waves */
1.145 brouard 939: double **matprod2(); /* test */
1.126 brouard 940: double **oldm, **newm, **savm; /* Working pointers to matrices */
941: double **oldms, **newms, **savms; /* Fixed working pointers to matrices */
1.218 brouard 942: double **ddnewms, **ddoldms, **ddsavms; /* for freeing later */
943:
1.136 brouard 944: /*FILE *fic ; */ /* Used in readdata only */
1.217 brouard 945: FILE *ficpar, *ficparo,*ficres, *ficresp, *ficresphtm, *ficresphtmfr, *ficrespl, *ficresplb,*ficrespij, *ficrespijb, *ficrest,*ficresf, *ficresfb,*ficrespop;
1.126 brouard 946: FILE *ficlog, *ficrespow;
1.130 brouard 947: int globpr=0; /* Global variable for printing or not */
1.126 brouard 948: double fretone; /* Only one call to likelihood */
1.130 brouard 949: long ipmx=0; /* Number of contributions */
1.126 brouard 950: double sw; /* Sum of weights */
951: char filerespow[FILENAMELENGTH];
952: char fileresilk[FILENAMELENGTH]; /* File of individual contributions to the likelihood */
953: FILE *ficresilk;
954: FILE *ficgp,*ficresprob,*ficpop, *ficresprobcov, *ficresprobcor;
955: FILE *ficresprobmorprev;
956: FILE *fichtm, *fichtmcov; /* Html File */
957: FILE *ficreseij;
958: char filerese[FILENAMELENGTH];
959: FILE *ficresstdeij;
960: char fileresstde[FILENAMELENGTH];
961: FILE *ficrescveij;
962: char filerescve[FILENAMELENGTH];
963: FILE *ficresvij;
964: char fileresv[FILENAMELENGTH];
965: FILE *ficresvpl;
966: char fileresvpl[FILENAMELENGTH];
967: char title[MAXLINE];
1.217 brouard 968: char optionfile[FILENAMELENGTH], datafile[FILENAMELENGTH], filerespl[FILENAMELENGTH], fileresplb[FILENAMELENGTH];
1.126 brouard 969: char plotcmd[FILENAMELENGTH], pplotcmd[FILENAMELENGTH];
970: char tmpout[FILENAMELENGTH], tmpout2[FILENAMELENGTH];
971: char command[FILENAMELENGTH];
972: int outcmd=0;
973:
1.217 brouard 974: char fileres[FILENAMELENGTH], filerespij[FILENAMELENGTH], filerespijb[FILENAMELENGTH], filereso[FILENAMELENGTH], rfileres[FILENAMELENGTH];
1.202 brouard 975: char fileresu[FILENAMELENGTH]; /* fileres without r in front */
1.126 brouard 976: char filelog[FILENAMELENGTH]; /* Log file */
977: char filerest[FILENAMELENGTH];
978: char fileregp[FILENAMELENGTH];
979: char popfile[FILENAMELENGTH];
980:
981: char optionfilegnuplot[FILENAMELENGTH], optionfilehtm[FILENAMELENGTH], optionfilehtmcov[FILENAMELENGTH] ;
982:
1.157 brouard 983: /* struct timeval start_time, end_time, curr_time, last_time, forecast_time; */
984: /* struct timezone tzp; */
985: /* extern int gettimeofday(); */
986: struct tm tml, *gmtime(), *localtime();
987:
988: extern time_t time();
989:
990: struct tm start_time, end_time, curr_time, last_time, forecast_time;
991: time_t rstart_time, rend_time, rcurr_time, rlast_time, rforecast_time; /* raw time */
992: struct tm tm;
993:
1.126 brouard 994: char strcurr[80], strfor[80];
995:
996: char *endptr;
997: long lval;
998: double dval;
999:
1000: #define NR_END 1
1001: #define FREE_ARG char*
1002: #define FTOL 1.0e-10
1003:
1004: #define NRANSI
1005: #define ITMAX 200
1006:
1007: #define TOL 2.0e-4
1008:
1009: #define CGOLD 0.3819660
1010: #define ZEPS 1.0e-10
1011: #define SHFT(a,b,c,d) (a)=(b);(b)=(c);(c)=(d);
1012:
1013: #define GOLD 1.618034
1014: #define GLIMIT 100.0
1015: #define TINY 1.0e-20
1016:
1017: static double maxarg1,maxarg2;
1018: #define FMAX(a,b) (maxarg1=(a),maxarg2=(b),(maxarg1)>(maxarg2)? (maxarg1):(maxarg2))
1019: #define FMIN(a,b) (maxarg1=(a),maxarg2=(b),(maxarg1)<(maxarg2)? (maxarg1):(maxarg2))
1020:
1021: #define SIGN(a,b) ((b)>0.0 ? fabs(a) : -fabs(a))
1022: #define rint(a) floor(a+0.5)
1.166 brouard 1023: /* http://www.thphys.uni-heidelberg.de/~robbers/cmbeasy/doc/html/myutils_8h-source.html */
1.183 brouard 1024: #define mytinydouble 1.0e-16
1.166 brouard 1025: /* #define DEQUAL(a,b) (fabs((a)-(b))<mytinydouble) */
1026: /* http://www.thphys.uni-heidelberg.de/~robbers/cmbeasy/doc/html/mynrutils_8h-source.html */
1027: /* static double dsqrarg; */
1028: /* #define DSQR(a) (DEQUAL((dsqrarg=(a)),0.0) ? 0.0 : dsqrarg*dsqrarg) */
1.126 brouard 1029: static double sqrarg;
1030: #define SQR(a) ((sqrarg=(a)) == 0.0 ? 0.0 :sqrarg*sqrarg)
1031: #define SWAP(a,b) {temp=(a);(a)=(b);(b)=temp;}
1032: int agegomp= AGEGOMP;
1033:
1034: int imx;
1035: int stepm=1;
1036: /* Stepm, step in month: minimum step interpolation*/
1037:
1038: int estepm;
1039: /* Estepm, step in month to interpolate survival function in order to approximate Life Expectancy*/
1040:
1041: int m,nb;
1042: long *num;
1.197 brouard 1043: int firstpass=0, lastpass=4,*cod, *cens;
1.192 brouard 1044: int *ncodemax; /* ncodemax[j]= Number of modalities of the j th
1045: covariate for which somebody answered excluding
1046: undefined. Usually 2: 0 and 1. */
1047: int *ncodemaxwundef; /* ncodemax[j]= Number of modalities of the j th
1048: covariate for which somebody answered including
1049: undefined. Usually 3: -1, 0 and 1. */
1.126 brouard 1050: double **agev,*moisnais, *annais, *moisdc, *andc,**mint, **anint;
1.218 brouard 1051: double **pmmij, ***probs; /* Global pointer */
1.219 brouard 1052: double ***mobaverage, ***mobaverages; /* New global variable */
1.126 brouard 1053: double *ageexmed,*agecens;
1054: double dateintmean=0;
1055:
1056: double *weight;
1057: int **s; /* Status */
1.141 brouard 1058: double *agedc;
1.145 brouard 1059: double **covar; /**< covar[j,i], value of jth covariate for individual i,
1.141 brouard 1060: * covar=matrix(0,NCOVMAX,1,n);
1.187 brouard 1061: * cov[Tage[kk]+2]=covar[Tvar[Tage[kk]]][i]*age; */
1.225 brouard 1062: double **coqvar; /* Fixed quantitative covariate iqv */
1063: double ***cotvar; /* Time varying covariate itv */
1064: double ***cotqvar; /* Time varying quantitative covariate itqv */
1.141 brouard 1065: double idx;
1066: int **nbcode, *Tvar; /**< model=V2 => Tvar[1]= 2 */
1.231 ! brouard 1067: int *TvarFD; /**< TvarFD[1]=V1 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
! 1068: int *TvarFDind; /* TvarFDind[1]=9 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
! 1069: int *TvarFQ; /* TvarFQ[1]=V2 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ /* Only simple fixed quantitative variable */
! 1070: int *TvarFQind; /* TvarFQind[1]=6 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ /* Only simple fixed quantitative variable */
! 1071: int *TvarVD; /* TvarVD[1]=V5 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ /* Only simple fixed quantitative variable */
! 1072: int *TvarVDind; /* TvarVDind[1]=1 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ /* Only simple fixed quantitative variable */
! 1073: 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 */
! 1074: 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 */
! 1075:
1.230 brouard 1076: int *Tvarsel; /**< Selected covariates for output */
1077: double *Tvalsel; /**< Selected modality value of covariate for output */
1.226 brouard 1078: int *Typevar; /**< 0 for simple covariate (dummy, quantitative, fixed or varying), 1 for age product, 2 for product */
1.227 brouard 1079: int *Fixed; /** Fixed[k] 0=fixed, 1 varying, 2 fixed with age product, 3 varying with age product */
1080: int *Dummy; /** Dummy[k] 0=dummy (0 1), 1 quantitative (single or product without age), 2 dummy with age product, 3 quant with age product */
1.197 brouard 1081: int *Tage;
1.227 brouard 1082: int anyvaryingduminmodel=0; /**< Any varying dummy in Model=1 yes, 0 no, to avoid a loop on waves in freq */
1.228 brouard 1083: int *Tmodelind; /** Tmodelind[Tvaraff[3]]=9 for V1 position,Tvaraff[1]@9={4, 3, 1, 0, 0, 0, 0, 0, 0}, model=V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1*/
1.230 brouard 1084: 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*/
1085: int *TmodelInvQind; /** Tmodelqind[1]=1 for V5(quantitative varying) position,Tvaraff[1]@9={4, 3, 1, 0, 0, 0, 0, 0, 0}, model=V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
1.145 brouard 1086: int *Ndum; /** Freq of modality (tricode */
1.200 brouard 1087: /* int **codtab;*/ /**< codtab=imatrix(1,100,1,10); */
1.227 brouard 1088: int **Tvard;
1089: int *Tprod;/**< Gives the k position of the k1 product */
1090: int *Tposprod; /**< Gives the k1 product from the k position */
1091: /* Tprod[k1=1]=3(=V1*V4) for V2+V1+V1*V4+age*V3
1092: if V2+V1+V1*V4+age*V3+V3*V2 TProd[k1=2]=5 (V3*V2)
1093: Tposprod[k]=k1 , Tposprod[3]=1, Tposprod[5]=2
1094: */
1095: int cptcovprod, *Tvaraff, *invalidvarcomb;
1.126 brouard 1096: double *lsurv, *lpop, *tpop;
1097:
1.231 ! brouard 1098: #define FD 1; /* Fixed dummy covariate */
! 1099: #define FQ 2; /* Fixed quantitative covariate */
! 1100: #define FP 3; /* Fixed product covariate */
! 1101: #define FPDD 7; /* Fixed product dummy*dummy covariate */
! 1102: #define FPDQ 8; /* Fixed product dummy*quantitative covariate */
! 1103: #define FPQQ 9; /* Fixed product quantitative*quantitative covariate */
! 1104: #define VD 10; /* Varying dummy covariate */
! 1105: #define VQ 11; /* Varying quantitative covariate */
! 1106: #define VP 12; /* Varying product covariate */
! 1107: #define VPDD 13; /* Varying product dummy*dummy covariate */
! 1108: #define VPDQ 14; /* Varying product dummy*quantitative covariate */
! 1109: #define VPQQ 15; /* Varying product quantitative*quantitative covariate */
! 1110: #define APFD 16; /* Age product * fixed dummy covariate */
! 1111: #define APFQ 17; /* Age product * fixed quantitative covariate */
! 1112: #define APVD 18; /* Age product * varying dummy covariate */
! 1113: #define APVQ 19; /* Age product * varying quantitative covariate */
! 1114:
! 1115: #define FTYPE 1; /* Fixed covariate */
! 1116: #define VTYPE 2; /* Varying covariate (loop in wave) */
! 1117: #define ATYPE 2; /* Age product covariate (loop in dh within wave)*/
! 1118:
! 1119: struct kmodel{
! 1120: int maintype; /* main type */
! 1121: int subtype; /* subtype */
! 1122: };
! 1123: struct kmodel modell[NCOVMAX];
! 1124:
1.143 brouard 1125: double ftol=FTOL; /**< Tolerance for computing Max Likelihood */
1126: double ftolhess; /**< Tolerance for computing hessian */
1.126 brouard 1127:
1128: /**************** split *************************/
1129: static int split( char *path, char *dirc, char *name, char *ext, char *finame )
1130: {
1131: /* From a file name with (full) path (either Unix or Windows) we extract the directory (dirc)
1132: the name of the file (name), its extension only (ext) and its first part of the name (finame)
1133: */
1134: char *ss; /* pointer */
1.186 brouard 1135: int l1=0, l2=0; /* length counters */
1.126 brouard 1136:
1137: l1 = strlen(path ); /* length of path */
1138: if ( l1 == 0 ) return( GLOCK_ERROR_NOPATH );
1139: ss= strrchr( path, DIRSEPARATOR ); /* find last / */
1140: if ( ss == NULL ) { /* no directory, so determine current directory */
1141: strcpy( name, path ); /* we got the fullname name because no directory */
1142: /*if(strrchr(path, ODIRSEPARATOR )==NULL)
1143: printf("Warning you should use %s as a separator\n",DIRSEPARATOR);*/
1144: /* get current working directory */
1145: /* extern char* getcwd ( char *buf , int len);*/
1.184 brouard 1146: #ifdef WIN32
1147: if (_getcwd( dirc, FILENAME_MAX ) == NULL ) {
1148: #else
1149: if (getcwd(dirc, FILENAME_MAX) == NULL) {
1150: #endif
1.126 brouard 1151: return( GLOCK_ERROR_GETCWD );
1152: }
1153: /* got dirc from getcwd*/
1154: printf(" DIRC = %s \n",dirc);
1.205 brouard 1155: } else { /* strip directory from path */
1.126 brouard 1156: ss++; /* after this, the filename */
1157: l2 = strlen( ss ); /* length of filename */
1158: if ( l2 == 0 ) return( GLOCK_ERROR_NOPATH );
1159: strcpy( name, ss ); /* save file name */
1160: strncpy( dirc, path, l1 - l2 ); /* now the directory */
1.186 brouard 1161: dirc[l1-l2] = '\0'; /* add zero */
1.126 brouard 1162: printf(" DIRC2 = %s \n",dirc);
1163: }
1164: /* We add a separator at the end of dirc if not exists */
1165: l1 = strlen( dirc ); /* length of directory */
1166: if( dirc[l1-1] != DIRSEPARATOR ){
1167: dirc[l1] = DIRSEPARATOR;
1168: dirc[l1+1] = 0;
1169: printf(" DIRC3 = %s \n",dirc);
1170: }
1171: ss = strrchr( name, '.' ); /* find last / */
1172: if (ss >0){
1173: ss++;
1174: strcpy(ext,ss); /* save extension */
1175: l1= strlen( name);
1176: l2= strlen(ss)+1;
1177: strncpy( finame, name, l1-l2);
1178: finame[l1-l2]= 0;
1179: }
1180:
1181: return( 0 ); /* we're done */
1182: }
1183:
1184:
1185: /******************************************/
1186:
1187: void replace_back_to_slash(char *s, char*t)
1188: {
1189: int i;
1190: int lg=0;
1191: i=0;
1192: lg=strlen(t);
1193: for(i=0; i<= lg; i++) {
1194: (s[i] = t[i]);
1195: if (t[i]== '\\') s[i]='/';
1196: }
1197: }
1198:
1.132 brouard 1199: char *trimbb(char *out, char *in)
1.137 brouard 1200: { /* Trim multiple blanks in line but keeps first blanks if line starts with blanks */
1.132 brouard 1201: char *s;
1202: s=out;
1203: while (*in != '\0'){
1.137 brouard 1204: while( *in == ' ' && *(in+1) == ' '){ /* && *(in+1) != '\0'){*/
1.132 brouard 1205: in++;
1206: }
1207: *out++ = *in++;
1208: }
1209: *out='\0';
1210: return s;
1211: }
1212:
1.187 brouard 1213: /* char *substrchaine(char *out, char *in, char *chain) */
1214: /* { */
1215: /* /\* Substract chain 'chain' from 'in', return and output 'out' *\/ */
1216: /* char *s, *t; */
1217: /* t=in;s=out; */
1218: /* while ((*in != *chain) && (*in != '\0')){ */
1219: /* *out++ = *in++; */
1220: /* } */
1221:
1222: /* /\* *in matches *chain *\/ */
1223: /* while ((*in++ == *chain++) && (*in != '\0')){ */
1224: /* printf("*in = %c, *out= %c *chain= %c \n", *in, *out, *chain); */
1225: /* } */
1226: /* in--; chain--; */
1227: /* while ( (*in != '\0')){ */
1228: /* printf("Bef *in = %c, *out= %c *chain= %c \n", *in, *out, *chain); */
1229: /* *out++ = *in++; */
1230: /* printf("Aft *in = %c, *out= %c *chain= %c \n", *in, *out, *chain); */
1231: /* } */
1232: /* *out='\0'; */
1233: /* out=s; */
1234: /* return out; */
1235: /* } */
1236: char *substrchaine(char *out, char *in, char *chain)
1237: {
1238: /* Substract chain 'chain' from 'in', return and output 'out' */
1239: /* in="V1+V1*age+age*age+V2", chain="age*age" */
1240:
1241: char *strloc;
1242:
1243: strcpy (out, in);
1244: strloc = strstr(out, chain); /* strloc points to out at age*age+V2 */
1245: printf("Bef strloc=%s chain=%s out=%s \n", strloc, chain, out);
1246: if(strloc != NULL){
1247: /* will affect out */ /* strloc+strlenc(chain)=+V2 */ /* Will also work in Unicode */
1248: memmove(strloc,strloc+strlen(chain), strlen(strloc+strlen(chain))+1);
1249: /* strcpy (strloc, strloc +strlen(chain));*/
1250: }
1251: printf("Aft strloc=%s chain=%s in=%s out=%s \n", strloc, chain, in, out);
1252: return out;
1253: }
1254:
1255:
1.145 brouard 1256: char *cutl(char *blocc, char *alocc, char *in, char occ)
1257: {
1.187 brouard 1258: /* cuts string in into blocc and alocc where blocc ends before FIRST occurence of char 'occ'
1.145 brouard 1259: and alocc starts after first occurence of char 'occ' : ex cutv(blocc,alocc,"abcdef2ghi2j",'2')
1.187 brouard 1260: gives blocc="abcdef" and alocc="ghi2j".
1.145 brouard 1261: If occ is not found blocc is null and alocc is equal to in. Returns blocc
1262: */
1.160 brouard 1263: char *s, *t;
1.145 brouard 1264: t=in;s=in;
1265: while ((*in != occ) && (*in != '\0')){
1266: *alocc++ = *in++;
1267: }
1268: if( *in == occ){
1269: *(alocc)='\0';
1270: s=++in;
1271: }
1272:
1273: if (s == t) {/* occ not found */
1274: *(alocc-(in-s))='\0';
1275: in=s;
1276: }
1277: while ( *in != '\0'){
1278: *blocc++ = *in++;
1279: }
1280:
1281: *blocc='\0';
1282: return t;
1283: }
1.137 brouard 1284: char *cutv(char *blocc, char *alocc, char *in, char occ)
1285: {
1.187 brouard 1286: /* cuts string in into blocc and alocc where blocc ends before LAST occurence of char 'occ'
1.137 brouard 1287: and alocc starts after last occurence of char 'occ' : ex cutv(blocc,alocc,"abcdef2ghi2j",'2')
1288: gives blocc="abcdef2ghi" and alocc="j".
1289: If occ is not found blocc is null and alocc is equal to in. Returns alocc
1290: */
1291: char *s, *t;
1292: t=in;s=in;
1293: while (*in != '\0'){
1294: while( *in == occ){
1295: *blocc++ = *in++;
1296: s=in;
1297: }
1298: *blocc++ = *in++;
1299: }
1300: if (s == t) /* occ not found */
1301: *(blocc-(in-s))='\0';
1302: else
1303: *(blocc-(in-s)-1)='\0';
1304: in=s;
1305: while ( *in != '\0'){
1306: *alocc++ = *in++;
1307: }
1308:
1309: *alocc='\0';
1310: return s;
1311: }
1312:
1.126 brouard 1313: int nbocc(char *s, char occ)
1314: {
1315: int i,j=0;
1316: int lg=20;
1317: i=0;
1318: lg=strlen(s);
1319: for(i=0; i<= lg; i++) {
1320: if (s[i] == occ ) j++;
1321: }
1322: return j;
1323: }
1324:
1.137 brouard 1325: /* void cutv(char *u,char *v, char*t, char occ) */
1326: /* { */
1327: /* /\* cuts string t into u and v where u ends before last occurence of char 'occ' */
1328: /* and v starts after last occurence of char 'occ' : ex cutv(u,v,"abcdef2ghi2j",'2') */
1329: /* gives u="abcdef2ghi" and v="j" *\/ */
1330: /* int i,lg,j,p=0; */
1331: /* i=0; */
1332: /* lg=strlen(t); */
1333: /* for(j=0; j<=lg-1; j++) { */
1334: /* if((t[j]!= occ) && (t[j+1]== occ)) p=j+1; */
1335: /* } */
1.126 brouard 1336:
1.137 brouard 1337: /* for(j=0; j<p; j++) { */
1338: /* (u[j] = t[j]); */
1339: /* } */
1340: /* u[p]='\0'; */
1.126 brouard 1341:
1.137 brouard 1342: /* for(j=0; j<= lg; j++) { */
1343: /* if (j>=(p+1))(v[j-p-1] = t[j]); */
1344: /* } */
1345: /* } */
1.126 brouard 1346:
1.160 brouard 1347: #ifdef _WIN32
1348: char * strsep(char **pp, const char *delim)
1349: {
1350: char *p, *q;
1351:
1352: if ((p = *pp) == NULL)
1353: return 0;
1354: if ((q = strpbrk (p, delim)) != NULL)
1355: {
1356: *pp = q + 1;
1357: *q = '\0';
1358: }
1359: else
1360: *pp = 0;
1361: return p;
1362: }
1363: #endif
1364:
1.126 brouard 1365: /********************** nrerror ********************/
1366:
1367: void nrerror(char error_text[])
1368: {
1369: fprintf(stderr,"ERREUR ...\n");
1370: fprintf(stderr,"%s\n",error_text);
1371: exit(EXIT_FAILURE);
1372: }
1373: /*********************** vector *******************/
1374: double *vector(int nl, int nh)
1375: {
1376: double *v;
1377: v=(double *) malloc((size_t)((nh-nl+1+NR_END)*sizeof(double)));
1378: if (!v) nrerror("allocation failure in vector");
1379: return v-nl+NR_END;
1380: }
1381:
1382: /************************ free vector ******************/
1383: void free_vector(double*v, int nl, int nh)
1384: {
1385: free((FREE_ARG)(v+nl-NR_END));
1386: }
1387:
1388: /************************ivector *******************************/
1389: int *ivector(long nl,long nh)
1390: {
1391: int *v;
1392: v=(int *) malloc((size_t)((nh-nl+1+NR_END)*sizeof(int)));
1393: if (!v) nrerror("allocation failure in ivector");
1394: return v-nl+NR_END;
1395: }
1396:
1397: /******************free ivector **************************/
1398: void free_ivector(int *v, long nl, long nh)
1399: {
1400: free((FREE_ARG)(v+nl-NR_END));
1401: }
1402:
1403: /************************lvector *******************************/
1404: long *lvector(long nl,long nh)
1405: {
1406: long *v;
1407: v=(long *) malloc((size_t)((nh-nl+1+NR_END)*sizeof(long)));
1408: if (!v) nrerror("allocation failure in ivector");
1409: return v-nl+NR_END;
1410: }
1411:
1412: /******************free lvector **************************/
1413: void free_lvector(long *v, long nl, long nh)
1414: {
1415: free((FREE_ARG)(v+nl-NR_END));
1416: }
1417:
1418: /******************* imatrix *******************************/
1419: int **imatrix(long nrl, long nrh, long ncl, long nch)
1420: /* allocate a int matrix with subscript range m[nrl..nrh][ncl..nch] */
1421: {
1422: long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
1423: int **m;
1424:
1425: /* allocate pointers to rows */
1426: m=(int **) malloc((size_t)((nrow+NR_END)*sizeof(int*)));
1427: if (!m) nrerror("allocation failure 1 in matrix()");
1428: m += NR_END;
1429: m -= nrl;
1430:
1431:
1432: /* allocate rows and set pointers to them */
1433: m[nrl]=(int *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(int)));
1434: if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
1435: m[nrl] += NR_END;
1436: m[nrl] -= ncl;
1437:
1438: for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
1439:
1440: /* return pointer to array of pointers to rows */
1441: return m;
1442: }
1443:
1444: /****************** free_imatrix *************************/
1445: void free_imatrix(m,nrl,nrh,ncl,nch)
1446: int **m;
1447: long nch,ncl,nrh,nrl;
1448: /* free an int matrix allocated by imatrix() */
1449: {
1450: free((FREE_ARG) (m[nrl]+ncl-NR_END));
1451: free((FREE_ARG) (m+nrl-NR_END));
1452: }
1453:
1454: /******************* matrix *******************************/
1455: double **matrix(long nrl, long nrh, long ncl, long nch)
1456: {
1457: long i, nrow=nrh-nrl+1, ncol=nch-ncl+1;
1458: double **m;
1459:
1460: m=(double **) malloc((size_t)((nrow+NR_END)*sizeof(double*)));
1461: if (!m) nrerror("allocation failure 1 in matrix()");
1462: m += NR_END;
1463: m -= nrl;
1464:
1465: m[nrl]=(double *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(double)));
1466: if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
1467: m[nrl] += NR_END;
1468: m[nrl] -= ncl;
1469:
1470: for (i=nrl+1; i<=nrh; i++) m[i]=m[i-1]+ncol;
1471: return m;
1.145 brouard 1472: /* print *(*(m+1)+70) or print m[1][70]; print m+1 or print &(m[1]) or &(m[1][0])
1473: m[i] = address of ith row of the table. &(m[i]) is its value which is another adress
1474: that of m[i][0]. In order to get the value p m[i][0] but it is unitialized.
1.126 brouard 1475: */
1476: }
1477:
1478: /*************************free matrix ************************/
1479: void free_matrix(double **m, long nrl, long nrh, long ncl, long nch)
1480: {
1481: free((FREE_ARG)(m[nrl]+ncl-NR_END));
1482: free((FREE_ARG)(m+nrl-NR_END));
1483: }
1484:
1485: /******************* ma3x *******************************/
1486: double ***ma3x(long nrl, long nrh, long ncl, long nch, long nll, long nlh)
1487: {
1488: long i, j, nrow=nrh-nrl+1, ncol=nch-ncl+1, nlay=nlh-nll+1;
1489: double ***m;
1490:
1491: m=(double ***) malloc((size_t)((nrow+NR_END)*sizeof(double*)));
1492: if (!m) nrerror("allocation failure 1 in matrix()");
1493: m += NR_END;
1494: m -= nrl;
1495:
1496: m[nrl]=(double **) malloc((size_t)((nrow*ncol+NR_END)*sizeof(double)));
1497: if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
1498: m[nrl] += NR_END;
1499: m[nrl] -= ncl;
1500:
1501: for (i=nrl+1; i<=nrh; i++) m[i]=m[i-1]+ncol;
1502:
1503: m[nrl][ncl]=(double *) malloc((size_t)((nrow*ncol*nlay+NR_END)*sizeof(double)));
1504: if (!m[nrl][ncl]) nrerror("allocation failure 3 in matrix()");
1505: m[nrl][ncl] += NR_END;
1506: m[nrl][ncl] -= nll;
1507: for (j=ncl+1; j<=nch; j++)
1508: m[nrl][j]=m[nrl][j-1]+nlay;
1509:
1510: for (i=nrl+1; i<=nrh; i++) {
1511: m[i][ncl]=m[i-1l][ncl]+ncol*nlay;
1512: for (j=ncl+1; j<=nch; j++)
1513: m[i][j]=m[i][j-1]+nlay;
1514: }
1515: return m;
1516: /* gdb: p *(m+1) <=> p m[1] and p (m+1) <=> p (m+1) <=> p &(m[1])
1517: &(m[i][j][k]) <=> *((*(m+i) + j)+k)
1518: */
1519: }
1520:
1521: /*************************free ma3x ************************/
1522: void free_ma3x(double ***m, long nrl, long nrh, long ncl, long nch,long nll, long nlh)
1523: {
1524: free((FREE_ARG)(m[nrl][ncl]+ nll-NR_END));
1525: free((FREE_ARG)(m[nrl]+ncl-NR_END));
1526: free((FREE_ARG)(m+nrl-NR_END));
1527: }
1528:
1529: /*************** function subdirf ***********/
1530: char *subdirf(char fileres[])
1531: {
1532: /* Caution optionfilefiname is hidden */
1533: strcpy(tmpout,optionfilefiname);
1534: strcat(tmpout,"/"); /* Add to the right */
1535: strcat(tmpout,fileres);
1536: return tmpout;
1537: }
1538:
1539: /*************** function subdirf2 ***********/
1540: char *subdirf2(char fileres[], char *preop)
1541: {
1542:
1543: /* Caution optionfilefiname is hidden */
1544: strcpy(tmpout,optionfilefiname);
1545: strcat(tmpout,"/");
1546: strcat(tmpout,preop);
1547: strcat(tmpout,fileres);
1548: return tmpout;
1549: }
1550:
1551: /*************** function subdirf3 ***********/
1552: char *subdirf3(char fileres[], char *preop, char *preop2)
1553: {
1554:
1555: /* Caution optionfilefiname is hidden */
1556: strcpy(tmpout,optionfilefiname);
1557: strcat(tmpout,"/");
1558: strcat(tmpout,preop);
1559: strcat(tmpout,preop2);
1560: strcat(tmpout,fileres);
1561: return tmpout;
1562: }
1.213 brouard 1563:
1564: /*************** function subdirfext ***********/
1565: char *subdirfext(char fileres[], char *preop, char *postop)
1566: {
1567:
1568: strcpy(tmpout,preop);
1569: strcat(tmpout,fileres);
1570: strcat(tmpout,postop);
1571: return tmpout;
1572: }
1.126 brouard 1573:
1.213 brouard 1574: /*************** function subdirfext3 ***********/
1575: char *subdirfext3(char fileres[], char *preop, char *postop)
1576: {
1577:
1578: /* Caution optionfilefiname is hidden */
1579: strcpy(tmpout,optionfilefiname);
1580: strcat(tmpout,"/");
1581: strcat(tmpout,preop);
1582: strcat(tmpout,fileres);
1583: strcat(tmpout,postop);
1584: return tmpout;
1585: }
1586:
1.162 brouard 1587: char *asc_diff_time(long time_sec, char ascdiff[])
1588: {
1589: long sec_left, days, hours, minutes;
1590: days = (time_sec) / (60*60*24);
1591: sec_left = (time_sec) % (60*60*24);
1592: hours = (sec_left) / (60*60) ;
1593: sec_left = (sec_left) %(60*60);
1594: minutes = (sec_left) /60;
1595: sec_left = (sec_left) % (60);
1596: sprintf(ascdiff,"%ld day(s) %ld hour(s) %ld minute(s) %ld second(s)",days, hours, minutes, sec_left);
1597: return ascdiff;
1598: }
1599:
1.126 brouard 1600: /***************** f1dim *************************/
1601: extern int ncom;
1602: extern double *pcom,*xicom;
1603: extern double (*nrfunc)(double []);
1604:
1605: double f1dim(double x)
1606: {
1607: int j;
1608: double f;
1609: double *xt;
1610:
1611: xt=vector(1,ncom);
1612: for (j=1;j<=ncom;j++) xt[j]=pcom[j]+x*xicom[j];
1613: f=(*nrfunc)(xt);
1614: free_vector(xt,1,ncom);
1615: return f;
1616: }
1617:
1618: /*****************brent *************************/
1619: double brent(double ax, double bx, double cx, double (*f)(double), double tol, double *xmin)
1.187 brouard 1620: {
1621: /* Given a function f, and given a bracketing triplet of abscissas ax, bx, cx (such that bx is
1622: * between ax and cx, and f(bx) is less than both f(ax) and f(cx) ), this routine isolates
1623: * the minimum to a fractional precision of about tol using Brent’s method. The abscissa of
1624: * the minimum is returned as xmin, and the minimum function value is returned as brent , the
1625: * returned function value.
1626: */
1.126 brouard 1627: int iter;
1628: double a,b,d,etemp;
1.159 brouard 1629: double fu=0,fv,fw,fx;
1.164 brouard 1630: double ftemp=0.;
1.126 brouard 1631: double p,q,r,tol1,tol2,u,v,w,x,xm;
1632: double e=0.0;
1633:
1634: a=(ax < cx ? ax : cx);
1635: b=(ax > cx ? ax : cx);
1636: x=w=v=bx;
1637: fw=fv=fx=(*f)(x);
1638: for (iter=1;iter<=ITMAX;iter++) {
1639: xm=0.5*(a+b);
1640: tol2=2.0*(tol1=tol*fabs(x)+ZEPS);
1641: /* if (2.0*fabs(fp-(*fret)) <= ftol*(fabs(fp)+fabs(*fret)))*/
1642: printf(".");fflush(stdout);
1643: fprintf(ficlog,".");fflush(ficlog);
1.162 brouard 1644: #ifdef DEBUGBRENT
1.126 brouard 1645: 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);
1646: 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);
1647: /* if ((fabs(x-xm) <= (tol2-0.5*(b-a)))||(2.0*fabs(fu-ftemp) <= ftol*1.e-2*(fabs(fu)+fabs(ftemp)))) { */
1648: #endif
1649: if (fabs(x-xm) <= (tol2-0.5*(b-a))){
1650: *xmin=x;
1651: return fx;
1652: }
1653: ftemp=fu;
1654: if (fabs(e) > tol1) {
1655: r=(x-w)*(fx-fv);
1656: q=(x-v)*(fx-fw);
1657: p=(x-v)*q-(x-w)*r;
1658: q=2.0*(q-r);
1659: if (q > 0.0) p = -p;
1660: q=fabs(q);
1661: etemp=e;
1662: e=d;
1663: if (fabs(p) >= fabs(0.5*q*etemp) || p <= q*(a-x) || p >= q*(b-x))
1.224 brouard 1664: d=CGOLD*(e=(x >= xm ? a-x : b-x));
1.126 brouard 1665: else {
1.224 brouard 1666: d=p/q;
1667: u=x+d;
1668: if (u-a < tol2 || b-u < tol2)
1669: d=SIGN(tol1,xm-x);
1.126 brouard 1670: }
1671: } else {
1672: d=CGOLD*(e=(x >= xm ? a-x : b-x));
1673: }
1674: u=(fabs(d) >= tol1 ? x+d : x+SIGN(tol1,d));
1675: fu=(*f)(u);
1676: if (fu <= fx) {
1677: if (u >= x) a=x; else b=x;
1678: SHFT(v,w,x,u)
1.183 brouard 1679: SHFT(fv,fw,fx,fu)
1680: } else {
1681: if (u < x) a=u; else b=u;
1682: if (fu <= fw || w == x) {
1.224 brouard 1683: v=w;
1684: w=u;
1685: fv=fw;
1686: fw=fu;
1.183 brouard 1687: } else if (fu <= fv || v == x || v == w) {
1.224 brouard 1688: v=u;
1689: fv=fu;
1.183 brouard 1690: }
1691: }
1.126 brouard 1692: }
1693: nrerror("Too many iterations in brent");
1694: *xmin=x;
1695: return fx;
1696: }
1697:
1698: /****************** mnbrak ***********************/
1699:
1700: void mnbrak(double *ax, double *bx, double *cx, double *fa, double *fb, double *fc,
1701: double (*func)(double))
1.183 brouard 1702: { /* Given a function func , and given distinct initial points ax and bx , this routine searches in
1703: the downhill direction (defined by the function as evaluated at the initial points) and returns
1704: new points ax , bx , cx that bracket a minimum of the function. Also returned are the function
1705: values at the three points, fa, fb , and fc such that fa > fb and fb < fc.
1706: */
1.126 brouard 1707: double ulim,u,r,q, dum;
1708: double fu;
1.187 brouard 1709:
1710: double scale=10.;
1711: int iterscale=0;
1712:
1713: *fa=(*func)(*ax); /* xta[j]=pcom[j]+(*ax)*xicom[j]; fa=f(xta[j])*/
1714: *fb=(*func)(*bx); /* xtb[j]=pcom[j]+(*bx)*xicom[j]; fb=f(xtb[j]) */
1715:
1716:
1717: /* while(*fb != *fb){ /\* *ax should be ok, reducing distance to *ax *\/ */
1718: /* printf("Warning mnbrak *fb = %lf, *bx=%lf *ax=%lf *fa==%lf iter=%d\n",*fb, *bx, *ax, *fa, iterscale++); */
1719: /* *bx = *ax - (*ax - *bx)/scale; */
1720: /* *fb=(*func)(*bx); /\* xtb[j]=pcom[j]+(*bx)*xicom[j]; fb=f(xtb[j]) *\/ */
1721: /* } */
1722:
1.126 brouard 1723: if (*fb > *fa) {
1724: SHFT(dum,*ax,*bx,dum)
1.183 brouard 1725: SHFT(dum,*fb,*fa,dum)
1726: }
1.126 brouard 1727: *cx=(*bx)+GOLD*(*bx-*ax);
1728: *fc=(*func)(*cx);
1.183 brouard 1729: #ifdef DEBUG
1.224 brouard 1730: printf("mnbrak0 a=%lf *fa=%lf, b=%lf *fb=%lf, c=%lf *fc=%lf\n",*ax,*fa,*bx,*fb,*cx, *fc);
1731: fprintf(ficlog,"mnbrak0 a=%lf *fa=%lf, b=%lf *fb=%lf, c=%lf *fc=%lf\n",*ax,*fa,*bx,*fb,*cx, *fc);
1.183 brouard 1732: #endif
1.224 brouard 1733: while (*fb > *fc) { /* Declining a,b,c with fa> fb > fc. If fc=inf it exits and if flat fb=fc it exits too.*/
1.126 brouard 1734: r=(*bx-*ax)*(*fb-*fc);
1.224 brouard 1735: q=(*bx-*cx)*(*fb-*fa); /* What if fa=inf */
1.126 brouard 1736: u=(*bx)-((*bx-*cx)*q-(*bx-*ax)*r)/
1.183 brouard 1737: (2.0*SIGN(FMAX(fabs(q-r),TINY),q-r)); /* Minimum abscissa of a parabolic estimated from (a,fa), (b,fb) and (c,fc). */
1738: ulim=(*bx)+GLIMIT*(*cx-*bx); /* Maximum abscissa where function should be evaluated */
1739: if ((*bx-u)*(u-*cx) > 0.0) { /* if u_p is between b and c */
1.126 brouard 1740: fu=(*func)(u);
1.163 brouard 1741: #ifdef DEBUG
1742: /* f(x)=A(x-u)**2+f(u) */
1743: double A, fparabu;
1744: A= (*fb - *fa)/(*bx-*ax)/(*bx+*ax-2*u);
1745: fparabu= *fa - A*(*ax-u)*(*ax-u);
1.224 brouard 1746: 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);
1747: fprintf(ficlog,"\nmnbrak (*ax=%.12f, *fa=%.12lf), (*bx=%.12f, *fb=%.12lf), (*cx=%.12f, *fc=%.12lf), (*u=%.12f, fu=%.12lf, fparabu=%.12f, q=%lf < %lf=r)\n",*ax,*fa,*bx,*fb,*cx,*fc,u,fu, fparabu,q,r);
1.183 brouard 1748: /* And thus,it can be that fu > *fc even if fparabu < *fc */
1749: /* mnbrak (*ax=7.666299858533, *fa=299039.693133272231), (*bx=8.595447774979, *fb=298976.598289369489),
1750: (*cx=10.098840694817, *fc=298946.631474258087), (*u=9.852501168332, fu=298948.773013752128, fparabu=298945.434711494134) */
1751: /* In that case, there is no bracket in the output! Routine is wrong with many consequences.*/
1.163 brouard 1752: #endif
1.184 brouard 1753: #ifdef MNBRAKORIGINAL
1.183 brouard 1754: #else
1.191 brouard 1755: /* if (fu > *fc) { */
1756: /* #ifdef DEBUG */
1757: /* printf("mnbrak4 fu > fc \n"); */
1758: /* fprintf(ficlog, "mnbrak4 fu > fc\n"); */
1759: /* #endif */
1760: /* /\* 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 *\\/ *\/ */
1761: /* /\* SHFT(*fa,*fc,fu,*fc) /\\* (b, u, c) is a bracket while test fb > fc will be fu > fc will exit *\\/ *\/ */
1762: /* dum=u; /\* Shifting c and u *\/ */
1763: /* u = *cx; */
1764: /* *cx = dum; */
1765: /* dum = fu; */
1766: /* fu = *fc; */
1767: /* *fc =dum; */
1768: /* } else { /\* end *\/ */
1769: /* #ifdef DEBUG */
1770: /* printf("mnbrak3 fu < fc \n"); */
1771: /* fprintf(ficlog, "mnbrak3 fu < fc\n"); */
1772: /* #endif */
1773: /* dum=u; /\* Shifting c and u *\/ */
1774: /* u = *cx; */
1775: /* *cx = dum; */
1776: /* dum = fu; */
1777: /* fu = *fc; */
1778: /* *fc =dum; */
1779: /* } */
1.224 brouard 1780: #ifdef DEBUGMNBRAK
1781: double A, fparabu;
1782: A= (*fb - *fa)/(*bx-*ax)/(*bx+*ax-2*u);
1783: fparabu= *fa - A*(*ax-u)*(*ax-u);
1784: 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);
1785: fprintf(ficlog,"\nmnbrak35 ax=%lf fa=%lf bx=%lf fb=%lf, u=%lf fp=%lf fu=%lf < or >= fc=%lf cx=%lf, q=%lf < %lf=r \n",*ax, *fa, *bx,*fb,u,fparabu,fu,*fc,*cx,q,r);
1.183 brouard 1786: #endif
1.191 brouard 1787: dum=u; /* Shifting c and u */
1788: u = *cx;
1789: *cx = dum;
1790: dum = fu;
1791: fu = *fc;
1792: *fc =dum;
1.183 brouard 1793: #endif
1.162 brouard 1794: } else if ((*cx-u)*(u-ulim) > 0.0) { /* u is after c but before ulim */
1.183 brouard 1795: #ifdef DEBUG
1.224 brouard 1796: printf("\nmnbrak2 u=%lf after c=%lf but before ulim\n",u,*cx);
1797: fprintf(ficlog,"\nmnbrak2 u=%lf after c=%lf but before ulim\n",u,*cx);
1.183 brouard 1798: #endif
1.126 brouard 1799: fu=(*func)(u);
1800: if (fu < *fc) {
1.183 brouard 1801: #ifdef DEBUG
1.224 brouard 1802: printf("\nmnbrak2 u=%lf after c=%lf but before ulim=%lf AND fu=%lf < %lf=fc\n",u,*cx,ulim,fu, *fc);
1803: fprintf(ficlog,"\nmnbrak2 u=%lf after c=%lf but before ulim=%lf AND fu=%lf < %lf=fc\n",u,*cx,ulim,fu, *fc);
1804: #endif
1805: SHFT(*bx,*cx,u,*cx+GOLD*(*cx-*bx))
1806: SHFT(*fb,*fc,fu,(*func)(u))
1807: #ifdef DEBUG
1808: printf("\nmnbrak2 shift GOLD c=%lf",*cx+GOLD*(*cx-*bx));
1.183 brouard 1809: #endif
1810: }
1.162 brouard 1811: } else if ((u-ulim)*(ulim-*cx) >= 0.0) { /* u outside ulim (verifying that ulim is beyond c) */
1.183 brouard 1812: #ifdef DEBUG
1.224 brouard 1813: printf("\nmnbrak2 u=%lf outside ulim=%lf (verifying that ulim is beyond c=%lf)\n",u,ulim,*cx);
1814: fprintf(ficlog,"\nmnbrak2 u=%lf outside ulim=%lf (verifying that ulim is beyond c=%lf)\n",u,ulim,*cx);
1.183 brouard 1815: #endif
1.126 brouard 1816: u=ulim;
1817: fu=(*func)(u);
1.183 brouard 1818: } else { /* u could be left to b (if r > q parabola has a maximum) */
1819: #ifdef DEBUG
1.224 brouard 1820: printf("\nmnbrak2 u=%lf could be left to b=%lf (if r=%lf > q=%lf parabola has a maximum)\n",u,*bx,r,q);
1821: fprintf(ficlog,"\nmnbrak2 u=%lf could be left to b=%lf (if r=%lf > q=%lf parabola has a maximum)\n",u,*bx,r,q);
1.183 brouard 1822: #endif
1.126 brouard 1823: u=(*cx)+GOLD*(*cx-*bx);
1824: fu=(*func)(u);
1.224 brouard 1825: #ifdef DEBUG
1826: printf("\nmnbrak2 new u=%lf fu=%lf shifted gold left from c=%lf and b=%lf \n",u,fu,*cx,*bx);
1827: fprintf(ficlog,"\nmnbrak2 new u=%lf fu=%lf shifted gold left from c=%lf and b=%lf \n",u,fu,*cx,*bx);
1828: #endif
1.183 brouard 1829: } /* end tests */
1.126 brouard 1830: SHFT(*ax,*bx,*cx,u)
1.183 brouard 1831: SHFT(*fa,*fb,*fc,fu)
1832: #ifdef DEBUG
1.224 brouard 1833: printf("\nmnbrak2 shift (*ax=%.12f, *fa=%.12lf), (*bx=%.12f, *fb=%.12lf), (*cx=%.12f, *fc=%.12lf)\n",*ax,*fa,*bx,*fb,*cx,*fc);
1834: fprintf(ficlog, "\nmnbrak2 shift (*ax=%.12f, *fa=%.12lf), (*bx=%.12f, *fb=%.12lf), (*cx=%.12f, *fc=%.12lf)\n",*ax,*fa,*bx,*fb,*cx,*fc);
1.183 brouard 1835: #endif
1836: } /* end while; ie return (a, b, c, fa, fb, fc) such that a < b < c with f(a) > f(b) and fb < f(c) */
1.126 brouard 1837: }
1838:
1839: /*************** linmin ************************/
1.162 brouard 1840: /* Given an n -dimensional point p[1..n] and an n -dimensional direction xi[1..n] , moves and
1841: resets p to where the function func(p) takes on a minimum along the direction xi from p ,
1842: and replaces xi by the actual vector displacement that p was moved. Also returns as fret
1843: the value of func at the returned location p . This is actually all accomplished by calling the
1844: routines mnbrak and brent .*/
1.126 brouard 1845: int ncom;
1846: double *pcom,*xicom;
1847: double (*nrfunc)(double []);
1848:
1.224 brouard 1849: #ifdef LINMINORIGINAL
1.126 brouard 1850: void linmin(double p[], double xi[], int n, double *fret,double (*func)(double []))
1.224 brouard 1851: #else
1852: void linmin(double p[], double xi[], int n, double *fret,double (*func)(double []), int *flat)
1853: #endif
1.126 brouard 1854: {
1855: double brent(double ax, double bx, double cx,
1856: double (*f)(double), double tol, double *xmin);
1857: double f1dim(double x);
1858: void mnbrak(double *ax, double *bx, double *cx, double *fa, double *fb,
1859: double *fc, double (*func)(double));
1860: int j;
1861: double xx,xmin,bx,ax;
1862: double fx,fb,fa;
1.187 brouard 1863:
1.203 brouard 1864: #ifdef LINMINORIGINAL
1865: #else
1866: double scale=10., axs, xxs; /* Scale added for infinity */
1867: #endif
1868:
1.126 brouard 1869: ncom=n;
1870: pcom=vector(1,n);
1871: xicom=vector(1,n);
1872: nrfunc=func;
1873: for (j=1;j<=n;j++) {
1874: pcom[j]=p[j];
1.202 brouard 1875: xicom[j]=xi[j]; /* Former scale xi[j] of currrent direction i */
1.126 brouard 1876: }
1.187 brouard 1877:
1.203 brouard 1878: #ifdef LINMINORIGINAL
1879: xx=1.;
1880: #else
1881: axs=0.0;
1882: xxs=1.;
1883: do{
1884: xx= xxs;
1885: #endif
1.187 brouard 1886: ax=0.;
1887: mnbrak(&ax,&xx,&bx,&fa,&fx,&fb,f1dim); /* Outputs: xtx[j]=pcom[j]+(*xx)*xicom[j]; fx=f(xtx[j]) */
1888: /* brackets with inputs ax=0 and xx=1, but points, pcom=p, and directions values, xicom=xi, are sent via f1dim(x) */
1889: /* 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)) */
1890: /* Outputs: fa=f(p(j)) and fx=f(p(j) + xxs * xi(j) ) and f(bx)= f(p(j)+ bx* xi(j)) */
1891: /* Given input ax=axs and xx=xxs, xx might be too far from ax to get a finite f(xx) */
1892: /* Searches on line, outputs (ax, xx, bx) such that fx < min(fa and fb) */
1893: /* Find a bracket a,x,b in direction n=xi ie xicom, order may change. Scale is [0:xxs*xi[j]] et non plus [0:xi[j]]*/
1.203 brouard 1894: #ifdef LINMINORIGINAL
1895: #else
1896: if (fx != fx){
1.224 brouard 1897: xxs=xxs/scale; /* Trying a smaller xx, closer to initial ax=0 */
1898: printf("|");
1899: fprintf(ficlog,"|");
1.203 brouard 1900: #ifdef DEBUGLINMIN
1.224 brouard 1901: printf("\nLinmin NAN : input [axs=%lf:xxs=%lf], mnbrak outputs fx=%lf <(fb=%lf and fa=%lf) with xx=%lf in [ax=%lf:bx=%lf] \n", axs, xxs, fx,fb, fa, xx, ax, bx);
1.203 brouard 1902: #endif
1903: }
1.224 brouard 1904: }while(fx != fx && xxs > 1.e-5);
1.203 brouard 1905: #endif
1906:
1.191 brouard 1907: #ifdef DEBUGLINMIN
1908: printf("\nLinmin after mnbrak: ax=%12.7f xx=%12.7f bx=%12.7f fa=%12.2f fx=%12.2f fb=%12.2f\n", ax,xx,bx,fa,fx,fb);
1.202 brouard 1909: fprintf(ficlog,"\nLinmin after mnbrak: ax=%12.7f xx=%12.7f bx=%12.7f fa=%12.2f fx=%12.2f fb=%12.2f\n", ax,xx,bx,fa,fx,fb);
1.191 brouard 1910: #endif
1.224 brouard 1911: #ifdef LINMINORIGINAL
1912: #else
1913: if(fb == fx){ /* Flat function in the direction */
1914: xmin=xx;
1915: *flat=1;
1916: }else{
1917: *flat=0;
1918: #endif
1919: /*Flat mnbrak2 shift (*ax=0.000000000000, *fa=51626.272983130431), (*bx=-1.618034000000, *fb=51590.149499362531), (*cx=-4.236068025156, *fc=51590.149499362531) */
1.187 brouard 1920: *fret=brent(ax,xx,bx,f1dim,TOL,&xmin); /* Giving a bracketting triplet (ax, xx, bx), find a minimum, xmin, according to f1dim, *fret(xmin),*/
1921: /* fa = f(p[j] + ax * xi[j]), fx = f(p[j] + xx * xi[j]), fb = f(p[j] + bx * xi[j]) */
1922: /* fmin = f(p[j] + xmin * xi[j]) */
1923: /* P+lambda n in that direction (lambdamin), with TOL between abscisses */
1924: /* f1dim(xmin): for (j=1;j<=ncom;j++) xt[j]=pcom[j]+xmin*xicom[j]; */
1.126 brouard 1925: #ifdef DEBUG
1.224 brouard 1926: 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);
1927: 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);
1928: #endif
1929: #ifdef LINMINORIGINAL
1930: #else
1931: }
1.126 brouard 1932: #endif
1.191 brouard 1933: #ifdef DEBUGLINMIN
1934: printf("linmin end ");
1.202 brouard 1935: fprintf(ficlog,"linmin end ");
1.191 brouard 1936: #endif
1.126 brouard 1937: for (j=1;j<=n;j++) {
1.203 brouard 1938: #ifdef LINMINORIGINAL
1939: xi[j] *= xmin;
1940: #else
1941: #ifdef DEBUGLINMIN
1942: if(xxs <1.0)
1943: printf(" before xi[%d]=%12.8f", j,xi[j]);
1944: #endif
1945: 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) */
1946: #ifdef DEBUGLINMIN
1947: if(xxs <1.0)
1948: 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 );
1949: #endif
1950: #endif
1.187 brouard 1951: p[j] += xi[j]; /* Parameters values are updated accordingly */
1.126 brouard 1952: }
1.191 brouard 1953: #ifdef DEBUGLINMIN
1.203 brouard 1954: printf("\n");
1.191 brouard 1955: printf("Comparing last *frec(xmin=%12.8f)=%12.8f from Brent and frec(0.)=%12.8f \n", xmin, *fret, (*func)(p));
1.202 brouard 1956: fprintf(ficlog,"Comparing last *frec(xmin=%12.8f)=%12.8f from Brent and frec(0.)=%12.8f \n", xmin, *fret, (*func)(p));
1.191 brouard 1957: for (j=1;j<=n;j++) {
1.202 brouard 1958: printf(" xi[%d]= %14.10f p[%d]= %12.7f",j,xi[j],j,p[j]);
1959: fprintf(ficlog," xi[%d]= %14.10f p[%d]= %12.7f",j,xi[j],j,p[j]);
1960: if(j % ncovmodel == 0){
1.191 brouard 1961: printf("\n");
1.202 brouard 1962: fprintf(ficlog,"\n");
1963: }
1.191 brouard 1964: }
1.203 brouard 1965: #else
1.191 brouard 1966: #endif
1.126 brouard 1967: free_vector(xicom,1,n);
1968: free_vector(pcom,1,n);
1969: }
1970:
1971:
1972: /*************** powell ************************/
1.162 brouard 1973: /*
1974: Minimization of a function func of n variables. Input consists of an initial starting point
1975: p[1..n] ; an initial matrix xi[1..n][1..n] , whose columns contain the initial set of di-
1976: rections (usually the n unit vectors); and ftol , the fractional tolerance in the function value
1977: such that failure to decrease by more than this amount on one iteration signals doneness. On
1978: output, p is set to the best point found, xi is the then-current direction set, fret is the returned
1979: function value at p , and iter is the number of iterations taken. The routine linmin is used.
1980: */
1.224 brouard 1981: #ifdef LINMINORIGINAL
1982: #else
1983: int *flatdir; /* Function is vanishing in that direction */
1.225 brouard 1984: int flat=0, flatd=0; /* Function is vanishing in that direction */
1.224 brouard 1985: #endif
1.126 brouard 1986: void powell(double p[], double **xi, int n, double ftol, int *iter, double *fret,
1987: double (*func)(double []))
1988: {
1.224 brouard 1989: #ifdef LINMINORIGINAL
1990: void linmin(double p[], double xi[], int n, double *fret,
1.126 brouard 1991: double (*func)(double []));
1.224 brouard 1992: #else
1993: void linmin(double p[], double xi[], int n, double *fret,
1994: double (*func)(double []),int *flat);
1995: #endif
1.126 brouard 1996: int i,ibig,j;
1997: double del,t,*pt,*ptt,*xit;
1.181 brouard 1998: double directest;
1.126 brouard 1999: double fp,fptt;
2000: double *xits;
2001: int niterf, itmp;
1.224 brouard 2002: #ifdef LINMINORIGINAL
2003: #else
2004:
2005: flatdir=ivector(1,n);
2006: for (j=1;j<=n;j++) flatdir[j]=0;
2007: #endif
1.126 brouard 2008:
2009: pt=vector(1,n);
2010: ptt=vector(1,n);
2011: xit=vector(1,n);
2012: xits=vector(1,n);
2013: *fret=(*func)(p);
2014: for (j=1;j<=n;j++) pt[j]=p[j];
1.202 brouard 2015: rcurr_time = time(NULL);
1.126 brouard 2016: for (*iter=1;;++(*iter)) {
1.187 brouard 2017: fp=(*fret); /* From former iteration or initial value */
1.126 brouard 2018: ibig=0;
2019: del=0.0;
1.157 brouard 2020: rlast_time=rcurr_time;
2021: /* (void) gettimeofday(&curr_time,&tzp); */
2022: rcurr_time = time(NULL);
2023: curr_time = *localtime(&rcurr_time);
2024: printf("\nPowell iter=%d -2*LL=%.12f %ld sec. %ld sec.",*iter,*fret, rcurr_time-rlast_time, rcurr_time-rstart_time);fflush(stdout);
2025: fprintf(ficlog,"\nPowell iter=%d -2*LL=%.12f %ld sec. %ld sec.",*iter,*fret,rcurr_time-rlast_time, rcurr_time-rstart_time); fflush(ficlog);
2026: /* fprintf(ficrespow,"%d %.12f %ld",*iter,*fret,curr_time.tm_sec-start_time.tm_sec); */
1.192 brouard 2027: for (i=1;i<=n;i++) {
1.126 brouard 2028: printf(" %d %.12f",i, p[i]);
2029: fprintf(ficlog," %d %.12lf",i, p[i]);
2030: fprintf(ficrespow," %.12lf", p[i]);
2031: }
2032: printf("\n");
2033: fprintf(ficlog,"\n");
2034: fprintf(ficrespow,"\n");fflush(ficrespow);
2035: if(*iter <=3){
1.157 brouard 2036: tml = *localtime(&rcurr_time);
2037: strcpy(strcurr,asctime(&tml));
2038: rforecast_time=rcurr_time;
1.126 brouard 2039: itmp = strlen(strcurr);
2040: if(strcurr[itmp-1]=='\n') /* Windows outputs with a new line */
1.224 brouard 2041: strcurr[itmp-1]='\0';
1.162 brouard 2042: printf("\nConsidering the time needed for the last iteration #%d: %ld seconds,\n",*iter,rcurr_time-rlast_time);
1.157 brouard 2043: fprintf(ficlog,"\nConsidering the time needed for this last iteration #%d: %ld seconds,\n",*iter,rcurr_time-rlast_time);
1.126 brouard 2044: for(niterf=10;niterf<=30;niterf+=10){
1.224 brouard 2045: rforecast_time=rcurr_time+(niterf-*iter)*(rcurr_time-rlast_time);
2046: forecast_time = *localtime(&rforecast_time);
2047: strcpy(strfor,asctime(&forecast_time));
2048: itmp = strlen(strfor);
2049: if(strfor[itmp-1]=='\n')
2050: strfor[itmp-1]='\0';
2051: printf(" - if your program needs %d iterations to converge, convergence will be \n reached in %s i.e.\n on %s (current time is %s);\n",niterf, asc_diff_time(rforecast_time-rcurr_time,tmpout),strfor,strcurr);
2052: fprintf(ficlog," - if your program needs %d iterations to converge, convergence will be \n reached in %s i.e.\n on %s (current time is %s);\n",niterf, asc_diff_time(rforecast_time-rcurr_time,tmpout),strfor,strcurr);
1.126 brouard 2053: }
2054: }
1.187 brouard 2055: for (i=1;i<=n;i++) { /* For each direction i */
2056: for (j=1;j<=n;j++) xit[j]=xi[j][i]; /* Directions stored from previous iteration with previous scales */
1.126 brouard 2057: fptt=(*fret);
2058: #ifdef DEBUG
1.203 brouard 2059: printf("fret=%lf, %lf, %lf \n", *fret, *fret, *fret);
2060: fprintf(ficlog, "fret=%lf, %lf, %lf \n", *fret, *fret, *fret);
1.126 brouard 2061: #endif
1.203 brouard 2062: printf("%d",i);fflush(stdout); /* print direction (parameter) i */
1.126 brouard 2063: fprintf(ficlog,"%d",i);fflush(ficlog);
1.224 brouard 2064: #ifdef LINMINORIGINAL
1.188 brouard 2065: linmin(p,xit,n,fret,func); /* Point p[n]. xit[n] has been loaded for direction i as input.*/
1.224 brouard 2066: #else
2067: linmin(p,xit,n,fret,func,&flat); /* Point p[n]. xit[n] has been loaded for direction i as input.*/
2068: flatdir[i]=flat; /* Function is vanishing in that direction i */
2069: #endif
2070: /* Outputs are fret(new point p) p is updated and xit rescaled */
1.188 brouard 2071: if (fabs(fptt-(*fret)) > del) { /* We are keeping the max gain on each of the n directions */
1.224 brouard 2072: /* because that direction will be replaced unless the gain del is small */
2073: /* in comparison with the 'probable' gain, mu^2, with the last average direction. */
2074: /* Unless the n directions are conjugate some gain in the determinant may be obtained */
2075: /* with the new direction. */
2076: del=fabs(fptt-(*fret));
2077: ibig=i;
1.126 brouard 2078: }
2079: #ifdef DEBUG
2080: printf("%d %.12e",i,(*fret));
2081: fprintf(ficlog,"%d %.12e",i,(*fret));
2082: for (j=1;j<=n;j++) {
1.224 brouard 2083: xits[j]=FMAX(fabs(p[j]-pt[j]),1.e-5);
2084: printf(" x(%d)=%.12e",j,xit[j]);
2085: fprintf(ficlog," x(%d)=%.12e",j,xit[j]);
1.126 brouard 2086: }
2087: for(j=1;j<=n;j++) {
1.225 brouard 2088: printf(" p(%d)=%.12e",j,p[j]);
2089: fprintf(ficlog," p(%d)=%.12e",j,p[j]);
1.126 brouard 2090: }
2091: printf("\n");
2092: fprintf(ficlog,"\n");
2093: #endif
1.187 brouard 2094: } /* end loop on each direction i */
2095: /* Convergence test will use last linmin estimation (fret) and compare former iteration (fp) */
1.188 brouard 2096: /* But p and xit have been updated at the end of linmin, *fret corresponds to new p, xit */
1.187 brouard 2097: /* New value of last point Pn is not computed, P(n-1) */
1.224 brouard 2098: for(j=1;j<=n;j++) {
1.225 brouard 2099: if(flatdir[j] >0){
2100: printf(" p(%d)=%lf flat=%d ",j,p[j],flatdir[j]);
2101: fprintf(ficlog," p(%d)=%lf flat=%d ",j,p[j],flatdir[j]);
2102: }
2103: /* printf("\n"); */
2104: /* fprintf(ficlog,"\n"); */
2105: }
1.182 brouard 2106: if (2.0*fabs(fp-(*fret)) <= ftol*(fabs(fp)+fabs(*fret))) { /* Did we reach enough precision? */
1.188 brouard 2107: /* We could compare with a chi^2. chisquare(0.95,ddl=1)=3.84 */
2108: /* By adding age*age in a model, the new -2LL should be lower and the difference follows a */
2109: /* a chisquare statistics with 1 degree. To be significant at the 95% level, it should have */
2110: /* decreased of more than 3.84 */
2111: /* By adding age*age and V1*age the gain (-2LL) should be more than 5.99 (ddl=2) */
2112: /* By using V1+V2+V3, the gain should be 7.82, compared with basic 1+age. */
2113: /* By adding 10 parameters more the gain should be 18.31 */
1.224 brouard 2114:
1.188 brouard 2115: /* Starting the program with initial values given by a former maximization will simply change */
2116: /* the scales of the directions and the directions, because the are reset to canonical directions */
2117: /* Thus the first calls to linmin will give new points and better maximizations until fp-(*fret) is */
2118: /* under the tolerance value. If the tolerance is very small 1.e-9, it could last long. */
1.126 brouard 2119: #ifdef DEBUG
2120: int k[2],l;
2121: k[0]=1;
2122: k[1]=-1;
2123: printf("Max: %.12e",(*func)(p));
2124: fprintf(ficlog,"Max: %.12e",(*func)(p));
2125: for (j=1;j<=n;j++) {
2126: printf(" %.12e",p[j]);
2127: fprintf(ficlog," %.12e",p[j]);
2128: }
2129: printf("\n");
2130: fprintf(ficlog,"\n");
2131: for(l=0;l<=1;l++) {
2132: for (j=1;j<=n;j++) {
2133: ptt[j]=p[j]+(p[j]-pt[j])*k[l];
2134: printf("l=%d j=%d ptt=%.12e, xits=%.12e, p=%.12e, xit=%.12e", l,j,ptt[j],xits[j],p[j],xit[j]);
2135: fprintf(ficlog,"l=%d j=%d ptt=%.12e, xits=%.12e, p=%.12e, xit=%.12e", l,j,ptt[j],xits[j],p[j],xit[j]);
2136: }
2137: printf("func(ptt)=%.12e, deriv=%.12e\n",(*func)(ptt),(ptt[j]-p[j])/((*func)(ptt)-(*func)(p)));
2138: fprintf(ficlog,"func(ptt)=%.12e, deriv=%.12e\n",(*func)(ptt),(ptt[j]-p[j])/((*func)(ptt)-(*func)(p)));
2139: }
2140: #endif
2141:
1.224 brouard 2142: #ifdef LINMINORIGINAL
2143: #else
2144: free_ivector(flatdir,1,n);
2145: #endif
1.126 brouard 2146: free_vector(xit,1,n);
2147: free_vector(xits,1,n);
2148: free_vector(ptt,1,n);
2149: free_vector(pt,1,n);
2150: return;
1.192 brouard 2151: } /* enough precision */
1.126 brouard 2152: if (*iter == ITMAX) nrerror("powell exceeding maximum iterations.");
1.181 brouard 2153: for (j=1;j<=n;j++) { /* Computes the extrapolated point P_0 + 2 (P_n-P_0) */
1.126 brouard 2154: ptt[j]=2.0*p[j]-pt[j];
2155: xit[j]=p[j]-pt[j];
2156: pt[j]=p[j];
2157: }
1.181 brouard 2158: fptt=(*func)(ptt); /* f_3 */
1.224 brouard 2159: #ifdef NODIRECTIONCHANGEDUNTILNITER /* No change in drections until some iterations are done */
2160: if (*iter <=4) {
1.225 brouard 2161: #else
2162: #endif
1.224 brouard 2163: #ifdef POWELLNOF3INFF1TEST /* skips test F3 <F1 */
1.192 brouard 2164: #else
1.161 brouard 2165: if (fptt < fp) { /* If extrapolated point is better, decide if we keep that new direction or not */
1.192 brouard 2166: #endif
1.162 brouard 2167: /* (x1 f1=fp), (x2 f2=*fret), (x3 f3=fptt), (xm fm) */
1.161 brouard 2168: /* From x1 (P0) distance of x2 is at h and x3 is 2h */
1.162 brouard 2169: /* Let f"(x2) be the 2nd derivative equal everywhere. */
2170: /* Then the parabolic through (x1,f1), (x2,f2) and (x3,f3) */
2171: /* will reach at f3 = fm + h^2/2 f"m ; f" = (f1 -2f2 +f3 ) / h**2 */
1.224 brouard 2172: /* Conditional for using this new direction is that mu^2 = (f1-2f2+f3)^2 /2 < del or directest <0 */
2173: /* also lamda^2=(f1-f2)^2/mu² is a parasite solution of powell */
2174: /* For powell, inclusion of this average direction is only if t(del)<0 or del inbetween mu^2 and lambda^2 */
1.161 brouard 2175: /* t=2.0*(fp-2.0*(*fret)+fptt)*SQR(fp-(*fret)-del)-del*SQR(fp-fptt); */
1.224 brouard 2176: /* Even if f3 <f1, directest can be negative and t >0 */
2177: /* mu² and del² are equal when f3=f1 */
2178: /* f3 < f1 : mu² < del <= lambda^2 both test are equivalent */
2179: /* f3 < f1 : mu² < lambda^2 < del then directtest is negative and powell t is positive */
2180: /* f3 > f1 : lambda² < mu^2 < del then t is negative and directest >0 */
2181: /* f3 > f1 : lambda² < del < mu^2 then t is positive and directest >0 */
1.183 brouard 2182: #ifdef NRCORIGINAL
2183: t=2.0*(fp-2.0*(*fret)+fptt)*SQR(fp-(*fret)-del)- del*SQR(fp-fptt); /* Original Numerical Recipes in C*/
2184: #else
2185: t=2.0*(fp-2.0*(*fret)+fptt)*SQR(fp-(*fret)-del); /* Intel compiler doesn't work on one line; bug reported */
1.161 brouard 2186: t= t- del*SQR(fp-fptt);
1.183 brouard 2187: #endif
1.202 brouard 2188: directest = fp-2.0*(*fret)+fptt - 2.0 * del; /* If delta was big enough we change it for a new direction */
1.161 brouard 2189: #ifdef DEBUG
1.181 brouard 2190: 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);
2191: fprintf(ficlog,"t1= %.12lf, t2= %.12lf, t=%.12lf directest=%.12lf\n", 2.0*(fp-2.0*(*fret)+fptt)*SQR(fp-(*fret)-del),del*SQR(fp-fptt),t,directest);
1.161 brouard 2192: printf("t3= %.12lf, t4= %.12lf, t3*= %.12lf, t4*= %.12lf\n",SQR(fp-(*fret)-del),SQR(fp-fptt),
2193: (fp-(*fret)-del)*(fp-(*fret)-del),(fp-fptt)*(fp-fptt));
2194: fprintf(ficlog,"t3= %.12lf, t4= %.12lf, t3*= %.12lf, t4*= %.12lf\n",SQR(fp-(*fret)-del),SQR(fp-fptt),
2195: (fp-(*fret)-del)*(fp-(*fret)-del),(fp-fptt)*(fp-fptt));
2196: 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);
2197: 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);
2198: #endif
1.183 brouard 2199: #ifdef POWELLORIGINAL
2200: if (t < 0.0) { /* Then we use it for new direction */
2201: #else
1.182 brouard 2202: if (directest*t < 0.0) { /* Contradiction between both tests */
1.224 brouard 2203: printf("directest= %.12lf (if <0 we include P0 Pn as new direction), t= %.12lf, f1= %.12lf,f2= %.12lf,f3= %.12lf, del= %.12lf\n",directest, t, fp,(*fret),fptt,del);
1.192 brouard 2204: printf("f1-2f2+f3= %.12lf, f1-f2-del= %.12lf, f1-f3= %.12lf\n",fp-2.0*(*fret)+fptt, fp -(*fret) -del, fp-fptt);
1.224 brouard 2205: fprintf(ficlog,"directest= %.12lf (if directest<0 or t<0 we include P0 Pn as new direction), t= %.12lf, f1= %.12lf,f2= %.12lf,f3= %.12lf, del= %.12lf\n",directest, t, fp,(*fret),fptt, del);
1.192 brouard 2206: fprintf(ficlog,"f1-2f2+f3= %.12lf, f1-f2-del= %.12lf, f1-f3= %.12lf\n",fp-2.0*(*fret)+fptt, fp -(*fret) -del, fp-fptt);
2207: }
1.181 brouard 2208: if (directest < 0.0) { /* Then we use it for new direction */
2209: #endif
1.191 brouard 2210: #ifdef DEBUGLINMIN
1.224 brouard 2211: printf("Before linmin in direction P%d-P0\n",n);
2212: for (j=1;j<=n;j++) {
2213: printf(" Before xit[%d]= %12.7f p[%d]= %12.7f",j,xit[j],j,p[j]);
2214: fprintf(ficlog," Before xit[%d]= %12.7f p[%d]= %12.7f",j,xit[j],j,p[j]);
2215: if(j % ncovmodel == 0){
2216: printf("\n");
2217: fprintf(ficlog,"\n");
2218: }
2219: }
2220: #endif
2221: #ifdef LINMINORIGINAL
2222: linmin(p,xit,n,fret,func); /* computes minimum on the extrapolated direction: changes p and rescales xit.*/
2223: #else
2224: linmin(p,xit,n,fret,func,&flat); /* computes minimum on the extrapolated direction: changes p and rescales xit.*/
2225: flatdir[i]=flat; /* Function is vanishing in that direction i */
1.191 brouard 2226: #endif
1.224 brouard 2227:
1.191 brouard 2228: #ifdef DEBUGLINMIN
1.224 brouard 2229: for (j=1;j<=n;j++) {
2230: printf("After xit[%d]= %12.7f p[%d]= %12.7f",j,xit[j],j,p[j]);
2231: fprintf(ficlog,"After xit[%d]= %12.7f p[%d]= %12.7f",j,xit[j],j,p[j]);
2232: if(j % ncovmodel == 0){
2233: printf("\n");
2234: fprintf(ficlog,"\n");
2235: }
2236: }
2237: #endif
2238: for (j=1;j<=n;j++) {
2239: xi[j][ibig]=xi[j][n]; /* Replace direction with biggest decrease by last direction n */
2240: xi[j][n]=xit[j]; /* and this nth direction by the by the average p_0 p_n */
2241: }
2242: #ifdef LINMINORIGINAL
2243: #else
1.225 brouard 2244: for (j=1, flatd=0;j<=n;j++) {
2245: if(flatdir[j]>0)
2246: flatd++;
2247: }
2248: if(flatd >0){
2249: printf("%d flat directions\n",flatd);
2250: fprintf(ficlog,"%d flat directions\n",flatd);
2251: for (j=1;j<=n;j++) {
2252: if(flatdir[j]>0){
2253: printf("%d ",j);
2254: fprintf(ficlog,"%d ",j);
2255: }
2256: }
2257: printf("\n");
2258: fprintf(ficlog,"\n");
2259: }
1.191 brouard 2260: #endif
1.224 brouard 2261: printf("Gaining to use new average direction of P0 P%d instead of biggest increase direction %d :\n",n,ibig);
2262: fprintf(ficlog,"Gaining to use new average direction of P0 P%d instead of biggest increase direction %d :\n",n,ibig);
2263:
1.126 brouard 2264: #ifdef DEBUG
1.224 brouard 2265: printf("Direction changed last moved %d in place of ibig=%d, new last is the average:\n",n,ibig);
2266: fprintf(ficlog,"Direction changed last moved %d in place of ibig=%d, new last is the average:\n",n,ibig);
2267: for(j=1;j<=n;j++){
2268: printf(" %lf",xit[j]);
2269: fprintf(ficlog," %lf",xit[j]);
2270: }
2271: printf("\n");
2272: fprintf(ficlog,"\n");
1.126 brouard 2273: #endif
1.192 brouard 2274: } /* end of t or directest negative */
1.224 brouard 2275: #ifdef POWELLNOF3INFF1TEST
1.192 brouard 2276: #else
1.162 brouard 2277: } /* end if (fptt < fp) */
1.192 brouard 2278: #endif
1.225 brouard 2279: #ifdef NODIRECTIONCHANGEDUNTILNITER /* No change in drections until some iterations are done */
1.224 brouard 2280: } /*NODIRECTIONCHANGEDUNTILNITER No change in drections until some iterations are done */
1.225 brouard 2281: #else
1.224 brouard 2282: #endif
1.192 brouard 2283: } /* loop iteration */
1.126 brouard 2284: }
2285:
2286: /**** Prevalence limit (stable or period prevalence) ****************/
2287:
1.203 brouard 2288: double **prevalim(double **prlim, int nlstate, double x[], double age, double **oldm, double **savm, double ftolpl, int *ncvyear, int ij)
1.126 brouard 2289: {
1.218 brouard 2290: /* Computes the prevalence limit in each live state at age x and for covariate ij by left multiplying the unit
1.203 brouard 2291: matrix by transitions matrix until convergence is reached with precision ftolpl */
1.206 brouard 2292: /* Wx= Wx-1 Px-1= Wx-2 Px-2 Px-1 = Wx-n Px-n ... Px-2 Px-1 I */
2293: /* Wx is row vector: population in state 1, population in state 2, population dead */
2294: /* or prevalence in state 1, prevalence in state 2, 0 */
2295: /* newm is the matrix after multiplications, its rows are identical at a factor */
2296: /* Initial matrix pimij */
2297: /* {0.85204250825084937, 0.13044499163996345, 0.017512500109187184, */
2298: /* 0.090851990222114765, 0.88271245433047185, 0.026435555447413338, */
2299: /* 0, 0 , 1} */
2300: /*
2301: * and after some iteration: */
2302: /* {0.45504275246439968, 0.42731458730878791, 0.11764266022681241, */
2303: /* 0.45201005341706885, 0.42865420071559901, 0.11933574586733192, */
2304: /* 0, 0 , 1} */
2305: /* And prevalence by suppressing the deaths are close to identical rows in prlim: */
2306: /* {0.51571254859325999, 0.4842874514067399, */
2307: /* 0.51326036147820708, 0.48673963852179264} */
2308: /* If we start from prlim again, prlim tends to a constant matrix */
2309:
1.126 brouard 2310: int i, ii,j,k;
1.209 brouard 2311: double *min, *max, *meandiff, maxmax,sumnew=0.;
1.145 brouard 2312: /* double **matprod2(); */ /* test */
1.218 brouard 2313: double **out, cov[NCOVMAX+1], **pmij(); /* **pmmij is a global variable feeded with oldms etc */
1.126 brouard 2314: double **newm;
1.209 brouard 2315: double agefin, delaymax=200. ; /* 100 Max number of years to converge */
1.203 brouard 2316: int ncvloop=0;
1.169 brouard 2317:
1.209 brouard 2318: min=vector(1,nlstate);
2319: max=vector(1,nlstate);
2320: meandiff=vector(1,nlstate);
2321:
1.218 brouard 2322: /* Starting with matrix unity */
1.126 brouard 2323: for (ii=1;ii<=nlstate+ndeath;ii++)
2324: for (j=1;j<=nlstate+ndeath;j++){
2325: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
2326: }
1.169 brouard 2327:
2328: cov[1]=1.;
2329:
2330: /* Even if hstepm = 1, at least one multiplication by the unit matrix */
1.202 brouard 2331: /* Start at agefin= age, computes the matrix of passage and loops decreasing agefin until convergence is reached */
1.126 brouard 2332: for(agefin=age-stepm/YEARM; agefin>=age-delaymax; agefin=agefin-stepm/YEARM){
1.202 brouard 2333: ncvloop++;
1.126 brouard 2334: newm=savm;
2335: /* Covariates have to be included here again */
1.138 brouard 2336: cov[2]=agefin;
1.187 brouard 2337: if(nagesqr==1)
2338: cov[3]= agefin*agefin;;
1.138 brouard 2339: for (k=1; k<=cptcovn;k++) {
1.200 brouard 2340: /* cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,Tvar[k])]; */
1.218 brouard 2341: /* Here comes the value of the covariate 'ij' */
1.200 brouard 2342: cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,k)];
1.198 brouard 2343: /* 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])]); */
1.138 brouard 2344: }
1.186 brouard 2345: /*wrong? for (k=1; k<=cptcovage;k++) cov[2+Tage[k]]=cov[2+Tage[k]]*cov[2]; */
1.200 brouard 2346: /* for (k=1; k<=cptcovage;k++) cov[2+nagesqr+Tage[k]]=nbcode[Tvar[k]][codtabm(ij,Tvar[k])]*cov[2]; */
2347: for (k=1; k<=cptcovage;k++) cov[2+nagesqr+Tage[k]]=nbcode[Tvar[k]][codtabm(ij,k)]*cov[2];
1.186 brouard 2348: for (k=1; k<=cptcovprod;k++) /* Useless */
1.200 brouard 2349: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,Tvard[k][1])] * nbcode[Tvard[k][2]][codtabm(ij,Tvard[k][2])]; */
2350: cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)] * nbcode[Tvard[k][2]][codtabm(ij,k)];
1.138 brouard 2351:
2352: /*printf("ij=%d cptcovprod=%d tvar=%d ", ij, cptcovprod, Tvar[1]);*/
2353: /*printf("ij=%d cov[3]=%lf cov[4]=%lf \n",ij, cov[3],cov[4]);*/
2354: /*printf("ij=%d cov[3]=%lf \n",ij, cov[3]);*/
1.145 brouard 2355: /* savm=pmij(pmmij,cov,ncovmodel,x,nlstate); */
2356: /* out=matprod2(newm, pmij(pmmij,cov,ncovmodel,x,nlstate),1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm); /\* Bug Valgrind *\/ */
1.218 brouard 2357: /* age and covariate values of ij are in 'cov' */
1.142 brouard 2358: out=matprod2(newm, pmij(pmmij,cov,ncovmodel,x,nlstate),1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm); /* Bug Valgrind */
1.138 brouard 2359:
1.126 brouard 2360: savm=oldm;
2361: oldm=newm;
1.209 brouard 2362:
2363: for(j=1; j<=nlstate; j++){
2364: max[j]=0.;
2365: min[j]=1.;
2366: }
2367: for(i=1;i<=nlstate;i++){
2368: sumnew=0;
2369: for(k=1; k<=ndeath; k++) sumnew+=newm[i][nlstate+k];
2370: for(j=1; j<=nlstate; j++){
2371: prlim[i][j]= newm[i][j]/(1-sumnew);
2372: max[j]=FMAX(max[j],prlim[i][j]);
2373: min[j]=FMIN(min[j],prlim[i][j]);
2374: }
2375: }
2376:
1.126 brouard 2377: maxmax=0.;
1.209 brouard 2378: for(j=1; j<=nlstate; j++){
2379: meandiff[j]=(max[j]-min[j])/(max[j]+min[j])*2.; /* mean difference for each column */
2380: maxmax=FMAX(maxmax,meandiff[j]);
2381: /* printf(" age= %d meandiff[%d]=%f, agefin=%d max[%d]=%f min[%d]=%f maxmax=%f\n", (int)age, j, meandiff[j],(int)agefin, j, max[j], j, min[j],maxmax); */
1.169 brouard 2382: } /* j loop */
1.203 brouard 2383: *ncvyear= (int)age- (int)agefin;
1.208 brouard 2384: /* printf("maxmax=%lf maxmin=%lf ncvloop=%d, age=%d, agefin=%d ncvyear=%d \n", maxmax, maxmin, ncvloop, (int)age, (int)agefin, *ncvyear); */
1.126 brouard 2385: if(maxmax < ftolpl){
1.209 brouard 2386: /* printf("maxmax=%lf ncvloop=%ld, age=%d, agefin=%d ncvyear=%d \n", maxmax, ncvloop, (int)age, (int)agefin, *ncvyear); */
2387: free_vector(min,1,nlstate);
2388: free_vector(max,1,nlstate);
2389: free_vector(meandiff,1,nlstate);
1.126 brouard 2390: return prlim;
2391: }
1.169 brouard 2392: } /* age loop */
1.208 brouard 2393: /* After some age loop it doesn't converge */
1.209 brouard 2394: printf("Warning: the stable prevalence at age %d did not converge with the required precision (%g > ftolpl=%g) within %.0f years. Try to lower 'ftolpl'. \n\
1.208 brouard 2395: Earliest age to start was %d-%d=%d, ncvloop=%d, ncvyear=%d\n", (int)age, maxmax, ftolpl, delaymax, (int)age, (int)delaymax, (int)agefin, ncvloop, *ncvyear);
1.209 brouard 2396: /* 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); */
2397: free_vector(min,1,nlstate);
2398: free_vector(max,1,nlstate);
2399: free_vector(meandiff,1,nlstate);
1.208 brouard 2400:
1.169 brouard 2401: return prlim; /* should not reach here */
1.126 brouard 2402: }
2403:
1.217 brouard 2404:
2405: /**** Back Prevalence limit (stable or period prevalence) ****************/
2406:
1.218 brouard 2407: /* 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) */
2408: /* 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) */
2409: double **bprevalim(double **bprlim, double ***prevacurrent, int nlstate, double x[], double age, double ftolpl, int *ncvyear, int ij)
1.217 brouard 2410: {
1.218 brouard 2411: /* Computes the prevalence limit in each live state at age x and covariate ij by left multiplying the unit
1.217 brouard 2412: matrix by transitions matrix until convergence is reached with precision ftolpl */
2413: /* Wx= Wx-1 Px-1= Wx-2 Px-2 Px-1 = Wx-n Px-n ... Px-2 Px-1 I */
2414: /* Wx is row vector: population in state 1, population in state 2, population dead */
2415: /* or prevalence in state 1, prevalence in state 2, 0 */
2416: /* newm is the matrix after multiplications, its rows are identical at a factor */
2417: /* Initial matrix pimij */
2418: /* {0.85204250825084937, 0.13044499163996345, 0.017512500109187184, */
2419: /* 0.090851990222114765, 0.88271245433047185, 0.026435555447413338, */
2420: /* 0, 0 , 1} */
2421: /*
2422: * and after some iteration: */
2423: /* {0.45504275246439968, 0.42731458730878791, 0.11764266022681241, */
2424: /* 0.45201005341706885, 0.42865420071559901, 0.11933574586733192, */
2425: /* 0, 0 , 1} */
2426: /* And prevalence by suppressing the deaths are close to identical rows in prlim: */
2427: /* {0.51571254859325999, 0.4842874514067399, */
2428: /* 0.51326036147820708, 0.48673963852179264} */
2429: /* If we start from prlim again, prlim tends to a constant matrix */
2430:
2431: int i, ii,j,k;
2432: double *min, *max, *meandiff, maxmax,sumnew=0.;
2433: /* double **matprod2(); */ /* test */
2434: double **out, cov[NCOVMAX+1], **bmij();
2435: double **newm;
1.218 brouard 2436: double **dnewm, **doldm, **dsavm; /* for use */
2437: double **oldm, **savm; /* for use */
2438:
1.217 brouard 2439: double agefin, delaymax=200. ; /* 100 Max number of years to converge */
2440: int ncvloop=0;
2441:
2442: min=vector(1,nlstate);
2443: max=vector(1,nlstate);
2444: meandiff=vector(1,nlstate);
2445:
1.218 brouard 2446: dnewm=ddnewms; doldm=ddoldms; dsavm=ddsavms;
2447: oldm=oldms; savm=savms;
2448:
2449: /* Starting with matrix unity */
2450: for (ii=1;ii<=nlstate+ndeath;ii++)
2451: for (j=1;j<=nlstate+ndeath;j++){
1.217 brouard 2452: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
2453: }
2454:
2455: cov[1]=1.;
2456:
2457: /* Even if hstepm = 1, at least one multiplication by the unit matrix */
2458: /* Start at agefin= age, computes the matrix of passage and loops decreasing agefin until convergence is reached */
1.218 brouard 2459: /* for(agefin=age+stepm/YEARM; agefin<=age+delaymax; agefin=agefin+stepm/YEARM){ /\* A changer en age *\/ */
2460: for(agefin=age; agefin<AGESUP; agefin=agefin+stepm/YEARM){ /* A changer en age */
1.217 brouard 2461: ncvloop++;
1.218 brouard 2462: newm=savm; /* oldm should be kept from previous iteration or unity at start */
2463: /* newm points to the allocated table savm passed by the function it can be written, savm could be reallocated */
1.217 brouard 2464: /* Covariates have to be included here again */
2465: cov[2]=agefin;
2466: if(nagesqr==1)
2467: cov[3]= agefin*agefin;;
2468: for (k=1; k<=cptcovn;k++) {
2469: /* cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,Tvar[k])]; */
2470: cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,k)];
2471: /* 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])]); */
2472: }
2473: /*wrong? for (k=1; k<=cptcovage;k++) cov[2+Tage[k]]=cov[2+Tage[k]]*cov[2]; */
2474: /* for (k=1; k<=cptcovage;k++) cov[2+nagesqr+Tage[k]]=nbcode[Tvar[k]][codtabm(ij,Tvar[k])]*cov[2]; */
2475: for (k=1; k<=cptcovage;k++) cov[2+nagesqr+Tage[k]]=nbcode[Tvar[k]][codtabm(ij,k)]*cov[2];
2476: for (k=1; k<=cptcovprod;k++) /* Useless */
2477: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,Tvard[k][1])] * nbcode[Tvard[k][2]][codtabm(ij,Tvard[k][2])]; */
2478: cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)] * nbcode[Tvard[k][2]][codtabm(ij,k)];
2479:
2480: /*printf("ij=%d cptcovprod=%d tvar=%d ", ij, cptcovprod, Tvar[1]);*/
2481: /*printf("ij=%d cov[3]=%lf cov[4]=%lf \n",ij, cov[3],cov[4]);*/
2482: /*printf("ij=%d cov[3]=%lf \n",ij, cov[3]);*/
2483: /* savm=pmij(pmmij,cov,ncovmodel,x,nlstate); */
2484: /* out=matprod2(newm, pmij(pmmij,cov,ncovmodel,x,nlstate),1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm); /\* Bug Valgrind *\/ */
1.218 brouard 2485: /* ij should be linked to the correct index of cov */
2486: /* age and covariate values ij are in 'cov', but we need to pass
2487: * ij for the observed prevalence at age and status and covariate
2488: * number: prevacurrent[(int)agefin][ii][ij]
2489: */
2490: /* 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 *\/ */
2491: /* 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 *\/ */
2492: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, bmij(pmmij,cov,ncovmodel,x,nlstate,prevacurrent,ij)); /* Bug Valgrind */
1.217 brouard 2493: savm=oldm;
2494: oldm=newm;
2495: for(j=1; j<=nlstate; j++){
2496: max[j]=0.;
2497: min[j]=1.;
2498: }
2499: for(j=1; j<=nlstate; j++){
2500: for(i=1;i<=nlstate;i++){
1.218 brouard 2501: /* bprlim[i][j]= newm[i][j]/(1-sumnew); */
2502: bprlim[i][j]= newm[i][j];
2503: max[i]=FMAX(max[i],bprlim[i][j]); /* Max in line */
2504: min[i]=FMIN(min[i],bprlim[i][j]);
1.217 brouard 2505: }
2506: }
1.218 brouard 2507:
1.217 brouard 2508: maxmax=0.;
2509: for(i=1; i<=nlstate; i++){
2510: meandiff[i]=(max[i]-min[i])/(max[i]+min[i])*2.; /* mean difference for each column */
2511: maxmax=FMAX(maxmax,meandiff[i]);
2512: /* 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); */
2513: } /* j loop */
2514: *ncvyear= -( (int)age- (int)agefin);
1.218 brouard 2515: /* printf("Back maxmax=%lf ncvloop=%d, age=%d, agefin=%d ncvyear=%d \n", maxmax, ncvloop, (int)age, (int)agefin, *ncvyear);*/
1.217 brouard 2516: if(maxmax < ftolpl){
1.220 brouard 2517: /* printf("OK Back maxmax=%lf ncvloop=%d, age=%d, agefin=%d ncvyear=%d \n", maxmax, ncvloop, (int)age, (int)agefin, *ncvyear); */
1.217 brouard 2518: free_vector(min,1,nlstate);
2519: free_vector(max,1,nlstate);
2520: free_vector(meandiff,1,nlstate);
2521: return bprlim;
2522: }
2523: } /* age loop */
2524: /* After some age loop it doesn't converge */
2525: printf("Warning: the back stable prevalence at age %d did not converge with the required precision (%g > ftolpl=%g) within %.0f years. Try to lower 'ftolpl'. \n\
2526: 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);
2527: /* 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); */
2528: free_vector(min,1,nlstate);
2529: free_vector(max,1,nlstate);
2530: free_vector(meandiff,1,nlstate);
2531:
2532: return bprlim; /* should not reach here */
2533: }
2534:
1.126 brouard 2535: /*************** transition probabilities ***************/
2536:
2537: double **pmij(double **ps, double *cov, int ncovmodel, double *x, int nlstate )
2538: {
1.138 brouard 2539: /* According to parameters values stored in x and the covariate's values stored in cov,
2540: computes the probability to be observed in state j being in state i by appying the
2541: model to the ncovmodel covariates (including constant and age).
2542: lnpijopii=ln(pij/pii)= aij+bij*age+cij*v1+dij*v2+... = sum_nc=1^ncovmodel xij(nc)*cov[nc]
2543: and, according on how parameters are entered, the position of the coefficient xij(nc) of the
2544: ncth covariate in the global vector x is given by the formula:
2545: j<i nc+((i-1)*(nlstate+ndeath-1)+j-1)*ncovmodel
2546: j>=i nc + ((i-1)*(nlstate+ndeath-1)+(j-2))*ncovmodel
2547: Computes ln(pij/pii) (lnpijopii), deduces pij/pii by exponentiation,
2548: sums on j different of i to get 1-pii/pii, deduces pii, and then all pij.
2549: Outputs ps[i][j] the probability to be observed in j being in j according to
2550: the values of the covariates cov[nc] and corresponding parameter values x[nc+shiftij]
2551: */
2552: double s1, lnpijopii;
1.126 brouard 2553: /*double t34;*/
1.164 brouard 2554: int i,j, nc, ii, jj;
1.126 brouard 2555:
1.223 brouard 2556: for(i=1; i<= nlstate; i++){
2557: for(j=1; j<i;j++){
2558: for (nc=1, lnpijopii=0.;nc <=ncovmodel; nc++){
2559: /*lnpijopii += param[i][j][nc]*cov[nc];*/
2560: lnpijopii += x[nc+((i-1)*(nlstate+ndeath-1)+j-1)*ncovmodel]*cov[nc];
2561: /* printf("Int j<i s1=%.17e, lnpijopii=%.17e\n",s1,lnpijopii); */
2562: }
2563: ps[i][j]=lnpijopii; /* In fact ln(pij/pii) */
2564: /* printf("s1=%.17e, lnpijopii=%.17e\n",s1,lnpijopii); */
2565: }
2566: for(j=i+1; j<=nlstate+ndeath;j++){
2567: for (nc=1, lnpijopii=0.;nc <=ncovmodel; nc++){
2568: /*lnpijopii += x[(i-1)*nlstate*ncovmodel+(j-2)*ncovmodel+nc+(i-1)*(ndeath-1)*ncovmodel]*cov[nc];*/
2569: lnpijopii += x[nc + ((i-1)*(nlstate+ndeath-1)+(j-2))*ncovmodel]*cov[nc];
2570: /* printf("Int j>i s1=%.17e, lnpijopii=%.17e %lx %lx\n",s1,lnpijopii,s1,lnpijopii); */
2571: }
2572: ps[i][j]=lnpijopii; /* In fact ln(pij/pii) */
2573: }
2574: }
1.218 brouard 2575:
1.223 brouard 2576: for(i=1; i<= nlstate; i++){
2577: s1=0;
2578: for(j=1; j<i; j++){
2579: s1+=exp(ps[i][j]); /* In fact sums pij/pii */
2580: /*printf("debug1 %d %d ps=%lf exp(ps)=%lf s1+=%lf\n",i,j,ps[i][j],exp(ps[i][j]),s1); */
2581: }
2582: for(j=i+1; j<=nlstate+ndeath; j++){
2583: s1+=exp(ps[i][j]); /* In fact sums pij/pii */
2584: /*printf("debug2 %d %d ps=%lf exp(ps)=%lf s1+=%lf\n",i,j,ps[i][j],exp(ps[i][j]),s1); */
2585: }
2586: /* s1= sum_{j<>i} pij/pii=(1-pii)/pii and thus pii is known from s1 */
2587: ps[i][i]=1./(s1+1.);
2588: /* Computing other pijs */
2589: for(j=1; j<i; j++)
2590: ps[i][j]= exp(ps[i][j])*ps[i][i];
2591: for(j=i+1; j<=nlstate+ndeath; j++)
2592: ps[i][j]= exp(ps[i][j])*ps[i][i];
2593: /* ps[i][nlstate+1]=1.-s1- ps[i][i];*/ /* Sum should be 1 */
2594: } /* end i */
1.218 brouard 2595:
1.223 brouard 2596: for(ii=nlstate+1; ii<= nlstate+ndeath; ii++){
2597: for(jj=1; jj<= nlstate+ndeath; jj++){
2598: ps[ii][jj]=0;
2599: ps[ii][ii]=1;
2600: }
2601: }
1.218 brouard 2602:
2603:
1.223 brouard 2604: /* for(ii=1; ii<= nlstate+ndeath; ii++){ */
2605: /* for(jj=1; jj<= nlstate+ndeath; jj++){ */
2606: /* printf(" pmij ps[%d][%d]=%lf ",ii,jj,ps[ii][jj]); */
2607: /* } */
2608: /* printf("\n "); */
2609: /* } */
2610: /* printf("\n ");printf("%lf ",cov[2]);*/
2611: /*
2612: for(i=1; i<= npar; i++) printf("%f ",x[i]);
1.218 brouard 2613: goto end;*/
1.223 brouard 2614: return ps;
1.126 brouard 2615: }
2616:
1.218 brouard 2617: /*************** backward transition probabilities ***************/
2618:
2619: /* 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 ) */
2620: /* double **bmij(double **ps, double *cov, int ncovmodel, double *x, int nlstate, double ***prevacurrent, double ***dnewm, double **doldm, double **dsavm, int ij ) */
2621: double **bmij(double **ps, double *cov, int ncovmodel, double *x, int nlstate, double ***prevacurrent, int ij )
2622: {
1.222 brouard 2623: /* Computes the backward probability at age agefin and covariate ij
2624: * and returns in **ps as well as **bmij.
2625: */
1.218 brouard 2626: int i, ii, j,k;
1.222 brouard 2627:
2628: double **out, **pmij();
2629: double sumnew=0.;
1.218 brouard 2630: double agefin;
1.222 brouard 2631:
2632: double **dnewm, **dsavm, **doldm;
2633: double **bbmij;
2634:
1.218 brouard 2635: doldm=ddoldms; /* global pointers */
1.222 brouard 2636: dnewm=ddnewms;
2637: dsavm=ddsavms;
2638:
2639: agefin=cov[2];
2640: /* bmij *//* age is cov[2], ij is included in cov, but we need for
2641: the observed prevalence (with this covariate ij) */
2642: dsavm=pmij(pmmij,cov,ncovmodel,x,nlstate);
2643: /* We do have the matrix Px in savm and we need pij */
2644: for (j=1;j<=nlstate+ndeath;j++){
2645: sumnew=0.; /* w1 p11 + w2 p21 only on live states */
2646: for (ii=1;ii<=nlstate;ii++){
2647: sumnew+=dsavm[ii][j]*prevacurrent[(int)agefin][ii][ij];
2648: } /* sumnew is (N11+N21)/N..= N.1/N.. = sum on i of w_i pij */
2649: for (ii=1;ii<=nlstate+ndeath;ii++){
2650: if(sumnew >= 1.e-10){
2651: /* if(agefin >= agemaxpar && agefin <= agemaxpar+stepm/YEARM){ */
2652: /* doldm[ii][j]=(ii==j ? 1./sumnew : 0.0); */
2653: /* }else if(agefin >= agemaxpar+stepm/YEARM){ */
2654: /* doldm[ii][j]=(ii==j ? 1./sumnew : 0.0); */
2655: /* }else */
2656: doldm[ii][j]=(ii==j ? 1./sumnew : 0.0);
2657: }else{
2658: printf("ii=%d, i=%d, doldm=%lf dsavm=%lf, probs=%lf, sumnew=%lf,agefin=%d\n",ii,j,doldm[ii][j],dsavm[ii][j],prevacurrent[(int)agefin][ii][ij],sumnew, (int)agefin);
2659: }
2660: } /*End ii */
2661: } /* End j, At the end doldm is diag[1/(w_1p1i+w_2 p2i)] */
2662: /* left Product of this diag matrix by dsavm=Px (newm=dsavm*doldm) */
2663: bbmij=matprod2(dnewm, dsavm,1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, doldm); /* Bug Valgrind */
2664: /* dsavm=doldm; /\* dsavm is now diag [1/(w_1p1i+w_2 p2i)] but can be overwritten*\/ */
2665: /* doldm=dnewm; /\* doldm is now Px * diag [1/(w_1p1i+w_2 p2i)] *\/ */
2666: /* dnewm=dsavm; /\* doldm is now Px * diag [1/(w_1p1i+w_2 p2i)] *\/ */
2667: /* left Product of this matrix by diag matrix of prevalences (savm) */
2668: for (j=1;j<=nlstate+ndeath;j++){
2669: for (ii=1;ii<=nlstate+ndeath;ii++){
2670: dsavm[ii][j]=(ii==j ? prevacurrent[(int)agefin][ii][ij] : 0.0);
2671: }
2672: } /* End j, At the end oldm is diag[1/(w_1p1i+w_2 p2i)] */
2673: ps=matprod2(doldm, dsavm,1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, dnewm); /* Bug Valgrind */
2674: /* newm or out is now diag[w_i] * Px * diag [1/(w_1p1i+w_2 p2i)] */
2675: /* end bmij */
2676: return ps;
1.218 brouard 2677: }
1.217 brouard 2678: /*************** transition probabilities ***************/
2679:
1.218 brouard 2680: double **bpmij(double **ps, double *cov, int ncovmodel, double *x, int nlstate )
1.217 brouard 2681: {
2682: /* According to parameters values stored in x and the covariate's values stored in cov,
2683: computes the probability to be observed in state j being in state i by appying the
2684: model to the ncovmodel covariates (including constant and age).
2685: lnpijopii=ln(pij/pii)= aij+bij*age+cij*v1+dij*v2+... = sum_nc=1^ncovmodel xij(nc)*cov[nc]
2686: and, according on how parameters are entered, the position of the coefficient xij(nc) of the
2687: ncth covariate in the global vector x is given by the formula:
2688: j<i nc+((i-1)*(nlstate+ndeath-1)+j-1)*ncovmodel
2689: j>=i nc + ((i-1)*(nlstate+ndeath-1)+(j-2))*ncovmodel
2690: Computes ln(pij/pii) (lnpijopii), deduces pij/pii by exponentiation,
2691: sums on j different of i to get 1-pii/pii, deduces pii, and then all pij.
2692: Outputs ps[i][j] the probability to be observed in j being in j according to
2693: the values of the covariates cov[nc] and corresponding parameter values x[nc+shiftij]
2694: */
2695: double s1, lnpijopii;
2696: /*double t34;*/
2697: int i,j, nc, ii, jj;
2698:
1.218 brouard 2699: for(i=1; i<= nlstate; i++){
2700: for(j=1; j<i;j++){
2701: for (nc=1, lnpijopii=0.;nc <=ncovmodel; nc++){
2702: /*lnpijopii += param[i][j][nc]*cov[nc];*/
2703: lnpijopii += x[nc+((i-1)*(nlstate+ndeath-1)+j-1)*ncovmodel]*cov[nc];
2704: /* printf("Int j<i s1=%.17e, lnpijopii=%.17e\n",s1,lnpijopii); */
2705: }
2706: ps[i][j]=lnpijopii; /* In fact ln(pij/pii) */
2707: /* printf("s1=%.17e, lnpijopii=%.17e\n",s1,lnpijopii); */
2708: }
2709: for(j=i+1; j<=nlstate+ndeath;j++){
2710: for (nc=1, lnpijopii=0.;nc <=ncovmodel; nc++){
2711: /*lnpijopii += x[(i-1)*nlstate*ncovmodel+(j-2)*ncovmodel+nc+(i-1)*(ndeath-1)*ncovmodel]*cov[nc];*/
2712: lnpijopii += x[nc + ((i-1)*(nlstate+ndeath-1)+(j-2))*ncovmodel]*cov[nc];
2713: /* printf("Int j>i s1=%.17e, lnpijopii=%.17e %lx %lx\n",s1,lnpijopii,s1,lnpijopii); */
2714: }
2715: ps[i][j]=lnpijopii; /* In fact ln(pij/pii) */
2716: }
2717: }
2718:
2719: for(i=1; i<= nlstate; i++){
2720: s1=0;
2721: for(j=1; j<i; j++){
2722: s1+=exp(ps[i][j]); /* In fact sums pij/pii */
2723: /*printf("debug1 %d %d ps=%lf exp(ps)=%lf s1+=%lf\n",i,j,ps[i][j],exp(ps[i][j]),s1); */
2724: }
2725: for(j=i+1; j<=nlstate+ndeath; j++){
2726: s1+=exp(ps[i][j]); /* In fact sums pij/pii */
2727: /*printf("debug2 %d %d ps=%lf exp(ps)=%lf s1+=%lf\n",i,j,ps[i][j],exp(ps[i][j]),s1); */
2728: }
2729: /* s1= sum_{j<>i} pij/pii=(1-pii)/pii and thus pii is known from s1 */
2730: ps[i][i]=1./(s1+1.);
2731: /* Computing other pijs */
2732: for(j=1; j<i; j++)
2733: ps[i][j]= exp(ps[i][j])*ps[i][i];
2734: for(j=i+1; j<=nlstate+ndeath; j++)
2735: ps[i][j]= exp(ps[i][j])*ps[i][i];
2736: /* ps[i][nlstate+1]=1.-s1- ps[i][i];*/ /* Sum should be 1 */
2737: } /* end i */
2738:
2739: for(ii=nlstate+1; ii<= nlstate+ndeath; ii++){
2740: for(jj=1; jj<= nlstate+ndeath; jj++){
2741: ps[ii][jj]=0;
2742: ps[ii][ii]=1;
2743: }
2744: }
2745: /* Added for backcast */ /* Transposed matrix too */
2746: for(jj=1; jj<= nlstate+ndeath; jj++){
2747: s1=0.;
2748: for(ii=1; ii<= nlstate+ndeath; ii++){
2749: s1+=ps[ii][jj];
2750: }
2751: for(ii=1; ii<= nlstate; ii++){
2752: ps[ii][jj]=ps[ii][jj]/s1;
2753: }
2754: }
2755: /* Transposition */
2756: for(jj=1; jj<= nlstate+ndeath; jj++){
2757: for(ii=jj; ii<= nlstate+ndeath; ii++){
2758: s1=ps[ii][jj];
2759: ps[ii][jj]=ps[jj][ii];
2760: ps[jj][ii]=s1;
2761: }
2762: }
2763: /* for(ii=1; ii<= nlstate+ndeath; ii++){ */
2764: /* for(jj=1; jj<= nlstate+ndeath; jj++){ */
2765: /* printf(" pmij ps[%d][%d]=%lf ",ii,jj,ps[ii][jj]); */
2766: /* } */
2767: /* printf("\n "); */
2768: /* } */
2769: /* printf("\n ");printf("%lf ",cov[2]);*/
2770: /*
2771: for(i=1; i<= npar; i++) printf("%f ",x[i]);
2772: goto end;*/
2773: return ps;
1.217 brouard 2774: }
2775:
2776:
1.126 brouard 2777: /**************** Product of 2 matrices ******************/
2778:
1.145 brouard 2779: double **matprod2(double **out, double **in,int nrl, int nrh, int ncl, int nch, int ncolol, int ncoloh, double **b)
1.126 brouard 2780: {
2781: /* Computes the matrix product of in(1,nrh-nrl+1)(1,nch-ncl+1) times
2782: b(1,nch-ncl+1)(1,ncoloh-ncolol+1) into out(...) */
2783: /* in, b, out are matrice of pointers which should have been initialized
2784: before: only the contents of out is modified. The function returns
2785: a pointer to pointers identical to out */
1.145 brouard 2786: int i, j, k;
1.126 brouard 2787: for(i=nrl; i<= nrh; i++)
1.145 brouard 2788: for(k=ncolol; k<=ncoloh; k++){
2789: out[i][k]=0.;
2790: for(j=ncl; j<=nch; j++)
2791: out[i][k] +=in[i][j]*b[j][k];
2792: }
1.126 brouard 2793: return out;
2794: }
2795:
2796:
2797: /************* Higher Matrix Product ***************/
2798:
2799: double ***hpxij(double ***po, int nhstepm, double age, int hstepm, double *x, int nlstate, int stepm, double **oldm, double **savm, int ij )
2800: {
1.218 brouard 2801: /* Computes the transition matrix starting at age 'age' and combination of covariate values corresponding to ij over
1.126 brouard 2802: 'nhstepm*hstepm*stepm' months (i.e. until
2803: age (in years) age+nhstepm*hstepm*stepm/12) by multiplying
2804: nhstepm*hstepm matrices.
2805: Output is stored in matrix po[i][j][h] for h every 'hstepm' step
2806: (typically every 2 years instead of every month which is too big
2807: for the memory).
2808: Model is determined by parameters x and covariates have to be
2809: included manually here.
2810:
2811: */
2812:
2813: int i, j, d, h, k;
1.131 brouard 2814: double **out, cov[NCOVMAX+1];
1.126 brouard 2815: double **newm;
1.187 brouard 2816: double agexact;
1.214 brouard 2817: double agebegin, ageend;
1.126 brouard 2818:
2819: /* Hstepm could be zero and should return the unit matrix */
2820: for (i=1;i<=nlstate+ndeath;i++)
2821: for (j=1;j<=nlstate+ndeath;j++){
2822: oldm[i][j]=(i==j ? 1.0 : 0.0);
2823: po[i][j][0]=(i==j ? 1.0 : 0.0);
2824: }
2825: /* Even if hstepm = 1, at least one multiplication by the unit matrix */
2826: for(h=1; h <=nhstepm; h++){
2827: for(d=1; d <=hstepm; d++){
2828: newm=savm;
2829: /* Covariates have to be included here again */
2830: cov[1]=1.;
1.214 brouard 2831: agexact=age+((h-1)*hstepm + (d-1))*stepm/YEARM; /* age just before transition */
1.187 brouard 2832: cov[2]=agexact;
2833: if(nagesqr==1)
1.227 brouard 2834: cov[3]= agexact*agexact;
1.131 brouard 2835: for (k=1; k<=cptcovn;k++)
1.227 brouard 2836: cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,k)];
2837: /* cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,Tvar[k])]; */
1.186 brouard 2838: for (k=1; k<=cptcovage;k++) /* Should start at cptcovn+1 */
1.227 brouard 2839: /* cov[2+Tage[k]]=cov[2+Tage[k]]*cov[2]; */
2840: cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,k)]*cov[2];
2841: /* cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,Tvar[Tage[k]])]*cov[2]; */
1.145 brouard 2842: for (k=1; k<=cptcovprod;k++) /* Useless because included in cptcovn */
1.227 brouard 2843: cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)]*nbcode[Tvard[k][2]][codtabm(ij,k)];
2844: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,Tvard[k][1])]*nbcode[Tvard[k][2]][codtabm(ij,Tvard[k][2])]; */
2845:
2846:
1.126 brouard 2847: /*printf("hxi cptcov=%d cptcode=%d\n",cptcov,cptcode);*/
2848: /*printf("h=%d d=%d age=%f cov=%f\n",h,d,age,cov[2]);*/
1.218 brouard 2849: /* right multiplication of oldm by the current matrix */
1.126 brouard 2850: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath,
2851: pmij(pmmij,cov,ncovmodel,x,nlstate));
1.217 brouard 2852: /* if((int)age == 70){ */
2853: /* printf(" Forward hpxij age=%d agexact=%f d=%d nhstepm=%d hstepm=%d\n", (int) age, agexact, d, nhstepm, hstepm); */
2854: /* for(i=1; i<=nlstate+ndeath; i++) { */
2855: /* printf("%d pmmij ",i); */
2856: /* for(j=1;j<=nlstate+ndeath;j++) { */
2857: /* printf("%f ",pmmij[i][j]); */
2858: /* } */
2859: /* printf(" oldm "); */
2860: /* for(j=1;j<=nlstate+ndeath;j++) { */
2861: /* printf("%f ",oldm[i][j]); */
2862: /* } */
2863: /* printf("\n"); */
2864: /* } */
2865: /* } */
1.126 brouard 2866: savm=oldm;
2867: oldm=newm;
2868: }
2869: for(i=1; i<=nlstate+ndeath; i++)
2870: for(j=1;j<=nlstate+ndeath;j++) {
1.218 brouard 2871: po[i][j][h]=newm[i][j];
2872: /*if(h==nhstepm) printf("po[%d][%d][%d]=%f ",i,j,h,po[i][j][h]);*/
1.126 brouard 2873: }
1.128 brouard 2874: /*printf("h=%d ",h);*/
1.126 brouard 2875: } /* end h */
1.218 brouard 2876: /* printf("\n H=%d \n",h); */
1.126 brouard 2877: return po;
2878: }
2879:
1.217 brouard 2880: /************* Higher Back Matrix Product ***************/
1.218 brouard 2881: /* double ***hbxij(double ***po, int nhstepm, double age, int hstepm, double *x, double ***prevacurrent, int nlstate, int stepm, double **oldm, double **savm, double **dnewm, double **doldm, double **dsavm, int ij ) */
1.222 brouard 2882: double ***hbxij(double ***po, int nhstepm, double age, int hstepm, double *x, double ***prevacurrent, int nlstate, int stepm, int ij )
1.217 brouard 2883: {
1.218 brouard 2884: /* Computes the transition matrix starting at age 'age' over
1.217 brouard 2885: 'nhstepm*hstepm*stepm' months (i.e. until
1.218 brouard 2886: age (in years) age+nhstepm*hstepm*stepm/12) by multiplying
2887: nhstepm*hstepm matrices.
2888: Output is stored in matrix po[i][j][h] for h every 'hstepm' step
2889: (typically every 2 years instead of every month which is too big
1.217 brouard 2890: for the memory).
1.218 brouard 2891: Model is determined by parameters x and covariates have to be
2892: included manually here.
1.217 brouard 2893:
1.222 brouard 2894: */
1.217 brouard 2895:
2896: int i, j, d, h, k;
2897: double **out, cov[NCOVMAX+1];
2898: double **newm;
2899: double agexact;
2900: double agebegin, ageend;
1.222 brouard 2901: double **oldm, **savm;
1.217 brouard 2902:
1.222 brouard 2903: oldm=oldms;savm=savms;
1.217 brouard 2904: /* Hstepm could be zero and should return the unit matrix */
2905: for (i=1;i<=nlstate+ndeath;i++)
2906: for (j=1;j<=nlstate+ndeath;j++){
2907: oldm[i][j]=(i==j ? 1.0 : 0.0);
2908: po[i][j][0]=(i==j ? 1.0 : 0.0);
2909: }
2910: /* Even if hstepm = 1, at least one multiplication by the unit matrix */
2911: for(h=1; h <=nhstepm; h++){
2912: for(d=1; d <=hstepm; d++){
2913: newm=savm;
2914: /* Covariates have to be included here again */
2915: cov[1]=1.;
2916: agexact=age-((h-1)*hstepm + (d-1))*stepm/YEARM; /* age just before transition */
2917: /* agexact=age+((h-1)*hstepm + (d-1))*stepm/YEARM; /\* age just before transition *\/ */
2918: cov[2]=agexact;
2919: if(nagesqr==1)
1.222 brouard 2920: cov[3]= agexact*agexact;
1.218 brouard 2921: for (k=1; k<=cptcovn;k++)
1.222 brouard 2922: cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,k)];
2923: /* cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,Tvar[k])]; */
1.217 brouard 2924: for (k=1; k<=cptcovage;k++) /* Should start at cptcovn+1 */
1.222 brouard 2925: /* cov[2+Tage[k]]=cov[2+Tage[k]]*cov[2]; */
2926: cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,k)]*cov[2];
2927: /* cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,Tvar[Tage[k]])]*cov[2]; */
1.217 brouard 2928: for (k=1; k<=cptcovprod;k++) /* Useless because included in cptcovn */
1.222 brouard 2929: cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)]*nbcode[Tvard[k][2]][codtabm(ij,k)];
2930: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,Tvard[k][1])]*nbcode[Tvard[k][2]][codtabm(ij,Tvard[k][2])]; */
1.218 brouard 2931:
2932:
1.217 brouard 2933: /*printf("hxi cptcov=%d cptcode=%d\n",cptcov,cptcode);*/
2934: /*printf("h=%d d=%d age=%f cov=%f\n",h,d,age,cov[2]);*/
1.218 brouard 2935: /* Careful transposed matrix */
1.222 brouard 2936: /* age is in cov[2] */
1.218 brouard 2937: /* out=matprod2(newm, bmij(pmmij,cov,ncovmodel,x,nlstate,prevacurrent, dnewm, doldm, dsavm,ij),\ */
1.222 brouard 2938: /* 1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm); */
1.218 brouard 2939: out=matprod2(newm, bmij(pmmij,cov,ncovmodel,x,nlstate,prevacurrent,ij),\
1.222 brouard 2940: 1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm);
1.217 brouard 2941: /* if((int)age == 70){ */
2942: /* printf(" Backward hbxij age=%d agexact=%f d=%d nhstepm=%d hstepm=%d\n", (int) age, agexact, d, nhstepm, hstepm); */
2943: /* for(i=1; i<=nlstate+ndeath; i++) { */
2944: /* printf("%d pmmij ",i); */
2945: /* for(j=1;j<=nlstate+ndeath;j++) { */
2946: /* printf("%f ",pmmij[i][j]); */
2947: /* } */
2948: /* printf(" oldm "); */
2949: /* for(j=1;j<=nlstate+ndeath;j++) { */
2950: /* printf("%f ",oldm[i][j]); */
2951: /* } */
2952: /* printf("\n"); */
2953: /* } */
2954: /* } */
2955: savm=oldm;
2956: oldm=newm;
2957: }
2958: for(i=1; i<=nlstate+ndeath; i++)
2959: for(j=1;j<=nlstate+ndeath;j++) {
1.222 brouard 2960: po[i][j][h]=newm[i][j];
2961: /*if(h==nhstepm) printf("po[%d][%d][%d]=%f ",i,j,h,po[i][j][h]);*/
1.217 brouard 2962: }
2963: /*printf("h=%d ",h);*/
2964: } /* end h */
1.222 brouard 2965: /* printf("\n H=%d \n",h); */
1.217 brouard 2966: return po;
2967: }
2968:
2969:
1.162 brouard 2970: #ifdef NLOPT
2971: double myfunc(unsigned n, const double *p1, double *grad, void *pd){
2972: double fret;
2973: double *xt;
2974: int j;
2975: myfunc_data *d2 = (myfunc_data *) pd;
2976: /* xt = (p1-1); */
2977: xt=vector(1,n);
2978: for (j=1;j<=n;j++) xt[j]=p1[j-1]; /* xt[1]=p1[0] */
2979:
2980: fret=(d2->function)(xt); /* p xt[1]@8 is fine */
2981: /* fret=(*func)(xt); /\* p xt[1]@8 is fine *\/ */
2982: printf("Function = %.12lf ",fret);
2983: for (j=1;j<=n;j++) printf(" %d %.8lf", j, xt[j]);
2984: printf("\n");
2985: free_vector(xt,1,n);
2986: return fret;
2987: }
2988: #endif
1.126 brouard 2989:
2990: /*************** log-likelihood *************/
2991: double func( double *x)
2992: {
1.226 brouard 2993: int i, ii, j, k, mi, d, kk;
2994: int ioffset=0;
2995: double l, ll[NLSTATEMAX+1], cov[NCOVMAX+1];
2996: double **out;
2997: double lli; /* Individual log likelihood */
2998: int s1, s2;
1.228 brouard 2999: int iv=0, iqv=0, itv=0, iqtv=0 ; /* Index of varying covariate, fixed quantitative cov, time varying covariate, quantitative time varying covariate */
1.226 brouard 3000: double bbh, survp;
3001: long ipmx;
3002: double agexact;
3003: /*extern weight */
3004: /* We are differentiating ll according to initial status */
3005: /* for (i=1;i<=npar;i++) printf("%f ", x[i]);*/
3006: /*for(i=1;i<imx;i++)
3007: printf(" %d\n",s[4][i]);
3008: */
1.162 brouard 3009:
1.226 brouard 3010: ++countcallfunc;
1.162 brouard 3011:
1.226 brouard 3012: cov[1]=1.;
1.126 brouard 3013:
1.226 brouard 3014: for(k=1; k<=nlstate; k++) ll[k]=0.;
1.224 brouard 3015: ioffset=0;
1.226 brouard 3016: if(mle==1){
3017: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
3018: /* Computes the values of the ncovmodel covariates of the model
3019: depending if the covariates are fixed or varying (age dependent) and stores them in cov[]
3020: Then computes with function pmij which return a matrix p[i][j] giving the elementary probability
3021: to be observed in j being in i according to the model.
3022: */
3023: ioffset=2+nagesqr+cptcovage;
3024: /* for (k=1; k<=cptcovn;k++){ /\* Simple and product covariates without age* products *\/ */
3025: for (k=1; k<=ncoveff;k++){ /* Simple and product covariates without age* products */
3026: cov[++ioffset]=covar[Tvar[k]][i];
3027: }
3028: for(iqv=1; iqv <= nqfveff; iqv++){ /* Quantitatives and Fixed covariates */
1.228 brouard 3029: cov[++ioffset]=coqvar[Tvar[iqv]][i];
1.226 brouard 3030: }
3031:
3032: /* In model V2+V1*V4+age*V3+V3*V2 Tvar[1] is V2, Tvar[2=V1*V4]
3033: is 6, Tvar[3=age*V3] should not be computed because of age Tvar[4=V3*V2]
3034: has been calculated etc */
3035: /* For an individual i, wav[i] gives the number of effective waves */
3036: /* We compute the contribution to Likelihood of each effective transition
3037: mw[mi][i] is real wave of the mi th effectve wave */
3038: /* Then statuses are computed at each begin and end of an effective wave s1=s[ mw[mi][i] ][i];
3039: s2=s[mw[mi+1][i]][i];
3040: And the iv th varying covariate is the cotvar[mw[mi+1][i]][iv][i]
3041: But if the variable is not in the model TTvar[iv] is the real variable effective in the model:
3042: meaning that decodemodel should be used cotvar[mw[mi+1][i]][TTvar[iv]][i]
3043: */
3044: for(mi=1; mi<= wav[i]-1; mi++){
1.231 ! brouard 3045: for(itv=1; itv <= ntveff; itv++){ /* Varying dummy covariates */
! 3046: /* cov[ioffset+itv]=cotvar[mw[mi][i]][Tvar[itv]][i]; /\* Not sure, Tvar V4+V3+V5 Tvaraff ? *\/ */
! 3047: cov[ioffset+itv]=cotvar[mw[mi][i]][TmodelInvind[itv]][i];
! 3048: }
! 3049: for(iqtv=1; iqtv <= nqtveff; iqtv++){ /* Varying quantitatives covariates */
! 3050: if(cotqvar[mw[mi][i]][iqtv][i] == -1){
! 3051: printf("i=%d, mi=%d, iqtv=%d, cotqvar[mw[mi][i]][iqtv][i]=%f",i,mi,iqtv,cotqvar[mw[mi][i]][iqtv][i]);
! 3052: }
! 3053: cov[ioffset+ntveff+iqtv]=cotqvar[mw[mi][i]][TmodelInvQind[iqtv]][i];
! 3054: /* cov[ioffset+ntveff+iqtv]=cotqvar[mw[mi][i]][iqtv][i]; */
! 3055: }
! 3056: /* ioffset=2+nagesqr+cptcovn+nqv+ntv+nqtv; */
! 3057: for (ii=1;ii<=nlstate+ndeath;ii++)
! 3058: for (j=1;j<=nlstate+ndeath;j++){
! 3059: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
! 3060: savm[ii][j]=(ii==j ? 1.0 : 0.0);
! 3061: }
! 3062: for(d=0; d<dh[mi][i]; d++){
! 3063: newm=savm;
! 3064: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM;
! 3065: cov[2]=agexact;
! 3066: if(nagesqr==1)
! 3067: cov[3]= agexact*agexact; /* Should be changed here */
! 3068: for (kk=1; kk<=cptcovage;kk++) {
! 3069: cov[Tage[kk]+2+nagesqr]=covar[Tvar[Tage[kk]]][i]*agexact; /* Tage[kk] gives the data-covariate associated with age */
! 3070: }
! 3071: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
! 3072: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
! 3073: savm=oldm;
! 3074: oldm=newm;
! 3075: } /* end mult */
1.224 brouard 3076:
1.231 ! brouard 3077: /*lli=log(out[s[mw[mi][i]][i]][s[mw[mi+1][i]][i]]);*/ /* Original formula */
! 3078: /* But now since version 0.9 we anticipate for bias at large stepm.
! 3079: * If stepm is larger than one month (smallest stepm) and if the exact delay
! 3080: * (in months) between two waves is not a multiple of stepm, we rounded to
! 3081: * the nearest (and in case of equal distance, to the lowest) interval but now
! 3082: * we keep into memory the bias bh[mi][i] and also the previous matrix product
! 3083: * (i.e to dh[mi][i]-1) saved in 'savm'. Then we inter(extra)polate the
! 3084: * probability in order to take into account the bias as a fraction of the way
! 3085: * from savm to out if bh is negative or even beyond if bh is positive. bh varies
! 3086: * -stepm/2 to stepm/2 .
! 3087: * For stepm=1 the results are the same as for previous versions of Imach.
! 3088: * For stepm > 1 the results are less biased than in previous versions.
! 3089: */
! 3090: s1=s[mw[mi][i]][i];
! 3091: s2=s[mw[mi+1][i]][i];
! 3092: bbh=(double)bh[mi][i]/(double)stepm;
! 3093: /* bias bh is positive if real duration
! 3094: * is higher than the multiple of stepm and negative otherwise.
! 3095: */
! 3096: /* lli= (savm[s1][s2]>1.e-8 ?(1.+bbh)*log(out[s1][s2])- bbh*log(savm[s1][s2]):log((1.+bbh)*out[s1][s2]));*/
! 3097: if( s2 > nlstate){
! 3098: /* i.e. if s2 is a death state and if the date of death is known
! 3099: then the contribution to the likelihood is the probability to
! 3100: die between last step unit time and current step unit time,
! 3101: which is also equal to probability to die before dh
! 3102: minus probability to die before dh-stepm .
! 3103: In version up to 0.92 likelihood was computed
! 3104: as if date of death was unknown. Death was treated as any other
! 3105: health state: the date of the interview describes the actual state
! 3106: and not the date of a change in health state. The former idea was
! 3107: to consider that at each interview the state was recorded
! 3108: (healthy, disable or death) and IMaCh was corrected; but when we
! 3109: introduced the exact date of death then we should have modified
! 3110: the contribution of an exact death to the likelihood. This new
! 3111: contribution is smaller and very dependent of the step unit
! 3112: stepm. It is no more the probability to die between last interview
! 3113: and month of death but the probability to survive from last
! 3114: interview up to one month before death multiplied by the
! 3115: probability to die within a month. Thanks to Chris
! 3116: Jackson for correcting this bug. Former versions increased
! 3117: mortality artificially. The bad side is that we add another loop
! 3118: which slows down the processing. The difference can be up to 10%
! 3119: lower mortality.
! 3120: */
! 3121: /* If, at the beginning of the maximization mostly, the
! 3122: cumulative probability or probability to be dead is
! 3123: constant (ie = 1) over time d, the difference is equal to
! 3124: 0. out[s1][3] = savm[s1][3]: probability, being at state
! 3125: s1 at precedent wave, to be dead a month before current
! 3126: wave is equal to probability, being at state s1 at
! 3127: precedent wave, to be dead at mont of the current
! 3128: wave. Then the observed probability (that this person died)
! 3129: is null according to current estimated parameter. In fact,
! 3130: it should be very low but not zero otherwise the log go to
! 3131: infinity.
! 3132: */
1.183 brouard 3133: /* #ifdef INFINITYORIGINAL */
3134: /* lli=log(out[s1][s2] - savm[s1][s2]); */
3135: /* #else */
3136: /* if ((out[s1][s2] - savm[s1][s2]) < mytinydouble) */
3137: /* lli=log(mytinydouble); */
3138: /* else */
3139: /* lli=log(out[s1][s2] - savm[s1][s2]); */
3140: /* #endif */
1.226 brouard 3141: lli=log(out[s1][s2] - savm[s1][s2]);
1.216 brouard 3142:
1.226 brouard 3143: } else if ( s2==-1 ) { /* alive */
3144: for (j=1,survp=0. ; j<=nlstate; j++)
3145: survp += (1.+bbh)*out[s1][j]- bbh*savm[s1][j];
3146: /*survp += out[s1][j]; */
3147: lli= log(survp);
3148: }
3149: else if (s2==-4) {
3150: for (j=3,survp=0. ; j<=nlstate; j++)
3151: survp += (1.+bbh)*out[s1][j]- bbh*savm[s1][j];
3152: lli= log(survp);
3153: }
3154: else if (s2==-5) {
3155: for (j=1,survp=0. ; j<=2; j++)
3156: survp += (1.+bbh)*out[s1][j]- bbh*savm[s1][j];
3157: lli= log(survp);
3158: }
3159: else{
3160: lli= log((1.+bbh)*out[s1][s2]- bbh*savm[s1][s2]); /* linear interpolation */
3161: /* 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 */
3162: }
3163: /*lli=(1.+bbh)*log(out[s1][s2])- bbh*log(savm[s1][s2]);*/
3164: /*if(lli ==000.0)*/
3165: /*printf("bbh= %f lli=%f savm=%f out=%f %d\n",bbh,lli,savm[s1][s2], out[s[mw[mi][i]][i]][s[mw[mi+1][i]][i]],i); */
3166: ipmx +=1;
3167: sw += weight[i];
3168: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
3169: /* if (lli < log(mytinydouble)){ */
3170: /* 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); */
3171: /* 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]); */
3172: /* } */
3173: } /* end of wave */
3174: } /* end of individual */
3175: } else if(mle==2){
3176: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
3177: for (k=1; k<=cptcovn;k++) cov[2+nagesqr+k]=covar[Tvar[k]][i];
3178: for(mi=1; mi<= wav[i]-1; mi++){
3179: for (ii=1;ii<=nlstate+ndeath;ii++)
3180: for (j=1;j<=nlstate+ndeath;j++){
3181: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
3182: savm[ii][j]=(ii==j ? 1.0 : 0.0);
3183: }
3184: for(d=0; d<=dh[mi][i]; d++){
3185: newm=savm;
3186: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM;
3187: cov[2]=agexact;
3188: if(nagesqr==1)
3189: cov[3]= agexact*agexact;
3190: for (kk=1; kk<=cptcovage;kk++) {
3191: cov[Tage[kk]+2+nagesqr]=covar[Tvar[Tage[kk]]][i]*agexact;
3192: }
3193: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
3194: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
3195: savm=oldm;
3196: oldm=newm;
3197: } /* end mult */
3198:
3199: s1=s[mw[mi][i]][i];
3200: s2=s[mw[mi+1][i]][i];
3201: bbh=(double)bh[mi][i]/(double)stepm;
3202: 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 */
3203: ipmx +=1;
3204: sw += weight[i];
3205: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
3206: } /* end of wave */
3207: } /* end of individual */
3208: } else if(mle==3){ /* exponential inter-extrapolation */
3209: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
3210: for (k=1; k<=cptcovn;k++) cov[2+nagesqr+k]=covar[Tvar[k]][i];
3211: for(mi=1; mi<= wav[i]-1; mi++){
3212: for (ii=1;ii<=nlstate+ndeath;ii++)
3213: for (j=1;j<=nlstate+ndeath;j++){
3214: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
3215: savm[ii][j]=(ii==j ? 1.0 : 0.0);
3216: }
3217: for(d=0; d<dh[mi][i]; d++){
3218: newm=savm;
3219: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM;
3220: cov[2]=agexact;
3221: if(nagesqr==1)
3222: cov[3]= agexact*agexact;
3223: for (kk=1; kk<=cptcovage;kk++) {
3224: cov[Tage[kk]+2+nagesqr]=covar[Tvar[Tage[kk]]][i]*agexact;
3225: }
3226: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
3227: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
3228: savm=oldm;
3229: oldm=newm;
3230: } /* end mult */
3231:
3232: s1=s[mw[mi][i]][i];
3233: s2=s[mw[mi+1][i]][i];
3234: bbh=(double)bh[mi][i]/(double)stepm;
3235: 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 */
3236: ipmx +=1;
3237: sw += weight[i];
3238: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
3239: } /* end of wave */
3240: } /* end of individual */
3241: }else if (mle==4){ /* ml=4 no inter-extrapolation */
3242: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
3243: for (k=1; k<=cptcovn;k++) cov[2+nagesqr+k]=covar[Tvar[k]][i];
3244: for(mi=1; mi<= wav[i]-1; mi++){
3245: for (ii=1;ii<=nlstate+ndeath;ii++)
3246: for (j=1;j<=nlstate+ndeath;j++){
3247: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
3248: savm[ii][j]=(ii==j ? 1.0 : 0.0);
3249: }
3250: for(d=0; d<dh[mi][i]; d++){
3251: newm=savm;
3252: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM;
3253: cov[2]=agexact;
3254: if(nagesqr==1)
3255: cov[3]= agexact*agexact;
3256: for (kk=1; kk<=cptcovage;kk++) {
3257: cov[Tage[kk]+2+nagesqr]=covar[Tvar[Tage[kk]]][i]*agexact;
3258: }
1.126 brouard 3259:
1.226 brouard 3260: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
3261: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
3262: savm=oldm;
3263: oldm=newm;
3264: } /* end mult */
3265:
3266: s1=s[mw[mi][i]][i];
3267: s2=s[mw[mi+1][i]][i];
3268: if( s2 > nlstate){
3269: lli=log(out[s1][s2] - savm[s1][s2]);
3270: } else if ( s2==-1 ) { /* alive */
3271: for (j=1,survp=0. ; j<=nlstate; j++)
3272: survp += out[s1][j];
3273: lli= log(survp);
3274: }else{
3275: lli=log(out[s[mw[mi][i]][i]][s[mw[mi+1][i]][i]]); /* Original formula */
3276: }
3277: ipmx +=1;
3278: sw += weight[i];
3279: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
1.126 brouard 3280: /* printf("i=%6d s1=%1d s2=%1d mi=%1d mw=%1d dh=%3d prob=%10.6f w=%6.4f out=%10.6f sav=%10.6f\n",i,s1,s2,mi,mw[mi][i],dh[mi][i],exp(lli),weight[i],out[s1][s2],savm[s1][s2]); */
1.226 brouard 3281: } /* end of wave */
3282: } /* end of individual */
3283: }else{ /* ml=5 no inter-extrapolation no jackson =0.8a */
3284: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
3285: for (k=1; k<=cptcovn;k++) cov[2+nagesqr+k]=covar[Tvar[k]][i];
3286: for(mi=1; mi<= wav[i]-1; mi++){
3287: for (ii=1;ii<=nlstate+ndeath;ii++)
3288: for (j=1;j<=nlstate+ndeath;j++){
3289: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
3290: savm[ii][j]=(ii==j ? 1.0 : 0.0);
3291: }
3292: for(d=0; d<dh[mi][i]; d++){
3293: newm=savm;
3294: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM;
3295: cov[2]=agexact;
3296: if(nagesqr==1)
3297: cov[3]= agexact*agexact;
3298: for (kk=1; kk<=cptcovage;kk++) {
3299: cov[Tage[kk]+2+nagesqr]=covar[Tvar[Tage[kk]]][i]*agexact;
3300: }
1.126 brouard 3301:
1.226 brouard 3302: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
3303: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
3304: savm=oldm;
3305: oldm=newm;
3306: } /* end mult */
3307:
3308: s1=s[mw[mi][i]][i];
3309: s2=s[mw[mi+1][i]][i];
3310: lli=log(out[s[mw[mi][i]][i]][s[mw[mi+1][i]][i]]); /* Original formula */
3311: ipmx +=1;
3312: sw += weight[i];
3313: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
3314: /*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]);*/
3315: } /* end of wave */
3316: } /* end of individual */
3317: } /* End of if */
3318: for(k=1,l=0.; k<=nlstate; k++) l += ll[k];
3319: /* printf("l1=%f l2=%f ",ll[1],ll[2]); */
3320: l= l*ipmx/sw; /* To get the same order of magnitude as if weight=1 for every body */
3321: return -l;
1.126 brouard 3322: }
3323:
3324: /*************** log-likelihood *************/
3325: double funcone( double *x)
3326: {
1.228 brouard 3327: /* Same as func but slower because of a lot of printf and if */
1.126 brouard 3328: int i, ii, j, k, mi, d, kk;
1.228 brouard 3329: int ioffset=0;
1.131 brouard 3330: double l, ll[NLSTATEMAX+1], cov[NCOVMAX+1];
1.126 brouard 3331: double **out;
3332: double lli; /* Individual log likelihood */
3333: double llt;
3334: int s1, s2;
1.228 brouard 3335: int iv=0, iqv=0, itv=0, iqtv=0 ; /* Index of varying covariate, fixed quantitative cov, time varying covariate, quantitative time varying covariate */
3336:
1.126 brouard 3337: double bbh, survp;
1.187 brouard 3338: double agexact;
1.214 brouard 3339: double agebegin, ageend;
1.126 brouard 3340: /*extern weight */
3341: /* We are differentiating ll according to initial status */
3342: /* for (i=1;i<=npar;i++) printf("%f ", x[i]);*/
3343: /*for(i=1;i<imx;i++)
3344: printf(" %d\n",s[4][i]);
3345: */
3346: cov[1]=1.;
3347:
3348: for(k=1; k<=nlstate; k++) ll[k]=0.;
1.224 brouard 3349: ioffset=0;
3350: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
1.225 brouard 3351: ioffset=2+nagesqr+cptcovage;
1.224 brouard 3352: /* for (k=1; k<=cptcovn;k++) cov[2+nagesqr+k]=covar[Tvar[k]][i]; */
1.231 ! brouard 3353: for (k=1; k<=ncoveff;k++){ /* Simple and product fixed Dummy covariates without age* products */
1.230 brouard 3354: cov[++ioffset]=covar[TvarFD[k]][i];/* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1, only V1 is fixed (k=6)*/
1.225 brouard 3355: }
1.231 ! brouard 3356: for (k=1; k<=nqfveff;k++){ /* Simple and product fixed Quantitative covariates without age* products */
! 3357: 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?)*/
1.225 brouard 3358: }
1.231 ! brouard 3359: /* for(iqv=1; iqv <= nqfveff; iqv++){ /\* Quantitative fixed covariates *\/ */
! 3360: /* cov[++ioffset]=coqvar[Tvar[iqv]][i]; /\* Only V2 k=6 and V1*V2 7 *\/ */
! 3361: /* } */
1.225 brouard 3362:
1.226 brouard 3363: for(mi=1; mi<= wav[i]-1; mi++){ /* Varying with waves */
1.231 ! brouard 3364: for(itv=1; itv <= ntveff; itv++){ /* Varying dummy covariates (single??)*/
! 3365: /* iv= Tvar[Tmodelind[ioffset-2-nagesqr-cptcovage+itv]]-ncovcol-nqv; /\* Counting the # varying covariate from 1 to ntveff *\/ */
! 3366: /* cov[ioffset+iv]=cotvar[mw[mi][i]][iv][i]; */
! 3367: k=ioffset-2-nagesqr-cptcovage+itv; /* position in simple model */
! 3368: cov[ioffset+itv]=cotvar[mw[mi][i]][TmodelInvind[itv]][i];
! 3369: /* printf(" i=%d,mi=%d,itv=%d,TmodelInvind[itv]=%d,cotvar[mw[mi][i]][TmodelInvind[itv]][i]=%f\n", i, mi, itv, TmodelInvind[itv],cotvar[mw[mi][i]][TmodelInvind[itv]][i]); */
1.225 brouard 3370: }
3371: for(iqtv=1; iqtv <= nqtveff; iqtv++){ /* Varying quantitatives covariates */
1.231 ! brouard 3372: iv=TmodelInvQind[iqtv]; /* Counting the # varying covariate from 1 to ntveff */
! 3373: /* 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]); */
! 3374: cov[ioffset+ntveff+iqtv]=cotqvar[mw[mi][i]][TmodelInvQind[iqtv]][i];
1.225 brouard 3375: }
1.126 brouard 3376: for (ii=1;ii<=nlstate+ndeath;ii++)
1.231 ! brouard 3377: for (j=1;j<=nlstate+ndeath;j++){
! 3378: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
! 3379: savm[ii][j]=(ii==j ? 1.0 : 0.0);
! 3380: }
1.214 brouard 3381:
3382: agebegin=agev[mw[mi][i]][i]; /* Age at beginning of effective wave */
3383: ageend=agev[mw[mi][i]][i] + (dh[mi][i])*stepm/YEARM; /* Age at end of effective wave and at the end of transition */
3384: for(d=0; d<dh[mi][i]; d++){ /* Delay between two effective waves */
1.231 ! brouard 3385: /*dh[m][i] or dh[mw[mi][i]][i] is the delay between two effective waves m=mw[mi][i]
! 3386: and mw[mi+1][i]. dh depends on stepm.*/
! 3387: newm=savm;
! 3388: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM;
! 3389: cov[2]=agexact;
! 3390: if(nagesqr==1)
! 3391: cov[3]= agexact*agexact;
! 3392: for (kk=1; kk<=cptcovage;kk++) {
! 3393: cov[Tage[kk]+2+nagesqr]=covar[Tvar[Tage[kk]]][i]*agexact;
! 3394: }
! 3395: /* printf("i=%d,mi=%d,d=%d,mw[mi][i]=%d\n",i, mi,d,mw[mi][i]); */
! 3396: /* savm=pmij(pmmij,cov,ncovmodel,x,nlstate); */
! 3397: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
! 3398: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
! 3399: /* out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath, */
! 3400: /* 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate)); */
! 3401: savm=oldm;
! 3402: oldm=newm;
1.126 brouard 3403: } /* end mult */
3404:
3405: s1=s[mw[mi][i]][i];
3406: s2=s[mw[mi+1][i]][i];
1.217 brouard 3407: /* if(s2==-1){ */
3408: /* printf(" s1=%d, s2=%d i=%d \n", s1, s2, i); */
3409: /* /\* exit(1); *\/ */
3410: /* } */
1.126 brouard 3411: bbh=(double)bh[mi][i]/(double)stepm;
3412: /* bias is positive if real duration
3413: * is higher than the multiple of stepm and negative otherwise.
3414: */
3415: if( s2 > nlstate && (mle <5) ){ /* Jackson */
1.225 brouard 3416: lli=log(out[s1][s2] - savm[s1][s2]);
1.216 brouard 3417: } else if ( s2==-1 ) { /* alive */
1.225 brouard 3418: for (j=1,survp=0. ; j<=nlstate; j++)
3419: survp += (1.+bbh)*out[s1][j]- bbh*savm[s1][j];
3420: lli= log(survp);
1.126 brouard 3421: }else if (mle==1){
1.225 brouard 3422: lli= log((1.+bbh)*out[s1][s2]- bbh*savm[s1][s2]); /* linear interpolation */
1.126 brouard 3423: } else if(mle==2){
1.225 brouard 3424: lli= (savm[s1][s2]>(double)1.e-8 ?log((1.+bbh)*out[s1][s2]- bbh*savm[s1][s2]):log((1.+bbh)*out[s1][s2])); /* linear interpolation */
1.126 brouard 3425: } else if(mle==3){ /* exponential inter-extrapolation */
1.225 brouard 3426: lli= (savm[s1][s2]>(double)1.e-8 ?(1.+bbh)*log(out[s1][s2])- bbh*log(savm[s1][s2]):log((1.+bbh)*out[s1][s2])); /* exponential inter-extrapolation */
1.126 brouard 3427: } else if (mle==4){ /* mle=4 no inter-extrapolation */
1.225 brouard 3428: lli=log(out[s1][s2]); /* Original formula */
1.136 brouard 3429: } else{ /* mle=0 back to 1 */
1.225 brouard 3430: lli= log((1.+bbh)*out[s1][s2]- bbh*savm[s1][s2]); /* linear interpolation */
3431: /*lli=log(out[s1][s2]); */ /* Original formula */
1.126 brouard 3432: } /* End of if */
3433: ipmx +=1;
3434: sw += weight[i];
3435: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
1.132 brouard 3436: /*printf("i=%6d s1=%1d s2=%1d mi=%1d mw=%1d dh=%3d prob=%10.6f w=%6.4f out=%10.6f sav=%10.6f\n",i,s1,s2,mi,mw[mi][i],dh[mi][i],exp(lli),weight[i],out[s1][s2],savm[s1][s2]); */
1.126 brouard 3437: if(globpr){
1.225 brouard 3438: fprintf(ficresilk,"%9ld %6.1f %6.1f %6d %2d %2d %2d %2d %3d %15.6f %8.4f %8.3f\
1.126 brouard 3439: %11.6f %11.6f %11.6f ", \
1.225 brouard 3440: num[i], agebegin, ageend, i,s1,s2,mi,mw[mi][i],dh[mi][i],exp(lli),weight[i],weight[i]*gipmx/gsw,
3441: 2*weight[i]*lli,out[s1][s2],savm[s1][s2]);
3442: for(k=1,llt=0.,l=0.; k<=nlstate; k++){
3443: llt +=ll[k]*gipmx/gsw;
3444: fprintf(ficresilk," %10.6f",-ll[k]*gipmx/gsw);
3445: }
3446: fprintf(ficresilk," %10.6f\n", -llt);
1.126 brouard 3447: }
3448: } /* end of wave */
3449: } /* end of individual */
3450: for(k=1,l=0.; k<=nlstate; k++) l += ll[k];
3451: /* printf("l1=%f l2=%f ",ll[1],ll[2]); */
3452: l= l*ipmx/sw; /* To get the same order of magnitude as if weight=1 for every body */
3453: if(globpr==0){ /* First time we count the contributions and weights */
3454: gipmx=ipmx;
3455: gsw=sw;
3456: }
3457: return -l;
3458: }
3459:
3460:
3461: /*************** function likelione ***********/
3462: void likelione(FILE *ficres,double p[], int npar, int nlstate, int *globpri, long *ipmx, double *sw, double *fretone, double (*funcone)(double []))
3463: {
3464: /* This routine should help understanding what is done with
3465: the selection of individuals/waves and
3466: to check the exact contribution to the likelihood.
3467: Plotting could be done.
3468: */
3469: int k;
3470:
3471: if(*globpri !=0){ /* Just counts and sums, no printings */
1.201 brouard 3472: strcpy(fileresilk,"ILK_");
1.202 brouard 3473: strcat(fileresilk,fileresu);
1.126 brouard 3474: if((ficresilk=fopen(fileresilk,"w"))==NULL) {
3475: printf("Problem with resultfile: %s\n", fileresilk);
3476: fprintf(ficlog,"Problem with resultfile: %s\n", fileresilk);
3477: }
1.214 brouard 3478: 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");
3479: fprintf(ficresilk, "#num_i ageb agend i s1 s2 mi mw dh likeli weight %%weight 2wlli out sav ");
1.126 brouard 3480: /* i,s1,s2,mi,mw[mi][i],dh[mi][i],exp(lli),weight[i],2*weight[i]*lli,out[s1][s2],savm[s1][s2]); */
3481: for(k=1; k<=nlstate; k++)
3482: fprintf(ficresilk," -2*gipw/gsw*weight*ll[%d]++",k);
3483: fprintf(ficresilk," -2*gipw/gsw*weight*ll(total)\n");
3484: }
3485:
3486: *fretone=(*funcone)(p);
3487: if(*globpri !=0){
3488: fclose(ficresilk);
1.205 brouard 3489: if (mle ==0)
3490: fprintf(fichtm,"\n<br>File of contributions to the likelihood computed with initial parameters and mle = %d.",mle);
3491: else if(mle >=1)
3492: fprintf(fichtm,"\n<br>File of contributions to the likelihood computed with optimized parameters mle = %d.",mle);
3493: fprintf(fichtm," You should at least run with mle >= 1 to get starting values corresponding to the optimized parameters in order to visualize the real contribution of each individual/wave: <a href=\"%s\">%s</a><br>\n",subdirf(fileresilk),subdirf(fileresilk));
1.207 brouard 3494:
1.208 brouard 3495:
3496: for (k=1; k<= nlstate ; k++) {
1.211 brouard 3497: fprintf(fichtm,"<br>- Probability p<sub>%dj</sub> by origin %d and destination j. Dot's sizes are related to corresponding weight: <a href=\"%s-p%dj.png\">%s-p%dj.png</a><br> \
1.208 brouard 3498: <img src=\"%s-p%dj.png\">",k,k,subdirf2(optionfilefiname,"ILK_"),k,subdirf2(optionfilefiname,"ILK_"),k,subdirf2(optionfilefiname,"ILK_"),k);
3499: }
1.207 brouard 3500: fprintf(fichtm,"<br>- The function drawn is -2Log(L) in Log scale: by state of origin <a href=\"%s-ori.png\">%s-ori.png</a><br> \
1.204 brouard 3501: <img src=\"%s-ori.png\">",subdirf2(optionfilefiname,"ILK_"),subdirf2(optionfilefiname,"ILK_"),subdirf2(optionfilefiname,"ILK_"));
1.207 brouard 3502: fprintf(fichtm,"<br>- and by state of destination <a href=\"%s-dest.png\">%s-dest.png</a><br> \
1.204 brouard 3503: <img src=\"%s-dest.png\">",subdirf2(optionfilefiname,"ILK_"),subdirf2(optionfilefiname,"ILK_"),subdirf2(optionfilefiname,"ILK_"));
1.207 brouard 3504: fflush(fichtm);
1.205 brouard 3505: }
1.126 brouard 3506: return;
3507: }
3508:
3509:
3510: /*********** Maximum Likelihood Estimation ***************/
3511:
3512: void mlikeli(FILE *ficres,double p[], int npar, int ncovmodel, int nlstate, double ftol, double (*func)(double []))
3513: {
1.165 brouard 3514: int i,j, iter=0;
1.126 brouard 3515: double **xi;
3516: double fret;
3517: double fretone; /* Only one call to likelihood */
3518: /* char filerespow[FILENAMELENGTH];*/
1.162 brouard 3519:
3520: #ifdef NLOPT
3521: int creturn;
3522: nlopt_opt opt;
3523: /* double lb[9] = { -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL }; /\* lower bounds *\/ */
3524: double *lb;
3525: double minf; /* the minimum objective value, upon return */
3526: double * p1; /* Shifted parameters from 0 instead of 1 */
3527: myfunc_data dinst, *d = &dinst;
3528: #endif
3529:
3530:
1.126 brouard 3531: xi=matrix(1,npar,1,npar);
3532: for (i=1;i<=npar;i++)
3533: for (j=1;j<=npar;j++)
3534: xi[i][j]=(i==j ? 1.0 : 0.0);
3535: printf("Powell\n"); fprintf(ficlog,"Powell\n");
1.201 brouard 3536: strcpy(filerespow,"POW_");
1.126 brouard 3537: strcat(filerespow,fileres);
3538: if((ficrespow=fopen(filerespow,"w"))==NULL) {
3539: printf("Problem with resultfile: %s\n", filerespow);
3540: fprintf(ficlog,"Problem with resultfile: %s\n", filerespow);
3541: }
3542: fprintf(ficrespow,"# Powell\n# iter -2*LL");
3543: for (i=1;i<=nlstate;i++)
3544: for(j=1;j<=nlstate+ndeath;j++)
3545: if(j!=i)fprintf(ficrespow," p%1d%1d",i,j);
3546: fprintf(ficrespow,"\n");
1.162 brouard 3547: #ifdef POWELL
1.126 brouard 3548: powell(p,xi,npar,ftol,&iter,&fret,func);
1.162 brouard 3549: #endif
1.126 brouard 3550:
1.162 brouard 3551: #ifdef NLOPT
3552: #ifdef NEWUOA
3553: opt = nlopt_create(NLOPT_LN_NEWUOA,npar);
3554: #else
3555: opt = nlopt_create(NLOPT_LN_BOBYQA,npar);
3556: #endif
3557: lb=vector(0,npar-1);
3558: for (i=0;i<npar;i++) lb[i]= -HUGE_VAL;
3559: nlopt_set_lower_bounds(opt, lb);
3560: nlopt_set_initial_step1(opt, 0.1);
3561:
3562: p1= (p+1); /* p *(p+1)@8 and p *(p1)@8 are equal p1[0]=p[1] */
3563: d->function = func;
3564: printf(" Func %.12lf \n",myfunc(npar,p1,NULL,d));
3565: nlopt_set_min_objective(opt, myfunc, d);
3566: nlopt_set_xtol_rel(opt, ftol);
3567: if ((creturn=nlopt_optimize(opt, p1, &minf)) < 0) {
3568: printf("nlopt failed! %d\n",creturn);
3569: }
3570: else {
3571: printf("found minimum after %d evaluations (NLOPT=%d)\n", countcallfunc ,NLOPT);
3572: printf("found minimum at f(%g,%g) = %0.10g\n", p[0], p[1], minf);
3573: iter=1; /* not equal */
3574: }
3575: nlopt_destroy(opt);
3576: #endif
1.126 brouard 3577: free_matrix(xi,1,npar,1,npar);
3578: fclose(ficrespow);
1.203 brouard 3579: printf("\n#Number of iterations & function calls = %d & %d, -2 Log likelihood = %.12f\n",iter, countcallfunc,func(p));
3580: fprintf(ficlog,"\n#Number of iterations & function calls = %d & %d, -2 Log likelihood = %.12f\n",iter, countcallfunc,func(p));
1.180 brouard 3581: fprintf(ficres,"#Number of iterations & function calls = %d & %d, -2 Log likelihood = %.12f\n",iter, countcallfunc,func(p));
1.126 brouard 3582:
3583: }
3584:
3585: /**** Computes Hessian and covariance matrix ***/
1.203 brouard 3586: void hesscov(double **matcov, double **hess, double p[], int npar, double delti[], double ftolhess, double (*func)(double []))
1.126 brouard 3587: {
3588: double **a,**y,*x,pd;
1.203 brouard 3589: /* double **hess; */
1.164 brouard 3590: int i, j;
1.126 brouard 3591: int *indx;
3592:
3593: double hessii(double p[], double delta, int theta, double delti[],double (*func)(double []),int npar);
1.203 brouard 3594: double hessij(double p[], double **hess, double delti[], int i, int j,double (*func)(double []),int npar);
1.126 brouard 3595: void lubksb(double **a, int npar, int *indx, double b[]) ;
3596: void ludcmp(double **a, int npar, int *indx, double *d) ;
3597: double gompertz(double p[]);
1.203 brouard 3598: /* hess=matrix(1,npar,1,npar); */
1.126 brouard 3599:
3600: printf("\nCalculation of the hessian matrix. Wait...\n");
3601: fprintf(ficlog,"\nCalculation of the hessian matrix. Wait...\n");
3602: for (i=1;i<=npar;i++){
1.203 brouard 3603: printf("%d-",i);fflush(stdout);
3604: fprintf(ficlog,"%d-",i);fflush(ficlog);
1.126 brouard 3605:
3606: hess[i][i]=hessii(p,ftolhess,i,delti,func,npar);
3607:
3608: /* printf(" %f ",p[i]);
3609: printf(" %lf %lf %lf",hess[i][i],ftolhess,delti[i]);*/
3610: }
3611:
3612: for (i=1;i<=npar;i++) {
3613: for (j=1;j<=npar;j++) {
3614: if (j>i) {
1.203 brouard 3615: printf(".%d-%d",i,j);fflush(stdout);
3616: fprintf(ficlog,".%d-%d",i,j);fflush(ficlog);
3617: hess[i][j]=hessij(p,hess, delti,i,j,func,npar);
1.126 brouard 3618:
3619: hess[j][i]=hess[i][j];
3620: /*printf(" %lf ",hess[i][j]);*/
3621: }
3622: }
3623: }
3624: printf("\n");
3625: fprintf(ficlog,"\n");
3626:
3627: printf("\nInverting the hessian to get the covariance matrix. Wait...\n");
3628: fprintf(ficlog,"\nInverting the hessian to get the covariance matrix. Wait...\n");
3629:
3630: a=matrix(1,npar,1,npar);
3631: y=matrix(1,npar,1,npar);
3632: x=vector(1,npar);
3633: indx=ivector(1,npar);
3634: for (i=1;i<=npar;i++)
3635: for (j=1;j<=npar;j++) a[i][j]=hess[i][j];
3636: ludcmp(a,npar,indx,&pd);
3637:
3638: for (j=1;j<=npar;j++) {
3639: for (i=1;i<=npar;i++) x[i]=0;
3640: x[j]=1;
3641: lubksb(a,npar,indx,x);
3642: for (i=1;i<=npar;i++){
3643: matcov[i][j]=x[i];
3644: }
3645: }
3646:
3647: printf("\n#Hessian matrix#\n");
3648: fprintf(ficlog,"\n#Hessian matrix#\n");
3649: for (i=1;i<=npar;i++) {
3650: for (j=1;j<=npar;j++) {
1.203 brouard 3651: printf("%.6e ",hess[i][j]);
3652: fprintf(ficlog,"%.6e ",hess[i][j]);
1.126 brouard 3653: }
3654: printf("\n");
3655: fprintf(ficlog,"\n");
3656: }
3657:
1.203 brouard 3658: /* printf("\n#Covariance matrix#\n"); */
3659: /* fprintf(ficlog,"\n#Covariance matrix#\n"); */
3660: /* for (i=1;i<=npar;i++) { */
3661: /* for (j=1;j<=npar;j++) { */
3662: /* printf("%.6e ",matcov[i][j]); */
3663: /* fprintf(ficlog,"%.6e ",matcov[i][j]); */
3664: /* } */
3665: /* printf("\n"); */
3666: /* fprintf(ficlog,"\n"); */
3667: /* } */
3668:
1.126 brouard 3669: /* Recompute Inverse */
1.203 brouard 3670: /* for (i=1;i<=npar;i++) */
3671: /* for (j=1;j<=npar;j++) a[i][j]=matcov[i][j]; */
3672: /* ludcmp(a,npar,indx,&pd); */
3673:
3674: /* printf("\n#Hessian matrix recomputed#\n"); */
3675:
3676: /* for (j=1;j<=npar;j++) { */
3677: /* for (i=1;i<=npar;i++) x[i]=0; */
3678: /* x[j]=1; */
3679: /* lubksb(a,npar,indx,x); */
3680: /* for (i=1;i<=npar;i++){ */
3681: /* y[i][j]=x[i]; */
3682: /* printf("%.3e ",y[i][j]); */
3683: /* fprintf(ficlog,"%.3e ",y[i][j]); */
3684: /* } */
3685: /* printf("\n"); */
3686: /* fprintf(ficlog,"\n"); */
3687: /* } */
3688:
3689: /* Verifying the inverse matrix */
3690: #ifdef DEBUGHESS
3691: y=matprod2(y,hess,1,npar,1,npar,1,npar,matcov);
1.126 brouard 3692:
1.203 brouard 3693: printf("\n#Verification: multiplying the matrix of covariance by the Hessian matrix, should be unity:#\n");
3694: fprintf(ficlog,"\n#Verification: multiplying the matrix of covariance by the Hessian matrix. Should be unity:#\n");
1.126 brouard 3695:
3696: for (j=1;j<=npar;j++) {
3697: for (i=1;i<=npar;i++){
1.203 brouard 3698: printf("%.2f ",y[i][j]);
3699: fprintf(ficlog,"%.2f ",y[i][j]);
1.126 brouard 3700: }
3701: printf("\n");
3702: fprintf(ficlog,"\n");
3703: }
1.203 brouard 3704: #endif
1.126 brouard 3705:
3706: free_matrix(a,1,npar,1,npar);
3707: free_matrix(y,1,npar,1,npar);
3708: free_vector(x,1,npar);
3709: free_ivector(indx,1,npar);
1.203 brouard 3710: /* free_matrix(hess,1,npar,1,npar); */
1.126 brouard 3711:
3712:
3713: }
3714:
3715: /*************** hessian matrix ****************/
3716: double hessii(double x[], double delta, int theta, double delti[], double (*func)(double []), int npar)
1.203 brouard 3717: { /* Around values of x, computes the function func and returns the scales delti and hessian */
1.126 brouard 3718: int i;
3719: int l=1, lmax=20;
1.203 brouard 3720: double k1,k2, res, fx;
1.132 brouard 3721: double p2[MAXPARM+1]; /* identical to x */
1.126 brouard 3722: double delt=0.0001, delts, nkhi=10.,nkhif=1., khi=1.e-4;
3723: int k=0,kmax=10;
3724: double l1;
3725:
3726: fx=func(x);
3727: for (i=1;i<=npar;i++) p2[i]=x[i];
1.145 brouard 3728: for(l=0 ; l <=lmax; l++){ /* Enlarging the zone around the Maximum */
1.126 brouard 3729: l1=pow(10,l);
3730: delts=delt;
3731: for(k=1 ; k <kmax; k=k+1){
3732: delt = delta*(l1*k);
3733: p2[theta]=x[theta] +delt;
1.145 brouard 3734: k1=func(p2)-fx; /* Might be negative if too close to the theoretical maximum */
1.126 brouard 3735: p2[theta]=x[theta]-delt;
3736: k2=func(p2)-fx;
3737: /*res= (k1-2.0*fx+k2)/delt/delt; */
1.203 brouard 3738: res= (k1+k2)/delt/delt/2.; /* Divided by 2 because L and not 2*L */
1.126 brouard 3739:
1.203 brouard 3740: #ifdef DEBUGHESSII
1.126 brouard 3741: 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);
3742: 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);
3743: #endif
3744: /*if(fabs(k1-2.0*fx+k2) <1.e-13){ */
3745: if((k1 <khi/nkhi/2.) || (k2 <khi/nkhi/2.)){
3746: k=kmax;
3747: }
3748: else if((k1 >khi/nkhif) || (k2 >khi/nkhif)){ /* Keeps lastvalue before 3.84/2 KHI2 5% 1d.f. */
1.164 brouard 3749: k=kmax; l=lmax*10;
1.126 brouard 3750: }
3751: else if((k1 >khi/nkhi) || (k2 >khi/nkhi)){
3752: delts=delt;
3753: }
1.203 brouard 3754: } /* End loop k */
1.126 brouard 3755: }
3756: delti[theta]=delts;
3757: return res;
3758:
3759: }
3760:
1.203 brouard 3761: double hessij( double x[], double **hess, double delti[], int thetai,int thetaj,double (*func)(double []),int npar)
1.126 brouard 3762: {
3763: int i;
1.164 brouard 3764: int l=1, lmax=20;
1.126 brouard 3765: double k1,k2,k3,k4,res,fx;
1.132 brouard 3766: double p2[MAXPARM+1];
1.203 brouard 3767: int k, kmax=1;
3768: double v1, v2, cv12, lc1, lc2;
1.208 brouard 3769:
3770: int firstime=0;
1.203 brouard 3771:
1.126 brouard 3772: fx=func(x);
1.203 brouard 3773: for (k=1; k<=kmax; k=k+10) {
1.126 brouard 3774: for (i=1;i<=npar;i++) p2[i]=x[i];
1.203 brouard 3775: p2[thetai]=x[thetai]+delti[thetai]*k;
3776: p2[thetaj]=x[thetaj]+delti[thetaj]*k;
1.126 brouard 3777: k1=func(p2)-fx;
3778:
1.203 brouard 3779: p2[thetai]=x[thetai]+delti[thetai]*k;
3780: p2[thetaj]=x[thetaj]-delti[thetaj]*k;
1.126 brouard 3781: k2=func(p2)-fx;
3782:
1.203 brouard 3783: p2[thetai]=x[thetai]-delti[thetai]*k;
3784: p2[thetaj]=x[thetaj]+delti[thetaj]*k;
1.126 brouard 3785: k3=func(p2)-fx;
3786:
1.203 brouard 3787: p2[thetai]=x[thetai]-delti[thetai]*k;
3788: p2[thetaj]=x[thetaj]-delti[thetaj]*k;
1.126 brouard 3789: k4=func(p2)-fx;
1.203 brouard 3790: res=(k1-k2-k3+k4)/4.0/delti[thetai]/k/delti[thetaj]/k/2.; /* Because of L not 2*L */
3791: if(k1*k2*k3*k4 <0.){
1.208 brouard 3792: firstime=1;
1.203 brouard 3793: kmax=kmax+10;
1.208 brouard 3794: }
3795: if(kmax >=10 || firstime ==1){
1.218 brouard 3796: printf("Warning: directions %d-%d, you are not estimating the Hessian at the exact maximum likelihood; you may increase ftol=%.2e\n",thetai,thetaj, ftol);
3797: fprintf(ficlog,"Warning: directions %d-%d, you are not estimating the Hessian at the exact maximum likelihood; you may increase ftol=%.2e\n",thetai,thetaj, ftol);
1.203 brouard 3798: 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);
3799: 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);
3800: }
3801: #ifdef DEBUGHESSIJ
3802: v1=hess[thetai][thetai];
3803: v2=hess[thetaj][thetaj];
3804: cv12=res;
3805: /* Computing eigen value of Hessian matrix */
3806: lc1=((v1+v2)+sqrt((v1+v2)*(v1+v2) - 4*(v1*v2-cv12*cv12)))/2.;
3807: lc2=((v1+v2)-sqrt((v1+v2)*(v1+v2) - 4*(v1*v2-cv12*cv12)))/2.;
3808: if ((lc2 <0) || (lc1 <0) ){
3809: printf("Warning: sub Hessian matrix '%d%d' does not have positive eigen values \n",thetai,thetaj);
3810: fprintf(ficlog, "Warning: sub Hessian matrix '%d%d' does not have positive eigen values \n",thetai,thetaj);
3811: 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);
3812: 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);
3813: }
1.126 brouard 3814: #endif
3815: }
3816: return res;
3817: }
3818:
1.203 brouard 3819: /* Not done yet: Was supposed to fix if not exactly at the maximum */
3820: /* double hessij( double x[], double delti[], int thetai,int thetaj,double (*func)(double []),int npar) */
3821: /* { */
3822: /* int i; */
3823: /* int l=1, lmax=20; */
3824: /* double k1,k2,k3,k4,res,fx; */
3825: /* double p2[MAXPARM+1]; */
3826: /* double delt=0.0001, delts, nkhi=10.,nkhif=1., khi=1.e-4; */
3827: /* int k=0,kmax=10; */
3828: /* double l1; */
3829:
3830: /* fx=func(x); */
3831: /* for(l=0 ; l <=lmax; l++){ /\* Enlarging the zone around the Maximum *\/ */
3832: /* l1=pow(10,l); */
3833: /* delts=delt; */
3834: /* for(k=1 ; k <kmax; k=k+1){ */
3835: /* delt = delti*(l1*k); */
3836: /* for (i=1;i<=npar;i++) p2[i]=x[i]; */
3837: /* p2[thetai]=x[thetai]+delti[thetai]/k; */
3838: /* p2[thetaj]=x[thetaj]+delti[thetaj]/k; */
3839: /* k1=func(p2)-fx; */
3840:
3841: /* p2[thetai]=x[thetai]+delti[thetai]/k; */
3842: /* p2[thetaj]=x[thetaj]-delti[thetaj]/k; */
3843: /* k2=func(p2)-fx; */
3844:
3845: /* p2[thetai]=x[thetai]-delti[thetai]/k; */
3846: /* p2[thetaj]=x[thetaj]+delti[thetaj]/k; */
3847: /* k3=func(p2)-fx; */
3848:
3849: /* p2[thetai]=x[thetai]-delti[thetai]/k; */
3850: /* p2[thetaj]=x[thetaj]-delti[thetaj]/k; */
3851: /* k4=func(p2)-fx; */
3852: /* res=(k1-k2-k3+k4)/4.0/delti[thetai]*k/delti[thetaj]*k/2.; /\* Because of L not 2*L *\/ */
3853: /* #ifdef DEBUGHESSIJ */
3854: /* 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); */
3855: /* 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); */
3856: /* #endif */
3857: /* if((k1 <khi/nkhi/2.) || (k2 <khi/nkhi/2.)|| (k4 <khi/nkhi/2.)|| (k4 <khi/nkhi/2.)){ */
3858: /* k=kmax; */
3859: /* } */
3860: /* else if((k1 >khi/nkhif) || (k2 >khi/nkhif) || (k4 >khi/nkhif) || (k4 >khi/nkhif)){ /\* Keeps lastvalue before 3.84/2 KHI2 5% 1d.f. *\/ */
3861: /* k=kmax; l=lmax*10; */
3862: /* } */
3863: /* else if((k1 >khi/nkhi) || (k2 >khi/nkhi)){ */
3864: /* delts=delt; */
3865: /* } */
3866: /* } /\* End loop k *\/ */
3867: /* } */
3868: /* delti[theta]=delts; */
3869: /* return res; */
3870: /* } */
3871:
3872:
1.126 brouard 3873: /************** Inverse of matrix **************/
3874: void ludcmp(double **a, int n, int *indx, double *d)
3875: {
3876: int i,imax,j,k;
3877: double big,dum,sum,temp;
3878: double *vv;
3879:
3880: vv=vector(1,n);
3881: *d=1.0;
3882: for (i=1;i<=n;i++) {
3883: big=0.0;
3884: for (j=1;j<=n;j++)
3885: if ((temp=fabs(a[i][j])) > big) big=temp;
3886: if (big == 0.0) nrerror("Singular matrix in routine ludcmp");
3887: vv[i]=1.0/big;
3888: }
3889: for (j=1;j<=n;j++) {
3890: for (i=1;i<j;i++) {
3891: sum=a[i][j];
3892: for (k=1;k<i;k++) sum -= a[i][k]*a[k][j];
3893: a[i][j]=sum;
3894: }
3895: big=0.0;
3896: for (i=j;i<=n;i++) {
3897: sum=a[i][j];
3898: for (k=1;k<j;k++)
3899: sum -= a[i][k]*a[k][j];
3900: a[i][j]=sum;
3901: if ( (dum=vv[i]*fabs(sum)) >= big) {
3902: big=dum;
3903: imax=i;
3904: }
3905: }
3906: if (j != imax) {
3907: for (k=1;k<=n;k++) {
3908: dum=a[imax][k];
3909: a[imax][k]=a[j][k];
3910: a[j][k]=dum;
3911: }
3912: *d = -(*d);
3913: vv[imax]=vv[j];
3914: }
3915: indx[j]=imax;
3916: if (a[j][j] == 0.0) a[j][j]=TINY;
3917: if (j != n) {
3918: dum=1.0/(a[j][j]);
3919: for (i=j+1;i<=n;i++) a[i][j] *= dum;
3920: }
3921: }
3922: free_vector(vv,1,n); /* Doesn't work */
3923: ;
3924: }
3925:
3926: void lubksb(double **a, int n, int *indx, double b[])
3927: {
3928: int i,ii=0,ip,j;
3929: double sum;
3930:
3931: for (i=1;i<=n;i++) {
3932: ip=indx[i];
3933: sum=b[ip];
3934: b[ip]=b[i];
3935: if (ii)
3936: for (j=ii;j<=i-1;j++) sum -= a[i][j]*b[j];
3937: else if (sum) ii=i;
3938: b[i]=sum;
3939: }
3940: for (i=n;i>=1;i--) {
3941: sum=b[i];
3942: for (j=i+1;j<=n;j++) sum -= a[i][j]*b[j];
3943: b[i]=sum/a[i][i];
3944: }
3945: }
3946:
3947: void pstamp(FILE *fichier)
3948: {
1.196 brouard 3949: fprintf(fichier,"# %s.%s\n#IMaCh version %s, %s\n#%s\n# %s", optionfilefiname,optionfilext,version,copyright, fullversion, strstart);
1.126 brouard 3950: }
3951:
3952: /************ Frequencies ********************/
1.226 brouard 3953: void freqsummary(char fileres[], int iagemin, int iagemax, int **s, double **agev, int nlstate, int imx, \
3954: int *Tvaraff, int *invalidvarcomb, int **nbcode, int *ncodemax,double **mint,double **anint, char strstart[], \
3955: int firstpass, int lastpass, int stepm, int weightopt, char model[])
3956: { /* Some frequencies */
3957:
1.227 brouard 3958: int i, m, jk, j1, bool, z1,j, k, iv;
1.226 brouard 3959: int iind=0, iage=0;
3960: int mi; /* Effective wave */
3961: int first;
3962: double ***freq; /* Frequencies */
3963: double *meanq;
3964: double **meanqt;
3965: double *pp, **prop, *posprop, *pospropt;
3966: double pos=0., posproptt=0., pospropta=0., k2, dateintsum=0,k2cpt=0;
3967: char fileresp[FILENAMELENGTH], fileresphtm[FILENAMELENGTH], fileresphtmfr[FILENAMELENGTH];
3968: double agebegin, ageend;
3969:
3970: pp=vector(1,nlstate);
3971: prop=matrix(1,nlstate,iagemin-AGEMARGE,iagemax+3+AGEMARGE);
3972: posprop=vector(1,nlstate); /* Counting the number of transition starting from a live state per age */
3973: pospropt=vector(1,nlstate); /* Counting the number of transition starting from a live state */
3974: /* prop=matrix(1,nlstate,iagemin,iagemax+3); */
3975: meanq=vector(1,nqfveff); /* Number of Quantitative Fixed Variables Effective */
3976: meanqt=matrix(1,lastpass,1,nqtveff);
3977: strcpy(fileresp,"P_");
3978: strcat(fileresp,fileresu);
3979: /*strcat(fileresphtm,fileresu);*/
3980: if((ficresp=fopen(fileresp,"w"))==NULL) {
3981: printf("Problem with prevalence resultfile: %s\n", fileresp);
3982: fprintf(ficlog,"Problem with prevalence resultfile: %s\n", fileresp);
3983: exit(0);
3984: }
1.214 brouard 3985:
1.226 brouard 3986: strcpy(fileresphtm,subdirfext(optionfilefiname,"PHTM_",".htm"));
3987: if((ficresphtm=fopen(fileresphtm,"w"))==NULL) {
3988: printf("Problem with prevalence HTM resultfile '%s' with errno='%s'\n",fileresphtm,strerror(errno));
3989: fprintf(ficlog,"Problem with prevalence HTM resultfile '%s' with errno='%s'\n",fileresphtm,strerror(errno));
3990: fflush(ficlog);
3991: exit(70);
3992: }
3993: else{
3994: fprintf(ficresphtm,"<html><head>\n<title>IMaCh PHTM_ %s</title></head>\n <body><font size=\"2\">%s <br> %s</font> \
1.214 brouard 3995: <hr size=\"2\" color=\"#EC5E5E\"> \n\
3996: Title=%s <br>Datafile=%s Firstpass=%d Lastpass=%d Stepm=%d Weight=%d Model=1+age+%s<br>\n",\
1.226 brouard 3997: fileresphtm,version,fullversion,title,datafile,firstpass,lastpass,stepm, weightopt, model);
3998: }
3999: fprintf(ficresphtm,"Current page is file <a href=\"%s\">%s</a><br>\n\n<h4>Frequencies and prevalence by age at begin of transition</h4>\n",fileresphtm, fileresphtm);
1.214 brouard 4000:
1.226 brouard 4001: strcpy(fileresphtmfr,subdirfext(optionfilefiname,"PHTMFR_",".htm"));
4002: if((ficresphtmfr=fopen(fileresphtmfr,"w"))==NULL) {
4003: printf("Problem with frequency table HTM resultfile '%s' with errno='%s'\n",fileresphtmfr,strerror(errno));
4004: fprintf(ficlog,"Problem with frequency table HTM resultfile '%s' with errno='%s'\n",fileresphtmfr,strerror(errno));
4005: fflush(ficlog);
4006: exit(70);
4007: }
4008: else{
4009: fprintf(ficresphtmfr,"<html><head>\n<title>IMaCh PHTM_Frequency table %s</title></head>\n <body><font size=\"2\">%s <br> %s</font> \
1.214 brouard 4010: <hr size=\"2\" color=\"#EC5E5E\"> \n\
4011: Title=%s <br>Datafile=%s Firstpass=%d Lastpass=%d Stepm=%d Weight=%d Model=1+age+%s<br>\n",\
1.226 brouard 4012: fileresphtmfr,version,fullversion,title,datafile,firstpass,lastpass,stepm, weightopt, model);
4013: }
4014: fprintf(ficresphtmfr,"Current page is file <a href=\"%s\">%s</a><br>\n\n<h4>Frequencies of all effective transitions by age at begin of transition </h4>Unknown status is -1<br/>\n",fileresphtmfr, fileresphtmfr);
1.214 brouard 4015:
1.226 brouard 4016: freq= ma3x(-5,nlstate+ndeath,-5,nlstate+ndeath,iagemin-AGEMARGE,iagemax+3+AGEMARGE);
4017: j1=0;
1.126 brouard 4018:
1.227 brouard 4019: /* j=ncoveff; /\* Only fixed dummy covariates *\/ */
4020: j=cptcoveff; /* Only dummy covariates of the model */
1.226 brouard 4021: if (cptcovn<1) {j=1;ncodemax[1]=1;}
1.220 brouard 4022:
1.226 brouard 4023: first=1;
1.220 brouard 4024:
1.226 brouard 4025: /* Detects if a combination j1 is empty: for a multinomial variable like 3 education levels:
4026: reference=low_education V1=0,V2=0
4027: med_educ V1=1 V2=0,
4028: high_educ V1=0 V2=1
4029: Then V1=1 and V2=1 is a noisy combination that we want to exclude for the list 2**cptcoveff
4030: */
1.126 brouard 4031:
1.227 brouard 4032: for (j1 = 1; j1 <= (int) pow(2,j); j1++){ /* Loop on covariates combination in order of model, excluding quantitatives V4=0, V3=0 for example, fixed or varying covariates */
1.226 brouard 4033: posproptt=0.;
4034: /*printf("cptcoveff=%d Tvaraff=%d", cptcoveff,Tvaraff[1]);
4035: scanf("%d", i);*/
4036: for (i=-5; i<=nlstate+ndeath; i++)
4037: for (jk=-5; jk<=nlstate+ndeath; jk++)
1.231 ! brouard 4038: for(m=iagemin; m <= iagemax+3; m++)
! 4039: freq[i][jk][m]=0;
! 4040:
1.226 brouard 4041: for (i=1; i<=nlstate; i++) {
4042: for(m=iagemin; m <= iagemax+3; m++)
1.231 ! brouard 4043: prop[i][m]=0;
1.226 brouard 4044: posprop[i]=0;
4045: pospropt[i]=0;
4046: }
1.227 brouard 4047: /* for (z1=1; z1<= nqfveff; z1++) { */
4048: /* meanq[z1]+=0.; */
4049: /* for(m=1;m<=lastpass;m++){ */
4050: /* meanqt[m][z1]=0.; */
4051: /* } */
4052: /* } */
1.231 ! brouard 4053:
1.226 brouard 4054: dateintsum=0;
4055: k2cpt=0;
1.227 brouard 4056: /* For that combination of covariate j1, we count and print the frequencies in one pass */
1.226 brouard 4057: for (iind=1; iind<=imx; iind++) { /* For each individual iind */
4058: bool=1;
1.227 brouard 4059: if(anyvaryingduminmodel==0){ /* If All fixed covariates */
1.231 ! brouard 4060: if (cptcoveff >0) { /* Filter is here: Must be looked at for model=V1+V2+V3+V4 */
1.227 brouard 4061: /* for (z1=1; z1<= nqfveff; z1++) { */
4062: /* meanq[z1]+=coqvar[Tvar[z1]][iind]; /\* Computes mean of quantitative with selected filter *\/ */
4063: /* } */
1.231 ! brouard 4064: for (z1=1; z1<=cptcoveff; z1++) {
! 4065: /* if(Tvaraff[z1] ==-20){ */
! 4066: /* /\* sumnew+=cotvar[mw[mi][iind]][z1][iind]; *\/ */
! 4067: /* }else if(Tvaraff[z1] ==-10){ */
! 4068: /* /\* sumnew+=coqvar[z1][iind]; *\/ */
! 4069: /* }else */
! 4070: if (covar[Tvaraff[z1]][iind]!= nbcode[Tvaraff[z1]][codtabm(j1,z1)]){
! 4071: /* Tests if this individual iind responded to j1 (V4=1 V3=0) */
! 4072: bool=0;
! 4073: /* 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",
! 4074: bool,i,z1, z1, Tvaraff[z1],i,covar[Tvaraff[z1]][i],j1,z1,codtabm(j1,z1),
! 4075: j1,z1,nbcode[Tvaraff[z1]][codtabm(j1,z1)],j1);*/
! 4076: /* For j1=7 in V1+V2+V3+V4 = 0 1 1 0 and codtabm(7,3)=1 and nbcde[3][?]=1*/
! 4077: } /* Onlyf fixed */
! 4078: } /* end z1 */
! 4079: } /* cptcovn > 0 */
1.227 brouard 4080: } /* end any */
4081: if (bool==1){ /* We selected an individual iind satisfying combination j1 or all fixed */
1.231 ! brouard 4082: /* for(m=firstpass; m<=lastpass; m++){ */
! 4083: for(mi=1; mi<wav[iind];mi++){ /* For that wave */
! 4084: m=mw[mi][iind];
! 4085: if(anyvaryingduminmodel==1){ /* Some are varying covariates */
! 4086: for (z1=1; z1<=cptcoveff; z1++) {
! 4087: if( Fixed[Tmodelind[z1]]==1){
! 4088: iv= Tvar[Tmodelind[z1]]-ncovcol-nqv;
! 4089: if (cotvar[m][iv][iind]!= nbcode[Tvaraff[z1]][codtabm(j1,z1)]) /* iv=1 to ntv, right modality */
! 4090: bool=0;
! 4091: }else if( Fixed[Tmodelind[z1]]== 0) { /* fixed */
! 4092: if (covar[Tvaraff[z1]][iind]!= nbcode[Tvaraff[z1]][codtabm(j1,z1)]) {
! 4093: bool=0;
! 4094: }
! 4095: }
! 4096: }
! 4097: }/* Some are varying covariates, we tried to speed up if all fixed covariates in the model, avoiding waves loop */
! 4098: /* bool =0 we keep that guy which corresponds to the combination of dummy values */
! 4099: if(bool==1){
! 4100: /* dh[m][iind] or dh[mw[mi][iind]][iind] is the delay between two effective (mi) waves m=mw[mi][iind]
! 4101: and mw[mi+1][iind]. dh depends on stepm. */
! 4102: agebegin=agev[m][iind]; /* Age at beginning of wave before transition*/
! 4103: ageend=agev[m][iind]+(dh[m][iind])*stepm/YEARM; /* Age at end of wave and transition */
! 4104: if(m >=firstpass && m <=lastpass){
! 4105: k2=anint[m][iind]+(mint[m][iind]/12.);
! 4106: /*if ((k2>=dateprev1) && (k2<=dateprev2)) {*/
! 4107: if(agev[m][iind]==0) agev[m][iind]=iagemax+1; /* All ages equal to 0 are in iagemax+1 */
! 4108: if(agev[m][iind]==1) agev[m][iind]=iagemax+2; /* All ages equal to 1 are in iagemax+2 */
! 4109: if (s[m][iind]>0 && s[m][iind]<=nlstate) /* If status at wave m is known and a live state */
! 4110: prop[s[m][iind]][(int)agev[m][iind]] += weight[iind]; /* At age of beginning of transition, where status is known */
! 4111: if (m<lastpass) {
! 4112: /* if(s[m][iind]==4 && s[m+1][iind]==4) */
! 4113: /* 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]); */
! 4114: if(s[m][iind]==-1)
! 4115: 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.));
! 4116: freq[s[m][iind]][s[m+1][iind]][(int)agev[m][iind]] += weight[iind]; /* At age of beginning of transition, where status is known */
! 4117: /* freq[s[m][iind]][s[m+1][iind]][(int)((agebegin+ageend)/2.)] += weight[iind]; */
! 4118: 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 */
! 4119: }
! 4120: } /* end if between passes */
! 4121: if ((agev[m][iind]>1) && (agev[m][iind]< (iagemax+3)) && (anint[m][iind]!=9999) && (mint[m][iind]!=99)) {
! 4122: dateintsum=dateintsum+k2;
! 4123: k2cpt++;
! 4124: /* printf("iind=%ld dateintmean = %lf dateintsum=%lf k2cpt=%lf k2=%lf\n",iind, dateintsum/k2cpt, dateintsum,k2cpt, k2); */
! 4125: }
! 4126: } /* end bool 2 */
! 4127: } /* end m */
1.226 brouard 4128: } /* end bool */
4129: } /* end iind = 1 to imx */
4130: /* prop[s][age] is feeded for any initial and valid live state as well as
4131: freq[s1][s2][age] at single age of beginning the transition, for a combination j1 */
1.231 ! brouard 4132:
! 4133:
1.226 brouard 4134: /* fprintf(ficresp, "#Count between %.lf/%.lf/%.lf and %.lf/%.lf/%.lf\n",jprev1, mprev1,anprev1,jprev2, mprev2,anprev2);*/
4135: pstamp(ficresp);
1.227 brouard 4136: /* if (ncoveff>0) { */
4137: if (cptcoveff>0) {
1.226 brouard 4138: fprintf(ficresp, "\n#********** Variable ");
4139: fprintf(ficresphtm, "\n<br/><br/><h3>********** Variable ");
4140: fprintf(ficresphtmfr, "\n<br/><br/><h3>********** Variable ");
1.227 brouard 4141: for (z1=1; z1<=cptcoveff; z1++){
1.231 ! brouard 4142: fprintf(ficresp, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
! 4143: fprintf(ficresphtm, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
! 4144: fprintf(ficresphtmfr, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
1.226 brouard 4145: }
4146: fprintf(ficresp, "**********\n#");
4147: fprintf(ficresphtm, "**********</h3>\n");
4148: fprintf(ficresphtmfr, "**********</h3>\n");
4149: fprintf(ficlog, "\n#********** Variable ");
1.227 brouard 4150: for (z1=1; z1<=cptcoveff; z1++) fprintf(ficlog, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
1.226 brouard 4151: fprintf(ficlog, "**********\n");
4152: }
4153: fprintf(ficresphtm,"<table style=\"text-align:center; border: 1px solid\">");
4154: for(i=1; i<=nlstate;i++) {
4155: fprintf(ficresp, " Age Prev(%d) N(%d) N",i,i);
4156: fprintf(ficresphtm, "<th>Age</th><th>Prev(%d)</th><th>N(%d)</th><th>N</th>",i,i);
4157: }
4158: fprintf(ficresp, "\n");
4159: fprintf(ficresphtm, "\n");
1.231 ! brouard 4160:
1.226 brouard 4161: /* Header of frequency table by age */
4162: fprintf(ficresphtmfr,"<table style=\"text-align:center; border: 1px solid\">");
4163: fprintf(ficresphtmfr,"<th>Age</th> ");
4164: for(jk=-1; jk <=nlstate+ndeath; jk++){
4165: for(m=-1; m <=nlstate+ndeath; m++){
1.231 ! brouard 4166: if(jk!=0 && m!=0)
! 4167: fprintf(ficresphtmfr,"<th>%d%d</th> ",jk,m);
1.226 brouard 4168: }
4169: }
4170: fprintf(ficresphtmfr, "\n");
1.231 ! brouard 4171:
1.226 brouard 4172: /* For each age */
4173: for(iage=iagemin; iage <= iagemax+3; iage++){
4174: fprintf(ficresphtm,"<tr>");
4175: if(iage==iagemax+1){
1.231 ! brouard 4176: fprintf(ficlog,"1");
! 4177: fprintf(ficresphtmfr,"<tr><th>0</th> ");
1.226 brouard 4178: }else if(iage==iagemax+2){
1.231 ! brouard 4179: fprintf(ficlog,"0");
! 4180: fprintf(ficresphtmfr,"<tr><th>Unknown</th> ");
1.226 brouard 4181: }else if(iage==iagemax+3){
1.231 ! brouard 4182: fprintf(ficlog,"Total");
! 4183: fprintf(ficresphtmfr,"<tr><th>Total</th> ");
1.226 brouard 4184: }else{
1.231 ! brouard 4185: if(first==1){
! 4186: first=0;
! 4187: printf("See log file for details...\n");
! 4188: }
! 4189: fprintf(ficresphtmfr,"<tr><th>%d</th> ",iage);
! 4190: fprintf(ficlog,"Age %d", iage);
1.226 brouard 4191: }
4192: for(jk=1; jk <=nlstate ; jk++){
1.231 ! brouard 4193: for(m=-1, pp[jk]=0; m <=nlstate+ndeath ; m++)
! 4194: pp[jk] += freq[jk][m][iage];
1.226 brouard 4195: }
4196: for(jk=1; jk <=nlstate ; jk++){
1.231 ! brouard 4197: for(m=-1, pos=0; m <=0 ; m++)
! 4198: pos += freq[jk][m][iage];
! 4199: if(pp[jk]>=1.e-10){
! 4200: if(first==1){
! 4201: printf(" %d.=%.0f loss[%d]=%.1f%%",jk,pp[jk],jk,100*pos/pp[jk]);
! 4202: }
! 4203: fprintf(ficlog," %d.=%.0f loss[%d]=%.1f%%",jk,pp[jk],jk,100*pos/pp[jk]);
! 4204: }else{
! 4205: if(first==1)
! 4206: printf(" %d.=%.0f loss[%d]=NaNQ%%",jk,pp[jk],jk);
! 4207: fprintf(ficlog," %d.=%.0f loss[%d]=NaNQ%%",jk,pp[jk],jk);
! 4208: }
1.226 brouard 4209: }
1.231 ! brouard 4210:
1.226 brouard 4211: for(jk=1; jk <=nlstate ; jk++){
1.231 ! brouard 4212: /* posprop[jk]=0; */
! 4213: for(m=0, pp[jk]=0; m <=nlstate+ndeath; m++)/* Summing on all ages */
! 4214: pp[jk] += freq[jk][m][iage];
1.226 brouard 4215: } /* pp[jk] is the total number of transitions starting from state jk and any ending status until this age */
1.231 ! brouard 4216:
1.226 brouard 4217: for(jk=1,pos=0, pospropta=0.; jk <=nlstate ; jk++){
1.231 ! brouard 4218: pos += pp[jk]; /* pos is the total number of transitions until this age */
! 4219: posprop[jk] += prop[jk][iage]; /* prop is the number of transitions from a live state
! 4220: from jk at age iage prop[s[m][iind]][(int)agev[m][iind]] += weight[iind] */
! 4221: pospropta += prop[jk][iage]; /* prop is the number of transitions from a live state
! 4222: from jk at age iage prop[s[m][iind]][(int)agev[m][iind]] += weight[iind] */
1.226 brouard 4223: }
4224: for(jk=1; jk <=nlstate ; jk++){
1.231 ! brouard 4225: if(pos>=1.e-5){
! 4226: if(first==1)
! 4227: printf(" %d.=%.0f prev[%d]=%.1f%%",jk,pp[jk],jk,100*pp[jk]/pos);
! 4228: fprintf(ficlog," %d.=%.0f prev[%d]=%.1f%%",jk,pp[jk],jk,100*pp[jk]/pos);
! 4229: }else{
! 4230: if(first==1)
! 4231: printf(" %d.=%.0f prev[%d]=NaNQ%%",jk,pp[jk],jk);
! 4232: fprintf(ficlog," %d.=%.0f prev[%d]=NaNQ%%",jk,pp[jk],jk);
! 4233: }
! 4234: if( iage <= iagemax){
! 4235: if(pos>=1.e-5){
! 4236: fprintf(ficresp," %d %.5f %.0f %.0f",iage,prop[jk][iage]/pospropta, prop[jk][iage],pospropta);
! 4237: fprintf(ficresphtm,"<th>%d</th><td>%.5f</td><td>%.0f</td><td>%.0f</td>",iage,prop[jk][iage]/pospropta, prop[jk][iage],pospropta);
! 4238: /*probs[iage][jk][j1]= pp[jk]/pos;*/
! 4239: /*printf("\niage=%d jk=%d j1=%d %.5f %.0f %.0f %f",iage,jk,j1,pp[jk]/pos, pp[jk],pos,probs[iage][jk][j1]);*/
! 4240: }
! 4241: else{
! 4242: fprintf(ficresp," %d NaNq %.0f %.0f",iage,prop[jk][iage],pospropta);
! 4243: fprintf(ficresphtm,"<th>%d</th><td>NaNq</td><td>%.0f</td><td>%.0f</td>",iage, prop[jk][iage],pospropta);
! 4244: }
! 4245: }
! 4246: pospropt[jk] +=posprop[jk];
1.226 brouard 4247: } /* end loop jk */
4248: /* pospropt=0.; */
4249: for(jk=-1; jk <=nlstate+ndeath; jk++){
1.231 ! brouard 4250: for(m=-1; m <=nlstate+ndeath; m++){
! 4251: if(freq[jk][m][iage] !=0 ) { /* minimizing output */
! 4252: if(first==1){
! 4253: printf(" %d%d=%.0f",jk,m,freq[jk][m][iage]);
! 4254: }
! 4255: fprintf(ficlog," %d%d=%.0f",jk,m,freq[jk][m][iage]);
! 4256: }
! 4257: if(jk!=0 && m!=0)
! 4258: fprintf(ficresphtmfr,"<td>%.0f</td> ",freq[jk][m][iage]);
! 4259: }
1.226 brouard 4260: } /* end loop jk */
4261: posproptt=0.;
4262: for(jk=1; jk <=nlstate; jk++){
1.231 ! brouard 4263: posproptt += pospropt[jk];
1.226 brouard 4264: }
4265: fprintf(ficresphtmfr,"</tr>\n ");
4266: if(iage <= iagemax){
1.231 ! brouard 4267: fprintf(ficresp,"\n");
! 4268: fprintf(ficresphtm,"</tr>\n");
1.226 brouard 4269: }
4270: if(first==1)
1.231 ! brouard 4271: printf("Others in log...\n");
1.226 brouard 4272: fprintf(ficlog,"\n");
4273: } /* end loop age iage */
4274: fprintf(ficresphtm,"<tr><th>Tot</th>");
4275: for(jk=1; jk <=nlstate ; jk++){
4276: if(posproptt < 1.e-5){
1.231 ! brouard 4277: fprintf(ficresphtm,"<td>Nanq</td><td>%.0f</td><td>%.0f</td>",pospropt[jk],posproptt);
1.226 brouard 4278: }else{
1.231 ! brouard 4279: fprintf(ficresphtm,"<td>%.5f</td><td>%.0f</td><td>%.0f</td>",pospropt[jk]/posproptt,pospropt[jk],posproptt);
1.226 brouard 4280: }
4281: }
4282: fprintf(ficresphtm,"</tr>\n");
4283: fprintf(ficresphtm,"</table>\n");
4284: fprintf(ficresphtmfr,"</table>\n");
4285: if(posproptt < 1.e-5){
4286: fprintf(ficresphtm,"\n <p><b> This combination (%d) is not valid and no result will be produced</b></p>",j1);
4287: fprintf(ficresphtmfr,"\n <p><b> This combination (%d) is not valid and no result will be produced</b></p>",j1);
4288: fprintf(ficres,"\n This combination (%d) is not valid and no result will be produced\n\n",j1);
4289: invalidvarcomb[j1]=1;
4290: }else{
4291: fprintf(ficresphtm,"\n <p> This combination (%d) is valid and result will be produced.</p>",j1);
4292: invalidvarcomb[j1]=0;
4293: }
4294: fprintf(ficresphtmfr,"</table>\n");
4295: } /* end selected combination of covariate j1 */
4296: dateintmean=dateintsum/k2cpt;
1.231 ! brouard 4297:
1.226 brouard 4298: fclose(ficresp);
4299: fclose(ficresphtm);
4300: fclose(ficresphtmfr);
4301: free_vector(meanq,1,nqfveff);
4302: free_matrix(meanqt,1,lastpass,1,nqtveff);
4303: free_ma3x(freq,-5,nlstate+ndeath,-5,nlstate+ndeath, iagemin-AGEMARGE, iagemax+3+AGEMARGE);
4304: free_vector(pospropt,1,nlstate);
4305: free_vector(posprop,1,nlstate);
4306: free_matrix(prop,1,nlstate,iagemin-AGEMARGE, iagemax+3+AGEMARGE);
4307: free_vector(pp,1,nlstate);
4308: /* End of freqsummary */
4309: }
1.126 brouard 4310:
4311: /************ Prevalence ********************/
1.227 brouard 4312: 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)
4313: {
4314: /* Compute observed prevalence between dateprev1 and dateprev2 by counting the number of people
4315: in each health status at the date of interview (if between dateprev1 and dateprev2).
4316: We still use firstpass and lastpass as another selection.
4317: */
1.126 brouard 4318:
1.227 brouard 4319: int i, m, jk, j1, bool, z1,j, iv;
4320: int mi; /* Effective wave */
4321: int iage;
4322: double agebegin, ageend;
4323:
4324: double **prop;
4325: double posprop;
4326: double y2; /* in fractional years */
4327: int iagemin, iagemax;
4328: int first; /** to stop verbosity which is redirected to log file */
4329:
4330: iagemin= (int) agemin;
4331: iagemax= (int) agemax;
4332: /*pp=vector(1,nlstate);*/
4333: prop=matrix(1,nlstate,iagemin-AGEMARGE,iagemax+3+AGEMARGE);
4334: /* freq=ma3x(-1,nlstate+ndeath,-1,nlstate+ndeath,iagemin,iagemax+3);*/
4335: j1=0;
1.222 brouard 4336:
1.227 brouard 4337: /*j=cptcoveff;*/
4338: if (cptcovn<1) {j=1;ncodemax[1]=1;}
1.222 brouard 4339:
1.227 brouard 4340: first=1;
4341: for(j1=1; j1<= (int) pow(2,cptcoveff);j1++){ /* For each combination of covariate */
4342: for (i=1; i<=nlstate; i++)
4343: for(iage=iagemin-AGEMARGE; iage <= iagemax+3+AGEMARGE; iage++)
4344: prop[i][iage]=0.0;
4345: printf("Prevalence combination of varying and fixed dummies %d\n",j1);
4346: /* fprintf(ficlog," V%d=%d ",Tvaraff[j1],nbcode[Tvaraff[j1]][codtabm(k,j1)]); */
4347: fprintf(ficlog,"Prevalence combination of varying and fixed dummies %d\n",j1);
4348:
4349: for (i=1; i<=imx; i++) { /* Each individual */
4350: bool=1;
4351: /* for(m=firstpass; m<=lastpass; m++){/\* Other selection (we can limit to certain interviews*\/ */
4352: for(mi=1; mi<wav[i];mi++){ /* For this wave too look where individual can be counted V4=0 V3=0 */
4353: m=mw[mi][i];
4354: /* Tmodelind[z1]=k is the position of the varying covariate in the model, but which # within 1 to ntv? */
4355: /* Tvar[Tmodelind[z1]] is the n of Vn; n-ncovcol-nqv is the first time varying covariate or iv */
4356: for (z1=1; z1<=cptcoveff; z1++){
4357: if( Fixed[Tmodelind[z1]]==1){
4358: iv= Tvar[Tmodelind[z1]]-ncovcol-nqv;
4359: if (cotvar[m][iv][i]!= nbcode[Tvaraff[z1]][codtabm(j1,z1)]) /* iv=1 to ntv, right modality */
4360: bool=0;
4361: }else if( Fixed[Tmodelind[z1]]== 0) /* fixed */
4362: if (covar[Tvaraff[z1]][i]!= nbcode[Tvaraff[z1]][codtabm(j1,z1)]) {
4363: bool=0;
4364: }
4365: }
4366: if(bool==1){ /* Otherwise we skip that wave/person */
4367: agebegin=agev[m][i]; /* Age at beginning of wave before transition*/
4368: /* ageend=agev[m][i]+(dh[m][i])*stepm/YEARM; /\* Age at end of wave and transition *\/ */
4369: if(m >=firstpass && m <=lastpass){
4370: y2=anint[m][i]+(mint[m][i]/12.); /* Fractional date in year */
4371: if ((y2>=dateprev1) && (y2<=dateprev2)) { /* Here is the main selection (fractional years) */
4372: if(agev[m][i]==0) agev[m][i]=iagemax+1;
4373: if(agev[m][i]==1) agev[m][i]=iagemax+2;
4374: if((int)agev[m][i] <iagemin-AGEMARGE || (int)agev[m][i] >iagemax+3+AGEMARGE){
4375: 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);
4376: exit(1);
4377: }
4378: if (s[m][i]>0 && s[m][i]<=nlstate) {
4379: /*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]]);*/
4380: prop[s[m][i]][(int)agev[m][i]] += weight[i];/* At age of beginning of transition, where status is known */
4381: prop[s[m][i]][iagemax+3] += weight[i];
4382: } /* end valid statuses */
4383: } /* end selection of dates */
4384: } /* end selection of waves */
4385: } /* end bool */
4386: } /* end wave */
4387: } /* end individual */
4388: for(i=iagemin; i <= iagemax+3; i++){
4389: for(jk=1,posprop=0; jk <=nlstate ; jk++) {
4390: posprop += prop[jk][i];
4391: }
4392:
4393: for(jk=1; jk <=nlstate ; jk++){
4394: if( i <= iagemax){
4395: if(posprop>=1.e-5){
4396: probs[i][jk][j1]= prop[jk][i]/posprop;
4397: } else{
4398: if(first==1){
4399: first=0;
4400: printf("Warning Observed prevalence probs[%d][%d][%d]=%lf because of lack of cases\nSee others in log file...\n",jk,i,j1,probs[i][jk][j1]);
4401: }
4402: }
4403: }
4404: }/* end jk */
4405: }/* end i */
1.222 brouard 4406: /*} *//* end i1 */
1.227 brouard 4407: } /* end j1 */
1.222 brouard 4408:
1.227 brouard 4409: /* free_ma3x(freq,-1,nlstate+ndeath,-1,nlstate+ndeath, iagemin, iagemax+3);*/
4410: /*free_vector(pp,1,nlstate);*/
4411: free_matrix(prop,1,nlstate, iagemin-AGEMARGE,iagemax+3+AGEMARGE);
4412: } /* End of prevalence */
1.126 brouard 4413:
4414: /************* Waves Concatenation ***************/
4415:
4416: 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)
4417: {
4418: /* Concatenates waves: wav[i] is the number of effective (useful waves) of individual i.
4419: Death is a valid wave (if date is known).
4420: mw[mi][i] is the mi (mi=1 to wav[i]) effective wave of individual i
4421: dh[m][i] or dh[mw[mi][i]][i] is the delay between two effective waves m=mw[mi][i]
4422: and mw[mi+1][i]. dh depends on stepm.
1.227 brouard 4423: */
1.126 brouard 4424:
1.224 brouard 4425: int i=0, mi=0, m=0, mli=0;
1.126 brouard 4426: /* int j, k=0,jk, ju, jl,jmin=1e+5, jmax=-1;
4427: double sum=0., jmean=0.;*/
1.224 brouard 4428: int first=0, firstwo=0, firsthree=0, firstfour=0, firstfiv=0;
1.126 brouard 4429: int j, k=0,jk, ju, jl;
4430: double sum=0.;
4431: first=0;
1.214 brouard 4432: firstwo=0;
1.217 brouard 4433: firsthree=0;
1.218 brouard 4434: firstfour=0;
1.164 brouard 4435: jmin=100000;
1.126 brouard 4436: jmax=-1;
4437: jmean=0.;
1.224 brouard 4438:
4439: /* Treating live states */
1.214 brouard 4440: for(i=1; i<=imx; i++){ /* For simple cases and if state is death */
1.224 brouard 4441: mi=0; /* First valid wave */
1.227 brouard 4442: mli=0; /* Last valid wave */
1.126 brouard 4443: m=firstpass;
1.214 brouard 4444: while(s[m][i] <= nlstate){ /* a live state */
1.227 brouard 4445: 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 */
4446: mli=m-1;/* mw[++mi][i]=m-1; */
4447: }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 */
4448: mw[++mi][i]=m;
4449: mli=m;
1.224 brouard 4450: } /* else might be a useless wave -1 and mi is not incremented and mw[mi] not updated */
4451: if(m < lastpass){ /* m < lastpass, standard case */
1.227 brouard 4452: m++; /* mi gives the "effective" current wave, m the current wave, go to next wave by incrementing m */
1.216 brouard 4453: }
1.227 brouard 4454: else{ /* m >= lastpass, eventual special issue with warning */
1.224 brouard 4455: #ifdef UNKNOWNSTATUSNOTCONTRIBUTING
1.227 brouard 4456: break;
1.224 brouard 4457: #else
1.227 brouard 4458: if(s[m][i]==-1 && (int) andc[i] == 9999 && (int)anint[m][i] != 9999){
4459: if(firsthree == 0){
4460: printf("Information! Unknown status for individual %ld line=%d occurred at last wave %d at known date %d/%d. Please, check if your unknown date of death %d/%d means a live state %d at wave %d. This case(%d)/wave(%d) contributes to the likelihood as pi. .\nOthers in log file only\n",num[i],i,lastpass,(int)mint[m][i],(int)anint[m][i], (int) moisdc[i], (int) andc[i], s[m][i], m, i, m);
4461: firsthree=1;
4462: }
4463: fprintf(ficlog,"Information! Unknown status for individual %ld line=%d occurred at last wave %d at known date %d/%d. Please, check if your unknown date of death %d/%d means a live state %d at wave %d. This case(%d)/wave(%d) contributes to the likelihood as pi. .\n",num[i],i,lastpass,(int)mint[m][i],(int)anint[m][i], (int) moisdc[i], (int) andc[i], s[m][i], m, i, m);
4464: mw[++mi][i]=m;
4465: mli=m;
4466: }
4467: if(s[m][i]==-2){ /* Vital status is really unknown */
4468: nbwarn++;
4469: if((int)anint[m][i] == 9999){ /* Has the vital status really been verified? */
4470: 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);
4471: 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);
4472: }
4473: break;
4474: }
4475: break;
1.224 brouard 4476: #endif
1.227 brouard 4477: }/* End m >= lastpass */
1.126 brouard 4478: }/* end while */
1.224 brouard 4479:
1.227 brouard 4480: /* mi is the last effective wave, m is lastpass, mw[j][i] gives the # of j-th effective wave for individual i */
1.216 brouard 4481: /* After last pass */
1.224 brouard 4482: /* Treating death states */
1.214 brouard 4483: if (s[m][i] > nlstate){ /* In a death state */
1.227 brouard 4484: /* if( mint[m][i]==mdc[m][i] && anint[m][i]==andc[m][i]){ /\* same date of death and date of interview *\/ */
4485: /* } */
1.126 brouard 4486: mi++; /* Death is another wave */
4487: /* if(mi==0) never been interviewed correctly before death */
1.227 brouard 4488: /* Only death is a correct wave */
1.126 brouard 4489: mw[mi][i]=m;
1.224 brouard 4490: }
4491: #ifndef DISPATCHINGKNOWNDEATHAFTERLASTWAVE
1.227 brouard 4492: else if ((int) andc[i] != 9999) { /* Status is negative. A death occured after lastpass, we can't take it into account because of potential bias */
1.216 brouard 4493: /* m++; */
4494: /* mi++; */
4495: /* s[m][i]=nlstate+1; /\* We are setting the status to the last of non live state *\/ */
4496: /* mw[mi][i]=m; */
1.218 brouard 4497: if ((int)anint[m][i]!= 9999) { /* date of last interview is known */
1.227 brouard 4498: if((andc[i]+moisdc[i]/12.) <=(anint[m][i]+mint[m][i]/12.)){ /* death occured before last wave and status should have been death instead of -1 */
4499: nbwarn++;
4500: if(firstfiv==0){
4501: printf("Warning! Death for individual %ld line=%d occurred at %d/%d before last wave %d interviewed at %d/%d and should have been coded as death instead of '%d'. This case (%d)/wave (%d) is contributing to likelihood.\nOthers in log file only\n",num[i],i,(int) moisdc[i], (int) andc[i], lastpass,(int)mint[m][i],(int)anint[m][i], s[m][i], i,m );
4502: firstfiv=1;
4503: }else{
4504: fprintf(ficlog,"Warning! Death for individual %ld line=%d occurred at %d/%d before last wave %d interviewed at %d/%d and should have been coded as death instead of '%d'. This case (%d)/wave (%d) is contributing to likelihood.\n",num[i],i,(int) moisdc[i], (int) andc[i], lastpass,(int)mint[m][i],(int)anint[m][i], s[m][i], i,m );
4505: }
4506: }else{ /* Death occured afer last wave potential bias */
4507: nberr++;
4508: if(firstwo==0){
4509: printf("Error! Death for individual %ld line=%d occurred at %d/%d after last wave %d interviewed at %d/%d. Potential bias if other individuals are still alive at this date but ignored. This case (%d)/wave (%d) is skipped, no contribution to likelihood.\nOthers in log file only\n",num[i],i,(int) moisdc[i], (int) andc[i], lastpass,(int)mint[m][i],(int)anint[m][i], i,m );
4510: firstwo=1;
4511: }
4512: fprintf(ficlog,"Error! Death for individual %ld line=%d occurred at %d/%d after last wave %d interviewed at %d/%d. Potential bias if other individuals are still alive at this date but ignored. This case (%d)/wave (%d) is skipped, no contribution to likelihood.\n",num[i],i,(int) moisdc[i], (int) andc[i], lastpass,(int)mint[m][i],(int)anint[m][i], i,m );
4513: }
1.218 brouard 4514: }else{ /* end date of interview is known */
1.227 brouard 4515: /* death is known but not confirmed by death status at any wave */
4516: if(firstfour==0){
4517: printf("Error! Death for individual %ld line=%d occurred %d/%d but not confirmed by any death status for any wave, including last wave %d at unknown date %d/%d. Potential bias if other individuals are still alive at this date but ignored. This case (%d)/wave (%d) is skipped, no contribution to likelihood.\nOthers in log file only\n",num[i],i,(int) moisdc[i], (int) andc[i], lastpass,(int)mint[m][i],(int)anint[m][i], i,m );
4518: firstfour=1;
4519: }
4520: fprintf(ficlog,"Error! Death for individual %ld line=%d occurred %d/%d but not confirmed by any death status for any wave, including last wave %d at unknown date %d/%d. Potential bias if other individuals are still alive at this date but ignored. This case (%d)/wave (%d) is skipped, no contribution to likelihood.\n",num[i],i,(int) moisdc[i], (int) andc[i], lastpass,(int)mint[m][i],(int)anint[m][i], i,m );
1.214 brouard 4521: }
1.224 brouard 4522: } /* end if date of death is known */
4523: #endif
4524: wav[i]=mi; /* mi should be the last effective wave (or mli) */
4525: /* wav[i]=mw[mi][i]; */
1.126 brouard 4526: if(mi==0){
4527: nbwarn++;
4528: if(first==0){
1.227 brouard 4529: printf("Warning! No valid information for individual %ld line=%d (skipped) and may be others, see log file\n",num[i],i);
4530: first=1;
1.126 brouard 4531: }
4532: if(first==1){
1.227 brouard 4533: fprintf(ficlog,"Warning! No valid information for individual %ld line=%d (skipped)\n",num[i],i);
1.126 brouard 4534: }
4535: } /* end mi==0 */
4536: } /* End individuals */
1.214 brouard 4537: /* wav and mw are no more changed */
1.223 brouard 4538:
1.214 brouard 4539:
1.126 brouard 4540: for(i=1; i<=imx; i++){
4541: for(mi=1; mi<wav[i];mi++){
4542: if (stepm <=0)
1.227 brouard 4543: dh[mi][i]=1;
1.126 brouard 4544: else{
1.227 brouard 4545: if (s[mw[mi+1][i]][i] > nlstate) { /* A death */
4546: if (agedc[i] < 2*AGESUP) {
4547: j= rint(agedc[i]*12-agev[mw[mi][i]][i]*12);
4548: if(j==0) j=1; /* Survives at least one month after exam */
4549: else if(j<0){
4550: nberr++;
4551: printf("Error! Negative delay (%d to death) between waves %d and %d of individual %ld at line %d who is aged %.1f with statuses from %d to %d\n ",j,mw[mi][i],mw[mi+1][i],num[i], i,agev[mw[mi][i]][i],s[mw[mi][i]][i] ,s[mw[mi+1][i]][i]);
4552: j=1; /* Temporary Dangerous patch */
4553: 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);
4554: fprintf(ficlog,"Error! Negative delay (%d to death) between waves %d and %d of individual %ld at line %d who is aged %.1f with statuses from %d to %d\n ",j,mw[mi][i],mw[mi+1][i],num[i], i,agev[mw[mi][i]][i],s[mw[mi][i]][i] ,s[mw[mi+1][i]][i]);
4555: 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);
4556: }
4557: k=k+1;
4558: if (j >= jmax){
4559: jmax=j;
4560: ijmax=i;
4561: }
4562: if (j <= jmin){
4563: jmin=j;
4564: ijmin=i;
4565: }
4566: sum=sum+j;
4567: /*if (j<0) printf("j=%d num=%d \n",j,i);*/
4568: /* printf("%d %d %d %d\n", s[mw[mi][i]][i] ,s[mw[mi+1][i]][i],j,i);*/
4569: }
4570: }
4571: else{
4572: j= rint( (agev[mw[mi+1][i]][i]*12 - agev[mw[mi][i]][i]*12));
1.126 brouard 4573: /* if (j<0) printf("%d %lf %lf %d %d %d\n", i,agev[mw[mi+1][i]][i], agev[mw[mi][i]][i],j,s[mw[mi][i]][i] ,s[mw[mi+1][i]][i]); */
1.223 brouard 4574:
1.227 brouard 4575: k=k+1;
4576: if (j >= jmax) {
4577: jmax=j;
4578: ijmax=i;
4579: }
4580: else if (j <= jmin){
4581: jmin=j;
4582: ijmin=i;
4583: }
4584: /* if (j<10) printf("j=%d jmin=%d num=%d ",j,jmin,i); */
4585: /*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]);*/
4586: if(j<0){
4587: nberr++;
4588: printf("Error! Negative delay (%d) between waves %d and %d of individual %ld at line %d who is aged %.1f with statuses from %d to %d\n ",j,mw[mi][i],mw[mi+1][i],num[i], i,agev[mw[mi][i]][i],s[mw[mi][i]][i] ,s[mw[mi+1][i]][i]);
4589: fprintf(ficlog,"Error! Negative delay (%d) between waves %d and %d of individual %ld at line %d who is aged %.1f with statuses from %d to %d\n ",j,mw[mi][i],mw[mi+1][i],num[i], i,agev[mw[mi][i]][i],s[mw[mi][i]][i] ,s[mw[mi+1][i]][i]);
4590: }
4591: sum=sum+j;
4592: }
4593: jk= j/stepm;
4594: jl= j -jk*stepm;
4595: ju= j -(jk+1)*stepm;
4596: if(mle <=1){ /* only if we use a the linear-interpoloation pseudo-likelihood */
4597: if(jl==0){
4598: dh[mi][i]=jk;
4599: bh[mi][i]=0;
4600: }else{ /* We want a negative bias in order to only have interpolation ie
4601: * to avoid the price of an extra matrix product in likelihood */
4602: dh[mi][i]=jk+1;
4603: bh[mi][i]=ju;
4604: }
4605: }else{
4606: if(jl <= -ju){
4607: dh[mi][i]=jk;
4608: bh[mi][i]=jl; /* bias is positive if real duration
4609: * is higher than the multiple of stepm and negative otherwise.
4610: */
4611: }
4612: else{
4613: dh[mi][i]=jk+1;
4614: bh[mi][i]=ju;
4615: }
4616: if(dh[mi][i]==0){
4617: dh[mi][i]=1; /* At least one step */
4618: bh[mi][i]=ju; /* At least one step */
4619: /* 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);*/
4620: }
4621: } /* end if mle */
1.126 brouard 4622: }
4623: } /* end wave */
4624: }
4625: jmean=sum/k;
4626: printf("Delay (in months) between two waves Min=%d (for indiviudal %ld) Max=%d (%ld) Mean=%f\n\n ",jmin, num[ijmin], jmax, num[ijmax], jmean);
1.141 brouard 4627: fprintf(ficlog,"Delay (in months) between two waves Min=%d (for indiviudal %d) Max=%d (%d) Mean=%f\n\n ",jmin, ijmin, jmax, ijmax, jmean);
1.227 brouard 4628: }
1.126 brouard 4629:
4630: /*********** Tricode ****************************/
1.220 brouard 4631: void tricode(int *cptcov, int *Tvar, int **nbcode, int imx, int *Ndum)
1.126 brouard 4632: {
1.144 brouard 4633: /**< Uses cptcovn+2*cptcovprod as the number of covariates */
4634: /* Tvar[i]=atoi(stre); find 'n' in Vn and stores in Tvar. If model=V2+V1 Tvar[1]=2 and Tvar[2]=1
1.169 brouard 4635: * Boring subroutine which should only output nbcode[Tvar[j]][k]
1.224 brouard 4636: * Tvar[5] in V2+V1+V3*age+V2*V4 is 4 (V4) even it is a time varying or quantitative variable
4637: * nbcode[Tvar[5]][1]= nbcode[4][1]=0, nbcode[4][2]=1 (usually);
1.144 brouard 4638: */
1.130 brouard 4639:
1.145 brouard 4640: int ij=1, k=0, j=0, i=0, maxncov=NCOVMAX;
1.136 brouard 4641: int modmaxcovj=0; /* Modality max of covariates j */
1.145 brouard 4642: int cptcode=0; /* Modality max of covariates j */
4643: int modmincovj=0; /* Modality min of covariates j */
4644:
4645:
1.220 brouard 4646: /* cptcoveff=0; */
1.224 brouard 4647: /* *cptcov=0; */
1.126 brouard 4648:
1.144 brouard 4649: for (k=1; k <= maxncov; k++) ncodemax[k]=0; /* Horrible constant again replaced by NCOVMAX */
1.126 brouard 4650:
1.224 brouard 4651: /* Loop on covariates without age and products and no quantitative variable */
4652: /* for (j=1; j<=(cptcovs); j++) { /\* From model V1 + V2*age+ V3 + V3*V4 keeps V1 + V3 = 2 only *\/ */
1.227 brouard 4653: for (k=1; k<=cptcovt; k++) { /* From model V1 + V2*age + V3 + V3*V4 keeps V1 + V3 = 2 only */
4654: for (j=-1; (j < maxncov); j++) Ndum[j]=0;
4655: if(Dummy[k]==0 && Typevar[k] !=1){ /* Dummy covariate and not age product */
4656: switch(Fixed[k]) {
4657: case 0: /* Testing on fixed dummy covariate, simple or product of fixed */
1.231 ! brouard 4658: 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*/
! 4659: ij=(int)(covar[Tvar[k]][i]);
! 4660: /* ij=0 or 1 or -1. Value of the covariate Tvar[j] for individual i
! 4661: * If product of Vn*Vm, still boolean *:
! 4662: * If it was coded 1, 2, 3, 4 should be splitted into 3 boolean variables
! 4663: * 1 => 0 0 0, 2 => 0 0 1, 3 => 0 1 1, 4=1 0 0 */
! 4664: /* Finds for covariate j, n=Tvar[j] of Vn . ij is the
! 4665: modality of the nth covariate of individual i. */
! 4666: if (ij > modmaxcovj)
! 4667: modmaxcovj=ij;
! 4668: else if (ij < modmincovj)
! 4669: modmincovj=ij;
! 4670: if ((ij < -1) && (ij > NCOVMAX)){
! 4671: printf( "Error: minimal is less than -1 or maximal is bigger than %d. Exiting. \n", NCOVMAX );
! 4672: exit(1);
! 4673: }else
! 4674: Ndum[ij]++; /*counts and stores the occurence of this modality 0, 1, -1*/
! 4675: /* If coded 1, 2, 3 , counts the number of 1 Ndum[1], number of 2, Ndum[2], etc */
! 4676: /*printf("i=%d ij=%d Ndum[ij]=%d imx=%d",i,ij,Ndum[ij],imx);*/
! 4677: /* getting the maximum value of the modality of the covariate
! 4678: (should be 0 or 1 now) Tvar[j]. If V=sex and male is coded 0 and
! 4679: female ies 1, then modmaxcovj=1.
! 4680: */
! 4681: } /* end for loop on individuals i */
! 4682: printf(" Minimal and maximal values of %d th covariate V%d: min=%d max=%d \n", k, Tvar[k], modmincovj, modmaxcovj);
! 4683: fprintf(ficlog," Minimal and maximal values of %d th covariate V%d: min=%d max=%d \n", k, Tvar[k], modmincovj, modmaxcovj);
! 4684: cptcode=modmaxcovj;
! 4685: /* Ndum[0] = frequency of 0 for model-covariate j, Ndum[1] frequency of 1 etc. */
! 4686: /*for (i=0; i<=cptcode; i++) {*/
! 4687: for (j=modmincovj; j<=modmaxcovj; j++) { /* j=-1 ? 0 and 1*//* For each value j of the modality of model-cov k */
! 4688: printf("Frequencies of covariates %d ie V%d with value %d: %d\n", k, Tvar[k], j, Ndum[j]);
! 4689: fprintf(ficlog, "Frequencies of covariates %d ie V%d with value %d: %d\n", k, Tvar[k], j, Ndum[j]);
! 4690: if( Ndum[j] != 0 ){ /* Counts if nobody answered modality j ie empty modality, we skip it and reorder */
! 4691: if( j != -1){
! 4692: ncodemax[k]++; /* ncodemax[k]= Number of modalities of the k th
! 4693: covariate for which somebody answered excluding
! 4694: undefined. Usually 2: 0 and 1. */
! 4695: }
! 4696: ncodemaxwundef[k]++; /* ncodemax[j]= Number of modalities of the k th
! 4697: covariate for which somebody answered including
! 4698: undefined. Usually 3: -1, 0 and 1. */
! 4699: } /* In fact ncodemax[k]=2 (dichotom. variables only) but it could be more for
! 4700: * historical reasons: 3 if coded 1, 2, 3 and 4 and Ndum[2]=0 */
! 4701: } /* Ndum[-1] number of undefined modalities */
! 4702:
! 4703: /* j is a covariate, n=Tvar[j] of Vn; Fills nbcode */
! 4704: /* For covariate j, modalities could be 1, 2, 3, 4, 5, 6, 7. */
! 4705: /* If Ndum[1]=0, Ndum[2]=0, Ndum[3]= 635, Ndum[4]=0, Ndum[5]=0, Ndum[6]=27, Ndum[7]=125; */
! 4706: /* modmincovj=3; modmaxcovj = 7; */
! 4707: /* There are only 3 modalities non empty 3, 6, 7 (or 2 if 27 is too few) : ncodemax[j]=3; */
! 4708: /* which will be coded 0, 1, 2 which in binary on 2=3-1 digits are 0=00 1=01, 2=10; */
! 4709: /* defining two dummy variables: variables V1_1 and V1_2.*/
! 4710: /* nbcode[Tvar[j]][ij]=k; */
! 4711: /* nbcode[Tvar[j]][1]=0; */
! 4712: /* nbcode[Tvar[j]][2]=1; */
! 4713: /* nbcode[Tvar[j]][3]=2; */
! 4714: /* To be continued (not working yet). */
! 4715: ij=0; /* ij is similar to i but can jump over null modalities */
! 4716: 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*/
! 4717: if (Ndum[i] == 0) { /* If nobody responded to this modality k */
! 4718: break;
! 4719: }
! 4720: ij++;
! 4721: 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*/
! 4722: cptcode = ij; /* New max modality for covar j */
! 4723: } /* end of loop on modality i=-1 to 1 or more */
! 4724: break;
1.227 brouard 4725: case 1: /* Testing on varying covariate, could be simple and
4726: * should look at waves or product of fixed *
4727: * varying. No time to test -1, assuming 0 and 1 only */
1.231 ! brouard 4728: ij=0;
! 4729: for(i=0; i<=1;i++){
! 4730: nbcode[Tvar[k]][++ij]=i;
! 4731: }
! 4732: break;
1.227 brouard 4733: default:
1.231 ! brouard 4734: break;
1.227 brouard 4735: } /* end switch */
4736: } /* end dummy test */
1.225 brouard 4737:
1.192 brouard 4738: /* for (k=0; k<= cptcode; k++) { /\* k=-1 ? k=0 to 1 *\//\* Could be 1 to 4 *\//\* cptcode=modmaxcovj *\/ */
4739: /* /\*recode from 0 *\/ */
4740: /* k is a modality. If we have model=V1+V1*sex */
4741: /* then: nbcode[1][1]=0 ; nbcode[1][2]=1; nbcode[2][1]=0 ; nbcode[2][2]=1; */
4742: /* But if some modality were not used, it is recoded from 0 to a newer modmaxcovj=cptcode *\/ */
4743: /* } */
4744: /* /\* cptcode = ij; *\/ /\* New max modality for covar j *\/ */
4745: /* if (ij > ncodemax[j]) { */
4746: /* printf( " Error ij=%d > ncodemax[%d]=%d\n", ij, j, ncodemax[j]); */
4747: /* fprintf(ficlog, " Error ij=%d > ncodemax[%d]=%d\n", ij, j, ncodemax[j]); */
4748: /* break; */
4749: /* } */
4750: /* } /\* end of loop on modality k *\/ */
1.137 brouard 4751: } /* end of loop on model-covariate j. nbcode[Tvarj][1]=0 and nbcode[Tvarj][2]=1 sets the value of covariate j*/
4752:
1.225 brouard 4753: for (k=-1; k< maxncov; k++) Ndum[k]=0;
1.227 brouard 4754: /* Look at fixed dummy (single or product) covariates to check empty modalities */
1.187 brouard 4755: for (i=1; i<=ncovmodel-2-nagesqr; i++) { /* -2, cste and age and eventually age*age */
1.225 brouard 4756: /* Listing of all covariables in statement model to see if some covariates appear twice. For example, V1 appears twice in V1+V1*V2.*/
1.227 brouard 4757: 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 */
4758: 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 */
4759: /* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1, {2, 1, 1, 1, 2, 1, 1, 0, 0} */
1.225 brouard 4760: } /* V4+V3+V5, Ndum[1]@5={0, 0, 1, 1, 1} */
4761:
4762: ij=0;
1.227 brouard 4763: /* for (i=0; i<= maxncov-1; i++) { /\* modmaxcovj is unknown here. Only Ndum[2(V2),3(age*V3), 5(V3*V2) 6(V1*V4) *\/ */
4764: for (k=1; k<= cptcovt; k++) { /* modmaxcovj is unknown here. Only Ndum[2(V2),3(age*V3), 5(V3*V2) 6(V1*V4) */
1.225 brouard 4765: /*printf("Ndum[%d]=%d\n",i, Ndum[i]);*/
1.227 brouard 4766: /* if((Ndum[i]!=0) && (i<=ncovcol)){ /\* Tvar[i] <= ncovmodel ? *\/ */
4767: if(Ndum[Tvar[k]]!=0 && Dummy[k] == 0 && Typevar[k]==0){ /* Only Dummy and non empty in the model */
4768: /* If product not in single variable we don't print results */
1.225 brouard 4769: /*printf("diff Ndum[%d]=%d\n",i, Ndum[i]);*/
1.230 brouard 4770: ++ij;/* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1, */
4771: 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*/
4772: Tmodelind[ij]=k; /* Tmodelind: index in model of dummies Tmodelind[1]=2 V4: pos=2; V3: pos=3, V1=9 {2, 3, 9, ?, ?,} */
1.231 ! brouard 4773: TmodelInvind[ij]=Tvar[k]- ncovcol-nqv; /* Inverse TmodelInvind[2=V4]=2 second dummy varying cov (V4)4-1-1 {0, 2, 1, } TmodelInvind[3]=1 */
1.227 brouard 4774: if(Fixed[k]!=0)
4775: anyvaryingduminmodel=1;
1.231 ! brouard 4776: /* }else if((Ndum[i]!=0) && (i<=ncovcol+nqv)){ */
! 4777: /* Tvaraff[++ij]=-10; /\* Dont'n know how to treat quantitative variables yet *\/ */
! 4778: /* }else if((Ndum[i]!=0) && (i<=ncovcol+nqv+ntv)){ */
! 4779: /* Tvaraff[++ij]=i; /\*For printing (unclear) *\/ */
! 4780: /* }else if((Ndum[i]!=0) && (i<=ncovcol+nqv+ntv+nqtv)){ */
! 4781: /* Tvaraff[++ij]=-20; /\* Dont'n know how to treat quantitative variables yet *\/ */
1.227 brouard 4782: }
1.225 brouard 4783: } /* Tvaraff[1]@5 {3, 4, -20, 0, 0} Very strange */
4784: /* ij--; */
4785: /* cptcoveff=ij; /\*Number of total covariates*\/ */
4786: *cptcov=ij; /*Number of total real effective covariates: effective
1.231 ! brouard 4787: * because they can be excluded from the model and real
! 4788: * if in the model but excluded because missing values, but how to get k from ij?*/
1.227 brouard 4789: for(j=ij+1; j<= cptcovt; j++){
4790: Tvaraff[j]=0;
4791: Tmodelind[j]=0;
4792: }
1.228 brouard 4793: for(j=ntveff+1; j<= cptcovt; j++){
4794: TmodelInvind[j]=0;
4795: }
1.227 brouard 4796: /* To be sorted */
4797: ;
1.126 brouard 4798: }
4799:
1.145 brouard 4800:
1.126 brouard 4801: /*********** Health Expectancies ****************/
4802:
1.127 brouard 4803: void evsij(double ***eij, double x[], int nlstate, int stepm, int bage, int fage, double **oldm, double **savm, int cij, int estepm,char strstart[] )
1.126 brouard 4804:
4805: {
4806: /* Health expectancies, no variances */
1.164 brouard 4807: int i, j, nhstepm, hstepm, h, nstepm;
1.126 brouard 4808: int nhstepma, nstepma; /* Decreasing with age */
4809: double age, agelim, hf;
4810: double ***p3mat;
4811: double eip;
4812:
4813: pstamp(ficreseij);
4814: fprintf(ficreseij,"# (a) Life expectancies by health status at initial age and (b) health expectancies by health status at initial age\n");
4815: fprintf(ficreseij,"# Age");
4816: for(i=1; i<=nlstate;i++){
4817: for(j=1; j<=nlstate;j++){
4818: fprintf(ficreseij," e%1d%1d ",i,j);
4819: }
4820: fprintf(ficreseij," e%1d. ",i);
4821: }
4822: fprintf(ficreseij,"\n");
4823:
4824:
4825: if(estepm < stepm){
4826: printf ("Problem %d lower than %d\n",estepm, stepm);
4827: }
4828: else hstepm=estepm;
4829: /* We compute the life expectancy from trapezoids spaced every estepm months
4830: * This is mainly to measure the difference between two models: for example
4831: * if stepm=24 months pijx are given only every 2 years and by summing them
4832: * we are calculating an estimate of the Life Expectancy assuming a linear
4833: * progression in between and thus overestimating or underestimating according
4834: * to the curvature of the survival function. If, for the same date, we
4835: * estimate the model with stepm=1 month, we can keep estepm to 24 months
4836: * to compare the new estimate of Life expectancy with the same linear
4837: * hypothesis. A more precise result, taking into account a more precise
4838: * curvature will be obtained if estepm is as small as stepm. */
4839:
4840: /* For example we decided to compute the life expectancy with the smallest unit */
4841: /* hstepm beeing the number of stepms, if hstepm=1 the length of hstepm is stepm.
4842: nhstepm is the number of hstepm from age to agelim
4843: nstepm is the number of stepm from age to agelin.
4844: Look at hpijx to understand the reason of that which relies in memory size
4845: and note for a fixed period like estepm months */
4846: /* We decided (b) to get a life expectancy respecting the most precise curvature of the
4847: survival function given by stepm (the optimization length). Unfortunately it
4848: means that if the survival funtion is printed only each two years of age and if
4849: you sum them up and add 1 year (area under the trapezoids) you won't get the same
4850: results. So we changed our mind and took the option of the best precision.
4851: */
4852: hstepm=hstepm/stepm; /* Typically in stepm units, if stepm=6 & estepm=24 , = 24/6 months = 4 */
4853:
4854: agelim=AGESUP;
4855: /* If stepm=6 months */
4856: /* Computed by stepm unit matrices, product of hstepm matrices, stored
4857: in an array of nhstepm length: nhstepm=10, hstepm=4, stepm=6 months */
4858:
4859: /* nhstepm age range expressed in number of stepm */
4860: nstepm=(int) rint((agelim-bage)*YEARM/stepm); /* Biggest nstepm */
4861: /* Typically if 20 years nstepm = 20*12/6=40 stepm */
4862: /* if (stepm >= YEARM) hstepm=1;*/
4863: nhstepm = nstepm/hstepm;/* Expressed in hstepm, typically nhstepm=40/4=10 */
4864: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
4865:
4866: for (age=bage; age<=fage; age ++){
4867: nstepma=(int) rint((agelim-bage)*YEARM/stepm); /* Biggest nstepm */
4868: /* Typically if 20 years nstepm = 20*12/6=40 stepm */
4869: /* if (stepm >= YEARM) hstepm=1;*/
4870: nhstepma = nstepma/hstepm;/* Expressed in hstepm, typically nhstepma=40/4=10 */
4871:
4872: /* If stepm=6 months */
4873: /* Computed by stepm unit matrices, product of hstepma matrices, stored
4874: in an array of nhstepma length: nhstepma=10, hstepm=4, stepm=6 months */
4875:
4876: hpxij(p3mat,nhstepma,age,hstepm,x,nlstate,stepm,oldm, savm, cij);
4877:
4878: hf=hstepm*stepm/YEARM; /* Duration of hstepm expressed in year unit. */
4879:
4880: printf("%d|",(int)age);fflush(stdout);
4881: fprintf(ficlog,"%d|",(int)age);fflush(ficlog);
4882:
4883: /* Computing expectancies */
4884: for(i=1; i<=nlstate;i++)
4885: for(j=1; j<=nlstate;j++)
4886: for (h=0, eij[i][j][(int)age]=0; h<=nhstepm-1; h++){
4887: eij[i][j][(int)age] += (p3mat[i][j][h]+p3mat[i][j][h+1])/2.0*hf;
4888:
4889: /* 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]);*/
4890:
4891: }
4892:
4893: fprintf(ficreseij,"%3.0f",age );
4894: for(i=1; i<=nlstate;i++){
4895: eip=0;
4896: for(j=1; j<=nlstate;j++){
4897: eip +=eij[i][j][(int)age];
4898: fprintf(ficreseij,"%9.4f", eij[i][j][(int)age] );
4899: }
4900: fprintf(ficreseij,"%9.4f", eip );
4901: }
4902: fprintf(ficreseij,"\n");
4903:
4904: }
4905: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
4906: printf("\n");
4907: fprintf(ficlog,"\n");
4908:
4909: }
4910:
1.127 brouard 4911: void cvevsij(double ***eij, double x[], int nlstate, int stepm, int bage, int fage, double **oldm, double **savm, int cij, int estepm,double delti[],double **matcov,char strstart[] )
1.126 brouard 4912:
4913: {
4914: /* Covariances of health expectancies eij and of total life expectancies according
1.222 brouard 4915: to initial status i, ei. .
1.126 brouard 4916: */
4917: int i, j, nhstepm, hstepm, h, nstepm, k, cptj, cptj2, i2, j2, ij, ji;
4918: int nhstepma, nstepma; /* Decreasing with age */
4919: double age, agelim, hf;
4920: double ***p3matp, ***p3matm, ***varhe;
4921: double **dnewm,**doldm;
4922: double *xp, *xm;
4923: double **gp, **gm;
4924: double ***gradg, ***trgradg;
4925: int theta;
4926:
4927: double eip, vip;
4928:
4929: varhe=ma3x(1,nlstate*nlstate,1,nlstate*nlstate,(int) bage, (int) fage);
4930: xp=vector(1,npar);
4931: xm=vector(1,npar);
4932: dnewm=matrix(1,nlstate*nlstate,1,npar);
4933: doldm=matrix(1,nlstate*nlstate,1,nlstate*nlstate);
4934:
4935: pstamp(ficresstdeij);
4936: fprintf(ficresstdeij,"# Health expectancies with standard errors\n");
4937: fprintf(ficresstdeij,"# Age");
4938: for(i=1; i<=nlstate;i++){
4939: for(j=1; j<=nlstate;j++)
4940: fprintf(ficresstdeij," e%1d%1d (SE)",i,j);
4941: fprintf(ficresstdeij," e%1d. ",i);
4942: }
4943: fprintf(ficresstdeij,"\n");
4944:
4945: pstamp(ficrescveij);
4946: fprintf(ficrescveij,"# Subdiagonal matrix of covariances of health expectancies by age: cov(eij,ekl)\n");
4947: fprintf(ficrescveij,"# Age");
4948: for(i=1; i<=nlstate;i++)
4949: for(j=1; j<=nlstate;j++){
4950: cptj= (j-1)*nlstate+i;
4951: for(i2=1; i2<=nlstate;i2++)
4952: for(j2=1; j2<=nlstate;j2++){
4953: cptj2= (j2-1)*nlstate+i2;
4954: if(cptj2 <= cptj)
4955: fprintf(ficrescveij," %1d%1d,%1d%1d",i,j,i2,j2);
4956: }
4957: }
4958: fprintf(ficrescveij,"\n");
4959:
4960: if(estepm < stepm){
4961: printf ("Problem %d lower than %d\n",estepm, stepm);
4962: }
4963: else hstepm=estepm;
4964: /* We compute the life expectancy from trapezoids spaced every estepm months
4965: * This is mainly to measure the difference between two models: for example
4966: * if stepm=24 months pijx are given only every 2 years and by summing them
4967: * we are calculating an estimate of the Life Expectancy assuming a linear
4968: * progression in between and thus overestimating or underestimating according
4969: * to the curvature of the survival function. If, for the same date, we
4970: * estimate the model with stepm=1 month, we can keep estepm to 24 months
4971: * to compare the new estimate of Life expectancy with the same linear
4972: * hypothesis. A more precise result, taking into account a more precise
4973: * curvature will be obtained if estepm is as small as stepm. */
4974:
4975: /* For example we decided to compute the life expectancy with the smallest unit */
4976: /* hstepm beeing the number of stepms, if hstepm=1 the length of hstepm is stepm.
4977: nhstepm is the number of hstepm from age to agelim
4978: nstepm is the number of stepm from age to agelin.
4979: Look at hpijx to understand the reason of that which relies in memory size
4980: and note for a fixed period like estepm months */
4981: /* We decided (b) to get a life expectancy respecting the most precise curvature of the
4982: survival function given by stepm (the optimization length). Unfortunately it
4983: means that if the survival funtion is printed only each two years of age and if
4984: you sum them up and add 1 year (area under the trapezoids) you won't get the same
4985: results. So we changed our mind and took the option of the best precision.
4986: */
4987: hstepm=hstepm/stepm; /* Typically in stepm units, if stepm=6 & estepm=24 , = 24/6 months = 4 */
4988:
4989: /* If stepm=6 months */
4990: /* nhstepm age range expressed in number of stepm */
4991: agelim=AGESUP;
4992: nstepm=(int) rint((agelim-bage)*YEARM/stepm);
4993: /* Typically if 20 years nstepm = 20*12/6=40 stepm */
4994: /* if (stepm >= YEARM) hstepm=1;*/
4995: nhstepm = nstepm/hstepm;/* Expressed in hstepm, typically nhstepm=40/4=10 */
4996:
4997: p3matp=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
4998: p3matm=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
4999: gradg=ma3x(0,nhstepm,1,npar,1,nlstate*nlstate);
5000: trgradg =ma3x(0,nhstepm,1,nlstate*nlstate,1,npar);
5001: gp=matrix(0,nhstepm,1,nlstate*nlstate);
5002: gm=matrix(0,nhstepm,1,nlstate*nlstate);
5003:
5004: for (age=bage; age<=fage; age ++){
5005: nstepma=(int) rint((agelim-bage)*YEARM/stepm); /* Biggest nstepm */
5006: /* Typically if 20 years nstepm = 20*12/6=40 stepm */
5007: /* if (stepm >= YEARM) hstepm=1;*/
5008: nhstepma = nstepma/hstepm;/* Expressed in hstepm, typically nhstepma=40/4=10 */
1.218 brouard 5009:
1.126 brouard 5010: /* If stepm=6 months */
5011: /* Computed by stepm unit matrices, product of hstepma matrices, stored
5012: in an array of nhstepma length: nhstepma=10, hstepm=4, stepm=6 months */
5013:
5014: hf=hstepm*stepm/YEARM; /* Duration of hstepm expressed in year unit. */
1.218 brouard 5015:
1.126 brouard 5016: /* Computing Variances of health expectancies */
5017: /* Gradient is computed with plus gp and minus gm. Code is duplicated in order to
5018: decrease memory allocation */
5019: for(theta=1; theta <=npar; theta++){
5020: for(i=1; i<=npar; i++){
1.222 brouard 5021: xp[i] = x[i] + (i==theta ?delti[theta]:0);
5022: xm[i] = x[i] - (i==theta ?delti[theta]:0);
1.126 brouard 5023: }
5024: hpxij(p3matp,nhstepm,age,hstepm,xp,nlstate,stepm,oldm,savm, cij);
5025: hpxij(p3matm,nhstepm,age,hstepm,xm,nlstate,stepm,oldm,savm, cij);
1.218 brouard 5026:
1.126 brouard 5027: for(j=1; j<= nlstate; j++){
1.222 brouard 5028: for(i=1; i<=nlstate; i++){
5029: for(h=0; h<=nhstepm-1; h++){
5030: gp[h][(j-1)*nlstate + i] = (p3matp[i][j][h]+p3matp[i][j][h+1])/2.;
5031: gm[h][(j-1)*nlstate + i] = (p3matm[i][j][h]+p3matm[i][j][h+1])/2.;
5032: }
5033: }
1.126 brouard 5034: }
1.218 brouard 5035:
1.126 brouard 5036: for(ij=1; ij<= nlstate*nlstate; ij++)
1.222 brouard 5037: for(h=0; h<=nhstepm-1; h++){
5038: gradg[h][theta][ij]= (gp[h][ij]-gm[h][ij])/2./delti[theta];
5039: }
1.126 brouard 5040: }/* End theta */
5041:
5042:
5043: for(h=0; h<=nhstepm-1; h++)
5044: for(j=1; j<=nlstate*nlstate;j++)
1.222 brouard 5045: for(theta=1; theta <=npar; theta++)
5046: trgradg[h][j][theta]=gradg[h][theta][j];
1.126 brouard 5047:
1.218 brouard 5048:
1.222 brouard 5049: for(ij=1;ij<=nlstate*nlstate;ij++)
1.126 brouard 5050: for(ji=1;ji<=nlstate*nlstate;ji++)
1.222 brouard 5051: varhe[ij][ji][(int)age] =0.;
1.218 brouard 5052:
1.222 brouard 5053: printf("%d|",(int)age);fflush(stdout);
5054: fprintf(ficlog,"%d|",(int)age);fflush(ficlog);
5055: for(h=0;h<=nhstepm-1;h++){
1.126 brouard 5056: for(k=0;k<=nhstepm-1;k++){
1.222 brouard 5057: matprod2(dnewm,trgradg[h],1,nlstate*nlstate,1,npar,1,npar,matcov);
5058: matprod2(doldm,dnewm,1,nlstate*nlstate,1,npar,1,nlstate*nlstate,gradg[k]);
5059: for(ij=1;ij<=nlstate*nlstate;ij++)
5060: for(ji=1;ji<=nlstate*nlstate;ji++)
5061: varhe[ij][ji][(int)age] += doldm[ij][ji]*hf*hf;
1.126 brouard 5062: }
5063: }
1.218 brouard 5064:
1.126 brouard 5065: /* Computing expectancies */
5066: hpxij(p3matm,nhstepm,age,hstepm,x,nlstate,stepm,oldm, savm, cij);
5067: for(i=1; i<=nlstate;i++)
5068: for(j=1; j<=nlstate;j++)
1.222 brouard 5069: for (h=0, eij[i][j][(int)age]=0; h<=nhstepm-1; h++){
5070: eij[i][j][(int)age] += (p3matm[i][j][h]+p3matm[i][j][h+1])/2.0*hf;
1.218 brouard 5071:
1.222 brouard 5072: /* if((int)age==70)printf("i=%2d,j=%2d,h=%2d,age=%3d,%9.4f,%9.4f,%9.4f\n",i,j,h,(int)age,p3mat[i][j][h],hf,eij[i][j][(int)age]);*/
1.218 brouard 5073:
1.222 brouard 5074: }
1.218 brouard 5075:
1.126 brouard 5076: fprintf(ficresstdeij,"%3.0f",age );
5077: for(i=1; i<=nlstate;i++){
5078: eip=0.;
5079: vip=0.;
5080: for(j=1; j<=nlstate;j++){
1.222 brouard 5081: eip += eij[i][j][(int)age];
5082: for(k=1; k<=nlstate;k++) /* Sum on j and k of cov(eij,eik) */
5083: vip += varhe[(j-1)*nlstate+i][(k-1)*nlstate+i][(int)age];
5084: fprintf(ficresstdeij," %9.4f (%.4f)", eij[i][j][(int)age], sqrt(varhe[(j-1)*nlstate+i][(j-1)*nlstate+i][(int)age]) );
1.126 brouard 5085: }
5086: fprintf(ficresstdeij," %9.4f (%.4f)", eip, sqrt(vip));
5087: }
5088: fprintf(ficresstdeij,"\n");
1.218 brouard 5089:
1.126 brouard 5090: fprintf(ficrescveij,"%3.0f",age );
5091: for(i=1; i<=nlstate;i++)
5092: for(j=1; j<=nlstate;j++){
1.222 brouard 5093: cptj= (j-1)*nlstate+i;
5094: for(i2=1; i2<=nlstate;i2++)
5095: for(j2=1; j2<=nlstate;j2++){
5096: cptj2= (j2-1)*nlstate+i2;
5097: if(cptj2 <= cptj)
5098: fprintf(ficrescveij," %.4f", varhe[cptj][cptj2][(int)age]);
5099: }
1.126 brouard 5100: }
5101: fprintf(ficrescveij,"\n");
1.218 brouard 5102:
1.126 brouard 5103: }
5104: free_matrix(gm,0,nhstepm,1,nlstate*nlstate);
5105: free_matrix(gp,0,nhstepm,1,nlstate*nlstate);
5106: free_ma3x(gradg,0,nhstepm,1,npar,1,nlstate*nlstate);
5107: free_ma3x(trgradg,0,nhstepm,1,nlstate*nlstate,1,npar);
5108: free_ma3x(p3matm,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
5109: free_ma3x(p3matp,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
5110: printf("\n");
5111: fprintf(ficlog,"\n");
1.218 brouard 5112:
1.126 brouard 5113: free_vector(xm,1,npar);
5114: free_vector(xp,1,npar);
5115: free_matrix(dnewm,1,nlstate*nlstate,1,npar);
5116: free_matrix(doldm,1,nlstate*nlstate,1,nlstate*nlstate);
5117: free_ma3x(varhe,1,nlstate*nlstate,1,nlstate*nlstate,(int) bage, (int)fage);
5118: }
1.218 brouard 5119:
1.126 brouard 5120: /************ Variance ******************/
1.209 brouard 5121: void varevsij(char optionfilefiname[], double ***vareij, double **matcov, double x[], double delti[], int nlstate, int stepm, double bage, double fage, double **oldm, double **savm, double **prlim, double ftolpl, int *ncvyearp, int ij, int estepm, int cptcov, int cptcod, int popbased, int mobilav, char strstart[])
1.218 brouard 5122: {
5123: /* Variance of health expectancies */
5124: /* double **prevalim(double **prlim, int nlstate, double *xp, double age, double **oldm, double ** savm,double ftolpl);*/
5125: /* double **newm;*/
5126: /* int movingaverage(double ***probs, double bage,double fage, double ***mobaverage, int mobilav)*/
5127:
5128: /* int movingaverage(); */
5129: double **dnewm,**doldm;
5130: double **dnewmp,**doldmp;
5131: int i, j, nhstepm, hstepm, h, nstepm ;
5132: int k;
5133: double *xp;
5134: double **gp, **gm; /* for var eij */
5135: double ***gradg, ***trgradg; /*for var eij */
5136: double **gradgp, **trgradgp; /* for var p point j */
5137: double *gpp, *gmp; /* for var p point j */
5138: double **varppt; /* for var p point j nlstate to nlstate+ndeath */
5139: double ***p3mat;
5140: double age,agelim, hf;
5141: /* double ***mobaverage; */
5142: int theta;
5143: char digit[4];
5144: char digitp[25];
5145:
5146: char fileresprobmorprev[FILENAMELENGTH];
5147:
5148: if(popbased==1){
5149: if(mobilav!=0)
5150: strcpy(digitp,"-POPULBASED-MOBILAV_");
5151: else strcpy(digitp,"-POPULBASED-NOMOBIL_");
5152: }
5153: else
5154: strcpy(digitp,"-STABLBASED_");
1.126 brouard 5155:
1.218 brouard 5156: /* if (mobilav!=0) { */
5157: /* mobaverage= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
5158: /* if (movingaverage(probs, bage, fage, mobaverage,mobilav)!=0){ */
5159: /* fprintf(ficlog," Error in movingaverage mobilav=%d\n",mobilav); */
5160: /* printf(" Error in movingaverage mobilav=%d\n",mobilav); */
5161: /* } */
5162: /* } */
5163:
5164: strcpy(fileresprobmorprev,"PRMORPREV-");
5165: sprintf(digit,"%-d",ij);
5166: /*printf("DIGIT=%s, ij=%d ijr=%-d|\n",digit, ij,ij);*/
5167: strcat(fileresprobmorprev,digit); /* Tvar to be done */
5168: strcat(fileresprobmorprev,digitp); /* Popbased or not, mobilav or not */
5169: strcat(fileresprobmorprev,fileresu);
5170: if((ficresprobmorprev=fopen(fileresprobmorprev,"w"))==NULL) {
5171: printf("Problem with resultfile: %s\n", fileresprobmorprev);
5172: fprintf(ficlog,"Problem with resultfile: %s\n", fileresprobmorprev);
5173: }
5174: printf("Computing total mortality p.j=w1*p1j+w2*p2j+..: result on file '%s' \n",fileresprobmorprev);
5175: fprintf(ficlog,"Computing total mortality p.j=w1*p1j+w2*p2j+..: result on file '%s' \n",fileresprobmorprev);
5176: pstamp(ficresprobmorprev);
5177: 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);
5178: fprintf(ficresprobmorprev,"# Age cov=%-d",ij);
5179: for(j=nlstate+1; j<=(nlstate+ndeath);j++){
5180: fprintf(ficresprobmorprev," p.%-d SE",j);
5181: for(i=1; i<=nlstate;i++)
5182: fprintf(ficresprobmorprev," w%1d p%-d%-d",i,i,j);
5183: }
5184: fprintf(ficresprobmorprev,"\n");
5185:
5186: fprintf(ficgp,"\n# Routine varevsij");
5187: fprintf(ficgp,"\nunset title \n");
5188: /* fprintf(fichtm, "#Local time at start: %s", strstart);*/
5189: 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");
5190: fprintf(fichtm,"\n<br>%s <br>\n",digitp);
5191: /* } */
5192: varppt = matrix(nlstate+1,nlstate+ndeath,nlstate+1,nlstate+ndeath);
5193: pstamp(ficresvij);
5194: fprintf(ficresvij,"# Variance and covariance of health expectancies e.j \n# (weighted average of eij where weights are ");
5195: if(popbased==1)
5196: 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);
5197: else
5198: fprintf(ficresvij,"the age specific period (stable) prevalences in each health state \n");
5199: fprintf(ficresvij,"# Age");
5200: for(i=1; i<=nlstate;i++)
5201: for(j=1; j<=nlstate;j++)
5202: fprintf(ficresvij," Cov(e.%1d, e.%1d)",i,j);
5203: fprintf(ficresvij,"\n");
5204:
5205: xp=vector(1,npar);
5206: dnewm=matrix(1,nlstate,1,npar);
5207: doldm=matrix(1,nlstate,1,nlstate);
5208: dnewmp= matrix(nlstate+1,nlstate+ndeath,1,npar);
5209: doldmp= matrix(nlstate+1,nlstate+ndeath,nlstate+1,nlstate+ndeath);
5210:
5211: gradgp=matrix(1,npar,nlstate+1,nlstate+ndeath);
5212: gpp=vector(nlstate+1,nlstate+ndeath);
5213: gmp=vector(nlstate+1,nlstate+ndeath);
5214: trgradgp =matrix(nlstate+1,nlstate+ndeath,1,npar); /* mu or p point j*/
1.126 brouard 5215:
1.218 brouard 5216: if(estepm < stepm){
5217: printf ("Problem %d lower than %d\n",estepm, stepm);
5218: }
5219: else hstepm=estepm;
5220: /* For example we decided to compute the life expectancy with the smallest unit */
5221: /* hstepm beeing the number of stepms, if hstepm=1 the length of hstepm is stepm.
5222: nhstepm is the number of hstepm from age to agelim
5223: nstepm is the number of stepm from age to agelim.
5224: Look at function hpijx to understand why because of memory size limitations,
5225: we decided (b) to get a life expectancy respecting the most precise curvature of the
5226: survival function given by stepm (the optimization length). Unfortunately it
5227: means that if the survival funtion is printed every two years of age and if
5228: you sum them up and add 1 year (area under the trapezoids) you won't get the same
5229: results. So we changed our mind and took the option of the best precision.
5230: */
5231: hstepm=hstepm/stepm; /* Typically in stepm units, if stepm=6 & estepm=24 , = 24/6 months = 4 */
5232: agelim = AGESUP;
5233: for (age=bage; age<=fage; age ++){ /* If stepm=6 months */
5234: nstepm=(int) rint((agelim-age)*YEARM/stepm); /* Typically 20 years = 20*12/6=40 */
5235: nhstepm = nstepm/hstepm;/* Expressed in hstepm, typically nhstepm=40/4=10 */
5236: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
5237: gradg=ma3x(0,nhstepm,1,npar,1,nlstate);
5238: gp=matrix(0,nhstepm,1,nlstate);
5239: gm=matrix(0,nhstepm,1,nlstate);
5240:
5241:
5242: for(theta=1; theta <=npar; theta++){
5243: for(i=1; i<=npar; i++){ /* Computes gradient x + delta*/
5244: xp[i] = x[i] + (i==theta ?delti[theta]:0);
5245: }
5246:
5247: prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp,ij);
5248:
5249: if (popbased==1) {
5250: if(mobilav ==0){
5251: for(i=1; i<=nlstate;i++)
5252: prlim[i][i]=probs[(int)age][i][ij];
5253: }else{ /* mobilav */
5254: for(i=1; i<=nlstate;i++)
5255: prlim[i][i]=mobaverage[(int)age][i][ij];
5256: }
5257: }
5258:
5259: hpxij(p3mat,nhstepm,age,hstepm,xp,nlstate,stepm,oldm,savm, ij); /* Returns p3mat[i][j][h] for h=1 to nhstepm */
5260: for(j=1; j<= nlstate; j++){
5261: for(h=0; h<=nhstepm; h++){
5262: for(i=1, gp[h][j]=0.;i<=nlstate;i++)
5263: gp[h][j] += prlim[i][i]*p3mat[i][j][h];
5264: }
5265: }
5266: /* Next for computing probability of death (h=1 means
5267: computed over hstepm matrices product = hstepm*stepm months)
5268: as a weighted average of prlim.
5269: */
5270: for(j=nlstate+1;j<=nlstate+ndeath;j++){
5271: for(i=1,gpp[j]=0.; i<= nlstate; i++)
5272: gpp[j] += prlim[i][i]*p3mat[i][j][1];
5273: }
5274: /* end probability of death */
5275:
5276: for(i=1; i<=npar; i++) /* Computes gradient x - delta */
5277: xp[i] = x[i] - (i==theta ?delti[theta]:0);
5278:
5279: prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp, ij);
5280:
5281: if (popbased==1) {
5282: if(mobilav ==0){
5283: for(i=1; i<=nlstate;i++)
5284: prlim[i][i]=probs[(int)age][i][ij];
5285: }else{ /* mobilav */
5286: for(i=1; i<=nlstate;i++)
5287: prlim[i][i]=mobaverage[(int)age][i][ij];
5288: }
5289: }
5290:
5291: hpxij(p3mat,nhstepm,age,hstepm,xp,nlstate,stepm,oldm,savm, ij);
5292:
5293: for(j=1; j<= nlstate; j++){ /* Sum of wi * eij = e.j */
5294: for(h=0; h<=nhstepm; h++){
5295: for(i=1, gm[h][j]=0.;i<=nlstate;i++)
5296: gm[h][j] += prlim[i][i]*p3mat[i][j][h];
5297: }
5298: }
5299: /* This for computing probability of death (h=1 means
5300: computed over hstepm matrices product = hstepm*stepm months)
5301: as a weighted average of prlim.
5302: */
5303: for(j=nlstate+1;j<=nlstate+ndeath;j++){
5304: for(i=1,gmp[j]=0.; i<= nlstate; i++)
5305: gmp[j] += prlim[i][i]*p3mat[i][j][1];
5306: }
5307: /* end probability of death */
5308:
5309: for(j=1; j<= nlstate; j++) /* vareij */
5310: for(h=0; h<=nhstepm; h++){
5311: gradg[h][theta][j]= (gp[h][j]-gm[h][j])/2./delti[theta];
5312: }
5313:
5314: for(j=nlstate+1; j<= nlstate+ndeath; j++){ /* var mu */
5315: gradgp[theta][j]= (gpp[j]-gmp[j])/2./delti[theta];
5316: }
5317:
5318: } /* End theta */
5319:
5320: trgradg =ma3x(0,nhstepm,1,nlstate,1,npar); /* veij */
5321:
5322: for(h=0; h<=nhstepm; h++) /* veij */
5323: for(j=1; j<=nlstate;j++)
5324: for(theta=1; theta <=npar; theta++)
5325: trgradg[h][j][theta]=gradg[h][theta][j];
5326:
5327: for(j=nlstate+1; j<=nlstate+ndeath;j++) /* mu */
5328: for(theta=1; theta <=npar; theta++)
5329: trgradgp[j][theta]=gradgp[theta][j];
5330:
5331:
5332: hf=hstepm*stepm/YEARM; /* Duration of hstepm expressed in year unit. */
5333: for(i=1;i<=nlstate;i++)
5334: for(j=1;j<=nlstate;j++)
5335: vareij[i][j][(int)age] =0.;
5336:
5337: for(h=0;h<=nhstepm;h++){
5338: for(k=0;k<=nhstepm;k++){
5339: matprod2(dnewm,trgradg[h],1,nlstate,1,npar,1,npar,matcov);
5340: matprod2(doldm,dnewm,1,nlstate,1,npar,1,nlstate,gradg[k]);
5341: for(i=1;i<=nlstate;i++)
5342: for(j=1;j<=nlstate;j++)
5343: vareij[i][j][(int)age] += doldm[i][j]*hf*hf;
5344: }
5345: }
5346:
5347: /* pptj */
5348: matprod2(dnewmp,trgradgp,nlstate+1,nlstate+ndeath,1,npar,1,npar,matcov);
5349: matprod2(doldmp,dnewmp,nlstate+1,nlstate+ndeath,1,npar,nlstate+1,nlstate+ndeath,gradgp);
5350: for(j=nlstate+1;j<=nlstate+ndeath;j++)
5351: for(i=nlstate+1;i<=nlstate+ndeath;i++)
5352: varppt[j][i]=doldmp[j][i];
5353: /* end ppptj */
5354: /* x centered again */
5355:
5356: prevalim(prlim,nlstate,x,age,oldm,savm,ftolpl,ncvyearp,ij);
5357:
5358: if (popbased==1) {
5359: if(mobilav ==0){
5360: for(i=1; i<=nlstate;i++)
5361: prlim[i][i]=probs[(int)age][i][ij];
5362: }else{ /* mobilav */
5363: for(i=1; i<=nlstate;i++)
5364: prlim[i][i]=mobaverage[(int)age][i][ij];
5365: }
5366: }
5367:
5368: /* This for computing probability of death (h=1 means
5369: computed over hstepm (estepm) matrices product = hstepm*stepm months)
5370: as a weighted average of prlim.
5371: */
5372: hpxij(p3mat,nhstepm,age,hstepm,x,nlstate,stepm,oldm,savm, ij);
5373: for(j=nlstate+1;j<=nlstate+ndeath;j++){
5374: for(i=1,gmp[j]=0.;i<= nlstate; i++)
5375: gmp[j] += prlim[i][i]*p3mat[i][j][1];
5376: }
5377: /* end probability of death */
5378:
5379: fprintf(ficresprobmorprev,"%3d %d ",(int) age, ij);
5380: for(j=nlstate+1; j<=(nlstate+ndeath);j++){
5381: fprintf(ficresprobmorprev," %11.3e %11.3e",gmp[j], sqrt(varppt[j][j]));
5382: for(i=1; i<=nlstate;i++){
5383: fprintf(ficresprobmorprev," %11.3e %11.3e ",prlim[i][i],p3mat[i][j][1]);
5384: }
5385: }
5386: fprintf(ficresprobmorprev,"\n");
5387:
5388: fprintf(ficresvij,"%.0f ",age );
5389: for(i=1; i<=nlstate;i++)
5390: for(j=1; j<=nlstate;j++){
5391: fprintf(ficresvij," %.4f", vareij[i][j][(int)age]);
5392: }
5393: fprintf(ficresvij,"\n");
5394: free_matrix(gp,0,nhstepm,1,nlstate);
5395: free_matrix(gm,0,nhstepm,1,nlstate);
5396: free_ma3x(gradg,0,nhstepm,1,npar,1,nlstate);
5397: free_ma3x(trgradg,0,nhstepm,1,nlstate,1,npar);
5398: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
5399: } /* End age */
5400: free_vector(gpp,nlstate+1,nlstate+ndeath);
5401: free_vector(gmp,nlstate+1,nlstate+ndeath);
5402: free_matrix(gradgp,1,npar,nlstate+1,nlstate+ndeath);
5403: free_matrix(trgradgp,nlstate+1,nlstate+ndeath,1,npar); /* mu or p point j*/
5404: /* fprintf(ficgp,"\nunset parametric;unset label; set ter png small size 320, 240"); */
5405: fprintf(ficgp,"\nunset parametric;unset label; set ter svg size 640, 480");
5406: /* for(j=nlstate+1; j<= nlstate+ndeath; j++){ *//* Only the first actually */
5407: fprintf(ficgp,"\n set log y; unset log x;set xlabel \"Age\"; set ylabel \"Force of mortality (year-1)\";");
5408: fprintf(ficgp,"\nset out \"%s%s.svg\";",subdirf3(optionfilefiname,"VARMUPTJGR-",digitp),digit);
5409: /* fprintf(ficgp,"\n plot \"%s\" u 1:($3*%6.3f) not w l 1 ",fileresprobmorprev,YEARM/estepm); */
5410: /* fprintf(ficgp,"\n replot \"%s\" u 1:(($3+1.96*$4)*%6.3f) t \"95\%% interval\" w l 2 ",fileresprobmorprev,YEARM/estepm); */
5411: /* fprintf(ficgp,"\n replot \"%s\" u 1:(($3-1.96*$4)*%6.3f) not w l 2 ",fileresprobmorprev,YEARM/estepm); */
5412: fprintf(ficgp,"\n plot \"%s\" u 1:($3) not w l lt 1 ",subdirf(fileresprobmorprev));
5413: fprintf(ficgp,"\n replot \"%s\" u 1:(($3+1.96*$4)) t \"95%% interval\" w l lt 2 ",subdirf(fileresprobmorprev));
5414: fprintf(ficgp,"\n replot \"%s\" u 1:(($3-1.96*$4)) not w l lt 2 ",subdirf(fileresprobmorprev));
5415: fprintf(fichtm,"\n<br> File (multiple files are possible if covariates are present): <A href=\"%s\">%s</a>\n",subdirf(fileresprobmorprev),subdirf(fileresprobmorprev));
5416: 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);
5417: /* fprintf(fichtm,"\n<br> Probability is computed over estepm=%d months and then divided by estepm and multiplied by %.0f in order to have the probability to die over a year <br> <img src=\"varmuptjgr%s%s.svg\"> <br>\n", stepm,YEARM,digitp,digit);
1.126 brouard 5418: */
1.218 brouard 5419: /* fprintf(ficgp,"\nset out \"varmuptjgr%s%s%s.svg\";replot;",digitp,optionfilefiname,digit); */
5420: fprintf(ficgp,"\nset out;\nset out \"%s%s.svg\";replot;set out;\n",subdirf3(optionfilefiname,"VARMUPTJGR-",digitp),digit);
1.126 brouard 5421:
1.218 brouard 5422: free_vector(xp,1,npar);
5423: free_matrix(doldm,1,nlstate,1,nlstate);
5424: free_matrix(dnewm,1,nlstate,1,npar);
5425: free_matrix(doldmp,nlstate+1,nlstate+ndeath,nlstate+1,nlstate+ndeath);
5426: free_matrix(dnewmp,nlstate+1,nlstate+ndeath,1,npar);
5427: free_matrix(varppt,nlstate+1,nlstate+ndeath,nlstate+1,nlstate+ndeath);
5428: /* if (mobilav!=0) free_ma3x(mobaverage,1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
5429: fclose(ficresprobmorprev);
5430: fflush(ficgp);
5431: fflush(fichtm);
5432: } /* end varevsij */
1.126 brouard 5433:
5434: /************ Variance of prevlim ******************/
1.209 brouard 5435: void varprevlim(char fileres[], double **varpl, double **matcov, double x[], double delti[], int nlstate, int stepm, double bage, double fage, double **oldm, double **savm, double **prlim, double ftolpl, int *ncvyearp, int ij, char strstart[])
1.126 brouard 5436: {
1.205 brouard 5437: /* Variance of prevalence limit for each state ij using current parameters x[] and estimates of neighbourhood give by delti*/
1.126 brouard 5438: /* double **prevalim(double **prlim, int nlstate, double *xp, double age, double **oldm, double **savm,double ftolpl);*/
1.164 brouard 5439:
1.126 brouard 5440: double **dnewm,**doldm;
5441: int i, j, nhstepm, hstepm;
5442: double *xp;
5443: double *gp, *gm;
5444: double **gradg, **trgradg;
1.208 brouard 5445: double **mgm, **mgp;
1.126 brouard 5446: double age,agelim;
5447: int theta;
5448:
5449: pstamp(ficresvpl);
5450: fprintf(ficresvpl,"# Standard deviation of period (stable) prevalences \n");
5451: fprintf(ficresvpl,"# Age");
5452: for(i=1; i<=nlstate;i++)
5453: fprintf(ficresvpl," %1d-%1d",i,i);
5454: fprintf(ficresvpl,"\n");
5455:
5456: xp=vector(1,npar);
5457: dnewm=matrix(1,nlstate,1,npar);
5458: doldm=matrix(1,nlstate,1,nlstate);
5459:
5460: hstepm=1*YEARM; /* Every year of age */
5461: hstepm=hstepm/stepm; /* Typically in stepm units, if j= 2 years, = 2/6 months = 4 */
5462: agelim = AGESUP;
5463: for (age=bage; age<=fage; age ++){ /* If stepm=6 months */
5464: nhstepm=(int) rint((agelim-age)*YEARM/stepm); /* Typically 20 years = 20*12/6=40 */
5465: if (stepm >= YEARM) hstepm=1;
5466: nhstepm = nhstepm/hstepm; /* Typically 40/4=10 */
5467: gradg=matrix(1,npar,1,nlstate);
1.208 brouard 5468: mgp=matrix(1,npar,1,nlstate);
5469: mgm=matrix(1,npar,1,nlstate);
1.126 brouard 5470: gp=vector(1,nlstate);
5471: gm=vector(1,nlstate);
5472:
5473: for(theta=1; theta <=npar; theta++){
5474: for(i=1; i<=npar; i++){ /* Computes gradient */
5475: xp[i] = x[i] + (i==theta ?delti[theta]:0);
5476: }
1.209 brouard 5477: if((int)age==79 ||(int)age== 80 ||(int)age== 81 )
5478: prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp,ij);
5479: else
5480: prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp,ij);
1.208 brouard 5481: for(i=1;i<=nlstate;i++){
1.126 brouard 5482: gp[i] = prlim[i][i];
1.208 brouard 5483: mgp[theta][i] = prlim[i][i];
5484: }
1.126 brouard 5485: for(i=1; i<=npar; i++) /* Computes gradient */
5486: xp[i] = x[i] - (i==theta ?delti[theta]:0);
1.209 brouard 5487: if((int)age==79 ||(int)age== 80 ||(int)age== 81 )
5488: prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp,ij);
5489: else
5490: prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp,ij);
1.208 brouard 5491: for(i=1;i<=nlstate;i++){
1.126 brouard 5492: gm[i] = prlim[i][i];
1.208 brouard 5493: mgm[theta][i] = prlim[i][i];
5494: }
1.126 brouard 5495: for(i=1;i<=nlstate;i++)
5496: gradg[theta][i]= (gp[i]-gm[i])/2./delti[theta];
1.209 brouard 5497: /* gradg[theta][2]= -gradg[theta][1]; */ /* For testing if nlstate=2 */
1.126 brouard 5498: } /* End theta */
5499:
5500: trgradg =matrix(1,nlstate,1,npar);
5501:
5502: for(j=1; j<=nlstate;j++)
5503: for(theta=1; theta <=npar; theta++)
5504: trgradg[j][theta]=gradg[theta][j];
1.209 brouard 5505: /* if((int)age==79 ||(int)age== 80 ||(int)age== 81 ){ */
5506: /* printf("\nmgm mgp %d ",(int)age); */
5507: /* for(j=1; j<=nlstate;j++){ */
5508: /* printf(" %d ",j); */
5509: /* for(theta=1; theta <=npar; theta++) */
5510: /* printf(" %d %lf %lf",theta,mgm[theta][j],mgp[theta][j]); */
5511: /* printf("\n "); */
5512: /* } */
5513: /* } */
5514: /* if((int)age==79 ||(int)age== 80 ||(int)age== 81 ){ */
5515: /* printf("\n gradg %d ",(int)age); */
5516: /* for(j=1; j<=nlstate;j++){ */
5517: /* printf("%d ",j); */
5518: /* for(theta=1; theta <=npar; theta++) */
5519: /* printf("%d %lf ",theta,gradg[theta][j]); */
5520: /* printf("\n "); */
5521: /* } */
5522: /* } */
1.126 brouard 5523:
5524: for(i=1;i<=nlstate;i++)
5525: varpl[i][(int)age] =0.;
1.209 brouard 5526: if((int)age==79 ||(int)age== 80 ||(int)age== 81){
1.205 brouard 5527: matprod2(dnewm,trgradg,1,nlstate,1,npar,1,npar,matcov);
5528: matprod2(doldm,dnewm,1,nlstate,1,npar,1,nlstate,gradg);
5529: }else{
1.126 brouard 5530: matprod2(dnewm,trgradg,1,nlstate,1,npar,1,npar,matcov);
5531: matprod2(doldm,dnewm,1,nlstate,1,npar,1,nlstate,gradg);
1.205 brouard 5532: }
1.126 brouard 5533: for(i=1;i<=nlstate;i++)
5534: varpl[i][(int)age] = doldm[i][i]; /* Covariances are useless */
5535:
5536: fprintf(ficresvpl,"%.0f ",age );
5537: for(i=1; i<=nlstate;i++)
5538: fprintf(ficresvpl," %.5f (%.5f)",prlim[i][i],sqrt(varpl[i][(int)age]));
5539: fprintf(ficresvpl,"\n");
5540: free_vector(gp,1,nlstate);
5541: free_vector(gm,1,nlstate);
1.208 brouard 5542: free_matrix(mgm,1,npar,1,nlstate);
5543: free_matrix(mgp,1,npar,1,nlstate);
1.126 brouard 5544: free_matrix(gradg,1,npar,1,nlstate);
5545: free_matrix(trgradg,1,nlstate,1,npar);
5546: } /* End age */
5547:
5548: free_vector(xp,1,npar);
5549: free_matrix(doldm,1,nlstate,1,npar);
5550: free_matrix(dnewm,1,nlstate,1,nlstate);
5551:
5552: }
5553:
5554: /************ Variance of one-step probabilities ******************/
5555: void varprob(char optionfilefiname[], double **matcov, double x[], double delti[], int nlstate, double bage, double fage, int ij, int *Tvar, int **nbcode, int *ncodemax, char strstart[])
1.222 brouard 5556: {
5557: int i, j=0, k1, l1, tj;
5558: int k2, l2, j1, z1;
5559: int k=0, l;
5560: int first=1, first1, first2;
5561: double cv12, mu1, mu2, lc1, lc2, v12, v21, v11, v22,v1,v2, c12, tnalp;
5562: double **dnewm,**doldm;
5563: double *xp;
5564: double *gp, *gm;
5565: double **gradg, **trgradg;
5566: double **mu;
5567: double age, cov[NCOVMAX+1];
5568: double std=2.0; /* Number of standard deviation wide of confidence ellipsoids */
5569: int theta;
5570: char fileresprob[FILENAMELENGTH];
5571: char fileresprobcov[FILENAMELENGTH];
5572: char fileresprobcor[FILENAMELENGTH];
5573: double ***varpij;
5574:
5575: strcpy(fileresprob,"PROB_");
5576: strcat(fileresprob,fileres);
5577: if((ficresprob=fopen(fileresprob,"w"))==NULL) {
5578: printf("Problem with resultfile: %s\n", fileresprob);
5579: fprintf(ficlog,"Problem with resultfile: %s\n", fileresprob);
5580: }
5581: strcpy(fileresprobcov,"PROBCOV_");
5582: strcat(fileresprobcov,fileresu);
5583: if((ficresprobcov=fopen(fileresprobcov,"w"))==NULL) {
5584: printf("Problem with resultfile: %s\n", fileresprobcov);
5585: fprintf(ficlog,"Problem with resultfile: %s\n", fileresprobcov);
5586: }
5587: strcpy(fileresprobcor,"PROBCOR_");
5588: strcat(fileresprobcor,fileresu);
5589: if((ficresprobcor=fopen(fileresprobcor,"w"))==NULL) {
5590: printf("Problem with resultfile: %s\n", fileresprobcor);
5591: fprintf(ficlog,"Problem with resultfile: %s\n", fileresprobcor);
5592: }
5593: printf("Computing standard deviation of one-step probabilities: result on file '%s' \n",fileresprob);
5594: fprintf(ficlog,"Computing standard deviation of one-step probabilities: result on file '%s' \n",fileresprob);
5595: printf("Computing matrix of variance covariance of one-step probabilities: result on file '%s' \n",fileresprobcov);
5596: fprintf(ficlog,"Computing matrix of variance covariance of one-step probabilities: result on file '%s' \n",fileresprobcov);
5597: printf("and correlation matrix of one-step probabilities: result on file '%s' \n",fileresprobcor);
5598: fprintf(ficlog,"and correlation matrix of one-step probabilities: result on file '%s' \n",fileresprobcor);
5599: pstamp(ficresprob);
5600: fprintf(ficresprob,"#One-step probabilities and stand. devi in ()\n");
5601: fprintf(ficresprob,"# Age");
5602: pstamp(ficresprobcov);
5603: fprintf(ficresprobcov,"#One-step probabilities and covariance matrix\n");
5604: fprintf(ficresprobcov,"# Age");
5605: pstamp(ficresprobcor);
5606: fprintf(ficresprobcor,"#One-step probabilities and correlation matrix\n");
5607: fprintf(ficresprobcor,"# Age");
1.126 brouard 5608:
5609:
1.222 brouard 5610: for(i=1; i<=nlstate;i++)
5611: for(j=1; j<=(nlstate+ndeath);j++){
5612: fprintf(ficresprob," p%1d-%1d (SE)",i,j);
5613: fprintf(ficresprobcov," p%1d-%1d ",i,j);
5614: fprintf(ficresprobcor," p%1d-%1d ",i,j);
5615: }
5616: /* fprintf(ficresprob,"\n");
5617: fprintf(ficresprobcov,"\n");
5618: fprintf(ficresprobcor,"\n");
5619: */
5620: xp=vector(1,npar);
5621: dnewm=matrix(1,(nlstate)*(nlstate+ndeath),1,npar);
5622: doldm=matrix(1,(nlstate)*(nlstate+ndeath),1,(nlstate)*(nlstate+ndeath));
5623: mu=matrix(1,(nlstate)*(nlstate+ndeath), (int) bage, (int)fage);
5624: varpij=ma3x(1,nlstate*(nlstate+ndeath),1,nlstate*(nlstate+ndeath),(int) bage, (int) fage);
5625: first=1;
5626: fprintf(ficgp,"\n# Routine varprob");
5627: fprintf(fichtm,"\n<li><h4> Computing and drawing one step probabilities with their confidence intervals</h4></li>\n");
5628: fprintf(fichtm,"\n");
5629:
5630: fprintf(fichtm,"\n<li><h4> <a href=\"%s\">Matrix of variance-covariance of one-step probabilities (drawings)</a></h4> this page is important in order to visualize confidence intervals and especially correlation between disability and recovery, or more generally, way in and way back.</li>\n",optionfilehtmcov);
5631: 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);
5632: fprintf(fichtmcov,"\nEllipsoids of confidence centered on point (p<inf>ij</inf>, p<inf>kl</inf>) are estimated \
1.126 brouard 5633: and drawn. It helps understanding how is the covariance between two incidences.\
5634: They are expressed in year<sup>-1</sup> in order to be less dependent of stepm.<br>\n");
1.222 brouard 5635: fprintf(fichtmcov,"\n<br> Contour plot corresponding to x'cov<sup>-1</sup>x = 4 (where x is the column vector (pij,pkl)) are drawn. \
1.126 brouard 5636: It can be understood this way: if pij and pkl where uncorrelated the (2x2) matrix of covariance \
5637: would have been (1/(var pij), 0 , 0, 1/(var pkl)), and the confidence interval would be 2 \
5638: standard deviations wide on each axis. <br>\
5639: Now, if both incidences are correlated (usual case) we diagonalised the inverse of the covariance matrix\
5640: and made the appropriate rotation to look at the uncorrelated principal directions.<br>\
5641: To be simple, these graphs help to understand the significativity of each parameter in relation to a second other one.<br> \n");
5642:
1.222 brouard 5643: cov[1]=1;
5644: /* tj=cptcoveff; */
1.225 brouard 5645: tj = (int) pow(2,cptcoveff);
1.222 brouard 5646: if (cptcovn<1) {tj=1;ncodemax[1]=1;}
5647: j1=0;
1.224 brouard 5648: for(j1=1; j1<=tj;j1++){ /* For each valid combination of covariates or only once*/
1.222 brouard 5649: if (cptcovn>0) {
5650: fprintf(ficresprob, "\n#********** Variable ");
1.225 brouard 5651: for (z1=1; z1<=cptcoveff; z1++) fprintf(ficresprob, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
1.222 brouard 5652: fprintf(ficresprob, "**********\n#\n");
5653: fprintf(ficresprobcov, "\n#********** Variable ");
1.225 brouard 5654: for (z1=1; z1<=cptcoveff; z1++) fprintf(ficresprobcov, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
1.222 brouard 5655: fprintf(ficresprobcov, "**********\n#\n");
1.220 brouard 5656:
1.222 brouard 5657: fprintf(ficgp, "\n#********** Variable ");
1.225 brouard 5658: for (z1=1; z1<=cptcoveff; z1++) fprintf(ficgp, " V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
1.222 brouard 5659: fprintf(ficgp, "**********\n#\n");
1.220 brouard 5660:
5661:
1.222 brouard 5662: fprintf(fichtmcov, "\n<hr size=\"2\" color=\"#EC5E5E\">********** Variable ");
1.225 brouard 5663: for (z1=1; z1<=cptcoveff; z1++) fprintf(fichtm, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
1.222 brouard 5664: fprintf(fichtmcov, "**********\n<hr size=\"2\" color=\"#EC5E5E\">");
1.220 brouard 5665:
1.222 brouard 5666: fprintf(ficresprobcor, "\n#********** Variable ");
1.225 brouard 5667: for (z1=1; z1<=cptcoveff; z1++) fprintf(ficresprobcor, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
1.222 brouard 5668: fprintf(ficresprobcor, "**********\n#");
5669: if(invalidvarcomb[j1]){
5670: fprintf(ficgp,"\n#Combination (%d) ignored because no cases \n",j1);
5671: fprintf(fichtmcov,"\n<h3>Combination (%d) ignored because no cases </h3>\n",j1);
5672: continue;
5673: }
5674: }
5675: gradg=matrix(1,npar,1,(nlstate)*(nlstate+ndeath));
5676: trgradg=matrix(1,(nlstate)*(nlstate+ndeath),1,npar);
5677: gp=vector(1,(nlstate)*(nlstate+ndeath));
5678: gm=vector(1,(nlstate)*(nlstate+ndeath));
5679: for (age=bage; age<=fage; age ++){
5680: cov[2]=age;
5681: if(nagesqr==1)
5682: cov[3]= age*age;
5683: for (k=1; k<=cptcovn;k++) {
5684: cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(j1,k)];
5685: /*cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(j1,Tvar[k])];*//* j1 1 2 3 4
5686: * 1 1 1 1 1
5687: * 2 2 1 1 1
5688: * 3 1 2 1 1
5689: */
5690: /* nbcode[1][1]=0 nbcode[1][2]=1;*/
5691: }
5692: /* for (k=1; k<=cptcovage;k++) cov[2+Tage[k]]=cov[2+Tage[k]]*cov[2]; */
5693: for (k=1; k<=cptcovage;k++) cov[2+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,k)]*cov[2];
5694: for (k=1; k<=cptcovprod;k++)
5695: cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)]*nbcode[Tvard[k][2]][codtabm(ij,k)];
1.220 brouard 5696:
5697:
1.222 brouard 5698: for(theta=1; theta <=npar; theta++){
5699: for(i=1; i<=npar; i++)
5700: xp[i] = x[i] + (i==theta ?delti[theta]:(double)0);
1.220 brouard 5701:
1.222 brouard 5702: pmij(pmmij,cov,ncovmodel,xp,nlstate);
1.220 brouard 5703:
1.222 brouard 5704: k=0;
5705: for(i=1; i<= (nlstate); i++){
5706: for(j=1; j<=(nlstate+ndeath);j++){
5707: k=k+1;
5708: gp[k]=pmmij[i][j];
5709: }
5710: }
1.220 brouard 5711:
1.222 brouard 5712: for(i=1; i<=npar; i++)
5713: xp[i] = x[i] - (i==theta ?delti[theta]:(double)0);
1.220 brouard 5714:
1.222 brouard 5715: pmij(pmmij,cov,ncovmodel,xp,nlstate);
5716: k=0;
5717: for(i=1; i<=(nlstate); i++){
5718: for(j=1; j<=(nlstate+ndeath);j++){
5719: k=k+1;
5720: gm[k]=pmmij[i][j];
5721: }
5722: }
1.220 brouard 5723:
1.222 brouard 5724: for(i=1; i<= (nlstate)*(nlstate+ndeath); i++)
5725: gradg[theta][i]=(gp[i]-gm[i])/(double)2./delti[theta];
5726: }
1.126 brouard 5727:
1.222 brouard 5728: for(j=1; j<=(nlstate)*(nlstate+ndeath);j++)
5729: for(theta=1; theta <=npar; theta++)
5730: trgradg[j][theta]=gradg[theta][j];
1.220 brouard 5731:
1.222 brouard 5732: matprod2(dnewm,trgradg,1,(nlstate)*(nlstate+ndeath),1,npar,1,npar,matcov);
5733: matprod2(doldm,dnewm,1,(nlstate)*(nlstate+ndeath),1,npar,1,(nlstate)*(nlstate+ndeath),gradg);
1.220 brouard 5734:
1.222 brouard 5735: pmij(pmmij,cov,ncovmodel,x,nlstate);
1.220 brouard 5736:
1.222 brouard 5737: k=0;
5738: for(i=1; i<=(nlstate); i++){
5739: for(j=1; j<=(nlstate+ndeath);j++){
5740: k=k+1;
5741: mu[k][(int) age]=pmmij[i][j];
5742: }
5743: }
5744: for(i=1;i<=(nlstate)*(nlstate+ndeath);i++)
5745: for(j=1;j<=(nlstate)*(nlstate+ndeath);j++)
5746: varpij[i][j][(int)age] = doldm[i][j];
1.220 brouard 5747:
1.222 brouard 5748: /*printf("\n%d ",(int)age);
5749: for (i=1; i<=(nlstate)*(nlstate+ndeath);i++){
5750: printf("%e [%e ;%e] ",gm[i],gm[i]-2*sqrt(doldm[i][i]),gm[i]+2*sqrt(doldm[i][i]));
5751: fprintf(ficlog,"%e [%e ;%e] ",gm[i],gm[i]-2*sqrt(doldm[i][i]),gm[i]+2*sqrt(doldm[i][i]));
5752: }*/
1.220 brouard 5753:
1.222 brouard 5754: fprintf(ficresprob,"\n%d ",(int)age);
5755: fprintf(ficresprobcov,"\n%d ",(int)age);
5756: fprintf(ficresprobcor,"\n%d ",(int)age);
1.220 brouard 5757:
1.222 brouard 5758: for (i=1; i<=(nlstate)*(nlstate+ndeath);i++)
5759: fprintf(ficresprob,"%11.3e (%11.3e) ",mu[i][(int) age],sqrt(varpij[i][i][(int)age]));
5760: for (i=1; i<=(nlstate)*(nlstate+ndeath);i++){
5761: fprintf(ficresprobcov,"%11.3e ",mu[i][(int) age]);
5762: fprintf(ficresprobcor,"%11.3e ",mu[i][(int) age]);
5763: }
5764: i=0;
5765: for (k=1; k<=(nlstate);k++){
5766: for (l=1; l<=(nlstate+ndeath);l++){
5767: i++;
5768: fprintf(ficresprobcov,"\n%d %d-%d",(int)age,k,l);
5769: fprintf(ficresprobcor,"\n%d %d-%d",(int)age,k,l);
5770: for (j=1; j<=i;j++){
5771: /* printf(" k=%d l=%d i=%d j=%d\n",k,l,i,j);fflush(stdout); */
5772: fprintf(ficresprobcov," %11.3e",varpij[i][j][(int)age]);
5773: fprintf(ficresprobcor," %11.3e",varpij[i][j][(int) age]/sqrt(varpij[i][i][(int) age])/sqrt(varpij[j][j][(int)age]));
5774: }
5775: }
5776: }/* end of loop for state */
5777: } /* end of loop for age */
5778: free_vector(gp,1,(nlstate+ndeath)*(nlstate+ndeath));
5779: free_vector(gm,1,(nlstate+ndeath)*(nlstate+ndeath));
5780: free_matrix(trgradg,1,(nlstate+ndeath)*(nlstate+ndeath),1,npar);
5781: free_matrix(gradg,1,(nlstate+ndeath)*(nlstate+ndeath),1,npar);
5782:
5783: /* Confidence intervalle of pij */
5784: /*
5785: fprintf(ficgp,"\nunset parametric;unset label");
5786: fprintf(ficgp,"\nset log y;unset log x; set xlabel \"Age\";set ylabel \"probability (year-1)\"");
5787: fprintf(ficgp,"\nset ter png small\nset size 0.65,0.65");
5788: 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);
5789: fprintf(fichtm,"\n<br><img src=\"pijgr%s.png\"> ",optionfilefiname);
5790: fprintf(ficgp,"\nset out \"pijgr%s.png\"",optionfilefiname);
5791: fprintf(ficgp,"\nplot \"%s\" every :::%d::%d u 1:2 \"\%%lf",k1,k2,xfilevarprob);
5792: */
5793:
5794: /* Drawing ellipsoids of confidence of two variables p(k1-l1,k2-l2)*/
5795: first1=1;first2=2;
5796: for (k2=1; k2<=(nlstate);k2++){
5797: for (l2=1; l2<=(nlstate+ndeath);l2++){
5798: if(l2==k2) continue;
5799: j=(k2-1)*(nlstate+ndeath)+l2;
5800: for (k1=1; k1<=(nlstate);k1++){
5801: for (l1=1; l1<=(nlstate+ndeath);l1++){
5802: if(l1==k1) continue;
5803: i=(k1-1)*(nlstate+ndeath)+l1;
5804: if(i<=j) continue;
5805: for (age=bage; age<=fage; age ++){
5806: if ((int)age %5==0){
5807: v1=varpij[i][i][(int)age]/stepm*YEARM/stepm*YEARM;
5808: v2=varpij[j][j][(int)age]/stepm*YEARM/stepm*YEARM;
5809: cv12=varpij[i][j][(int)age]/stepm*YEARM/stepm*YEARM;
5810: mu1=mu[i][(int) age]/stepm*YEARM ;
5811: mu2=mu[j][(int) age]/stepm*YEARM;
5812: c12=cv12/sqrt(v1*v2);
5813: /* Computing eigen value of matrix of covariance */
5814: lc1=((v1+v2)+sqrt((v1+v2)*(v1+v2) - 4*(v1*v2-cv12*cv12)))/2.;
5815: lc2=((v1+v2)-sqrt((v1+v2)*(v1+v2) - 4*(v1*v2-cv12*cv12)))/2.;
5816: if ((lc2 <0) || (lc1 <0) ){
5817: if(first2==1){
5818: first1=0;
5819: 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);
5820: }
5821: 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);
5822: /* lc1=fabs(lc1); */ /* If we want to have them positive */
5823: /* lc2=fabs(lc2); */
5824: }
1.220 brouard 5825:
1.222 brouard 5826: /* Eigen vectors */
5827: v11=(1./sqrt(1+(v1-lc1)*(v1-lc1)/cv12/cv12));
5828: /*v21=sqrt(1.-v11*v11); *//* error */
5829: v21=(lc1-v1)/cv12*v11;
5830: v12=-v21;
5831: v22=v11;
5832: tnalp=v21/v11;
5833: if(first1==1){
5834: first1=0;
5835: 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);
5836: }
5837: 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);
5838: /*printf(fignu*/
5839: /* mu1+ v11*lc1*cost + v12*lc2*sin(t) */
5840: /* mu2+ v21*lc1*cost + v22*lc2*sin(t) */
5841: if(first==1){
5842: first=0;
5843: fprintf(ficgp,"\n# Ellipsoids of confidence\n#\n");
5844: fprintf(ficgp,"\nset parametric;unset label");
5845: 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);
5846: fprintf(ficgp,"\nset ter svg size 640, 480");
5847: fprintf(fichtmcov,"\n<br>Ellipsoids of confidence cov(p%1d%1d,p%1d%1d) expressed in year<sup>-1</sup>\
1.220 brouard 5848: :<a href=\"%s_%d%1d%1d-%1d%1d.svg\"> \
1.201 brouard 5849: %s_%d%1d%1d-%1d%1d.svg</A>, ",k1,l1,k2,l2,\
1.222 brouard 5850: subdirf2(optionfilefiname,"VARPIJGR_"), j1,k1,l1,k2,l2, \
5851: subdirf2(optionfilefiname,"VARPIJGR_"), j1,k1,l1,k2,l2);
5852: fprintf(fichtmcov,"\n<br><img src=\"%s_%d%1d%1d-%1d%1d.svg\"> ",subdirf2(optionfilefiname,"VARPIJGR_"), j1,k1,l1,k2,l2);
5853: fprintf(fichtmcov,"\n<br> Correlation at age %d (%.3f),",(int) age, c12);
5854: fprintf(ficgp,"\nset out \"%s_%d%1d%1d-%1d%1d.svg\"",subdirf2(optionfilefiname,"VARPIJGR_"), j1,k1,l1,k2,l2);
5855: fprintf(ficgp,"\nset label \"%d\" at %11.3e,%11.3e center",(int) age, mu1,mu2);
5856: fprintf(ficgp,"\n# Age %d, p%1d%1d - p%1d%1d",(int) age, k1,l1,k2,l2);
5857: 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", \
5858: mu1,std,v11,sqrt(lc1),v12,sqrt(lc2), \
5859: mu2,std,v21,sqrt(lc1),v22,sqrt(lc2));
5860: }else{
5861: first=0;
5862: fprintf(fichtmcov," %d (%.3f),",(int) age, c12);
5863: fprintf(ficgp,"\n# Age %d, p%1d%1d - p%1d%1d",(int) age, k1,l1,k2,l2);
5864: fprintf(ficgp,"\nset label \"%d\" at %11.3e,%11.3e center",(int) age, mu1,mu2);
5865: 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", \
5866: mu1,std,v11,sqrt(lc1),v12,sqrt(lc2), \
5867: mu2,std,v21,sqrt(lc1),v22,sqrt(lc2));
5868: }/* if first */
5869: } /* age mod 5 */
5870: } /* end loop age */
5871: fprintf(ficgp,"\nset out;\nset out \"%s_%d%1d%1d-%1d%1d.svg\";replot;set out;",subdirf2(optionfilefiname,"VARPIJGR_"), j1,k1,l1,k2,l2);
5872: first=1;
5873: } /*l12 */
5874: } /* k12 */
5875: } /*l1 */
5876: }/* k1 */
5877: } /* loop on combination of covariates j1 */
5878: free_ma3x(varpij,1,nlstate,1,nlstate+ndeath,(int) bage, (int)fage);
5879: free_matrix(mu,1,(nlstate+ndeath)*(nlstate+ndeath),(int) bage, (int)fage);
5880: free_matrix(doldm,1,(nlstate)*(nlstate+ndeath),1,(nlstate)*(nlstate+ndeath));
5881: free_matrix(dnewm,1,(nlstate)*(nlstate+ndeath),1,npar);
5882: free_vector(xp,1,npar);
5883: fclose(ficresprob);
5884: fclose(ficresprobcov);
5885: fclose(ficresprobcor);
5886: fflush(ficgp);
5887: fflush(fichtmcov);
5888: }
1.126 brouard 5889:
5890:
5891: /******************* Printing html file ***********/
1.201 brouard 5892: void printinghtml(char fileresu[], char title[], char datafile[], int firstpass, \
1.126 brouard 5893: int lastpass, int stepm, int weightopt, char model[],\
5894: int imx,int jmin, int jmax, double jmeanint,char rfileres[],\
1.217 brouard 5895: int popforecast, int prevfcast, int backcast, int estepm , \
1.213 brouard 5896: double jprev1, double mprev1,double anprev1, double dateprev1, \
5897: double jprev2, double mprev2,double anprev2, double dateprev2){
1.126 brouard 5898: int jj1, k1, i1, cpt;
5899:
5900: fprintf(fichtm,"<ul><li><a href='#firstorder'>Result files (first order: no variance)</a>\n \
5901: <li><a href='#secondorder'>Result files (second order (variance)</a>\n \
5902: </ul>");
1.214 brouard 5903: fprintf(fichtm,"<ul><li><h4><a name='firstorder'>Result files (first order: no variance)</a></h4>\n");
5904: 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",
5905: jprev1, mprev1,anprev1,jprev2, mprev2,anprev2,subdirfext3(optionfilefiname,"PHTMFR_",".htm"),subdirfext3(optionfilefiname,"PHTMFR_",".htm"));
5906: fprintf(fichtm,"<li> - Observed prevalence in each state (during the period defined between %.lf/%.lf/%.lf and %.lf/%.lf/%.lf): <a href=\"%s\">%s</a> (html file) ",
1.213 brouard 5907: jprev1, mprev1,anprev1,jprev2, mprev2,anprev2,subdirfext3(optionfilefiname,"PHTM_",".htm"),subdirfext3(optionfilefiname,"PHTM_",".htm"));
5908: fprintf(fichtm,", <a href=\"%s\">%s</a> (text file) <br>\n",subdirf2(fileresu,"P_"),subdirf2(fileresu,"P_"));
1.126 brouard 5909: fprintf(fichtm,"\
5910: - Estimated transition probabilities over %d (stepm) months: <a href=\"%s\">%s</a><br>\n ",
1.201 brouard 5911: stepm,subdirf2(fileresu,"PIJ_"),subdirf2(fileresu,"PIJ_"));
1.126 brouard 5912: fprintf(fichtm,"\
1.217 brouard 5913: - Estimated back transition probabilities over %d (stepm) months: <a href=\"%s\">%s</a><br>\n ",
5914: stepm,subdirf2(fileresu,"PIJB_"),subdirf2(fileresu,"PIJB_"));
5915: fprintf(fichtm,"\
1.126 brouard 5916: - Period (stable) prevalence in each health state: <a href=\"%s\">%s</a> <br>\n",
1.201 brouard 5917: subdirf2(fileresu,"PL_"),subdirf2(fileresu,"PL_"));
1.126 brouard 5918: fprintf(fichtm,"\
1.217 brouard 5919: - Period (stable) back prevalence in each health state: <a href=\"%s\">%s</a> <br>\n",
5920: subdirf2(fileresu,"PLB_"),subdirf2(fileresu,"PLB_"));
5921: fprintf(fichtm,"\
1.211 brouard 5922: - (a) Life expectancies by health status at initial age, e<sub>i.</sub> (b) health expectancies by health status at initial age, e<sub>ij</sub> . If one or more covariates are included, specific tables for each value of the covariate are output in sequences within the same file (estepm=%2d months): \
1.126 brouard 5923: <a href=\"%s\">%s</a> <br>\n",
1.201 brouard 5924: estepm,subdirf2(fileresu,"E_"),subdirf2(fileresu,"E_"));
1.211 brouard 5925: if(prevfcast==1){
5926: fprintf(fichtm,"\
5927: - Prevalence projections by age and states: \
1.201 brouard 5928: <a href=\"%s\">%s</a> <br>\n</li>", subdirf2(fileresu,"F_"),subdirf2(fileresu,"F_"));
1.211 brouard 5929: }
1.126 brouard 5930:
1.222 brouard 5931: fprintf(fichtm," \n<ul><li><b>Graphs</b></li><p>");
1.126 brouard 5932:
1.225 brouard 5933: m=pow(2,cptcoveff);
1.222 brouard 5934: if (cptcovn < 1) {m=1;ncodemax[1]=1;}
1.126 brouard 5935:
1.222 brouard 5936: jj1=0;
5937: for(k1=1; k1<=m;k1++){
1.220 brouard 5938:
1.222 brouard 5939: /* for(i1=1; i1<=ncodemax[k1];i1++){ */
5940: jj1++;
5941: if (cptcovn > 0) {
5942: fprintf(fichtm,"<hr size=\"2\" color=\"#EC5E5E\">************ Results for covariates");
1.225 brouard 5943: for (cpt=1; cpt<=cptcoveff;cpt++){
1.222 brouard 5944: fprintf(fichtm," V%d=%d ",Tvaraff[cpt],nbcode[Tvaraff[cpt]][codtabm(jj1,cpt)]);
5945: printf(" V%d=%d ",Tvaraff[cpt],nbcode[Tvaraff[cpt]][codtabm(jj1,cpt)]);fflush(stdout);
5946: }
1.230 brouard 5947: /* if(nqfveff+nqtveff 0) */ /* Test to be done */
1.222 brouard 5948: fprintf(fichtm," ************\n<hr size=\"2\" color=\"#EC5E5E\">");
5949: if(invalidvarcomb[k1]){
5950: fprintf(fichtm,"\n<h3>Combination (%d) ignored because no cases </h3>\n",k1);
5951: printf("\nCombination (%d) ignored because no cases \n",k1);
5952: continue;
5953: }
5954: }
5955: /* aij, bij */
5956: fprintf(fichtm,"<br>- Logit model (yours is: 1+age+%s), for example: logit(pij)=log(pij/pii)= aij+ bij age + V1 age + etc. as a function of age: <a href=\"%s_%d-1.svg\">%s_%d-1.svg</a><br> \
1.211 brouard 5957: <img src=\"%s_%d-1.svg\">",model,subdirf2(optionfilefiname,"PE_"),jj1,subdirf2(optionfilefiname,"PE_"),jj1,subdirf2(optionfilefiname,"PE_"),jj1);
1.222 brouard 5958: /* Pij */
5959: fprintf(fichtm,"<br>\n- P<sub>ij</sub> or conditional probabilities to be observed in state j being in state i, %d (stepm) months before: <a href=\"%s_%d-2.svg\">%s_%d-2.svg</a><br> \
1.201 brouard 5960: <img src=\"%s_%d-2.svg\">",stepm,subdirf2(optionfilefiname,"PE_"),jj1,subdirf2(optionfilefiname,"PE_"),jj1,subdirf2(optionfilefiname,"PE_"),jj1);
1.222 brouard 5961: /* Quasi-incidences */
5962: fprintf(fichtm,"<br>\n- I<sub>ij</sub> or Conditional probabilities to be observed in state j being in state i %d (stepm) months\
1.220 brouard 5963: before but expressed in per year i.e. quasi incidences if stepm is small and probabilities too, \
1.211 brouard 5964: incidence (rates) are the limit when h tends to zero of the ratio of the probability <sub>h</sub>P<sub>ij</sub> \
5965: divided by h: <sub>h</sub>P<sub>ij</sub>/h : <a href=\"%s_%d-3.svg\">%s_%d-3.svg</a><br> \
1.201 brouard 5966: <img src=\"%s_%d-3.svg\">",stepm,subdirf2(optionfilefiname,"PE_"),jj1,subdirf2(optionfilefiname,"PE_"),jj1,subdirf2(optionfilefiname,"PE_"),jj1);
1.222 brouard 5967: /* Survival functions (period) in state j */
5968: for(cpt=1; cpt<=nlstate;cpt++){
5969: fprintf(fichtm,"<br>\n- Survival functions in state %d. Or probability to survive in state %d being in state (1 to %d) at different ages. <a href=\"%s%d_%d.svg\">%s%d_%d.svg</a><br> \
1.201 brouard 5970: <img src=\"%s_%d-%d.svg\">", cpt, cpt, nlstate, subdirf2(optionfilefiname,"LIJ_"),cpt,jj1,subdirf2(optionfilefiname,"LIJ_"),cpt,jj1,subdirf2(optionfilefiname,"LIJ_"),cpt,jj1);
1.222 brouard 5971: }
5972: /* State specific survival functions (period) */
5973: for(cpt=1; cpt<=nlstate;cpt++){
5974: fprintf(fichtm,"<br>\n- Survival functions from state %d in each live state and total.\
1.220 brouard 5975: Or probability to survive in various states (1 to %d) being in state %d at different ages. \
1.201 brouard 5976: <a href=\"%s%d_%d.svg\">%s%d_%d.svg</a><br> <img src=\"%s_%d-%d.svg\">", cpt, nlstate, cpt, subdirf2(optionfilefiname,"LIJT_"),cpt,jj1,subdirf2(optionfilefiname,"LIJT_"),cpt,jj1,subdirf2(optionfilefiname,"LIJT_"),cpt,jj1);
1.222 brouard 5977: }
5978: /* Period (stable) prevalence in each health state */
5979: for(cpt=1; cpt<=nlstate;cpt++){
5980: fprintf(fichtm,"<br>\n- Convergence to period (stable) prevalence in state %d. Or probability to be in state %d being in state (1 to %d) at different ages. <a href=\"%s_%d-%d.svg\">%s_%d-%d.svg</a><br> \
1.201 brouard 5981: <img src=\"%s_%d-%d.svg\">", cpt, cpt, nlstate, subdirf2(optionfilefiname,"P_"),cpt,jj1,subdirf2(optionfilefiname,"P_"),cpt,jj1,subdirf2(optionfilefiname,"P_"),cpt,jj1);
1.222 brouard 5982: }
5983: if(backcast==1){
5984: /* Period (stable) back prevalence in each health state */
5985: for(cpt=1; cpt<=nlstate;cpt++){
5986: fprintf(fichtm,"<br>\n- Convergence to period (stable) back prevalence in state %d. Or probability to be in state %d being in state (1 to %d) at different ages. <a href=\"%s_%d-%d.svg\">%s_%d-%d.svg</a><br> \
1.217 brouard 5987: <img src=\"%s_%d-%d.svg\">", cpt, cpt, nlstate, subdirf2(optionfilefiname,"PB_"),cpt,jj1,subdirf2(optionfilefiname,"PB_"),cpt,jj1,subdirf2(optionfilefiname,"PB_"),cpt,jj1);
1.222 brouard 5988: }
1.217 brouard 5989: }
1.222 brouard 5990: if(prevfcast==1){
5991: /* Projection of prevalence up to period (stable) prevalence in each health state */
5992: for(cpt=1; cpt<=nlstate;cpt++){
5993: fprintf(fichtm,"<br>\n- Projection of cross-sectional prevalence (estimated with cases observed from %.1f to %.1f) up to period (stable) prevalence in state %d. Or probability to be in state %d being in state (1 to %d) at different ages. <a href=\"%s%d_%d.svg\">%s%d_%d.svg</a><br> \
1.213 brouard 5994: <img src=\"%s_%d-%d.svg\">", dateprev1, dateprev2, cpt, cpt, nlstate, subdirf2(optionfilefiname,"PROJ_"),cpt,jj1,subdirf2(optionfilefiname,"PROJ_"),cpt,jj1,subdirf2(optionfilefiname,"PROJ_"),cpt,jj1);
1.222 brouard 5995: }
5996: }
1.220 brouard 5997:
1.222 brouard 5998: for(cpt=1; cpt<=nlstate;cpt++) {
5999: fprintf(fichtm,"\n<br>- Life expectancy by health state (%d) at initial age and its decomposition into health expectancies in each alive state (1 to %d) (or area under each survival functions): <a href=\"%s_%d%d.svg\">%s_%d%d.svg</a> <br> \
1.201 brouard 6000: <img src=\"%s_%d%d.svg\">",cpt,nlstate,subdirf2(optionfilefiname,"EXP_"),cpt,jj1,subdirf2(optionfilefiname,"EXP_"),cpt,jj1,subdirf2(optionfilefiname,"EXP_"),cpt,jj1);
1.222 brouard 6001: }
6002: /* } /\* end i1 *\/ */
6003: }/* End k1 */
6004: fprintf(fichtm,"</ul>");
1.126 brouard 6005:
1.222 brouard 6006: fprintf(fichtm,"\
1.126 brouard 6007: \n<br><li><h4> <a name='secondorder'>Result files (second order: variances)</a></h4>\n\
1.193 brouard 6008: - Parameter file with estimated parameters and covariance matrix: <a href=\"%s\">%s</a> <br> \
1.203 brouard 6009: - 95%% confidence intervals and Wald tests of the estimated parameters are in the log file if optimization has been done (mle != 0).<br> \
1.197 brouard 6010: But because parameters are usually highly correlated (a higher incidence of disability \
6011: and a higher incidence of recovery can give very close observed transition) it might \
6012: be very useful to look not only at linear confidence intervals estimated from the \
6013: variances but at the covariance matrix. And instead of looking at the estimated coefficients \
6014: (parameters) of the logistic regression, it might be more meaningful to visualize the \
6015: covariance matrix of the one-step probabilities. \
6016: See page 'Matrix of variance-covariance of one-step probabilities' below. \n", rfileres,rfileres);
1.126 brouard 6017:
1.222 brouard 6018: fprintf(fichtm," - Standard deviation of one-step probabilities: <a href=\"%s\">%s</a> <br>\n",
6019: subdirf2(fileresu,"PROB_"),subdirf2(fileresu,"PROB_"));
6020: fprintf(fichtm,"\
1.126 brouard 6021: - Variance-covariance of one-step probabilities: <a href=\"%s\">%s</a> <br>\n",
1.222 brouard 6022: subdirf2(fileresu,"PROBCOV_"),subdirf2(fileresu,"PROBCOV_"));
1.126 brouard 6023:
1.222 brouard 6024: fprintf(fichtm,"\
1.126 brouard 6025: - Correlation matrix of one-step probabilities: <a href=\"%s\">%s</a> <br>\n",
1.222 brouard 6026: subdirf2(fileresu,"PROBCOR_"),subdirf2(fileresu,"PROBCOR_"));
6027: fprintf(fichtm,"\
1.126 brouard 6028: - 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): \
6029: <a href=\"%s\">%s</a> <br>\n</li>",
1.201 brouard 6030: estepm,subdirf2(fileresu,"CVE_"),subdirf2(fileresu,"CVE_"));
1.222 brouard 6031: fprintf(fichtm,"\
1.126 brouard 6032: - (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): \
6033: <a href=\"%s\">%s</a> <br>\n</li>",
1.201 brouard 6034: estepm,subdirf2(fileresu,"STDE_"),subdirf2(fileresu,"STDE_"));
1.222 brouard 6035: fprintf(fichtm,"\
1.128 brouard 6036: - Variances and covariances of health expectancies by age. Status (i) based health expectancies (in state j), e<sup>ij</sup> are weighted by the period prevalences in each state i (if popbased=1, an additional computation is done using the cross-sectional prevalences, i.e population based) (estepm=%d months): <a href=\"%s\">%s</a><br>\n",
1.222 brouard 6037: estepm, subdirf2(fileresu,"V_"),subdirf2(fileresu,"V_"));
6038: fprintf(fichtm,"\
1.128 brouard 6039: - Total life expectancy and total health expectancies to be spent in each health state e<sup>.j</sup> with their standard errors (if popbased=1, an additional computation is done using the cross-sectional prevalences, i.e population based) (estepm=%d months): <a href=\"%s\">%s</a> <br>\n",
1.222 brouard 6040: estepm, subdirf2(fileresu,"T_"),subdirf2(fileresu,"T_"));
6041: fprintf(fichtm,"\
1.126 brouard 6042: - Standard deviation of period (stable) prevalences: <a href=\"%s\">%s</a> <br>\n",\
1.222 brouard 6043: subdirf2(fileresu,"VPL_"),subdirf2(fileresu,"VPL_"));
1.126 brouard 6044:
6045: /* if(popforecast==1) fprintf(fichtm,"\n */
6046: /* - Prevalences forecasting: <a href=\"f%s\">f%s</a> <br>\n */
6047: /* - Population forecasting (if popforecast=1): <a href=\"pop%s\">pop%s</a> <br>\n */
6048: /* <br>",fileres,fileres,fileres,fileres); */
6049: /* else */
6050: /* fprintf(fichtm,"\n No population forecast: popforecast = %d (instead of 1) or stepm = %d (instead of 1) or model=%s (instead of .)<br><br></li>\n",popforecast, stepm, model); */
1.222 brouard 6051: fflush(fichtm);
6052: fprintf(fichtm," <ul><li><b>Graphs</b></li><p>");
1.126 brouard 6053:
1.225 brouard 6054: m=pow(2,cptcoveff);
1.222 brouard 6055: if (cptcovn < 1) {m=1;ncodemax[1]=1;}
1.126 brouard 6056:
1.222 brouard 6057: jj1=0;
6058: for(k1=1; k1<=m;k1++){
6059: /* for(i1=1; i1<=ncodemax[k1];i1++){ */
6060: jj1++;
1.126 brouard 6061: if (cptcovn > 0) {
6062: fprintf(fichtm,"<hr size=\"2\" color=\"#EC5E5E\">************ Results for covariates");
1.225 brouard 6063: for (cpt=1; cpt<=cptcoveff;cpt++) /**< cptcoveff number of variables */
1.222 brouard 6064: fprintf(fichtm," V%d=%d ",Tvaraff[cpt],nbcode[Tvaraff[cpt]][codtabm(jj1,cpt)]);
1.126 brouard 6065: fprintf(fichtm," ************\n<hr size=\"2\" color=\"#EC5E5E\">");
1.220 brouard 6066:
1.222 brouard 6067: if(invalidvarcomb[k1]){
6068: fprintf(fichtm,"\n<h4>Combination (%d) ignored because no cases </h4>\n",k1);
6069: continue;
6070: }
1.126 brouard 6071: }
6072: for(cpt=1; cpt<=nlstate;cpt++) {
1.218 brouard 6073: fprintf(fichtm,"\n<br>- Observed (cross-sectional) and period (incidence based) \
6074: prevalence (with 95%% confidence interval) in state (%d): <a href=\"%s_%d-%d.svg\"> %s_%d-%d.svg</a>\n <br>\
1.205 brouard 6075: <img src=\"%s_%d-%d.svg\">",cpt,subdirf2(optionfilefiname,"V_"),cpt,jj1,subdirf2(optionfilefiname,"V_"),cpt,jj1,subdirf2(optionfilefiname,"V_"),cpt,jj1);
1.126 brouard 6076: }
6077: fprintf(fichtm,"\n<br>- Total life expectancy by age and \
1.128 brouard 6078: health expectancies in states (1) and (2). If popbased=1 the smooth (due to the model) \
6079: true period expectancies (those weighted with period prevalences are also\
6080: drawn in addition to the population based expectancies computed using\
1.218 brouard 6081: observed and cahotic prevalences: <a href=\"%s_%d.svg\">%s_%d.svg</a>\n<br>\
1.205 brouard 6082: <img src=\"%s_%d.svg\">",subdirf2(optionfilefiname,"E_"),jj1,subdirf2(optionfilefiname,"E_"),jj1,subdirf2(optionfilefiname,"E_"),jj1);
1.222 brouard 6083: /* } /\* end i1 *\/ */
6084: }/* End k1 */
6085: fprintf(fichtm,"</ul>");
6086: fflush(fichtm);
1.126 brouard 6087: }
6088:
6089: /******************* Gnuplot file **************/
1.223 brouard 6090: void printinggnuplot(char fileresu[], char optionfilefiname[], double ageminpar, double agemaxpar, double fage , int prevfcast, int backcast, char pathc[], double p[]){
1.126 brouard 6091:
6092: char dirfileres[132],optfileres[132];
1.223 brouard 6093: char gplotcondition[132];
1.164 brouard 6094: int cpt=0,k1=0,i=0,k=0,j=0,jk=0,k2=0,k3=0,ij=0,l=0;
1.211 brouard 6095: int lv=0, vlv=0, kl=0;
1.130 brouard 6096: int ng=0;
1.201 brouard 6097: int vpopbased;
1.223 brouard 6098: int ioffset; /* variable offset for columns */
1.219 brouard 6099:
1.126 brouard 6100: /* if((ficgp=fopen(optionfilegnuplot,"a"))==NULL) { */
6101: /* printf("Problem with file %s",optionfilegnuplot); */
6102: /* fprintf(ficlog,"Problem with file %s",optionfilegnuplot); */
6103: /* } */
6104:
6105: /*#ifdef windows */
6106: fprintf(ficgp,"cd \"%s\" \n",pathc);
1.223 brouard 6107: /*#endif */
1.225 brouard 6108: m=pow(2,cptcoveff);
1.126 brouard 6109:
1.202 brouard 6110: /* Contribution to likelihood */
6111: /* Plot the probability implied in the likelihood */
1.223 brouard 6112: fprintf(ficgp,"\n# Contributions to the Likelihood, mle >=1. For mle=4 no interpolation, pure matrix products.\n#\n");
6113: fprintf(ficgp,"\n set log y; unset log x;set xlabel \"Age\"; set ylabel \"Likelihood (-2Log(L))\";");
6114: /* fprintf(ficgp,"\nset ter svg size 640, 480"); */ /* Too big for svg */
6115: fprintf(ficgp,"\nset ter pngcairo size 640, 480");
1.204 brouard 6116: /* nice for mle=4 plot by number of matrix products.
1.202 brouard 6117: replot "rrtest1/toto.txt" u 2:($4 == 1 && $5==2 ? $9 : 1/0):5 t "p12" with point lc 1 */
6118: /* replot exp(p1+p2*x)/(1+exp(p1+p2*x)+exp(p3+p4*x)+exp(p5+p6*x)) t "p12(x)" */
1.223 brouard 6119: /* fprintf(ficgp,"\nset out \"%s.svg\";",subdirf2(optionfilefiname,"ILK_")); */
6120: fprintf(ficgp,"\nset out \"%s-dest.png\";",subdirf2(optionfilefiname,"ILK_"));
6121: 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));
6122: fprintf(ficgp,"\nset out \"%s-ori.png\";",subdirf2(optionfilefiname,"ILK_"));
6123: 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));
6124: for (i=1; i<= nlstate ; i ++) {
6125: fprintf(ficgp,"\nset out \"%s-p%dj.png\";set ylabel \"Probability for each individual/wave\";",subdirf2(optionfilefiname,"ILK_"),i);
6126: fprintf(ficgp,"unset log;\n# plot weighted, mean weight should have point size of 0.5\n plot \"%s\"",subdirf(fileresilk));
6127: 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);
6128: for (j=2; j<= nlstate+ndeath ; j ++) {
6129: 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);
6130: }
6131: fprintf(ficgp,";\nset out; unset ylabel;\n");
6132: }
6133: /* 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 */
6134: /* fprintf(ficgp,"\nset log y;plot \"%s\" u 2:(-$11):3 t \"All sample, all transitions\" with dots lc variable",subdirf(fileresilk)); */
6135: /* fprintf(ficgp,"\nreplot \"%s\" u 2:($3 <= 3 ? -$11 : 1/0):3 t \"First 3 individuals\" with line lc variable", subdirf(fileresilk)); */
6136: fprintf(ficgp,"\nset out;unset log\n");
6137: /* fprintf(ficgp,"\nset out \"%s.svg\"; replot; set out; # bug gnuplot",subdirf2(optionfilefiname,"ILK_")); */
1.202 brouard 6138:
1.126 brouard 6139: strcpy(dirfileres,optionfilefiname);
6140: strcpy(optfileres,"vpl");
1.223 brouard 6141: /* 1eme*/
1.211 brouard 6142: for (cpt=1; cpt<= nlstate ; cpt ++) { /* For each live state */
1.230 brouard 6143: for (k1=1; k1<= m && selected(k1) ; k1 ++) { /* For each valid combination of covariate */
1.211 brouard 6144: /* plot [100000000000000000000:-100000000000000000000] "mysbiaspar/vplrmysbiaspar.txt to check */
6145: fprintf(ficgp,"\n# 1st: Period (stable) prevalence with CI: 'VPL_' files ");
1.225 brouard 6146: for (k=1; k<=cptcoveff; k++){ /* For each covariate k get corresponding value lv for combination k1 */
6147: lv= decodtabm(k1,k,cptcoveff); /* Should be the value of the covariate corresponding to k1 combination */
1.223 brouard 6148: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
6149: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
6150: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
6151: vlv= nbcode[Tvaraff[k]][lv]; /* vlv is the value of the covariate lv, 0 or 1 */
6152: /* For each combination of covariate k1 (V1=1, V3=0), we printed the current covariate k and its value vlv */
6153: fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv);
1.211 brouard 6154: }
6155: fprintf(ficgp,"\n#\n");
1.223 brouard 6156: if(invalidvarcomb[k1]){
6157: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
6158: continue;
6159: }
1.211 brouard 6160:
1.223 brouard 6161: fprintf(ficgp,"\nset out \"%s_%d-%d.svg\" \n",subdirf2(optionfilefiname,"V_"),cpt,k1);
6162: fprintf(ficgp,"\n#set out \"V_%s_%d-%d.svg\" \n",optionfilefiname,cpt,k1);
6163: fprintf(ficgp,"set xlabel \"Age\" \n\
1.219 brouard 6164: set ylabel \"Probability\" \n \
6165: set ter svg size 640, 480\n \
1.201 brouard 6166: plot [%.f:%.f] \"%s\" every :::%d::%d u 1:2 \"%%lf",ageminpar,fage,subdirf2(fileresu,"VPL_"),k1-1,k1-1);
1.219 brouard 6167:
1.223 brouard 6168: for (i=1; i<= nlstate ; i ++) {
6169: if (i==cpt) fprintf(ficgp," %%lf (%%lf)");
6170: else fprintf(ficgp," %%*lf (%%*lf)");
6171: }
6172: fprintf(ficgp,"\" t\"Period (stable) prevalence\" w l lt 0,\"%s\" every :::%d::%d u 1:($2+1.96*$3) \"%%lf",subdirf2(fileresu,"VPL_"),k1-1,k1-1);
6173: for (i=1; i<= nlstate ; i ++) {
6174: if (i==cpt) fprintf(ficgp," %%lf (%%lf)");
6175: else fprintf(ficgp," %%*lf (%%*lf)");
6176: }
6177: fprintf(ficgp,"\" t\"95%% CI\" w l lt 1,\"%s\" every :::%d::%d u 1:($2-1.96*$3) \"%%lf",subdirf2(fileresu,"VPL_"),k1-1,k1-1);
6178: for (i=1; i<= nlstate ; i ++) {
6179: if (i==cpt) fprintf(ficgp," %%lf (%%lf)");
6180: else fprintf(ficgp," %%*lf (%%*lf)");
6181: }
6182: 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));
6183: if(backcast==1){ /* We need to get the corresponding values of the covariates involved in this combination k1 */
6184: /* 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); */
6185: fprintf(ficgp,",\"%s\" u 1:((",subdirf2(fileresu,"PLB_")); /* Age is in 1 */
1.225 brouard 6186: if(cptcoveff ==0){
1.223 brouard 6187: fprintf(ficgp,"$%d)) t 'Backward prevalence in state %d' with line ", 2+(cpt-1), cpt );
6188: }else{
6189: kl=0;
1.225 brouard 6190: for (k=1; k<=cptcoveff; k++){ /* For each combination of covariate */
6191: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate value corresponding to k1 combination and kth covariate */
1.223 brouard 6192: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
6193: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
6194: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
6195: vlv= nbcode[Tvaraff[k]][lv];
6196: kl++;
6197: /* 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 *\/ */
6198: /*6+(cpt-1)*(nlstate+1)+1+(i-1)+(nlstate+1)*nlstate; 6+(1-1)*(2+1)+1+(1-1) +(2+1)*2=13 */
6199: /*6+1+(i-1)+(nlstate+1)*nlstate; 6+1+(1-1) +(2+1)*2=13 */
6200: /* '' u 6:(($1==1 && $2==0 && $3==2 && $4==0)? $9/(1.-$15) : 1/0):($5==2000? 3:2) t 'p.1' with line lc variable*/
1.225 brouard 6201: if(k==cptcoveff){
1.227 brouard 6202: fprintf(ficgp,"$%d==%d && $%d==%d)? $%d : 1/0) t 'Backward prevalence in state %d' ",kl+1, Tvaraff[k],kl+1+1,nbcode[Tvaraff[k]][lv], \
6203: 4+(cpt-1), cpt ); /* 4 or 6 ?*/
1.223 brouard 6204: }else{
6205: fprintf(ficgp,"$%d==%d && $%d==%d && ",kl+1, Tvaraff[k],kl+1+1,nbcode[Tvaraff[k]][lv]);
6206: kl++;
6207: }
6208: } /* end covariate */
6209: } /* end if no covariate */
6210: } /* end if backcast */
6211: fprintf(ficgp,"\nset out \n");
1.201 brouard 6212: } /* k1 */
6213: } /* cpt */
1.126 brouard 6214: /*2 eme*/
6215: for (k1=1; k1<= m ; k1 ++) {
1.220 brouard 6216:
1.223 brouard 6217: fprintf(ficgp,"\n# 2nd: Total life expectancy with CI: 't' files ");
1.225 brouard 6218: for (k=1; k<=cptcoveff; k++){ /* For each covariate and each value */
6219: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate number corresponding to k1 combination */
1.223 brouard 6220: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
6221: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
6222: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
6223: vlv= nbcode[Tvaraff[k]][lv];
6224: fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv);
6225: }
6226: fprintf(ficgp,"\n#\n");
6227: if(invalidvarcomb[k1]){
6228: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
6229: continue;
6230: }
1.219 brouard 6231:
1.223 brouard 6232: fprintf(ficgp,"\nset out \"%s_%d.svg\" \n",subdirf2(optionfilefiname,"E_"),k1);
6233: for(vpopbased=0; vpopbased <= popbased; vpopbased++){ /* Done for vpopbased=0 and vpopbased=1 if popbased==1*/
6234: if(vpopbased==0)
6235: fprintf(ficgp,"set ylabel \"Years\" \nset ter svg size 640, 480\nplot [%.f:%.f] ",ageminpar,fage);
6236: else
6237: fprintf(ficgp,"\nreplot ");
6238: for (i=1; i<= nlstate+1 ; i ++) {
6239: k=2*i;
6240: fprintf(ficgp,"\"%s\" every :::%d::%d u 1:($2==%d && $4!=0 ?$4 : 1/0) \"%%lf %%lf %%lf",subdirf2(fileresu,"T_"),k1-1,k1-1, vpopbased);
6241: for (j=1; j<= nlstate+1 ; j ++) {
6242: if (j==i) fprintf(ficgp," %%lf (%%lf)");
6243: else fprintf(ficgp," %%*lf (%%*lf)");
6244: }
6245: if (i== 1) fprintf(ficgp,"\" t\"TLE\" w l lt %d, \\\n",i);
6246: else fprintf(ficgp,"\" t\"LE in state (%d)\" w l lt %d, \\\n",i-1,i+1);
6247: fprintf(ficgp,"\"%s\" every :::%d::%d u 1:($2==%d && $4!=0 ? $4-$5*2 : 1/0) \"%%lf %%lf %%lf",subdirf2(fileresu,"T_"),k1-1,k1-1,vpopbased);
6248: for (j=1; j<= nlstate+1 ; j ++) {
6249: if (j==i) fprintf(ficgp," %%lf (%%lf)");
6250: else fprintf(ficgp," %%*lf (%%*lf)");
6251: }
6252: fprintf(ficgp,"\" t\"\" w l lt 0,");
6253: fprintf(ficgp,"\"%s\" every :::%d::%d u 1:($2==%d && $4!=0 ? $4+$5*2 : 1/0) \"%%lf %%lf %%lf",subdirf2(fileresu,"T_"),k1-1,k1-1,vpopbased);
6254: for (j=1; j<= nlstate+1 ; j ++) {
6255: if (j==i) fprintf(ficgp," %%lf (%%lf)");
6256: else fprintf(ficgp," %%*lf (%%*lf)");
6257: }
6258: if (i== (nlstate+1)) fprintf(ficgp,"\" t\"\" w l lt 0");
6259: else fprintf(ficgp,"\" t\"\" w l lt 0,\\\n");
6260: } /* state */
6261: } /* vpopbased */
6262: fprintf(ficgp,"\nset out;set out \"%s_%d.svg\"; replot; set out; \n",subdirf2(optionfilefiname,"E_"),k1); /* Buggy gnuplot */
1.201 brouard 6263: } /* k1 */
1.219 brouard 6264:
6265:
1.126 brouard 6266: /*3eme*/
6267: for (k1=1; k1<= m ; k1 ++) {
1.220 brouard 6268:
1.126 brouard 6269: for (cpt=1; cpt<= nlstate ; cpt ++) {
1.211 brouard 6270: fprintf(ficgp,"\n# 3d: Life expectancy with EXP_ files: cov=%d state=%d",k1, cpt);
1.225 brouard 6271: for (k=1; k<=cptcoveff; k++){ /* For each covariate and each value */
6272: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate number corresponding to k1 combination */
1.223 brouard 6273: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
6274: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
6275: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
6276: vlv= nbcode[Tvaraff[k]][lv];
6277: fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv);
1.211 brouard 6278: }
6279: fprintf(ficgp,"\n#\n");
1.223 brouard 6280: if(invalidvarcomb[k1]){
6281: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
6282: continue;
6283: }
1.219 brouard 6284:
1.126 brouard 6285: /* k=2+nlstate*(2*cpt-2); */
6286: k=2+(nlstate+1)*(cpt-1);
1.201 brouard 6287: fprintf(ficgp,"\nset out \"%s_%d%d.svg\" \n",subdirf2(optionfilefiname,"EXP_"),cpt,k1);
1.199 brouard 6288: fprintf(ficgp,"set ter svg size 640, 480\n\
1.201 brouard 6289: plot [%.f:%.f] \"%s\" every :::%d::%d u 1:%d t \"e%d1\" w l",ageminpar,fage,subdirf2(fileresu,"E_"),k1-1,k1-1,k,cpt);
1.126 brouard 6290: /*fprintf(ficgp,",\"e%s\" every :::%d::%d u 1:($%d-2*$%d) \"\%%lf ",fileres,k1-1,k1-1,k,k+1);
1.223 brouard 6291: for (i=1; i<= nlstate*2 ; i ++) fprintf(ficgp,"\%%lf (\%%lf) ");
6292: fprintf(ficgp,"\" t \"e%d1\" w l",cpt);
6293: fprintf(ficgp,",\"e%s\" every :::%d::%d u 1:($%d+2*$%d) \"\%%lf ",fileres,k1-1,k1-1,k,k+1);
6294: for (i=1; i<= nlstate*2 ; i ++) fprintf(ficgp,"\%%lf (\%%lf) ");
6295: fprintf(ficgp,"\" t \"e%d1\" w l",cpt);
1.219 brouard 6296:
1.126 brouard 6297: */
6298: for (i=1; i< nlstate ; i ++) {
1.223 brouard 6299: fprintf(ficgp," ,\"%s\" every :::%d::%d u 1:%d t \"e%d%d\" w l",subdirf2(fileresu,"E_"),k1-1,k1-1,k+i,cpt,i+1);
6300: /* fprintf(ficgp," ,\"%s\" every :::%d::%d u 1:%d t \"e%d%d\" w l",subdirf2(fileres,"e"),k1-1,k1-1,k+2*i,cpt,i+1);*/
1.219 brouard 6301:
1.126 brouard 6302: }
1.201 brouard 6303: fprintf(ficgp," ,\"%s\" every :::%d::%d u 1:%d t \"e%d.\" w l",subdirf2(fileresu,"E_"),k1-1,k1-1,k+nlstate,cpt);
1.126 brouard 6304: }
6305: }
6306:
1.223 brouard 6307: /* 4eme */
1.201 brouard 6308: /* Survival functions (period) from state i in state j by initial state i */
6309: for (k1=1; k1<= m ; k1 ++) { /* For each multivariate if any */
1.220 brouard 6310:
1.201 brouard 6311: for (cpt=1; cpt<=nlstate ; cpt ++) { /* For each life state */
1.211 brouard 6312: fprintf(ficgp,"\n#\n#\n# Survival functions in state j : 'LIJ_' files, cov=%d state=%d",k1, cpt);
1.225 brouard 6313: for (k=1; k<=cptcoveff; k++){ /* For each covariate and each value */
6314: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate number corresponding to k1 combination */
1.223 brouard 6315: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
6316: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
6317: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
6318: vlv= nbcode[Tvaraff[k]][lv];
6319: fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv);
1.211 brouard 6320: }
6321: fprintf(ficgp,"\n#\n");
1.223 brouard 6322: if(invalidvarcomb[k1]){
6323: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
6324: continue;
6325: }
1.220 brouard 6326:
1.201 brouard 6327: fprintf(ficgp,"\nset out \"%s_%d-%d.svg\" \n",subdirf2(optionfilefiname,"LIJ_"),cpt,k1);
6328: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability to be alive\" \n\
1.220 brouard 6329: set ter svg size 640, 480\n \
6330: unset log y\n \
1.201 brouard 6331: plot [%.f:%.f] ", ageminpar, agemaxpar);
1.211 brouard 6332: k=3;
1.201 brouard 6333: for (i=1; i<= nlstate ; i ++){
1.223 brouard 6334: if(i==1){
6335: fprintf(ficgp,"\"%s\"",subdirf2(fileresu,"PIJ_"));
6336: }else{
6337: fprintf(ficgp,", '' ");
6338: }
6339: l=(nlstate+ndeath)*(i-1)+1;
6340: fprintf(ficgp," u ($1==%d ? ($3):1/0):($%d/($%d",k1,k+l+(cpt-1),k+l);
6341: for (j=2; j<= nlstate+ndeath ; j ++)
6342: fprintf(ficgp,"+$%d",k+l+j-1);
6343: fprintf(ficgp,")) t \"l(%d,%d)\" w l",i,cpt);
1.201 brouard 6344: } /* nlstate */
6345: fprintf(ficgp,"\nset out\n");
6346: } /* end cpt state*/
6347: } /* end covariate */
1.220 brouard 6348:
6349: /* 5eme */
1.201 brouard 6350: /* Survival functions (period) from state i in state j by final state j */
1.202 brouard 6351: for (k1=1; k1<= m ; k1 ++) { /* For each covariate if any */
1.201 brouard 6352: for (cpt=1; cpt<=nlstate ; cpt ++) { /* For each inital state */
1.223 brouard 6353:
1.201 brouard 6354: fprintf(ficgp,"\n#\n#\n# Survival functions in state j and all livestates from state i by final state j: 'lij' files, cov=%d state=%d",k1, cpt);
1.225 brouard 6355: for (k=1; k<=cptcoveff; k++){ /* For each covariate and each value */
1.227 brouard 6356: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate number corresponding to k1 combination */
6357: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
6358: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
6359: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
6360: vlv= nbcode[Tvaraff[k]][lv];
6361: fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv);
1.211 brouard 6362: }
6363: fprintf(ficgp,"\n#\n");
1.223 brouard 6364: if(invalidvarcomb[k1]){
1.227 brouard 6365: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
6366: continue;
1.223 brouard 6367: }
1.227 brouard 6368:
1.201 brouard 6369: fprintf(ficgp,"\nset out \"%s_%d-%d.svg\" \n",subdirf2(optionfilefiname,"LIJT_"),cpt,k1);
6370: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability to be alive\" \n\
1.227 brouard 6371: set ter svg size 640, 480\n \
6372: unset log y\n \
1.201 brouard 6373: plot [%.f:%.f] ", ageminpar, agemaxpar);
1.211 brouard 6374: k=3;
1.201 brouard 6375: for (j=1; j<= nlstate ; j ++){ /* Lived in state j */
1.227 brouard 6376: if(j==1)
6377: fprintf(ficgp,"\"%s\"",subdirf2(fileresu,"PIJ_"));
6378: else
6379: fprintf(ficgp,", '' ");
6380: l=(nlstate+ndeath)*(cpt-1) +j;
6381: fprintf(ficgp," u (($1==%d && (floor($2)%%5 == 0)) ? ($3):1/0):($%d",k1,k+l);
6382: /* for (i=2; i<= nlstate+ndeath ; i ++) */
6383: /* fprintf(ficgp,"+$%d",k+l+i-1); */
6384: fprintf(ficgp,") t \"l(%d,%d)\" w l",cpt,j);
1.201 brouard 6385: } /* nlstate */
6386: fprintf(ficgp,", '' ");
6387: fprintf(ficgp," u (($1==%d && (floor($2)%%5 == 0)) ? ($3):1/0):(",k1);
6388: for (j=1; j<= nlstate ; j ++){ /* Lived in state j */
1.227 brouard 6389: l=(nlstate+ndeath)*(cpt-1) +j;
6390: if(j < nlstate)
6391: fprintf(ficgp,"$%d +",k+l);
6392: else
6393: fprintf(ficgp,"$%d) t\"l(%d,.)\" w l",k+l,cpt);
1.201 brouard 6394: }
6395: fprintf(ficgp,"\nset out\n");
6396: } /* end cpt state*/
6397: } /* end covariate */
1.227 brouard 6398:
1.220 brouard 6399: /* 6eme */
1.202 brouard 6400: /* CV preval stable (period) for each covariate */
1.211 brouard 6401: for (k1=1; k1<= m ; k1 ++) { /* For each covariate combination (1 to m=2**k), if any covariate is present */
1.153 brouard 6402: for (cpt=1; cpt<=nlstate ; cpt ++) { /* For each life state */
1.227 brouard 6403:
1.211 brouard 6404: fprintf(ficgp,"\n#\n#\n#CV preval stable (period): 'pij' files, covariatecombination#=%d state=%d",k1, cpt);
1.225 brouard 6405: for (k=1; k<=cptcoveff; k++){ /* For each covariate and each value */
1.227 brouard 6406: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate number corresponding to k1 combination */
6407: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
6408: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
6409: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
6410: vlv= nbcode[Tvaraff[k]][lv];
6411: fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv);
1.211 brouard 6412: }
6413: fprintf(ficgp,"\n#\n");
1.223 brouard 6414: if(invalidvarcomb[k1]){
1.227 brouard 6415: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
6416: continue;
1.223 brouard 6417: }
1.227 brouard 6418:
1.201 brouard 6419: fprintf(ficgp,"\nset out \"%s_%d-%d.svg\" \n",subdirf2(optionfilefiname,"P_"),cpt,k1);
1.126 brouard 6420: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability\" \n\
1.227 brouard 6421: set ter svg size 640, 480\n \
6422: unset log y\n \
1.153 brouard 6423: plot [%.f:%.f] ", ageminpar, agemaxpar);
1.211 brouard 6424: k=3; /* Offset */
1.153 brouard 6425: for (i=1; i<= nlstate ; i ++){
1.227 brouard 6426: if(i==1)
6427: fprintf(ficgp,"\"%s\"",subdirf2(fileresu,"PIJ_"));
6428: else
6429: fprintf(ficgp,", '' ");
6430: l=(nlstate+ndeath)*(i-1)+1;
6431: fprintf(ficgp," u ($1==%d ? ($3):1/0):($%d/($%d",k1,k+l+(cpt-1),k+l);
6432: for (j=2; j<= nlstate ; j ++)
6433: fprintf(ficgp,"+$%d",k+l+j-1);
6434: fprintf(ficgp,")) t \"prev(%d,%d)\" w l",i,cpt);
1.153 brouard 6435: } /* nlstate */
1.201 brouard 6436: fprintf(ficgp,"\nset out\n");
1.153 brouard 6437: } /* end cpt state*/
6438: } /* end covariate */
1.227 brouard 6439:
6440:
1.220 brouard 6441: /* 7eme */
1.218 brouard 6442: if(backcast == 1){
1.217 brouard 6443: /* CV back preval stable (period) for each covariate */
1.218 brouard 6444: for (k1=1; k1<= m ; k1 ++) { /* For each covariate combination (1 to m=2**k), if any covariate is present */
6445: for (cpt=1; cpt<=nlstate ; cpt ++) { /* For each life state */
1.227 brouard 6446: fprintf(ficgp,"\n#\n#\n#CV Back preval stable (period): 'pij' files, covariatecombination#=%d state=%d",k1, cpt);
6447: for (k=1; k<=cptcoveff; k++){ /* For each covariate and each value */
6448: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate number corresponding to k1 combination */
6449: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
6450: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
1.223 brouard 6451: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
1.227 brouard 6452: vlv= nbcode[Tvaraff[k]][lv];
6453: fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv);
6454: }
6455: fprintf(ficgp,"\n#\n");
6456: if(invalidvarcomb[k1]){
6457: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
6458: continue;
6459: }
6460:
6461: fprintf(ficgp,"\nset out \"%s_%d-%d.svg\" \n",subdirf2(optionfilefiname,"PB_"),cpt,k1);
6462: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability\" \n\
6463: set ter svg size 640, 480\n \
6464: unset log y\n \
1.218 brouard 6465: plot [%.f:%.f] ", ageminpar, agemaxpar);
1.227 brouard 6466: k=3; /* Offset */
6467: for (i=1; i<= nlstate ; i ++){
6468: if(i==1)
6469: fprintf(ficgp,"\"%s\"",subdirf2(fileresu,"PIJB_"));
6470: else
6471: fprintf(ficgp,", '' ");
6472: /* l=(nlstate+ndeath)*(i-1)+1; */
6473: l=(nlstate+ndeath)*(cpt-1)+1;
6474: /* fprintf(ficgp," u ($1==%d ? ($3):1/0):($%d/($%d",k1,k+l+(cpt-1),k+l); /\* a vérifier *\/ */
6475: /* fprintf(ficgp," u ($1==%d ? ($3):1/0):($%d/($%d",k1,k+l+(cpt-1),k+l+(cpt-1)+i-1); /\* a vérifier *\/ */
6476: fprintf(ficgp," u ($1==%d ? ($3):1/0):($%d",k1,k+l+(cpt-1)+i-1); /* a vérifier */
6477: /* for (j=2; j<= nlstate ; j ++) */
6478: /* fprintf(ficgp,"+$%d",k+l+j-1); */
6479: /* /\* fprintf(ficgp,"+$%d",k+l+j-1); *\/ */
6480: fprintf(ficgp,") t \"bprev(%d,%d)\" w l",i,cpt);
6481: } /* nlstate */
6482: fprintf(ficgp,"\nset out\n");
1.218 brouard 6483: } /* end cpt state*/
6484: } /* end covariate */
6485: } /* End if backcast */
6486:
1.223 brouard 6487: /* 8eme */
1.218 brouard 6488: if(prevfcast==1){
6489: /* Projection from cross-sectional to stable (period) for each covariate */
6490:
6491: for (k1=1; k1<= m ; k1 ++) { /* For each covariate combination (1 to m=2**k), if any covariate is present */
1.211 brouard 6492: for (cpt=1; cpt<=nlstate ; cpt ++) { /* For each life state */
1.227 brouard 6493: fprintf(ficgp,"\n#\n#\n#Projection of prevalence to stable (period): 'PROJ_' files, covariatecombination#=%d state=%d",k1, cpt);
6494: for (k=1; k<=cptcoveff; k++){ /* For each correspondig covariate value */
6495: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate value corresponding to k1 combination and kth covariate */
6496: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
6497: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
6498: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
6499: vlv= nbcode[Tvaraff[k]][lv];
6500: fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv);
6501: }
6502: fprintf(ficgp,"\n#\n");
6503: if(invalidvarcomb[k1]){
6504: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
6505: continue;
6506: }
6507:
6508: fprintf(ficgp,"# hpijx=probability over h years, hp.jx is weighted by observed prev\n ");
6509: fprintf(ficgp,"\nset out \"%s_%d-%d.svg\" \n",subdirf2(optionfilefiname,"PROJ_"),cpt,k1);
6510: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Prevalence\" \n\
6511: set ter svg size 640, 480\n \
6512: unset log y\n \
1.219 brouard 6513: plot [%.f:%.f] ", ageminpar, agemaxpar);
1.227 brouard 6514: for (i=1; i<= nlstate+1 ; i ++){ /* nlstate +1 p11 p21 p.1 */
6515: /*# V1 = 1 V2 = 0 yearproj age p11 p21 p.1 p12 p22 p.2 p13 p23 p.3*/
6516: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
6517: /*# yearproj age p11 p21 p.1 p12 p22 p.2 p13 p23 p.3*/
6518: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
6519: if(i==1){
6520: fprintf(ficgp,"\"%s\"",subdirf2(fileresu,"F_"));
6521: }else{
6522: fprintf(ficgp,",\\\n '' ");
6523: }
6524: if(cptcoveff ==0){ /* No covariate */
6525: ioffset=2; /* Age is in 2 */
6526: /*# yearproj age p11 p21 p31 p.1 p12 p22 p32 p.2 p13 p23 p33 p.3 p14 p24 p34 p.4*/
6527: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 */
6528: /*# V1 = 1 yearproj age p11 p21 p31 p.1 p12 p22 p32 p.2 p13 p23 p33 p.3 p14 p24 p34 p.4*/
6529: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 */
6530: fprintf(ficgp," u %d:(", ioffset);
6531: if(i==nlstate+1)
6532: fprintf(ficgp," $%d/(1.-$%d)) t 'pw.%d' with line ", \
6533: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset+1+(i-1)+(nlstate+1)*nlstate,cpt );
6534: else
6535: fprintf(ficgp," $%d/(1.-$%d)) t 'p%d%d' with line ", \
6536: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset+1+(i-1)+(nlstate+1)*nlstate,i,cpt );
6537: }else{ /* more than 2 covariates */
6538: if(cptcoveff ==1){
6539: ioffset=4; /* Age is in 4 */
6540: }else{
6541: ioffset=6; /* Age is in 6 */
6542: /*# V1 = 1 V2 = 0 yearproj age p11 p21 p.1 p12 p22 p.2 p13 p23 p.3*/
6543: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
6544: }
6545: fprintf(ficgp," u %d:(",ioffset);
6546: kl=0;
6547: strcpy(gplotcondition,"(");
6548: for (k=1; k<=cptcoveff; k++){ /* For each covariate writing the chain of conditions */
6549: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate value corresponding to combination k1 and covariate k */
6550: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
6551: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
6552: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
6553: vlv= nbcode[Tvaraff[k]][lv]; /* Value of the modality of Tvaraff[k] */
6554: kl++;
6555: sprintf(gplotcondition+strlen(gplotcondition),"$%d==%d && $%d==%d " ,kl,Tvaraff[k], kl+1, nbcode[Tvaraff[k]][lv]);
6556: kl++;
6557: if(k <cptcoveff && cptcoveff>1)
6558: sprintf(gplotcondition+strlen(gplotcondition)," && ");
6559: }
6560: strcpy(gplotcondition+strlen(gplotcondition),")");
6561: /* 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 *\/ */
6562: /*6+(cpt-1)*(nlstate+1)+1+(i-1)+(nlstate+1)*nlstate; 6+(1-1)*(2+1)+1+(1-1) +(2+1)*2=13 */
6563: /*6+1+(i-1)+(nlstate+1)*nlstate; 6+1+(1-1) +(2+1)*2=13 */
6564: /* '' 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*/
6565: if(i==nlstate+1){
6566: fprintf(ficgp,"%s ? $%d/(1.-$%d) : 1/0) t 'p.%d' with line ", gplotcondition, \
6567: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset+1+(i-1)+(nlstate+1)*nlstate,cpt );
6568: }else{
6569: fprintf(ficgp,"%s ? $%d/(1.-$%d) : 1/0) t 'p%d%d' with line ", gplotcondition, \
6570: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset +1+(i-1)+(nlstate+1)*nlstate,i,cpt );
6571: }
6572: } /* end if covariate */
6573: } /* nlstate */
6574: fprintf(ficgp,"\nset out\n");
1.223 brouard 6575: } /* end cpt state*/
6576: } /* end covariate */
6577: } /* End if prevfcast */
1.227 brouard 6578:
6579:
1.223 brouard 6580: /* proba elementaires */
6581: fprintf(ficgp,"\n##############\n#MLE estimated parameters\n#############\n");
1.126 brouard 6582: for(i=1,jk=1; i <=nlstate; i++){
1.187 brouard 6583: fprintf(ficgp,"# initial state %d\n",i);
1.126 brouard 6584: for(k=1; k <=(nlstate+ndeath); k++){
6585: if (k != i) {
1.227 brouard 6586: fprintf(ficgp,"# current state %d\n",k);
6587: for(j=1; j <=ncovmodel; j++){
6588: fprintf(ficgp,"p%d=%f; ",jk,p[jk]);
6589: jk++;
6590: }
6591: fprintf(ficgp,"\n");
1.126 brouard 6592: }
6593: }
1.223 brouard 6594: }
1.187 brouard 6595: fprintf(ficgp,"##############\n#\n");
1.227 brouard 6596:
1.145 brouard 6597: /*goto avoid;*/
1.200 brouard 6598: fprintf(ficgp,"\n##############\n#Graphics of probabilities or incidences\n#############\n");
1.187 brouard 6599: fprintf(ficgp,"# logi(p12/p11)=a12+b12*age+c12age*age+d12*V1+e12*V1*age\n");
6600: fprintf(ficgp,"# logi(p12/p11)=p1 +p2*age +p3*age*age+ p4*V1+ p5*V1*age\n");
6601: fprintf(ficgp,"# logi(p13/p11)=a13+b13*age+c13age*age+d13*V1+e13*V1*age\n");
6602: fprintf(ficgp,"# logi(p13/p11)=p6 +p7*age +p8*age*age+ p9*V1+ p10*V1*age\n");
6603: fprintf(ficgp,"# p12+p13+p14+p11=1=p11(1+exp(a12+b12*age+c12age*age+d12*V1+e12*V1*age)\n");
6604: fprintf(ficgp,"# +exp(a13+b13*age+c13age*age+d13*V1+e13*V1*age)+...)\n");
6605: fprintf(ficgp,"# p11=1/(1+exp(a12+b12*age+c12age*age+d12*V1+e12*V1*age)\n");
6606: fprintf(ficgp,"# +exp(a13+b13*age+c13age*age+d13*V1+e13*V1*age)+...)\n");
6607: fprintf(ficgp,"# p12=exp(a12+b12*age+c12age*age+d12*V1+e12*V1*age)/\n");
6608: fprintf(ficgp,"# (1+exp(a12+b12*age+c12age*age+d12*V1+e12*V1*age)\n");
6609: fprintf(ficgp,"# +exp(a13+b13*age+c13age*age+d13*V1+e13*V1*age))\n");
6610: fprintf(ficgp,"# +exp(a14+b14*age+c14age*age+d14*V1+e14*V1*age)+...)\n");
6611: fprintf(ficgp,"#\n");
1.223 brouard 6612: for(ng=1; ng<=3;ng++){ /* Number of graphics: first is logit, 2nd is probabilities, third is incidences per year*/
6613: fprintf(ficgp,"# ng=%d\n",ng);
1.225 brouard 6614: fprintf(ficgp,"# jk=1 to 2^%d=%d\n",cptcoveff,m);
1.223 brouard 6615: for(jk=1; jk <=m; jk++) {
6616: fprintf(ficgp,"# jk=%d\n",jk);
6617: fprintf(ficgp,"\nset out \"%s_%d-%d.svg\" ",subdirf2(optionfilefiname,"PE_"),jk,ng);
6618: fprintf(ficgp,"\nset ter svg size 640, 480 ");
6619: if (ng==1){
6620: fprintf(ficgp,"\nset ylabel \"Value of the logit of the model\"\n"); /* exp(a12+b12*x) could be nice */
6621: fprintf(ficgp,"\nunset log y");
6622: }else if (ng==2){
6623: fprintf(ficgp,"\nset ylabel \"Probability\"\n");
6624: fprintf(ficgp,"\nset log y");
6625: }else if (ng==3){
6626: fprintf(ficgp,"\nset ylabel \"Quasi-incidence per year\"\n");
6627: fprintf(ficgp,"\nset log y");
6628: }else
6629: fprintf(ficgp,"\nunset title ");
6630: fprintf(ficgp,"\nplot [%.f:%.f] ",ageminpar,agemaxpar);
6631: i=1;
6632: for(k2=1; k2<=nlstate; k2++) {
6633: k3=i;
6634: for(k=1; k<=(nlstate+ndeath); k++) {
6635: if (k != k2){
6636: switch( ng) {
6637: case 1:
6638: if(nagesqr==0)
6639: fprintf(ficgp," p%d+p%d*x",i,i+1);
6640: else /* nagesqr =1 */
6641: fprintf(ficgp," p%d+p%d*x+p%d*x*x",i,i+1,i+1+nagesqr);
6642: break;
6643: case 2: /* ng=2 */
6644: if(nagesqr==0)
6645: fprintf(ficgp," exp(p%d+p%d*x",i,i+1);
6646: else /* nagesqr =1 */
6647: fprintf(ficgp," exp(p%d+p%d*x+p%d*x*x",i,i+1,i+1+nagesqr);
6648: break;
6649: case 3:
6650: if(nagesqr==0)
6651: fprintf(ficgp," %f*exp(p%d+p%d*x",YEARM/stepm,i,i+1);
6652: else /* nagesqr =1 */
6653: fprintf(ficgp," %f*exp(p%d+p%d*x+p%d*x*x",YEARM/stepm,i,i+1,i+1+nagesqr);
6654: break;
6655: }
6656: ij=1;/* To be checked else nbcode[0][0] wrong */
6657: for(j=3; j <=ncovmodel-nagesqr; j++) {
6658: /* printf("Tage[%d]=%d, j=%d\n", ij, Tage[ij], j); */
6659: if(ij <=cptcovage) { /* Bug valgrind */
6660: if((j-2)==Tage[ij]) { /* Bug valgrind */
6661: fprintf(ficgp,"+p%d*%d*x",i+j+nagesqr-1,nbcode[Tvar[j-2]][codtabm(jk,j-2)]);
6662: /* fprintf(ficgp,"+p%d*%d*x",i+j+nagesqr-1,nbcode[Tvar[j-2]][codtabm(jk,Tvar[j-2])]); */
6663: ij++;
6664: }
6665: }
6666: else
1.227 brouard 6667: fprintf(ficgp,"+p%d*%d",i+j+nagesqr-1,nbcode[Tvar[j-2]][codtabm(jk,j-2)]); /* Valgrind bug nbcode */
1.223 brouard 6668: }
6669: }else{
6670: i=i-ncovmodel;
6671: if(ng !=1 ) /* For logit formula of log p11 is more difficult to get */
6672: fprintf(ficgp," (1.");
6673: }
1.227 brouard 6674:
1.223 brouard 6675: if(ng != 1){
6676: fprintf(ficgp,")/(1");
1.227 brouard 6677:
1.223 brouard 6678: for(k1=1; k1 <=nlstate; k1++){
6679: if(nagesqr==0)
6680: fprintf(ficgp,"+exp(p%d+p%d*x",k3+(k1-1)*ncovmodel,k3+(k1-1)*ncovmodel+1);
6681: else /* nagesqr =1 */
6682: fprintf(ficgp,"+exp(p%d+p%d*x+p%d*x*x",k3+(k1-1)*ncovmodel,k3+(k1-1)*ncovmodel+1,k3+(k1-1)*ncovmodel+1+nagesqr);
1.217 brouard 6683:
1.223 brouard 6684: ij=1;
6685: for(j=3; j <=ncovmodel-nagesqr; j++){
6686: if(ij <=cptcovage) { /* Bug valgrind */
6687: if((j-2)==Tage[ij]) { /* Bug valgrind */
6688: fprintf(ficgp,"+p%d*%d*x",k3+(k1-1)*ncovmodel+1+j-2+nagesqr,nbcode[Tvar[j-2]][codtabm(jk,j-2)]);
6689: /* fprintf(ficgp,"+p%d*%d*x",k3+(k1-1)*ncovmodel+1+j-2+nagesqr,nbcode[Tvar[j-2]][codtabm(jk,Tvar[j-2])]); */
6690: ij++;
6691: }
6692: }
6693: else
1.225 brouard 6694: fprintf(ficgp,"+p%d*%d",k3+(k1-1)*ncovmodel+1+j-2+nagesqr,nbcode[Tvar[j-2]][codtabm(jk,j-2)]);/* Valgrind bug nbcode */
1.223 brouard 6695: }
6696: fprintf(ficgp,")");
6697: }
6698: fprintf(ficgp,")");
6699: if(ng ==2)
6700: fprintf(ficgp," t \"p%d%d\" ", k2,k);
6701: else /* ng= 3 */
6702: fprintf(ficgp," t \"i%d%d\" ", k2,k);
6703: }else{ /* end ng <> 1 */
6704: if( k !=k2) /* logit p11 is hard to draw */
6705: fprintf(ficgp," t \"logit(p%d%d)\" ", k2,k);
6706: }
6707: if ((k+k2)!= (nlstate*2+ndeath) && ng != 1)
6708: fprintf(ficgp,",");
6709: if (ng == 1 && k!=k2 && (k+k2)!= (nlstate*2+ndeath))
6710: fprintf(ficgp,",");
6711: i=i+ncovmodel;
6712: } /* end k */
6713: } /* end k2 */
6714: fprintf(ficgp,"\n set out\n");
6715: } /* end jk */
6716: } /* end ng */
6717: /* avoid: */
6718: fflush(ficgp);
1.126 brouard 6719: } /* end gnuplot */
6720:
6721:
6722: /*************** Moving average **************/
1.219 brouard 6723: /* int movingaverage(double ***probs, double bage, double fage, double ***mobaverage, int mobilav, double bageout, double fageout){ */
1.222 brouard 6724: int movingaverage(double ***probs, double bage, double fage, double ***mobaverage, int mobilav){
1.218 brouard 6725:
1.222 brouard 6726: int i, cpt, cptcod;
6727: int modcovmax =1;
6728: int mobilavrange, mob;
6729: int iage=0;
6730:
6731: double sum=0.;
6732: double age;
6733: double *sumnewp, *sumnewm;
6734: double *agemingood, *agemaxgood; /* Currently identical for all covariates */
6735:
6736:
1.225 brouard 6737: /* modcovmax=2*cptcoveff;/\* Max number of modalities. We suppose */
1.222 brouard 6738: /* a covariate has 2 modalities, should be equal to ncovcombmax *\/ */
6739:
6740: sumnewp = vector(1,ncovcombmax);
6741: sumnewm = vector(1,ncovcombmax);
6742: agemingood = vector(1,ncovcombmax);
6743: agemaxgood = vector(1,ncovcombmax);
6744:
6745: for (cptcod=1;cptcod<=ncovcombmax;cptcod++){
6746: sumnewm[cptcod]=0.;
6747: sumnewp[cptcod]=0.;
6748: agemingood[cptcod]=0;
6749: agemaxgood[cptcod]=0;
6750: }
6751: if (cptcovn<1) ncovcombmax=1; /* At least 1 pass */
6752:
6753: if(mobilav==1||mobilav ==3 ||mobilav==5 ||mobilav== 7){
6754: if(mobilav==1) mobilavrange=5; /* default */
6755: else mobilavrange=mobilav;
6756: for (age=bage; age<=fage; age++)
6757: for (i=1; i<=nlstate;i++)
6758: for (cptcod=1;cptcod<=ncovcombmax;cptcod++)
6759: mobaverage[(int)age][i][cptcod]=probs[(int)age][i][cptcod];
6760: /* We keep the original values on the extreme ages bage, fage and for
6761: fage+1 and bage-1 we use a 3 terms moving average; for fage+2 bage+2
6762: we use a 5 terms etc. until the borders are no more concerned.
6763: */
6764: for (mob=3;mob <=mobilavrange;mob=mob+2){
6765: for (age=bage+(mob-1)/2; age<=fage-(mob-1)/2; age++){
6766: for (i=1; i<=nlstate;i++){
6767: for (cptcod=1;cptcod<=ncovcombmax;cptcod++){
6768: mobaverage[(int)age][i][cptcod] =probs[(int)age][i][cptcod];
6769: for (cpt=1;cpt<=(mob-1)/2;cpt++){
6770: mobaverage[(int)age][i][cptcod] +=probs[(int)age-cpt][i][cptcod];
6771: mobaverage[(int)age][i][cptcod] +=probs[(int)age+cpt][i][cptcod];
6772: }
6773: mobaverage[(int)age][i][cptcod]=mobaverage[(int)age][i][cptcod]/mob;
6774: }
6775: }
6776: }/* end age */
6777: }/* end mob */
6778: }else
6779: return -1;
6780: for (cptcod=1;cptcod<=ncovcombmax;cptcod++){
6781: /* for (age=bage+(mob-1)/2; age<=fage-(mob-1)/2; age++){ */
6782: if(invalidvarcomb[cptcod]){
6783: printf("\nCombination (%d) ignored because no cases \n",cptcod);
6784: continue;
6785: }
1.219 brouard 6786:
1.222 brouard 6787: agemingood[cptcod]=fage-(mob-1)/2;
6788: for (age=fage-(mob-1)/2; age>=bage; age--){/* From oldest to youngest, finding the youngest wrong */
6789: sumnewm[cptcod]=0.;
6790: for (i=1; i<=nlstate;i++){
6791: sumnewm[cptcod]+=mobaverage[(int)age][i][cptcod];
6792: }
6793: if(fabs(sumnewm[cptcod] - 1.) <= 1.e-3) { /* good */
6794: agemingood[cptcod]=age;
6795: }else{ /* bad */
6796: for (i=1; i<=nlstate;i++){
6797: mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemingood[cptcod]][i][cptcod];
6798: } /* i */
6799: } /* end bad */
6800: }/* age */
6801: sum=0.;
6802: for (i=1; i<=nlstate;i++){
6803: sum+=mobaverage[(int)agemingood[cptcod]][i][cptcod];
6804: }
6805: if(fabs(sum - 1.) > 1.e-3) { /* bad */
6806: printf("For this combination of covariate cptcod=%d, we can't get a smoothed prevalence which sums to one at any descending age!\n",cptcod);
6807: /* for (i=1; i<=nlstate;i++){ */
6808: /* mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemingood[cptcod]][i][cptcod]; */
6809: /* } /\* i *\/ */
6810: } /* end bad */
6811: /* else{ /\* We found some ages summing to one, we will smooth the oldest *\/ */
6812: /* From youngest, finding the oldest wrong */
6813: agemaxgood[cptcod]=bage+(mob-1)/2;
6814: for (age=bage+(mob-1)/2; age<=fage; age++){
6815: sumnewm[cptcod]=0.;
6816: for (i=1; i<=nlstate;i++){
6817: sumnewm[cptcod]+=mobaverage[(int)age][i][cptcod];
6818: }
6819: if(fabs(sumnewm[cptcod] - 1.) <= 1.e-3) { /* good */
6820: agemaxgood[cptcod]=age;
6821: }else{ /* bad */
6822: for (i=1; i<=nlstate;i++){
6823: mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemaxgood[cptcod]][i][cptcod];
6824: } /* i */
6825: } /* end bad */
6826: }/* age */
6827: sum=0.;
6828: for (i=1; i<=nlstate;i++){
6829: sum+=mobaverage[(int)agemaxgood[cptcod]][i][cptcod];
6830: }
6831: if(fabs(sum - 1.) > 1.e-3) { /* bad */
6832: printf("For this combination of covariate cptcod=%d, we can't get a smoothed prevalence which sums to one at any ascending age!\n",cptcod);
6833: /* for (i=1; i<=nlstate;i++){ */
6834: /* mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemingood[cptcod]][i][cptcod]; */
6835: /* } /\* i *\/ */
6836: } /* end bad */
6837:
6838: for (age=bage; age<=fage; age++){
6839: printf("%d %d ", cptcod, (int)age);
6840: sumnewp[cptcod]=0.;
6841: sumnewm[cptcod]=0.;
6842: for (i=1; i<=nlstate;i++){
6843: sumnewp[cptcod]+=probs[(int)age][i][cptcod];
6844: sumnewm[cptcod]+=mobaverage[(int)age][i][cptcod];
6845: /* printf("%.4f %.4f ",probs[(int)age][i][cptcod], mobaverage[(int)age][i][cptcod]); */
6846: }
6847: /* printf("%.4f %.4f \n",sumnewp[cptcod], sumnewm[cptcod]); */
6848: }
6849: /* printf("\n"); */
6850: /* } */
6851: /* brutal averaging */
6852: for (i=1; i<=nlstate;i++){
6853: for (age=1; age<=bage; age++){
6854: mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemingood[cptcod]][i][cptcod];
6855: /* printf("age=%d i=%d cptcod=%d mobaverage=%.4f \n",(int)age,i, cptcod, mobaverage[(int)age][i][cptcod]); */
6856: }
6857: for (age=fage; age<=AGESUP; age++){
6858: mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemaxgood[cptcod]][i][cptcod];
6859: /* printf("age=%d i=%d cptcod=%d mobaverage=%.4f \n",(int)age,i, cptcod, mobaverage[(int)age][i][cptcod]); */
6860: }
6861: } /* end i status */
6862: for (i=nlstate+1; i<=nlstate+ndeath;i++){
6863: for (age=1; age<=AGESUP; age++){
6864: /*printf("i=%d, age=%d, cptcod=%d\n",i, (int)age, cptcod);*/
6865: mobaverage[(int)age][i][cptcod]=0.;
6866: }
6867: }
6868: }/* end cptcod */
6869: free_vector(sumnewm,1, ncovcombmax);
6870: free_vector(sumnewp,1, ncovcombmax);
6871: free_vector(agemaxgood,1, ncovcombmax);
6872: free_vector(agemingood,1, ncovcombmax);
6873: return 0;
6874: }/* End movingaverage */
1.218 brouard 6875:
1.126 brouard 6876:
6877: /************** Forecasting ******************/
1.225 brouard 6878: void prevforecast(char fileres[], double anproj1, double mproj1, double jproj1, double ageminpar, double agemax, double dateprev1, double dateprev2, int mobilav, double bage, double fage, int firstpass, int lastpass, double anproj2, double p[], int cptcoveff){
1.126 brouard 6879: /* proj1, year, month, day of starting projection
6880: agemin, agemax range of age
6881: dateprev1 dateprev2 range of dates during which prevalence is computed
6882: anproj2 year of en of projection (same day and month as proj1).
6883: */
1.164 brouard 6884: int yearp, stepsize, hstepm, nhstepm, j, k, cptcod, i, h, i1;
1.126 brouard 6885: double agec; /* generic age */
6886: double agelim, ppij, yp,yp1,yp2,jprojmean,mprojmean,anprojmean;
6887: double *popeffectif,*popcount;
6888: double ***p3mat;
1.218 brouard 6889: /* double ***mobaverage; */
1.126 brouard 6890: char fileresf[FILENAMELENGTH];
6891:
6892: agelim=AGESUP;
1.211 brouard 6893: /* Compute observed prevalence between dateprev1 and dateprev2 by counting the number of people
6894: in each health status at the date of interview (if between dateprev1 and dateprev2).
6895: We still use firstpass and lastpass as another selection.
6896: */
1.214 brouard 6897: /* freqsummary(fileres, agemin, agemax, s, agev, nlstate, imx,Tvaraff,nbcode, ncodemax,mint,anint,strstart,\ */
6898: /* firstpass, lastpass, stepm, weightopt, model); */
1.126 brouard 6899:
1.201 brouard 6900: strcpy(fileresf,"F_");
6901: strcat(fileresf,fileresu);
1.126 brouard 6902: if((ficresf=fopen(fileresf,"w"))==NULL) {
6903: printf("Problem with forecast resultfile: %s\n", fileresf);
6904: fprintf(ficlog,"Problem with forecast resultfile: %s\n", fileresf);
6905: }
1.215 brouard 6906: printf("Computing forecasting: result on file '%s', please wait... \n", fileresf);
6907: fprintf(ficlog,"Computing forecasting: result on file '%s', please wait... \n", fileresf);
1.126 brouard 6908:
1.225 brouard 6909: if (cptcoveff==0) ncodemax[cptcoveff]=1;
1.126 brouard 6910:
6911:
6912: stepsize=(int) (stepm+YEARM-1)/YEARM;
6913: if (stepm<=12) stepsize=1;
6914: if(estepm < stepm){
6915: printf ("Problem %d lower than %d\n",estepm, stepm);
6916: }
6917: else hstepm=estepm;
6918:
6919: hstepm=hstepm/stepm;
6920: yp1=modf(dateintmean,&yp);/* extracts integral of datemean in yp and
6921: fractional in yp1 */
6922: anprojmean=yp;
6923: yp2=modf((yp1*12),&yp);
6924: mprojmean=yp;
6925: yp1=modf((yp2*30.5),&yp);
6926: jprojmean=yp;
6927: if(jprojmean==0) jprojmean=1;
6928: if(mprojmean==0) jprojmean=1;
6929:
1.227 brouard 6930: i1=pow(2,cptcoveff);
1.126 brouard 6931: if (cptcovn < 1){i1=1;}
6932:
6933: fprintf(ficresf,"# Mean day of interviews %.lf/%.lf/%.lf (%.2f) between %.2f and %.2f \n",jprojmean,mprojmean,anprojmean,dateintmean,dateprev1,dateprev2);
6934:
6935: fprintf(ficresf,"#****** Routine prevforecast **\n");
1.227 brouard 6936:
1.126 brouard 6937: /* if (h==(int)(YEARM*yearp)){ */
1.227 brouard 6938: for(k=1;k<=i1;k++){
6939: if(invalidvarcomb[k]){
6940: printf("\nCombination (%d) projection ignored because no cases \n",k);
6941: continue;
6942: }
6943: fprintf(ficresf,"\n#****** hpijx=probability over h years, hp.jx is weighted by observed prev \n#");
6944: for(j=1;j<=cptcoveff;j++) {
6945: fprintf(ficresf," V%d (=) %d",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
6946: }
6947: fprintf(ficresf," yearproj age");
6948: for(j=1; j<=nlstate+ndeath;j++){
6949: for(i=1; i<=nlstate;i++)
6950: fprintf(ficresf," p%d%d",i,j);
6951: fprintf(ficresf," wp.%d",j);
6952: }
6953: for (yearp=0; yearp<=(anproj2-anproj1);yearp +=stepsize) {
6954: fprintf(ficresf,"\n");
6955: fprintf(ficresf,"\n# Forecasting at date %.lf/%.lf/%.lf ",jproj1,mproj1,anproj1+yearp);
6956: for (agec=fage; agec>=(ageminpar-1); agec--){
6957: nhstepm=(int) rint((agelim-agec)*YEARM/stepm);
6958: nhstepm = nhstepm/hstepm;
6959: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
6960: oldm=oldms;savm=savms;
6961: hpxij(p3mat,nhstepm,agec,hstepm,p,nlstate,stepm,oldm,savm, k);
6962:
6963: for (h=0; h<=nhstepm; h++){
6964: if (h*hstepm/YEARM*stepm ==yearp) {
6965: fprintf(ficresf,"\n");
6966: for(j=1;j<=cptcoveff;j++)
6967: fprintf(ficresf,"%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
6968: fprintf(ficresf,"%.f %.f ",anproj1+yearp,agec+h*hstepm/YEARM*stepm);
6969: }
6970: for(j=1; j<=nlstate+ndeath;j++) {
6971: ppij=0.;
6972: for(i=1; i<=nlstate;i++) {
6973: if (mobilav==1)
6974: ppij=ppij+p3mat[i][j][h]*mobaverage[(int)agec][i][k];
6975: else {
6976: ppij=ppij+p3mat[i][j][h]*probs[(int)(agec)][i][k];
6977: }
6978: if (h*hstepm/YEARM*stepm== yearp) {
6979: fprintf(ficresf," %.3f", p3mat[i][j][h]);
6980: }
6981: } /* end i */
6982: if (h*hstepm/YEARM*stepm==yearp) {
6983: fprintf(ficresf," %.3f", ppij);
6984: }
6985: }/* end j */
6986: } /* end h */
6987: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
6988: } /* end agec */
6989: } /* end yearp */
6990: } /* end k */
1.219 brouard 6991:
1.126 brouard 6992: fclose(ficresf);
1.215 brouard 6993: printf("End of Computing forecasting \n");
6994: fprintf(ficlog,"End of Computing forecasting\n");
6995:
1.126 brouard 6996: }
6997:
1.218 brouard 6998: /* /\************** Back Forecasting ******************\/ */
1.225 brouard 6999: /* void prevbackforecast(char fileres[], double anback1, double mback1, double jback1, double ageminpar, double agemax, double dateprev1, double dateprev2, int mobilav, double bage, double fage, int firstpass, int lastpass, double anback2, double p[], int cptcoveff){ */
1.218 brouard 7000: /* /\* back1, year, month, day of starting backection */
7001: /* agemin, agemax range of age */
7002: /* dateprev1 dateprev2 range of dates during which prevalence is computed */
7003: /* anback2 year of en of backection (same day and month as back1). */
7004: /* *\/ */
7005: /* int yearp, stepsize, hstepm, nhstepm, j, k, cptcod, i, h, i1; */
7006: /* double agec; /\* generic age *\/ */
7007: /* double agelim, ppij, yp,yp1,yp2,jprojmean,mprojmean,anprojmean; */
7008: /* double *popeffectif,*popcount; */
7009: /* double ***p3mat; */
7010: /* /\* double ***mobaverage; *\/ */
7011: /* char fileresfb[FILENAMELENGTH]; */
7012:
7013: /* agelim=AGESUP; */
7014: /* /\* Compute observed prevalence between dateprev1 and dateprev2 by counting the number of people */
7015: /* in each health status at the date of interview (if between dateprev1 and dateprev2). */
7016: /* We still use firstpass and lastpass as another selection. */
7017: /* *\/ */
7018: /* /\* freqsummary(fileres, agemin, agemax, s, agev, nlstate, imx,Tvaraff,nbcode, ncodemax,mint,anint,strstart,\ *\/ */
7019: /* /\* firstpass, lastpass, stepm, weightopt, model); *\/ */
7020: /* prevalence(probs, ageminpar, agemax, s, agev, nlstate, imx, Tvar, nbcode, ncodemax, mint, anint, dateprev1, dateprev2, firstpass, lastpass); */
7021:
7022: /* strcpy(fileresfb,"FB_"); */
7023: /* strcat(fileresfb,fileresu); */
7024: /* if((ficresfb=fopen(fileresfb,"w"))==NULL) { */
7025: /* printf("Problem with back forecast resultfile: %s\n", fileresfb); */
7026: /* fprintf(ficlog,"Problem with back forecast resultfile: %s\n", fileresfb); */
7027: /* } */
7028: /* printf("Computing back forecasting: result on file '%s', please wait... \n", fileresfb); */
7029: /* fprintf(ficlog,"Computing back forecasting: result on file '%s', please wait... \n", fileresfb); */
7030:
1.225 brouard 7031: /* if (cptcoveff==0) ncodemax[cptcoveff]=1; */
1.218 brouard 7032:
7033: /* /\* if (mobilav!=0) { *\/ */
7034: /* /\* mobaverage= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX); *\/ */
7035: /* /\* if (movingaverage(probs, ageminpar, fage, mobaverage,mobilav)!=0){ *\/ */
7036: /* /\* fprintf(ficlog," Error in movingaverage mobilav=%d\n",mobilav); *\/ */
7037: /* /\* printf(" Error in movingaverage mobilav=%d\n",mobilav); *\/ */
7038: /* /\* } *\/ */
7039: /* /\* } *\/ */
7040:
7041: /* stepsize=(int) (stepm+YEARM-1)/YEARM; */
7042: /* if (stepm<=12) stepsize=1; */
7043: /* if(estepm < stepm){ */
7044: /* printf ("Problem %d lower than %d\n",estepm, stepm); */
7045: /* } */
7046: /* else hstepm=estepm; */
7047:
7048: /* hstepm=hstepm/stepm; */
7049: /* yp1=modf(dateintmean,&yp);/\* extracts integral of datemean in yp and */
7050: /* fractional in yp1 *\/ */
7051: /* anprojmean=yp; */
7052: /* yp2=modf((yp1*12),&yp); */
7053: /* mprojmean=yp; */
7054: /* yp1=modf((yp2*30.5),&yp); */
7055: /* jprojmean=yp; */
7056: /* if(jprojmean==0) jprojmean=1; */
7057: /* if(mprojmean==0) jprojmean=1; */
7058:
1.225 brouard 7059: /* i1=cptcoveff; */
1.218 brouard 7060: /* if (cptcovn < 1){i1=1;} */
1.217 brouard 7061:
1.218 brouard 7062: /* fprintf(ficresfb,"# Mean day of interviews %.lf/%.lf/%.lf (%.2f) between %.2f and %.2f \n",jprojmean,mprojmean,anprojmean,dateintmean,dateprev1,dateprev2); */
1.217 brouard 7063:
1.218 brouard 7064: /* fprintf(ficresfb,"#****** Routine prevbackforecast **\n"); */
7065:
7066: /* /\* if (h==(int)(YEARM*yearp)){ *\/ */
7067: /* for(cptcov=1, k=0;cptcov<=i1;cptcov++){ */
1.225 brouard 7068: /* for(cptcod=1;cptcod<=ncodemax[cptcoveff];cptcod++){ */
1.218 brouard 7069: /* k=k+1; */
7070: /* fprintf(ficresfb,"\n#****** hbijx=probability over h years, hp.jx is weighted by observed prev \n#"); */
1.225 brouard 7071: /* for(j=1;j<=cptcoveff;j++) { */
1.218 brouard 7072: /* fprintf(ficresfb," V%d (=) %d",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]); */
7073: /* } */
7074: /* fprintf(ficresfb," yearbproj age"); */
7075: /* for(j=1; j<=nlstate+ndeath;j++){ */
7076: /* for(i=1; i<=nlstate;i++) */
7077: /* fprintf(ficresfb," p%d%d",i,j); */
7078: /* fprintf(ficresfb," p.%d",j); */
7079: /* } */
7080: /* for (yearp=0; yearp>=(anback2-anback1);yearp -=stepsize) { */
7081: /* /\* for (yearp=0; yearp<=(anproj2-anproj1);yearp +=stepsize) { *\/ */
7082: /* fprintf(ficresfb,"\n"); */
7083: /* fprintf(ficresfb,"\n# Back Forecasting at date %.lf/%.lf/%.lf ",jback1,mback1,anback1+yearp); */
7084: /* for (agec=fage; agec>=(ageminpar-1); agec--){ */
7085: /* nhstepm=(int) rint((agelim-agec)*YEARM/stepm); */
7086: /* nhstepm = nhstepm/hstepm; */
7087: /* p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); */
7088: /* oldm=oldms;savm=savms; */
7089: /* hbxij(p3mat,nhstepm,agec,hstepm,p,prevacurrent,nlstate,stepm,oldm,savm,oldm,savm, dnewm, doldm, dsavm, k); */
7090: /* for (h=0; h<=nhstepm; h++){ */
7091: /* if (h*hstepm/YEARM*stepm ==yearp) { */
7092: /* fprintf(ficresfb,"\n"); */
1.225 brouard 7093: /* for(j=1;j<=cptcoveff;j++) */
1.218 brouard 7094: /* fprintf(ficresfb,"%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]); */
7095: /* fprintf(ficresfb,"%.f %.f ",anback1+yearp,agec+h*hstepm/YEARM*stepm); */
7096: /* } */
7097: /* for(j=1; j<=nlstate+ndeath;j++) { */
7098: /* ppij=0.; */
7099: /* for(i=1; i<=nlstate;i++) { */
7100: /* if (mobilav==1) */
7101: /* ppij=ppij+p3mat[i][j][h]*mobaverage[(int)agec][i][cptcod]; */
7102: /* else { */
7103: /* ppij=ppij+p3mat[i][j][h]*probs[(int)(agec)][i][cptcod]; */
7104: /* } */
7105: /* if (h*hstepm/YEARM*stepm== yearp) { */
7106: /* fprintf(ficresfb," %.3f", p3mat[i][j][h]); */
7107: /* } */
7108: /* } /\* end i *\/ */
7109: /* if (h*hstepm/YEARM*stepm==yearp) { */
7110: /* fprintf(ficresfb," %.3f", ppij); */
7111: /* } */
7112: /* }/\* end j *\/ */
7113: /* } /\* end h *\/ */
7114: /* free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); */
7115: /* } /\* end agec *\/ */
7116: /* } /\* end yearp *\/ */
7117: /* } /\* end cptcod *\/ */
7118: /* } /\* end cptcov *\/ */
7119:
7120: /* /\* if (mobilav!=0) free_ma3x(mobaverage,1, AGESUP,1,NCOVMAX, 1,NCOVMAX); *\/ */
7121:
7122: /* fclose(ficresfb); */
7123: /* printf("End of Computing Back forecasting \n"); */
7124: /* fprintf(ficlog,"End of Computing Back forecasting\n"); */
1.217 brouard 7125:
1.218 brouard 7126: /* } */
1.217 brouard 7127:
1.126 brouard 7128: /************** Forecasting *****not tested NB*************/
1.227 brouard 7129: /* void populforecast(char fileres[], double anpyram,double mpyram,double jpyram,double ageminpar, double agemax,double dateprev1, double dateprev2s, int mobilav, double agedeb, double fage, int popforecast, char popfile[], double anpyram1,double p[], int i2){ */
1.126 brouard 7130:
1.227 brouard 7131: /* int cpt, stepsize, hstepm, nhstepm, j,k,c, cptcod, i,h; */
7132: /* int *popage; */
7133: /* double calagedatem, agelim, kk1, kk2; */
7134: /* double *popeffectif,*popcount; */
7135: /* double ***p3mat,***tabpop,***tabpopprev; */
7136: /* /\* double ***mobaverage; *\/ */
7137: /* char filerespop[FILENAMELENGTH]; */
1.126 brouard 7138:
1.227 brouard 7139: /* tabpop= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
7140: /* tabpopprev= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
7141: /* agelim=AGESUP; */
7142: /* calagedatem=(anpyram+mpyram/12.+jpyram/365.-dateintmean)*YEARM; */
1.126 brouard 7143:
1.227 brouard 7144: /* prevalence(probs, ageminpar, agemax, s, agev, nlstate, imx, Tvar, nbcode, ncodemax, mint, anint, dateprev1, dateprev2, firstpass, lastpass); */
1.126 brouard 7145:
7146:
1.227 brouard 7147: /* strcpy(filerespop,"POP_"); */
7148: /* strcat(filerespop,fileresu); */
7149: /* if((ficrespop=fopen(filerespop,"w"))==NULL) { */
7150: /* printf("Problem with forecast resultfile: %s\n", filerespop); */
7151: /* fprintf(ficlog,"Problem with forecast resultfile: %s\n", filerespop); */
7152: /* } */
7153: /* printf("Computing forecasting: result on file '%s' \n", filerespop); */
7154: /* fprintf(ficlog,"Computing forecasting: result on file '%s' \n", filerespop); */
1.126 brouard 7155:
1.227 brouard 7156: /* if (cptcoveff==0) ncodemax[cptcoveff]=1; */
1.126 brouard 7157:
1.227 brouard 7158: /* /\* if (mobilav!=0) { *\/ */
7159: /* /\* mobaverage= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX); *\/ */
7160: /* /\* if (movingaverage(probs, ageminpar, fage, mobaverage,mobilav)!=0){ *\/ */
7161: /* /\* fprintf(ficlog," Error in movingaverage mobilav=%d\n",mobilav); *\/ */
7162: /* /\* printf(" Error in movingaverage mobilav=%d\n",mobilav); *\/ */
7163: /* /\* } *\/ */
7164: /* /\* } *\/ */
1.126 brouard 7165:
1.227 brouard 7166: /* stepsize=(int) (stepm+YEARM-1)/YEARM; */
7167: /* if (stepm<=12) stepsize=1; */
1.126 brouard 7168:
1.227 brouard 7169: /* agelim=AGESUP; */
1.126 brouard 7170:
1.227 brouard 7171: /* hstepm=1; */
7172: /* hstepm=hstepm/stepm; */
1.218 brouard 7173:
1.227 brouard 7174: /* if (popforecast==1) { */
7175: /* if((ficpop=fopen(popfile,"r"))==NULL) { */
7176: /* printf("Problem with population file : %s\n",popfile);exit(0); */
7177: /* fprintf(ficlog,"Problem with population file : %s\n",popfile);exit(0); */
7178: /* } */
7179: /* popage=ivector(0,AGESUP); */
7180: /* popeffectif=vector(0,AGESUP); */
7181: /* popcount=vector(0,AGESUP); */
1.126 brouard 7182:
1.227 brouard 7183: /* i=1; */
7184: /* while ((c=fscanf(ficpop,"%d %lf\n",&popage[i],&popcount[i])) != EOF) i=i+1; */
1.218 brouard 7185:
1.227 brouard 7186: /* imx=i; */
7187: /* for (i=1; i<imx;i++) popeffectif[popage[i]]=popcount[i]; */
7188: /* } */
1.218 brouard 7189:
1.227 brouard 7190: /* for(cptcov=1,k=0;cptcov<=i2;cptcov++){ */
7191: /* for(cptcod=1;cptcod<=ncodemax[cptcoveff];cptcod++){ */
7192: /* k=k+1; */
7193: /* fprintf(ficrespop,"\n#******"); */
7194: /* for(j=1;j<=cptcoveff;j++) { */
7195: /* fprintf(ficrespop," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]); */
7196: /* } */
7197: /* fprintf(ficrespop,"******\n"); */
7198: /* fprintf(ficrespop,"# Age"); */
7199: /* for(j=1; j<=nlstate+ndeath;j++) fprintf(ficrespop," P.%d",j); */
7200: /* if (popforecast==1) fprintf(ficrespop," [Population]"); */
1.126 brouard 7201:
1.227 brouard 7202: /* for (cpt=0; cpt<=0;cpt++) { */
7203: /* fprintf(ficrespop,"\n\n# Forecasting at date %.lf/%.lf/%.lf ",jpyram,mpyram,anpyram+cpt); */
1.126 brouard 7204:
1.227 brouard 7205: /* for (agedeb=(fage-((int)calagedatem %12/12.)); agedeb>=(ageminpar-((int)calagedatem %12)/12.); agedeb--){ */
7206: /* nhstepm=(int) rint((agelim-agedeb)*YEARM/stepm); */
7207: /* nhstepm = nhstepm/hstepm; */
1.126 brouard 7208:
1.227 brouard 7209: /* p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); */
7210: /* oldm=oldms;savm=savms; */
7211: /* hpxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k); */
1.218 brouard 7212:
1.227 brouard 7213: /* for (h=0; h<=nhstepm; h++){ */
7214: /* if (h==(int) (calagedatem+YEARM*cpt)) { */
7215: /* fprintf(ficrespop,"\n %3.f ",agedeb+h*hstepm/YEARM*stepm); */
7216: /* } */
7217: /* for(j=1; j<=nlstate+ndeath;j++) { */
7218: /* kk1=0.;kk2=0; */
7219: /* for(i=1; i<=nlstate;i++) { */
7220: /* if (mobilav==1) */
7221: /* kk1=kk1+p3mat[i][j][h]*mobaverage[(int)agedeb+1][i][cptcod]; */
7222: /* else { */
7223: /* kk1=kk1+p3mat[i][j][h]*probs[(int)(agedeb+1)][i][cptcod]; */
7224: /* } */
7225: /* } */
7226: /* if (h==(int)(calagedatem+12*cpt)){ */
7227: /* tabpop[(int)(agedeb)][j][cptcod]=kk1; */
7228: /* /\*fprintf(ficrespop," %.3f", kk1); */
7229: /* if (popforecast==1) fprintf(ficrespop," [%.f]", kk1*popeffectif[(int)agedeb+1]);*\/ */
7230: /* } */
7231: /* } */
7232: /* for(i=1; i<=nlstate;i++){ */
7233: /* kk1=0.; */
7234: /* for(j=1; j<=nlstate;j++){ */
7235: /* kk1= kk1+tabpop[(int)(agedeb)][j][cptcod]; */
7236: /* } */
7237: /* tabpopprev[(int)(agedeb)][i][cptcod]=tabpop[(int)(agedeb)][i][cptcod]/kk1*popeffectif[(int)(agedeb+(calagedatem+12*cpt)*hstepm/YEARM*stepm-1)]; */
7238: /* } */
1.218 brouard 7239:
1.227 brouard 7240: /* if (h==(int)(calagedatem+12*cpt)) */
7241: /* for(j=1; j<=nlstate;j++) */
7242: /* fprintf(ficrespop," %15.2f",tabpopprev[(int)(agedeb+1)][j][cptcod]); */
7243: /* } */
7244: /* free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); */
7245: /* } */
7246: /* } */
1.218 brouard 7247:
1.227 brouard 7248: /* /\******\/ */
1.218 brouard 7249:
1.227 brouard 7250: /* for (cpt=1; cpt<=(anpyram1-anpyram);cpt++) { */
7251: /* fprintf(ficrespop,"\n\n# Forecasting at date %.lf/%.lf/%.lf ",jpyram,mpyram,anpyram+cpt); */
7252: /* for (agedeb=(fage-((int)calagedatem %12/12.)); agedeb>=(ageminpar-((int)calagedatem %12)/12.); agedeb--){ */
7253: /* nhstepm=(int) rint((agelim-agedeb)*YEARM/stepm); */
7254: /* nhstepm = nhstepm/hstepm; */
1.126 brouard 7255:
1.227 brouard 7256: /* p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); */
7257: /* oldm=oldms;savm=savms; */
7258: /* hpxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k); */
7259: /* for (h=0; h<=nhstepm; h++){ */
7260: /* if (h==(int) (calagedatem+YEARM*cpt)) { */
7261: /* fprintf(ficresf,"\n %3.f ",agedeb+h*hstepm/YEARM*stepm); */
7262: /* } */
7263: /* for(j=1; j<=nlstate+ndeath;j++) { */
7264: /* kk1=0.;kk2=0; */
7265: /* for(i=1; i<=nlstate;i++) { */
7266: /* kk1=kk1+p3mat[i][j][h]*tabpopprev[(int)agedeb+1][i][cptcod]; */
7267: /* } */
7268: /* if (h==(int)(calagedatem+12*cpt)) fprintf(ficresf," %15.2f", kk1); */
7269: /* } */
7270: /* } */
7271: /* free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); */
7272: /* } */
7273: /* } */
7274: /* } */
7275: /* } */
1.218 brouard 7276:
1.227 brouard 7277: /* /\* if (mobilav!=0) free_ma3x(mobaverage,1, AGESUP,1,NCOVMAX, 1,NCOVMAX); *\/ */
1.218 brouard 7278:
1.227 brouard 7279: /* if (popforecast==1) { */
7280: /* free_ivector(popage,0,AGESUP); */
7281: /* free_vector(popeffectif,0,AGESUP); */
7282: /* free_vector(popcount,0,AGESUP); */
7283: /* } */
7284: /* free_ma3x(tabpop,1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
7285: /* free_ma3x(tabpopprev,1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
7286: /* fclose(ficrespop); */
7287: /* } /\* End of popforecast *\/ */
1.218 brouard 7288:
1.126 brouard 7289: int fileappend(FILE *fichier, char *optionfich)
7290: {
7291: if((fichier=fopen(optionfich,"a"))==NULL) {
7292: printf("Problem with file: %s\n", optionfich);
7293: fprintf(ficlog,"Problem with file: %s\n", optionfich);
7294: return (0);
7295: }
7296: fflush(fichier);
7297: return (1);
7298: }
7299:
7300:
7301: /**************** function prwizard **********************/
7302: void prwizard(int ncovmodel, int nlstate, int ndeath, char model[], FILE *ficparo)
7303: {
7304:
7305: /* Wizard to print covariance matrix template */
7306:
1.164 brouard 7307: char ca[32], cb[32];
7308: int i,j, k, li, lj, lk, ll, jj, npar, itimes;
1.126 brouard 7309: int numlinepar;
7310:
7311: printf("# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n");
7312: fprintf(ficparo,"# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n");
7313: for(i=1; i <=nlstate; i++){
7314: jj=0;
7315: for(j=1; j <=nlstate+ndeath; j++){
7316: if(j==i) continue;
7317: jj++;
7318: /*ca[0]= k+'a'-1;ca[1]='\0';*/
7319: printf("%1d%1d",i,j);
7320: fprintf(ficparo,"%1d%1d",i,j);
7321: for(k=1; k<=ncovmodel;k++){
7322: /* printf(" %lf",param[i][j][k]); */
7323: /* fprintf(ficparo," %lf",param[i][j][k]); */
7324: printf(" 0.");
7325: fprintf(ficparo," 0.");
7326: }
7327: printf("\n");
7328: fprintf(ficparo,"\n");
7329: }
7330: }
7331: printf("# Scales (for hessian or gradient estimation)\n");
7332: fprintf(ficparo,"# Scales (for hessian or gradient estimation)\n");
7333: npar= (nlstate+ndeath-1)*nlstate*ncovmodel; /* Number of parameters*/
7334: for(i=1; i <=nlstate; i++){
7335: jj=0;
7336: for(j=1; j <=nlstate+ndeath; j++){
7337: if(j==i) continue;
7338: jj++;
7339: fprintf(ficparo,"%1d%1d",i,j);
7340: printf("%1d%1d",i,j);
7341: fflush(stdout);
7342: for(k=1; k<=ncovmodel;k++){
7343: /* printf(" %le",delti3[i][j][k]); */
7344: /* fprintf(ficparo," %le",delti3[i][j][k]); */
7345: printf(" 0.");
7346: fprintf(ficparo," 0.");
7347: }
7348: numlinepar++;
7349: printf("\n");
7350: fprintf(ficparo,"\n");
7351: }
7352: }
7353: printf("# Covariance matrix\n");
7354: /* # 121 Var(a12)\n\ */
7355: /* # 122 Cov(b12,a12) Var(b12)\n\ */
7356: /* # 131 Cov(a13,a12) Cov(a13,b12, Var(a13)\n\ */
7357: /* # 132 Cov(b13,a12) Cov(b13,b12, Cov(b13,a13) Var(b13)\n\ */
7358: /* # 212 Cov(a21,a12) Cov(a21,b12, Cov(a21,a13) Cov(a21,b13) Var(a21)\n\ */
7359: /* # 212 Cov(b21,a12) Cov(b21,b12, Cov(b21,a13) Cov(b21,b13) Cov(b21,a21) Var(b21)\n\ */
7360: /* # 232 Cov(a23,a12) Cov(a23,b12, Cov(a23,a13) Cov(a23,b13) Cov(a23,a21) Cov(a23,b21) Var(a23)\n\ */
7361: /* # 232 Cov(b23,a12) Cov(b23,b12) ... Var (b23)\n" */
7362: fflush(stdout);
7363: fprintf(ficparo,"# Covariance matrix\n");
7364: /* # 121 Var(a12)\n\ */
7365: /* # 122 Cov(b12,a12) Var(b12)\n\ */
7366: /* # ...\n\ */
7367: /* # 232 Cov(b23,a12) Cov(b23,b12) ... Var (b23)\n" */
7368:
7369: for(itimes=1;itimes<=2;itimes++){
7370: jj=0;
7371: for(i=1; i <=nlstate; i++){
7372: for(j=1; j <=nlstate+ndeath; j++){
7373: if(j==i) continue;
7374: for(k=1; k<=ncovmodel;k++){
7375: jj++;
7376: ca[0]= k+'a'-1;ca[1]='\0';
7377: if(itimes==1){
7378: printf("#%1d%1d%d",i,j,k);
7379: fprintf(ficparo,"#%1d%1d%d",i,j,k);
7380: }else{
7381: printf("%1d%1d%d",i,j,k);
7382: fprintf(ficparo,"%1d%1d%d",i,j,k);
7383: /* printf(" %.5le",matcov[i][j]); */
7384: }
7385: ll=0;
7386: for(li=1;li <=nlstate; li++){
7387: for(lj=1;lj <=nlstate+ndeath; lj++){
7388: if(lj==li) continue;
7389: for(lk=1;lk<=ncovmodel;lk++){
7390: ll++;
7391: if(ll<=jj){
7392: cb[0]= lk +'a'-1;cb[1]='\0';
7393: if(ll<jj){
7394: if(itimes==1){
7395: printf(" Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj);
7396: fprintf(ficparo," Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj);
7397: }else{
7398: printf(" 0.");
7399: fprintf(ficparo," 0.");
7400: }
7401: }else{
7402: if(itimes==1){
7403: printf(" Var(%s%1d%1d)",ca,i,j);
7404: fprintf(ficparo," Var(%s%1d%1d)",ca,i,j);
7405: }else{
7406: printf(" 0.");
7407: fprintf(ficparo," 0.");
7408: }
7409: }
7410: }
7411: } /* end lk */
7412: } /* end lj */
7413: } /* end li */
7414: printf("\n");
7415: fprintf(ficparo,"\n");
7416: numlinepar++;
7417: } /* end k*/
7418: } /*end j */
7419: } /* end i */
7420: } /* end itimes */
7421:
7422: } /* end of prwizard */
7423: /******************* Gompertz Likelihood ******************************/
7424: double gompertz(double x[])
7425: {
7426: double A,B,L=0.0,sump=0.,num=0.;
7427: int i,n=0; /* n is the size of the sample */
7428:
1.220 brouard 7429: for (i=1;i<=imx ; i++) {
1.126 brouard 7430: sump=sump+weight[i];
7431: /* sump=sump+1;*/
7432: num=num+1;
7433: }
7434:
7435:
7436: /* for (i=0; i<=imx; i++)
7437: 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]);*/
7438:
7439: for (i=1;i<=imx ; i++)
7440: {
7441: if (cens[i] == 1 && wav[i]>1)
7442: A=-x[1]/(x[2])*(exp(x[2]*(agecens[i]-agegomp))-exp(x[2]*(ageexmed[i]-agegomp)));
7443:
7444: if (cens[i] == 0 && wav[i]>1)
7445: A=-x[1]/(x[2])*(exp(x[2]*(agedc[i]-agegomp))-exp(x[2]*(ageexmed[i]-agegomp)))
7446: +log(x[1]/YEARM)+x[2]*(agedc[i]-agegomp)+log(YEARM);
7447:
7448: /*if (wav[i] > 1 && agecens[i] > 15) {*/ /* ??? */
7449: if (wav[i] > 1 ) { /* ??? */
7450: L=L+A*weight[i];
7451: /* 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]);*/
7452: }
7453: }
7454:
7455: /*printf("x1=%2.9f x2=%2.9f x3=%2.9f L=%f\n",x[1],x[2],x[3],L);*/
7456:
7457: return -2*L*num/sump;
7458: }
7459:
1.136 brouard 7460: #ifdef GSL
7461: /******************* Gompertz_f Likelihood ******************************/
7462: double gompertz_f(const gsl_vector *v, void *params)
7463: {
7464: double A,B,LL=0.0,sump=0.,num=0.;
7465: double *x= (double *) v->data;
7466: int i,n=0; /* n is the size of the sample */
7467:
7468: for (i=0;i<=imx-1 ; i++) {
7469: sump=sump+weight[i];
7470: /* sump=sump+1;*/
7471: num=num+1;
7472: }
7473:
7474:
7475: /* for (i=0; i<=imx; i++)
7476: 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]);*/
7477: printf("x[0]=%lf x[1]=%lf\n",x[0],x[1]);
7478: for (i=1;i<=imx ; i++)
7479: {
7480: if (cens[i] == 1 && wav[i]>1)
7481: A=-x[0]/(x[1])*(exp(x[1]*(agecens[i]-agegomp))-exp(x[1]*(ageexmed[i]-agegomp)));
7482:
7483: if (cens[i] == 0 && wav[i]>1)
7484: A=-x[0]/(x[1])*(exp(x[1]*(agedc[i]-agegomp))-exp(x[1]*(ageexmed[i]-agegomp)))
7485: +log(x[0]/YEARM)+x[1]*(agedc[i]-agegomp)+log(YEARM);
7486:
7487: /*if (wav[i] > 1 && agecens[i] > 15) {*/ /* ??? */
7488: if (wav[i] > 1 ) { /* ??? */
7489: LL=LL+A*weight[i];
7490: /* 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]);*/
7491: }
7492: }
7493:
7494: /*printf("x1=%2.9f x2=%2.9f x3=%2.9f L=%f\n",x[1],x[2],x[3],L);*/
7495: printf("x[0]=%lf x[1]=%lf -2*LL*num/sump=%lf\n",x[0],x[1],-2*LL*num/sump);
7496:
7497: return -2*LL*num/sump;
7498: }
7499: #endif
7500:
1.126 brouard 7501: /******************* Printing html file ***********/
1.201 brouard 7502: void printinghtmlmort(char fileresu[], char title[], char datafile[], int firstpass, \
1.126 brouard 7503: int lastpass, int stepm, int weightopt, char model[],\
7504: int imx, double p[],double **matcov,double agemortsup){
7505: int i,k;
7506:
7507: fprintf(fichtm,"<ul><li><h4>Result files </h4>\n Force of mortality. Parameters of the Gompertz fit (with confidence interval in brackets):<br>");
7508: fprintf(fichtm," mu(age) =%lf*exp(%lf*(age-%d)) per year<br><br>",p[1],p[2],agegomp);
7509: for (i=1;i<=2;i++)
7510: fprintf(fichtm," p[%d] = %lf [%f ; %f]<br>\n",i,p[i],p[i]-2*sqrt(matcov[i][i]),p[i]+2*sqrt(matcov[i][i]));
1.199 brouard 7511: fprintf(fichtm,"<br><br><img src=\"graphmort.svg\">");
1.126 brouard 7512: fprintf(fichtm,"</ul>");
7513:
7514: fprintf(fichtm,"<ul><li><h4>Life table</h4>\n <br>");
7515:
7516: 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>");
7517:
7518: for (k=agegomp;k<(agemortsup-2);k++)
7519: 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]);
7520:
7521:
7522: fflush(fichtm);
7523: }
7524:
7525: /******************* Gnuplot file **************/
1.201 brouard 7526: void printinggnuplotmort(char fileresu[], char optionfilefiname[], double ageminpar, double agemaxpar, double fage , char pathc[], double p[]){
1.126 brouard 7527:
7528: char dirfileres[132],optfileres[132];
1.164 brouard 7529:
1.126 brouard 7530: int ng;
7531:
7532:
7533: /*#ifdef windows */
7534: fprintf(ficgp,"cd \"%s\" \n",pathc);
7535: /*#endif */
7536:
7537:
7538: strcpy(dirfileres,optionfilefiname);
7539: strcpy(optfileres,"vpl");
1.199 brouard 7540: fprintf(ficgp,"set out \"graphmort.svg\"\n ");
1.126 brouard 7541: fprintf(ficgp,"set xlabel \"Age\"\n set ylabel \"Force of mortality (per year)\" \n ");
1.199 brouard 7542: fprintf(ficgp, "set ter svg size 640, 480\n set log y\n");
1.145 brouard 7543: /* fprintf(ficgp, "set size 0.65,0.65\n"); */
1.126 brouard 7544: fprintf(ficgp,"plot [%d:100] %lf*exp(%lf*(x-%d))",agegomp,p[1],p[2],agegomp);
7545:
7546: }
7547:
1.136 brouard 7548: int readdata(char datafile[], int firstobs, int lastobs, int *imax)
7549: {
1.126 brouard 7550:
1.136 brouard 7551: /*-------- data file ----------*/
7552: FILE *fic;
7553: char dummy[]=" ";
1.223 brouard 7554: int i=0, j=0, n=0, iv=0;
7555: int lstra;
1.136 brouard 7556: int linei, month, year,iout;
7557: char line[MAXLINE], linetmp[MAXLINE];
1.164 brouard 7558: char stra[MAXLINE], strb[MAXLINE];
1.136 brouard 7559: char *stratrunc;
1.223 brouard 7560:
1.126 brouard 7561:
7562:
1.136 brouard 7563: if((fic=fopen(datafile,"r"))==NULL) {
1.218 brouard 7564: printf("Problem while opening datafile: %s with errno='%s'\n", datafile,strerror(errno));fflush(stdout);
7565: fprintf(ficlog,"Problem while opening datafile: %s with errno='%s'\n", datafile,strerror(errno));fflush(ficlog);return 1;
1.136 brouard 7566: }
1.126 brouard 7567:
1.136 brouard 7568: i=1;
7569: linei=0;
7570: while ((fgets(line, MAXLINE, fic) != NULL) &&((i >= firstobs) && (i <=lastobs))) {
7571: linei=linei+1;
7572: for(j=strlen(line); j>=0;j--){ /* Untabifies line */
7573: if(line[j] == '\t')
7574: line[j] = ' ';
7575: }
7576: for(j=strlen(line)-1; (line[j]==' ')||(line[j]==10)||(line[j]==13);j--){
7577: ;
7578: };
7579: line[j+1]=0; /* Trims blanks at end of line */
7580: if(line[0]=='#'){
7581: fprintf(ficlog,"Comment line\n%s\n",line);
7582: printf("Comment line\n%s\n",line);
7583: continue;
7584: }
7585: trimbb(linetmp,line); /* Trims multiple blanks in line */
1.164 brouard 7586: strcpy(line, linetmp);
1.223 brouard 7587:
7588: /* Loops on waves */
7589: for (j=maxwav;j>=1;j--){
7590: for (iv=nqtv;iv>=1;iv--){ /* Loop on time varying quantitative variables */
1.225 brouard 7591: cutv(stra, strb, line, ' ');
7592: if(strb[0]=='.') { /* Missing value */
7593: lval=-1;
7594: cotqvar[j][iv][i]=-1; /* 0.0/0.0 */
7595: if(isalpha(strb[1])) { /* .m or .d Really Missing value */
7596: 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);
7597: 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);
7598: return 1;
7599: }
7600: }else{
7601: errno=0;
7602: /* what_kind_of_number(strb); */
7603: dval=strtod(strb,&endptr);
7604: /* if( strb[0]=='\0' || (*endptr != '\0')){ */
7605: /* if(strb != endptr && *endptr == '\0') */
7606: /* dval=dlval; */
7607: /* if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN)) */
7608: if( strb[0]=='\0' || (*endptr != '\0')){
7609: 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);
7610: 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);
7611: return 1;
7612: }
7613: cotqvar[j][iv][i]=dval;
7614: }
7615: strcpy(line,stra);
1.223 brouard 7616: }/* end loop ntqv */
1.225 brouard 7617:
1.223 brouard 7618: for (iv=ntv;iv>=1;iv--){ /* Loop on time varying dummies */
1.225 brouard 7619: cutv(stra, strb, line, ' ');
7620: if(strb[0]=='.') { /* Missing value */
7621: lval=-1;
7622: }else{
7623: errno=0;
7624: lval=strtol(strb,&endptr,10);
7625: /* if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN))*/
7626: if( strb[0]=='\0' || (*endptr != '\0')){
7627: 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);
7628: 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);
7629: return 1;
7630: }
7631: }
7632: if(lval <-1 || lval >1){
7633: printf("Error reading data around '%ld' at line number %d for individual %d, '%s'\n \
1.223 brouard 7634: Should be a value of %d(nth) covariate (0 should be the value for the reference and 1\n \
7635: for the alternative. IMaCh does not build design variables automatically, do it yourself.\n \
1.225 brouard 7636: For example, for multinomial values like 1, 2 and 3,\n \
7637: build V1=0 V2=0 for the reference value (1),\n \
7638: V1=1 V2=0 for (2) \n \
1.223 brouard 7639: and V1=0 V2=1 for (3). V1=1 V2=1 should not exist and the corresponding\n \
1.225 brouard 7640: output of IMaCh is often meaningless.\n \
1.223 brouard 7641: Exiting.\n",lval,linei, i,line,j);
1.225 brouard 7642: fprintf(ficlog,"Error reading data around '%ld' at line number %d for individual %d, '%s'\n \
1.223 brouard 7643: Should be a value of %d(nth) covariate (0 should be the value for the reference and 1\n \
7644: for the alternative. IMaCh does not build design variables automatically, do it yourself.\n \
1.225 brouard 7645: For example, for multinomial values like 1, 2 and 3,\n \
7646: build V1=0 V2=0 for the reference value (1),\n \
7647: V1=1 V2=0 for (2) \n \
1.223 brouard 7648: and V1=0 V2=1 for (3). V1=1 V2=1 should not exist and the corresponding\n \
7649: output of IMaCh is often meaningless.\n \
7650: Exiting.\n",lval,linei, i,line,j);fflush(ficlog);
1.225 brouard 7651: return 1;
7652: }
7653: cotvar[j][iv][i]=(double)(lval);
7654: strcpy(line,stra);
1.223 brouard 7655: }/* end loop ntv */
1.225 brouard 7656:
1.223 brouard 7657: /* Statuses at wave */
1.137 brouard 7658: cutv(stra, strb, line, ' ');
1.223 brouard 7659: if(strb[0]=='.') { /* Missing value */
1.225 brouard 7660: lval=-1;
1.136 brouard 7661: }else{
1.225 brouard 7662: errno=0;
7663: lval=strtol(strb,&endptr,10);
7664: /* if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN))*/
7665: if( strb[0]=='\0' || (*endptr != '\0')){
7666: 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);
7667: 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);
7668: return 1;
7669: }
1.136 brouard 7670: }
1.225 brouard 7671:
1.136 brouard 7672: s[j][i]=lval;
1.225 brouard 7673:
1.223 brouard 7674: /* Date of Interview */
1.136 brouard 7675: strcpy(line,stra);
7676: cutv(stra, strb,line,' ');
1.169 brouard 7677: if( (iout=sscanf(strb,"%d/%d",&month, &year)) != 0){
1.136 brouard 7678: }
1.169 brouard 7679: else if( (iout=sscanf(strb,"%s.",dummy)) != 0){
1.225 brouard 7680: month=99;
7681: year=9999;
1.136 brouard 7682: }else{
1.225 brouard 7683: 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);
7684: 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);
7685: return 1;
1.136 brouard 7686: }
7687: anint[j][i]= (double) year;
7688: mint[j][i]= (double)month;
7689: strcpy(line,stra);
1.223 brouard 7690: } /* End loop on waves */
1.225 brouard 7691:
1.223 brouard 7692: /* Date of death */
1.136 brouard 7693: cutv(stra, strb,line,' ');
1.169 brouard 7694: if( (iout=sscanf(strb,"%d/%d",&month, &year)) != 0){
1.136 brouard 7695: }
1.169 brouard 7696: else if( (iout=sscanf(strb,"%s.",dummy)) != 0){
1.136 brouard 7697: month=99;
7698: year=9999;
7699: }else{
1.141 brouard 7700: printf("Error reading data around '%s' at line number %d for individual %d, '%s'\nShould be a date of death (mm/yyyy or .). Exiting.\n",strb, linei,i,line);
1.225 brouard 7701: 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);
7702: return 1;
1.136 brouard 7703: }
7704: andc[i]=(double) year;
7705: moisdc[i]=(double) month;
7706: strcpy(line,stra);
7707:
1.223 brouard 7708: /* Date of birth */
1.136 brouard 7709: cutv(stra, strb,line,' ');
1.169 brouard 7710: if( (iout=sscanf(strb,"%d/%d",&month, &year)) != 0){
1.136 brouard 7711: }
1.169 brouard 7712: else if( (iout=sscanf(strb,"%s.", dummy)) != 0){
1.136 brouard 7713: month=99;
7714: year=9999;
7715: }else{
1.141 brouard 7716: 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);
7717: fprintf(ficlog,"Error reading data around '%s' at line number %d for individual %d, '%s'\nShould be a date of birth (mm/yyyy or .). Exiting.\n",strb, linei,i,line);fflush(ficlog);
1.225 brouard 7718: return 1;
1.136 brouard 7719: }
7720: if (year==9999) {
1.141 brouard 7721: 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);
7722: fprintf(ficlog,"Error reading data around '%s' at line number %d for individual %d, '%s'\nShould be a date of birth (mm/yyyy) but at least the year of birth should be given. Exiting.\n",strb, linei,i,line);fflush(ficlog);
1.225 brouard 7723: return 1;
7724:
1.136 brouard 7725: }
7726: annais[i]=(double)(year);
7727: moisnais[i]=(double)(month);
7728: strcpy(line,stra);
1.225 brouard 7729:
1.223 brouard 7730: /* Sample weight */
1.136 brouard 7731: cutv(stra, strb,line,' ');
7732: errno=0;
7733: dval=strtod(strb,&endptr);
7734: if( strb[0]=='\0' || (*endptr != '\0')){
1.141 brouard 7735: printf("Error reading data around '%f' at line number %d, \"%s\" for individual %d\nShould be a weight. Exiting.\n",dval, i,line,linei);
7736: fprintf(ficlog,"Error reading data around '%f' at line number %d, \"%s\" for individual %d\nShould be a weight. Exiting.\n",dval, i,line,linei);
1.136 brouard 7737: fflush(ficlog);
7738: return 1;
7739: }
7740: weight[i]=dval;
7741: strcpy(line,stra);
1.225 brouard 7742:
1.223 brouard 7743: for (iv=nqv;iv>=1;iv--){ /* Loop on fixed quantitative variables */
7744: cutv(stra, strb, line, ' ');
7745: if(strb[0]=='.') { /* Missing value */
1.225 brouard 7746: lval=-1;
1.223 brouard 7747: }else{
1.225 brouard 7748: errno=0;
7749: /* what_kind_of_number(strb); */
7750: dval=strtod(strb,&endptr);
7751: /* if(strb != endptr && *endptr == '\0') */
7752: /* dval=dlval; */
7753: /* if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN)) */
7754: if( strb[0]=='\0' || (*endptr != '\0')){
7755: 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);
7756: 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);
7757: return 1;
7758: }
7759: coqvar[iv][i]=dval;
1.226 brouard 7760: covar[ncovcol+iv][i]=dval; /* including qvar in standard covar for performance reasons */
1.223 brouard 7761: }
7762: strcpy(line,stra);
7763: }/* end loop nqv */
1.136 brouard 7764:
1.223 brouard 7765: /* Covariate values */
1.136 brouard 7766: for (j=ncovcol;j>=1;j--){
7767: cutv(stra, strb,line,' ');
1.223 brouard 7768: if(strb[0]=='.') { /* Missing covariate value */
1.225 brouard 7769: lval=-1;
1.136 brouard 7770: }else{
1.225 brouard 7771: errno=0;
7772: lval=strtol(strb,&endptr,10);
7773: if( strb[0]=='\0' || (*endptr != '\0')){
7774: 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);
7775: 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);
7776: return 1;
7777: }
1.136 brouard 7778: }
7779: if(lval <-1 || lval >1){
1.225 brouard 7780: printf("Error reading data around '%ld' at line number %d for individual %d, '%s'\n \
1.136 brouard 7781: Should be a value of %d(nth) covariate (0 should be the value for the reference and 1\n \
7782: for the alternative. IMaCh does not build design variables automatically, do it yourself.\n \
1.225 brouard 7783: For example, for multinomial values like 1, 2 and 3,\n \
7784: build V1=0 V2=0 for the reference value (1),\n \
7785: V1=1 V2=0 for (2) \n \
1.136 brouard 7786: and V1=0 V2=1 for (3). V1=1 V2=1 should not exist and the corresponding\n \
1.225 brouard 7787: output of IMaCh is often meaningless.\n \
1.136 brouard 7788: Exiting.\n",lval,linei, i,line,j);
1.225 brouard 7789: fprintf(ficlog,"Error reading data around '%ld' at line number %d for individual %d, '%s'\n \
1.136 brouard 7790: Should be a value of %d(nth) covariate (0 should be the value for the reference and 1\n \
7791: for the alternative. IMaCh does not build design variables automatically, do it yourself.\n \
1.225 brouard 7792: For example, for multinomial values like 1, 2 and 3,\n \
7793: build V1=0 V2=0 for the reference value (1),\n \
7794: V1=1 V2=0 for (2) \n \
1.136 brouard 7795: and V1=0 V2=1 for (3). V1=1 V2=1 should not exist and the corresponding\n \
1.225 brouard 7796: output of IMaCh is often meaningless.\n \
1.136 brouard 7797: Exiting.\n",lval,linei, i,line,j);fflush(ficlog);
1.225 brouard 7798: return 1;
1.136 brouard 7799: }
7800: covar[j][i]=(double)(lval);
7801: strcpy(line,stra);
7802: }
7803: lstra=strlen(stra);
1.225 brouard 7804:
1.136 brouard 7805: if(lstra > 9){ /* More than 2**32 or max of what printf can write with %ld */
7806: stratrunc = &(stra[lstra-9]);
7807: num[i]=atol(stratrunc);
7808: }
7809: else
7810: num[i]=atol(stra);
7811: /*if((s[2][i]==2) && (s[3][i]==-1)&&(s[4][i]==9)){
7812: 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;}*/
7813:
7814: i=i+1;
7815: } /* End loop reading data */
1.225 brouard 7816:
1.136 brouard 7817: *imax=i-1; /* Number of individuals */
7818: fclose(fic);
1.225 brouard 7819:
1.136 brouard 7820: return (0);
1.164 brouard 7821: /* endread: */
1.225 brouard 7822: printf("Exiting readdata: ");
7823: fclose(fic);
7824: return (1);
1.223 brouard 7825: }
1.126 brouard 7826:
1.230 brouard 7827: void removespace(char **stri){/*, char stro[]) {*/
7828: char *p1 = *stri, *p2 = *stri;
1.145 brouard 7829: do
7830: while (*p2 == ' ')
7831: p2++;
1.169 brouard 7832: while (*p1++ == *p2++);
1.230 brouard 7833: *stri=p1;
1.145 brouard 7834: }
7835:
1.230 brouard 7836: int decoderesult ( char resultline[])
7837: /**< This routine decode one result line and returns the combination # of dummy covariates only **/
7838: {
7839: int j=0, k=0;
7840: char resultsav[MAXLINE];
7841: char stra[80], strb[80], strc[80], strd[80],stre[80];
7842:
7843: removespace(&resultline);
7844: printf("decoderesult=%s\n",resultline);
7845:
7846: if (strstr(resultline,"v") !=0){
7847: printf("Error. 'v' must be in upper case 'V' result: %s ",resultline);
7848: fprintf(ficlog,"Error. 'v' must be in upper case result: %s ",resultline);fflush(ficlog);
7849: return 1;
7850: }
7851: trimbb(resultsav, resultline);
7852: if (strlen(resultsav) >1){
7853: j=nbocc(resultsav,'='); /**< j=Number of covariate values'=' */
7854: }
7855:
7856: for(k=1; k<=j;k++){ /* Loop on total covariates of the model */
7857: cutl(stra,strb,resultsav,' '); /* keeps in strb after the first ' '
7858: resultsav= V4=1 V5=25.1 V3=0 strb=V3=0 stra= V4=1 V5=25.1 */
7859: cutl(strc,strd,strb,'='); /* strb:V4=1 strc=1 strd=V4 */
7860: Tvalsel[k]=atof(strc); /* 1 */
7861:
7862: cutl(strc,stre,strd,'V'); /* strd='V4' strc=4 stre='V' */;
7863: Tvarsel[k]=atoi(strc);
7864: /* Typevarsel[k]=1; /\* 1 for age product *\/ */
7865: /* cptcovsel++; */
7866: if (nbocc(stra,'=') >0)
7867: strcpy(resultsav,stra); /* and analyzes it */
7868: }
7869: return (0);
7870: }
7871: int selected( int kvar){ /* Selected combination of covariates */
7872: if(Tvarsel[kvar])
7873: return (0);
7874: else
7875: return(1);
7876: }
7877: int decodemodel( char model[], int lastobs)
7878: /**< This routine decodes the model and returns:
1.224 brouard 7879: * Model V1+V2+V3+V8+V7*V8+V5*V6+V8*age+V3*age+age*age
7880: * - nagesqr = 1 if age*age in the model, otherwise 0.
7881: * - cptcovt total number of covariates of the model nbocc(+)+1 = 8 excepting constant and age and age*age
7882: * - cptcovn or number of covariates k of the models excluding age*products =6 and age*age
7883: * - cptcovage number of covariates with age*products =2
7884: * - cptcovs number of simple covariates
7885: * - 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
7886: * which is a new column after the 9 (ncovcol) variables.
7887: * - if k is a product Vn*Vm covar[k][i] is filled with correct values for each individual
7888: * - Tprod[l] gives the kth covariates of the product Vn*Vm l=1 to cptcovprod-cptcovage
7889: * Tprod[1]@2 {5, 6}: position of first product V7*V8 is 5, and second V5*V6 is 6.
7890: * - Tvard[k] p Tvard[1][1]@4 {7, 8, 5, 6} for V7*V8 and V5*V6 .
7891: */
1.136 brouard 7892: {
1.145 brouard 7893: int i, j, k, ks;
1.227 brouard 7894: int j1, k1, k2, k3, k4;
1.136 brouard 7895: char modelsav[80];
1.145 brouard 7896: char stra[80], strb[80], strc[80], strd[80],stre[80];
1.187 brouard 7897: char *strpt;
1.136 brouard 7898:
1.145 brouard 7899: /*removespace(model);*/
1.136 brouard 7900: if (strlen(model) >1){ /* If there is at least 1 covariate */
1.145 brouard 7901: j=0, j1=0, k1=0, k2=-1, ks=0, cptcovn=0;
1.137 brouard 7902: if (strstr(model,"AGE") !=0){
1.192 brouard 7903: printf("Error. AGE must be in lower case 'age' model=1+age+%s. ",model);
7904: fprintf(ficlog,"Error. AGE must be in lower case model=1+age+%s. ",model);fflush(ficlog);
1.136 brouard 7905: return 1;
7906: }
1.141 brouard 7907: if (strstr(model,"v") !=0){
7908: printf("Error. 'v' must be in upper case 'V' model=%s ",model);
7909: fprintf(ficlog,"Error. 'v' must be in upper case model=%s ",model);fflush(ficlog);
7910: return 1;
7911: }
1.187 brouard 7912: strcpy(modelsav,model);
7913: if ((strpt=strstr(model,"age*age")) !=0){
7914: printf(" strpt=%s, model=%s\n",strpt, model);
7915: if(strpt != model){
1.231 ! brouard 7916: printf("Error in model: 'model=%s'; 'age*age' should in first place before other covariates\n \
1.192 brouard 7917: 'model=1+age+age*age+V1.' or 'model=1+age+age*age+V1+V1*age.', please swap as well as \n \
1.187 brouard 7918: corresponding column of parameters.\n",model);
1.231 ! brouard 7919: fprintf(ficlog,"Error in model: 'model=%s'; 'age*age' should in first place before other covariates\n \
1.192 brouard 7920: 'model=1+age+age*age+V1.' or 'model=1+age+age*age+V1+V1*age.', please swap as well as \n \
1.187 brouard 7921: corresponding column of parameters.\n",model); fflush(ficlog);
1.231 ! brouard 7922: return 1;
1.225 brouard 7923: }
1.187 brouard 7924: nagesqr=1;
7925: if (strstr(model,"+age*age") !=0)
1.231 ! brouard 7926: substrchaine(modelsav, model, "+age*age");
1.187 brouard 7927: else if (strstr(model,"age*age+") !=0)
1.231 ! brouard 7928: substrchaine(modelsav, model, "age*age+");
1.187 brouard 7929: else
1.231 ! brouard 7930: substrchaine(modelsav, model, "age*age");
1.187 brouard 7931: }else
7932: nagesqr=0;
7933: if (strlen(modelsav) >1){
7934: j=nbocc(modelsav,'+'); /**< j=Number of '+' */
7935: j1=nbocc(modelsav,'*'); /**< j1=Number of '*' */
1.224 brouard 7936: cptcovs=j+1-j1; /**< Number of simple covariates V1+V1*age+V3 +V3*V4+age*age=> V1 + V3 =5-3=2 */
1.187 brouard 7937: cptcovt= j+1; /* Number of total covariates in the model, not including
1.225 brouard 7938: * cst, age and age*age
7939: * V1+V1*age+ V3 + V3*V4+age*age=> 3+1=4*/
7940: /* including age products which are counted in cptcovage.
7941: * but the covariates which are products must be treated
7942: * separately: ncovn=4- 2=2 (V1+V3). */
1.187 brouard 7943: cptcovprod=j1; /**< Number of products V1*V2 +v3*age = 2 */
7944: cptcovprodnoage=0; /**< Number of covariate products without age: V3*V4 =1 */
1.225 brouard 7945:
7946:
1.187 brouard 7947: /* Design
7948: * V1 V2 V3 V4 V5 V6 V7 V8 V9 Weight
7949: * < ncovcol=8 >
7950: * Model V2 + V1 + V3*age + V3 + V5*V6 + V7*V8 + V8*age + V8
7951: * k= 1 2 3 4 5 6 7 8
7952: * cptcovn number of covariates (not including constant and age ) = # of + plus 1 = 7+1=8
7953: * covar[k,i], value of kth covariate if not including age for individual i:
1.224 brouard 7954: * covar[1][i]= (V1), covar[4][i]=(V4), covar[8][i]=(V8)
7955: * Tvar[k] # of the kth covariate: Tvar[1]=2 Tvar[2]=1 Tvar[4]=3 Tvar[8]=8
1.187 brouard 7956: * if multiplied by age: V3*age Tvar[3=V3*age]=3 (V3) Tvar[7]=8 and
7957: * Tage[++cptcovage]=k
7958: * if products, new covar are created after ncovcol with k1
7959: * Tvar[k]=ncovcol+k1; # of the kth covariate product: Tvar[5]=ncovcol+1=10 Tvar[6]=ncovcol+1=11
7960: * Tprod[k1]=k; Tprod[1]=5 Tprod[2]= 6; gives the position of the k1th product
7961: * 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
7962: * Tvar[cptcovn+k2]=Tvard[k1][1];Tvar[cptcovn+k2+1]=Tvard[k1][2];
7963: * Tvar[8+1]=5;Tvar[8+2]=6;Tvar[8+3]=7;Tvar[8+4]=8 inverted
7964: * V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11
7965: * < ncovcol=8 >
7966: * Model V2 + V1 + V3*age + V3 + V5*V6 + V7*V8 + V8*age + V8 d1 d1 d2 d2
7967: * k= 1 2 3 4 5 6 7 8 9 10 11 12
7968: * Tvar[k]= 2 1 3 3 10 11 8 8 5 6 7 8
7969: * p Tvar[1]@12={2, 1, 3, 3, 11, 10, 8, 8, 7, 8, 5, 6}
7970: * p Tprod[1]@2={ 6, 5}
7971: *p Tvard[1][1]@4= {7, 8, 5, 6}
7972: * covar[k][i]= V2 V1 ? V3 V5*V6? V7*V8? ? V8
7973: * cov[Tage[kk]+2]=covar[Tvar[Tage[kk]]][i]*cov[2];
7974: *How to reorganize?
7975: * Model V1 + V2 + V3 + V8 + V5*V6 + V7*V8 + V3*age + V8*age
7976: * Tvars {2, 1, 3, 3, 11, 10, 8, 8, 7, 8, 5, 6}
7977: * {2, 1, 4, 8, 5, 6, 3, 7}
7978: * Struct []
7979: */
1.225 brouard 7980:
1.187 brouard 7981: /* This loop fills the array Tvar from the string 'model'.*/
7982: /* j is the number of + signs in the model V1+V2+V3 j=2 i=3 to 1 */
7983: /* modelsav=V2+V1+V4+age*V3 strb=age*V3 stra=V2+V1+V4 */
7984: /* k=4 (age*V3) Tvar[k=4]= 3 (from V3) Tage[cptcovage=1]=4 */
7985: /* k=3 V4 Tvar[k=3]= 4 (from V4) */
7986: /* k=2 V1 Tvar[k=2]= 1 (from V1) */
7987: /* k=1 Tvar[1]=2 (from V2) */
7988: /* k=5 Tvar[5] */
7989: /* for (k=1; k<=cptcovn;k++) { */
1.198 brouard 7990: /* cov[2+k]=nbcode[Tvar[k]][codtabm(ij,Tvar[k])]; */
1.187 brouard 7991: /* } */
1.198 brouard 7992: /* for (k=1; k<=cptcovage;k++) cov[2+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,Tvar[Tage[k])]]*cov[2]; */
1.187 brouard 7993: /*
7994: * Treating invertedly V2+V1+V3*age+V2*V4 is as if written V2*V4 +V3*age + V1 + V2 */
1.227 brouard 7995: for(k=cptcovt; k>=1;k--){ /**< Number of covariates not including constant and age, neither age*age*/
7996: Tvar[k]=0; Tprod[k]=0; Tposprod[k]=0;
7997: }
1.187 brouard 7998: cptcovage=0;
7999: for(k=1; k<=cptcovt;k++){ /* Loop on total covariates of the model */
1.225 brouard 8000: cutl(stra,strb,modelsav,'+'); /* keeps in strb after the first '+'
8001: modelsav==V2+V1+V4+V3*age strb=V3*age stra=V2+V1+V4 */
8002: if (nbocc(modelsav,'+')==0) strcpy(strb,modelsav); /* and analyzes it */
8003: /* printf("i=%d a=%s b=%s sav=%s\n",i, stra,strb,modelsav);*/
8004: /*scanf("%d",i);*/
8005: if (strchr(strb,'*')) { /**< Model includes a product V2+V1+V4+V3*age strb=V3*age */
8006: cutl(strc,strd,strb,'*'); /**< strd*strc Vm*Vn: strb=V3*age(input) strc=age strd=V3 ; V3*V2 strc=V2, strd=V3 */
8007: if (strcmp(strc,"age")==0) { /**< Model includes age: Vn*age */
8008: /* covar is not filled and then is empty */
8009: cptcovprod--;
8010: cutl(stre,strb,strd,'V'); /* strd=V3(input): stre="3" */
8011: Tvar[k]=atoi(stre); /* V2+V1+V4+V3*age Tvar[4]=3 ; V1+V2*age Tvar[2]=2; V1+V1*age Tvar[2]=1 */
1.226 brouard 8012: Typevar[k]=1; /* 1 for age product */
1.225 brouard 8013: cptcovage++; /* Sums the number of covariates which include age as a product */
8014: Tage[cptcovage]=k; /* Tvar[4]=3, Tage[1] = 4 or V1+V1*age Tvar[2]=1, Tage[1]=2 */
8015: /*printf("stre=%s ", stre);*/
8016: } else if (strcmp(strd,"age")==0) { /* or age*Vn */
8017: cptcovprod--;
8018: cutl(stre,strb,strc,'V');
8019: Tvar[k]=atoi(stre);
8020: Typevar[k]=1; /* 1 for age product */
8021: cptcovage++;
8022: Tage[cptcovage]=k;
8023: } else { /* Age is not in the model product V2+V1+V1*V4+V3*age+V3*V2 strb=V3*V2*/
8024: /* loops on k1=1 (V3*V2) and k1=2 V4*V3 */
8025: cptcovn++;
8026: cptcovprodnoage++;k1++;
8027: cutl(stre,strb,strc,'V'); /* strc= Vn, stre is n; strb=V3*V2 stre=3 strc=*/
8028: Tvar[k]=ncovcol+nqv+ntv+nqtv+k1; /* For model-covariate k tells which data-covariate to use but
8029: because this model-covariate is a construction we invent a new column
8030: which is after existing variables ncovcol+nqv+ntv+nqtv + k1
8031: If already ncovcol=4 and model=V2+V1+V1*V4+age*V3+V3*V2
8032: Tvar[3=V1*V4]=4+1 Tvar[5=V3*V2]=4 + 2= 6, etc */
8033: Typevar[k]=2; /* 2 for double fixed dummy covariates */
8034: cutl(strc,strb,strd,'V'); /* strd was Vm, strc is m */
8035: Tprod[k1]=k; /* Tprod[1]=3(=V1*V4) for V2+V1+V1*V4+age*V3+V3*V2 */
1.227 brouard 8036: Tposprod[k]=k1; /* Tpsprod[3]=1, Tposprod[2]=5 */
1.225 brouard 8037: Tvard[k1][1] =atoi(strc); /* m 1 for V1*/
8038: Tvard[k1][2] =atoi(stre); /* n 4 for V4*/
8039: k2=k2+2; /* k2 is initialize to -1, We want to store the n and m in Vn*Vm at the end of Tvar */
8040: /* Tvar[cptcovt+k2]=Tvard[k1][1]; /\* Tvar[(cptcovt=4+k2=1)=5]= 1 (V1) *\/ */
8041: /* Tvar[cptcovt+k2+1]=Tvard[k1][2]; /\* Tvar[(cptcovt=4+(k2=1)+1)=6]= 4 (V4) *\/ */
8042: /*ncovcol=4 and model=V2+V1+V1*V4+age*V3+V3*V2, Tvar[3]=5, Tvar[4]=6, cptcovt=5 */
8043: /* 1 2 3 4 5 | Tvar[5+1)=1, Tvar[7]=2 */
8044: for (i=1; i<=lastobs;i++){
8045: /* Computes the new covariate which is a product of
8046: covar[n][i]* covar[m][i] and stores it at ncovol+k1 May not be defined */
8047: covar[ncovcol+k1][i]=covar[atoi(stre)][i]*covar[atoi(strc)][i];
8048: }
8049: } /* End age is not in the model */
8050: } /* End if model includes a product */
8051: else { /* no more sum */
8052: /*printf("d=%s c=%s b=%s\n", strd,strc,strb);*/
8053: /* scanf("%d",i);*/
8054: cutl(strd,strc,strb,'V');
1.227 brouard 8055: ks++; /**< Number of simple covariates dummy or quantitative, fixe or varying */
1.225 brouard 8056: cptcovn++; /** V4+V3+V5: V4 and V3 timevarying dummy covariates, V5 timevarying quantitative */
8057: Tvar[k]=atoi(strd);
8058: Typevar[k]=0; /* 0 for simple covariates */
8059: }
8060: strcpy(modelsav,stra); /* modelsav=V2+V1+V4 stra=V2+V1+V4 */
1.223 brouard 8061: /*printf("a=%s b=%s sav=%s\n", stra,strb,modelsav);
1.225 brouard 8062: scanf("%d",i);*/
1.187 brouard 8063: } /* end of loop + on total covariates */
8064: } /* end if strlen(modelsave == 0) age*age might exist */
8065: } /* end if strlen(model == 0) */
1.136 brouard 8066:
8067: /*The number n of Vn is stored in Tvar. cptcovage =number of age covariate. Tage gives the position of age. cptcovprod= number of products.
8068: If model=V1+V1*age then Tvar[1]=1 Tvar[2]=1 cptcovage=1 Tage[1]=2 cptcovprod=0*/
1.225 brouard 8069:
1.136 brouard 8070: /* printf("tvar1=%d tvar2=%d tvar3=%d cptcovage=%d Tage=%d",Tvar[1],Tvar[2],Tvar[3],cptcovage,Tage[1]);
1.225 brouard 8071: printf("cptcovprod=%d ", cptcovprod);
8072: fprintf(ficlog,"cptcovprod=%d ", cptcovprod);
8073: scanf("%d ",i);*/
8074:
8075:
1.230 brouard 8076: /* Until here, decodemodel knows only the grammar (simple, product, age*) of the model but not what kind
8077: of variable (dummy vs quantitative, fixed vs time varying) is behind. But we know the # of each. */
1.226 brouard 8078: /* ncovcol= 1, nqv=1 | ntv=2, nqtv= 1 = 5 possible variables data: 2 fixed 3, varying
8079: model= V5 + V4 +V3 + V4*V3 + V5*age + V2 + V1*V2 + V1*age + V5*age, V1 is not used saving its place
8080: k = 1 2 3 4 5 6 7 8 9
8081: Tvar[k]= 5 4 3 1+1+2+1+1=6 5 2 7 1 5
8082: Typevar[k]= 0 0 0 2 1 0 2 1 1
1.227 brouard 8083: Fixed[k] 1 1 1 1 3 0 0 or 2 2 3
8084: Dummy[k] 1 0 0 0 3 1 1 2 3
8085: Tmodelind[combination of covar]=k;
1.225 brouard 8086: */
8087: /* Dispatching between quantitative and time varying covariates */
1.226 brouard 8088: /* If Tvar[k] >ncovcol it is a product */
1.225 brouard 8089: /* Tvar[k] is the value n of Vn with n varying for 1 to nvcol, or p Vp=Vn*Vm for product */
1.226 brouard 8090: /* Computing effective variables, ie used by the model, that is from the cptcovt variables */
1.227 brouard 8091: printf("Model=%s\n\
8092: Typevar: 0 for simple covariate (dummy, quantitative, fixed or varying), 1 for age product, 2 for product \n\
8093: Fixed[k] 0=fixed (product or simple), 1 varying, 2 fixed with age product, 3 varying with age product \n\
8094: 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);
8095: fprintf(ficlog,"Model=%s\n\
8096: Typevar: 0 for simple covariate (dummy, quantitative, fixed or varying), 1 for age product, 2 for product \n\
8097: Fixed[k] 0=fixed (product or simple), 1 varying, 2 fixed with age product, 3 varying with age product \n\
8098: 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);
8099:
1.225 brouard 8100: for(k=1, ncoveff=0, nqfveff=0, ntveff=0, nqtveff=0;k<=cptcovt; k++){ /* or cptocvt */
1.231 ! brouard 8101: if (Tvar[k] <=ncovcol && (Typevar[k]==0 || Typevar[k]==2)){ /* Simple or product fixed dummy (<=ncovcol) covariates */
1.227 brouard 8102: Fixed[k]= 0;
8103: Dummy[k]= 0;
1.225 brouard 8104: ncoveff++;
1.231 ! brouard 8105: modell[k].maintype= FTYPE;
1.230 brouard 8106: TvarFD[ncoveff]=Tvar[k]; /* TvarFD[1]=V1 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
1.231 ! brouard 8107: TvarFDind[ncoveff]=k; /* TvarFDind[1]=9 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
! 8108: }else if( Tvar[k] <=ncovcol+nqv && Typevar[k]==0){ /* Remind that product Vn*Vm are added in k*/ /* Only simple fixed quantitative variable */
1.227 brouard 8109: Fixed[k]= 0;
8110: Dummy[k]= 1;
1.230 brouard 8111: nqfveff++;
1.231 ! brouard 8112: modell[k].maintype= FTYPE;
! 8113: modell[k].subtype= FQ;
! 8114: TvarFQ[nqfveff]=Tvar[k]-ncovcol; /* TvarFQ[1]=V2-1=1st in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ /* Only simple fixed quantitative variable */
1.230 brouard 8115: TvarFQind[nqfveff]=k; /* TvarFQind[1]=6 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ /* Only simple fixed quantitative variable */
1.225 brouard 8116: }else if( Tvar[k] <=ncovcol+nqv+ntv && Typevar[k]==0){
1.227 brouard 8117: Fixed[k]= 1;
8118: Dummy[k]= 0;
1.225 brouard 8119: ntveff++; /* Only simple time varying dummy variable */
1.231 ! brouard 8120: modell[k].maintype= VTYPE;
! 8121: modell[k].subtype= VD;
! 8122: 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 */
! 8123: TvarVDind[ntveff]=k; /* TvarVDind[1]=2 TvarVDind[2]=3 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ /* Only simple time varying dummy variable */
1.228 brouard 8124: 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);
8125: printf("Quasi TmodelInvind[%d]=%d\n",k,Tvar[k]- ncovcol-nqv);
1.231 ! brouard 8126: }else if( Tvar[k] <=ncovcol+nqv+ntv+nqtv && Typevar[k]==0){ /* Only simple time varying quantitative variable V5*/
! 8127: Fixed[k]= 1;
! 8128: Dummy[k]= 1;
! 8129: nqtveff++;
! 8130: modell[k].maintype= VTYPE;
! 8131: modell[k].subtype= VQ;
! 8132: 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 */
! 8133: 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 */
! 8134: TmodelInvQind[nqtveff]=Tvar[k]- ncovcol-nqv-ntv;/* Only simple time varying quantitative variable */
! 8135: /* Tmodeliqind[k]=nqtveff;/\* Only simple time varying quantitative variable *\/ */
! 8136: printf("Quasi TmodelQind[%d]=%d,Tvar[TmodelQind[%d]]=V%d, ncovcol=%d, nqv=%d, ntv=%d,Tvar[k]- ncovcol-nqv-ntv=%d\n",nqtveff,k,nqtveff,Tvar[k], ncovcol, nqv, ntv, Tvar[k]- ncovcol-nqv-ntv);
1.228 brouard 8137: printf("Quasi TmodelInvQind[%d]=%d\n",k,Tvar[k]- ncovcol-nqv-ntv);
1.227 brouard 8138: }else if (Typevar[k] == 1) { /* product with age */
1.231 ! brouard 8139: if (Tvar[k] <=ncovcol ){ /* Product age with fixed dummy covariatee */
! 8140: Fixed[k]= 2;
! 8141: Dummy[k]= 2;
! 8142: modell[k].maintype= ATYPE;
! 8143: modell[k].subtype= APFD;
! 8144: /* ncoveff++; */
1.227 brouard 8145: }else if( Tvar[k] <=ncovcol+nqv) { /* Remind that product Vn*Vm are added in k*/
1.231 ! brouard 8146: Fixed[k]= 2;
! 8147: Dummy[k]= 3;
! 8148: modell[k].maintype= ATYPE;
! 8149: modell[k].subtype= APFQ; /* Product age * fixed quantitative */
! 8150: /* nqfveff++; /\* Only simple fixed quantitative variable *\/ */
1.227 brouard 8151: }else if( Tvar[k] <=ncovcol+nqv+ntv ){
1.231 ! brouard 8152: Fixed[k]= 3;
! 8153: Dummy[k]= 2;
! 8154: modell[k].maintype= ATYPE;
! 8155: modell[k].subtype= APVD; /* Product age * varying dummy */
! 8156: /* ntveff++; /\* Only simple time varying dummy variable *\/ */
1.227 brouard 8157: }else if( Tvar[k] <=ncovcol+nqv+ntv+nqtv){
1.231 ! brouard 8158: Fixed[k]= 3;
! 8159: Dummy[k]= 3;
! 8160: modell[k].maintype= ATYPE;
! 8161: modell[k].subtype= APVQ; /* Product age * varying quantitative */
! 8162: /* nqtveff++;/\* Only simple time varying quantitative variable *\/ */
1.227 brouard 8163: }
8164: }else if (Typevar[k] == 2) { /* product without age */
8165: k1=Tposprod[k];
8166: if(Tvard[k1][1] <=ncovcol){
1.231 ! brouard 8167: if(Tvard[k1][2] <=ncovcol){
! 8168: Fixed[k]= 1;
! 8169: Dummy[k]= 0;
! 8170: modell[k].maintype= FTYPE;
! 8171: modell[k].subtype= FPDD; /* Product fixed dummy * fixed dummy */
! 8172: }else if(Tvard[k1][2] <=ncovcol+nqv){
! 8173: Fixed[k]= 0; /* or 2 ?*/
! 8174: Dummy[k]= 1;
! 8175: modell[k].maintype= FTYPE;
! 8176: modell[k].subtype= FPDQ; /* Product fixed dummy * fixed quantitative */
! 8177: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv){
! 8178: Fixed[k]= 1;
! 8179: Dummy[k]= 0;
! 8180: modell[k].maintype= VTYPE;
! 8181: modell[k].subtype= VPDD; /* Product fixed dummy * varying dummy */
! 8182: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv+nqtv){
! 8183: Fixed[k]= 1;
! 8184: Dummy[k]= 1;
! 8185: modell[k].maintype= VTYPE;
! 8186: modell[k].subtype= VPDQ; /* Product fixed dummy * varying quantitative */
! 8187: }
1.227 brouard 8188: }else if(Tvard[k1][1] <=ncovcol+nqv){
1.231 ! brouard 8189: if(Tvard[k1][2] <=ncovcol){
! 8190: Fixed[k]= 0; /* or 2 ?*/
! 8191: Dummy[k]= 1;
! 8192: modell[k].maintype= FTYPE;
! 8193: modell[k].subtype= FPDQ; /* Product fixed quantitative * fixed dummy */
! 8194: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv){
! 8195: Fixed[k]= 1;
! 8196: Dummy[k]= 1;
! 8197: modell[k].maintype= VTYPE;
! 8198: modell[k].subtype= VPDQ; /* Product fixed quantitative * varying dummy */
! 8199: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv+nqtv){
! 8200: Fixed[k]= 1;
! 8201: Dummy[k]= 1;
! 8202: modell[k].maintype= VTYPE;
! 8203: modell[k].subtype= VPQQ; /* Product fixed quantitative * varying quantitative */
! 8204: }
1.227 brouard 8205: }else if(Tvard[k1][1] <=ncovcol+nqv+ntv){
1.231 ! brouard 8206: if(Tvard[k1][2] <=ncovcol){
! 8207: Fixed[k]= 1;
! 8208: Dummy[k]= 1;
! 8209: modell[k].maintype= VTYPE;
! 8210: modell[k].subtype= VPDD; /* Product time varying dummy * fixed dummy */
! 8211: }else if(Tvard[k1][2] <=ncovcol+nqv){
! 8212: Fixed[k]= 1;
! 8213: Dummy[k]= 1;
! 8214: modell[k].maintype= VTYPE;
! 8215: modell[k].subtype= VPDQ; /* Product time varying dummy * fixed quantitative */
! 8216: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv){
! 8217: Fixed[k]= 1;
! 8218: Dummy[k]= 0;
! 8219: modell[k].maintype= VTYPE;
! 8220: modell[k].subtype= VPDD; /* Product time varying dummy * time varying dummy */
! 8221: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv+nqtv){
! 8222: Fixed[k]= 1;
! 8223: Dummy[k]= 1;
! 8224: modell[k].maintype= VTYPE;
! 8225: modell[k].subtype= VPDQ; /* Product time varying dummy * time varying quantitative */
! 8226: }
1.227 brouard 8227: }else if(Tvard[k1][1] <=ncovcol+nqv+ntv+nqtv){
1.231 ! brouard 8228: if(Tvard[k1][2] <=ncovcol){
! 8229: Fixed[k]= 1;
! 8230: Dummy[k]= 1;
! 8231: modell[k].maintype= VTYPE;
! 8232: modell[k].subtype= VPDQ; /* Product time varying quantitative * fixed dummy */
! 8233: }else if(Tvard[k1][2] <=ncovcol+nqv){
! 8234: Fixed[k]= 1;
! 8235: Dummy[k]= 1;
! 8236: modell[k].maintype= VTYPE;
! 8237: modell[k].subtype= VPQQ; /* Product time varying quantitative * fixed quantitative */
! 8238: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv){
! 8239: Fixed[k]= 1;
! 8240: Dummy[k]= 1;
! 8241: modell[k].maintype= VTYPE;
! 8242: modell[k].subtype= VPDQ; /* Product time varying quantitative * time varying dummy */
! 8243: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv+nqtv){
! 8244: Fixed[k]= 1;
! 8245: Dummy[k]= 1;
! 8246: modell[k].maintype= VTYPE;
! 8247: modell[k].subtype= VPQQ; /* Product time varying quantitative * time varying quantitative */
! 8248: }
1.227 brouard 8249: }else{
1.231 ! brouard 8250: printf("Error unknown type of covariate: Tvard[%d][1]=%d,Tvard[%d][2]=%d\n",k1,Tvard[k1][1],k1,Tvard[k1][2]);
! 8251: fprintf(ficlog,"Error unknown type of covariate: Tvard[%d][1]=%d,Tvard[%d][2]=%d\n",k1,Tvard[k1][1],k1,Tvard[k1][2]);
1.226 brouard 8252: } /* end k1 */
1.225 brouard 8253: }else{
1.226 brouard 8254: printf("Error, current version can't treat for performance reasons, Tvar[%d]=%d, Typevar[%d]=%d\n", k, Tvar[k], k, Typevar[k]);
8255: fprintf(ficlog,"Error, current version can't treat for performance reasons, Tvar[%d]=%d, Typevar[%d]=%d\n", k, Tvar[k], k, Typevar[k]);
1.225 brouard 8256: }
1.227 brouard 8257: printf("Decodemodel, k=%d, Tvar[%d]=V%d,Typevar=%d, Fixed=%d, Dummy=%d\n",k, k,Tvar[k],Typevar[k],Fixed[k],Dummy[k]);
1.231 ! brouard 8258: printf(" modell[%d].maintype=%d, modell[%d].subtype=%d\n",k,modell[k].maintype,k,modell[k].subtype);
1.227 brouard 8259: 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]);
8260: }
8261: /* Searching for doublons in the model */
8262: for(k1=1; k1<= cptcovt;k1++){
8263: for(k2=1; k2 <k1;k2++){
8264: if((Typevar[k1]==Typevar[k2]) && (Fixed[Tvar[k1]]==Fixed[Tvar[k2]]) && (Dummy[Tvar[k1]]==Dummy[Tvar[k2]] )){
1.231 ! brouard 8265: if((Typevar[k1] == 0 || Typevar[k1] == 1)){ /* Simple or age product */
! 8266: if(Tvar[k1]==Tvar[k2]){
! 8267: printf("Error duplication in the model=%s at positions (+) %d and %d, Tvar[%d]=V%d, Tvar[%d]=V%d, Typevar=%d, Fixed=%d, Dummy=%d\n", model, k1,k2, k1, Tvar[k1], k2, Tvar[k2],Typevar[k1],Fixed[Tvar[k1]],Dummy[Tvar[k1]]);
! 8268: fprintf(ficlog,"Error duplication in the model=%s at positions (+) %d and %d, Tvar[%d]=V%d, Tvar[%d]=V%d, Typevar=%d, Fixed=%d, Dummy=%d\n", model, k1,k2, k1, Tvar[k1], k2, Tvar[k2],Typevar[k1],Fixed[Tvar[k1]],Dummy[Tvar[k1]]); fflush(ficlog);
! 8269: return(1);
! 8270: }
! 8271: }else if (Typevar[k1] ==2){
! 8272: k3=Tposprod[k1];
! 8273: k4=Tposprod[k2];
! 8274: 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])) ){
! 8275: printf("Error duplication in the model=%s at positions (+) %d and %d, V%d*V%d, Typevar=%d, Fixed=%d, Dummy=%d\n",model, k1,k2, Tvard[k3][1], Tvard[k3][2],Typevar[k1],Fixed[Tvar[k1]],Dummy[Tvar[k1]]);
! 8276: fprintf(ficlog,"Error duplication in the model=%s at positions (+) %d and %d, V%d*V%d, Typevar=%d, Fixed=%d, Dummy=%d\n",model, k1,k2, Tvard[k3][1], Tvard[k3][2],Typevar[k1],Fixed[Tvar[k1]],Dummy[Tvar[k1]]); fflush(ficlog);
! 8277: return(1);
! 8278: }
! 8279: }
1.227 brouard 8280: }
8281: }
1.225 brouard 8282: }
8283: printf("ncoveff=%d, nqfveff=%d, ntveff=%d, nqtveff=%d, cptcovn=%d\n",ncoveff,nqfveff,ntveff,nqtveff,cptcovn);
8284: fprintf(ficlog,"ncoveff=%d, nqfveff=%d, ntveff=%d, nqtveff=%d, cptcovn=%d\n",ncoveff,nqfveff,ntveff,nqtveff,cptcovn);
1.137 brouard 8285: return (0); /* with covar[new additional covariate if product] and Tage if age */
1.164 brouard 8286: /*endread:*/
1.225 brouard 8287: printf("Exiting decodemodel: ");
8288: return (1);
1.136 brouard 8289: }
8290:
1.169 brouard 8291: int calandcheckages(int imx, int maxwav, double *agemin, double *agemax, int *nberr, int *nbwarn )
1.136 brouard 8292: {
8293: int i, m;
1.218 brouard 8294: int firstone=0;
8295:
1.136 brouard 8296: for (i=1; i<=imx; i++) {
8297: for(m=2; (m<= maxwav); m++) {
8298: if (((int)mint[m][i]== 99) && (s[m][i] <= nlstate)){
8299: anint[m][i]=9999;
1.216 brouard 8300: if (s[m][i] != -2) /* Keeping initial status of unknown vital status */
8301: s[m][i]=-1;
1.136 brouard 8302: }
8303: if((int)moisdc[i]==99 && (int)andc[i]==9999 && s[m][i]>nlstate){
1.169 brouard 8304: *nberr = *nberr + 1;
1.218 brouard 8305: if(firstone == 0){
8306: firstone=1;
8307: printf("Error! Date of death (month %2d and year %4d) of individual %ld on line %d was unknown, you must set an arbitrary year of death or he/she is skipped and results can be biased (%d) because status is a death state %d at wave %d. Wave dropped.\nOther similar cases in log file\n",(int)moisdc[i],(int)andc[i],num[i],i, *nberr,s[m][i],m);
8308: }
8309: fprintf(ficlog,"Error! Date of death (month %2d and year %4d) of individual %ld on line %d was unknown, you must set an arbitrary year of death or he/she is skipped and results can be biased (%d) because status is a death state %d at wave %d. Wave dropped.\n",(int)moisdc[i],(int)andc[i],num[i],i, *nberr,s[m][i],m);
1.136 brouard 8310: s[m][i]=-1;
8311: }
8312: if((int)moisdc[i]==99 && (int)andc[i]!=9999 && s[m][i]>nlstate){
1.169 brouard 8313: (*nberr)++;
1.136 brouard 8314: printf("Error! Month of death of individual %ld on line %d was unknown %2d, you should set it otherwise the information on the death is skipped and results are biased.\n",num[i],i,(int)moisdc[i]);
8315: fprintf(ficlog,"Error! Month of death of individual %ld on line %d was unknown %f, you should set it otherwise the information on the death is skipped and results are biased.\n",num[i],i,moisdc[i]);
8316: s[m][i]=-1; /* We prefer to skip it (and to skip it in version 0.8a1 too */
8317: }
8318: }
8319: }
8320:
8321: for (i=1; i<=imx; i++) {
8322: agedc[i]=(moisdc[i]/12.+andc[i])-(moisnais[i]/12.+annais[i]);
8323: for(m=firstpass; (m<= lastpass); m++){
1.214 brouard 8324: if(s[m][i] >0 || s[m][i]==-1 || s[m][i]==-2 || s[m][i]==-4 || s[m][i]==-5){ /* What if s[m][i]=-1 */
1.136 brouard 8325: if (s[m][i] >= nlstate+1) {
1.169 brouard 8326: if(agedc[i]>0){
8327: if((int)moisdc[i]!=99 && (int)andc[i]!=9999){
1.136 brouard 8328: agev[m][i]=agedc[i];
1.214 brouard 8329: /*if(moisdc[i]==99 && andc[i]==9999) s[m][i]=-1;*/
1.169 brouard 8330: }else {
1.136 brouard 8331: if ((int)andc[i]!=9999){
8332: nbwarn++;
8333: printf("Warning negative age at death: %ld line:%d\n",num[i],i);
8334: fprintf(ficlog,"Warning negative age at death: %ld line:%d\n",num[i],i);
8335: agev[m][i]=-1;
8336: }
8337: }
1.169 brouard 8338: } /* agedc > 0 */
1.214 brouard 8339: } /* end if */
1.136 brouard 8340: else if(s[m][i] !=9){ /* Standard case, age in fractional
8341: years but with the precision of a month */
8342: agev[m][i]=(mint[m][i]/12.+1./24.+anint[m][i])-(moisnais[i]/12.+1./24.+annais[i]);
8343: if((int)mint[m][i]==99 || (int)anint[m][i]==9999)
8344: agev[m][i]=1;
8345: else if(agev[m][i] < *agemin){
8346: *agemin=agev[m][i];
8347: printf(" Min anint[%d][%d]=%.2f annais[%d]=%.2f, agemin=%.2f\n",m,i,anint[m][i], i,annais[i], *agemin);
8348: }
8349: else if(agev[m][i] >*agemax){
8350: *agemax=agev[m][i];
1.156 brouard 8351: /* printf(" Max anint[%d][%d]=%.0f annais[%d]=%.0f, agemax=%.2f\n",m,i,anint[m][i], i,annais[i], *agemax);*/
1.136 brouard 8352: }
8353: /*agev[m][i]=anint[m][i]-annais[i];*/
8354: /* agev[m][i] = age[i]+2*m;*/
1.214 brouard 8355: } /* en if 9*/
1.136 brouard 8356: else { /* =9 */
1.214 brouard 8357: /* printf("Debug num[%d]=%ld s[%d][%d]=%d\n",i,num[i], m,i, s[m][i]); */
1.136 brouard 8358: agev[m][i]=1;
8359: s[m][i]=-1;
8360: }
8361: }
1.214 brouard 8362: else if(s[m][i]==0) /*= 0 Unknown */
1.136 brouard 8363: agev[m][i]=1;
1.214 brouard 8364: else{
8365: printf("Warning, num[%d]=%ld, s[%d][%d]=%d\n", i, num[i], m, i,s[m][i]);
8366: fprintf(ficlog, "Warning, num[%d]=%ld, s[%d][%d]=%d\n", i, num[i], m, i,s[m][i]);
8367: agev[m][i]=0;
8368: }
8369: } /* End for lastpass */
8370: }
1.136 brouard 8371:
8372: for (i=1; i<=imx; i++) {
8373: for(m=firstpass; (m<=lastpass); m++){
8374: if (s[m][i] > (nlstate+ndeath)) {
1.169 brouard 8375: (*nberr)++;
1.136 brouard 8376: 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);
8377: 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);
8378: return 1;
8379: }
8380: }
8381: }
8382:
8383: /*for (i=1; i<=imx; i++){
8384: for (m=firstpass; (m<lastpass); m++){
8385: printf("%ld %d %.lf %d %d\n", num[i],(covar[1][i]),agev[m][i],s[m][i],s[m+1][i]);
8386: }
8387:
8388: }*/
8389:
8390:
1.139 brouard 8391: printf("Total number of individuals= %d, Agemin = %.2f, Agemax= %.2f\n\n", imx, *agemin, *agemax);
8392: fprintf(ficlog,"Total number of individuals= %d, Agemin = %.2f, Agemax= %.2f\n\n", imx, *agemin, *agemax);
1.136 brouard 8393:
8394: return (0);
1.164 brouard 8395: /* endread:*/
1.136 brouard 8396: printf("Exiting calandcheckages: ");
8397: return (1);
8398: }
8399:
1.172 brouard 8400: #if defined(_MSC_VER)
8401: /*printf("Visual C++ compiler: %s \n;", _MSC_FULL_VER);*/
8402: /*fprintf(ficlog, "Visual C++ compiler: %s \n;", _MSC_FULL_VER);*/
8403: //#include "stdafx.h"
8404: //#include <stdio.h>
8405: //#include <tchar.h>
8406: //#include <windows.h>
8407: //#include <iostream>
8408: typedef BOOL(WINAPI *LPFN_ISWOW64PROCESS) (HANDLE, PBOOL);
8409:
8410: LPFN_ISWOW64PROCESS fnIsWow64Process;
8411:
8412: BOOL IsWow64()
8413: {
8414: BOOL bIsWow64 = FALSE;
8415:
8416: //typedef BOOL (APIENTRY *LPFN_ISWOW64PROCESS)
8417: // (HANDLE, PBOOL);
8418:
8419: //LPFN_ISWOW64PROCESS fnIsWow64Process;
8420:
8421: HMODULE module = GetModuleHandle(_T("kernel32"));
8422: const char funcName[] = "IsWow64Process";
8423: fnIsWow64Process = (LPFN_ISWOW64PROCESS)
8424: GetProcAddress(module, funcName);
8425:
8426: if (NULL != fnIsWow64Process)
8427: {
8428: if (!fnIsWow64Process(GetCurrentProcess(),
8429: &bIsWow64))
8430: //throw std::exception("Unknown error");
8431: printf("Unknown error\n");
8432: }
8433: return bIsWow64 != FALSE;
8434: }
8435: #endif
1.177 brouard 8436:
1.191 brouard 8437: void syscompilerinfo(int logged)
1.167 brouard 8438: {
8439: /* #include "syscompilerinfo.h"*/
1.185 brouard 8440: /* command line Intel compiler 32bit windows, XP compatible:*/
8441: /* /GS /W3 /Gy
8442: /Zc:wchar_t /Zi /O2 /Fd"Release\vc120.pdb" /D "WIN32" /D "NDEBUG" /D
8443: "_CONSOLE" /D "_LIB" /D "_USING_V110_SDK71_" /D "_UNICODE" /D
8444: "UNICODE" /Qipo /Zc:forScope /Gd /Oi /MT /Fa"Release\" /EHsc /nologo
1.186 brouard 8445: /Fo"Release\" /Qprof-dir "Release\" /Fp"Release\IMaCh.pch"
8446: */
8447: /* 64 bits */
1.185 brouard 8448: /*
8449: /GS /W3 /Gy
8450: /Zc:wchar_t /Zi /O2 /Fd"x64\Release\vc120.pdb" /D "WIN32" /D "NDEBUG"
8451: /D "_CONSOLE" /D "_LIB" /D "_UNICODE" /D "UNICODE" /Qipo /Zc:forScope
8452: /Oi /MD /Fa"x64\Release\" /EHsc /nologo /Fo"x64\Release\" /Qprof-dir
8453: "x64\Release\" /Fp"x64\Release\IMaCh.pch" */
8454: /* Optimization are useless and O3 is slower than O2 */
8455: /*
8456: /GS /W3 /Gy /Zc:wchar_t /Zi /O3 /Fd"x64\Release\vc120.pdb" /D "WIN32"
8457: /D "NDEBUG" /D "_CONSOLE" /D "_LIB" /D "_UNICODE" /D "UNICODE" /Qipo
8458: /Zc:forScope /Oi /MD /Fa"x64\Release\" /EHsc /nologo /Qparallel
8459: /Fo"x64\Release\" /Qprof-dir "x64\Release\" /Fp"x64\Release\IMaCh.pch"
8460: */
1.186 brouard 8461: /* Link is */ /* /OUT:"visual studio
1.185 brouard 8462: 2013\Projects\IMaCh\Release\IMaCh.exe" /MANIFEST /NXCOMPAT
8463: /PDB:"visual studio
8464: 2013\Projects\IMaCh\Release\IMaCh.pdb" /DYNAMICBASE
8465: "kernel32.lib" "user32.lib" "gdi32.lib" "winspool.lib"
8466: "comdlg32.lib" "advapi32.lib" "shell32.lib" "ole32.lib"
8467: "oleaut32.lib" "uuid.lib" "odbc32.lib" "odbccp32.lib"
8468: /MACHINE:X86 /OPT:REF /SAFESEH /INCREMENTAL:NO
8469: /SUBSYSTEM:CONSOLE",5.01" /MANIFESTUAC:"level='asInvoker'
8470: uiAccess='false'"
8471: /ManifestFile:"Release\IMaCh.exe.intermediate.manifest" /OPT:ICF
8472: /NOLOGO /TLBID:1
8473: */
1.177 brouard 8474: #if defined __INTEL_COMPILER
1.178 brouard 8475: #if defined(__GNUC__)
8476: struct utsname sysInfo; /* For Intel on Linux and OS/X */
8477: #endif
1.177 brouard 8478: #elif defined(__GNUC__)
1.179 brouard 8479: #ifndef __APPLE__
1.174 brouard 8480: #include <gnu/libc-version.h> /* Only on gnu */
1.179 brouard 8481: #endif
1.177 brouard 8482: struct utsname sysInfo;
1.178 brouard 8483: int cross = CROSS;
8484: if (cross){
8485: printf("Cross-");
1.191 brouard 8486: if(logged) fprintf(ficlog, "Cross-");
1.178 brouard 8487: }
1.174 brouard 8488: #endif
8489:
1.171 brouard 8490: #include <stdint.h>
1.178 brouard 8491:
1.191 brouard 8492: printf("Compiled with:");if(logged)fprintf(ficlog,"Compiled with:");
1.169 brouard 8493: #if defined(__clang__)
1.191 brouard 8494: printf(" Clang/LLVM");if(logged)fprintf(ficlog," Clang/LLVM"); /* Clang/LLVM. ---------------------------------------------- */
1.169 brouard 8495: #endif
8496: #if defined(__ICC) || defined(__INTEL_COMPILER)
1.191 brouard 8497: printf(" Intel ICC/ICPC");if(logged)fprintf(ficlog," Intel ICC/ICPC");/* Intel ICC/ICPC. ------------------------------------------ */
1.169 brouard 8498: #endif
8499: #if defined(__GNUC__) || defined(__GNUG__)
1.191 brouard 8500: printf(" GNU GCC/G++");if(logged)fprintf(ficlog," GNU GCC/G++");/* GNU GCC/G++. --------------------------------------------- */
1.169 brouard 8501: #endif
8502: #if defined(__HP_cc) || defined(__HP_aCC)
1.191 brouard 8503: printf(" Hewlett-Packard C/aC++");if(logged)fprintf(fcilog," Hewlett-Packard C/aC++"); /* Hewlett-Packard C/aC++. ---------------------------------- */
1.169 brouard 8504: #endif
8505: #if defined(__IBMC__) || defined(__IBMCPP__)
1.191 brouard 8506: printf(" IBM XL C/C++"); if(logged) fprintf(ficlog," IBM XL C/C++");/* IBM XL C/C++. -------------------------------------------- */
1.169 brouard 8507: #endif
8508: #if defined(_MSC_VER)
1.191 brouard 8509: printf(" Microsoft Visual Studio");if(logged)fprintf(ficlog," Microsoft Visual Studio");/* Microsoft Visual Studio. --------------------------------- */
1.169 brouard 8510: #endif
8511: #if defined(__PGI)
1.191 brouard 8512: printf(" Portland Group PGCC/PGCPP");if(logged) fprintf(ficlog," Portland Group PGCC/PGCPP");/* Portland Group PGCC/PGCPP. ------------------------------- */
1.169 brouard 8513: #endif
8514: #if defined(__SUNPRO_C) || defined(__SUNPRO_CC)
1.191 brouard 8515: printf(" Oracle Solaris Studio");if(logged)fprintf(ficlog," Oracle Solaris Studio\n");/* Oracle Solaris Studio. ----------------------------------- */
1.167 brouard 8516: #endif
1.191 brouard 8517: printf(" for "); if (logged) fprintf(ficlog, " for ");
1.169 brouard 8518:
1.167 brouard 8519: // http://stackoverflow.com/questions/4605842/how-to-identify-platform-compiler-from-preprocessor-macros
8520: #ifdef _WIN32 // note the underscore: without it, it's not msdn official!
8521: // Windows (x64 and x86)
1.191 brouard 8522: printf("Windows (x64 and x86) ");if(logged) fprintf(ficlog,"Windows (x64 and x86) ");
1.167 brouard 8523: #elif __unix__ // all unices, not all compilers
8524: // Unix
1.191 brouard 8525: printf("Unix ");if(logged) fprintf(ficlog,"Unix ");
1.167 brouard 8526: #elif __linux__
8527: // linux
1.191 brouard 8528: printf("linux ");if(logged) fprintf(ficlog,"linux ");
1.167 brouard 8529: #elif __APPLE__
1.174 brouard 8530: // Mac OS, not sure if this is covered by __posix__ and/or __unix__ though..
1.191 brouard 8531: printf("Mac OS ");if(logged) fprintf(ficlog,"Mac OS ");
1.167 brouard 8532: #endif
8533:
8534: /* __MINGW32__ */
8535: /* __CYGWIN__ */
8536: /* __MINGW64__ */
8537: // http://msdn.microsoft.com/en-us/library/b0084kay.aspx
8538: /* _MSC_VER //the Visual C++ compiler is 17.00.51106.1, the _MSC_VER macro evaluates to 1700. Type cl /? */
8539: /* _MSC_FULL_VER //the Visual C++ compiler is 15.00.20706.01, the _MSC_FULL_VER macro evaluates to 150020706 */
8540: /* _WIN64 // Defined for applications for Win64. */
8541: /* _M_X64 // Defined for compilations that target x64 processors. */
8542: /* _DEBUG // Defined when you compile with /LDd, /MDd, and /MTd. */
1.171 brouard 8543:
1.167 brouard 8544: #if UINTPTR_MAX == 0xffffffff
1.191 brouard 8545: printf(" 32-bit"); if(logged) fprintf(ficlog," 32-bit");/* 32-bit */
1.167 brouard 8546: #elif UINTPTR_MAX == 0xffffffffffffffff
1.191 brouard 8547: printf(" 64-bit"); if(logged) fprintf(ficlog," 64-bit");/* 64-bit */
1.167 brouard 8548: #else
1.191 brouard 8549: printf(" wtf-bit"); if(logged) fprintf(ficlog," wtf-bit");/* wtf */
1.167 brouard 8550: #endif
8551:
1.169 brouard 8552: #if defined(__GNUC__)
8553: # if defined(__GNUC_PATCHLEVEL__)
8554: # define __GNUC_VERSION__ (__GNUC__ * 10000 \
8555: + __GNUC_MINOR__ * 100 \
8556: + __GNUC_PATCHLEVEL__)
8557: # else
8558: # define __GNUC_VERSION__ (__GNUC__ * 10000 \
8559: + __GNUC_MINOR__ * 100)
8560: # endif
1.174 brouard 8561: printf(" using GNU C version %d.\n", __GNUC_VERSION__);
1.191 brouard 8562: if(logged) fprintf(ficlog, " using GNU C version %d.\n", __GNUC_VERSION__);
1.176 brouard 8563:
8564: if (uname(&sysInfo) != -1) {
8565: printf("Running on: %s %s %s %s %s\n",sysInfo.sysname, sysInfo.nodename, sysInfo.release, sysInfo.version, sysInfo.machine);
1.191 brouard 8566: if(logged) fprintf(ficlog,"Running on: %s %s %s %s %s\n ",sysInfo.sysname, sysInfo.nodename, sysInfo.release, sysInfo.version, sysInfo.machine);
1.176 brouard 8567: }
8568: else
8569: perror("uname() error");
1.179 brouard 8570: //#ifndef __INTEL_COMPILER
8571: #if !defined (__INTEL_COMPILER) && !defined(__APPLE__)
1.174 brouard 8572: printf("GNU libc version: %s\n", gnu_get_libc_version());
1.191 brouard 8573: if(logged) fprintf(ficlog,"GNU libc version: %s\n", gnu_get_libc_version());
1.177 brouard 8574: #endif
1.169 brouard 8575: #endif
1.172 brouard 8576:
8577: // void main()
8578: // {
1.169 brouard 8579: #if defined(_MSC_VER)
1.174 brouard 8580: if (IsWow64()){
1.191 brouard 8581: printf("\nThe program (probably compiled for 32bit) is running under WOW64 (64bit) emulation.\n");
8582: if (logged) fprintf(ficlog, "\nThe program (probably compiled for 32bit) is running under WOW64 (64bit) emulation.\n");
1.174 brouard 8583: }
8584: else{
1.191 brouard 8585: printf("\nThe program is not running under WOW64 (i.e probably on a 64bit Windows).\n");
8586: if (logged) fprintf(ficlog, "\nThe programm is not running under WOW64 (i.e probably on a 64bit Windows).\n");
1.174 brouard 8587: }
1.172 brouard 8588: // printf("\nPress Enter to continue...");
8589: // getchar();
8590: // }
8591:
1.169 brouard 8592: #endif
8593:
1.167 brouard 8594:
1.219 brouard 8595: }
1.136 brouard 8596:
1.219 brouard 8597: int prevalence_limit(double *p, double **prlim, double ageminpar, double agemaxpar, double ftolpl, int *ncvyearp){
1.180 brouard 8598: /*--------------- Prevalence limit (period or stable prevalence) --------------*/
8599: int i, j, k, i1 ;
1.202 brouard 8600: /* double ftolpl = 1.e-10; */
1.180 brouard 8601: double age, agebase, agelim;
1.203 brouard 8602: double tot;
1.180 brouard 8603:
1.202 brouard 8604: strcpy(filerespl,"PL_");
8605: strcat(filerespl,fileresu);
8606: if((ficrespl=fopen(filerespl,"w"))==NULL) {
8607: printf("Problem with period (stable) prevalence resultfile: %s\n", filerespl);return 1;
8608: fprintf(ficlog,"Problem with period (stable) prevalence resultfile: %s\n", filerespl);return 1;
8609: }
1.227 brouard 8610: printf("\nComputing period (stable) prevalence: result on file '%s' \n", filerespl);
8611: fprintf(ficlog,"\nComputing period (stable) prevalence: result on file '%s' \n", filerespl);
1.202 brouard 8612: pstamp(ficrespl);
1.203 brouard 8613: fprintf(ficrespl,"# Period (stable) prevalence. Precision given by ftolpl=%g \n", ftolpl);
1.202 brouard 8614: fprintf(ficrespl,"#Age ");
8615: for(i=1; i<=nlstate;i++) fprintf(ficrespl,"%d-%d ",i,i);
8616: fprintf(ficrespl,"\n");
1.180 brouard 8617:
1.219 brouard 8618: /* prlim=matrix(1,nlstate,1,nlstate);*/ /* back in main */
1.180 brouard 8619:
1.219 brouard 8620: agebase=ageminpar;
8621: agelim=agemaxpar;
1.180 brouard 8622:
1.227 brouard 8623: /* i1=pow(2,ncoveff); */
8624: i1=pow(2,cptcoveff); /* Number of dummy covariates */
1.219 brouard 8625: if (cptcovn < 1){i1=1;}
1.180 brouard 8626:
1.220 brouard 8627: for(k=1; k<=i1;k++){
8628: /* for(cptcov=1,k=0;cptcov<=i1;cptcov++){ */
1.180 brouard 8629: /* for(cptcov=1,k=0;cptcov<=1;cptcov++){ */
1.219 brouard 8630: //for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){
1.220 brouard 8631: /* k=k+1; */
1.219 brouard 8632: /* to clean */
8633: //printf("cptcov=%d cptcod=%d codtab=%d\n",cptcov, cptcod,codtabm(cptcod,cptcov));
8634: fprintf(ficrespl,"#******");
8635: printf("#******");
8636: fprintf(ficlog,"#******");
1.227 brouard 8637: for(j=1;j<=cptcoveff ;j++) {/* all covariates */
8638: fprintf(ficrespl," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]); /* Here problem for varying dummy*/
1.219 brouard 8639: printf(" V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
8640: fprintf(ficlog," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
8641: }
8642: fprintf(ficrespl,"******\n");
8643: printf("******\n");
8644: fprintf(ficlog,"******\n");
1.227 brouard 8645: if(invalidvarcomb[k]){
8646: printf("\nCombination (%d) ignored because no case \n",k);
8647: fprintf(ficrespl,"#Combination (%d) ignored because no case \n",k);
8648: fprintf(ficlog,"\nCombination (%d) ignored because no case \n",k);
1.220 brouard 8649: continue;
1.227 brouard 8650: }
1.219 brouard 8651:
8652: fprintf(ficrespl,"#Age ");
1.227 brouard 8653: for(j=1;j<=cptcoveff;j++) {
1.219 brouard 8654: fprintf(ficrespl,"V%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
8655: }
8656: for(i=1; i<=nlstate;i++) fprintf(ficrespl," %d-%d ",i,i);
8657: fprintf(ficrespl,"Total Years_to_converge\n");
1.227 brouard 8658:
1.219 brouard 8659: for (age=agebase; age<=agelim; age++){
8660: /* for (age=agebase; age<=agebase; age++){ */
8661: prevalim(prlim, nlstate, p, age, oldm, savm, ftolpl, ncvyearp, k);
8662: fprintf(ficrespl,"%.0f ",age );
1.227 brouard 8663: for(j=1;j<=cptcoveff;j++)
8664: fprintf(ficrespl,"%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
1.219 brouard 8665: tot=0.;
8666: for(i=1; i<=nlstate;i++){
1.227 brouard 8667: tot += prlim[i][i];
8668: fprintf(ficrespl," %.5f", prlim[i][i]);
1.219 brouard 8669: }
8670: fprintf(ficrespl," %.3f %d\n", tot, *ncvyearp);
8671: } /* Age */
8672: /* was end of cptcod */
8673: } /* cptcov */
8674: return 0;
1.180 brouard 8675: }
8676:
1.218 brouard 8677: 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){
8678: /*--------------- Back Prevalence limit (period or stable prevalence) --------------*/
8679:
8680: /* Computes the back prevalence limit for any combination of covariate values
8681: * at any age between ageminpar and agemaxpar
8682: */
1.217 brouard 8683: int i, j, k, i1 ;
8684: /* double ftolpl = 1.e-10; */
8685: double age, agebase, agelim;
8686: double tot;
1.218 brouard 8687: /* double ***mobaverage; */
8688: /* double **dnewm, **doldm, **dsavm; /\* for use *\/ */
1.217 brouard 8689:
8690: strcpy(fileresplb,"PLB_");
8691: strcat(fileresplb,fileresu);
8692: if((ficresplb=fopen(fileresplb,"w"))==NULL) {
8693: printf("Problem with period (stable) back prevalence resultfile: %s\n", fileresplb);return 1;
8694: fprintf(ficlog,"Problem with period (stable) back prevalence resultfile: %s\n", fileresplb);return 1;
8695: }
8696: printf("Computing period (stable) back prevalence: result on file '%s' \n", fileresplb);
8697: fprintf(ficlog,"Computing period (stable) back prevalence: result on file '%s' \n", fileresplb);
8698: pstamp(ficresplb);
8699: fprintf(ficresplb,"# Period (stable) back prevalence. Precision given by ftolpl=%g \n", ftolpl);
8700: fprintf(ficresplb,"#Age ");
8701: for(i=1; i<=nlstate;i++) fprintf(ficresplb,"%d-%d ",i,i);
8702: fprintf(ficresplb,"\n");
8703:
1.218 brouard 8704:
8705: /* prlim=matrix(1,nlstate,1,nlstate);*/ /* back in main */
8706:
8707: agebase=ageminpar;
8708: agelim=agemaxpar;
8709:
8710:
1.227 brouard 8711: i1=pow(2,cptcoveff);
1.218 brouard 8712: if (cptcovn < 1){i1=1;}
1.227 brouard 8713:
8714: for(k=1; k<=i1;k++){
1.218 brouard 8715: //printf("cptcov=%d cptcod=%d codtab=%d\n",cptcov, cptcod,codtabm(cptcod,cptcov));
8716: fprintf(ficresplb,"#******");
8717: printf("#******");
8718: fprintf(ficlog,"#******");
1.227 brouard 8719: for(j=1;j<=cptcoveff ;j++) {/* all covariates */
1.218 brouard 8720: fprintf(ficresplb," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
8721: printf(" V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
8722: fprintf(ficlog," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
8723: }
8724: fprintf(ficresplb,"******\n");
8725: printf("******\n");
8726: fprintf(ficlog,"******\n");
1.227 brouard 8727: if(invalidvarcomb[k]){
8728: printf("\nCombination (%d) ignored because no cases \n",k);
8729: fprintf(ficresplb,"#Combination (%d) ignored because no cases \n",k);
8730: fprintf(ficlog,"\nCombination (%d) ignored because no cases \n",k);
8731: continue;
8732: }
1.218 brouard 8733:
8734: fprintf(ficresplb,"#Age ");
1.227 brouard 8735: for(j=1;j<=cptcoveff;j++) {
1.218 brouard 8736: fprintf(ficresplb,"V%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
8737: }
8738: for(i=1; i<=nlstate;i++) fprintf(ficresplb," %d-%d ",i,i);
8739: fprintf(ficresplb,"Total Years_to_converge\n");
8740:
8741:
8742: for (age=agebase; age<=agelim; age++){
8743: /* for (age=agebase; age<=agebase; age++){ */
8744: if(mobilavproj > 0){
8745: /* bprevalim(bprlim, mobaverage, nlstate, p, age, ageminpar, agemaxpar, oldm, savm, doldm, dsavm, ftolpl, ncvyearp, k); */
8746: /* bprevalim(bprlim, mobaverage, nlstate, p, age, oldm, savm, dnewm, doldm, dsavm, ftolpl, ncvyearp, k); */
1.227 brouard 8747: bprevalim(bprlim, mobaverage, nlstate, p, age, ftolpl, ncvyearp, k);
1.218 brouard 8748: }else if (mobilavproj == 0){
1.227 brouard 8749: 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);
8750: 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);
8751: exit(1);
1.218 brouard 8752: }else{
1.227 brouard 8753: /* bprevalim(bprlim, probs, nlstate, p, age, oldm, savm, dnewm, doldm, dsavm, ftolpl, ncvyearp, k); */
8754: bprevalim(bprlim, probs, nlstate, p, age, ftolpl, ncvyearp, k);
1.218 brouard 8755: }
8756: fprintf(ficresplb,"%.0f ",age );
1.227 brouard 8757: for(j=1;j<=cptcoveff;j++)
8758: fprintf(ficresplb,"%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
1.218 brouard 8759: tot=0.;
8760: for(i=1; i<=nlstate;i++){
1.227 brouard 8761: tot += bprlim[i][i];
8762: fprintf(ficresplb," %.5f", bprlim[i][i]);
1.218 brouard 8763: }
8764: fprintf(ficresplb," %.3f %d\n", tot, *ncvyearp);
8765: } /* Age */
8766: /* was end of cptcod */
8767: } /* cptcov */
8768:
8769: /* hBijx(p, bage, fage); */
8770: /* fclose(ficrespijb); */
8771:
8772: return 0;
1.217 brouard 8773: }
1.218 brouard 8774:
1.180 brouard 8775: int hPijx(double *p, int bage, int fage){
8776: /*------------- h Pij x at various ages ------------*/
8777:
8778: int stepsize;
8779: int agelim;
8780: int hstepm;
8781: int nhstepm;
8782: int h, i, i1, j, k;
8783:
8784: double agedeb;
8785: double ***p3mat;
8786:
1.201 brouard 8787: strcpy(filerespij,"PIJ_"); strcat(filerespij,fileresu);
1.180 brouard 8788: if((ficrespij=fopen(filerespij,"w"))==NULL) {
8789: printf("Problem with Pij resultfile: %s\n", filerespij); return 1;
8790: fprintf(ficlog,"Problem with Pij resultfile: %s\n", filerespij); return 1;
8791: }
8792: printf("Computing pij: result on file '%s' \n", filerespij);
8793: fprintf(ficlog,"Computing pij: result on file '%s' \n", filerespij);
8794:
8795: stepsize=(int) (stepm+YEARM-1)/YEARM;
8796: /*if (stepm<=24) stepsize=2;*/
8797:
8798: agelim=AGESUP;
8799: hstepm=stepsize*YEARM; /* Every year of age */
8800: hstepm=hstepm/stepm; /* Typically 2 years, = 2/6 months = 4 */
1.218 brouard 8801:
1.180 brouard 8802: /* hstepm=1; aff par mois*/
8803: pstamp(ficrespij);
8804: fprintf(ficrespij,"#****** h Pij x Probability to be in state j at age x+h being in i at x ");
1.227 brouard 8805: i1= pow(2,cptcoveff);
1.218 brouard 8806: /* for(cptcov=1,k=0;cptcov<=i1;cptcov++){ */
8807: /* /\*for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){*\/ */
8808: /* k=k+1; */
1.227 brouard 8809: for (k=1; k <= (int) pow(2,cptcoveff); k++){
1.183 brouard 8810: fprintf(ficrespij,"\n#****** ");
1.227 brouard 8811: for(j=1;j<=cptcoveff;j++)
1.198 brouard 8812: fprintf(ficrespij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
1.183 brouard 8813: fprintf(ficrespij,"******\n");
8814:
8815: for (agedeb=fage; agedeb>=bage; agedeb--){ /* If stepm=6 months */
8816: nhstepm=(int) rint((agelim-agedeb)*YEARM/stepm); /* Typically 20 years = 20*12/6=40 */
8817: nhstepm = nhstepm/hstepm; /* Typically 40/4=10 */
8818:
8819: /* nhstepm=nhstepm*YEARM; aff par mois*/
1.180 brouard 8820:
1.183 brouard 8821: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
8822: oldm=oldms;savm=savms;
8823: hpxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k);
8824: fprintf(ficrespij,"# Cov Agex agex+h hpijx with i,j=");
8825: for(i=1; i<=nlstate;i++)
8826: for(j=1; j<=nlstate+ndeath;j++)
8827: fprintf(ficrespij," %1d-%1d",i,j);
8828: fprintf(ficrespij,"\n");
8829: for (h=0; h<=nhstepm; h++){
8830: /*agedebphstep = agedeb + h*hstepm/YEARM*stepm;*/
8831: fprintf(ficrespij,"%d %3.f %3.f",k, agedeb, agedeb + h*hstepm/YEARM*stepm );
1.180 brouard 8832: for(i=1; i<=nlstate;i++)
8833: for(j=1; j<=nlstate+ndeath;j++)
1.183 brouard 8834: fprintf(ficrespij," %.5f", p3mat[i][j][h]);
1.180 brouard 8835: fprintf(ficrespij,"\n");
8836: }
1.183 brouard 8837: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
8838: fprintf(ficrespij,"\n");
8839: }
1.180 brouard 8840: /*}*/
8841: }
1.218 brouard 8842: return 0;
1.180 brouard 8843: }
1.218 brouard 8844:
8845: int hBijx(double *p, int bage, int fage, double ***prevacurrent){
1.217 brouard 8846: /*------------- h Bij x at various ages ------------*/
8847:
8848: int stepsize;
1.218 brouard 8849: /* int agelim; */
8850: int ageminl;
1.217 brouard 8851: int hstepm;
8852: int nhstepm;
8853: int h, i, i1, j, k;
1.218 brouard 8854:
1.217 brouard 8855: double agedeb;
8856: double ***p3mat;
1.218 brouard 8857:
8858: strcpy(filerespijb,"PIJB_"); strcat(filerespijb,fileresu);
8859: if((ficrespijb=fopen(filerespijb,"w"))==NULL) {
8860: printf("Problem with Pij back resultfile: %s\n", filerespijb); return 1;
8861: fprintf(ficlog,"Problem with Pij back resultfile: %s\n", filerespijb); return 1;
8862: }
8863: printf("Computing pij back: result on file '%s' \n", filerespijb);
8864: fprintf(ficlog,"Computing pij back: result on file '%s' \n", filerespijb);
8865:
8866: stepsize=(int) (stepm+YEARM-1)/YEARM;
8867: /*if (stepm<=24) stepsize=2;*/
1.217 brouard 8868:
1.218 brouard 8869: /* agelim=AGESUP; */
8870: ageminl=30;
8871: hstepm=stepsize*YEARM; /* Every year of age */
8872: hstepm=hstepm/stepm; /* Typically 2 years, = 2/6 months = 4 */
8873:
8874: /* hstepm=1; aff par mois*/
8875: pstamp(ficrespijb);
8876: fprintf(ficrespijb,"#****** h Pij x Back Probability to be in state i at age x-h being in j at x ");
1.227 brouard 8877: i1= pow(2,cptcoveff);
1.218 brouard 8878: /* for(cptcov=1,k=0;cptcov<=i1;cptcov++){ */
8879: /* /\*for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){*\/ */
8880: /* k=k+1; */
1.227 brouard 8881: for (k=1; k <= (int) pow(2,cptcoveff); k++){
1.218 brouard 8882: fprintf(ficrespijb,"\n#****** ");
1.227 brouard 8883: for(j=1;j<=cptcoveff;j++)
1.218 brouard 8884: fprintf(ficrespijb,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
8885: fprintf(ficrespijb,"******\n");
1.222 brouard 8886: if(invalidvarcomb[k]){
8887: fprintf(ficrespijb,"\n#Combination (%d) ignored because no cases \n",k);
8888: continue;
8889: }
1.218 brouard 8890:
8891: /* for (agedeb=fage; agedeb>=bage; agedeb--){ /\* If stepm=6 months *\/ */
8892: for (agedeb=bage; agedeb<=fage; agedeb++){ /* If stepm=6 months and estepm=24 (2 years) */
8893: /* nhstepm=(int) rint((agelim-agedeb)*YEARM/stepm); /\* Typically 20 years = 20*12/6=40 *\/ */
8894: nhstepm=(int) rint((agedeb-ageminl)*YEARM/stepm); /* Typically 20 years = 20*12/6=40 */
8895: nhstepm = nhstepm/hstepm; /* Typically 40/4=10, because estepm=24 stepm=6 => hstepm=24/6=4 */
8896:
8897: /* nhstepm=nhstepm*YEARM; aff par mois*/
8898:
8899: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
8900: /* oldm=oldms;savm=savms; */
8901: /* hbxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k); */
8902: hbxij(p3mat,nhstepm,agedeb,hstepm,p,prevacurrent,nlstate,stepm, k);
8903: /* hbxij(p3mat,nhstepm,agedeb,hstepm,p,prevacurrent,nlstate,stepm,oldm,savm, dnewm, doldm, dsavm, k); */
8904: fprintf(ficrespijb,"# Cov Agex agex-h hpijx with i,j=");
8905: for(i=1; i<=nlstate;i++)
8906: for(j=1; j<=nlstate+ndeath;j++)
8907: fprintf(ficrespijb," %1d-%1d",i,j);
8908: fprintf(ficrespijb,"\n");
8909: for (h=0; h<=nhstepm; h++){
8910: /*agedebphstep = agedeb + h*hstepm/YEARM*stepm;*/
8911: fprintf(ficrespijb,"%d %3.f %3.f",k, agedeb, agedeb - h*hstepm/YEARM*stepm );
8912: /* fprintf(ficrespijb,"%d %3.f %3.f",k, agedeb, agedeb + h*hstepm/YEARM*stepm ); */
1.217 brouard 8913: for(i=1; i<=nlstate;i++)
8914: for(j=1; j<=nlstate+ndeath;j++)
1.218 brouard 8915: fprintf(ficrespijb," %.5f", p3mat[i][j][h]);
1.217 brouard 8916: fprintf(ficrespijb,"\n");
8917: }
1.218 brouard 8918: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
8919: fprintf(ficrespijb,"\n");
1.217 brouard 8920: }
1.218 brouard 8921: /*}*/
8922: }
8923: return 0;
8924: } /* hBijx */
1.217 brouard 8925:
1.180 brouard 8926:
1.136 brouard 8927: /***********************************************/
8928: /**************** Main Program *****************/
8929: /***********************************************/
8930:
8931: int main(int argc, char *argv[])
8932: {
8933: #ifdef GSL
8934: const gsl_multimin_fminimizer_type *T;
8935: size_t iteri = 0, it;
8936: int rval = GSL_CONTINUE;
8937: int status = GSL_SUCCESS;
8938: double ssval;
8939: #endif
8940: int movingaverage(double ***probs, double bage,double fage, double ***mobaverage, int mobilav);
1.164 brouard 8941: int i,j, k, n=MAXN,iter=0,m,size=100, cptcod;
1.209 brouard 8942: int ncvyear=0; /* Number of years needed for the period prevalence to converge */
1.164 brouard 8943: int jj, ll, li, lj, lk;
1.136 brouard 8944: int numlinepar=0; /* Current linenumber of parameter file */
1.197 brouard 8945: int num_filled;
1.136 brouard 8946: int itimes;
8947: int NDIM=2;
8948: int vpopbased=0;
8949:
1.164 brouard 8950: char ca[32], cb[32];
1.136 brouard 8951: /* FILE *fichtm; *//* Html File */
8952: /* FILE *ficgp;*/ /*Gnuplot File */
8953: struct stat info;
1.191 brouard 8954: double agedeb=0.;
1.194 brouard 8955:
8956: double ageminpar=AGEOVERFLOW,agemin=AGEOVERFLOW, agemaxpar=-AGEOVERFLOW, agemax=-AGEOVERFLOW;
1.219 brouard 8957: double ageminout=-AGEOVERFLOW,agemaxout=AGEOVERFLOW; /* Smaller Age range redefined after movingaverage */
1.136 brouard 8958:
1.165 brouard 8959: double fret;
1.191 brouard 8960: double dum=0.; /* Dummy variable */
1.136 brouard 8961: double ***p3mat;
1.218 brouard 8962: /* double ***mobaverage; */
1.164 brouard 8963:
8964: char line[MAXLINE];
1.197 brouard 8965: char path[MAXLINE],pathc[MAXLINE],pathcd[MAXLINE],pathtot[MAXLINE];
8966:
8967: char model[MAXLINE], modeltemp[MAXLINE];
1.230 brouard 8968: char resultline[MAXLINE];
8969:
1.136 brouard 8970: char pathr[MAXLINE], pathimach[MAXLINE];
1.164 brouard 8971: char *tok, *val; /* pathtot */
1.136 brouard 8972: int firstobs=1, lastobs=10;
1.195 brouard 8973: int c, h , cpt, c2;
1.191 brouard 8974: int jl=0;
8975: int i1, j1, jk, stepsize=0;
1.194 brouard 8976: int count=0;
8977:
1.164 brouard 8978: int *tab;
1.136 brouard 8979: int mobilavproj=0 , prevfcast=0 ; /* moving average of prev, If prevfcast=1 prevalence projection */
1.217 brouard 8980: int backcast=0;
1.136 brouard 8981: int mobilav=0,popforecast=0;
1.191 brouard 8982: int hstepm=0, nhstepm=0;
1.136 brouard 8983: int agemortsup;
8984: float sumlpop=0.;
8985: double jprev1=1, mprev1=1,anprev1=2000,jprev2=1, mprev2=1,anprev2=2000;
8986: double jpyram=1, mpyram=1,anpyram=2000,jpyram1=1, mpyram1=1,anpyram1=2000;
8987:
1.191 brouard 8988: double bage=0, fage=110., age, agelim=0., agebase=0.;
1.136 brouard 8989: double ftolpl=FTOL;
8990: double **prlim;
1.217 brouard 8991: double **bprlim;
1.136 brouard 8992: double ***param; /* Matrix of parameters */
8993: double *p;
8994: double **matcov; /* Matrix of covariance */
1.203 brouard 8995: double **hess; /* Hessian matrix */
1.136 brouard 8996: double ***delti3; /* Scale */
8997: double *delti; /* Scale */
8998: double ***eij, ***vareij;
8999: double **varpl; /* Variances of prevalence limits by age */
9000: double *epj, vepp;
1.164 brouard 9001:
1.136 brouard 9002: double dateprev1, dateprev2,jproj1=1,mproj1=1,anproj1=2000,jproj2=1,mproj2=1,anproj2=2000;
1.217 brouard 9003: double jback1=1,mback1=1,anback1=2000,jback2=1,mback2=1,anback2=2000;
9004:
1.136 brouard 9005: double **ximort;
1.145 brouard 9006: char *alph[]={"a","a","b","c","d","e"}, str[4]="1234";
1.136 brouard 9007: int *dcwave;
9008:
1.164 brouard 9009: char z[1]="c";
1.136 brouard 9010:
9011: /*char *strt;*/
9012: char strtend[80];
1.126 brouard 9013:
1.164 brouard 9014:
1.126 brouard 9015: /* setlocale (LC_ALL, ""); */
9016: /* bindtextdomain (PACKAGE, LOCALEDIR); */
9017: /* textdomain (PACKAGE); */
9018: /* setlocale (LC_CTYPE, ""); */
9019: /* setlocale (LC_MESSAGES, ""); */
9020:
9021: /* gettimeofday(&start_time, (struct timezone*)0); */ /* at first time */
1.157 brouard 9022: rstart_time = time(NULL);
9023: /* (void) gettimeofday(&start_time,&tzp);*/
9024: start_time = *localtime(&rstart_time);
1.126 brouard 9025: curr_time=start_time;
1.157 brouard 9026: /*tml = *localtime(&start_time.tm_sec);*/
9027: /* strcpy(strstart,asctime(&tml)); */
9028: strcpy(strstart,asctime(&start_time));
1.126 brouard 9029:
9030: /* printf("Localtime (at start)=%s",strstart); */
1.157 brouard 9031: /* tp.tm_sec = tp.tm_sec +86400; */
9032: /* tm = *localtime(&start_time.tm_sec); */
1.126 brouard 9033: /* tmg.tm_year=tmg.tm_year +dsign*dyear; */
9034: /* tmg.tm_mon=tmg.tm_mon +dsign*dmonth; */
9035: /* tmg.tm_hour=tmg.tm_hour + 1; */
1.157 brouard 9036: /* tp.tm_sec = mktime(&tmg); */
1.126 brouard 9037: /* strt=asctime(&tmg); */
9038: /* printf("Time(after) =%s",strstart); */
9039: /* (void) time (&time_value);
9040: * printf("time=%d,t-=%d\n",time_value,time_value-86400);
9041: * tm = *localtime(&time_value);
9042: * strstart=asctime(&tm);
9043: * printf("tim_value=%d,asctime=%s\n",time_value,strstart);
9044: */
9045:
9046: nberr=0; /* Number of errors and warnings */
9047: nbwarn=0;
1.184 brouard 9048: #ifdef WIN32
9049: _getcwd(pathcd, size);
9050: #else
1.126 brouard 9051: getcwd(pathcd, size);
1.184 brouard 9052: #endif
1.191 brouard 9053: syscompilerinfo(0);
1.196 brouard 9054: printf("\nIMaCh version %s, %s\n%s",version, copyright, fullversion);
1.126 brouard 9055: if(argc <=1){
9056: printf("\nEnter the parameter file name: ");
1.205 brouard 9057: if(!fgets(pathr,FILENAMELENGTH,stdin)){
9058: printf("ERROR Empty parameter file name\n");
9059: goto end;
9060: }
1.126 brouard 9061: i=strlen(pathr);
9062: if(pathr[i-1]=='\n')
9063: pathr[i-1]='\0';
1.156 brouard 9064: i=strlen(pathr);
1.205 brouard 9065: if(i >= 1 && pathr[i-1]==' ') {/* This may happen when dragging on oS/X! */
1.156 brouard 9066: pathr[i-1]='\0';
1.205 brouard 9067: }
9068: i=strlen(pathr);
9069: if( i==0 ){
9070: printf("ERROR Empty parameter file name\n");
9071: goto end;
9072: }
9073: for (tok = pathr; tok != NULL; ){
1.126 brouard 9074: printf("Pathr |%s|\n",pathr);
9075: while ((val = strsep(&tok, "\"" )) != NULL && *val == '\0');
9076: printf("val= |%s| pathr=%s\n",val,pathr);
9077: strcpy (pathtot, val);
9078: if(pathr[0] == '\0') break; /* Dirty */
9079: }
9080: }
9081: else{
9082: strcpy(pathtot,argv[1]);
9083: }
9084: /*if(getcwd(pathcd, MAXLINE)!= NULL)printf ("Error pathcd\n");*/
9085: /*cygwin_split_path(pathtot,path,optionfile);
9086: printf("pathtot=%s, path=%s, optionfile=%s\n",pathtot,path,optionfile);*/
9087: /* cutv(path,optionfile,pathtot,'\\');*/
9088:
9089: /* Split argv[0], imach program to get pathimach */
9090: printf("\nargv[0]=%s argv[1]=%s, \n",argv[0],argv[1]);
9091: split(argv[0],pathimach,optionfile,optionfilext,optionfilefiname);
9092: printf("\nargv[0]=%s pathimach=%s, \noptionfile=%s \noptionfilext=%s \noptionfilefiname=%s\n",argv[0],pathimach,optionfile,optionfilext,optionfilefiname);
9093: /* strcpy(pathimach,argv[0]); */
9094: /* Split argv[1]=pathtot, parameter file name to get path, optionfile, extension and name */
9095: split(pathtot,path,optionfile,optionfilext,optionfilefiname);
9096: printf("\npathtot=%s,\npath=%s,\noptionfile=%s \noptionfilext=%s \noptionfilefiname=%s\n",pathtot,path,optionfile,optionfilext,optionfilefiname);
1.184 brouard 9097: #ifdef WIN32
9098: _chdir(path); /* Can be a relative path */
9099: if(_getcwd(pathcd,MAXLINE) > 0) /* So pathcd is the full path */
9100: #else
1.126 brouard 9101: chdir(path); /* Can be a relative path */
1.184 brouard 9102: if (getcwd(pathcd, MAXLINE) > 0) /* So pathcd is the full path */
9103: #endif
9104: printf("Current directory %s!\n",pathcd);
1.126 brouard 9105: strcpy(command,"mkdir ");
9106: strcat(command,optionfilefiname);
9107: if((outcmd=system(command)) != 0){
1.169 brouard 9108: printf("Directory already exists (or can't create it) %s%s, err=%d\n",path,optionfilefiname,outcmd);
1.126 brouard 9109: /* fprintf(ficlog,"Problem creating directory %s%s\n",path,optionfilefiname); */
9110: /* fclose(ficlog); */
9111: /* exit(1); */
9112: }
9113: /* if((imk=mkdir(optionfilefiname))<0){ */
9114: /* perror("mkdir"); */
9115: /* } */
9116:
9117: /*-------- arguments in the command line --------*/
9118:
1.186 brouard 9119: /* Main Log file */
1.126 brouard 9120: strcat(filelog, optionfilefiname);
9121: strcat(filelog,".log"); /* */
9122: if((ficlog=fopen(filelog,"w"))==NULL) {
9123: printf("Problem with logfile %s\n",filelog);
9124: goto end;
9125: }
9126: fprintf(ficlog,"Log filename:%s\n",filelog);
1.197 brouard 9127: fprintf(ficlog,"Version %s %s",version,fullversion);
1.126 brouard 9128: fprintf(ficlog,"\nEnter the parameter file name: \n");
9129: fprintf(ficlog,"pathimach=%s\npathtot=%s\n\
9130: path=%s \n\
9131: optionfile=%s\n\
9132: optionfilext=%s\n\
1.156 brouard 9133: optionfilefiname='%s'\n",pathimach,pathtot,path,optionfile,optionfilext,optionfilefiname);
1.126 brouard 9134:
1.197 brouard 9135: syscompilerinfo(1);
1.167 brouard 9136:
1.126 brouard 9137: printf("Local time (at start):%s",strstart);
9138: fprintf(ficlog,"Local time (at start): %s",strstart);
9139: fflush(ficlog);
9140: /* (void) gettimeofday(&curr_time,&tzp); */
1.157 brouard 9141: /* printf("Elapsed time %d\n", asc_diff_time(curr_time.tm_sec-start_time.tm_sec,tmpout)); */
1.126 brouard 9142:
9143: /* */
9144: strcpy(fileres,"r");
9145: strcat(fileres, optionfilefiname);
1.201 brouard 9146: strcat(fileresu, optionfilefiname); /* Without r in front */
1.126 brouard 9147: strcat(fileres,".txt"); /* Other files have txt extension */
1.201 brouard 9148: strcat(fileresu,".txt"); /* Other files have txt extension */
1.126 brouard 9149:
1.186 brouard 9150: /* Main ---------arguments file --------*/
1.126 brouard 9151:
9152: if((ficpar=fopen(optionfile,"r"))==NULL) {
1.155 brouard 9153: printf("Problem with optionfile '%s' with errno='%s'\n",optionfile,strerror(errno));
9154: fprintf(ficlog,"Problem with optionfile '%s' with errno='%s'\n",optionfile,strerror(errno));
1.126 brouard 9155: fflush(ficlog);
1.149 brouard 9156: /* goto end; */
9157: exit(70);
1.126 brouard 9158: }
9159:
9160:
9161:
9162: strcpy(filereso,"o");
1.201 brouard 9163: strcat(filereso,fileresu);
1.126 brouard 9164: if((ficparo=fopen(filereso,"w"))==NULL) { /* opened on subdirectory */
9165: printf("Problem with Output resultfile: %s\n", filereso);
9166: fprintf(ficlog,"Problem with Output resultfile: %s\n", filereso);
9167: fflush(ficlog);
9168: goto end;
9169: }
9170:
9171: /* Reads comments: lines beginning with '#' */
9172: numlinepar=0;
1.197 brouard 9173:
9174: /* First parameter line */
9175: while(fgets(line, MAXLINE, ficpar)) {
9176: /* If line starts with a # it is a comment */
9177: if (line[0] == '#') {
9178: numlinepar++;
9179: fputs(line,stdout);
9180: fputs(line,ficparo);
9181: fputs(line,ficlog);
9182: continue;
9183: }else
9184: break;
9185: }
9186: if((num_filled=sscanf(line,"title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\n", \
9187: title, datafile, &lastobs, &firstpass,&lastpass)) !=EOF){
9188: if (num_filled != 5) {
9189: printf("Should be 5 parameters\n");
9190: }
1.126 brouard 9191: numlinepar++;
1.197 brouard 9192: printf("title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\n", title, datafile, lastobs, firstpass,lastpass);
9193: }
9194: /* Second parameter line */
9195: while(fgets(line, MAXLINE, ficpar)) {
9196: /* If line starts with a # it is a comment */
9197: if (line[0] == '#') {
9198: numlinepar++;
9199: fputs(line,stdout);
9200: fputs(line,ficparo);
9201: fputs(line,ficlog);
9202: continue;
9203: }else
9204: break;
9205: }
1.223 brouard 9206: 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", \
9207: &ftol, &stepm, &ncovcol, &nqv, &ntv, &nqtv, &nlstate, &ndeath, &maxwav, &mle, &weightopt)) !=EOF){
9208: if (num_filled != 11) {
9209: printf("Not 11 parameters, for example:ftol=1.e-8 stepm=12 ncovcol=2 nqv=1 ntv=2 nqtv=1 nlstate=2 ndeath=1 maxwav=3 mle=1 weight=1\n");
1.209 brouard 9210: printf("but line=%s\n",line);
1.197 brouard 9211: }
1.223 brouard 9212: printf("ftol=%e stepm=%d ncovcol=%d nqv=%d ntv=%d nqtv=%d nlstate=%d ndeath=%d maxwav=%d mle=%d weight=%d\n",ftol, stepm, ncovcol, nqv, ntv, nqtv, nlstate, ndeath, maxwav, mle, weightopt);
1.126 brouard 9213: }
1.203 brouard 9214: /* ftolpl=6*ftol*1.e5; /\* 6.e-3 make convergences in less than 80 loops for the prevalence limit *\/ */
1.209 brouard 9215: /*ftolpl=6.e-4; *//* 6.e-3 make convergences in less than 80 loops for the prevalence limit */
1.197 brouard 9216: /* Third parameter line */
9217: while(fgets(line, MAXLINE, ficpar)) {
9218: /* If line starts with a # it is a comment */
9219: if (line[0] == '#') {
9220: numlinepar++;
9221: fputs(line,stdout);
9222: fputs(line,ficparo);
9223: fputs(line,ficlog);
9224: continue;
9225: }else
9226: break;
9227: }
1.201 brouard 9228: if((num_filled=sscanf(line,"model=1+age%[^.\n]", model)) !=EOF){
9229: if (num_filled == 0)
9230: model[0]='\0';
9231: else if (num_filled != 1){
1.197 brouard 9232: printf("ERROR %d: Model should be at minimum 'model=1+age.' %s\n",num_filled, line);
9233: fprintf(ficlog,"ERROR %d: Model should be at minimum 'model=1+age.' %s\n",num_filled, line);
9234: model[0]='\0';
9235: goto end;
9236: }
9237: else{
9238: if (model[0]=='+'){
9239: for(i=1; i<=strlen(model);i++)
9240: modeltemp[i-1]=model[i];
1.201 brouard 9241: strcpy(model,modeltemp);
1.197 brouard 9242: }
9243: }
1.199 brouard 9244: /* printf(" model=1+age%s modeltemp= %s, model=%s\n",model, modeltemp, model);fflush(stdout); */
1.203 brouard 9245: printf("model=1+age+%s\n",model);fflush(stdout);
1.197 brouard 9246: }
9247: /* 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); */
9248: /* numlinepar=numlinepar+3; /\* In general *\/ */
9249: /* printf("title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\nftol=%e stepm=%d ncovcol=%d nlstate=%d ndeath=%d maxwav=%d mle=%d weight=%d\nmodel=1+age+%s\n", title, datafile, lastobs, firstpass,lastpass,ftol, stepm, ncovcol, nlstate,ndeath, maxwav, mle, weightopt,model); */
1.223 brouard 9250: 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);
9251: fprintf(ficlog,"title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\nftol=%e stepm=%d ncovcol=%d nqv=%d ntv=%d nqtv=%d nlstate=%d ndeath=%d maxwav=%d mle=%d weight=%d\nmodel=1+age+%s.\n", title, datafile, lastobs, firstpass,lastpass,ftol,stepm,ncovcol, nqv, ntv, nqtv, nlstate,ndeath,maxwav, mle, weightopt,model);
1.126 brouard 9252: fflush(ficlog);
1.190 brouard 9253: /* if(model[0]=='#'|| model[0]== '\0'){ */
9254: if(model[0]=='#'){
1.187 brouard 9255: printf("Error in 'model' line: model should start with 'model=1+age+' and end with '.' \n \
9256: 'model=1+age+.' or 'model=1+age+V1.' or 'model=1+age+age*age+V1+V1*age.' or \n \
9257: 'model=1+age+V1+V2.' or 'model=1+age+V1+V2+V1*V2.' etc. \n"); \
9258: if(mle != -1){
9259: printf("Fix the model line and run imach with mle=-1 to get a correct template of the parameter file.\n");
9260: exit(1);
9261: }
9262: }
1.126 brouard 9263: while((c=getc(ficpar))=='#' && c!= EOF){
9264: ungetc(c,ficpar);
9265: fgets(line, MAXLINE, ficpar);
9266: numlinepar++;
1.195 brouard 9267: if(line[1]=='q'){ /* This #q will quit imach (the answer is q) */
9268: z[0]=line[1];
9269: }
9270: /* printf("****line [1] = %c \n",line[1]); */
1.141 brouard 9271: fputs(line, stdout);
9272: //puts(line);
1.126 brouard 9273: fputs(line,ficparo);
9274: fputs(line,ficlog);
9275: }
9276: ungetc(c,ficpar);
9277:
9278:
1.145 brouard 9279: covar=matrix(0,NCOVMAX,1,n); /**< used in readdata */
1.225 brouard 9280: coqvar=matrix(1,nqv,1,n); /**< Fixed quantitative covariate */
9281: cotvar=ma3x(1,maxwav,1,ntv,1,n); /**< Time varying covariate */
9282: cotqvar=ma3x(1,maxwav,1,nqtv,1,n); /**< Time varying quantitative covariate */
1.136 brouard 9283: cptcovn=0; /*Number of covariates, i.e. number of '+' in model statement plus one, indepently of n in Vn*/
9284: /* v1+v2+v3+v2*v4+v5*age makes cptcovn = 5
9285: v1+v2*age+v2*v3 makes cptcovn = 3
9286: */
9287: if (strlen(model)>1)
1.187 brouard 9288: ncovmodel=2+nbocc(model,'+')+1; /*Number of variables including intercept and age = cptcovn + intercept + age : v1+v2+v3+v2*v4+v5*age makes 5+2=7,age*age makes 3*/
1.145 brouard 9289: else
1.187 brouard 9290: ncovmodel=2; /* Constant and age */
1.133 brouard 9291: nforce= (nlstate+ndeath-1)*nlstate; /* Number of forces ij from state i to j */
9292: npar= nforce*ncovmodel; /* Number of parameters like aij*/
1.131 brouard 9293: if(npar >MAXPARM || nlstate >NLSTATEMAX || ndeath >NDEATHMAX || ncovmodel>NCOVMAX){
9294: 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);
9295: 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);
9296: fflush(stdout);
9297: fclose (ficlog);
9298: goto end;
9299: }
1.126 brouard 9300: delti3= ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel);
9301: delti=delti3[1][1];
9302: /*delti=vector(1,npar); *//* Scale of each paramater (output from hesscov)*/
9303: if(mle==-1){ /* Print a wizard for help writing covariance matrix */
9304: prwizard(ncovmodel, nlstate, ndeath, model, ficparo);
1.191 brouard 9305: printf(" You chose mle=-1, look at file %s for a template of covariance matrix \n",filereso);
9306: fprintf(ficlog," You chose mle=-1, look at file %s for a template of covariance matrix \n",filereso);
1.126 brouard 9307: free_ma3x(delti3,1,nlstate,1, nlstate+ndeath-1,1,ncovmodel);
9308: fclose (ficparo);
9309: fclose (ficlog);
9310: goto end;
9311: exit(0);
1.220 brouard 9312: } else if(mle==-5) { /* Main Wizard */
1.126 brouard 9313: prwizard(ncovmodel, nlstate, ndeath, model, ficparo);
1.192 brouard 9314: printf(" You chose mle=-3, look at file %s for a template of covariance matrix \n",filereso);
9315: fprintf(ficlog," You chose mle=-3, look at file %s for a template of covariance matrix \n",filereso);
1.126 brouard 9316: param= ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel);
9317: matcov=matrix(1,npar,1,npar);
1.203 brouard 9318: hess=matrix(1,npar,1,npar);
1.220 brouard 9319: } else{ /* Begin of mle != -1 or -5 */
1.145 brouard 9320: /* Read guessed parameters */
1.126 brouard 9321: /* Reads comments: lines beginning with '#' */
9322: while((c=getc(ficpar))=='#' && c!= EOF){
9323: ungetc(c,ficpar);
9324: fgets(line, MAXLINE, ficpar);
9325: numlinepar++;
1.141 brouard 9326: fputs(line,stdout);
1.126 brouard 9327: fputs(line,ficparo);
9328: fputs(line,ficlog);
9329: }
9330: ungetc(c,ficpar);
9331:
9332: param= ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel);
9333: for(i=1; i <=nlstate; i++){
1.220 brouard 9334: j=0;
1.126 brouard 9335: for(jj=1; jj <=nlstate+ndeath; jj++){
1.220 brouard 9336: if(jj==i) continue;
9337: j++;
9338: fscanf(ficpar,"%1d%1d",&i1,&j1);
9339: if ((i1 != i) || (j1 != jj)){
9340: printf("Error in line parameters number %d, %1d%1d instead of %1d%1d \n \
1.126 brouard 9341: It might be a problem of design; if ncovcol and the model are correct\n \
9342: run imach with mle=-1 to get a correct template of the parameter file.\n",numlinepar, i,j, i1, j1);
1.220 brouard 9343: exit(1);
9344: }
9345: fprintf(ficparo,"%1d%1d",i1,j1);
9346: if(mle==1)
9347: printf("%1d%1d",i,jj);
9348: fprintf(ficlog,"%1d%1d",i,jj);
9349: for(k=1; k<=ncovmodel;k++){
9350: fscanf(ficpar," %lf",¶m[i][j][k]);
9351: if(mle==1){
9352: printf(" %lf",param[i][j][k]);
9353: fprintf(ficlog," %lf",param[i][j][k]);
9354: }
9355: else
9356: fprintf(ficlog," %lf",param[i][j][k]);
9357: fprintf(ficparo," %lf",param[i][j][k]);
9358: }
9359: fscanf(ficpar,"\n");
9360: numlinepar++;
9361: if(mle==1)
9362: printf("\n");
9363: fprintf(ficlog,"\n");
9364: fprintf(ficparo,"\n");
1.126 brouard 9365: }
9366: }
9367: fflush(ficlog);
9368:
1.145 brouard 9369: /* Reads scales values */
1.126 brouard 9370: p=param[1][1];
9371:
9372: /* Reads comments: lines beginning with '#' */
9373: while((c=getc(ficpar))=='#' && c!= EOF){
9374: ungetc(c,ficpar);
9375: fgets(line, MAXLINE, ficpar);
9376: numlinepar++;
1.141 brouard 9377: fputs(line,stdout);
1.126 brouard 9378: fputs(line,ficparo);
9379: fputs(line,ficlog);
9380: }
9381: ungetc(c,ficpar);
9382:
9383: for(i=1; i <=nlstate; i++){
9384: for(j=1; j <=nlstate+ndeath-1; j++){
1.220 brouard 9385: fscanf(ficpar,"%1d%1d",&i1,&j1);
9386: if ( (i1-i) * (j1-j) != 0){
9387: printf("Error in line parameters number %d, %1d%1d instead of %1d%1d \n",numlinepar, i,j, i1, j1);
9388: exit(1);
9389: }
9390: printf("%1d%1d",i,j);
9391: fprintf(ficparo,"%1d%1d",i1,j1);
9392: fprintf(ficlog,"%1d%1d",i1,j1);
9393: for(k=1; k<=ncovmodel;k++){
9394: fscanf(ficpar,"%le",&delti3[i][j][k]);
9395: printf(" %le",delti3[i][j][k]);
9396: fprintf(ficparo," %le",delti3[i][j][k]);
9397: fprintf(ficlog," %le",delti3[i][j][k]);
9398: }
9399: fscanf(ficpar,"\n");
9400: numlinepar++;
9401: printf("\n");
9402: fprintf(ficparo,"\n");
9403: fprintf(ficlog,"\n");
1.126 brouard 9404: }
9405: }
9406: fflush(ficlog);
1.220 brouard 9407:
1.145 brouard 9408: /* Reads covariance matrix */
1.126 brouard 9409: delti=delti3[1][1];
1.220 brouard 9410:
9411:
1.126 brouard 9412: /* free_ma3x(delti3,1,nlstate,1,nlstate+ndeath-1,1,ncovmodel); */ /* Hasn't to to freed here otherwise delti is no more allocated */
1.220 brouard 9413:
1.126 brouard 9414: /* Reads comments: lines beginning with '#' */
9415: while((c=getc(ficpar))=='#' && c!= EOF){
9416: ungetc(c,ficpar);
9417: fgets(line, MAXLINE, ficpar);
9418: numlinepar++;
1.141 brouard 9419: fputs(line,stdout);
1.126 brouard 9420: fputs(line,ficparo);
9421: fputs(line,ficlog);
9422: }
9423: ungetc(c,ficpar);
1.220 brouard 9424:
1.126 brouard 9425: matcov=matrix(1,npar,1,npar);
1.203 brouard 9426: hess=matrix(1,npar,1,npar);
1.131 brouard 9427: for(i=1; i <=npar; i++)
9428: for(j=1; j <=npar; j++) matcov[i][j]=0.;
1.220 brouard 9429:
1.194 brouard 9430: /* Scans npar lines */
1.126 brouard 9431: for(i=1; i <=npar; i++){
1.226 brouard 9432: count=fscanf(ficpar,"%1d%1d%d",&i1,&j1,&jk);
1.194 brouard 9433: if(count != 3){
1.226 brouard 9434: printf("Error! Error in parameter file %s at line %d after line starting with %1d%1d%1d\n\
1.194 brouard 9435: This is probably because your covariance matrix doesn't \n contain exactly %d lines corresponding to your model line '1+age+%s'.\n\
9436: Please run with mle=-1 to get a correct covariance matrix.\n",optionfile,numlinepar, i1,j1,jk, npar, model);
1.226 brouard 9437: fprintf(ficlog,"Error! Error in parameter file %s at line %d after line starting with %1d%1d%1d\n\
1.194 brouard 9438: This is probably because your covariance matrix doesn't \n contain exactly %d lines corresponding to your model line '1+age+%s'.\n\
9439: Please run with mle=-1 to get a correct covariance matrix.\n",optionfile,numlinepar, i1,j1,jk, npar, model);
1.226 brouard 9440: exit(1);
1.220 brouard 9441: }else{
1.226 brouard 9442: if(mle==1)
9443: printf("%1d%1d%d",i1,j1,jk);
9444: }
9445: fprintf(ficlog,"%1d%1d%d",i1,j1,jk);
9446: fprintf(ficparo,"%1d%1d%d",i1,j1,jk);
1.126 brouard 9447: for(j=1; j <=i; j++){
1.226 brouard 9448: fscanf(ficpar," %le",&matcov[i][j]);
9449: if(mle==1){
9450: printf(" %.5le",matcov[i][j]);
9451: }
9452: fprintf(ficlog," %.5le",matcov[i][j]);
9453: fprintf(ficparo," %.5le",matcov[i][j]);
1.126 brouard 9454: }
9455: fscanf(ficpar,"\n");
9456: numlinepar++;
9457: if(mle==1)
1.220 brouard 9458: printf("\n");
1.126 brouard 9459: fprintf(ficlog,"\n");
9460: fprintf(ficparo,"\n");
9461: }
1.194 brouard 9462: /* End of read covariance matrix npar lines */
1.126 brouard 9463: for(i=1; i <=npar; i++)
9464: for(j=i+1;j<=npar;j++)
1.226 brouard 9465: matcov[i][j]=matcov[j][i];
1.126 brouard 9466:
9467: if(mle==1)
9468: printf("\n");
9469: fprintf(ficlog,"\n");
9470:
9471: fflush(ficlog);
9472:
9473: /*-------- Rewriting parameter file ----------*/
9474: strcpy(rfileres,"r"); /* "Rparameterfile */
9475: strcat(rfileres,optionfilefiname); /* Parameter file first name*/
9476: strcat(rfileres,"."); /* */
9477: strcat(rfileres,optionfilext); /* Other files have txt extension */
9478: if((ficres =fopen(rfileres,"w"))==NULL) {
1.201 brouard 9479: printf("Problem writing new parameter file: %s\n", rfileres);goto end;
9480: fprintf(ficlog,"Problem writing new parameter file: %s\n", rfileres);goto end;
1.126 brouard 9481: }
9482: fprintf(ficres,"#%s\n",version);
9483: } /* End of mle != -3 */
1.218 brouard 9484:
1.186 brouard 9485: /* Main data
9486: */
1.126 brouard 9487: n= lastobs;
9488: num=lvector(1,n);
9489: moisnais=vector(1,n);
9490: annais=vector(1,n);
9491: moisdc=vector(1,n);
9492: andc=vector(1,n);
1.220 brouard 9493: weight=vector(1,n);
1.126 brouard 9494: agedc=vector(1,n);
9495: cod=ivector(1,n);
1.220 brouard 9496: for(i=1;i<=n;i++){
9497: num[i]=0;
9498: moisnais[i]=0;
9499: annais[i]=0;
9500: moisdc[i]=0;
9501: andc[i]=0;
9502: agedc[i]=0;
9503: cod[i]=0;
9504: weight[i]=1.0; /* Equal weights, 1 by default */
9505: }
1.126 brouard 9506: mint=matrix(1,maxwav,1,n);
9507: anint=matrix(1,maxwav,1,n);
1.131 brouard 9508: s=imatrix(1,maxwav+1,1,n); /* s[i][j] health state for wave i and individual j */
1.126 brouard 9509: tab=ivector(1,NCOVMAX);
1.144 brouard 9510: ncodemax=ivector(1,NCOVMAX); /* Number of code per covariate; if O and 1 only, 2**ncov; V1+V2+V3+V4=>16 */
1.192 brouard 9511: ncodemaxwundef=ivector(1,NCOVMAX); /* Number of code per covariate; if - 1 O and 1 only, 2**ncov; V1+V2+V3+V4=>16 */
1.126 brouard 9512:
1.136 brouard 9513: /* Reads data from file datafile */
9514: if (readdata(datafile, firstobs, lastobs, &imx)==1)
9515: goto end;
9516:
9517: /* Calculation of the number of parameters from char model */
1.137 brouard 9518: /* modelsav=V2+V1+V4+age*V3 strb=age*V3 stra=V2+V1+V4
9519: k=4 (age*V3) Tvar[k=4]= 3 (from V3) Tag[cptcovage=1]=4
9520: k=3 V4 Tvar[k=3]= 4 (from V4)
9521: k=2 V1 Tvar[k=2]= 1 (from V1)
9522: k=1 Tvar[1]=2 (from V2)
9523: */
1.231 ! brouard 9524:
! 9525: Tvar=ivector(1,NCOVMAX); /* Was 15 changed to NCOVMAX. */
! 9526: TvarFD=ivector(1,NCOVMAX); /* */
! 9527: TvarFDind=ivector(1,NCOVMAX); /* */
! 9528: TvarFQ=ivector(1,NCOVMAX); /* */
! 9529: TvarFQind=ivector(1,NCOVMAX); /* */
! 9530: TvarVD=ivector(1,NCOVMAX); /* */
! 9531: TvarVDind=ivector(1,NCOVMAX); /* */
! 9532: TvarVQ=ivector(1,NCOVMAX); /* */
! 9533: TvarVQind=ivector(1,NCOVMAX); /* */
! 9534:
1.230 brouard 9535: Tvalsel=vector(1,NCOVMAX); /* */
1.226 brouard 9536: Typevar=ivector(-1,NCOVMAX); /* -1 to 2 */
9537: Fixed=ivector(-1,NCOVMAX); /* -1 to 3 */
9538: Dummy=ivector(-1,NCOVMAX); /* -1 to 3 */
1.137 brouard 9539: /* V2+V1+V4+age*V3 is a model with 4 covariates (3 plus signs).
9540: For each model-covariate stores the data-covariate id. Tvar[1]=2, Tvar[2]=1, Tvar[3]=4,
9541: Tvar[4=age*V3] is 3 and 'age' is recorded in Tage.
9542: */
9543: /* For model-covariate k tells which data-covariate to use but
9544: because this model-covariate is a construction we invent a new column
9545: ncovcol + k1
9546: If already ncovcol=4 and model=V2+V1+V1*V4+age*V3
9547: Tvar[3=V1*V4]=4+1 etc */
1.227 brouard 9548: Tprod=ivector(1,NCOVMAX); /* Gives the k position of the k1 product */
9549: Tposprod=ivector(1,NCOVMAX); /* Gives the k1 product from the k position */
1.137 brouard 9550: /* Tprod[k1=1]=3(=V1*V4) for V2+V1+V1*V4+age*V3
9551: if V2+V1+V1*V4+age*V3+V3*V2 TProd[k1=2]=5 (V3*V2)
1.227 brouard 9552: Tposprod[k]=k1 , Tposprod[3]=1, Tposprod[5]=2
1.137 brouard 9553: */
1.145 brouard 9554: Tvaraff=ivector(1,NCOVMAX); /* Unclear */
9555: Tvard=imatrix(1,NCOVMAX,1,2); /* n=Tvard[k1][1] and m=Tvard[k1][2] gives the couple n,m of the k1 th product Vn*Vm
1.141 brouard 9556: * For V3*V2 (in V2+V1+V1*V4+age*V3+V3*V2), V3*V2 position is 2nd.
9557: * Tvard[k1=2][1]=3 (V3) Tvard[k1=2][2]=2(V2) */
1.145 brouard 9558: Tage=ivector(1,NCOVMAX); /* Gives the covariate id of covariates associated with age: V2 + V1 + age*V4 + V3*age
1.137 brouard 9559: 4 covariates (3 plus signs)
9560: Tage[1=V3*age]= 4; Tage[2=age*V4] = 3
9561: */
1.230 brouard 9562: Tmodelind=ivector(1,NCOVMAX);/** gives the k model position of an
1.227 brouard 9563: * individual dummy, fixed or varying:
9564: * Tmodelind[Tvaraff[3]]=9,Tvaraff[1]@9={4,
9565: * 3, 1, 0, 0, 0, 0, 0, 0},
1.230 brouard 9566: * model=V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 ,
9567: * V1 df, V2 qf, V3 & V4 dv, V5 qv
9568: * Tmodelind[1]@9={9,0,3,2,}*/
9569: TmodelInvind=ivector(1,NCOVMAX); /* TmodelInvind=Tvar[k]- ncovcol-nqv={5-2-1=2,*/
9570: TmodelInvQind=ivector(1,NCOVMAX);/** gives the k model position of an
1.228 brouard 9571: * individual quantitative, fixed or varying:
9572: * Tmodelqind[1]=1,Tvaraff[1]@9={4,
9573: * 3, 1, 0, 0, 0, 0, 0, 0},
9574: * model=V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1*/
1.186 brouard 9575: /* Main decodemodel */
9576:
1.187 brouard 9577:
1.223 brouard 9578: if(decodemodel(model, lastobs) == 1) /* In order to get Tvar[k] V4+V3+V5 p Tvar[1]@3 = {4, 3, 5}*/
1.136 brouard 9579: goto end;
9580:
1.137 brouard 9581: if((double)(lastobs-imx)/(double)imx > 1.10){
9582: nbwarn++;
9583: 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);
9584: 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);
9585: }
1.136 brouard 9586: /* if(mle==1){*/
1.137 brouard 9587: if (weightopt != 1) { /* Maximisation without weights. We can have weights different from 1 but want no weight*/
9588: for(i=1;i<=imx;i++) weight[i]=1.0; /* changed to imx */
1.136 brouard 9589: }
9590:
9591: /*-calculation of age at interview from date of interview and age at death -*/
9592: agev=matrix(1,maxwav,1,imx);
9593:
9594: if(calandcheckages(imx, maxwav, &agemin, &agemax, &nberr, &nbwarn) == 1)
9595: goto end;
9596:
1.126 brouard 9597:
1.136 brouard 9598: agegomp=(int)agemin;
9599: free_vector(moisnais,1,n);
9600: free_vector(annais,1,n);
1.126 brouard 9601: /* free_matrix(mint,1,maxwav,1,n);
9602: free_matrix(anint,1,maxwav,1,n);*/
1.215 brouard 9603: /* free_vector(moisdc,1,n); */
9604: /* free_vector(andc,1,n); */
1.145 brouard 9605: /* */
9606:
1.126 brouard 9607: wav=ivector(1,imx);
1.214 brouard 9608: /* dh=imatrix(1,lastpass-firstpass+1,1,imx); */
9609: /* bh=imatrix(1,lastpass-firstpass+1,1,imx); */
9610: /* mw=imatrix(1,lastpass-firstpass+1,1,imx); */
9611: 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.*/
9612: bh=imatrix(1,lastpass-firstpass+2,1,imx);
9613: mw=imatrix(1,lastpass-firstpass+2,1,imx);
1.126 brouard 9614:
9615: /* Concatenates waves */
1.214 brouard 9616: /* Concatenates waves: wav[i] is the number of effective (useful waves) of individual i.
9617: Death is a valid wave (if date is known).
9618: mw[mi][i] is the number of (mi=1 to wav[i]) effective wave out of mi of individual i
9619: dh[m][i] or dh[mw[mi][i]][i] is the delay between two effective waves m=mw[mi][i]
9620: and mw[mi+1][i]. dh depends on stepm.
9621: */
9622:
1.126 brouard 9623: concatwav(wav, dh, bh, mw, s, agedc, agev, firstpass, lastpass, imx, nlstate, stepm);
1.145 brouard 9624: /* */
9625:
1.215 brouard 9626: free_vector(moisdc,1,n);
9627: free_vector(andc,1,n);
9628:
1.126 brouard 9629: /* Routine tricode is to calculate cptcoveff (real number of unique covariates) and to associate covariable number and modality */
9630: nbcode=imatrix(0,NCOVMAX,0,NCOVMAX);
9631: ncodemax[1]=1;
1.145 brouard 9632: Ndum =ivector(-1,NCOVMAX);
1.225 brouard 9633: cptcoveff=0;
1.220 brouard 9634: if (ncovmodel-nagesqr > 2 ){ /* That is if covariate other than cst, age and age*age */
9635: tricode(&cptcoveff,Tvar,nbcode,imx, Ndum); /**< Fills nbcode[Tvar[j]][l]; */
1.227 brouard 9636: }
9637:
9638: ncovcombmax=pow(2,cptcoveff);
9639: invalidvarcomb=ivector(1, ncovcombmax);
9640: for(i=1;i<ncovcombmax;i++)
9641: invalidvarcomb[i]=0;
9642:
1.211 brouard 9643: /* Nbcode gives the value of the lth modality (currently 1 to 2) of jth covariate, in
1.186 brouard 9644: V2+V1*age, there are 3 covariates Tvar[2]=1 (V1).*/
1.211 brouard 9645: /* 1 to ncodemax[j] which is the maximum value of this jth covariate */
1.227 brouard 9646:
1.200 brouard 9647: /* codtab=imatrix(1,100,1,10);*/ /* codtab[h,k]=( (h-1) - mod(k-1,2**(k-1) )/2**(k-1) */
1.198 brouard 9648: /*printf(" codtab[1,1],codtab[100,10]=%d,%d\n", codtab[1][1],codtabm(100,10));*/
1.186 brouard 9649: /* codtab gives the value 1 or 2 of the hth combination of k covariates (1 or 2).*/
1.211 brouard 9650: /* nbcode[Tvaraff[j]][codtabm(h,j)]) : if there are only 2 modalities for a covariate j,
9651: * codtabm(h,j) gives its value classified at position h and nbcode gives how it is coded
9652: * (currently 0 or 1) in the data.
9653: * In a loop on h=1 to 2**k, and a loop on j (=1 to k), we get the value of
9654: * corresponding modality (h,j).
9655: */
9656:
1.145 brouard 9657: h=0;
9658: /*if (cptcovn > 0) */
1.126 brouard 9659: m=pow(2,cptcoveff);
9660:
1.144 brouard 9661: /**< codtab(h,k) k = codtab[h,k]=( (h-1) - mod(k-1,2**(k-1) )/2**(k-1) + 1
1.211 brouard 9662: * For k=4 covariates, h goes from 1 to m=2**k
9663: * codtabm(h,k)= (1 & (h-1) >> (k-1)) + 1;
9664: * #define codtabm(h,k) (1 & (h-1) >> (k-1))+1
1.186 brouard 9665: * h\k 1 2 3 4
1.143 brouard 9666: *______________________________
9667: * 1 i=1 1 i=1 1 i=1 1 i=1 1
9668: * 2 2 1 1 1
9669: * 3 i=2 1 2 1 1
9670: * 4 2 2 1 1
9671: * 5 i=3 1 i=2 1 2 1
9672: * 6 2 1 2 1
9673: * 7 i=4 1 2 2 1
9674: * 8 2 2 2 1
1.197 brouard 9675: * 9 i=5 1 i=3 1 i=2 1 2
9676: * 10 2 1 1 2
9677: * 11 i=6 1 2 1 2
9678: * 12 2 2 1 2
9679: * 13 i=7 1 i=4 1 2 2
9680: * 14 2 1 2 2
9681: * 15 i=8 1 2 2 2
9682: * 16 2 2 2 2
1.143 brouard 9683: */
1.212 brouard 9684: /* How to do the opposite? From combination h (=1 to 2**k) how to get the value on the covariates? */
1.211 brouard 9685: /* from h=5 and m, we get then number of covariates k=log(m)/log(2)=4
9686: * and the value of each covariate?
9687: * V1=1, V2=1, V3=2, V4=1 ?
9688: * h-1=4 and 4 is 0100 or reverse 0010, and +1 is 1121 ok.
9689: * h=6, 6-1=5, 5 is 0101, 1010, 2121, V1=2nd, V2=1st, V3=2nd, V4=1st.
9690: * In order to get the real value in the data, we use nbcode
9691: * nbcode[Tvar[3][2nd]]=1 and nbcode[Tvar[4][1]]=0
9692: * We are keeping this crazy system in order to be able (in the future?)
9693: * to have more than 2 values (0 or 1) for a covariate.
9694: * #define codtabm(h,k) (1 & (h-1) >> (k-1))+1
9695: * h=6, k=2? h-1=5=0101, reverse 1010, +1=2121, k=2nd position: value is 1: codtabm(6,2)=1
9696: * bbbbbbbb
9697: * 76543210
9698: * h-1 00000101 (6-1=5)
1.219 brouard 9699: *(h-1)>>(k-1)= 00000010 >> (2-1) = 1 right shift
1.211 brouard 9700: * &
9701: * 1 00000001 (1)
1.219 brouard 9702: * 00000000 = 1 & ((h-1) >> (k-1))
9703: * +1= 00000001 =1
1.211 brouard 9704: *
9705: * h=14, k=3 => h'=h-1=13, k'=k-1=2
9706: * h' 1101 =2^3+2^2+0x2^1+2^0
9707: * >>k' 11
9708: * & 00000001
9709: * = 00000001
9710: * +1 = 00000010=2 = codtabm(14,3)
9711: * Reverse h=6 and m=16?
9712: * cptcoveff=log(16)/log(2)=4 covariate: 6-1=5=0101 reversed=1010 +1=2121 =>V1=2, V2=1, V3=2, V4=1.
9713: * for (j=1 to cptcoveff) Vj=decodtabm(j,h,cptcoveff)
9714: * decodtabm(h,j,cptcoveff)= (((h-1) >> (j-1)) & 1) +1
9715: * decodtabm(h,j,cptcoveff)= (h <= (1<<cptcoveff)?(((h-1) >> (j-1)) & 1) +1 : -1)
9716: * V3=decodtabm(14,3,2**4)=2
9717: * h'=13 1101 =2^3+2^2+0x2^1+2^0
9718: *(h-1) >> (j-1) 0011 =13 >> 2
9719: * &1 000000001
9720: * = 000000001
9721: * +1= 000000010 =2
9722: * 2211
9723: * V1=1+1, V2=0+1, V3=1+1, V4=1+1
9724: * V3=2
1.220 brouard 9725: * codtabm and decodtabm are identical
1.211 brouard 9726: */
9727:
1.145 brouard 9728:
9729: free_ivector(Ndum,-1,NCOVMAX);
9730:
9731:
1.126 brouard 9732:
1.186 brouard 9733: /* Initialisation of ----------- gnuplot -------------*/
1.126 brouard 9734: strcpy(optionfilegnuplot,optionfilefiname);
9735: if(mle==-3)
1.201 brouard 9736: strcat(optionfilegnuplot,"-MORT_");
1.126 brouard 9737: strcat(optionfilegnuplot,".gp");
9738:
9739: if((ficgp=fopen(optionfilegnuplot,"w"))==NULL) {
9740: printf("Problem with file %s",optionfilegnuplot);
9741: }
9742: else{
1.204 brouard 9743: fprintf(ficgp,"\n# IMaCh-%s\n", version);
1.126 brouard 9744: fprintf(ficgp,"# %s\n", optionfilegnuplot);
1.141 brouard 9745: //fprintf(ficgp,"set missing 'NaNq'\n");
9746: fprintf(ficgp,"set datafile missing 'NaNq'\n");
1.126 brouard 9747: }
9748: /* fclose(ficgp);*/
1.186 brouard 9749:
9750:
9751: /* Initialisation of --------- index.htm --------*/
1.126 brouard 9752:
9753: strcpy(optionfilehtm,optionfilefiname); /* Main html file */
9754: if(mle==-3)
1.201 brouard 9755: strcat(optionfilehtm,"-MORT_");
1.126 brouard 9756: strcat(optionfilehtm,".htm");
9757: if((fichtm=fopen(optionfilehtm,"w"))==NULL) {
1.131 brouard 9758: printf("Problem with %s \n",optionfilehtm);
9759: exit(0);
1.126 brouard 9760: }
9761:
9762: strcpy(optionfilehtmcov,optionfilefiname); /* Only for matrix of covariance */
9763: strcat(optionfilehtmcov,"-cov.htm");
9764: if((fichtmcov=fopen(optionfilehtmcov,"w"))==NULL) {
9765: printf("Problem with %s \n",optionfilehtmcov), exit(0);
9766: }
9767: else{
9768: fprintf(fichtmcov,"<html><head>\n<title>IMaCh Cov %s</title></head>\n <body><font size=\"2\">%s <br> %s</font> \
9769: <hr size=\"2\" color=\"#EC5E5E\"> \n\
1.204 brouard 9770: Title=%s <br>Datafile=%s Firstpass=%d Lastpass=%d Stepm=%d Weight=%d Model=1+age+%s<br>\n",\
1.126 brouard 9771: optionfilehtmcov,version,fullversion,title,datafile,firstpass,lastpass,stepm, weightopt, model);
9772: }
9773:
1.213 brouard 9774: fprintf(fichtm,"<html><head>\n<head>\n<meta charset=\"utf-8\"/><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n<title>IMaCh %s</title></head>\n <body><font size=\"7\"><a href=http:/euroreves.ined.fr/imach>IMaCh for Interpolated Markov Chain</a> </font><br>\n<font size=\"3\">Sponsored by Copyright (C) 2002-2015 <a href=http://www.ined.fr>INED</a>-EUROREVES-Institut de longévité-2013-2016-Japan Society for the Promotion of Sciences 日本学術振興会 (<a href=https://www.jsps.go.jp/english/e-grants/>Grant-in-Aid for Scientific Research 25293121</a>) - <a href=https://software.intel.com/en-us>Intel Software 2015-2018</a></font><br> \
1.204 brouard 9775: <hr size=\"2\" color=\"#EC5E5E\"> \n\
9776: <font size=\"2\">IMaCh-%s <br> %s</font> \
1.126 brouard 9777: <hr size=\"2\" color=\"#EC5E5E\"> \n\
1.204 brouard 9778: Title=%s <br>Datafile=%s Firstpass=%d Lastpass=%d Stepm=%d Weight=%d Model=1+age+%s<br>\n\
1.126 brouard 9779: \n\
9780: <hr size=\"2\" color=\"#EC5E5E\">\
9781: <ul><li><h4>Parameter files</h4>\n\
9782: - Parameter file: <a href=\"%s.%s\">%s.%s</a><br>\n\
9783: - Copy of the parameter file: <a href=\"o%s\">o%s</a><br>\n\
9784: - Log file of the run: <a href=\"%s\">%s</a><br>\n\
9785: - Gnuplot file name: <a href=\"%s\">%s</a><br>\n\
9786: - Date and time at start: %s</ul>\n",\
9787: optionfilehtm,version,fullversion,title,datafile,firstpass,lastpass,stepm, weightopt, model,\
9788: optionfilefiname,optionfilext,optionfilefiname,optionfilext,\
9789: fileres,fileres,\
9790: filelog,filelog,optionfilegnuplot,optionfilegnuplot,strstart);
9791: fflush(fichtm);
9792:
9793: strcpy(pathr,path);
9794: strcat(pathr,optionfilefiname);
1.184 brouard 9795: #ifdef WIN32
9796: _chdir(optionfilefiname); /* Move to directory named optionfile */
9797: #else
1.126 brouard 9798: chdir(optionfilefiname); /* Move to directory named optionfile */
1.184 brouard 9799: #endif
9800:
1.126 brouard 9801:
1.220 brouard 9802: /* Calculates basic frequencies. Computes observed prevalence at single age
9803: and for any valid combination of covariates
1.126 brouard 9804: and prints on file fileres'p'. */
1.227 brouard 9805: freqsummary(fileres, agemin, agemax, s, agev, nlstate, imx, Tvaraff, invalidvarcomb, nbcode, ncodemax,mint,anint,strstart, \
9806: firstpass, lastpass, stepm, weightopt, model);
1.126 brouard 9807:
9808: fprintf(fichtm,"\n");
9809: fprintf(fichtm,"<br>Total number of observations=%d <br>\n\
9810: Youngest age at first (selected) pass %.2f, oldest age %.2f<br>\n\
9811: Interval (in months) between two waves: Min=%d Max=%d Mean=%.2lf<br>\n",\
9812: imx,agemin,agemax,jmin,jmax,jmean);
9813: pmmij= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
1.220 brouard 9814: oldms= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
9815: newms= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
9816: savms= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
9817: oldm=oldms; newm=newms; savm=savms; /* Keeps fixed addresses to free */
1.218 brouard 9818:
1.126 brouard 9819: /* For Powell, parameters are in a vector p[] starting at p[1]
9820: so we point p on param[1][1] so that p[1] maps on param[1][1][1] */
9821: p=param[1][1]; /* *(*(*(param +1)+1)+0) */
9822:
9823: globpr=0; /* To get the number ipmx of contributions and the sum of weights*/
1.186 brouard 9824: /* For mortality only */
1.126 brouard 9825: if (mle==-3){
1.136 brouard 9826: ximort=matrix(1,NDIM,1,NDIM);
1.220 brouard 9827: for(i=1;i<=NDIM;i++)
9828: for(j=1;j<=NDIM;j++)
9829: ximort[i][j]=0.;
1.186 brouard 9830: /* ximort=gsl_matrix_alloc(1,NDIM,1,NDIM); */
1.126 brouard 9831: cens=ivector(1,n);
9832: ageexmed=vector(1,n);
9833: agecens=vector(1,n);
9834: dcwave=ivector(1,n);
1.223 brouard 9835:
1.126 brouard 9836: for (i=1; i<=imx; i++){
9837: dcwave[i]=-1;
9838: for (m=firstpass; m<=lastpass; m++)
1.226 brouard 9839: if (s[m][i]>nlstate) {
9840: dcwave[i]=m;
9841: /* printf("i=%d j=%d s=%d dcwave=%d\n",i,j, s[j][i],dcwave[i]);*/
9842: break;
9843: }
1.126 brouard 9844: }
1.226 brouard 9845:
1.126 brouard 9846: for (i=1; i<=imx; i++) {
9847: if (wav[i]>0){
1.226 brouard 9848: ageexmed[i]=agev[mw[1][i]][i];
9849: j=wav[i];
9850: agecens[i]=1.;
9851:
9852: if (ageexmed[i]> 1 && wav[i] > 0){
9853: agecens[i]=agev[mw[j][i]][i];
9854: cens[i]= 1;
9855: }else if (ageexmed[i]< 1)
9856: cens[i]= -1;
9857: if (agedc[i]< AGESUP && agedc[i]>1 && dcwave[i]>firstpass && dcwave[i]<=lastpass)
9858: cens[i]=0 ;
1.126 brouard 9859: }
9860: else cens[i]=-1;
9861: }
9862:
9863: for (i=1;i<=NDIM;i++) {
9864: for (j=1;j<=NDIM;j++)
1.226 brouard 9865: ximort[i][j]=(i == j ? 1.0 : 0.0);
1.126 brouard 9866: }
9867:
1.145 brouard 9868: /*p[1]=0.0268; p[NDIM]=0.083;*/
1.126 brouard 9869: /*printf("%lf %lf", p[1], p[2]);*/
9870:
9871:
1.136 brouard 9872: #ifdef GSL
9873: printf("GSL optimization\n"); fprintf(ficlog,"Powell\n");
1.162 brouard 9874: #else
1.126 brouard 9875: printf("Powell\n"); fprintf(ficlog,"Powell\n");
1.136 brouard 9876: #endif
1.201 brouard 9877: strcpy(filerespow,"POW-MORT_");
9878: strcat(filerespow,fileresu);
1.126 brouard 9879: if((ficrespow=fopen(filerespow,"w"))==NULL) {
9880: printf("Problem with resultfile: %s\n", filerespow);
9881: fprintf(ficlog,"Problem with resultfile: %s\n", filerespow);
9882: }
1.136 brouard 9883: #ifdef GSL
9884: fprintf(ficrespow,"# GSL optimization\n# iter -2*LL");
1.162 brouard 9885: #else
1.126 brouard 9886: fprintf(ficrespow,"# Powell\n# iter -2*LL");
1.136 brouard 9887: #endif
1.126 brouard 9888: /* for (i=1;i<=nlstate;i++)
9889: for(j=1;j<=nlstate+ndeath;j++)
9890: if(j!=i)fprintf(ficrespow," p%1d%1d",i,j);
9891: */
9892: fprintf(ficrespow,"\n");
1.136 brouard 9893: #ifdef GSL
9894: /* gsl starts here */
9895: T = gsl_multimin_fminimizer_nmsimplex;
9896: gsl_multimin_fminimizer *sfm = NULL;
9897: gsl_vector *ss, *x;
9898: gsl_multimin_function minex_func;
9899:
9900: /* Initial vertex size vector */
9901: ss = gsl_vector_alloc (NDIM);
9902:
9903: if (ss == NULL){
9904: GSL_ERROR_VAL ("failed to allocate space for ss", GSL_ENOMEM, 0);
9905: }
9906: /* Set all step sizes to 1 */
9907: gsl_vector_set_all (ss, 0.001);
9908:
9909: /* Starting point */
1.126 brouard 9910:
1.136 brouard 9911: x = gsl_vector_alloc (NDIM);
9912:
9913: if (x == NULL){
9914: gsl_vector_free(ss);
9915: GSL_ERROR_VAL ("failed to allocate space for x", GSL_ENOMEM, 0);
9916: }
9917:
9918: /* Initialize method and iterate */
9919: /* p[1]=0.0268; p[NDIM]=0.083; */
1.186 brouard 9920: /* gsl_vector_set(x, 0, 0.0268); */
9921: /* gsl_vector_set(x, 1, 0.083); */
1.136 brouard 9922: gsl_vector_set(x, 0, p[1]);
9923: gsl_vector_set(x, 1, p[2]);
9924:
9925: minex_func.f = &gompertz_f;
9926: minex_func.n = NDIM;
9927: minex_func.params = (void *)&p; /* ??? */
9928:
9929: sfm = gsl_multimin_fminimizer_alloc (T, NDIM);
9930: gsl_multimin_fminimizer_set (sfm, &minex_func, x, ss);
9931:
9932: printf("Iterations beginning .....\n\n");
9933: printf("Iter. # Intercept Slope -Log Likelihood Simplex size\n");
9934:
9935: iteri=0;
9936: while (rval == GSL_CONTINUE){
9937: iteri++;
9938: status = gsl_multimin_fminimizer_iterate(sfm);
9939:
9940: if (status) printf("error: %s\n", gsl_strerror (status));
9941: fflush(0);
9942:
9943: if (status)
9944: break;
9945:
9946: rval = gsl_multimin_test_size (gsl_multimin_fminimizer_size (sfm), 1e-6);
9947: ssval = gsl_multimin_fminimizer_size (sfm);
9948:
9949: if (rval == GSL_SUCCESS)
9950: printf ("converged to a local maximum at\n");
9951:
9952: printf("%5d ", iteri);
9953: for (it = 0; it < NDIM; it++){
9954: printf ("%10.5f ", gsl_vector_get (sfm->x, it));
9955: }
9956: printf("f() = %-10.5f ssize = %.7f\n", sfm->fval, ssval);
9957: }
9958:
9959: printf("\n\n Please note: Program should be run many times with varying starting points to detemine global maximum\n\n");
9960:
9961: gsl_vector_free(x); /* initial values */
9962: gsl_vector_free(ss); /* inital step size */
9963: for (it=0; it<NDIM; it++){
9964: p[it+1]=gsl_vector_get(sfm->x,it);
9965: fprintf(ficrespow," %.12lf", p[it]);
9966: }
9967: gsl_multimin_fminimizer_free (sfm); /* p *(sfm.x.data) et p *(sfm.x.data+1) */
9968: #endif
9969: #ifdef POWELL
9970: powell(p,ximort,NDIM,ftol,&iter,&fret,gompertz);
9971: #endif
1.126 brouard 9972: fclose(ficrespow);
9973:
1.203 brouard 9974: hesscov(matcov, hess, p, NDIM, delti, 1e-4, gompertz);
1.126 brouard 9975:
9976: for(i=1; i <=NDIM; i++)
9977: for(j=i+1;j<=NDIM;j++)
1.220 brouard 9978: matcov[i][j]=matcov[j][i];
1.126 brouard 9979:
9980: printf("\nCovariance matrix\n ");
1.203 brouard 9981: fprintf(ficlog,"\nCovariance matrix\n ");
1.126 brouard 9982: for(i=1; i <=NDIM; i++) {
9983: for(j=1;j<=NDIM;j++){
1.220 brouard 9984: printf("%f ",matcov[i][j]);
9985: fprintf(ficlog,"%f ",matcov[i][j]);
1.126 brouard 9986: }
1.203 brouard 9987: printf("\n "); fprintf(ficlog,"\n ");
1.126 brouard 9988: }
9989:
9990: printf("iter=%d MLE=%f Eq=%lf*exp(%lf*(age-%d))\n",iter,-gompertz(p),p[1],p[2],agegomp);
1.193 brouard 9991: for (i=1;i<=NDIM;i++) {
1.126 brouard 9992: printf("%f [%f ; %f]\n",p[i],p[i]-2*sqrt(matcov[i][i]),p[i]+2*sqrt(matcov[i][i]));
1.193 brouard 9993: fprintf(ficlog,"%f [%f ; %f]\n",p[i],p[i]-2*sqrt(matcov[i][i]),p[i]+2*sqrt(matcov[i][i]));
9994: }
1.126 brouard 9995: lsurv=vector(1,AGESUP);
9996: lpop=vector(1,AGESUP);
9997: tpop=vector(1,AGESUP);
9998: lsurv[agegomp]=100000;
9999:
10000: for (k=agegomp;k<=AGESUP;k++) {
10001: agemortsup=k;
10002: if (p[1]*exp(p[2]*(k-agegomp))>1) break;
10003: }
10004:
10005: for (k=agegomp;k<agemortsup;k++)
10006: lsurv[k+1]=lsurv[k]-lsurv[k]*(p[1]*exp(p[2]*(k-agegomp)));
10007:
10008: for (k=agegomp;k<agemortsup;k++){
10009: lpop[k]=(lsurv[k]+lsurv[k+1])/2.;
10010: sumlpop=sumlpop+lpop[k];
10011: }
10012:
10013: tpop[agegomp]=sumlpop;
10014: for (k=agegomp;k<(agemortsup-3);k++){
10015: /* tpop[k+1]=2;*/
10016: tpop[k+1]=tpop[k]-lpop[k];
10017: }
10018:
10019:
10020: printf("\nAge lx qx dx Lx Tx e(x)\n");
10021: for (k=agegomp;k<(agemortsup-2);k++)
10022: 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]);
10023:
10024:
10025: replace_back_to_slash(pathc,pathcd); /* Even gnuplot wants a / */
1.220 brouard 10026: ageminpar=50;
10027: agemaxpar=100;
1.194 brouard 10028: if(ageminpar == AGEOVERFLOW ||agemaxpar == AGEOVERFLOW){
10029: printf("Warning! Error in gnuplot file with ageminpar %f or agemaxpar %f overflow\n\
10030: This is probably because your parameter file doesn't \n contain the exact number of lines (or columns) corresponding to your model line.\n\
10031: Please run with mle=-1 to get a correct covariance matrix.\n",ageminpar,agemaxpar);
10032: fprintf(ficlog,"Warning! Error in gnuplot file with ageminpar %f or agemaxpar %f overflow\n\
10033: This is probably because your parameter file doesn't \n contain the exact number of lines (or columns) corresponding to your model line.\n\
10034: Please run with mle=-1 to get a correct covariance matrix.\n",ageminpar,agemaxpar);
1.220 brouard 10035: }else{
10036: printf("Warning! ageminpar %f and agemaxpar %f have been fixed because for simplification until it is fixed...\n\n",ageminpar,agemaxpar);
10037: fprintf(ficlog,"Warning! ageminpar %f and agemaxpar %f have been fixed because for simplification until it is fixed...\n\n",ageminpar,agemaxpar);
1.201 brouard 10038: printinggnuplotmort(fileresu, optionfilefiname,ageminpar,agemaxpar,fage, pathc,p);
1.220 brouard 10039: }
1.201 brouard 10040: printinghtmlmort(fileresu,title,datafile, firstpass, lastpass, \
1.126 brouard 10041: stepm, weightopt,\
10042: model,imx,p,matcov,agemortsup);
10043:
10044: free_vector(lsurv,1,AGESUP);
10045: free_vector(lpop,1,AGESUP);
10046: free_vector(tpop,1,AGESUP);
1.220 brouard 10047: free_matrix(ximort,1,NDIM,1,NDIM);
1.136 brouard 10048: free_ivector(cens,1,n);
10049: free_vector(agecens,1,n);
10050: free_ivector(dcwave,1,n);
1.220 brouard 10051: #ifdef GSL
1.136 brouard 10052: #endif
1.186 brouard 10053: } /* Endof if mle==-3 mortality only */
1.205 brouard 10054: /* Standard */
10055: else{ /* For mle !=- 3, could be 0 or 1 or 4 etc. */
10056: globpr=0;/* Computes sum of likelihood for globpr=1 and funcone */
10057: /* Computes likelihood for initial parameters, uses funcone to compute gpimx and gsw */
1.132 brouard 10058: likelione(ficres, p, npar, nlstate, &globpr, &ipmx, &sw, &fretone, funcone); /* Prints the contributions to the likelihood */
1.126 brouard 10059: printf("First Likeli=%12.6f ipmx=%ld sw=%12.6f",fretone,ipmx,sw);
10060: for (k=1; k<=npar;k++)
10061: printf(" %d %8.5f",k,p[k]);
10062: printf("\n");
1.205 brouard 10063: if(mle>=1){ /* Could be 1 or 2, Real Maximization */
10064: /* mlikeli uses func not funcone */
10065: mlikeli(ficres,p, npar, ncovmodel, nlstate, ftol, func);
10066: }
10067: if(mle==0) {/* No optimization, will print the likelihoods for the datafile */
10068: globpr=0;/* Computes sum of likelihood for globpr=1 and funcone */
10069: /* Computes likelihood for initial parameters, uses funcone to compute gpimx and gsw */
10070: likelione(ficres, p, npar, nlstate, &globpr, &ipmx, &sw, &fretone, funcone); /* Prints the contributions to the likelihood */
10071: }
10072: globpr=1; /* again, to print the individual contributions using computed gpimx and gsw */
1.126 brouard 10073: likelione(ficres, p, npar, nlstate, &globpr, &ipmx, &sw, &fretone, funcone); /* Prints the contributions to the likelihood */
10074: printf("Second Likeli=%12.6f ipmx=%ld sw=%12.6f",fretone,ipmx,sw);
10075: for (k=1; k<=npar;k++)
10076: printf(" %d %8.5f",k,p[k]);
10077: printf("\n");
10078:
10079: /*--------- results files --------------*/
1.224 brouard 10080: fprintf(ficres,"title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\nftol=%e stepm=%d ncovcol=%d nqv=%d ntv=%d nqtv=%d nlstate=%d ndeath=%d maxwav=%d mle= 0 weight=%d\nmodel=1+age+%s.\n", title, datafile, lastobs, firstpass,lastpass,ftol, stepm, ncovcol, nqv, ntv, nqtv, nlstate, ndeath, maxwav, weightopt,model);
1.126 brouard 10081:
10082:
10083: fprintf(ficres,"# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n");
10084: printf("# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n");
10085: fprintf(ficlog,"# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n");
10086: for(i=1,jk=1; i <=nlstate; i++){
10087: for(k=1; k <=(nlstate+ndeath); k++){
1.225 brouard 10088: if (k != i) {
10089: printf("%d%d ",i,k);
10090: fprintf(ficlog,"%d%d ",i,k);
10091: fprintf(ficres,"%1d%1d ",i,k);
10092: for(j=1; j <=ncovmodel; j++){
10093: printf("%12.7f ",p[jk]);
10094: fprintf(ficlog,"%12.7f ",p[jk]);
10095: fprintf(ficres,"%12.7f ",p[jk]);
10096: jk++;
10097: }
10098: printf("\n");
10099: fprintf(ficlog,"\n");
10100: fprintf(ficres,"\n");
10101: }
1.126 brouard 10102: }
10103: }
1.203 brouard 10104: if(mle != 0){
10105: /* Computing hessian and covariance matrix only at a peak of the Likelihood, that is after optimization */
1.126 brouard 10106: ftolhess=ftol; /* Usually correct */
1.203 brouard 10107: hesscov(matcov, hess, p, npar, delti, ftolhess, func);
10108: 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");
10109: 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");
10110: for(i=1,jk=1; i <=nlstate; i++){
1.225 brouard 10111: for(k=1; k <=(nlstate+ndeath); k++){
10112: if (k != i) {
10113: printf("%d%d ",i,k);
10114: fprintf(ficlog,"%d%d ",i,k);
10115: for(j=1; j <=ncovmodel; j++){
10116: printf("%12.7f W=%8.3f CI=[%12.7f ; %12.7f] ",p[jk], p[jk]/sqrt(matcov[jk][jk]), p[jk]-1.96*sqrt(matcov[jk][jk]),p[jk]+1.96*sqrt(matcov[jk][jk]));
10117: fprintf(ficlog,"%12.7f W=%8.3f CI=[%12.7f ; %12.7f] ",p[jk], p[jk]/sqrt(matcov[jk][jk]), p[jk]-1.96*sqrt(matcov[jk][jk]),p[jk]+1.96*sqrt(matcov[jk][jk]));
10118: jk++;
10119: }
10120: printf("\n");
10121: fprintf(ficlog,"\n");
10122: }
10123: }
1.193 brouard 10124: }
1.203 brouard 10125: } /* end of hesscov and Wald tests */
1.225 brouard 10126:
1.203 brouard 10127: /* */
1.126 brouard 10128: fprintf(ficres,"# Scales (for hessian or gradient estimation)\n");
10129: printf("# Scales (for hessian or gradient estimation)\n");
10130: fprintf(ficlog,"# Scales (for hessian or gradient estimation)\n");
10131: for(i=1,jk=1; i <=nlstate; i++){
10132: for(j=1; j <=nlstate+ndeath; j++){
1.225 brouard 10133: if (j!=i) {
10134: fprintf(ficres,"%1d%1d",i,j);
10135: printf("%1d%1d",i,j);
10136: fprintf(ficlog,"%1d%1d",i,j);
10137: for(k=1; k<=ncovmodel;k++){
10138: printf(" %.5e",delti[jk]);
10139: fprintf(ficlog," %.5e",delti[jk]);
10140: fprintf(ficres," %.5e",delti[jk]);
10141: jk++;
10142: }
10143: printf("\n");
10144: fprintf(ficlog,"\n");
10145: fprintf(ficres,"\n");
10146: }
1.126 brouard 10147: }
10148: }
10149:
10150: fprintf(ficres,"# Covariance matrix \n# 121 Var(a12)\n# 122 Cov(b12,a12) Var(b12)\n# ...\n# 232 Cov(b23,a12) Cov(b23,b12) ... Var (b23)\n");
1.203 brouard 10151: if(mle >= 1) /* To big for the screen */
1.126 brouard 10152: 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");
10153: 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");
10154: /* # 121 Var(a12)\n\ */
10155: /* # 122 Cov(b12,a12) Var(b12)\n\ */
10156: /* # 131 Cov(a13,a12) Cov(a13,b12, Var(a13)\n\ */
10157: /* # 132 Cov(b13,a12) Cov(b13,b12, Cov(b13,a13) Var(b13)\n\ */
10158: /* # 212 Cov(a21,a12) Cov(a21,b12, Cov(a21,a13) Cov(a21,b13) Var(a21)\n\ */
10159: /* # 212 Cov(b21,a12) Cov(b21,b12, Cov(b21,a13) Cov(b21,b13) Cov(b21,a21) Var(b21)\n\ */
10160: /* # 232 Cov(a23,a12) Cov(a23,b12, Cov(a23,a13) Cov(a23,b13) Cov(a23,a21) Cov(a23,b21) Var(a23)\n\ */
10161: /* # 232 Cov(b23,a12) Cov(b23,b12) ... Var (b23)\n" */
10162:
10163:
10164: /* Just to have a covariance matrix which will be more understandable
10165: even is we still don't want to manage dictionary of variables
10166: */
10167: for(itimes=1;itimes<=2;itimes++){
10168: jj=0;
10169: for(i=1; i <=nlstate; i++){
1.225 brouard 10170: for(j=1; j <=nlstate+ndeath; j++){
10171: if(j==i) continue;
10172: for(k=1; k<=ncovmodel;k++){
10173: jj++;
10174: ca[0]= k+'a'-1;ca[1]='\0';
10175: if(itimes==1){
10176: if(mle>=1)
10177: printf("#%1d%1d%d",i,j,k);
10178: fprintf(ficlog,"#%1d%1d%d",i,j,k);
10179: fprintf(ficres,"#%1d%1d%d",i,j,k);
10180: }else{
10181: if(mle>=1)
10182: printf("%1d%1d%d",i,j,k);
10183: fprintf(ficlog,"%1d%1d%d",i,j,k);
10184: fprintf(ficres,"%1d%1d%d",i,j,k);
10185: }
10186: ll=0;
10187: for(li=1;li <=nlstate; li++){
10188: for(lj=1;lj <=nlstate+ndeath; lj++){
10189: if(lj==li) continue;
10190: for(lk=1;lk<=ncovmodel;lk++){
10191: ll++;
10192: if(ll<=jj){
10193: cb[0]= lk +'a'-1;cb[1]='\0';
10194: if(ll<jj){
10195: if(itimes==1){
10196: if(mle>=1)
10197: printf(" Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj);
10198: fprintf(ficlog," Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj);
10199: fprintf(ficres," Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj);
10200: }else{
10201: if(mle>=1)
10202: printf(" %.5e",matcov[jj][ll]);
10203: fprintf(ficlog," %.5e",matcov[jj][ll]);
10204: fprintf(ficres," %.5e",matcov[jj][ll]);
10205: }
10206: }else{
10207: if(itimes==1){
10208: if(mle>=1)
10209: printf(" Var(%s%1d%1d)",ca,i,j);
10210: fprintf(ficlog," Var(%s%1d%1d)",ca,i,j);
10211: fprintf(ficres," Var(%s%1d%1d)",ca,i,j);
10212: }else{
10213: if(mle>=1)
10214: printf(" %.7e",matcov[jj][ll]);
10215: fprintf(ficlog," %.7e",matcov[jj][ll]);
10216: fprintf(ficres," %.7e",matcov[jj][ll]);
10217: }
10218: }
10219: }
10220: } /* end lk */
10221: } /* end lj */
10222: } /* end li */
10223: if(mle>=1)
10224: printf("\n");
10225: fprintf(ficlog,"\n");
10226: fprintf(ficres,"\n");
10227: numlinepar++;
10228: } /* end k*/
10229: } /*end j */
1.126 brouard 10230: } /* end i */
10231: } /* end itimes */
10232:
10233: fflush(ficlog);
10234: fflush(ficres);
1.225 brouard 10235: while(fgets(line, MAXLINE, ficpar)) {
10236: /* If line starts with a # it is a comment */
10237: if (line[0] == '#') {
10238: numlinepar++;
10239: fputs(line,stdout);
10240: fputs(line,ficparo);
10241: fputs(line,ficlog);
10242: continue;
10243: }else
10244: break;
10245: }
10246:
1.209 brouard 10247: /* while((c=getc(ficpar))=='#' && c!= EOF){ */
10248: /* ungetc(c,ficpar); */
10249: /* fgets(line, MAXLINE, ficpar); */
10250: /* fputs(line,stdout); */
10251: /* fputs(line,ficparo); */
10252: /* } */
10253: /* ungetc(c,ficpar); */
1.126 brouard 10254:
10255: estepm=0;
1.209 brouard 10256: if((num_filled=sscanf(line,"agemin=%lf agemax=%lf bage=%lf fage=%lf estepm=%d ftolpl=%lf\n",&ageminpar,&agemaxpar, &bage, &fage, &estepm, &ftolpl)) !=EOF){
1.225 brouard 10257:
10258: if (num_filled != 6) {
10259: 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);
10260: 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);
10261: goto end;
10262: }
10263: printf("agemin=%lf agemax=%lf bage=%lf fage=%lf estepm=%d ftolpl=%lf\n",ageminpar,agemaxpar, bage, fage, estepm, ftolpl);
10264: }
10265: /* ftolpl=6*ftol*1.e5; /\* 6.e-3 make convergences in less than 80 loops for the prevalence limit *\/ */
10266: /*ftolpl=6.e-4;*/ /* 6.e-3 make convergences in less than 80 loops for the prevalence limit */
10267:
1.209 brouard 10268: /* fscanf(ficpar,"agemin=%lf agemax=%lf bage=%lf fage=%lf estepm=%d ftolpl=%\n",&ageminpar,&agemaxpar, &bage, &fage, &estepm); */
1.126 brouard 10269: if (estepm==0 || estepm < stepm) estepm=stepm;
10270: if (fage <= 2) {
10271: bage = ageminpar;
10272: fage = agemaxpar;
10273: }
10274:
10275: fprintf(ficres,"# agemin agemax for life expectancy, bage fage (if mle==0 ie no data nor Max likelihood).\n");
1.211 brouard 10276: fprintf(ficres,"agemin=%.0f agemax=%.0f bage=%.0f fage=%.0f estepm=%d ftolpl=%e\n",ageminpar,agemaxpar,bage,fage, estepm, ftolpl);
10277: fprintf(ficparo,"agemin=%.0f agemax=%.0f bage=%.0f fage=%.0f estepm=%d, ftolpl=%e\n",ageminpar,agemaxpar,bage,fage, estepm, ftolpl);
1.220 brouard 10278:
1.186 brouard 10279: /* Other stuffs, more or less useful */
1.126 brouard 10280: while((c=getc(ficpar))=='#' && c!= EOF){
10281: ungetc(c,ficpar);
10282: fgets(line, MAXLINE, ficpar);
1.141 brouard 10283: fputs(line,stdout);
1.126 brouard 10284: fputs(line,ficparo);
10285: }
10286: ungetc(c,ficpar);
10287:
10288: fscanf(ficpar,"begin-prev-date=%lf/%lf/%lf end-prev-date=%lf/%lf/%lf mov_average=%d\n",&jprev1, &mprev1,&anprev1,&jprev2, &mprev2,&anprev2,&mobilav);
10289: 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);
10290: 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);
10291: printf("begin-prev-date=%.lf/%.lf/%.lf end-prev-date=%.lf/%.lf/%.lf mov_average=%d\n",jprev1, mprev1,anprev1,jprev2, mprev2,anprev2,mobilav);
10292: 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);
10293:
10294: while((c=getc(ficpar))=='#' && c!= EOF){
10295: ungetc(c,ficpar);
10296: fgets(line, MAXLINE, ficpar);
1.141 brouard 10297: fputs(line,stdout);
1.126 brouard 10298: fputs(line,ficparo);
10299: }
10300: ungetc(c,ficpar);
10301:
10302:
10303: dateprev1=anprev1+(mprev1-1)/12.+(jprev1-1)/365.;
10304: dateprev2=anprev2+(mprev2-1)/12.+(jprev2-1)/365.;
10305:
10306: fscanf(ficpar,"pop_based=%d\n",&popbased);
1.193 brouard 10307: fprintf(ficlog,"pop_based=%d\n",popbased);
1.126 brouard 10308: fprintf(ficparo,"pop_based=%d\n",popbased);
10309: fprintf(ficres,"pop_based=%d\n",popbased);
10310:
10311: while((c=getc(ficpar))=='#' && c!= EOF){
10312: ungetc(c,ficpar);
10313: fgets(line, MAXLINE, ficpar);
1.141 brouard 10314: fputs(line,stdout);
1.126 brouard 10315: fputs(line,ficparo);
10316: }
10317: ungetc(c,ficpar);
10318:
10319: fscanf(ficpar,"prevforecast=%d starting-proj-date=%lf/%lf/%lf final-proj-date=%lf/%lf/%lf mobil_average=%d\n",&prevfcast,&jproj1,&mproj1,&anproj1,&jproj2,&mproj2,&anproj2,&mobilavproj);
10320: 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);
10321: 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);
10322: 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);
10323: 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);
10324: /* day and month of proj2 are not used but only year anproj2.*/
10325:
1.217 brouard 10326: while((c=getc(ficpar))=='#' && c!= EOF){
10327: ungetc(c,ficpar);
10328: fgets(line, MAXLINE, ficpar);
10329: fputs(line,stdout);
10330: fputs(line,ficparo);
10331: }
10332: ungetc(c,ficpar);
10333:
10334: fscanf(ficpar,"backcast=%d starting-back-date=%lf/%lf/%lf final-back-date=%lf/%lf/%lf mobil_average=%d\n",&backcast,&jback1,&mback1,&anback1,&jback2,&mback2,&anback2,&mobilavproj);
1.223 brouard 10335: fprintf(ficparo,"backcast=%d starting-back-date=%.lf/%.lf/%.lf final-back-date=%.lf/%.lf/%.lf mobil_average=%d\n",backcast,jback1,mback1,anback1,jback2,mback2,anback2,mobilavproj);
10336: fprintf(ficlog,"backcast=%d starting-back-date=%.lf/%.lf/%.lf final-back-date=%.lf/%.lf/%.lf mobil_average=%d\n",backcast,jback1,mback1,anback1,jback2,mback2,anback2,mobilavproj);
10337: fprintf(ficres,"backcast=%d starting-back-date=%.lf/%.lf/%.lf final-back-date=%.lf/%.lf/%.lf mobil_average=%d\n",backcast,jback1,mback1,anback1,jback2,mback2,anback2,mobilavproj);
1.217 brouard 10338: /* day and month of proj2 are not used but only year anproj2.*/
1.126 brouard 10339:
1.230 brouard 10340: /* Results */
10341: while(fgets(line, MAXLINE, ficpar)) {
10342: /* If line starts with a # it is a comment */
10343: if (line[0] == '#') {
10344: numlinepar++;
10345: fputs(line,stdout);
10346: fputs(line,ficparo);
10347: fputs(line,ficlog);
10348: continue;
10349: }else
10350: break;
10351: }
10352: while((num_filled=sscanf(line,"result:%[^\n]\n",resultline)) !=EOF){
10353: if (num_filled == 0)
10354: resultline[0]='\0';
10355: else if (num_filled != 1){
10356: printf("ERROR %d: result line should be at minimum 'result=' %s\n",num_filled, line);
10357: }
10358: printf("Result %d: result line should be at minimum 'line=' %s, result=%s\n",num_filled, line, resultline);
10359: decoderesult(resultline);
10360: while(fgets(line, MAXLINE, ficpar)) {
10361: /* If line starts with a # it is a comment */
10362: if (line[0] == '#') {
10363: numlinepar++;
10364: fputs(line,stdout);
10365: fputs(line,ficparo);
10366: fputs(line,ficlog);
10367: continue;
10368: }else
10369: break;
10370: }
10371: if (feof(ficpar))
10372: break;
10373: else{ /* Processess output results for this combination of covariate values */
10374: }
10375: }
10376:
10377:
1.126 brouard 10378:
1.230 brouard 10379: /* freqsummary(fileres, agemin, agemax, s, agev, nlstate, imx,Tvaraff,nbcode, ncodemax,mint,anint); */
1.145 brouard 10380: /* ,dateprev1,dateprev2,jprev1, mprev1,anprev1,jprev2, mprev2,anprev2); */
1.126 brouard 10381:
10382: replace_back_to_slash(pathc,pathcd); /* Even gnuplot wants a / */
1.194 brouard 10383: if(ageminpar == AGEOVERFLOW ||agemaxpar == -AGEOVERFLOW){
1.230 brouard 10384: printf("Warning! Error in gnuplot file with ageminpar %f or agemaxpar %f overflow\n\
1.194 brouard 10385: This is probably because your parameter file doesn't \n contain the exact number of lines (or columns) corresponding to your model line.\n\
10386: Please run with mle=-1 to get a correct covariance matrix.\n",ageminpar,agemaxpar);
1.230 brouard 10387: fprintf(ficlog,"Warning! Error in gnuplot file with ageminpar %f or agemaxpar %f overflow\n\
1.194 brouard 10388: This is probably because your parameter file doesn't \n contain the exact number of lines (or columns) corresponding to your model line.\n\
10389: Please run with mle=-1 to get a correct covariance matrix.\n",ageminpar,agemaxpar);
1.220 brouard 10390: }else{
1.218 brouard 10391: printinggnuplot(fileresu, optionfilefiname,ageminpar,agemaxpar,fage, prevfcast, backcast, pathc,p);
1.220 brouard 10392: }
10393: printinghtml(fileresu,title,datafile, firstpass, lastpass, stepm, weightopt, \
1.225 brouard 10394: model,imx,jmin,jmax,jmean,rfileres,popforecast,prevfcast,backcast, estepm, \
10395: jprev1,mprev1,anprev1,dateprev1,jprev2,mprev2,anprev2,dateprev2);
1.220 brouard 10396:
1.225 brouard 10397: /*------------ free_vector -------------*/
10398: /* chdir(path); */
1.220 brouard 10399:
1.215 brouard 10400: /* free_ivector(wav,1,imx); */ /* Moved after last prevalence call */
10401: /* free_imatrix(dh,1,lastpass-firstpass+2,1,imx); */
10402: /* free_imatrix(bh,1,lastpass-firstpass+2,1,imx); */
10403: /* free_imatrix(mw,1,lastpass-firstpass+2,1,imx); */
1.126 brouard 10404: free_lvector(num,1,n);
10405: free_vector(agedc,1,n);
10406: /*free_matrix(covar,0,NCOVMAX,1,n);*/
10407: /*free_matrix(covar,1,NCOVMAX,1,n);*/
10408: fclose(ficparo);
10409: fclose(ficres);
1.220 brouard 10410:
10411:
1.186 brouard 10412: /* Other results (useful)*/
1.220 brouard 10413:
10414:
1.126 brouard 10415: /*--------------- Prevalence limit (period or stable prevalence) --------------*/
1.180 brouard 10416: /*#include "prevlim.h"*/ /* Use ficrespl, ficlog */
10417: prlim=matrix(1,nlstate,1,nlstate);
1.209 brouard 10418: prevalence_limit(p, prlim, ageminpar, agemaxpar, ftolpl, &ncvyear);
1.126 brouard 10419: fclose(ficrespl);
10420:
10421: /*------------- h Pij x at various ages ------------*/
1.180 brouard 10422: /*#include "hpijx.h"*/
10423: hPijx(p, bage, fage);
1.145 brouard 10424: fclose(ficrespij);
1.227 brouard 10425:
1.220 brouard 10426: /* ncovcombmax= pow(2,cptcoveff); */
1.219 brouard 10427: /*-------------- Variance of one-step probabilities---*/
1.145 brouard 10428: k=1;
1.126 brouard 10429: varprob(optionfilefiname, matcov, p, delti, nlstate, bage, fage,k,Tvar,nbcode, ncodemax,strstart);
1.227 brouard 10430:
1.219 brouard 10431: /* Prevalence for each covariates in probs[age][status][cov] */
1.218 brouard 10432: probs= ma3x(1,AGESUP,1,nlstate+ndeath, 1,ncovcombmax);
1.126 brouard 10433: for(i=1;i<=AGESUP;i++)
1.219 brouard 10434: for(j=1;j<=nlstate+ndeath;j++) /* ndeath is useless but a necessity to be compared with mobaverages */
1.225 brouard 10435: for(k=1;k<=ncovcombmax;k++)
10436: probs[i][j][k]=0.;
1.219 brouard 10437: prevalence(probs, ageminpar, agemaxpar, s, agev, nlstate, imx, Tvar, nbcode, ncodemax, mint, anint, dateprev1, dateprev2, firstpass, lastpass);
10438: if (mobilav!=0 ||mobilavproj !=0 ) {
10439: mobaverages= ma3x(1, AGESUP,1,nlstate+ndeath, 1,ncovcombmax);
1.227 brouard 10440: for(i=1;i<=AGESUP;i++)
10441: for(j=1;j<=nlstate;j++)
10442: for(k=1;k<=ncovcombmax;k++)
10443: mobaverages[i][j][k]=0.;
1.219 brouard 10444: mobaverage=mobaverages;
10445: if (mobilav!=0) {
1.227 brouard 10446: if (movingaverage(probs, ageminpar, agemaxpar, mobaverage, mobilav)!=0){
10447: fprintf(ficlog," Error in movingaverage mobilav=%d\n",mobilav);
10448: printf(" Error in movingaverage mobilav=%d\n",mobilav);
10449: }
1.219 brouard 10450: }
10451: /* /\* Prevalence for each covariates in probs[age][status][cov] *\/ */
10452: /* prevalence(probs, ageminpar, agemaxpar, s, agev, nlstate, imx, Tvar, nbcode, ncodemax, mint, anint, dateprev1, dateprev2, firstpass, lastpass); */
10453: else if (mobilavproj !=0) {
1.227 brouard 10454: if (movingaverage(probs, ageminpar, agemaxpar, mobaverage, mobilavproj)!=0){
10455: fprintf(ficlog," Error in movingaverage mobilavproj=%d\n",mobilavproj);
10456: printf(" Error in movingaverage mobilavproj=%d\n",mobilavproj);
10457: }
1.219 brouard 10458: }
10459: }/* end if moving average */
1.227 brouard 10460:
1.126 brouard 10461: /*---------- Forecasting ------------------*/
10462: /*if((stepm == 1) && (strcmp(model,".")==0)){*/
10463: if(prevfcast==1){
10464: /* if(stepm ==1){*/
1.225 brouard 10465: prevforecast(fileresu, anproj1, mproj1, jproj1, agemin, agemax, dateprev1, dateprev2, mobilavproj, bage, fage, firstpass, lastpass, anproj2, p, cptcoveff);
1.126 brouard 10466: }
1.217 brouard 10467: if(backcast==1){
1.219 brouard 10468: ddnewms=matrix(1,nlstate+ndeath,1,nlstate+ndeath);
10469: ddoldms=matrix(1,nlstate+ndeath,1,nlstate+ndeath);
10470: ddsavms=matrix(1,nlstate+ndeath,1,nlstate+ndeath);
10471:
10472: /*--------------- Back Prevalence limit (period or stable prevalence) --------------*/
10473:
10474: bprlim=matrix(1,nlstate,1,nlstate);
10475: back_prevalence_limit(p, bprlim, ageminpar, agemaxpar, ftolpl, &ncvyear, dateprev1, dateprev2, firstpass, lastpass, mobilavproj);
10476: fclose(ficresplb);
10477:
1.222 brouard 10478: hBijx(p, bage, fage, mobaverage);
10479: fclose(ficrespijb);
1.219 brouard 10480: free_matrix(bprlim,1,nlstate,1,nlstate); /*here or after loop ? */
10481:
10482: /* prevbackforecast(fileresu, anback1, mback1, jback1, agemin, agemax, dateprev1, dateprev2, mobilavproj,
1.225 brouard 10483: bage, fage, firstpass, lastpass, anback2, p, cptcoveff); */
1.219 brouard 10484: free_matrix(ddnewms, 1, nlstate+ndeath, 1, nlstate+ndeath);
10485: free_matrix(ddsavms, 1, nlstate+ndeath, 1, nlstate+ndeath);
10486: free_matrix(ddoldms, 1, nlstate+ndeath, 1, nlstate+ndeath);
10487: }
1.217 brouard 10488:
1.186 brouard 10489:
10490: /* ------ Other prevalence ratios------------ */
1.126 brouard 10491:
1.215 brouard 10492: free_ivector(wav,1,imx);
10493: free_imatrix(dh,1,lastpass-firstpass+2,1,imx);
10494: free_imatrix(bh,1,lastpass-firstpass+2,1,imx);
10495: free_imatrix(mw,1,lastpass-firstpass+2,1,imx);
1.218 brouard 10496:
10497:
1.127 brouard 10498: /*---------- Health expectancies, no variances ------------*/
1.218 brouard 10499:
1.201 brouard 10500: strcpy(filerese,"E_");
10501: strcat(filerese,fileresu);
1.126 brouard 10502: if((ficreseij=fopen(filerese,"w"))==NULL) {
10503: printf("Problem with Health Exp. resultfile: %s\n", filerese); exit(0);
10504: fprintf(ficlog,"Problem with Health Exp. resultfile: %s\n", filerese); exit(0);
10505: }
1.208 brouard 10506: printf("Computing Health Expectancies: result on file '%s' ...", filerese);fflush(stdout);
10507: fprintf(ficlog,"Computing Health Expectancies: result on file '%s' ...", filerese);fflush(ficlog);
1.219 brouard 10508:
1.227 brouard 10509: for (k=1; k <= (int) pow(2,cptcoveff); k++){ /* For any combination of dummy covariates, fixed and varying */
1.219 brouard 10510: fprintf(ficreseij,"\n#****** ");
1.225 brouard 10511: for(j=1;j<=cptcoveff;j++) {
1.227 brouard 10512: fprintf(ficreseij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
1.219 brouard 10513: }
10514: fprintf(ficreseij,"******\n");
10515:
10516: eij=ma3x(1,nlstate,1,nlstate,(int) bage, (int) fage);
10517: oldm=oldms;savm=savms;
10518: evsij(eij, p, nlstate, stepm, (int) bage, (int)fage, oldm, savm, k, estepm, strstart);
1.127 brouard 10519:
1.219 brouard 10520: free_ma3x(eij,1,nlstate,1,nlstate,(int) bage, (int)fage);
1.127 brouard 10521: }
10522: fclose(ficreseij);
1.208 brouard 10523: printf("done evsij\n");fflush(stdout);
10524: fprintf(ficlog,"done evsij\n");fflush(ficlog);
1.218 brouard 10525:
1.227 brouard 10526: /*---------- State-specific expectancies and variances ------------*/
1.218 brouard 10527:
10528:
1.201 brouard 10529: strcpy(filerest,"T_");
10530: strcat(filerest,fileresu);
1.127 brouard 10531: if((ficrest=fopen(filerest,"w"))==NULL) {
10532: printf("Problem with total LE resultfile: %s\n", filerest);goto end;
10533: fprintf(ficlog,"Problem with total LE resultfile: %s\n", filerest);goto end;
10534: }
1.208 brouard 10535: printf("Computing Total Life expectancies with their standard errors: file '%s' ...\n", filerest); fflush(stdout);
10536: fprintf(ficlog,"Computing Total Life expectancies with their standard errors: file '%s' ...\n", filerest); fflush(ficlog);
1.218 brouard 10537:
1.126 brouard 10538:
1.201 brouard 10539: strcpy(fileresstde,"STDE_");
10540: strcat(fileresstde,fileresu);
1.126 brouard 10541: if((ficresstdeij=fopen(fileresstde,"w"))==NULL) {
1.227 brouard 10542: printf("Problem with State specific Exp. and std errors resultfile: %s\n", fileresstde); exit(0);
10543: fprintf(ficlog,"Problem with State specific Exp. and std errors resultfile: %s\n", fileresstde); exit(0);
1.126 brouard 10544: }
1.227 brouard 10545: printf(" Computing State-specific Expectancies and standard errors: result on file '%s' \n", fileresstde);
10546: fprintf(ficlog," Computing State-specific Expectancies and standard errors: result on file '%s' \n", fileresstde);
1.126 brouard 10547:
1.201 brouard 10548: strcpy(filerescve,"CVE_");
10549: strcat(filerescve,fileresu);
1.126 brouard 10550: if((ficrescveij=fopen(filerescve,"w"))==NULL) {
1.227 brouard 10551: printf("Problem with Covar. State-specific Exp. resultfile: %s\n", filerescve); exit(0);
10552: fprintf(ficlog,"Problem with Covar. State-specific Exp. resultfile: %s\n", filerescve); exit(0);
1.126 brouard 10553: }
1.227 brouard 10554: printf(" Computing Covar. of State-specific Expectancies: result on file '%s' \n", filerescve);
10555: fprintf(ficlog," Computing Covar. of State-specific Expectancies: result on file '%s' \n", filerescve);
1.126 brouard 10556:
1.201 brouard 10557: strcpy(fileresv,"V_");
10558: strcat(fileresv,fileresu);
1.126 brouard 10559: if((ficresvij=fopen(fileresv,"w"))==NULL) {
10560: printf("Problem with variance resultfile: %s\n", fileresv);exit(0);
10561: fprintf(ficlog,"Problem with variance resultfile: %s\n", fileresv);exit(0);
10562: }
1.227 brouard 10563: printf(" Computing Variance-covariance of State-specific Expectancies: file '%s' ... ", fileresv);fflush(stdout);
10564: fprintf(ficlog," Computing Variance-covariance of State-specific Expectancies: file '%s' ... ", fileresv);fflush(ficlog);
1.126 brouard 10565:
1.145 brouard 10566: /*for(cptcov=1,k=0;cptcov<=i1;cptcov++){
10567: for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){*/
10568:
1.225 brouard 10569: for (k=1; k <= (int) pow(2,cptcoveff); k++){
1.227 brouard 10570: printf("\n#****** ");
1.208 brouard 10571: fprintf(ficrest,"\n#****** ");
1.227 brouard 10572: fprintf(ficlog,"\n#****** ");
10573: for(j=1;j<=cptcoveff;j++){
10574: printf("V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
10575: fprintf(ficrest,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
10576: fprintf(ficlog,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
10577: }
1.208 brouard 10578: fprintf(ficrest,"******\n");
1.227 brouard 10579: fprintf(ficlog,"******\n");
10580: printf("******\n");
1.208 brouard 10581:
10582: fprintf(ficresstdeij,"\n#****** ");
10583: fprintf(ficrescveij,"\n#****** ");
1.225 brouard 10584: for(j=1;j<=cptcoveff;j++) {
1.227 brouard 10585: fprintf(ficresstdeij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
10586: fprintf(ficrescveij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
1.208 brouard 10587: }
10588: fprintf(ficresstdeij,"******\n");
10589: fprintf(ficrescveij,"******\n");
10590:
10591: fprintf(ficresvij,"\n#****** ");
1.225 brouard 10592: for(j=1;j<=cptcoveff;j++)
1.227 brouard 10593: fprintf(ficresvij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
1.208 brouard 10594: fprintf(ficresvij,"******\n");
10595:
10596: eij=ma3x(1,nlstate,1,nlstate,(int) bage, (int) fage);
10597: oldm=oldms;savm=savms;
1.227 brouard 10598: printf(" cvevsij combination#=%d, ",k);
10599: fprintf(ficlog, " cvevsij combination#=%d, ",k);
1.208 brouard 10600: cvevsij(eij, p, nlstate, stepm, (int) bage, (int)fage, oldm, savm, k, estepm, delti, matcov, strstart);
10601: printf(" end cvevsij \n ");
10602: fprintf(ficlog, " end cvevsij \n ");
10603:
10604: /*
10605: */
10606: /* goto endfree; */
10607:
10608: vareij=ma3x(1,nlstate,1,nlstate,(int) bage, (int) fage);
10609: pstamp(ficrest);
10610:
10611:
10612: for(vpopbased=0; vpopbased <= popbased; vpopbased++){ /* Done for vpopbased=0 and vpopbased=1 if popbased==1*/
1.227 brouard 10613: oldm=oldms;savm=savms; /* ZZ Segmentation fault */
10614: cptcod= 0; /* To be deleted */
10615: printf("varevsij vpopbased=%d \n",vpopbased);
10616: fprintf(ficlog, "varevsij vpopbased=%d \n",vpopbased);
10617: varevsij(optionfilefiname, vareij, matcov, p, delti, nlstate, stepm, (int) bage, (int) fage, oldm, savm, prlim, ftolpl, &ncvyear, k, estepm, cptcov,cptcod,vpopbased,mobilav, strstart); /* cptcod not initialized Intel */
10618: fprintf(ficrest,"# Total life expectancy with std error and decomposition into time to be expected in each health state\n# (weighted average of eij where weights are ");
10619: if(vpopbased==1)
10620: 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);
10621: else
10622: fprintf(ficrest,"the age specific period (stable) prevalences in each health state \n");
10623: fprintf(ficrest,"# Age popbased mobilav e.. (std) ");
10624: for (i=1;i<=nlstate;i++) fprintf(ficrest,"e.%d (std) ",i);
10625: fprintf(ficrest,"\n");
10626: /* printf("Which p?\n"); for(i=1;i<=npar;i++)printf("p[i=%d]=%lf,",i,p[i]);printf("\n"); */
10627: epj=vector(1,nlstate+1);
10628: printf("Computing age specific period (stable) prevalences in each health state \n");
10629: fprintf(ficlog,"Computing age specific period (stable) prevalences in each health state \n");
10630: for(age=bage; age <=fage ;age++){
10631: prevalim(prlim, nlstate, p, age, oldm, savm, ftolpl, &ncvyear, k); /*ZZ Is it the correct prevalim */
10632: if (vpopbased==1) {
10633: if(mobilav ==0){
10634: for(i=1; i<=nlstate;i++)
10635: prlim[i][i]=probs[(int)age][i][k];
10636: }else{ /* mobilav */
10637: for(i=1; i<=nlstate;i++)
10638: prlim[i][i]=mobaverage[(int)age][i][k];
10639: }
10640: }
1.219 brouard 10641:
1.227 brouard 10642: fprintf(ficrest," %4.0f %d %d",age, vpopbased, mobilav);
10643: /* fprintf(ficrest," %4.0f %d %d %d %d",age, vpopbased, mobilav,Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]); */ /* to be done */
10644: /* printf(" age %4.0f ",age); */
10645: for(j=1, epj[nlstate+1]=0.;j <=nlstate;j++){
10646: for(i=1, epj[j]=0.;i <=nlstate;i++) {
10647: epj[j] += prlim[i][i]*eij[i][j][(int)age];
10648: /*ZZZ printf("%lf %lf ", prlim[i][i] ,eij[i][j][(int)age]);*/
10649: /* printf("%lf %lf ", prlim[i][i] ,eij[i][j][(int)age]); */
10650: }
10651: epj[nlstate+1] +=epj[j];
10652: }
10653: /* printf(" age %4.0f \n",age); */
1.219 brouard 10654:
1.227 brouard 10655: for(i=1, vepp=0.;i <=nlstate;i++)
10656: for(j=1;j <=nlstate;j++)
10657: vepp += vareij[i][j][(int)age];
10658: fprintf(ficrest," %7.3f (%7.3f)", epj[nlstate+1],sqrt(vepp));
10659: for(j=1;j <=nlstate;j++){
10660: fprintf(ficrest," %7.3f (%7.3f)", epj[j],sqrt(vareij[j][j][(int)age]));
10661: }
10662: fprintf(ficrest,"\n");
10663: }
1.208 brouard 10664: } /* End vpopbased */
10665: free_ma3x(eij,1,nlstate,1,nlstate,(int) bage, (int)fage);
10666: free_ma3x(vareij,1,nlstate,1,nlstate,(int) bage, (int)fage);
10667: free_vector(epj,1,nlstate+1);
10668: printf("done \n");fflush(stdout);
10669: fprintf(ficlog,"done\n");fflush(ficlog);
10670:
1.145 brouard 10671: /*}*/
1.208 brouard 10672: } /* End k */
1.227 brouard 10673:
10674: printf("done State-specific expectancies\n");fflush(stdout);
10675: fprintf(ficlog,"done State-specific expectancies\n");fflush(ficlog);
10676:
1.126 brouard 10677: /*------- Variance of period (stable) prevalence------*/
1.227 brouard 10678:
1.201 brouard 10679: strcpy(fileresvpl,"VPL_");
10680: strcat(fileresvpl,fileresu);
1.126 brouard 10681: if((ficresvpl=fopen(fileresvpl,"w"))==NULL) {
10682: printf("Problem with variance of period (stable) prevalence resultfile: %s\n", fileresvpl);
10683: exit(0);
10684: }
1.208 brouard 10685: printf("Computing Variance-covariance of period (stable) prevalence: file '%s' ...", fileresvpl);fflush(stdout);
10686: fprintf(ficlog, "Computing Variance-covariance of period (stable) prevalence: file '%s' ...", fileresvpl);fflush(ficlog);
1.227 brouard 10687:
1.145 brouard 10688: /*for(cptcov=1,k=0;cptcov<=i1;cptcov++){
10689: for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){*/
1.227 brouard 10690:
1.225 brouard 10691: for (k=1; k <= (int) pow(2,cptcoveff); k++){
1.227 brouard 10692: fprintf(ficresvpl,"\n#****** ");
10693: printf("\n#****** ");
10694: fprintf(ficlog,"\n#****** ");
10695: for(j=1;j<=cptcoveff;j++) {
10696: fprintf(ficresvpl,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
10697: fprintf(ficlog,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
10698: printf("V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
10699: }
10700: fprintf(ficresvpl,"******\n");
10701: printf("******\n");
10702: fprintf(ficlog,"******\n");
10703:
10704: varpl=matrix(1,nlstate,(int) bage, (int) fage);
10705: oldm=oldms;savm=savms;
10706: varprevlim(fileres, varpl, matcov, p, delti, nlstate, stepm, (int) bage, (int) fage, oldm, savm, prlim, ftolpl, &ncvyear, k, strstart);
10707: free_matrix(varpl,1,nlstate,(int) bage, (int)fage);
1.145 brouard 10708: /*}*/
1.126 brouard 10709: }
1.227 brouard 10710:
1.126 brouard 10711: fclose(ficresvpl);
1.208 brouard 10712: printf("done variance-covariance of period prevalence\n");fflush(stdout);
10713: fprintf(ficlog,"done variance-covariance of period prevalence\n");fflush(ficlog);
1.227 brouard 10714:
10715: free_vector(weight,1,n);
10716: free_imatrix(Tvard,1,NCOVMAX,1,2);
10717: free_imatrix(s,1,maxwav+1,1,n);
10718: free_matrix(anint,1,maxwav,1,n);
10719: free_matrix(mint,1,maxwav,1,n);
10720: free_ivector(cod,1,n);
10721: free_ivector(tab,1,NCOVMAX);
10722: fclose(ficresstdeij);
10723: fclose(ficrescveij);
10724: fclose(ficresvij);
10725: fclose(ficrest);
10726: fclose(ficpar);
10727:
10728:
1.126 brouard 10729: /*---------- End : free ----------------*/
1.219 brouard 10730: if (mobilav!=0 ||mobilavproj !=0)
10731: free_ma3x(mobaverages,1, AGESUP,1,nlstate+ndeath, 1,ncovcombmax); /* We need to have a squared matrix with prevalence of the dead! */
1.218 brouard 10732: free_ma3x(probs,1,AGESUP,1,nlstate+ndeath, 1,ncovcombmax);
1.220 brouard 10733: free_matrix(prlim,1,nlstate,1,nlstate); /*here or after loop ? */
10734: free_matrix(pmmij,1,nlstate+ndeath,1,nlstate+ndeath);
1.126 brouard 10735: } /* mle==-3 arrives here for freeing */
1.227 brouard 10736: /* endfree:*/
10737: free_matrix(oldms, 1,nlstate+ndeath,1,nlstate+ndeath);
10738: free_matrix(newms, 1,nlstate+ndeath,1,nlstate+ndeath);
10739: free_matrix(savms, 1,nlstate+ndeath,1,nlstate+ndeath);
10740: free_ma3x(cotqvar,1,maxwav,1,nqtv,1,n);
10741: free_ma3x(cotvar,1,maxwav,1,ntv,1,n);
10742: free_matrix(coqvar,1,maxwav,1,n);
10743: free_matrix(covar,0,NCOVMAX,1,n);
10744: free_matrix(matcov,1,npar,1,npar);
10745: free_matrix(hess,1,npar,1,npar);
10746: /*free_vector(delti,1,npar);*/
10747: free_ma3x(delti3,1,nlstate,1, nlstate+ndeath-1,1,ncovmodel);
10748: free_matrix(agev,1,maxwav,1,imx);
10749: free_ma3x(param,1,nlstate,1, nlstate+ndeath-1,1,ncovmodel);
10750:
10751: free_ivector(ncodemax,1,NCOVMAX);
10752: free_ivector(ncodemaxwundef,1,NCOVMAX);
10753: free_ivector(Dummy,-1,NCOVMAX);
10754: free_ivector(Fixed,-1,NCOVMAX);
10755: free_ivector(Typevar,-1,NCOVMAX);
10756: free_ivector(Tvar,1,NCOVMAX);
1.231 ! brouard 10757: free_ivector(TvarFD,1,NCOVMAX);
! 10758: free_ivector(TvarFDind,1,NCOVMAX);
! 10759: free_ivector(TvarFQ,1,NCOVMAX);
! 10760: free_ivector(TvarFQind,1,NCOVMAX);
! 10761: free_ivector(TvarVD,1,NCOVMAX);
! 10762: free_ivector(TvarVDind,1,NCOVMAX);
! 10763: free_ivector(TvarVQ,1,NCOVMAX);
! 10764: free_ivector(TvarVQind,1,NCOVMAX);
1.230 brouard 10765: free_ivector(Tvarsel,1,NCOVMAX);
10766: free_vector(Tvalsel,1,NCOVMAX);
1.227 brouard 10767: free_ivector(Tposprod,1,NCOVMAX);
10768: free_ivector(Tprod,1,NCOVMAX);
10769: free_ivector(Tvaraff,1,NCOVMAX);
10770: free_ivector(invalidvarcomb,1,ncovcombmax);
10771: free_ivector(Tage,1,NCOVMAX);
10772: free_ivector(Tmodelind,1,NCOVMAX);
1.228 brouard 10773: free_ivector(TmodelInvind,1,NCOVMAX);
10774: free_ivector(TmodelInvQind,1,NCOVMAX);
1.227 brouard 10775:
10776: free_imatrix(nbcode,0,NCOVMAX,0,NCOVMAX);
10777: /* free_imatrix(codtab,1,100,1,10); */
1.126 brouard 10778: fflush(fichtm);
10779: fflush(ficgp);
10780:
1.227 brouard 10781:
1.126 brouard 10782: if((nberr >0) || (nbwarn>0)){
1.216 brouard 10783: printf("End of Imach with %d errors and/or %d warnings. Please look at the log file for details.\n",nberr,nbwarn);
10784: fprintf(ficlog,"End of Imach with %d errors and/or warnings %d. Please look at the log file for details.\n",nberr,nbwarn);
1.126 brouard 10785: }else{
10786: printf("End of Imach\n");
10787: fprintf(ficlog,"End of Imach\n");
10788: }
10789: printf("See log file on %s\n",filelog);
10790: /* gettimeofday(&end_time, (struct timezone*)0);*/ /* after time */
1.157 brouard 10791: /*(void) gettimeofday(&end_time,&tzp);*/
10792: rend_time = time(NULL);
10793: end_time = *localtime(&rend_time);
10794: /* tml = *localtime(&end_time.tm_sec); */
10795: strcpy(strtend,asctime(&end_time));
1.126 brouard 10796: printf("Local time at start %s\nLocal time at end %s",strstart, strtend);
10797: fprintf(ficlog,"Local time at start %s\nLocal time at end %s\n",strstart, strtend);
1.157 brouard 10798: printf("Total time used %s\n", asc_diff_time(rend_time -rstart_time,tmpout));
1.227 brouard 10799:
1.157 brouard 10800: printf("Total time was %.0lf Sec.\n", difftime(rend_time,rstart_time));
10801: fprintf(ficlog,"Total time used %s\n", asc_diff_time(rend_time -rstart_time,tmpout));
10802: fprintf(ficlog,"Total time was %.0lf Sec.\n", difftime(rend_time,rstart_time));
1.126 brouard 10803: /* printf("Total time was %d uSec.\n", total_usecs);*/
10804: /* if(fileappend(fichtm,optionfilehtm)){ */
10805: fprintf(fichtm,"<br>Local time at start %s<br>Local time at end %s<br>\n</body></html>",strstart, strtend);
10806: fclose(fichtm);
10807: fprintf(fichtmcov,"<br>Local time at start %s<br>Local time at end %s<br>\n</body></html>",strstart, strtend);
10808: fclose(fichtmcov);
10809: fclose(ficgp);
10810: fclose(ficlog);
10811: /*------ End -----------*/
1.227 brouard 10812:
10813:
10814: printf("Before Current directory %s!\n",pathcd);
1.184 brouard 10815: #ifdef WIN32
1.227 brouard 10816: if (_chdir(pathcd) != 0)
10817: printf("Can't move to directory %s!\n",path);
10818: if(_getcwd(pathcd,MAXLINE) > 0)
1.184 brouard 10819: #else
1.227 brouard 10820: if(chdir(pathcd) != 0)
10821: printf("Can't move to directory %s!\n", path);
10822: if (getcwd(pathcd, MAXLINE) > 0)
1.184 brouard 10823: #endif
1.126 brouard 10824: printf("Current directory %s!\n",pathcd);
10825: /*strcat(plotcmd,CHARSEPARATOR);*/
10826: sprintf(plotcmd,"gnuplot");
1.157 brouard 10827: #ifdef _WIN32
1.126 brouard 10828: sprintf(plotcmd,"\"%sgnuplot.exe\"",pathimach);
10829: #endif
10830: if(!stat(plotcmd,&info)){
1.158 brouard 10831: printf("Error or gnuplot program not found: '%s'\n",plotcmd);fflush(stdout);
1.126 brouard 10832: if(!stat(getenv("GNUPLOTBIN"),&info)){
1.158 brouard 10833: printf("Error or gnuplot program not found: '%s' Environment GNUPLOTBIN not set.\n",plotcmd);fflush(stdout);
1.126 brouard 10834: }else
10835: strcpy(pplotcmd,plotcmd);
1.157 brouard 10836: #ifdef __unix
1.126 brouard 10837: strcpy(plotcmd,GNUPLOTPROGRAM);
10838: if(!stat(plotcmd,&info)){
1.158 brouard 10839: printf("Error gnuplot program not found: '%s'\n",plotcmd);fflush(stdout);
1.126 brouard 10840: }else
10841: strcpy(pplotcmd,plotcmd);
10842: #endif
10843: }else
10844: strcpy(pplotcmd,plotcmd);
10845:
10846: sprintf(plotcmd,"%s %s",pplotcmd, optionfilegnuplot);
1.158 brouard 10847: printf("Starting graphs with: '%s'\n",plotcmd);fflush(stdout);
1.227 brouard 10848:
1.126 brouard 10849: if((outcmd=system(plotcmd)) != 0){
1.158 brouard 10850: printf("gnuplot command might not be in your path: '%s', err=%d\n", plotcmd, outcmd);
1.154 brouard 10851: printf("\n Trying if gnuplot resides on the same directory that IMaCh\n");
1.152 brouard 10852: sprintf(plotcmd,"%sgnuplot %s", pathimach, optionfilegnuplot);
1.150 brouard 10853: if((outcmd=system(plotcmd)) != 0)
1.153 brouard 10854: printf("\n Still a problem with gnuplot command %s, err=%d\n", plotcmd, outcmd);
1.126 brouard 10855: }
1.158 brouard 10856: printf(" Successful, please wait...");
1.126 brouard 10857: while (z[0] != 'q') {
10858: /* chdir(path); */
1.154 brouard 10859: printf("\nType e to edit results with your browser, g to graph again and q for exit: ");
1.126 brouard 10860: scanf("%s",z);
10861: /* if (z[0] == 'c') system("./imach"); */
10862: if (z[0] == 'e') {
1.158 brouard 10863: #ifdef __APPLE__
1.152 brouard 10864: sprintf(pplotcmd, "open %s", optionfilehtm);
1.157 brouard 10865: #elif __linux
10866: sprintf(pplotcmd, "xdg-open %s", optionfilehtm);
1.153 brouard 10867: #else
1.152 brouard 10868: sprintf(pplotcmd, "%s", optionfilehtm);
1.153 brouard 10869: #endif
10870: printf("Starting browser with: %s",pplotcmd);fflush(stdout);
10871: system(pplotcmd);
1.126 brouard 10872: }
10873: else if (z[0] == 'g') system(plotcmd);
10874: else if (z[0] == 'q') exit(0);
10875: }
1.227 brouard 10876: end:
1.126 brouard 10877: while (z[0] != 'q') {
1.195 brouard 10878: printf("\nType q for exiting: "); fflush(stdout);
1.126 brouard 10879: scanf("%s",z);
10880: }
10881: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>