1 /*
2  *  gretl -- Gnu Regression, Econometrics and Time-series Library
3  *  Copyright (C) 2001 Allin Cottrell and Riccardo "Jack" Lucchetti
4  *
5  *  This program is free software: you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation, either version 3 of the License, or
8  *  (at your option) any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  *
18  */
19 
20 #include "libgretl.h"
21 #include "libset.h"
22 #include "system.h"
23 #include "texprint.h"
24 #include "usermat.h"
25 #include "gretl_midas.h"
26 
27 static int
28 plain_print_coefficients (const MODEL *pmod, const DATASET *dset, PRN *prn);
29 static int
30 alt_print_coefficients (const MODEL *pmod, const DATASET *dset, PRN *prn);
31 static void alt_print_rho_terms (const MODEL *pmod, PRN *prn);
32 static void logit_probit_stats (const MODEL *pmod, PRN *prn);
33 static void print_arma_roots (const MODEL *pmod, PRN *prn);
34 static void print_heckit_stats (const MODEL *pmod, PRN *prn);
35 
36 #define RTFTAB "\\par \\ql \\tab "
37 
38 #define XDIGITS(m) (((m)->ci == MPOLS)? GRETL_MP_DIGITS : get_gretl_digits())
39 
40 #define FDIGITS(m) (((m)->ci == MPOLS)? GRETL_MP_DIGITS : 5)
41 
42 #define ordered_model(m) ((m->ci == LOGIT || m->ci == PROBIT) &&	\
43 			  gretl_model_get_int(m, "ordered"))
44 
45 #define binary_model(m) ((m->ci == LOGIT || m->ci == PROBIT) && \
46                          !gretl_model_get_int(m, "ordered") &&	\
47                          !gretl_model_get_int(m, "multinom"))
48 
49 #define multinomial_model(m) (m->ci == LOGIT && gretl_model_get_int(m, "multinom"))
50 
51 #define logit_probit_model(m) (m->ci == LOGIT || m->ci == PROBIT)
52 
53 #define re_probit_model(m) (m->ci == PROBIT && (m->opt & OPT_E))
54 
55 #define liml_equation(m) (gretl_model_get_int(m, "method") == SYS_METHOD_LIML)
56 
57 #define tsls_model(m) (m->ci == IVREG &&	\
58                        !(m->opt & OPT_L) &&	\
59 	               !(m->opt & OPT_G) &&	\
60                        !m->aux)
61 
62 #define liml_model(m) (m->ci == IVREG && (m->opt & OPT_L))
63 
64 #define gmm_model(m) (m->ci == GMM || (m->ci == IVREG && (m->opt & OPT_G)))
65 
66 #define intreg_model(m) (m->ci == INTREG || m->ci == TOBIT)
67 
68 #define hessian_maybe_fishy(m) (m->ci == ARMA ||			\
69 				(m->ci == PROBIT && (m->opt & OPT_E)))
70 
model_coeff_init(model_coeff * mc)71 void model_coeff_init (model_coeff *mc)
72 {
73     mc->b = NADBL;
74     mc->se = NADBL;
75     mc->tval = NADBL;
76     mc->pval = NADBL;
77     mc->slope = NADBL;
78     mc->lo = mc->hi = NADBL;
79     mc->show_tval = 1;
80     mc->show_pval = 1;
81     mc->df_pval = 0;
82     mc->multi = 0;
83     mc->name[0] = '\0';
84 }
85 
glyph_count(const char * str)86 static int glyph_count (const char *str)
87 {
88     gunichar *u;
89     glong nu;
90     int i, ng = 0;
91 
92     u = g_utf8_to_ucs4_fast(str, -1, &nu);
93 
94     for (i=0; i<nu; i++) {
95 	if (g_unichar_iswide(u[i])) {
96 	    ng += 2;
97 	} else if (u[i] > 0) {
98 	    ng++;
99 	}
100     }
101 
102     g_free(u);
103 
104     return ng;
105 }
106 
char_len(const char * s)107 static int char_len (const char *s)
108 {
109     static int ea = -1;
110 
111     if (ea < 0) {
112 	ea = east_asian_locale();
113     }
114 
115     if (g_utf8_validate(s, -1, NULL)) {
116 	return ea ? glyph_count(s) : g_utf8_strlen(s, -1);
117     } else {
118 	return strlen(s);
119     }
120 }
121 
plain_print_double(char * s,int d,double x,PRN * prn)122 static void plain_print_double (char *s, int d, double x, PRN *prn)
123 {
124     if (na(x)) {
125 	strcpy(s, "NA");
126     } else if (x < 0 && gretl_print_has_minus(prn)) {
127 	char tmp[32];
128 
129 	*s = '\0';
130 	strcat(s, "−"); /* U+2212: minus */
131 	sprintf(tmp, "%.*g", d, -x);
132 	strcat(s, tmp);
133     } else {
134 	sprintf(s, "%.*g", d, x);
135     }
136 }
137 
138 /* for use when printing a user-defined model */
139 
print_model_stats_table(const double * stats,const char ** names,int ns,gretlopt opt,PRN * prn)140 static void print_model_stats_table (const double *stats,
141 				     const char **names,
142 				     int ns, gretlopt opt,
143 				     PRN *prn)
144 {
145     int oneline = opt & OPT_I;
146     char tmp1[32], tmp2[32];
147     char pt = get_local_decpoint();
148     char delim;
149     int i, d;
150 
151     if (plain_format(prn)) {
152 	pputc(prn, '\n');
153     } else if (tex_format(prn)) {
154 	pputs(prn, "\\medskip\n\n");
155 	pprintf(prn, "\\begin{tabular}{lr@{%c}l}\n", pt);
156     }
157 
158     d = get_gretl_digits();
159     delim = (pt == ',')? ';' : ',';
160 
161     for (i=0; i<ns; i++) {
162 	if (plain_format(prn)) {
163 	    plain_print_double(tmp1, d, stats[i], prn);
164 	    if (oneline && ns > 1) {
165 		if (i == 0) {
166 		    pprintf(prn, "  %s = %s", names[i], tmp1);
167 		} else if (i == ns-1) {
168 		    pprintf(prn, "%c %s = %s\n", delim, names[i], tmp1);
169 		} else {
170 		    pprintf(prn, "%c %s = %s", delim, names[i], tmp1);;
171 		}
172 	    } else {
173 		pprintf(prn, "  %s = %s\n", names[i], tmp1);
174 	    }
175 	} else if (tex_format(prn)) {
176 	    tex_escape_special(tmp1, names[i]);
177 	    tex_rl_double(stats[i], tmp2);
178 	    pprintf(prn, "%s & %s \\\\\n", tmp1, tmp2);
179 	} else if (rtf_format(prn)) {
180 	    if (na(stats[i])) {
181 		pprintf(prn, RTFTAB "%s = NA\n", names[i]);
182 	    } else {
183 		pprintf(prn, RTFTAB "%s = %g\n", names[i], stats[i]);
184 	    }
185 	} else if (csv_format(prn)) {
186 	    pprintf(prn, "\"%s\"%c%.15g\n", names[i], prn_delim(prn), stats[i]);
187 	}
188     }
189 
190     if (tex_format(prn)) {
191 	pputs(prn, "\\end{tabular}");
192     }
193 }
194 
ensure_vsep(PRN * prn)195 static void ensure_vsep (PRN *prn)
196 {
197     if (tex_format(prn)) {
198 	pputs(prn, "\n\\vspace{1ex}\n");
199     } else if (rtf_format(prn)) {
200 	pputs(prn, "\\par\n");
201     }
202 }
203 
garch_variance_line(const MODEL * pmod,PRN * prn)204 static void garch_variance_line (const MODEL *pmod, PRN *prn)
205 {
206     const char *varstr = N_("Unconditional error variance");
207     double v = pmod->sigma * pmod->sigma;
208     int d = get_gretl_digits();
209 
210     ensure_vsep(prn);
211 
212     if (plain_format(prn)) {
213 	double LR = gretl_model_get_double(pmod, "garch_LR");
214 	int LRdf = gretl_model_get_int(pmod, "garch_LR_df");
215 
216 	pprintf(prn, "%s = %.*g\n", _(varstr), d, v);
217 	if (pmod->opt & OPT_Z) {
218 	    pprintf(prn, "%s\n", _("The residuals are standardized"));
219 	}
220 	if (LR >= 0 && LRdf > 0) {
221 	    pprintf(prn, "%s:\n", _("Likelihood ratio test for (G)ARCH terms"));
222 	    pprintf(prn, "  %s(%d) = %g [%g]\n",
223 		    _("Chi-square"), LRdf, LR, chisq_cdf_comp(LRdf, LR));
224 	}
225 	pputc(prn, '\n');
226     } else if (rtf_format(prn)) {
227 	pprintf(prn, RTFTAB "%s = %g\n", _(varstr), v);
228     } else if (tex_format(prn)) {
229 	pprintf(prn, "%s = %g \\\\\n", _(varstr), v);
230     } else if (csv_format(prn)) {
231 	pprintf(prn, "\"%s\"%c%.15g\n", _(varstr), prn_delim(prn), v);
232     }
233 }
234 
print_intreg_info(const MODEL * pmod,const DATASET * dset,PRN * prn)235 static void print_intreg_info (const MODEL *pmod,
236 			       const DATASET *dset,
237 			       PRN *prn)
238 {
239     const char *nstrs[] = {
240 	N_("Left-unbounded observations"),
241 	N_("Right-unbounded observations"),
242 	N_("Bounded observations"),
243 	N_("Point observations"),
244 	N_("Pseudo-point observations"),
245     };
246     gchar *lstr = NULL, *rstr = NULL;
247     int nl = gretl_model_get_int(pmod, "n_left");
248     int nr = gretl_model_get_int(pmod, "n_right");
249     int nb = -1, np = -1, nfp = -1;
250     int digits = get_gretl_digits();
251     double llim = 0, rlim = NADBL;
252     double se_sigma;
253 
254     if (pmod->ci == INTREG) {
255 	nb = gretl_model_get_int(pmod, "n_both");
256 	np = gretl_model_get_int(pmod, "n_point");
257 	nfp = gretl_model_get_int(pmod, "n_fpoint");
258     } else {
259 	nstrs[0] = N_("Left-censored observations");
260 	nstrs[1] = N_("Right-censored observations");
261 	if (pmod->opt & OPT_L) {
262 	    llim = gretl_model_get_double(pmod, "llimit");
263 	    if (tex_format(prn)) {
264 		lstr = g_strdup_printf(" (%s $\\le$ %g)",
265 				       dset->varname[pmod->list[1]],
266 				       llim);
267 	    } else {
268 		lstr = g_strdup_printf(" (%s <= %g)",
269 				       dset->varname[pmod->list[1]],
270 				       llim);
271 	    }
272 	}
273 	if ((pmod->opt & OPT_M) && nr > 0) {
274 	    rlim = gretl_model_get_double(pmod, "rlimit");
275 	    if (!na(rlim) && tex_format(prn)) {
276 		rstr = g_strdup_printf(" (%s $\\ge$ %g)",
277 				       dset->varname[pmod->list[1]],
278 				       rlim);
279 	    } else if (!na(rlim)) {
280 		rstr = g_strdup_printf(" (%s >= %g)",
281 				       dset->varname[pmod->list[1]],
282 				       rlim);
283 	    }
284 	}
285     }
286 
287     ensure_vsep(prn);
288 
289     se_sigma = gretl_model_get_double(pmod, "se_sigma");
290 
291     if (plain_format(prn)) {
292 	pprintf(prn, "%s = %.*g", _("sigma"), digits, pmod->sigma);
293 	if (!na(se_sigma)) {
294 	    pprintf(prn, " (%g)", se_sigma);
295 	}
296 	pputc(prn, '\n');
297 	pprintf(prn, "%s: %d%s\n", _(nstrs[0]), nl, lstr == NULL ? "" : lstr);
298 	pprintf(prn, "%s: %d%s\n", _(nstrs[1]), nr, rstr == NULL ? "" : rstr);
299 	if (nb >= 0 && np >= 0) {
300 	    pprintf(prn, "%s: %d\n", _(nstrs[2]), nb);
301 	    pprintf(prn, "%s: %d\n", _(nstrs[3]), np);
302 	}
303 	if (nfp > 0) {
304 	    pprintf(prn, "%s: %d\n", _(nstrs[4]), nfp);
305 	}
306 	pputc(prn, '\n');
307     } else if (rtf_format(prn)) {
308 	pprintf(prn, RTFTAB "%s = %g", _("sigma"), pmod->sigma);
309 	if (!na(se_sigma)) {
310 	    pprintf(prn, " (%g)", se_sigma);
311 	}
312 	pputc(prn, '\n');
313 	pprintf(prn, RTFTAB "%s: %d%s\n", _(nstrs[0]), nl, lstr == NULL ? "" : lstr);
314 	pprintf(prn, RTFTAB "%s: %d%s\n", _(nstrs[1]), nr, rstr == NULL ? "" : rstr);
315 	if (nb >= 0 && np >= 0) {
316 	    pprintf(prn, RTFTAB "%s: %d\n", _(nstrs[2]), nb);
317 	    pprintf(prn, RTFTAB "%s: %d\n", _(nstrs[3]), np);
318 	}
319 	if (nfp > 0) {
320 	    pprintf(prn, RTFTAB "%s: %d\n", _(nstrs[4]), nfp);
321 	}
322     } else if (tex_format(prn)) {
323 	pprintf(prn, "$\\hat{\\sigma}$ = %g", pmod->sigma);
324 	if (!na(se_sigma)) {
325 	    pprintf(prn, " (%g)", se_sigma);
326 	}
327 	pputs(prn, " \\\\\n");
328 	pprintf(prn, "%s: %d%s \\\\\n", _(nstrs[0]), nl, lstr == NULL ? "" : lstr);
329 	pprintf(prn, "%s: %d%s \\\\\n", _(nstrs[1]), nr, rstr == NULL ? "" : rstr);
330 	if (nb >= 0 && np >= 0) {
331 	    pprintf(prn, "%s: %d \\\\\n", _(nstrs[2]), nb);
332 	    pprintf(prn, "%s: %d \\\\\n", _(nstrs[3]), np);
333 	}
334 	if (nfp > 0) {
335 	    pprintf(prn, "%s: %d \\\\\n", _(nstrs[4]), nfp);
336 	}
337     } else if (csv_format(prn)) {
338 	int d = prn_delim(prn);
339 
340 	pprintf(prn, "%s%c%.15g\n", _("sigma"), d, pmod->sigma);
341 	pprintf(prn, "\"%s\"%c%d\n", _(nstrs[0]), d, nl);
342 	pprintf(prn, "\"%s\"%c%d\n", _(nstrs[1]), d, nr);
343 	if (nb >= 0 && np >= 0) {
344 	    pprintf(prn, "\"%s\"%c%d\n", _(nstrs[2]), d, nb);
345 	    pprintf(prn, "\"%s\"%c%d\n", _(nstrs[3]), d, np);
346 	}
347 	if (nfp > 0) {
348 	    pprintf(prn, "\"%s\"%c%d\n", _(nstrs[4]), d, nfp);
349 	}
350     }
351 
352     g_free(lstr);
353     g_free(rstr);
354 }
355 
rsqline(const MODEL * pmod,PRN * prn)356 static void rsqline (const MODEL *pmod, PRN *prn)
357 {
358     if (!na(pmod->rsq) && plain_format(prn)) {
359 	pprintf(prn, "  %s = %f\n", _("Unadjusted R-squared"), pmod->rsq);
360     }
361 }
362 
ssrline(const MODEL * pmod,PRN * prn)363 static void ssrline (const MODEL *pmod, PRN *prn)
364 {
365     if (!na(pmod->ess) && plain_format(prn)) {
366 	pprintf(prn, "  %s = %.*g\n", _("Sum of squared residuals"),
367 		XDIGITS(pmod), pmod->ess);
368     }
369 }
370 
rssline(const MODEL * pmod,PRN * prn)371 static void rssline (const MODEL *pmod, PRN *prn)
372 {
373     if (!na(pmod->ess) && !na(pmod->tss) && plain_format(prn)) {
374 	pprintf(prn, "  %s = %.*g\n", _("Explained sum of squares"),
375 		XDIGITS(pmod), pmod->tss - pmod->ess);
376     }
377 }
378 
print_liml_equation_data(const MODEL * pmod,PRN * prn)379 static void print_liml_equation_data (const MODEL *pmod, PRN *prn)
380 {
381     double lmin = gretl_model_get_double(pmod, "lmin");
382     int idf = gretl_model_get_int(pmod, "idf");
383 
384     if (!na(lmin)) {
385 	ensure_vsep(prn);
386 	if (!pmod->aux) {
387 	    if (tex_format(prn)) {
388 		pprintf(prn, "%s = %g\\\\\n", _("Smallest eigenvalue"), lmin);
389 	    } else {
390 		pprintf(prn, "%s = %g\n", _("Smallest eigenvalue"), lmin);
391 	    }
392 	}
393 	if (idf > 0) {
394 	    double X2 = pmod->nobs * log(lmin);
395 	    double pv = chisq_cdf_comp(idf, X2);
396 
397 	    if (tex_format(prn)) {
398 		pprintf(prn, "%s: $\\chi^2(%d)$ = %g [%.4f] \\\\\n",
399 			_("LR over-identification test"), idf, X2, pv);
400 	    } else {
401 		pprintf(prn, "%s: ", _("LR over-identification test"));
402 		pprintf(prn, "%s(%d) = %g [%.4f]\n\n", _("Chi-square"),
403 			idf, X2, pv);
404 	    }
405 	} else if (idf == 0) {
406 	    pprintf(prn, "%s\n\n", _("Equation is just identified"));
407 	}
408     }
409 }
410 
print_panel_AR_test(double z,int order,PRN * prn)411 static void print_panel_AR_test (double z, int order, PRN *prn)
412 {
413     pprintf(prn, _("Test for AR(%d) errors:"), order);
414 
415     if (na(z)) {
416 	if (tex_format(prn)) {
417 	    pputs(prn, " & $z$ = NA");
418 	} else {
419 	    pputs(prn, " z = NA");
420 	}
421     } else {
422 	double pv = normal_pvalue_2(z);
423 
424 	if (tex_format(prn)) {
425 	    char numstr[32];
426 
427 	    tex_sprint_double_digits(z, numstr, 4);
428 	    pprintf(prn, " & $z$ = %s [%.4f]", numstr, pv);
429 	} else {
430 	    pprintf(prn, " z = %g [%.4f]", z, pv);
431 	}
432     }
433 
434     gretl_prn_newline(prn);
435 }
436 
437 enum {
438     AB_SARGAN,
439     AB_WALD,
440     AB_WALD_TIME,
441     J_TEST,
442     OVERDISP
443 };
444 
445 static void
print_model_chi2_test(const MODEL * pmod,double x,int j,PRN * prn)446 print_model_chi2_test (const MODEL *pmod, double x, int j, PRN *prn)
447 {
448     const char *strs[] = {
449 	N_("Sargan over-identification test"),
450 	N_("Wald (joint) test"),
451 	N_("Wald (time dummies)"),
452 	N_("J test"),
453 	N_("Overdispersion test")
454     };
455     const char *texstrs[] = {
456 	N_("Sargan test"),
457 	N_("Wald (joint) test"),
458 	N_("Wald (time dummies)"),
459 	N_("J test"),
460 	N_("Overdispersion test")
461     };
462     double pv;
463     int df;
464 
465     if (j == AB_SARGAN) {
466 	df = gretl_model_get_int(pmod, "sargan_df");
467     } else if (j == AB_WALD) {
468 	df = gretl_model_get_int(pmod, "wald_df");
469     } else if (j == AB_WALD_TIME) {
470 	df = gretl_model_get_int(pmod, "wald_time_df");
471     } else if (j == J_TEST) {
472 	df = gretl_model_get_int(pmod, "J_df");
473     } else if (j == OVERDISP) {
474 	df = 1;
475     } else {
476 	return;
477     }
478 
479     if (na(x)) {
480 	if (j == AB_SARGAN || j == AB_WALD) {
481 	    if (df < 0) {
482 		df = 0;
483 	    }
484 	    if (tex_format(prn)) {
485 		pprintf(prn, "%s: ", _(texstrs[j]));
486 		pprintf(prn, " $\\chi^2(%d)$ = NA", df);
487 	    } else if (plain_format(prn)) {
488 		if (gmm_model(pmod)) {
489 		    pputs(prn, "  ");
490 		}
491 		pprintf(prn, "%s: ", _(strs[j]));
492 		pprintf(prn, "%s(%d) = NA", _("Chi-square"), df);
493 	    } else {
494 		pprintf(prn, "%s: ", _(strs[j]));
495 		pprintf(prn, "%s(%d) = NA", _("Chi-square"), df);
496 	    }
497 	    gretl_prn_newline(prn);
498 	}
499 	return;
500     }
501 
502     pv = chisq_cdf_comp(df, x);
503 
504     if (na(pv)) {
505 	return;
506     }
507 
508     if (tex_format(prn)) {
509 	pprintf(prn, "%s: ", _(texstrs[j]));
510 	pprintf(prn, " $\\chi^2(%d)$ = %g [%.4f]", df, x, pv);
511     } else if (plain_format(prn)) {
512 	if (gmm_model(pmod)) {
513 	    pputs(prn, "  ");
514 	}
515 	pprintf(prn, "%s: ", _(strs[j]));
516 	pprintf(prn, "%s(%d) = %g [%.4f]", _("Chi-square"), df, x, pv);
517     } else {
518 	pprintf(prn, "%s: ", _(strs[j]));
519 	pprintf(prn, "%s(%d) = %g [%.4f]", _("Chi-square"), df, x, pv);
520     }
521 
522     gretl_prn_newline(prn);
523 }
524 
GMM_crit_line(const MODEL * pmod,PRN * prn)525 static int GMM_crit_line (const MODEL *pmod, PRN *prn)
526 {
527     double Q = pmod->ess;
528     double TQ = pmod->ess * pmod->nobs;
529 
530     if (plain_format(prn)) {
531 	pprintf(prn, "  %s: Q = %.*g (TQ = %.*g)\n", _("GMM criterion"),
532 		XDIGITS(pmod), Q, XDIGITS(pmod), TQ);
533     } else if (rtf_format(prn)) {
534 	pprintf(prn, RTFTAB "%s: Q = %g (TQ = %g)\n", _("GMM criterion"),
535 		Q, TQ);
536     } else if (tex_format(prn)) {
537 	char x1[32], x2[32];
538 
539 	tex_sprint_double(Q, x1);
540 	tex_sprint_double(TQ, x2);
541 	pprintf(prn, "%s, $Q$ = %s ($TQ$ = %s)\\\\\n",
542 		_("GMM criterion"), x1, x2);
543     } else if (csv_format(prn)) {
544 	pprintf(prn, "\"%s\"%c%.15g\n", _("GMM criterion"),
545 		prn_delim(prn), Q);
546     }
547 
548     return 0;
549 }
550 
print_GMM_stats(const MODEL * pmod,PRN * prn)551 static void print_GMM_stats (const MODEL *pmod, PRN *prn)
552 {
553     double x;
554 
555     ensure_vsep(prn);
556 
557     GMM_crit_line(pmod, prn);
558     x = gretl_model_get_double(pmod, "J_test");
559     if (!na(x)) {
560 	print_model_chi2_test(pmod, x, J_TEST, prn);
561     }
562 
563     if (!tex_format(prn)) {
564 	gretl_prn_newline(prn);
565     }
566 }
567 
print_DPD_stats(const MODEL * pmod,PRN * prn)568 static void print_DPD_stats (const MODEL *pmod, PRN *prn)
569 {
570     double x, y;
571     int k;
572 
573     /* 2021-01-28: mask stats not calculated when showing
574        the 1-step model in the context of 2-step estimation
575        with the --verbose flag
576     */
577     if (pmod->ID < 0) {
578 	return;
579     }
580 
581     ensure_vsep(prn);
582 
583     if (tex_format(prn)) {
584 	pputs(prn, "\\begin{tabular}{ll}\n");
585     }
586 
587     k = gretl_model_get_int(pmod, "ninst");
588     if (k > 0) {
589 	pprintf(prn, _("Number of instruments = %d"), k);
590 	gretl_prn_newline(prn);
591     }
592 
593     x = gretl_model_get_double(pmod, "AR1");
594     y = gretl_model_get_double(pmod, "AR2");
595     if (!na(x) && !na(y)) {
596 	print_panel_AR_test(x, 1, prn);
597 	print_panel_AR_test(y, 2, prn);
598     }
599 
600     x = gretl_model_get_double(pmod, "sargan");
601     print_model_chi2_test(pmod, x, AB_SARGAN, prn);
602 
603     x = gretl_model_get_double(pmod, "wald");
604     print_model_chi2_test(pmod, x, AB_WALD, prn);
605 
606     x = gretl_model_get_double(pmod, "wald_time");
607     if (!na(x)) {
608 	print_model_chi2_test(pmod, x, AB_WALD_TIME, prn);
609     }
610 
611     if (tex_format(prn)) {
612 	pputs(prn, "\\end{tabular}\n");
613     } else {
614 	gretl_prn_newline(prn);
615     }
616 }
617 
print_overdisp_test(const MODEL * pmod,PRN * prn)618 static void print_overdisp_test (const MODEL *pmod, PRN *prn)
619 {
620     double x = gretl_model_get_double(pmod, "overdisp");
621 
622     if (!na(x)) {
623 	ensure_vsep(prn);
624 	print_model_chi2_test(pmod, x, OVERDISP, prn);
625 	if (!tex_format(prn)) {
626 	    gretl_prn_newline(prn);
627 	}
628     }
629 }
630 
print_duration_alpha(const MODEL * pmod,PRN * prn)631 static void print_duration_alpha (const MODEL *pmod, PRN *prn)
632 {
633     if ((pmod->opt & OPT_B) && plain_format(prn)) {
634 	/* Weibull */
635 	double a = 1.0 / pmod->coeff[pmod->ncoeff - 1];
636 	double sa = a * a * pmod->sderr[pmod->ncoeff - 1];
637 
638 	ensure_vsep(prn);
639 	pprintf(prn, "1/sigma = %g (%g)\n", a, sa);
640 	if (!tex_format(prn)) {
641 	    gretl_prn_newline(prn);
642 	}
643     }
644 }
645 
print_probit_rho(const MODEL * pmod,PRN * prn)646 static void print_probit_rho (const MODEL *pmod, PRN *prn)
647 {
648     ensure_vsep(prn);
649 
650     if (re_probit_model(pmod)) {
651 	if (tex_format(prn)) {
652 	    pprintf(prn, "$\\hat{\\sigma}_u$ = %.5f\n", pmod->sigma);
653 	} else {
654 	    pprintf(prn, "sigma_u = %g\n", pmod->sigma);
655 	}
656     }
657 
658     if (tex_format(prn)) {
659 	double r = pmod->rho;
660 
661 	if (r < 0) {
662 	    pprintf(prn, "$\\hat{\\rho}$ = $-$%.5f\n", fabs(r));
663 	} else {
664 	    pprintf(prn, "$\\hat{\\rho}$ = %.5f\n", r);
665 	}
666     } else {
667 	pprintf(prn, "rho = %g\n", pmod->rho);
668     }
669 
670     gretl_prn_newline(prn);
671 }
672 
print_GNR_info(const MODEL * pmod,PRN * prn)673 static void print_GNR_info (const MODEL *pmod, PRN *prn)
674 {
675     double R2 = gretl_model_get_double(pmod, "GNR_Rsquared");
676     double tmax = gretl_model_get_double(pmod, "GNR_tmax");
677     int rd = gretl_model_get_int(pmod, "near-singular");
678     const char *msg;
679 
680     if (na(R2) && na(tmax) && rd == 0) {
681 	return;
682     }
683 
684     ensure_vsep(prn);
685 
686     if (!na(R2) && !na(tmax)) {
687 	if (tex_format(prn)) {
688 	    pprintf(prn, "GNR: $R^2$ = %g, max $|t|$ = %g", R2, tmax);
689 	} else {
690 	    pprintf(prn, "GNR: R-squared = %g, max |t| = %g", R2, tmax);
691 	}
692 	gretl_prn_newline(prn);
693 
694 	if (R2 > 1.0e-8 || tmax > 1.0e-4) {
695 	    msg = N_("Warning: convergence is questionable");
696 	} else {
697 	    msg = N_("Convergence seems to be reasonably complete");
698 	}
699 	pputs(prn, _(msg));
700 	gretl_prn_newline(prn);
701     } else {
702 	pputs(prn, _("GNR: got incomplete results"));
703 	gretl_prn_newline(prn);
704     }
705 
706     if (rd > 0) {
707 	msg = (rd > 1)? N_("Warning: Jacobian is rank-deficient") :
708 	    N_("Warning: Jacobian near to rank-deficiency");
709 	pputs(prn, _(msg));
710 	gretl_prn_newline(prn);
711     }
712 
713     if (!tex_format(prn)) {
714 	gretl_prn_newline(prn);
715     }
716 }
717 
maybe_print_lad_warning(const MODEL * pmod,PRN * prn)718 static void maybe_print_lad_warning (const MODEL *pmod, PRN *prn)
719 {
720     if (gretl_model_get_int(pmod, "nonunique")) {
721 	pputs(prn, _("Warning: solution is probably not unique"));
722 	pputc(prn, '\n');
723     }
724 }
725 
maybe_print_hessian_warning(const MODEL * pmod,PRN * prn)726 static void maybe_print_hessian_warning (const MODEL *pmod, PRN *prn)
727 {
728     if (gretl_model_get_int(pmod, "hess-error")) {
729 	pputs(prn, _("Warning: couldn't compute numerical Hessian"));
730 	pputc(prn, '\n');
731     }
732     if (gretl_model_get_int(pmod, "non-pd-hess")) {
733 	pputs(prn, _("Warning: non-pd Hessian (but still nonsingular)"));
734 	pputc(prn, '\n');
735     }
736 }
737 
panel_variance_lines(const MODEL * pmod,PRN * prn)738 static void panel_variance_lines (const MODEL *pmod, PRN *prn)
739 {
740     double s2v = gretl_model_get_double(pmod, "s2v");
741     double s2e = gretl_model_get_double(pmod, "s2e");
742     double theta = gretl_model_get_double(pmod, "theta");
743     double theta_bar = gretl_model_get_double(pmod, "theta_bar");
744     double rsq = gretl_model_get_double(pmod, "corr-rsq");
745 
746     if (na(s2v) || na(s2e)) {
747 	return;
748     }
749 
750     ensure_vsep(prn);
751 
752     if (plain_format(prn)) {
753 	pprintf(prn, "%s = %g\n", _("'Between' variance"), s2v);
754 	pprintf(prn, "%s = %g\n", _("'Within' variance"), s2e);
755 	if (!na(theta)) {
756 	    pprintf(prn, "%s = %g\n", _("theta used for quasi-demeaning"), theta);
757 	} else if (!na(theta_bar)) {
758 	    pprintf(prn, "%s = %g\n", _("mean theta"), theta_bar);
759 	}
760 	if (!na(rsq)) {
761 	    pprintf(prn, "corr(y,yhat)^2 = %g\n", rsq);
762 	}
763 	pputc(prn, '\n');
764     } else if (tex_format(prn)) {
765 	char xstr[32];
766 
767 	tex_sprint_double(s2v, xstr);
768 	pprintf(prn, "$\\hat{\\sigma}^2_v$ = %s \\\\\n", xstr);
769 	tex_sprint_double(s2e, xstr);
770 	pprintf(prn, "$\\hat{\\sigma}^2_{\\varepsilon}$ = %s \\\\\n", xstr);
771 	if (!na(theta)) {
772 	    tex_sprint_double(theta, xstr);
773 	    pprintf(prn, "$\\theta$ = %s \\\\\n", xstr);
774 	} else if (!na(theta_bar)) {
775 	    tex_sprint_double(theta_bar, xstr);
776 	    pprintf(prn, "$\\bar{\\theta}$ = %s \\\\\n", xstr);
777 	}
778     } else if (rtf_format(prn)) {
779 	pprintf(prn, RTFTAB "%s = %g", _("'Between' variance"), s2v);
780 	pprintf(prn, RTFTAB "%s = %g", _("'Within' variance"), s2e);
781 	if (!na(theta)) {
782 	    pprintf(prn, RTFTAB "%s = %g", _("theta used for quasi-demeaning"), theta);
783 	} else if (!na(theta_bar)) {
784 	    pprintf(prn, RTFTAB "%s = %g", _("mean theta"), theta_bar);
785 	}
786     } else if (csv_format(prn)) {
787 	char d = prn_delim(prn);
788 
789 	pprintf(prn, "\"%s\"%c%.15g\n", _("'Between' variance"), d, s2v);
790 	pprintf(prn, "\"%s\"%c%.15g\n", _("'Within' variance"), d, s2e);
791 	if (!na(theta)) {
792 	    pprintf(prn, "\"%s\"%c%.15g\n", _("theta used for quasi-demeaning"),
793 		    d, theta);
794 	} else if (!na(theta_bar)) {
795 	    pprintf(prn, "\"%s\"%c%.15g\n", _("mean theta"),
796 		    d, theta_bar);
797 	}
798     }
799 }
800 
durbins_h(MODEL * pmod,int * err)801 static double durbins_h (MODEL *pmod, int *err)
802 {
803     int ldv = gretl_model_get_int(pmod, "ldepvar");
804     double se = pmod->sderr[ldv - 2];
805     int T = pmod->nobs;
806     double h = NADBL;
807 
808     if (pmod->ess <= 0.0 || na(se) || na(pmod->rho)) {
809 	/* the model is flaky */
810 	*err = E_BADSTAT;
811     } else if ((T * se * se) >= 1.0) {
812 	/* h is undefined */
813 	*err = E_SQRT;
814     } else {
815 	h = pmod->rho * sqrt(T / (1 - T * se * se));
816 	gretl_model_set_double(pmod, "durbin_h", h);
817     }
818 
819     return h;
820 }
821 
least_significant_coeff(const MODEL * pmod)822 static int least_significant_coeff (const MODEL *pmod)
823 {
824     double x, tmin = 3.2;
825     int i, imax, k = 0;
826 
827     if (pmod->list == NULL ||
828 	gretl_list_separator_position(pmod->list) > 0) {
829 	return 0;
830     }
831 
832     /* Don't go beyond the last _listed_ regressor: some
833        models may have "extra" coefficients that should be
834        ignored here.
835     */
836     imax = pmod->list[0] - 1;
837 
838     for (i=pmod->ifc; i<imax; i++) {
839 	if (pmod->sderr[i] > 0) {
840 	    x = fabs(pmod->coeff[i] / pmod->sderr[i]);
841 	    if (x < tmin) {
842 		tmin = x;
843 		k = i;
844 	    }
845 	}
846     }
847 
848     if (tmin < 3.0) {
849 	x = model_coeff_pval(pmod, tmin);
850 	if (!na(x) && x > .10) {
851 	    return pmod->list[k+2];
852 	}
853     }
854 
855     return 0;
856 }
857 
pval_max_line(const MODEL * pmod,const DATASET * dset,PRN * prn)858 static void pval_max_line (const MODEL *pmod, const DATASET *dset,
859 			   PRN *prn)
860 {
861     int k = pmod->ncoeff - pmod->ifc;
862 
863     if (k < 3) return;
864 
865     k = least_significant_coeff(pmod);
866 
867     if (k > 0 && k < dset->v) {
868 	if (pmod->ifc) {
869 	    pprintf(prn, _("Excluding the constant, p-value was highest "
870 			   "for variable %d (%s)"), k, dset->varname[k]);
871 	} else {
872 	    pprintf(prn, _("P-value was highest for variable %d (%s)"),
873 		    k, dset->varname[k]);
874 	}
875 	pputs(prn, "\n\n");
876     }
877 }
878 
print_aux_string(const MODEL * pmod,PRN * prn)879 static void print_aux_string (const MODEL *pmod, PRN *prn)
880 {
881     int aux = pmod->aux;
882     int plain = plain_format(prn);
883     int tex = tex_format(prn);
884     int csv = csv_format(prn);
885     int close = 1;
886 
887     if (plain || tex) {
888 	pputc(prn, '\n');
889     } else if (csv) {
890 	pputc(prn, '"');
891     }
892 
893     if (aux == AUX_SQ) {
894 	pputs(prn, _("Auxiliary regression for non-linearity test "
895 		     "(squared terms)"));
896     } else if (aux == AUX_LOG) {
897 	pputs(prn, _("Auxiliary regression for non-linearity test "
898 		     "(log terms)"));
899     } else if (aux == AUX_ADD) {
900 	pputs(prn, _("Auxiliary regression for added variables"));
901     } else if (aux == AUX_WHITE) {
902 	pputs(prn, _("White's test for heteroskedasticity"));
903 	if (pmod->opt & OPT_X) {
904 	    pprintf(prn, " (%s)", _("squares only"));
905 	}
906     } else if (aux == AUX_BP) {
907 	pputs(prn, _("Breusch-Pagan test for heteroskedasticity"));
908     } else if (aux == AUX_HET_1) {
909 	pputs(prn, _("Pesaran-Taylor test for heteroskedasticity"));
910     } else if (aux == AUX_CHOW) {
911 	pputs(prn, _("Augmented regression for Chow test"));
912     } else if (aux == AUX_COINT) {
913 	if (tex_format(prn)) {
914 	    pputs(prn, _("Cointegrating regression -- "));
915 	} else {
916 	    pputs(prn, _("Cointegrating regression - "));
917 	}
918     } else if (aux == AUX_ADF) {
919 	if (tex_format(prn)) {
920 	    pputs(prn, _("Augmented Dickey--Fuller regression"));
921 	} else {
922 	    pputs(prn, _("Augmented Dickey-Fuller regression"));
923 	}
924     } else if (aux == AUX_DF) {
925 	if (tex_format(prn)) {
926 	    pputs(prn, _("Dickey--Fuller regression"));
927 	} else {
928 	    pputs(prn, _("Dickey-Fuller regression"));
929 	}
930     } else if (aux == AUX_KPSS) {
931 	pputs(prn, _("KPSS regression"));
932     } else if (aux == AUX_RESET) {
933 	pputs(prn, _("Auxiliary regression for RESET specification test"));
934     } else if (aux == AUX_GROUPWISE) {
935 	pputs(prn, _("Groupwise heteroskedasticity"));
936     } else if (aux == AUX_COMFAC) {
937 	pputs(prn, _("Augmented regression for common factor test"));
938     } else {
939 	close = 0;
940     }
941 
942     if (close) {
943 	if (plain || tex) {
944 	    pputc(prn, '\n');
945 	} else if (csv) {
946 	    pputs(prn, "\"\n");
947 	} else { /* RTF */
948 	    pputs(prn, "\\par\n");
949 	}
950     }
951 }
952 
simple_estimator_string(int ci,PRN * prn)953 static const char *simple_estimator_string (int ci, PRN *prn)
954 {
955     if (ci == OLS || ci == VAR) return N_("OLS");
956     else if (ci == WLS)  return N_("WLS");
957     else if (ci == ARCH) return N_("WLS (ARCH)");
958     else if (ci == HSK)  return N_("Heteroskedasticity-corrected");
959     else if (ci == AR)   return N_("AR");
960     else if (ci == LAD)  return N_("LAD");
961     else if (ci == MPOLS) return N_("High-Precision OLS");
962     else if (ci == PROBIT) return N_("Probit");
963     else if (ci == LOGIT)  return N_("Logit");
964     else if (ci == TOBIT)  return N_("Tobit");
965     else if (ci == HECKIT) return N_("Heckit");
966     else if (ci == POISSON) return N_("Poisson");
967     else if (ci == NEGBIN) return N_("Negative Binomial");
968     else if (ci == DURATION) return N_("Duration");
969     else if (ci == NLS) return N_("NLS");
970     else if (ci == MLE) return N_("ML");
971     else if (ci == GMM) return N_("GMM");
972     else if (ci == LOGISTIC) return N_("Logistic");
973     else if (ci == GARCH) return N_("GARCH");
974     else if (ci == INTREG) return N_("Interval estimates");
975     else if (ci == DPANEL) return N_("Dynamic panel");
976     else if (ci == BIPROBIT) return N_("Bivariate probit");
977     else if (ci == MIDASREG) return N_("MIDAS");
978     else return "";
979 }
980 
estimator_string(const MODEL * pmod,PRN * prn)981 const char *estimator_string (const MODEL *pmod, PRN *prn)
982 {
983     if (pmod->ci == AR1) {
984 	if (pmod->opt & OPT_H) {
985 	    if (tex_format(prn)) return N_("Hildreth--Lu");
986 	    else return N_("Hildreth-Lu");
987 	} else if (pmod->opt & OPT_P) {
988 	    if (tex_format(prn)) return N_("Prais--Winsten");
989 	    else return N_("Prais-Winsten");
990 	} else {
991 	    if (tex_format(prn)) return N_("Cochrane--Orcutt");
992 	    else return N_("Cochrane-Orcutt");
993 	}
994     } else if (pmod->ci == ARMA) {
995 	if (gretl_model_get_int(pmod, "armax")) {
996 	    return N_("ARMAX");
997 	} else if (gretl_model_get_int(pmod, "arima_d") ||
998 		   gretl_model_get_int(pmod, "arima_D")) {
999 	    return N_("ARIMA");
1000 	} else {
1001 	    return N_("ARMA");
1002 	}
1003     } else if (POOLED_MODEL(pmod)) {
1004 	return N_("Pooled OLS");
1005     } else if (pmod->ci == PANEL) {
1006 	if (pmod->opt & OPT_F) {
1007 	    return N_("Fixed-effects");
1008 	} else if (pmod->opt & OPT_U) {
1009 	    return N_("Random-effects (GLS)");
1010 	} else if (pmod->opt & OPT_H) {
1011 	    if (gretl_model_get_int(pmod, "iters")) {
1012 		return N_("Maximum Likelihood");
1013 	    } else {
1014 		return N_("WLS");
1015 	    }
1016 	} else {
1017 	    return N_("Between-groups");
1018 	}
1019     } else if (pmod->ci == DPANEL) {
1020 	if (gretl_model_get_int(pmod, "step") == 2) {
1021 	    return N_("2-step dynamic panel");
1022 	} else {
1023 	    return N_("1-step dynamic panel");
1024 	}
1025     } else if (gmm_model(pmod)) {
1026 	if (pmod->opt & OPT_T) {
1027 	    return N_("2-step GMM");
1028 	} else if (pmod->opt & OPT_I) {
1029 	    return N_("Iterated GMM");
1030 	} else if (gretl_model_get_int(pmod, "step") == 2) {
1031 	    return N_("2-step GMM");
1032 	} else if (gretl_model_get_int(pmod, "step") > 2) {
1033 	    return N_("Iterated GMM");
1034 	} else {
1035 	    return N_("1-step GMM");
1036 	}
1037     } else if (pmod->ci == LOGIT) {
1038 	if (gretl_model_get_int(pmod, "ordered")) {
1039 	    return N_("Ordered Logit");
1040 	} else if (gretl_model_get_int(pmod, "multinom")) {
1041 	    return N_("Multinomial Logit");
1042 	} else {
1043 	    return N_("Logit");
1044 	}
1045     } else if (pmod->ci == PROBIT) {
1046 	if (pmod->opt & OPT_E) {
1047 	    return N_("Random-effects probit");
1048 	} else if (gretl_model_get_int(pmod, "ordered")) {
1049 	    return N_("Ordered Probit");
1050 	} else {
1051 	    return N_("Probit");
1052 	}
1053     } else if (pmod->ci == LOGISTIC) {
1054 	if (pmod->opt & OPT_F) {
1055 	    return N_("Fixed-effects logistic");
1056 	} else {
1057 	    return N_("Logistic");
1058 	}
1059     } else if (pmod->ci == HECKIT) {
1060 	if (pmod->opt & OPT_T) {
1061 	    return N_("Two-step Heckit");
1062 	} else {
1063 	    return N_("ML Heckit");
1064 	}
1065     } else if (pmod->ci == LAD) {
1066 	if (gretl_model_get_int(pmod, "rq")) {
1067 	    return N_("Quantile estimates");
1068 	} else {
1069 	    return N_("LAD");
1070 	}
1071     } else if (pmod->ci == IVREG) {
1072 	if (pmod->opt & OPT_L) {
1073 	    return N_("LIML");
1074 	} else {
1075 	    return N_("TSLS");
1076 	}
1077     } else if (pmod->ci == NEGBIN) {
1078 	if (pmod->opt & OPT_M) {
1079 	    return N_("Negative Binomial 1");
1080 	} else {
1081 	    return N_("Negative Binomial");
1082 	}
1083     } else if (pmod->ci == DURATION) {
1084 	if (pmod->opt & OPT_E) {
1085 	    return N_("Duration (exponential)");
1086 	} else if (pmod->opt & OPT_L) {
1087 	    return N_("Duration (log-logistic)");
1088 	} else if (pmod->opt & OPT_Z) {
1089 	    return N_("Duration (log-normal)");
1090 	} else {
1091 	    return N_("Duration (Weibull)");
1092 	}
1093     } else if (pmod->ci == OLS &&
1094 	       gretl_model_get_int(pmod, "restricted")) {
1095 	return N_("Restricted OLS");
1096     } else if (pmod->ci == MIDASREG) {
1097 	if (gretl_model_get_int(pmod, "umidas")) {
1098 	    return N_("MIDAS (OLS)");
1099 	} else {
1100 	    return N_("MIDAS (NLS)");
1101 	}
1102     } else {
1103 	return simple_estimator_string(pmod->ci, prn);
1104     }
1105 }
1106 
any_tests(const MODEL * pmod)1107 static int any_tests (const MODEL *pmod)
1108 {
1109     if (pmod->ntests > 0) {
1110 	return 1;
1111     }
1112 
1113     if (pmod->ci == IVREG && gretl_model_get_int(pmod, "stage1-dfn")) {
1114 	return 1;
1115     }
1116 
1117     return 0;
1118 }
1119 
get_stock_yogo_critvals(int n,int K2,gretlopt opt,gretl_matrix ** p1,gretl_matrix ** p2)1120 static void get_stock_yogo_critvals (int n, int K2, gretlopt opt,
1121 				     gretl_matrix **p1,
1122 				     gretl_matrix **p2)
1123 {
1124     gretl_matrix *(*lookup) (int, int, int);
1125 
1126     lookup = get_plugin_function("stock_yogo_lookup");
1127 
1128     if (lookup != NULL) {
1129 	if (opt & OPT_L) {
1130 	    /* LIML test size */
1131 	    *p2 = (*lookup) (n, K2, 3);
1132 	} else {
1133 	    /* TSLS relative bias, test size */
1134 	    *p1 = (*lookup) (n, K2, 1);
1135 	    *p2 = (*lookup) (n, K2, 2);
1136 	}
1137     }
1138 }
1139 
plain_print_sy_vals(gretl_matrix * v,double g,int k,gretlopt opt,PRN * prn)1140 static void plain_print_sy_vals (gretl_matrix *v, double g, int k,
1141 				 gretlopt opt, PRN *prn)
1142 {
1143     int i, gpos = -1;
1144     double x;
1145 
1146     pputs(prn, "  ");
1147 
1148     if (k == 1) {
1149 	pputs(prn, _("Critical values for TSLS bias relative to OLS:\n"));
1150     } else if (opt & OPT_L) {
1151 	/* xgettext:no-c-format */
1152 	pputs(prn, _("Critical values for desired LIML maximal size, when running\n"
1153 		     "  tests at a nominal 5% significance level:\n"));
1154     } else {
1155 	/* xgettext:no-c-format */
1156 	pputs(prn, _("Critical values for desired TSLS maximal size, when running\n"
1157 		     "  tests at a nominal 5% significance level:\n"));
1158     }
1159 
1160     pprintf(prn, "\n%9s", (k == 1)? _("bias") : _("size"));
1161     for (i=0; i<4; i++) {
1162 	pprintf(prn, "%8g%%", 100 * gretl_matrix_get(v, 0, i));
1163     }
1164     pprintf(prn, "\n%9s", _("value"));
1165     for (i=0; i<4; i++) {
1166 	x = gretl_matrix_get(v, 1, i);
1167 	if (gpos < 0 && g > x) {
1168 	    gpos = i;
1169 	}
1170 	pprintf(prn, "%9.2f", x);
1171     }
1172     pputs(prn, "\n\n  ");
1173 
1174     if (gpos == 0) {
1175 	x = gretl_matrix_get(v, 0, 0);
1176 	if (k == 1) {
1177 	    pprintf(prn, _("Relative bias is probably less than %g%%"), 100 * x);
1178 	} else {
1179 	    pprintf(prn, _("Maximal size is probably less than %g%%"), 100 * x);
1180 	}
1181     } else {
1182 	if (gpos < 0) {
1183 	    gpos = 3;
1184 	} else {
1185 	    gpos--;
1186 	}
1187 	x = gretl_matrix_get(v, 0, gpos);
1188 	if (k == 1) {
1189 	    pprintf(prn, _("Relative bias may exceed %g%%"), 100 * x);
1190 	} else {
1191 	    pprintf(prn, _("Maximal size may exceed %g%%"), 100 * x);
1192 	}
1193     }
1194 
1195     pputs(prn, "\n\n");
1196 }
1197 
maybe_print_weak_insts_test(const MODEL * pmod,PRN * prn)1198 static void maybe_print_weak_insts_test (const MODEL *pmod, PRN *prn)
1199 {
1200     const char *head = N_("Weak instrument test");
1201     double F = gretl_model_get_double(pmod, "stage1-F");
1202     double g = gretl_model_get_double(pmod, "gmin");
1203     int digits = get_gretl_digits();
1204     int got_critvals = 0;
1205     int dfn = 0, dfd = 0;
1206 
1207     if (na(F) && na(g)) {
1208 	return;
1209     }
1210 
1211     if (plain_format(prn)) {
1212 	pprintf(prn, "%s - \n", _(head));
1213     } else if (tex_format(prn)) {
1214 	pprintf(prn, "%s -- \\\\\n", _(head));
1215     } else if (rtf_format(prn)) {
1216 	pprintf(prn, "%s - \\par\n", _(head));
1217     }
1218 
1219     if (!na(F)) {
1220 	/* got first-stage F-test (single endogenous regressor) */
1221 	dfn = gretl_model_get_int(pmod, "stage1-dfn");
1222 	dfd = gretl_model_get_int(pmod, "stage1-dfd");
1223 
1224 	if (plain_format(prn)) {
1225 	    pprintf(prn, "  %s (%d, %d) = %.*g\n", _("First-stage F-statistic"),
1226 		    dfn, dfd, digits, F);
1227 	} else if (tex_format(prn)) {
1228 	    char x1str[32];
1229 
1230 	    tex_sprint_double(F, x1str);
1231 	    pprintf(prn, "\\quad First-stage $F(%d, %d)$ = %s \\\\\n", dfn, dfd, x1str);
1232 	} else if (rtf_format(prn)) {
1233 	    pprintf(prn, "  %s (%d, %d) = %g\n", _("First-stage F-statistic"),
1234 		    dfn, dfd, F);
1235 	}
1236     } else {
1237 	/* got minimum eigenvalue test statistic */
1238 	if (plain_format(prn)) {
1239 	    pprintf(prn, "  %s = %.*g\n", _("Cragg-Donald minimum eigenvalue"),
1240 		    digits, g);
1241 	} else if (tex_format(prn)) {
1242 	    char x1str[32];
1243 
1244 	    tex_sprint_double(g, x1str);
1245 	    pprintf(prn, "\\quad  %s = %s \\\\\n", _("Cragg--Donald minimum eigenvalue"),
1246 		    x1str);
1247 	} else if (rtf_format(prn)) {
1248 	    pprintf(prn, "  %s = %g\n", _("Cragg-Donald minimum eigenvalue"), g);
1249 	}
1250     }
1251 
1252     if (!na(g)) {
1253 	/* print Stock-Yogo critical values, if available */
1254 	gretl_matrix *bvals = NULL;
1255 	gretl_matrix *svals = NULL;
1256 	int n, K2;
1257 
1258 	if (na(F)) {
1259 	    n = gretl_model_get_int(pmod, "n");
1260 	    K2 = gretl_model_get_int(pmod, "K2");
1261 	} else {
1262 	    n = 1;
1263 	    K2 = dfn;
1264 	}
1265 
1266 	get_stock_yogo_critvals(n, K2, pmod->opt, &bvals, &svals);
1267 
1268 	if (bvals != NULL) {
1269 	    got_critvals = 1;
1270 	    if (plain_format(prn)) {
1271 		plain_print_sy_vals(bvals, g, 1, pmod->opt, prn);
1272 	    }
1273 	    gretl_matrix_free(bvals);
1274 	}
1275 
1276 	if (svals != NULL) {
1277 	    got_critvals = 1;
1278 	    if (plain_format(prn)) {
1279 		plain_print_sy_vals(svals, g, 2, pmod->opt, prn);
1280 	    }
1281 	    gretl_matrix_free(svals);
1282 	}
1283     }
1284 
1285     if (!na(F) && !got_critvals && plain_format(prn)) {
1286 	pprintf(prn, "  %s\n\n", _("A value < 10 may indicate weak instruments"));
1287     }
1288 }
1289 
print_model_tests(const MODEL * pmod,PRN * prn)1290 static void print_model_tests (const MODEL *pmod, PRN *prn)
1291 {
1292     int i;
1293 
1294     if (tex_format(prn)) {
1295 	pputs(prn, "\\vspace{1em}\n\\begin{raggedright}\n");
1296 	for (i=0; i<pmod->ntests; i++) {
1297 	    if (i > 0) {
1298 		pputs(prn, "\\vspace{1ex}\n");
1299 	    }
1300 	    gretl_model_test_print(pmod, i, prn);
1301 	}
1302 	if (pmod->ntests > 0) {
1303 	    pputs(prn, "\\vspace{1ex}\n");
1304 	}
1305 	maybe_print_weak_insts_test(pmod, prn);
1306 	pputs(prn, "\\end{raggedright}\n");
1307     } else {
1308 	for (i=0; i<pmod->ntests; i++) {
1309 	    gretl_model_test_print(pmod, i, prn);
1310 	}
1311 	maybe_print_weak_insts_test(pmod, prn);
1312     }
1313 }
1314 
1315 static int
print_ivreg_instruments(const MODEL * pmod,const DATASET * dset,PRN * prn)1316 print_ivreg_instruments (const MODEL *pmod, const DATASET *dset, PRN *prn)
1317 {
1318     const char *labels[] = {
1319 	N_("Instrumented"),
1320 	N_("Instruments")
1321     };
1322     const int *list;
1323     char vname[VNAMELEN];
1324     int tex = tex_format(prn);
1325     int i, j, imin, jmin, vi;
1326 
1327     jmin = (pmod->aux)? 1 : 0;
1328 
1329     /* below: first print list of endogenous variables, then
1330        list of instruments */
1331 
1332     for (j=jmin; j<2; j++) {
1333 	int ccount, nv = 0;
1334 
1335 	if (j == 0) {
1336 	    list = gretl_model_get_list(pmod, "endolist");
1337 	    imin = (list == NULL)? -1 : 1;
1338 	} else {
1339 	    /* j = 1: instruments */
1340 	    list = pmod->list;
1341 	    imin = gretl_list_separator_position(list);
1342 	    if (imin > 0) {
1343 		imin++;
1344 	    } else {
1345 		imin = -1;
1346 	    }
1347 	}
1348 
1349 	if (imin < 0) {
1350 	    continue;
1351 	}
1352 
1353 	ccount = pprintf(prn, "%s: ", _(labels[j]));
1354 
1355 	for (i=imin; i<=list[0]; i++) {
1356 	    vi = list[i];
1357 	    if (vi >= dset->v) {
1358 		fprintf(stderr, "print_ivreg_instruments: bad varnum %d\n", vi);
1359 		continue;
1360 	    }
1361 	    if (tex) {
1362 		tex_escape(vname, dset->varname[vi]);
1363 	    } else {
1364 		strcpy(vname, dset->varname[vi]);
1365 	    }
1366 	    ccount += pprintf(prn, "%s ", vname);
1367 	    if (ccount >= 64) {
1368 		if (tex) {
1369 		    pputs(prn, "\\\\\n");
1370 		} else if (rtf_format(prn)) {
1371 		    pputs(prn, "\\par\n");
1372 		} else {
1373 		    pputs(prn, "\n  ");
1374 		}
1375 		ccount = 0;
1376 	    }
1377 	    nv++;
1378 	}
1379 
1380 	if (nv == 0) {
1381 	    pputs(prn, "none??");
1382 	}
1383 
1384 	gretl_prn_newline(prn);
1385     }
1386 
1387     return 0;
1388 }
1389 
dpd_asy_vcv_line(PRN * prn)1390 static void dpd_asy_vcv_line (PRN *prn)
1391 {
1392     if (csv_format(prn)) {
1393 	pprintf(prn, "\"%s\"", _("Asymptotic standard errors"));
1394     } else {
1395 	pputs(prn, _("Asymptotic standard errors"));
1396     }
1397 
1398     pputc(prn, '\n');
1399 }
1400 
panel_vcv_line(const VCVInfo * vi,PRN * prn)1401 static void panel_vcv_line (const VCVInfo *vi, PRN *prn)
1402 {
1403     if (vi->vmin == PANEL_HAC) {
1404 	if (csv_format(prn)) {
1405 	    pprintf(prn, "\"%s\"", _("Robust (HAC) standard errors"));
1406 	} else {
1407 	    pputs(prn, _("Robust (HAC) standard errors"));
1408 	}
1409 	pputc(prn, '\n');
1410     } else if (vi->vmin == PANEL_BK) {
1411 	if (csv_format(prn)) {
1412 	    pprintf(prn, "\"%s\"", _("Beck-Katz standard errors"));
1413 	} else if (tex_format(prn)) {
1414 	    pputs(prn, _("Beck--Katz standard errors"));
1415 	} else {
1416 	    pputs(prn, _("Beck-Katz standard errors"));
1417 	}
1418 	pputc(prn, '\n');
1419     }
1420 }
1421 
cluster_vcv_line(const MODEL * pmod,const VCVInfo * vi,const DATASET * dset,PRN * prn)1422 static void cluster_vcv_line (const MODEL *pmod, const VCVInfo *vi,
1423 			      const DATASET *dset, PRN *prn)
1424 {
1425     gchar *cstr;
1426 
1427     if (vi->vmin >= 1 && vi->vmin < dset->v) {
1428 	int n_c = gretl_model_get_int(pmod, "n_clusters");
1429 
1430 	cstr = g_strdup_printf(_("Standard errors clustered by %d values of %s"),
1431 			       n_c, dset->varname[vi->vmin]);
1432     } else {
1433 	cstr = g_strdup(_("Clustered standard errors"));
1434     }
1435 
1436     if (csv_format(prn)) {
1437 	pprintf(prn, "\"%s\"", cstr);
1438     } else {
1439 	pputs(prn, cstr);
1440     }
1441     pputc(prn, '\n');
1442 
1443     g_free(cstr);
1444 }
1445 
beck_katz_failed_line(PRN * prn)1446 static void beck_katz_failed_line (PRN *prn)
1447 {
1448     if (plain_format(prn)) {
1449 	pputs(prn, _("Could not compute Beck-Katz standard errors"));
1450 	pputc(prn, '\n');
1451     }
1452 }
1453 
nls_robust_failed_line(PRN * prn)1454 static void nls_robust_failed_line (PRN *prn)
1455 {
1456     if (plain_format(prn)) {
1457 	pputs(prn, _("Could not compute robust standard errors"));
1458 	pputc(prn, '\n');
1459     }
1460 }
1461 
hac_vcv_line(const VCVInfo * vi,PRN * prn)1462 static void hac_vcv_line (const VCVInfo *vi, PRN *prn)
1463 {
1464     const char *kstrs[] = {
1465 	N_("Bartlett kernel"),
1466 	N_("Parzen kernel"),
1467 	N_("QS kernel")
1468     };
1469 
1470     if (vi->vmin == KERNEL_QS) {
1471 	pprintf(prn, _("HAC standard errors, "
1472 		       "bandwidth %.2f"), vi->bw);
1473     } else {
1474 	pprintf(prn, _("HAC standard errors, "
1475 		       "bandwidth %d"), vi->order);
1476     }
1477 
1478     pprintf(prn, " (%s", _(kstrs[vi->vmin]));
1479 
1480     if (vi->flags) {
1481 	pprintf(prn, ", %s", _("prewhitened"));
1482     }
1483 
1484     pputs(prn, ")\n");
1485 }
1486 
hc_vcv_line(const VCVInfo * vi,PRN * prn)1487 static void hc_vcv_line (const VCVInfo *vi, PRN *prn)
1488 {
1489     int hcv = vi->vmin;
1490     int jack = 0;
1491 
1492     if (vi->vmin == 4) {
1493 	jack = 1;
1494 	hcv--;
1495     }
1496 
1497     pprintf(prn, "%s, %s%sHC%d%s",
1498 	    _("Heteroskedasticity-robust standard errors"),
1499 	    (jack)? "" : _("variant"),
1500 	    (jack)? "" : " ",
1501 	    hcv, (jack)? " (jackknife)" : "");
1502 
1503     if (rtf_format(prn)) {
1504 	pputs(prn, "\\par\n");
1505     } else {
1506 	pputc(prn, '\n');
1507     }
1508 }
1509 
ml_vcv_line(const VCVInfo * vi,PRN * prn)1510 static void ml_vcv_line (const VCVInfo *vi, PRN *prn)
1511 {
1512     int tex = tex_format(prn);
1513     const char *s = NULL;
1514 
1515     switch (vi->vmin) {
1516     case ML_HESSIAN:
1517 	s = N_("Standard errors based on Hessian");
1518 	break;
1519     case ML_IM:
1520 	s = N_("Standard errors based on Information Matrix");
1521 	break;
1522     case ML_OP:
1523 	s = N_("Standard errors based on Outer Products matrix");
1524 	break;
1525     case ML_QML:
1526 	s = N_("QML standard errors");
1527 	break;
1528     case ML_HAC:
1529 	s = N_("HAC standard errors");
1530 	break;
1531     case ML_BW:
1532 	if (tex) {
1533 	    s = N_("Bollerslev--Wooldridge standard errors");
1534 	} else {
1535 	    s = N_("Bollerslev-Wooldridge standard errors");
1536 	}
1537 	break;
1538     case ML_VCVMAX:
1539 	s = N_("Warning: could not compute standard errors");
1540 	break;
1541     default:
1542 	break;
1543     }
1544 
1545     if (s != NULL) {
1546 	if (csv_format(prn)) {
1547 	    pprintf(prn, "\"%s\"\n", _(s));
1548 	} else {
1549 	    pprintf(prn, "%s\n", _(s));
1550 	}
1551     }
1552 }
1553 
rq_vcv_line(const MODEL * pmod,PRN * prn)1554 static void rq_vcv_line (const MODEL *pmod, PRN *prn)
1555 {
1556     int robust = gretl_model_get_int(pmod, "rq_nid");
1557     double a = gretl_model_get_double(pmod, "rq_alpha");
1558     int free_s = 0;
1559     char *s;
1560 
1561     if (!na(a)) {
1562 	if (robust) {
1563 	    s = g_strdup_printf(N_("With robust %g percent confidence intervals"),
1564 				100 * (1 - a));
1565 	} else {
1566 	    s = g_strdup_printf(N_("With %g percent confidence intervals"),
1567 				100 * (1 - a));
1568 	}
1569 	free_s = 1;
1570     } else if (robust) {
1571 	s = N_("Robust (sandwich) standard errors");
1572     } else {
1573 	s = N_("Asymptotic standard errors assuming IID errors");
1574     }
1575 
1576     if (csv_format(prn)) {
1577 	pprintf(prn, "\"%s\"", _(s));
1578     } else {
1579 	pprintf(prn, "%s", _(s));
1580     }
1581 
1582     gretl_prn_newline(prn);
1583 
1584     if (free_s) {
1585 	g_free(s);
1586     }
1587 }
1588 
tex_vecm_depvar_name(char * s,const char * vname)1589 static void tex_vecm_depvar_name (char *s, const char *vname)
1590 {
1591     char tmp[14];
1592     int gotit = 0;
1593 
1594     if (sscanf(vname, "d_%13s", tmp)) {
1595 	char myvar[24];
1596 
1597 	tex_escape(myvar, tmp);
1598 	sprintf(s, "$\\Delta$%s", myvar);
1599 	gotit = 1;
1600     }
1601 
1602     if (!gotit) {
1603 	tex_escape(s, vname);
1604     }
1605 }
1606 
tex_dpd_depvar_name(char * s,const char * vname)1607 static void tex_dpd_depvar_name (char *s, const char *vname)
1608 {
1609     char vnesc[32];
1610 
1611     tex_escape(vnesc, vname);
1612     sprintf(s, "$\\Delta$%s", vnesc);
1613 }
1614 
print_model_vcv_info(const MODEL * pmod,const DATASET * dset,PRN * prn)1615 void print_model_vcv_info (const MODEL *pmod, const DATASET *dset,
1616 			   PRN *prn)
1617 {
1618     VCVInfo *vi = NULL;
1619 
1620     if (pmod->ci == LAD && gretl_model_get_int(pmod, "rq")) {
1621 	rq_vcv_line(pmod, prn);
1622     } else if (gretl_model_get_int(pmod, "panel_bk_failed")) {
1623 	beck_katz_failed_line(prn);
1624     } else if (pmod->ci == DPANEL && gretl_model_get_int(pmod, "asy")) {
1625 	dpd_asy_vcv_line(prn);
1626     } else if ((pmod->ci == NLS || pmod->ci == MIDASREG) &&
1627 	       gretl_model_get_int(pmod, "non-robust")) {
1628 	nls_robust_failed_line(prn);
1629     } else {
1630 	vi = gretl_model_get_data(pmod, "vcv_info");
1631     }
1632 
1633     if (vi != NULL) {
1634 	switch (vi->vmaj) {
1635 	case VCV_HC:
1636 	    hc_vcv_line(vi, prn);
1637 	    break;
1638 	case VCV_HAC:
1639 	    hac_vcv_line(vi, prn);
1640 	    break;
1641 	case VCV_ML:
1642 	    ml_vcv_line(vi, prn);
1643 	    break;
1644 	case VCV_PANEL:
1645 	    panel_vcv_line(vi, prn);
1646 	    break;
1647 	case VCV_CLUSTER:
1648 	    cluster_vcv_line(pmod, vi, dset, prn);
1649 	    break;
1650 	default:
1651 	    break;
1652 	}
1653     }
1654 }
1655 
print_extra_list(const char * tag,const int * list,const DATASET * dset,PRN * prn)1656 static void print_extra_list (const char *tag, const int *list,
1657 			      const DATASET *dset, PRN *prn)
1658 {
1659     int i, v, len;
1660 
1661     len = pputs(prn, _(tag));
1662 
1663     for (i=1; i<=list[0]; i++) {
1664 	v = list[i];
1665 	if (v < dset->v) {
1666 	    len += pprintf(prn, " %s", dset->varname[v]);
1667 	} else {
1668 	    len += pprintf(prn, " %d", v);
1669 	}
1670 	if (len > 68 && i < list[0]) {
1671 	    pputc(prn, '\n');
1672 	    len = 0;
1673 	}
1674     }
1675 
1676     pputc(prn, '\n');
1677 }
1678 
print_model_zerolist(const MODEL * pmod,const DATASET * dset,PRN * prn)1679 static void print_model_zerolist (const MODEL *pmod,
1680 				  const DATASET *dset,
1681 				  PRN *prn)
1682 {
1683     const int *zlist = gretl_model_get_list(pmod, "zerolist");
1684     const char *tag = N_("Omitted because all values were zero:");
1685 
1686     if (gretl_is_between_model(pmod)) {
1687 	return;
1688     }
1689 
1690     print_extra_list(tag, zlist, dset, prn);
1691 }
1692 
print_model_droplist(const MODEL * pmod,const DATASET * dset,PRN * prn)1693 static void print_model_droplist (const MODEL *pmod,
1694 				  const DATASET *dset,
1695 				  PRN *prn)
1696 {
1697     const int *dlist = gretl_model_get_list(pmod, "droplist");
1698     const char *tag = N_("Omitted due to exact collinearity:");
1699 
1700     if (gretl_is_between_model(pmod) && pmod->dataset == NULL) {
1701 	/* any droplist references will probably be wrong,
1702 	   and may be out of bounds */
1703 	return;
1704     } else if (dlist[0] > 10) {
1705 	/* let's not spew a ton of series names here */
1706 	pputs(prn, _(tag));
1707 	pprintf(prn, _(" %d series"), dlist[0]);
1708 	pputc(prn, '\n');
1709 	return;
1710     }
1711 
1712     print_extra_list(tag, dlist, dset, prn);
1713 }
1714 
print_ivreg_droplist(const MODEL * pmod,const DATASET * dset,PRN * prn)1715 static void print_ivreg_droplist (const MODEL *pmod,
1716 				  const DATASET *dset,
1717 				  PRN *prn)
1718 {
1719     const int *dlist = gretl_model_get_list(pmod, "inst_droplist");
1720     int i, v;
1721 
1722     pputs(prn, _("Redundant instruments:"));
1723     for (i=1; i<=dlist[0]; i++) {
1724 	v = dlist[i];
1725 	if (v < dset->v) {
1726 	    pprintf(prn, " %s", dset->varname[v]);
1727 	} else {
1728 	    pprintf(prn, " %d", v);
1729 	}
1730     }
1731     pputc(prn, '\n');
1732 }
1733 
print_arma_depvar(const MODEL * pmod,const DATASET * dset,PRN * prn)1734 static void print_arma_depvar (const MODEL *pmod,
1735 			       const DATASET *dset,
1736 			       PRN *prn)
1737 {
1738     int tex = tex_format(prn);
1739     int yno = gretl_model_get_depvar(pmod);
1740     int d = gretl_model_get_int(pmod, "arima_d");
1741     int D = gretl_model_get_int(pmod, "arima_D");
1742     char vname[64];
1743 
1744     *vname = 0;
1745 
1746     if (tex) {
1747 	char tmp[32];
1748 
1749 	if (d > 0 || D > 0) {
1750 	    strcat(vname, "$");
1751 	}
1752 	if (d == 1) {
1753 	    strcat(vname, "(1-L)");
1754 	} else if (d == 2) {
1755 	    strcat(vname, "(1-L)^2");
1756 	}
1757 	if (D == 1) {
1758 	    strcat(vname, "(1-L^s)");
1759 	} else if (D == 2) {
1760 	    strcat(vname, "(1-L^s)^2");
1761 	}
1762 	if (d > 0 || D > 0) {
1763 	    strcat(vname, "$");
1764 	}
1765 	tex_escape(tmp, dset->varname[yno]);
1766 	strcat(vname, tmp);
1767     } else {
1768 	if (d == 1) {
1769 	    strcat(vname, "(1-L)");
1770 	} else if (d == 2) {
1771 	    strcat(vname, "(1-L)^2");
1772 	}
1773 	if (D == 1) {
1774 	    strcat(vname, "(1-Ls)");
1775 	} else if (D == 2) {
1776 	    strcat(vname, "(1-Ls)^2");
1777 	}
1778 	if (d > 0 || D > 0) {
1779 	    strcat(vname, " ");
1780 	}
1781 	strcat(vname, dset->varname[yno]);
1782     }
1783 
1784     pprintf(prn, "%s: %s", _("Dependent variable"), vname);
1785 }
1786 
arma_extra_info(const MODEL * pmod,PRN * prn)1787 static void arma_extra_info (const MODEL *pmod, PRN *prn)
1788 {
1789     int acode = gretl_model_get_int(pmod, "arma_flags");
1790 
1791     if (acode & ARMA_X12A) {
1792 	if (gretl_x12_is_x13()) {
1793 	    pputs(prn, _("Estimated using X-13-ARIMA"));
1794 	} else {
1795 	    pputs(prn, _("Estimated using X-12-ARIMA"));
1796 	}
1797 	pputs(prn, " (");
1798 	pputs(prn, (acode & ARMA_EXACT)? _("exact ML") : _("conditional ML"));
1799 	pputs(prn, ")\n");
1800     } else if (acode & ARMA_OLS) {
1801 	if (gretl_model_get_int(pmod, "null-model")) {
1802 	    return;
1803 	}
1804 	pputs(prn, _("Estimated using least squares"));
1805 	pputs(prn, " (");
1806 	pputs(prn, _("= MLE"));
1807 	pputs(prn, ")\n");
1808     } else if (acode & ARMA_EXACT) {
1809 	int asnum = gretl_model_get_int(pmod, "as_algo");
1810 
1811 	if (asnum > 0) {
1812 	    pprintf(prn, _("Estimated using AS %d"), asnum);
1813 	} else {
1814 	    pputs(prn, _("Estimated using Kalman filter"));
1815 	}
1816 	pputs(prn, " (");
1817 	pputs(prn, _("exact ML"));
1818 	pputs(prn, ")\n");
1819     } else if (acode & ARMA_LS) {
1820 	pputs(prn, _("Estimated using least squares"));
1821 	pputs(prn, " (");
1822 	pputs(prn, _("conditional ML"));
1823 	pputs(prn, ")\n");
1824     } else {
1825 	pputs(prn, _("Estimated using BHHH method"));
1826 	pputs(prn, " (");
1827 	pputs(prn, _("conditional ML"));
1828 	pputs(prn, ")\n");
1829     }
1830 }
1831 
godfrey_test_string(int ci,int order,PRN * prn)1832 static void godfrey_test_string (int ci, int order, PRN *prn)
1833 {
1834     pputc(prn, '\n');
1835 
1836     if (ci == IVREG) {
1837 	if (order > 1) {
1838 	    pprintf(prn, _("Godfrey (1994) test for autocorrelation up to order %d"),
1839 		    order);
1840 	} else {
1841 	    pputs(prn, _("Godfrey (1994) test for first-order autocorrelation"));
1842 	}
1843     } else {
1844 	if (order > 1) {
1845 	    pprintf(prn, _("Breusch-Godfrey test for autocorrelation up to order %d"),
1846 		    order);
1847 	} else {
1848 	    pputs(prn, _("Breusch-Godfrey test for first-order autocorrelation"));
1849 	}
1850     }
1851 
1852     pputc(prn, '\n');
1853 }
1854 
1855 /* The selection variable should be the first variable following
1856    the list separator */
1857 
heckit_selvar_name(const MODEL * pmod,const DATASET * dset)1858 static const char *heckit_selvar_name (const MODEL *pmod,
1859 				       const DATASET *dset)
1860 {
1861     const int *list = pmod->list;
1862     int pos = gretl_list_separator_position(list);
1863 
1864     if (pos > 0 && pos < list[0] && list[pos+1] < dset->v) {
1865 	return dset->varname[list[pos+1]];
1866     } else {
1867 	return NULL;
1868     }
1869 }
1870 
print_intreg_depvar(const MODEL * pmod,const DATASET * dset,PRN * prn)1871 static void print_intreg_depvar (const MODEL *pmod,
1872 				 const DATASET *dset,
1873 				 PRN *prn)
1874 {
1875     int lov = gretl_model_get_int(pmod, "lovar");
1876     int hiv = gretl_model_get_int(pmod, "hivar");
1877 
1878     if (lov < dset->v && hiv < dset->v) {
1879 	pprintf(prn, "%s: %s", _("Lower limit"), dset->varname[lov]);
1880 	pprintf(prn, ", %s: %s", _("Upper limit"), dset->varname[hiv]);
1881     }
1882 }
1883 
maybe_print_T(const MODEL * pmod,const DATASET * dset,const char * start,PRN * prn)1884 static void maybe_print_T (const MODEL *pmod,
1885 			   const DATASET *dset,
1886 			   const char *start,
1887 			   PRN *prn)
1888 {
1889     if (pmod->ci == HECKIT) {
1890 	return;
1891     } else if (!strcmp(start, "1") && !model_has_missing_obs(pmod)) {
1892 	return;
1893     } else {
1894 	int xsect = dataset_is_cross_section(dset);
1895 
1896 	if (model_has_missing_obs(pmod) || !xsect || strcmp(start, "1")) {
1897 	    const char *nstrs[] = {
1898 		/* TRANSLATORS: 'n' denotes sample size */
1899 		N_("n"),
1900 		/* TRANSLATORS: 'T' denotes time-series sample size */
1901 		N_("T")
1902 	    };
1903 	    const char *nstr = xsect ? nstrs[0] : nstrs[1];
1904 
1905 	    if (tex_format(prn)) {
1906 		pprintf(prn, " ($%s$ = %d)", _(nstr), pmod->nobs);
1907 	    } else {
1908 		pprintf(prn, " (%s = %d)", _(nstr), pmod->nobs);
1909 	    }
1910 	}
1911     }
1912 }
1913 
make_obs_sep(char * targ,const char * obs,int tex)1914 static void make_obs_sep (char *targ, const char *obs, int tex)
1915 {
1916     if (tex) {
1917 	strcpy(targ, "--");
1918     } else if (strchr(obs, '-') != NULL) {
1919 	strcpy(targ, ":");
1920     } else {
1921 	strcpy(targ, "-");
1922     }
1923 }
1924 
maybe_show_midas_method(const MODEL * pmod,PRN * prn)1925 static void maybe_show_midas_method (const MODEL *pmod, PRN *prn)
1926 {
1927     if (gretl_model_get_int(pmod, "umidas") == 0) {
1928 	gretl_prn_newline(prn);
1929 	if (gretl_model_get_int(pmod, "GSS")) {
1930 	    pputs(prn, _("Using line search with conditional OLS"));
1931 	} else if (gretl_model_get_int(pmod, "BFGS")) {
1932 	    pputs(prn, _("Using L-BFGS-B with conditional OLS"));
1933 	} else if (tex_format(prn)) {
1934 	    pputs(prn, _("Using Levenberg--Marquardt algorithm"));
1935 	} else {
1936 	    pputs(prn, _("Using Levenberg-Marquardt algorithm"));
1937 	}
1938     }
1939 }
1940 
maybe_show_SA_method(const MODEL * pmod,PRN * prn)1941 static void maybe_show_SA_method (const MODEL *pmod, PRN *prn)
1942 {
1943     const char *s = gretl_model_get_data(pmod, "anova_method");
1944 
1945     if (s != NULL) {
1946 	gretl_prn_newline(prn);
1947 	if (!strcmp(s, "stata")) {
1948 	    pputs(prn, _("Using Swamy-Arora as per Stata"));
1949 	} else {
1950 	    pputs(prn, _("Using Swamy-Arora as per Baltagi-Chang"));
1951 	}
1952     }
1953 }
1954 
print_model_heading(const MODEL * pmod,const DATASET * dset,gretlopt opt,PRN * prn)1955 static void print_model_heading (const MODEL *pmod,
1956 				 const DATASET *dset,
1957 				 gretlopt opt,
1958 				 PRN *prn)
1959 {
1960     char startdate[OBSLEN], enddate[OBSLEN];
1961     char vname[48], datesep[4];
1962     int t1 = pmod->t1, t2 = pmod->t2;
1963     int tex = tex_format(prn);
1964     int csv = csv_format(prn);
1965     int dvnl = 1;
1966     int order = 0;
1967 
1968     if (pmod->aux != AUX_VAR && pmod->aux != AUX_VECM) {
1969 	ntolabel(startdate, t1, dset);
1970 	ntolabel(enddate, t2, dset);
1971 	make_obs_sep(datesep, startdate, tex);
1972     }
1973 
1974     switch (pmod->aux) {
1975     case AUX_SQ:
1976     case AUX_LOG:
1977     case AUX_ADD:
1978     case AUX_WHITE:
1979     case AUX_BP:
1980     case AUX_HET_1:
1981     case AUX_CHOW:
1982     case AUX_COINT:
1983     case AUX_ADF:
1984     case AUX_DF:
1985     case AUX_KPSS:
1986     case AUX_RESET:
1987     case AUX_GROUPWISE:
1988     case AUX_COMFAC:
1989     case AUX_BIPROB:
1990 	print_aux_string(pmod, prn);
1991 	break;
1992     case AUX_AR:
1993 	order = gretl_model_get_int(pmod, "BG_order");
1994 	if (order > 0) {
1995 	    godfrey_test_string(pmod->ci, order, prn);
1996 	} else {
1997 	    pputc(prn, '\n');
1998 	}
1999 	break;
2000     case AUX_ARCH:
2001 	order = gretl_model_get_int(pmod, "arch_order");
2002 	pputc(prn, '\n');
2003 	pprintf(prn, _("Test for ARCH of order %d"), order);
2004 	pputc(prn, '\n');
2005 	break;
2006     case AUX_SYS:
2007 	pprintf(prn, "%s %d: ", _("Equation"), pmod->ID + 1);
2008 	break;
2009     case AUX_VAR:
2010 	pprintf(prn, "\n%s %d: ", _("Equation"), pmod->ID);
2011 	break;
2012     case AUX_VECM:
2013 	pprintf(prn, "%s %d: ", _("Equation"), pmod->ID);
2014 	break;
2015     case AUX_AUX:
2016 	pputc(prn, '\n');
2017 	break;
2018     default:
2019 	/* 2017-03-12: first test was 'pmod->ID < 0' */
2020 	if (pmod->ID <= 0 || (opt & OPT_S)) {
2021 	    if (!csv) {
2022 		pputc(prn, '\n');
2023 	    }
2024 	} else if (pmod->name) {
2025 	    char modname[32];
2026 
2027 	    if (tex) {
2028 		tex_escape(modname, pmod->name);
2029 	    } else {
2030 		strcpy(modname, pmod->name);
2031 	    }
2032 
2033 	    if (csv) {
2034 		pprintf(prn, "\"%s:\"\n", modname);
2035 	    } else if (strlen(pmod->name) > 8) {
2036 		pprintf(prn, "\n%s:\n", modname);
2037 	    } else {
2038 		pprintf(prn, "\n%s: ", modname);
2039 	    }
2040 	} else {
2041 	    if (csv) {
2042 		pprintf(prn, "\"%s %d: ", _("Model"), pmod->ID);
2043 	    } else {
2044 		pprintf(prn, "\n%s %d: ", _("Model"), pmod->ID);
2045 	    }
2046 	}
2047 	break;
2048     }
2049 
2050     if (pmod->aux == AUX_VAR || pmod->aux == AUX_VECM) {
2051 	;
2052     } else if (pmod->aux == AUX_SYS) {
2053 	pprintf(prn, _("%s, using observations %s%s%s"),
2054 		_(system_short_string(pmod)),
2055 		startdate, datesep, enddate);
2056 	maybe_print_T(pmod, dset, startdate, prn);
2057     } else if (!dataset_is_panel(dset)) {
2058 	int mc, Tmax = pmod->t2 - pmod->t1 + 1;
2059 	const char *estr = estimator_string(pmod, prn);
2060 	const char *fmt;
2061 
2062 	if (char_len(estr) > 32) {
2063 	    fmt = N_("%s, obs. %s%s%s");
2064 	    pprintf(prn, _(fmt), _(estr), startdate,
2065 		    (tex)? "--" : "-", enddate);
2066 	    maybe_print_T(pmod, dset, startdate, prn);
2067 	} else {
2068 	    fmt = N_("%s, using observations %s%s%s");
2069 	    pprintf(prn, _(fmt), _(estr), startdate,
2070 		    datesep, enddate);
2071 	    maybe_print_T(pmod, dset, startdate, prn);
2072 	}
2073 
2074 	if (pmod->ci == MIDASREG) {
2075 	    maybe_show_midas_method(pmod, prn);
2076 	}
2077 
2078 	if (gretl_model_get_int(pmod, "binary_obs_dropped")) {
2079 	    /* the relevant info should be printed already */
2080 	    mc = 0;
2081 	} else if (pmod->ci == HECKIT) {
2082 	    mc = Tmax - gretl_model_get_int(pmod, "totobs");
2083 	} else {
2084 	    mc = Tmax - pmod->nobs;
2085 	    if (pmod->ci == WLS) {
2086 		mc -= gretl_model_get_int(pmod, "wt_zeros");
2087 	    }
2088 	}
2089 
2090 	if (mc > 0) {
2091 	    gretl_prn_newline(prn);
2092 	    pprintf(prn, "%s: %d", _("Missing or incomplete observations dropped"),
2093 		    mc);
2094 	}
2095     } else {
2096 	/* panel data */
2097 	int effn = gretl_model_get_int(pmod, "n_included_units");
2098 	int Tmin = gretl_model_get_int(pmod, "Tmin");
2099 	int Tmax = gretl_model_get_int(pmod, "Tmax");
2100 
2101 	pprintf(prn, _("%s, using %d observations"),
2102 		_(estimator_string(pmod, prn)), pmod->nobs);
2103 
2104 	if (pmod->opt & OPT_U) {
2105 	    /* random effects */
2106 	    if (pmod->opt & OPT_N) {
2107 		gretl_prn_newline(prn);
2108 		if (pmod->opt & OPT_X) {
2109 		    pputs(prn, _("Using weighted Nerlove transformation"));
2110 		} else {
2111 		    pputs(prn, _("Using Nerlove's transformation"));
2112 		}
2113 	    } else if (pmod->opt & OPT_X) {
2114 		/* Swamy-Arora special */
2115 		maybe_show_SA_method(pmod, prn);
2116 	    }
2117 	}
2118 
2119 	if (effn > 0) {
2120 	    gretl_prn_newline(prn);
2121 	    pprintf(prn, _("Included %d cross-sectional units"), effn);
2122 	}
2123 	if (Tmin > 0 && Tmax > 0) {
2124 	    gretl_prn_newline(prn);
2125 	    if (Tmin == Tmax) {
2126 		pprintf(prn, _("Time-series length = %d"), Tmin);
2127 	    } else {
2128 		pprintf(prn, _("Time-series length: minimum %d, maximum %d"),
2129 			Tmin, Tmax);
2130 	    }
2131 	}
2132 	if (pmod->ci == DPANEL) {
2133 	    if (pmod->opt & OPT_L) {
2134 		gretl_prn_newline(prn);
2135 		pputs(prn, _("Including equations in levels"));
2136 	    }
2137 	    if (pmod->opt & OPT_X) {
2138 		gretl_prn_newline(prn);
2139 		pputs(prn, _("H-matrix as per Ox/DPD"));
2140 	    }
2141 	}
2142     }
2143 
2144     if (csv) pputc(prn, '"');
2145 
2146     if (pmod->aux != AUX_VAR && pmod->aux != AUX_VECM) {
2147 	gretl_prn_newline(prn);
2148     }
2149 
2150     if (pmod->ci == ARMA && plain_format(prn)) {
2151 	arma_extra_info(pmod, prn);
2152     }
2153 
2154     if (csv) pputc(prn, '"');
2155 
2156     /* special formulations for dependent variable in various cases */
2157     if (pmod->aux == AUX_SQ || pmod->aux == AUX_LOG || pmod->aux == AUX_ADD) {
2158 	pprintf(prn, "%s: %s", _("Dependent variable"),
2159 		(tex)? "$\\hat{u}$" : "uhat");
2160     } else if (pmod->aux == AUX_WHITE || pmod->aux == AUX_HET_1) {
2161 	pprintf(prn, "%s: %s", _("Dependent variable"),
2162 		(tex)? "$\\hat{u}^2$" : "uhat^2");
2163     } else if (pmod->aux == AUX_BP) {
2164 	const char *fmt;
2165 
2166 	if (pmod->opt & OPT_R) {
2167 	    fmt = N_("scaled %s (Koenker robust variant)");
2168 	} else {
2169 	    fmt = N_("scaled %s");
2170 	}
2171 	pprintf(prn, "%s: ", _("Dependent variable"));
2172 	pprintf(prn, _(fmt), (tex)? "$\\hat{u}^2$" : "uhat^2");
2173     } else if (pmod->aux == AUX_ARCH) {
2174 	pprintf(prn, "%s: %s", _("Dependent variable"),
2175 		(tex)? "$u_t^2$" : "ut^2");
2176     } else if (pmod->ci == NLS || pmod->ci == MLE || pmod->ci == GMM) {
2177 	if (pmod->depvar != NULL) {
2178 	    if (tex) {
2179 		pprintf(prn, "\\verb!%s!", pmod->depvar);
2180 	    } else {
2181 		pputs(prn, pmod->depvar);
2182 	    }
2183 	} else {
2184 	    dvnl = 0;
2185 	}
2186     } else if (pmod->ci == ARMA) {
2187 	print_arma_depvar(pmod, dset, prn);
2188     } else if (pmod->ci == INTREG) {
2189 	print_intreg_depvar(pmod, dset, prn);
2190     } else if (pmod->ci == BIPROBIT) {
2191 	dvnl = 0;
2192     } else {
2193 	const char *dvname =
2194 	    gretl_model_get_depvar_name(pmod, dset);
2195 
2196 	if (tex) {
2197 	    if (pmod->aux == AUX_VECM) {
2198 		tex_vecm_depvar_name(vname, dvname);
2199 	    } else if (pmod->ci == DPANEL) {
2200 		tex_dpd_depvar_name(vname, dvname);
2201 	    } else {
2202 		tex_escape(vname, dvname);
2203 	    }
2204 	}
2205 
2206 	if (pmod->aux == AUX_VAR || pmod->aux == AUX_VECM) {
2207 	    pputs(prn, (tex)? vname : dvname);
2208 	} else {
2209 	    pprintf(prn, "%s: %s", _("Dependent variable"),
2210 		    (tex)? vname : dvname);
2211 	}
2212     }
2213 
2214     if (csv) pputc(prn, '"');
2215 
2216     if (dvnl) {
2217 	gretl_prn_newline(prn);
2218     }
2219 
2220     /* supplementary strings below the estimator and sample info */
2221 
2222     if (pmod->ci == IVREG) {
2223 	/* list of instruments for IV estimation */
2224 	int method = gretl_model_get_int(pmod, "method");
2225 
2226 	if (method != SYS_METHOD_FIML && method != SYS_METHOD_LIML) {
2227 	    print_ivreg_instruments(pmod, dset, prn);
2228 	}
2229     } else if (pmod->ci == LAD) {
2230 	/* tau for quantile regression */
2231 	double tau = gretl_model_get_double(pmod, "tau");
2232 
2233 	if (!na(tau)) {
2234 	    if (tex) {
2235 		pprintf(prn, "$\\tau$ = %g", tau);
2236 	    } else {
2237 		pprintf(prn, "tau = %g", tau);
2238 	    }
2239 	    gretl_prn_newline(prn);
2240 	}
2241     } else if (pmod->ci == HECKIT) {
2242 	/* selection variable for Heckit */
2243 	const char *selvar = heckit_selvar_name(pmod, dset);
2244 
2245 	if (selvar != NULL) {
2246 	    if (csv) pputc(prn, '"');
2247 	    if (tex) {
2248 		tex_escape(vname, selvar);
2249 	    }
2250 	    pprintf(prn, "%s: %s", _("Selection variable"),
2251 		    (tex)? vname : selvar);
2252 	    if (csv) pputc(prn, '"');
2253 	    pputc(prn, '\n');
2254 	}
2255     } else if (pmod->ci == AR1) {
2256 	double rho = gretl_model_get_double(pmod, "rho_gls");
2257 
2258 	if (!na(rho)) {
2259 	    if (tex) {
2260 		pprintf(prn, "$\\rho$ = %g", rho);
2261 	    } else {
2262 		pprintf(prn, "rho = %g", rho);
2263 	    }
2264 	    gretl_prn_newline(prn);
2265 	}
2266     } else if (pmod->ci == PROBIT && (pmod->opt & OPT_E)) {
2267 	int qp = gretl_model_get_int(pmod, "quadpoints");
2268 
2269 	if (qp > 0) {
2270 	    pprintf(prn, _("Using %d quadrature points"), qp);
2271 	    gretl_prn_newline(prn);
2272 	}
2273     } else if (pmod->ci == HSK && (pmod->opt & OPT_N)) {
2274 	pputs(prn, _("Without squared terms in variance equation"));
2275 	gretl_prn_newline(prn);
2276     }
2277 
2278     /* VCV variants */
2279     print_model_vcv_info(pmod, dset, prn);
2280 
2281     if (pmod->ci == PANEL && (pmod->opt & OPT_H) && !pmod->aux) {
2282 	/* WLS on panel data */
2283 	if (tex) {
2284 	    pputs(prn, "\\\\\n");
2285 	}
2286 	if (gretl_model_get_int(pmod, "iters")) {
2287 	    pprintf(prn, _("Allowing for groupwise heteroskedasticity"));
2288 	} else {
2289 	    pprintf(prn, _("Weights based on per-unit error variances"));
2290 	}
2291 	pputc(prn, '\n');
2292     } else if (pmod->ci == WLS && !pmod->aux) {
2293 	/* weight variable for WLS */
2294 	if (tex) {
2295 	    tex_escape(vname, dset->varname[pmod->nwt]);
2296 	}
2297 	if (csv) pputc(prn, '"');
2298 	pprintf(prn, "%s: %s", _("Variable used as weight"),
2299 		(tex)? vname : dset->varname[pmod->nwt]);
2300 	if (csv) pputc(prn, '"');
2301 	pputc(prn, '\n');
2302     } else if (pmod->ci == ARCH) {
2303 	/* weight variable for ARCH */
2304 	if (csv) pputc(prn, '"');
2305 	pprintf(prn, "%s: %s", _("Variable used as weight"),
2306 		(tex)? "$1/\\hat{\\sigma}_t$" : "1/sigma");
2307 	if (csv) pputc(prn, '"');
2308 	pputc(prn, '\n');
2309     } else if (pmod->ci == AR1) {
2310 	/* rhohat for AR1 (TeX only) */
2311 	if (tex) {
2312 	    pprintf(prn, "$\\hat{\\rho}$ = %g\n",
2313 		    gretl_model_get_double(pmod, "rho_gls"));
2314 	}
2315     } else if (pmod->ci == LOGISTIC) {
2316 	/* y-hat formula for logistic regression */
2317 	if (tex) {
2318 	    pprintf(prn, "$\\hat{y} \\approx E\\left(%g / (1 + e^{-X\\hat{\\beta}})\\right)$\n",
2319 		    gretl_model_get_double(pmod, "lmax"));
2320 	} else {
2321 	    pprintf(prn, "yhat =~ E(%g / (1 + exp(-X*b)))\n",
2322 		    gretl_model_get_double(pmod, "lmax"));
2323 	}
2324     }
2325 
2326     /* IVREG: message about redundant instruments */
2327     if (plain_format(prn) && pmod->ci == IVREG &&
2328 	gretl_model_get_list(pmod, "inst_droplist") != NULL) {
2329 	print_ivreg_droplist(pmod, dset, prn);
2330     }
2331 
2332     /* messages about collinear and/or zero regressors */
2333     if (plain_format(prn)) {
2334 	if (gretl_model_get_list(pmod, "zerolist") != NULL) {
2335 	    print_model_zerolist(pmod, dset, prn);
2336 	}
2337 	if (gretl_model_get_list(pmod, "droplist") != NULL) {
2338 	    print_model_droplist(pmod, dset, prn);
2339 	}
2340     }
2341 
2342     if (plain_format(prn) && pmod->ci == LAD) {
2343 	maybe_print_lad_warning(pmod, prn);
2344     }
2345 
2346     if (plain_format(prn) && hessian_maybe_fishy(pmod)) {
2347 	maybe_print_hessian_warning(pmod, prn);
2348     }
2349 
2350     if (pmod->missmask == NULL && gretl_model_get_int(pmod, "wt_dummy")) {
2351 	/* FIXME alt formats */
2352 	pprintf(prn, "%s %d\n",
2353 		_("Weight var is a dummy variable, effective obs ="),
2354 		pmod->nobs);
2355     }
2356 
2357     if (rtf_format(prn)) {
2358 	pputs(prn, "\\par\n");
2359     } else {
2360 	pputc(prn, '\n');
2361     }
2362 }
2363 
model_format_start(PRN * prn)2364 static void model_format_start (PRN *prn)
2365 {
2366     if (tex_format(prn)) {
2367 	if (tex_doc_format(prn)) {
2368 	    gretl_tex_preamble(prn, 0);
2369 	}
2370 	pputs(prn, "\\begin{center}\n");
2371     } else if (rtf_format(prn)) {
2372 	gretl_print_set_has_minus(prn);
2373 	if (rtf_doc_format(prn)) {
2374 	    pputs(prn, "{\\rtf1\\par\n\\qc ");
2375 	} else {
2376 	    pputs(prn, "\\par\n\\qc ");
2377 	}
2378     }
2379 }
2380 
2381 #define RTF_MULTICOL  "\\trowd \\trqc \\trgaph30\\trleft-30\\trrh262"	\
2382     "\\cellx8000\n\\intbl"
2383 
2384 #define RTF_COEFF_ROW  "\\trowd \\trqc \\trgaph30\\trleft-30\\trrh262"	\
2385     "\\cellx1900\\cellx3300\\cellx4700\\cellx6100"			\
2386     "\\cellx7500\\cellx8000\n\\intbl"
2387 
2388 #define RTF_BINARY_ROW "\\trowd \\trqc \\trgaph30\\trleft-30\\trrh262"	\
2389     "\\cellx1900\\cellx3300\\cellx4700\\cellx6100"			\
2390     "\\cellx8000\n\\intbl"
2391 
2392 #define RTF_INTVL_ROW "\\trowd \\trqc \\trgaph30\\trleft-30\\trrh262"	\
2393     "\\cellx1900\\cellx3300\\cellx4700\\cellx6100"			\
2394     "\n\\intbl"
2395 
2396 #define RTF_ROOT_ROW   "\\trowd \\trqc \\trgaph30\\trleft-30\\trrh262"	\
2397     "\\cellx500\\cellx1500\\cellx2900\\cellx4300"			\
2398     "\\cellx5700\\cellx7100\n\\intbl"
2399 
2400 /* below: this is used when we're doing something other than a plain
2401    text print of a model. When using TeX, returns the number of
2402    columns in the table, otherwise just returns 0.
2403 */
2404 
alt_print_coeff_table_start(const MODEL * pmod,int ci,PRN * prn)2405 static int alt_print_coeff_table_start (const MODEL *pmod, int ci, PRN *prn)
2406 {
2407     const char *tlabel;
2408     int use_param = 0;
2409     int slopes = 0;
2410     int intervals = 0;
2411     int seqcols = 0;
2412     int mp = 0, ret = 0;
2413 
2414     if (model_use_zscore(pmod)) {
2415 	tlabel = (tex_format(prn))? N_("$z$") : N_("z");
2416     } else {
2417 	tlabel = (tex_format(prn))? N_("$t$-ratio") : N_("t-ratio");
2418     }
2419 
2420     if (pmod != NULL) {
2421 	gretl_matrix *m;
2422 
2423 	use_param = NONLIST_MODEL(pmod->ci);
2424 	slopes = binary_model(pmod) && !(pmod->opt & OPT_P);
2425 	intervals = gretl_model_get_data(pmod, "coeff_intervals") != NULL;
2426 	m = gretl_model_get_data(pmod, "rq_sequence");
2427 	seqcols = gretl_matrix_cols(m);
2428 	mp = (pmod->ci == MPOLS);
2429     }
2430 
2431     if (csv_format(prn)) {
2432 	char d = prn_delim(prn);
2433 
2434 	if (mp) {
2435 	    pprintf(prn, "%c\"%s\"%c\"%s\"\n",
2436 		    d, _("coefficient"), d, _("std. error"));
2437 	} else if (slopes) {
2438 	    pprintf(prn, "%c\"%s\"%c\"%s\"%c\"%s\"%c\"%s\"\n",
2439 		    d, _("coefficient"), d, _("std. error"),
2440 		    d, _(tlabel), d, _("slope at mean"));
2441 	} else if (use_param) {
2442 	    pprintf(prn, "%c\"%s\"%c\"%s\"%c\"%s\"%c\"%s\"\n",
2443 		    d, _("estimate"), d, _("std. error"),
2444 		    d, _(tlabel), d, _("p-value"));
2445 	} else if (intervals) {
2446 	    pprintf(prn, "%c\"%s\"%c\"%s\"%c\"%s\"\n",
2447 		    d, _("coefficient"), d, _("lower"),
2448 		    d, _("upper"));
2449 	} else if (seqcols == 3) {
2450 	    pprintf(prn, "%c\"%s\"%c\"%s\"%c\"%s\"%c\"%s\"\n",
2451 		    d, "tau", d, _("coefficient"), d, _("lower"),
2452 		    d, _("upper"));
2453 	} else if (seqcols == 2) {
2454 	    pprintf(prn, "%c\"%s\"%c\"%s\"%c\"%s\"\n",
2455 		    d, _("coefficient"), d, _("std. error"),
2456 		    d, _(tlabel));
2457 	} else {
2458 	    pprintf(prn, "%c\"%s\"%c\"%s\"%c\"%s\"%c\"%s\"\n",
2459 		    d, _("coefficient"), d, _("std. error"),
2460 		    d, _(tlabel), d, _("p-value"));
2461 	}
2462     } else {
2463 	const char *cols[6] = { NULL };
2464 	int i;
2465 
2466 	cols[0] = " ";
2467 	cols[1] = (use_param)? N_("Estimate") : N_("Coefficient");
2468 
2469 	if (intervals) {
2470 	    cols[2] = N_("Lower");
2471 	    cols[3] = N_("Upper");
2472 	} else if (seqcols == 3) {
2473 	    cols[2] = N_("tau");
2474 	    cols[3] = N_("Lower");
2475 	    cols[4] = N_("Upper");
2476 	} else {
2477 	    cols[2] = (tex_format(prn))? N_("Std.\\ Error") : N_("Std. Error");
2478 	    if (!mp) {
2479 		cols[3] = tlabel;
2480 		cols[4] = (slopes)? N_("Slope") : N_("p-value");
2481 	    }
2482 	}
2483 
2484 	if (tex_format(prn)) {
2485 	    gretlopt tabopt = (ci == MODPRINT)? OPT_U : OPT_NONE;
2486 
2487 	    if (slopes) {
2488 		tabopt |= OPT_B; /* "binary" */
2489 	    }
2490 	    if (mp) {
2491 		tabopt |= OPT_M; /* multiple precision */
2492 	    }
2493 	    ret = tex_coeff_table_start(cols, tabopt, prn);
2494 	} else if (rtf_format(prn)) {
2495 	    pputc(prn, '{');
2496 	    if (intervals) {
2497 		pputs(prn, RTF_INTVL_ROW);
2498 	    } else if (slopes) {
2499 		pputs(prn, RTF_BINARY_ROW);
2500 	    } else {
2501 		pputs(prn, RTF_COEFF_ROW);
2502 	    }
2503 	    for (i=0; cols[i] != NULL; i++) {
2504 		if (slopes && i == 4) {
2505 		    pprintf(prn, " \\qc {\\i %s{\\super *}}\\cell", _(cols[i]));
2506 		} else {
2507 		    pprintf(prn, " \\qc {\\i %s}\\cell", _(cols[i]));
2508 		}
2509 	    }
2510 	    if (!slopes && !intervals) {
2511 		pputs(prn, " \\ql \\cell");
2512 	    }
2513 	    pputs(prn, " \\intbl \\row\n");
2514 	}
2515     }
2516 
2517     return ret;
2518 }
2519 
print_coeff_table_end(const MODEL * pmod,PRN * prn)2520 static void print_coeff_table_end (const MODEL *pmod, PRN *prn)
2521 {
2522     if (plain_format(prn) || csv_format(prn)) {
2523 	pputc(prn, '\n');
2524     } else if (tex_format(prn)) {
2525 	tex_coeff_table_end(prn);
2526     } else if (rtf_format(prn)) {
2527 	pputs(prn, "}\n\n");
2528     }
2529 
2530     if (pmod == NULL) {
2531 	/* no MODEL is given when we're called via the
2532 	   "modprint" command */
2533 	return;
2534     }
2535 
2536     /* Maybe print "near-singularity" message? Not in the
2537        case of NLS or MIDAS regression, when this will be
2538        handled in conjunction with printing GNR info.
2539     */
2540     if (pmod->ci == NLS || pmod->ci == MIDASREG) {
2541 	;
2542     } else if (plain_format(prn) && gretl_model_get_int(pmod, "near-singular")) {
2543 	const char *msg = N_("Warning: data matrix close to singularity!");
2544 
2545 	pprintf(prn, "%s\n\n", _(msg));
2546     }
2547 }
2548 
model_format_end(PRN * prn)2549 static void model_format_end (PRN *prn)
2550 {
2551     if (tex_format(prn)) {
2552 	pputs(prn, "\n\\end{center}\n");
2553 	if (tex_doc_format(prn)) {
2554 	    pputs(prn, "\n\\end{document}\n");
2555 	}
2556     } else if (rtf_doc_format(prn)) {
2557 	pputs(prn, "\n}\n");
2558     }
2559 }
2560 
addconst_message(const MODEL * pmod,PRN * prn)2561 static void addconst_message (const MODEL *pmod, PRN *prn)
2562 {
2563     if (gretl_model_get_int(pmod, "addconst")) {
2564 	pprintf(prn, "\n\n%s\n",
2565 		_("WARNING: The constant was present among the regressors but not among the\n"
2566 		  "instruments, so it has been automatically added to the instrument list.\n"
2567 		  "This behavior may change in future versions, so you may want to adjust your\n"
2568 		  "scripts accordingly.\n"));
2569     }
2570 }
2571 
2572 enum {
2573     ORIG_STATS,
2574     WTD_STATS,
2575     RHODIFF_STATS,
2576     TRANSFORM_STATS
2577 };
2578 
alternate_stats_message(int i,PRN * prn)2579 static void alternate_stats_message (int i, PRN *prn)
2580 {
2581     const char *msg[] = {
2582 	N_("Statistics based on the original data"),
2583 	N_("Statistics based on the weighted data"),
2584 	N_("Statistics based on the rho-differenced data"),
2585 	N_("Statistics based on the transformed data")
2586     };
2587 
2588     if (plain_format(prn)) {
2589 	pprintf(prn, "%s:\n\n", _(msg[i]));
2590     } else if (tex_format(prn)) {
2591 	pprintf(prn, "\\vspace{1em}%s:\n\n", _(msg[i]));
2592     } else if (csv_format(prn)) {
2593 	pprintf(prn, "\"%s\"\n", _(msg[i]));
2594     } else {
2595 	/* RTF */
2596 	pprintf(prn, "\\par \\qc\n%s:\n\n", _(msg[i]));
2597     }
2598 }
2599 
print_whites_results(const MODEL * pmod,PRN * prn)2600 static void print_whites_results (const MODEL *pmod, PRN *prn)
2601 {
2602     double X = pmod->rsq * pmod->nobs;
2603     int df = pmod->ncoeff - 1;
2604     double pv = chisq_cdf_comp(df, X);
2605 
2606     if (plain_format(prn)) {
2607 	pprintf(prn, "\n%s: TR^2 = %f,\n", _("Test statistic"), X);
2608 	pprintf(prn, "%s = P(%s(%d) > %f) = %f\n\n",
2609 		_("with p-value"), _("Chi-square"), df, X, pv);
2610     } else if (rtf_format(prn)) { /* FIXME */
2611 	pprintf(prn, "\\par \\ql\n%s: TR{\\super 2} = %f,\n", _("Test statistic"),
2612 		X);
2613 	pprintf(prn, "%s = P(%s(%d) > %f) = %f\n\n",
2614 		_("with p-value"), _("Chi-square"), df, X, pv);
2615     } else if (tex_format(prn)) {
2616 	pprintf(prn, "\n%s: $TR^2$ = %f,\n", _("Test statistic"), X);
2617 	pprintf(prn, "%s = $P$($\\chi^2(%d)$ > %f) = %f\n\n",
2618 		_("with p-value"), df, X, pv);
2619     }
2620 }
2621 
print_bp_results(const MODEL * pmod,PRN * prn)2622 static void print_bp_results (const MODEL *pmod, PRN *prn)
2623 {
2624     double pv, X = gretl_model_get_double(pmod, "BPLM");
2625     int df = pmod->ncoeff - 1;
2626 
2627     if (na(X)) {
2628 	return;
2629     }
2630 
2631     pv = chisq_cdf_comp(df, X);
2632 
2633     if (plain_format(prn)) {
2634 	pprintf(prn, "\n%s: LM = %f,\n", _("Test statistic"), X);
2635 	pprintf(prn, "%s = P(%s(%d) > %f) = %f\n\n",
2636 		_("with p-value"), _("Chi-square"), df, X, pv);
2637     } else if (rtf_format(prn)) { /* FIXME */
2638 	pprintf(prn, "\\par \\ql\n%s: LM = %f,\n", _("Test statistic"),
2639 		X);
2640 	pprintf(prn, "%s = P(%s(%d) > %f) = %f\n\n",
2641 		_("with p-value"), _("Chi-square"), df, X, pv);
2642     } else if (tex_format(prn)) {
2643 	pprintf(prn, "\n%s: LM = %f,\n", _("Test statistic"), X);
2644 	pprintf(prn, "%s = $P$($\\chi^2(%d)$ > %f) = %f\n\n",
2645 		_("with p-value"), df, X, pv);
2646     }
2647 }
2648 
print_HET_1_results(const MODEL * pmod,PRN * prn)2649 static void print_HET_1_results (const MODEL *pmod, PRN *prn)
2650 {
2651     double z = fabs(pmod->coeff[1]) / pmod->sderr[1];
2652     double pv = 2.0 * (1 - normal_cdf(z));
2653 
2654     if (plain_format(prn)) {
2655 	pprintf(prn, "\n%s: HET_1 = |%f| / %f = %f,\n", _("Test statistic"),
2656 		pmod->coeff[1], pmod->sderr[1], z);
2657 	pprintf(prn, "%s = 2 * P(z > %f) = %.3g\n\n",
2658 		_("with p-value"), z, pv);
2659     } else if (rtf_format(prn)) { /* FIXME */
2660 	pprintf(prn, "\\par \\ql\n%s: HET_1 = %f,\n", _("Test statistic"), z);
2661 	pprintf(prn, "%s = 2 * P(z > %f) = %.3g\n\n",
2662 		_("with p-value"), z, pv);
2663     } else if (tex_format(prn)) {
2664 	pprintf(prn, "\n%s: \verb|HET_1| = %f,\n", _("Test statistic"), z);
2665 	pprintf(prn, "%s = $2 \times P$($z$ > %f) = %f\n\n",
2666 		_("with p-value"), z, pv);
2667     }
2668 }
2669 
maybe_print_jll(const MODEL * pmod,int lldig,PRN * prn)2670 static void maybe_print_jll (const MODEL *pmod, int lldig, PRN *prn)
2671 {
2672     double jll = gretl_model_get_double(pmod, "jll");
2673 
2674     if (!na(jll)) {
2675 	const char *lp;
2676 	gchar *jllstr;
2677 	char xstr[32];
2678 
2679 	lp = gretl_model_get_data(pmod, "log-parent");
2680 	jllstr = g_strdup_printf(_("Log-likelihood for %s"), lp);
2681 	if (lldig > 0) {
2682 	    plain_print_double(xstr, lldig, jll, prn);
2683 	    pprintf(prn, "  (%s = %s)\n", jllstr, xstr);
2684 	} else {
2685 	    plain_print_double(xstr, XDIGITS(pmod), jll, prn);
2686 	    pprintf(prn, "%s = %s\n\n", jllstr, xstr);
2687 	}
2688 	g_free(jllstr);
2689     }
2690 }
2691 
2692 #define fixed_effects_model(m) (m->ci == PANEL && (m->opt & OPT_F))
2693 
2694 #define random_effects_model(m) (m->ci == PANEL && (m->opt & OPT_U))
2695 
2696 #define weighted_model(m) (m->ci == HSK || m->ci == ARCH ||		\
2697 			   (m->ci == WLS && !gretl_model_get_int(m, "wt_dummy")) || \
2698                            (m->ci == PANEL && (m->opt & OPT_H)))
2699 
2700 #define panel_ML_model(m) (m->ci == PANEL && (m->opt & OPT_H) &&	\
2701 			   gretl_model_get_int(m, "iters"))
2702 
2703 #define non_weighted_panel(m) (m->ci == PANEL && !(m->opt & OPT_H))
2704 
2705 #define MID_STATS 14
2706 
2707 struct middletab {
2708     const char **key;     /* stats strings */
2709     double *val;          /* stats values */
2710     int minus;            /* variant of minus sign in use */
2711     int mlen;             /* max length of translated string */
2712     int ipos[MID_STATS];  /* is i-th stat integer? */
2713     int nls;              /* translation on? (0/1) */
2714     int d;                /* CSV field delimiter */
2715     int multi;            /* Using multiple precision? (0/1) */
2716     char txt_fmt[48];     /* format for plain text output */
2717 };
2718 
set_mtab_string_width(struct middletab * mt)2719 static void set_mtab_string_width (struct middletab *mt)
2720 {
2721     int len, badkey = 0, maxlen = 0;
2722     int i, j;
2723 
2724     for (i=0, j=0; i<7; i++, j+=2) {
2725 	if (!na(mt->val[j])) {
2726 	    len = g_utf8_strlen(_(mt->key[j]), -1);
2727 	    if (len > maxlen) {
2728 		maxlen = len;
2729 		badkey = j;
2730 	    }
2731 	    len = g_utf8_strlen(_(mt->key[j+1]), -1);
2732 	    if (len > maxlen) {
2733 		maxlen = len;
2734 		badkey = j+1;
2735 	    }
2736 	}
2737     }
2738 
2739     if (maxlen > 22) {
2740 	fprintf(stderr, "Can't make compact model stats table -- the max\n"
2741 		"length translated string is %d chars, should be < 23\n",
2742 		maxlen);
2743 	fprintf(stderr, "offending string: '%s' ->\n '%s'\n",
2744 		mt->key[badkey], _(mt->key[badkey]));
2745     }
2746 
2747     mt->mlen = maxlen;
2748 }
2749 
middletab_prepare_format(struct middletab * mt,int j)2750 static void middletab_prepare_format (struct middletab *mt, int j)
2751 {
2752     const char *s1 = mt->key[j];
2753     const char *s2 = mt->key[j+1];
2754     int d1 = 0, d2 = 0;
2755     int len = mt->mlen;
2756 
2757     if (mt->nls) {
2758 	/* calculate bytes minus glyphs */
2759 	d1 = strlen(_(s1)) - g_utf8_strlen(_(s1), -1);
2760 	d2 = strlen(_(s2)) - g_utf8_strlen(_(s2), -1);
2761     }
2762 
2763     if (mt->multi) {
2764 	len = (len < 20)? 20 : len + 1;
2765 	sprintf(mt->txt_fmt, "  %%-%ds  %%s\n  %%-%ds  %%s\n", len + d1, len + d2);
2766     } else if (len < 20) {
2767 	sprintf(mt->txt_fmt, "%%-%ds%%s   %%-%ds%%s\n", 20 + d1, 20 + d2);
2768     } else if (len < 23) {
2769 	sprintf(mt->txt_fmt, "%%-%ds%%s   %%-%ds%%s\n", len + d1 + 1, len + d2 + 1);
2770     } else {
2771 	sprintf(mt->txt_fmt, "  %%-%ds  %%s\n  %%-%ds  %%s\n", len + d1, len + d2);
2772     }
2773 }
2774 
print_csv(char * s,double x)2775 static char *print_csv (char *s, double x)
2776 {
2777     if (na(x)) {
2778 	strcpy(s, "NA");
2779     } else {
2780 	sprintf(s, "%.15g", x);
2781     }
2782 
2783     return s;
2784 }
2785 
2786 enum {
2787     MINUS_HYPHEN,
2788     MINUS_UTF,
2789     MINUS_TEX
2790 };
2791 
2792 #define RSQ_POS 4
2793 
mtab_numstart(char * s,double x,int d7,int minus)2794 static void mtab_numstart (char *s, double x, int d7, int minus)
2795 {
2796     if (x < 0) {
2797 	if (minus == MINUS_UTF) {
2798 	    strcpy(s, (d7)? " −" : "−"); /* U+2212: minus */
2799 	} else if (minus == MINUS_TEX) {
2800 	    strcpy(s, "$-$");
2801 	} else {
2802 	    strcpy(s, (d7)? " -" : "-"); /* ASCII: use hyphen */
2803 	}
2804     } else {
2805 	strcpy(s, " ");
2806     }
2807 }
2808 
2809 #define seven_digits(x) (x > 999999 && x < 1.0e8)
2810 
2811 /* Try to pack as much as we can of a given number into a fixed width
2812    of 8 characters (a leading minus, if needed, is a ninth).
2813 */
2814 
print_eight(char * s,struct middletab * mt,int i)2815 static char *print_eight (char *s, struct middletab *mt, int i)
2816 {
2817     double ax, x = mt->val[i];
2818     int d7 = (x < 0 && seven_digits(-x));
2819     char tmp[16];
2820 
2821     if (mt->ipos[i]) {
2822 	sprintf(s, "%9d", (int) x);
2823 	return s;
2824     }
2825 
2826     if (na(x)) {
2827 	sprintf(s, "%9s", "NA");
2828 	return s;
2829     }
2830 
2831     if (i == RSQ_POS || i == RSQ_POS + 1) {
2832 	/* R-squared: don't use scientific notation */
2833 	sprintf(s, "%9.6f", x);
2834 	return s;
2835     }
2836 
2837     mtab_numstart(s, x, d7, mt->minus);
2838 
2839     ax = fabs(x);
2840 
2841     if (d7) {
2842 	sprintf(tmp, "%.0f", ax);
2843     } else if (ax < 0.00001 || ax > 99999999) {
2844 	char *p;
2845 
2846 	sprintf(tmp, "%#.3g", ax);
2847 	p = strrchr(tmp, (ax < 1)? '-' : '+');
2848 	if (p == NULL) {
2849 	    sprintf(tmp, "%8.6f", ax);
2850 	} else if (strlen(p) == 4) {
2851 	    if (*(p+1) == '0') {
2852 		memmove(p+1, p+2, 3);
2853 	    } else {
2854 		sprintf(tmp, "%#.2g", ax);
2855 	    }
2856 	}
2857     } else if (ax < 10.0) {
2858 	sprintf(tmp, "%8.6f", ax);
2859     } else {
2860 	double lx = log10(ax);
2861 	int ldig = ceil(lx) + (lx == floor(lx));
2862 
2863 	ldig = (ldig > 7)? 7 : ldig;
2864 	sprintf(tmp, "%8.*f", 8 - ldig - 1, ax);
2865     }
2866 
2867     strcat(s, tmp);
2868 
2869     if (mt->minus == MINUS_TEX && strchr(s, 'e') != NULL) {
2870 	tex_modify_exponent(s);
2871     }
2872 
2873     return s;
2874 }
2875 
print_fifteen(char * s,double x,int minus)2876 static char *print_fifteen (char *s, double x, int minus)
2877 {
2878     if (na(x)) {
2879 	strcpy(s, " NA");
2880     } else if (minus == MINUS_TEX) {
2881 	char *p;
2882 
2883 	if (x < 0) {
2884 	    sprintf(s, "$-$%.15E", -x);
2885 	} else {
2886 	    sprintf(s, "%.15E", x);
2887 	}
2888 
2889 	if ((p = strstr(s, "E-")) != NULL) {
2890 	    char tmp[8];
2891 
2892 	    sprintf(tmp, "E--%s", p + 2);
2893 	    strcpy(p, tmp);
2894 	}
2895     } else if (minus == MINUS_UTF) {
2896 	if (x < 0) {
2897 	    sprintf(s, "−%.15E", -x);
2898 	} else {
2899 	    sprintf(s, "% .15E", x);
2900 	}
2901     } else {
2902 	sprintf(s, "% .15E", x);
2903     }
2904 
2905     return s;
2906 }
2907 
2908 #define RTF_MT_ROW "\\trowd \\trqc \\trgaph30\\trleft-30\\trrh262"	\
2909     "\\cellx2500\\cellx3800\\cellx4200\\cellx6700"			\
2910     "\\cellx8000\n"
2911 
2912 #define RTF_MULTI_ROW "\\trowd \\trqc \\trgaph30\\trleft-30\\trrh262"	\
2913     "\\cellx2500\\cellx6000\n"
2914 
2915 #define RTF_MT_FMT "\\intbl\\ql %s\\cell\\qr %s\\cell \\qc \\cell"	\
2916     "\\ql %s\\cell\\qr %s\\cell\\intbl\\row\n"
2917 
2918 #define RTF_MULTI_FMT "\\intbl\\ql %s\\cell\\qr %s\\cell\\intbl\\row\n"
2919 
2920 #define TXT_MT_FMT "%-20s%s   %-20s%s\n"
2921 
2922 enum {
2923     MIDDLE_REGULAR,
2924     MIDDLE_TRANSFORM,
2925     MIDDLE_ORIG
2926 };
2927 
middle_table_row(struct middletab * mt,int j,PRN * prn)2928 static void middle_table_row (struct middletab *mt, int j, PRN *prn)
2929 {
2930     const char *s1 = mt->key[j];
2931     const char *s2 = mt->key[j+1];
2932     char x1[48], x2[48];
2933     int k = j + 1;
2934 
2935     if (tex_format(prn)) {
2936 	if (mt->multi) {
2937 	    pprintf(prn, "%s & %s \\\\\n%s & %s \\\\\n",
2938 		    _(s1), print_fifteen(x1, mt->val[j], mt->minus),
2939 		    _(s2), print_fifteen(x2, mt->val[k], mt->minus));
2940 	} else {
2941 	    pprintf(prn, "%s & %s & %s & %s \\\\\n",
2942 		    _(s1), print_eight(x1, mt, j),
2943 		    _(s2), print_eight(x2, mt, k));
2944 	}
2945     } else if (rtf_format(prn)) {
2946 	if (mt->multi) {
2947 	    pputs(prn, RTF_MULTI_ROW);
2948 	    pprintf(prn, RTF_MULTI_FMT, _(s1),
2949 		    print_fifteen(x1, mt->val[j], mt->minus));
2950 	    pputs(prn, RTF_MULTI_ROW);
2951 	    pprintf(prn, RTF_MULTI_FMT, _(s2),
2952 		    print_fifteen(x1, mt->val[k], mt->minus));
2953 	} else {
2954 	    pputs(prn, RTF_MT_ROW);
2955 	    pprintf(prn, RTF_MT_FMT,
2956 		    _(s1), print_eight(x1, mt, j),
2957 		    _(s2), print_eight(x2, mt, k));
2958 	}
2959     } else if (csv_format(prn)) {
2960 	pprintf(prn, "\"%s\"%c%s%c\"%s\"%c%s\n",
2961 		_(s1), mt->d, print_csv(x1, mt->val[j]), mt->d,
2962 		_(s2), mt->d, print_csv(x2, mt->val[k]));
2963     } else {
2964 	if (mt->nls || mt->multi) {
2965 	    middletab_prepare_format(mt, j);
2966 	}
2967 	if (mt->multi) {
2968 	    pprintf(prn, mt->txt_fmt,
2969 		    _(s1), print_fifteen(x1, mt->val[j], mt->minus),
2970 		    _(s2), print_fifteen(x2, mt->val[k], mt->minus));
2971 	} else {
2972 	    pprintf(prn, mt->txt_fmt,
2973 		    _(s1), print_eight(x1, mt, j),
2974 		    _(s2), print_eight(x2, mt, k));
2975 	}
2976     }
2977 }
2978 
string_is_translated(const char * s)2979 static int string_is_translated (const char *s)
2980 {
2981     if (strcmp(s, "Hannan-Quinn") && strcmp(s, "rho")) {
2982 	return strcmp(s, _(s));
2983     } else {
2984 	/* give the benefit of the doubt */
2985 	return 1;
2986     }
2987 }
2988 
2989 /* If we haven't found a translation for a new-style short string,
2990    use the corresponding old-style long string instead.
2991 */
2992 
maybe_remedy_translations(const char ** S,int n)2993 static void maybe_remedy_translations (const char **S, int n)
2994 {
2995     const char *old_key[] = {
2996 	N_("Mean of dependent variable"),
2997 	N_("Standard deviation of dep. var."),
2998 	N_("Sum of squared residuals"),
2999 	N_("Standard error of the regression"),
3000 	N_("Unadjusted R-squared"),
3001 	NULL,
3002 	NULL,
3003 	NULL,
3004 	NULL,
3005 	N_("Akaike information criterion"),
3006 	N_("Schwarz Bayesian criterion"),
3007 	N_("Hannan-Quinn Information Criterion"),
3008 	NULL,
3009 	NULL
3010     };
3011     int i;
3012 
3013     for (i=0; i<n; i++) {
3014 	if (old_key[i] != NULL && !string_is_translated(S[i])) {
3015 	    /* new-style string is not translated */
3016 	    if (strcmp(old_key[i], _(old_key[i]))) {
3017 		/* but the old-style one is, so we'll use it */
3018 		S[i] = old_key[i];
3019 	    }
3020 	}
3021     }
3022 }
3023 
3024 /* order of keys for model stats in middle table */
3025 
3026 enum {
3027     K_YBAR,
3028     K_SDY,
3029     K_SSR,
3030     K_SER,
3031     K_RSQ,
3032     K_R22,
3033     K_FST,
3034     K_PVF,
3035     K_LNL,
3036     K_AKA,
3037     K_SCZ,
3038     K_HQ,
3039     K_RHO,
3040     K_DW
3041 };
3042 
3043 /* print the block of statistics that appears beneath of the
3044    table of coefficients, standard errors, etc.
3045 */
3046 
print_middle_table(MODEL * pmod,PRN * prn,int code)3047 static void print_middle_table (MODEL *pmod, PRN *prn, int code)
3048 {
3049     const char *note =
3050 	/* TRANSLATORS: please do not translate literally: this is for
3051 	   your use in describing locale-specific abbreviations. It is
3052 	   OK to leave it untranslated, in which case it will not be
3053 	   printed.
3054 	*/
3055 	N_("note on model statistics abbreviations here");
3056     int rtf = rtf_format(prn);
3057     int tex = tex_format(prn);
3058     int csv = csv_format(prn);
3059     gchar *teststr = NULL;
3060     const char *key[] = {
3061 	/* TRANSLATORS: maximum length of string is 22 characters */
3062 	N_("Mean dependent var"),  /* Mean of dependent variable */
3063 	/* TRANSLATORS: maximum length of string is 22 characters */
3064 	N_("S.D. dependent var"),  /* Standard deviation of dependent var */
3065 	/* TRANSLATORS: maximum length of string is 22 characters */
3066 	N_("Sum squared resid"),   /* Sum of squared residuals */
3067 	/* TRANSLATORS: maximum length of string is 22 characters */
3068 	N_("S.E. of regression"),  /* Standard error of the regression */
3069 	/* TRANSLATORS: maximum length of string is 22 characters */
3070 	N_("R-squared"),
3071 	/* TRANSLATORS: maximum length of string is 22 characters */
3072 	N_("Adjusted R-squared"),
3073 	"F-statistic",             /* will be replaced below */
3074 	/* TRANSLATORS: maximum length of string is 22 characters */
3075 	N_("P-value(F)"),          /* P-value of F-statistic */
3076 	/* TRANSLATORS: maximum length of string is 22 characters */
3077 	N_("Log-likelihood"),
3078 	/* TRANSLATORS: maximum length of string is 22 characters */
3079 	N_("Akaike criterion"),    /* Akaike Information Criterion */
3080 	/* TRANSLATORS: maximum length of string is 22 characters */
3081 	N_("Schwarz criterion"),   /* Schwarz Bayesian Criterion */
3082 	/* TRANSLATORS: maximum length of string is 22 characters */
3083 	N_("Hannan-Quinn"),        /* Hannan-Quinn Criterion */
3084 	/* TRANSLATORS: maximum length of string is 22 characters */
3085 	N_("rho"),                 /* 1st-order autocorrelation coeff. */
3086 	/* TRANSLATORS: maximum length of string is 22 characters */
3087 	N_("Durbin-Watson")        /* Durbin-Watson statistic */
3088     };
3089     double val[MID_STATS] = {
3090 	pmod->ybar,
3091 	pmod->sdy,
3092 	pmod->ess,
3093 	pmod->sigma,
3094 	pmod->rsq,
3095 	pmod->adjrsq,
3096 	pmod->fstt,
3097 	NADBL,
3098 	pmod->lnL,
3099 	pmod->criterion[C_AIC],
3100 	pmod->criterion[C_BIC],
3101 	pmod->criterion[C_HQC],
3102 	pmod->rho,
3103 	pmod->dw
3104     };
3105     struct middletab mtab;
3106     int i, j;
3107 
3108     mtab.mlen = 0;
3109     mtab.d = 0;
3110     mtab.minus = MINUS_HYPHEN;
3111     for (i=0; i<MID_STATS; i++) {
3112 	mtab.ipos[i] = 0;
3113     }
3114     mtab.nls = 0;
3115     mtab.multi = (pmod->ci == MPOLS);
3116 
3117     mtab.nls = doing_nls();
3118     if (mtab.nls) {
3119 	maybe_remedy_translations(key, MID_STATS);
3120     }
3121 
3122     if (tex) {
3123 	/* some special strings for TeX output */
3124 	mtab.minus = MINUS_TEX;
3125 	key[K_RSQ] = "$R^2$";
3126 	key[K_R22] = N_("Adjusted $R^2$");
3127 	key[K_PVF] = N_("P-value($F$)");
3128 	key[K_HQ]  = "Hannan--Quinn";
3129 	key[K_RHO] = "$\\hat{\\rho}$";
3130 	key[K_DW]  = "Durbin--Watson";
3131     } else if (gretl_print_has_minus(prn)) {
3132 	/* print a 'real' minus sign? */
3133 	mtab.minus = MINUS_UTF;
3134     }
3135 
3136     if (pmod->aux == AUX_VECM || pmod->aux == AUX_COINT) {
3137 	/* VECM equation or Engle-Granger test: suppress F-test */
3138 	val[K_FST] = val[K_PVF] = NADBL;
3139     } else if (!na(pmod->fstt) && pmod->dfd > 0) {
3140 	/* format F-stat and get its p-value */
3141 	int nc = gretl_model_get_int(pmod, "n_clusters");
3142 	int dfd = nc > 1 ? nc - 1 : pmod->dfd;
3143 
3144 	if (tex) {
3145 	    teststr = g_strdup_printf("$F(%d, %d)$", pmod->dfn, dfd);
3146 	} else if ((pmod->ci == PANEL || pmod->ci == LOGISTIC) &&
3147 		   (pmod->opt & OPT_F)) {
3148 	    teststr = g_strdup_printf(_("LSDV F(%d, %d)"), pmod->dfn, dfd);
3149 	} else {
3150 	    teststr = g_strdup_printf("F(%d, %d)", pmod->dfn, dfd);
3151 	}
3152 	key[K_FST] = teststr;
3153 	val[K_PVF] = snedecor_cdf_comp(pmod->dfn, dfd, pmod->fstt);
3154     } else if (!na(pmod->chisq)) {
3155 	/* alternative: chi-square and its p-value */
3156 	teststr = g_strdup_printf("%s(%d)", _("Chi-square"), pmod->dfn);
3157 	key[K_FST] = teststr;
3158 	val[K_FST] = pmod->chisq;
3159 	key[K_PVF] = N_("p-value");
3160 	val[K_PVF] = chisq_cdf_comp(pmod->dfn, val[6]);
3161     }
3162 
3163     /* special variants of R-squared */
3164     if (gretl_model_get_int(pmod, "uncentered")) {
3165 	key[K_RSQ] = (tex)? N_("Uncentered $R^2$") :
3166 	    N_("Uncentered R-squared"); /* 22: */
3167 	key[K_R22] = (tex)? N_("Centered $R^2$") :
3168 	    N_("Centered R-squared");  /* 22: */
3169 	val[K_R22] = gretl_model_get_double(pmod, "centered-R2");
3170     } else if (COUNT_MODEL(pmod->ci)) {
3171 	key[K_RSQ] = (tex)? N_("McFadden $R^2$") :
3172 	    N_("McFadden R-squared");  /* 22: McFadden pseudo-R^2 */
3173     } else if (binary_model(pmod)) {
3174 	if (pmod->opt & OPT_S) {
3175 	    key[K_RSQ] = (tex)? N_("Estrella $R^2$") :
3176 		N_("Estrella R-squared");  /* 22: Estrella pseudo-R^2 */
3177 	} else {
3178 	    key[K_RSQ] = (tex)? N_("McFadden $R^2$") :
3179 		N_("McFadden R-squared");  /* 22: McFadden pseudo-R^2 */
3180 	}
3181     } else if ((pmod->ci == PANEL || pmod->ci == LOGISTIC) &&
3182 	       (pmod->opt & OPT_F)) {
3183 	/* 22: panel, fixed effects */
3184 	key[K_RSQ] = (tex)? N_("LSDV $R^2$") : N_("LSDV R-squared");
3185 	key[K_R22] = (tex)? N_("Within $R^2$") : N_("Within R-squared");
3186     }
3187 
3188     if (pmod->ci == DPANEL) {
3189 	for (i=0; i<MID_STATS; i++) {
3190 	    if (i < K_SSR || i > K_SER) {
3191 		val[i] = NADBL;
3192 	    }
3193 	}
3194     } else if (pmod->ci == IVREG && (pmod->opt & OPT_G)) {
3195 	/* IVREG via GMM */
3196 	for (i=K_SSR; i<MID_STATS; i++) {
3197 	    val[i] = NADBL;
3198 	}
3199     } else if (pmod->aux == AUX_SYS) {
3200 	/* only dep. var. stats, SSR and SE, unless LIML */
3201 	if (liml_equation(pmod) && !gretl_model_get_int(pmod, "restricted")) {
3202 	    for (i=4; i<MID_STATS; i++) {
3203 		if (i < K_LNL || i > K_AKA) {
3204 		    val[i] = NADBL;
3205 		}
3206 	    }
3207 	    key[K_AKA] = N_("Smallest eigenvalue"); /* 22: */
3208 	    val[K_AKA] = gretl_model_get_double(pmod, "lmin");
3209 	} else {
3210 	    for (i=K_FST; i<MID_STATS; i++) {
3211 		val[i] = NADBL;
3212 	    }
3213 	}
3214     } else if (pmod->ci == ARMA) {
3215 	key[K_SSR] = N_("Mean of innovations"); /* 22: Mean of ARMA innovations */
3216 	val[K_SSR] = gretl_model_get_double(pmod, "mean_error");
3217 	key[K_SER] = N_("S.D. of innovations"); /* 22: Std. dev. of ARMA innovations */
3218 #if 0 /* allow printing of R^2 for ARMA models */
3219 	for (i=K_RSQ; i<MID_STATS; i++) {
3220 	    if (i < K_LNL || i > K_HQ) {
3221 		val[i] = NADBL;
3222 	    }
3223 	}
3224 #endif
3225     } else if (pmod->ci == LAD) {
3226 	key[K_YBAR] = N_("Median depend. var");  /* 22: Median of dependent variable */
3227 	val[K_YBAR] = gretl_model_get_double(pmod, "ymedian");
3228 	key[K_SSR] = N_("Sum absolute resid");  /* 22: Sum of absolute residuals */
3229 	val[K_SSR] = gretl_model_get_double(pmod, "ladsum");
3230 	key[K_SER] = N_("Sum squared resid");
3231 	val[K_SER] = pmod->ess;
3232 	for (i=K_RSQ; i<MID_STATS; i++) {
3233 	    if (i < K_LNL || i > K_HQ) {
3234 		val[i] = NADBL;
3235 	    }
3236 	}
3237     } else if (logit_probit_model(pmod)) {
3238 	val[K_SSR] = val[K_SER] = NADBL;
3239 	val[K_FST] = val[K_PVF] = NADBL;
3240 	val[K_RHO] = val[K_DW] = NADBL;
3241     } else if (pmod->ci == BIPROBIT) {
3242 	val[K_SSR] = val[K_SER] = NADBL;
3243 	val[K_FST] = val[K_PVF] = NADBL;
3244 	val[K_RHO] = val[K_DW] = NADBL;
3245     } else if (pmod->ci == HECKIT) {
3246 	key[K_SSR] = (tex)? "$\\hat{\\sigma}$" : N_("sigma");
3247 	val[K_SSR] = pmod->sigma;
3248 	key[K_SER] = (tex)? "$\\hat{\\rho}$" : N_("rho");
3249 	val[K_SER] = pmod->rho;
3250 	for (i=K_RSQ; i<MID_STATS; i++) {
3251 	    /* may add an R-squared (item 4)? */
3252 	    if (i < K_LNL || i > K_HQ) {
3253 		val[i] = NADBL;
3254 	    }
3255 	}
3256     } else if (panel_ML_model(pmod) || pmod->ci == GARCH) {
3257 	for (i=K_SSR; i<MID_STATS; i++) {
3258 	    if (i < K_LNL || i > K_HQ) {
3259 		val[i] = NADBL;
3260 	    }
3261 	}
3262     } else if (pmod->ci == MLE || ordered_model(pmod)) {
3263 	for (i=0; i<MID_STATS; i++) {
3264 	    if (i < K_LNL || i > K_HQ) {
3265 		val[i] = NADBL;
3266 	    }
3267 	}
3268     } else if (pmod->ci != VAR && pmod->aux != AUX_VECM &&
3269 	       !na(pmod->rho) && gretl_model_get_int(pmod, "ldepvar")) {
3270         int err = 0;
3271         double h = durbins_h(pmod, &err);
3272 
3273 	key[K_DW] = (tex)? N_("Durbin's $h$") : N_("Durbin's h");
3274 	val[K_DW] = err ? NADBL : h;
3275     } else if (intreg_model(pmod)) {
3276 	for (i=0; i<MID_STATS; i++) {
3277 	    if (i < K_FST || i > K_HQ) {
3278 		val[i] = NADBL;
3279 	    }
3280 	}
3281     }
3282 
3283     if (code == MIDDLE_TRANSFORM) {
3284 	/* transformed data: don't print mean, s.d. of dep. var. */
3285 	val[K_YBAR] = val[K_SDY] = NADBL;
3286     } else if (code == MIDDLE_ORIG) {
3287 	/* print a limited range of stats */
3288 	val[K_SSR] = gretl_model_get_double(pmod, "ess_orig");
3289 	val[K_SER] = gretl_model_get_double(pmod, "sigma_orig");
3290 	for (i=K_RSQ; i<MID_STATS; i++) {
3291 	    val[i] = NADBL;
3292 	}
3293 	if (pmod->ci == LOGISTIC) {
3294 	    val[K_LNL] = gretl_model_get_double(pmod, "jll");
3295 	    val[K_AKA] = gretl_model_get_double(pmod, "jaic");
3296 	}
3297     }
3298 
3299     /* start the table */
3300     if (tex) {
3301 	pputs(prn, "\\vspace{1ex}\n");
3302 	if (mtab.multi) {
3303 	    pputs(prn, "\\begin{tabular}{lr}\n");
3304 	} else {
3305 	    pputs(prn, "\\begin{tabular}{lrlr}\n");
3306 	}
3307     } else if (rtf) {
3308 	pputs(prn, "\\par\n{");
3309     } else if (csv) {
3310 	mtab.d = prn_delim(prn);
3311     }
3312 
3313     mtab.key = key;
3314     mtab.val = val;
3315     strcpy(mtab.txt_fmt, TXT_MT_FMT);
3316 
3317     if (plain_format(prn) && mtab.nls) {
3318 	set_mtab_string_width(&mtab);
3319     }
3320 
3321     /* print the various statistics */
3322     for (i=0, j=0; i<7; i++, j+=2) {
3323 	if (!na(val[j])) {
3324 	    middle_table_row(&mtab, j, prn);
3325 	}
3326     }
3327 
3328     if (tex) {
3329 	pputs(prn, "\\end{tabular}\n\n");
3330     } else if (rtf) {
3331 	pputs(prn, "}\n\n"); /* close RTF table */
3332     }
3333 
3334     if (plain_format(prn) && strcmp(note, _(note)) &&
3335 	!string_is_blank(_(note))) {
3336 	pputs(prn, _(note));
3337 	pputc(prn, '\n');
3338     }
3339 
3340     if (!tex && !rtf) {
3341 	pputc(prn, '\n');
3342     }
3343 
3344     g_free(teststr);
3345 }
3346 
print_model_iter_info(const MODEL * pmod,PRN * prn)3347 static void print_model_iter_info (const MODEL *pmod, PRN *prn)
3348 {
3349     int iters = gretl_model_get_int(pmod, "iters");
3350 
3351     if (iters > 0) {
3352 	/* pputc(prn, '\n'); */
3353 	pprintf(prn, _("Convergence achieved after %d iterations\n"), iters);
3354     } else {
3355 	int fncount = gretl_model_get_int(pmod, "fncount");
3356 	int grcount = gretl_model_get_int(pmod, "grcount");
3357 
3358 	if (fncount > 0) {
3359 	    pputc(prn, '\n');
3360 	    pprintf(prn, _("Function evaluations: %d\n"), fncount);
3361 	    pprintf(prn, _("Evaluations of gradient: %d\n"), grcount);
3362 	}
3363     }
3364 }
3365 
aux_print_info_criteria(const MODEL * pmod,PRN * prn)3366 static void aux_print_info_criteria (const MODEL *pmod, PRN *prn)
3367 {
3368     const char *istrs[] = {
3369 	N_("AIC"), N_("BIC"), N_("HQC")
3370     };
3371     int i, n = 0;
3372 
3373     for (i=0; i<C_MAX; i++) {
3374 	if (!na(pmod->criterion[i])) {
3375 	    if (n > 0) {
3376 		pputc(prn, ' ');
3377 	    }
3378 	    pprintf(prn, "  %s: %g", _(istrs[i]), pmod->criterion[i]);
3379 	    n++;
3380 	}
3381     }
3382 
3383     if (n > 0) {
3384 	if (gretl_model_get_int(pmod, "eg-resids")) {
3385 	    pputc(prn, '\n');
3386 	} else {
3387 	    pputs(prn, "\n\n");
3388 	}
3389     }
3390 }
3391 
set_csv_delim(PRN * prn)3392 static void set_csv_delim (PRN *prn)
3393 {
3394     char test[4];
3395 
3396     sprintf(test, "%.1f", 1.0);
3397 
3398     if (test[1] == ',') {
3399 	gretl_print_set_delim(prn, '\t');
3400     } else {
3401 	gretl_print_set_delim(prn, ',');
3402     }
3403 }
3404 
3405 /**
3406  * printmodel:
3407  * @pmod: pointer to gretl model.
3408  * @dset: data information struct.
3409  * @opt: may contain %OPT_O to print covariance matrix, %OPT_S
3410  * to get a "simple" print (just coefficients and standard
3411  * errors), %OPT_P for printing model in the context of panel
3412  * diagnostics only.
3413  * @prn: gretl printing struct.
3414  *
3415  * Print to @prn the estimates in @pmod plus associated statistics.
3416  *
3417  * Returns: 0 on success, 1 if some of the values to print were %NaN.
3418  */
3419 
printmodel(MODEL * pmod,const DATASET * dset,gretlopt opt,PRN * prn)3420 int printmodel (MODEL *pmod, const DATASET *dset, gretlopt opt,
3421 		PRN *prn)
3422 {
3423     int gotnan = 0;
3424 
3425     if (prn == NULL || (opt & OPT_Q)) {
3426 	return 0;
3427     }
3428 
3429     if (csv_format(prn)) {
3430 	set_csv_delim(prn);
3431     }
3432 
3433     if (plain_format(prn)) {
3434 	print_model_iter_info(pmod, prn);
3435     }
3436 
3437     if (gretl_is_between_model(pmod) && pmod->dataset != NULL) {
3438 	dset = pmod->dataset;
3439     }
3440 
3441     model_format_start(prn);
3442 
3443     if (!(opt & OPT_P)) {
3444 	print_model_heading(pmod, dset, opt, prn);
3445     }
3446 
3447     if (plain_format(prn)) {
3448 	gotnan = plain_print_coefficients(pmod, dset, prn);
3449     } else {
3450 	gotnan = alt_print_coefficients(pmod, dset, prn);
3451     }
3452 
3453     print_coeff_table_end(pmod, prn);
3454 
3455     if (pmod->aux == AUX_DF || pmod->aux == AUX_ADF ||
3456 	pmod->aux == AUX_KPSS) {
3457 	if (!gretl_model_get_int(pmod, "dfgls")) {
3458 	    aux_print_info_criteria(pmod, prn);
3459 	}
3460 	goto close_format;
3461     }
3462 
3463     if (pmod->aux == AUX_ARCH || pmod->aux == AUX_RESET) {
3464 	goto close_format;
3465     } else if (opt & OPT_P) {
3466 	goto close_format;
3467     }
3468 
3469     /* other auxiliary regressions */
3470 
3471     if (pmod->aux == AUX_HET_1) {
3472 	rsqline(pmod, prn);
3473 	print_HET_1_results(pmod, prn);
3474 	goto close_format;
3475     } else if (pmod->aux == AUX_WHITE) {
3476 	rsqline(pmod, prn);
3477 	print_whites_results(pmod, prn);
3478 	goto close_format;
3479     } else if (pmod->aux == AUX_BP) {
3480 	rssline(pmod, prn);
3481 	print_bp_results(pmod, prn);
3482 	goto close_format;
3483     } else if (pmod->aux == AUX_SQ ||
3484 	       pmod->aux == AUX_LOG ||
3485 	       pmod->aux == AUX_AR ||
3486 	       pmod->aux == AUX_ADD) {
3487 	rsqline(pmod, prn);
3488 	goto close_format;
3489     } else if (pmod->aux == AUX_COMFAC) {
3490 	ssrline(pmod, prn);
3491 	goto close_format;
3492     }
3493 
3494     if (pmod->ci == OLS && (opt & OPT_S)) {
3495 	/* --simple-print */
3496 	if (pmod->ci == OLS && !na(pmod->rsq) && plain_format(prn)) {
3497 	    int uc = gretl_model_get_int(pmod, "uncentered");
3498 
3499 	    pprintf(prn, "%s = %g, %s = %f\n\n", _("SSR"), pmod->ess,
3500 		    uc ? _("Uncentered R-squared") : _("R-squared"),
3501 		    pmod->rsq);
3502 	}
3503 	goto close_format;
3504     }
3505 
3506     if (weighted_model(pmod)) {
3507 	alternate_stats_message(WTD_STATS, prn);
3508 	print_middle_table(pmod, prn, MIDDLE_TRANSFORM);
3509 	alternate_stats_message(ORIG_STATS, prn);
3510 	print_middle_table(pmod, prn, MIDDLE_ORIG);
3511 	goto pval_max;
3512     }
3513 
3514     if (pmod->ci == LOGISTIC) {
3515 	alternate_stats_message(TRANSFORM_STATS, prn);
3516 	print_middle_table(pmod, prn, MIDDLE_TRANSFORM);
3517 	alternate_stats_message(ORIG_STATS, prn);
3518 	print_middle_table(pmod, prn, MIDDLE_ORIG);
3519 	goto pval_max;
3520     }
3521 
3522     if (pmod->ci == AR || pmod->ci == AR1) {
3523 	alternate_stats_message(RHODIFF_STATS, prn);
3524 	print_middle_table(pmod, prn, MIDDLE_TRANSFORM);
3525 	alternate_stats_message(ORIG_STATS, prn);
3526 	print_middle_table(pmod, prn, MIDDLE_ORIG);
3527 	goto pval_max;
3528     }
3529 
3530     /* print table of model stats */
3531     if (pmod->ci != GMM) {
3532 	print_middle_table(pmod, prn, MIDDLE_REGULAR);
3533     }
3534 
3535     /* random effects probit */
3536     if (re_probit_model(pmod)) {
3537 	print_probit_rho(pmod, prn);
3538     }
3539 
3540     /* additional stats/info for some cases */
3541     if ((pmod->aux == AUX_SYS && liml_equation(pmod)) ||
3542 	liml_model(pmod)) {
3543 	print_liml_equation_data(pmod, prn);
3544     } else if (pmod->ci == ARMA) {
3545 	print_arma_roots(pmod, prn);
3546     } else if (pmod->ci == GARCH) {
3547 	garch_variance_line(pmod, prn);
3548     } else if (pmod->ci == HECKIT) {
3549 	print_heckit_stats(pmod, prn);
3550     } else if (random_effects_model(pmod)) {
3551 	panel_variance_lines(pmod, prn);
3552     } else if (gmm_model(pmod)) {
3553 	print_GMM_stats(pmod, prn);
3554     } else if (pmod->ci == DPANEL) {
3555 	print_DPD_stats(pmod, prn);
3556     } else if (logit_probit_model(pmod)) {
3557 	if (!pmod->aux) {
3558 	    logit_probit_stats(pmod, prn);
3559 	}
3560     } else if (tsls_model(pmod) && plain_format(prn)) {
3561 	addconst_message(pmod, prn);
3562     } else if (intreg_model(pmod)) {
3563 	print_intreg_info(pmod, dset, prn);
3564     } else if (pmod->ci == POISSON) {
3565 	print_overdisp_test(pmod, prn);
3566     } else if (pmod->ci == DURATION) {
3567 	print_duration_alpha(pmod, prn);
3568     } else if (pmod->ci == NLS ||
3569 	       (pmod->ci == MIDASREG &&
3570 		!gretl_model_get_int(pmod, "umidas"))) {
3571 	print_GNR_info(pmod, prn);
3572     }
3573 
3574     /* FIXME alternate R^2 measures (within, centered) */
3575 
3576     if (plain_format(prn) && !pmod->aux) {
3577 	maybe_print_jll(pmod, 0, prn);
3578     }
3579 
3580  pval_max:
3581 
3582     if (plain_format(prn) && pmod->ci != MLE && pmod->ci != PANEL &&
3583 	pmod->ci != ARMA && pmod->ci != NLS && pmod->ci != GMM &&
3584 	pmod->ci != LAD && pmod->ci != HECKIT && pmod->ci != MIDASREG &&
3585 	pmod->ci != DPANEL && pmod->ci != GARCH && pmod->ci != DURATION &&
3586 	!ordered_model(pmod) && !multinomial_model(pmod) &&
3587 	!COUNT_MODEL(pmod->ci) && !intreg_model(pmod) &&
3588 	pmod->ci != BIPROBIT && !pmod->aux) {
3589 	pval_max_line(pmod, dset, prn);
3590     }
3591 
3592  close_format:
3593 
3594     if (opt & OPT_V) {
3595 	ols_print_anova(pmod, prn);
3596     }
3597 
3598     if (opt & OPT_O) {
3599 	outcovmx(pmod, dset, prn);
3600     }
3601 
3602     if (any_tests(pmod) && !(opt & OPT_S)) {
3603 	print_model_tests(pmod, prn);
3604     }
3605 
3606     model_format_end(prn);
3607 
3608     if (gotnan) {
3609 	pmod->errcode = E_NAN;
3610     }
3611 
3612     return gotnan;
3613 }
3614 
print_pval_str(double pval,char * str)3615 static void print_pval_str (double pval, char *str)
3616 {
3617     if (pval < .0001) {
3618 	sprintf(str, "<%.4f", 0.0001);
3619     } else {
3620 	sprintf(str, "%.4f", pval);
3621     }
3622 }
3623 
3624 /* used only for "alternate" model-output formats */
3625 
3626 static int
prepare_model_coeff(const MODEL * pmod,const DATASET * dset,int i,int adfnum,model_coeff * mc,PRN * prn)3627 prepare_model_coeff (const MODEL *pmod, const DATASET *dset,
3628 		     int i, int adfnum, model_coeff *mc, PRN *prn)
3629 {
3630     int gotnan = 0;
3631 
3632     model_coeff_init(mc);
3633 
3634     mc->show_pval = !binary_model(pmod) || (pmod->opt & OPT_P);
3635 
3636     if (tex_format(prn)) {
3637 	make_tex_coeff_name(pmod, dset, i, mc->name);
3638     } else {
3639 	gretl_model_get_param_name(pmod, dset, i, mc->name);
3640     }
3641 
3642     if (na(pmod->coeff[i])) {
3643 	gotnan = 1;
3644     } else {
3645 	mc->b = pmod->coeff[i];
3646     }
3647 
3648     if (!na(pmod->sderr[i])) {
3649 	mc->se = pmod->sderr[i];
3650     }
3651 
3652     if (pmod->ci == DURATION && i > 0 && !strcmp(mc->name, "sigma")) {
3653 	mc->tval = NADBL;
3654 	mc->pval = NADBL;
3655 	mc->show_tval = 0;
3656 	mc->show_pval = 0;
3657 	return gotnan;
3658     }
3659 
3660     if (!na(mc->b) && !na(mc->se) && mc->se > 0.0) {
3661 	mc->tval = mc->b / mc->se;
3662 	if (na(mc->tval)) {
3663 	    mc->tval = NADBL;
3664 	}
3665     }
3666 
3667     if (mc->show_pval && !na(mc->tval)) {
3668 	if (i == adfnum) {
3669 	    mc->pval = gretl_model_get_double(pmod, "dfpval");
3670 	    mc->df_pval = 1;
3671 	} else {
3672 	    mc->pval = model_coeff_pval(pmod, mc->tval);
3673 	}
3674     }
3675 
3676     if (!gotnan && !mc->show_pval &&
3677 	binary_model(pmod) &&
3678 	pmod->list[i+2] != 0) {
3679 	double *slopes = gretl_model_get_data(pmod, "slopes");
3680 
3681 	if (slopes != NULL) {
3682 	    mc->slope = slopes[i];
3683 	}
3684     }
3685 
3686     if (pmod->ci == MPOLS) {
3687 	mc->multi = 1;
3688     }
3689 
3690     return gotnan;
3691 }
3692 
rtf_print_double(double xx,PRN * prn)3693 static void rtf_print_double (double xx, PRN *prn)
3694 {
3695     char numstr[32];
3696 
3697     xx = screen_zero(xx);
3698 
3699     if (xx < 0 && gretl_print_has_minus(prn)) {
3700 	sprintf(numstr, "%#.*g", get_gretl_digits(), fabs(xx));
3701 	gretl_fix_exponent(numstr);
3702 	pprintf(prn, " \\qc −%s\\cell", numstr);
3703     } else {
3704 	sprintf(numstr, "%#.*g", get_gretl_digits(), xx);
3705 	gretl_fix_exponent(numstr);
3706 	pprintf(prn, " \\qc %s\\cell", numstr);
3707     }
3708 }
3709 
rtf_print_coeff(const model_coeff * mc,PRN * prn)3710 static void rtf_print_coeff (const model_coeff *mc, PRN *prn)
3711 {
3712     if (!na(mc->lo)) {
3713 	pputs(prn, RTF_INTVL_ROW);
3714     } else {
3715 	pputs(prn, RTF_COEFF_ROW);
3716     }
3717 
3718     pprintf(prn, "\\ql %s\\cell", mc->name);
3719 
3720     if (na(mc->b)) {
3721 	pprintf(prn, " \\qc %s\\cell", _("undefined"));
3722     } else {
3723 	rtf_print_double(mc->b, prn);
3724     }
3725 
3726     if (!na(mc->lo) && !na(mc->hi)) {
3727 	rtf_print_double(mc->lo, prn);
3728 	rtf_print_double(mc->hi, prn);
3729 	goto rtf_finish;
3730     }
3731 
3732     if (na(mc->se)) {
3733 	pprintf(prn, " \\qc %s\\cell", _("undefined"));
3734 	pprintf(prn, " \\qc %s\\cell", _("undefined"));
3735 	pprintf(prn, " \\qc %s\\cell", _("undefined"));
3736 	goto rtf_finish;
3737     }
3738 
3739     rtf_print_double(mc->se, prn);
3740 
3741     if (!na(mc->tval)) {
3742 	if (mc->tval < 0 && gretl_print_has_minus(prn)) {
3743 	    pprintf(prn, " \\qc −%#.4g\\cell", -mc->tval);
3744 	} else {
3745 	    pprintf(prn, " \\qc %#.4g\\cell", mc->tval);
3746 	}
3747     } else if (!mc->show_tval) {
3748 	pputs(prn, " \\qc \\cell");
3749     } else {
3750 	pprintf(prn, " \\qc %s\\cell", _("undefined"));
3751     }
3752 
3753     if (!na(mc->slope)) {
3754 	rtf_print_double(mc->slope, prn);
3755     } else if (mc->show_pval) {
3756 	if (na(mc->pval)) {
3757 	    if (mc->df_pval) {
3758 		pprintf(prn, " \\qc %s\\cell", _("unknown"));
3759 	    } else {
3760 		pprintf(prn, " \\qc %s\\cell", _("undefined"));
3761 	    }
3762 	} else {
3763 	    char pvalstr[16];
3764 
3765 	    print_pval_str(mc->pval, pvalstr);
3766 	    pprintf(prn, " \\qc %s\\cell", pvalstr);
3767 
3768 	    if (mc->pval < 0.01) {
3769 		pputs(prn, " \\ql ***\\cell");
3770 	    } else if (mc->pval < 0.05) {
3771 		pputs(prn, " \\ql **\\cell");
3772 	    } else if (mc->pval < 0.10) {
3773 		pputs(prn, " \\ql *\\cell");
3774 	    } else {
3775 		pputs(prn, " \\ql \\cell");
3776 	    }
3777 	}
3778     } else {
3779 	pputs(prn, " \\qc \\cell \\ql \\cell");
3780     }
3781 
3782  rtf_finish:
3783 
3784     pputs(prn, " \\intbl \\row\n");
3785 }
3786 
csv_print_coeff(const model_coeff * mc,PRN * prn)3787 static void csv_print_coeff (const model_coeff *mc, PRN *prn)
3788 {
3789     char d = prn_delim(prn);
3790 
3791     pprintf(prn, "\"%s\"", mc->name);
3792 
3793     if (na(mc->b)) {
3794 	pprintf(prn, "%c\"%s\"", d, _("undefined"));
3795     } else {
3796 	pprintf(prn, "%c%.15g", d, mc->b);
3797     }
3798 
3799     if (!na(mc->lo) && !na(mc->hi)) {
3800 	pprintf(prn, "%c%.15g", d, mc->lo);
3801 	pprintf(prn, "%c%.15g\n", d, mc->hi);
3802 	return;
3803     }
3804 
3805     /* get out if std error is undefined */
3806     if (na(mc->se)) {
3807 	pprintf(prn, "%c\"%s\"\n", d, _("undefined"));
3808 	return;
3809     }
3810 
3811     pprintf(prn, "%c%.15g", d, mc->se);
3812 
3813     if (!na(mc->tval)) {
3814 	pprintf(prn, "%c%.15g", d, mc->tval);
3815     } else if (!mc->show_tval) {
3816 	pputs(prn, "%c\n");
3817     } else {
3818 	pprintf(prn, "%c\"%s\"\n", d, _("undefined"));
3819     }
3820 
3821     if (!na(mc->slope)) {
3822 	/* slope for binary models */
3823 	pprintf(prn, "%c%.15g", d, mc->slope);
3824     } else if (mc->show_pval) {
3825 	if (na(mc->pval)) {
3826 	    if (mc->df_pval) {
3827 		pprintf(prn, "%c\"%s\"\n", d, _("unknown"));
3828 	    } else {
3829 		pprintf(prn, "%c\"%s\"\n", d, _("undefined"));
3830 	    }
3831 	} else {
3832 	    pprintf(prn, "%c%.15g", d, mc->pval);
3833 	}
3834     }
3835 
3836     pputc(prn, '\n');
3837 }
3838 
alt_print_coeff(const model_coeff * mc,PRN * prn)3839 static void alt_print_coeff (const model_coeff *mc, PRN *prn)
3840 {
3841     if (rtf_format(prn)) {
3842 	rtf_print_coeff(mc, prn);
3843     } else if (tex_format(prn)) {
3844 	tex_print_coeff(mc, prn);
3845     } else if (csv_format(prn)) {
3846 	csv_print_coeff(mc, prn);
3847     }
3848 }
3849 
tex_handle_underscores(const char * s,int n)3850 static char *tex_handle_underscores (const char *s,
3851 				     int n)
3852 {
3853     char *ret = malloc(strlen(s) + 1 + n);
3854     int i, j = 0;
3855 
3856     if (ret != NULL) {
3857 
3858 	for (i=0; s[i] != '\0'; i++) {
3859 	    if (s[i] == '_' && (i == 0 || s[i-1] != '\\')) {
3860 		ret[j++] = '\\';
3861 		ret[j++] = '_';
3862 	    } else {
3863 		ret[j++] = s[i];
3864 	    }
3865 	}
3866 
3867 	ret[j] = '\0';
3868     }
3869 
3870     return ret;
3871 }
3872 
has_raw_underscores(const char * s)3873 static int has_raw_underscores (const char *s)
3874 {
3875     int i, n = 0;
3876 
3877     for (i=0; s[i] != '\0'; i++) {
3878 	if (s[i] == '_' && (i == 0 || s[i-1] != '\\')) {
3879 	    n++;
3880 	}
3881     }
3882 
3883     return n;
3884 }
3885 
print_coeff_separator(const char * s,int width,PRN * prn)3886 static void print_coeff_separator (const char *s, int width,
3887 				   PRN *prn)
3888 {
3889     int havestr = (s != NULL && *s != '\0');
3890     int vspace = (width > 0);
3891 
3892     if (plain_format(prn)) {
3893 	if (havestr) {
3894 	    if (vspace) {
3895 		pputs(prn, "\n  ");
3896 	    } else {
3897 		pputs(prn, "  ");
3898 	    }
3899 	    if (width > 0) {
3900 		print_centered(_(s), width, prn);
3901 	    } else {
3902 		pputs(prn, _(s));
3903 	    }
3904 	    pputc(prn, '\n');
3905 	}
3906 	if (vspace) {
3907 	    pputc(prn, '\n');
3908 	}
3909     } else if (tex_format(prn)) {
3910 	if (havestr) {
3911 	    int n = has_raw_underscores(s);
3912 	    char *repl = NULL;
3913 
3914 	    if (n > 0) {
3915 		repl = tex_handle_underscores(s, n);
3916 	    }
3917 	    pputs(prn, "\\\\ [-8pt]\n");
3918 	    pprintf(prn, "\\multicolumn{%d}{c}{%s} \\\\[1ex]\n", width,
3919 		    repl != NULL ? _(repl) : _(s));
3920 	    free(repl);
3921 	} else {
3922 	    pputs(prn, "\\\\ \n");
3923 	}
3924     } else if (rtf_format(prn)) {
3925 	pputs(prn, RTF_MULTICOL);
3926 	if (havestr) {
3927 	    pprintf(prn, "\\qc %s", _(s));
3928 	}
3929 	pputs(prn, "\\cell\\intbl\\row\n");
3930     } else if (csv_format(prn)) {
3931 	if (havestr) {
3932 	    pprintf(prn, "\n\"%s\"\n", _(s));
3933 	} else {
3934 	    pputc(prn, '\n');
3935 	}
3936     }
3937 }
3938 
print_coeff_left_string(const char * s,PRN * prn)3939 static void print_coeff_left_string (const char *s, PRN *prn)
3940 {
3941     if (plain_format(prn)) {
3942 	pprintf(prn, " %s:\n", s);
3943     } else if (tex_format(prn)) {
3944 	char tmp[48];
3945 
3946 	tex_escape(tmp, s);
3947 	pputs(prn, "\\\\ [-8pt]\n");
3948 
3949 	pprintf(prn, "%s \\\\[1ex]\n", tmp);
3950     } else if (rtf_format(prn)) {
3951 	pputs(prn, RTF_MULTICOL);
3952 	pprintf(prn, "\\ql %s", s);
3953 	pputs(prn, "\\cell\\intbl\\row\n");
3954     } else if (csv_format(prn)) {
3955 	pprintf(prn, "\n\"%s\"\n", s);
3956     }
3957 }
3958 
3959 static int
print_rq_sequence(const MODEL * pmod,const DATASET * dset,PRN * prn)3960 print_rq_sequence (const MODEL *pmod, const DATASET *dset, PRN *prn)
3961 {
3962     gretl_vector *tauvec = gretl_model_get_data(pmod, "rq_tauvec");
3963     gretl_matrix *B = gretl_model_get_data(pmod, "rq_sequence");
3964     double tau, bi, se = NADBL;
3965     double blo = 0, bhi = 0;
3966     char test[16];
3967     int n, bcols, taulen = 5;
3968     int namelen = 8;
3969     int offset;
3970     int ntau, i, j, k = 0;
3971     const char *headings[] = {
3972 	N_("tau"),
3973 	N_("coefficient"),
3974 	N_("lower"),
3975 	N_("upper"),
3976 	N_("std. error"),
3977 	N_("t-ratio")
3978     };
3979     const char *head;
3980 
3981     if (tauvec == NULL || B == NULL) {
3982 	return E_DATA;
3983     }
3984 
3985     ntau = gretl_vector_get_length(tauvec);
3986     bcols = gretl_matrix_cols(B);
3987 
3988     for (i=2; i<=pmod->list[0]; i++) {
3989 	n = char_len(dset->varname[pmod->list[i]]);
3990 	if (n > namelen) {
3991 	    namelen = n;
3992 	}
3993     }
3994 
3995     for (j=0; j<ntau; j++) {
3996 	sprintf(test, "%g", gretl_vector_get(tauvec, j));
3997 	n = strlen(test);
3998 	if (n > taulen) {
3999 	    taulen = n;
4000 	}
4001     }
4002 
4003     /* headings row */
4004 
4005     bufspace(namelen + 4, prn);
4006     pprintf(prn, " %-*s", taulen, _(headings[0]));
4007     for (i=1; i<4; i++) {
4008 	j = (i == 1)? 1 : (bcols == 3)? i : i + 2;
4009 	head = _(headings[j]);
4010 	n = char_len(head);
4011 	offset = (i == 1)? 13 - n : 12 - n;
4012 	bufspace(offset, prn);
4013 	pputs(prn, head);
4014 	pputc(prn, ' ');
4015     }
4016     pputc(prn, '\n');
4017 
4018     /* separator row */
4019     pputs(prn, "  ");
4020     n = namelen + 2 + taulen + 1 + 13 * 3;
4021     for (i=0; i<n; i++) {
4022 	pputc(prn, '-');
4023     }
4024     pputc(prn, '\n');
4025 
4026     for (i=0; i<pmod->ncoeff; i++) {
4027 	pprintf(prn, "  %-*s  ", namelen, dset->varname[pmod->list[i+2]]);
4028 	for (j=0; j<ntau; j++) {
4029 	    tau = gretl_vector_get(tauvec, j);
4030 	    bi  = gretl_matrix_get(B, k, 0);
4031 	    if (bcols == 3) {
4032 		blo = gretl_matrix_get(B, k, 1);
4033 		bhi = gretl_matrix_get(B, k, 2);
4034 	    } else {
4035 		se = gretl_matrix_get(B, k, 1);
4036 	    }
4037 	    if (j > 0) {
4038 		bufspace(namelen + 4, prn);
4039 	    }
4040 	    if (bcols == 3) {
4041 		pprintf(prn, "%.*f  %#12.6g %#12.6g %#12.6g\n", taulen - 2,
4042 			tau, bi, blo, bhi);
4043 	    } else {
4044 		pprintf(prn, "%.*f  %#12.6g %#12.6g %#12.6g\n", taulen - 2,
4045 			tau, bi, se, bi / se);
4046 	    }
4047 	    k++;
4048 	}
4049 	if (i < pmod->ncoeff - 1) {
4050 	    pputc(prn, '\n');
4051 	}
4052     }
4053 
4054     return 0;
4055 }
4056 
4057 #if 0 /* not ready */
4058 
4059 static int
4060 alt_print_rq_sequence (const MODEL *pmod, const DATASET *dset, PRN *prn)
4061 {
4062     gretl_vector *tauvec = gretl_model_get_data(pmod, "rq_tauvec");
4063     gretl_matrix *B = gretl_model_get_data(pmod, "rq_sequence");
4064     double tau, bi, se = NADBL;
4065     double blo = 0, bhi = 0;
4066     char test[16];
4067     int n, bcols;
4068     int offset;
4069     int ntau, i, j, k = 0;
4070 
4071     if (tauvec == NULL || B == NULL) {
4072 	return E_DATA;
4073     }
4074 
4075     ntau = gretl_vector_get_length(tauvec);
4076     bcols = gretl_matrix_cols(B);
4077 
4078 
4079     for (i=0; i<pmod->ncoeff; i++) {
4080 	pprintf(prn, "  %-*s  ", namelen, dset->varname[pmod->list[i+2]]);
4081 	for (j=0; j<ntau; j++) {
4082 	    tau = gretl_vector_get(tauvec, j);
4083 	    bi  = gretl_matrix_get(B, k, 0);
4084 	    if (bcols == 3) {
4085 		blo = gretl_matrix_get(B, k, 1);
4086 		bhi = gretl_matrix_get(B, k, 2);
4087 	    } else {
4088 		se = gretl_matrix_get(B, k, 1);
4089 	    }
4090 	    if (bcols == 3) {
4091 		pprintf(prn, "%.*f  %#12.6g %#12.6g %#12.6g\n", taulen - 2,
4092 			tau, bi, blo, bhi);
4093 	    } else {
4094 		pprintf(prn, "%.*f  %#12.6g %#12.6g %#12.6g\n", taulen - 2,
4095 			tau, bi, se, bi / se);
4096 	    }
4097 	    k++;
4098 	}
4099 	if (i < pmod->ncoeff - 1) {
4100 	    pputc(prn, '\n');
4101 	}
4102     }
4103 
4104     return 0;
4105 }
4106 
4107 #endif
4108 
4109 struct printval {
4110     char s[36];
4111     int lw, rw;
4112     double x;
4113 };
4114 
get_number_dims(struct printval * v,int * lmax,int * rmax)4115 static void get_number_dims (struct printval *v, int *lmax, int *rmax)
4116 {
4117     char tmp[36];
4118     const char *s = v->s;
4119     char *p;
4120     int i, n;
4121 
4122     while (*s == ' ') s++;
4123     n = strlen(s);
4124     for (i=n-1; i>0 && s[i] == ' '; i--) {
4125 	n--;
4126     }
4127 
4128     *tmp = '\0';
4129     strncat(tmp, s, n);
4130 
4131     p = strchr(tmp, '.');
4132     if (p == NULL) {
4133 	p = strchr(tmp, ',');
4134     }
4135 
4136     if (p == NULL) {
4137 	v->lw = char_len(tmp);
4138 	v->rw = 0;
4139     } else {
4140 	n = char_len(tmp);
4141 	*p = '\0';
4142 	v->lw = char_len(tmp);
4143 	v->rw = n - v->lw;
4144     }
4145 
4146     if (v->lw > *lmax) *lmax = v->lw;
4147     if (v->rw > *rmax) *rmax = v->rw;
4148 }
4149 
print_padded_head(const char * s,int w,PRN * prn)4150 static void print_padded_head (const char *s, int w, PRN *prn)
4151 {
4152     int hlen = char_len(s);
4153     int offset = (w - hlen) / 2;
4154     int pad = w - hlen - offset;
4155 
4156     bufspace(offset, prn);
4157     pputs(prn, s);
4158     bufspace(pad, prn);
4159 }
4160 
print_padded_value(struct printval * val,int w,int lmax,int addoff,PRN * prn)4161 static void print_padded_value (struct printval *val, int w,
4162 				int lmax, int addoff, PRN *prn)
4163 {
4164     int offset = lmax - val->lw + addoff;
4165     int pad = w - (lmax + val->rw + addoff);
4166 
4167     bufspace(offset, prn);
4168     pputs(prn, val->s);
4169     bufspace(pad, prn);
4170 }
4171 
allocate_printvals(int n,int m)4172 static struct printval **allocate_printvals (int n, int m)
4173 {
4174     struct printval **vals;
4175     int i, j;
4176 
4177     vals = malloc(n * sizeof *vals);
4178 
4179     for (i=0; i<n && vals != NULL; i++) {
4180 	vals[i] = malloc(m * sizeof **vals);
4181 	if (vals[i] == NULL) {
4182 	    for (j=0; j<i; j++) {
4183 		free(vals[j]);
4184 	    }
4185 	    free(vals);
4186 	    vals = NULL;
4187 	}
4188     }
4189 
4190     return vals;
4191 }
4192 
get_ar_data(const MODEL * pmod,double ** pb,double ** pse,int * pk)4193 static int get_ar_data (const MODEL *pmod, double **pb,
4194 			double **pse, int *pk)
4195 {
4196     double *b, *se;
4197     int i, k, err = 0;
4198 
4199     if (pmod->arinfo == NULL ||
4200 	pmod->arinfo->arlist == NULL ||
4201 	pmod->arinfo->rho == NULL ||
4202 	pmod->arinfo->sderr == NULL) {
4203 	return E_DATA;
4204     }
4205 
4206     k = pmod->arinfo->arlist[0];
4207 
4208     b = malloc((pmod->ncoeff + k) * sizeof *b);
4209     se = malloc((pmod->ncoeff + k) * sizeof *se);
4210 
4211     if (b == NULL || se == NULL) {
4212 	free(b);
4213 	free(se);
4214 	return E_ALLOC;
4215     }
4216 
4217     for (i=0; i<pmod->ncoeff; i++) {
4218 	b[i] = pmod->coeff[i];
4219 	se[i] = pmod->sderr[i];
4220     }
4221 
4222     for (i=0; i<k; i++) {
4223 	b[pmod->ncoeff + i] = pmod->arinfo->rho[i];
4224 	se[pmod->ncoeff + i] = pmod->arinfo->sderr[i];
4225     }
4226 
4227     *pb = b;
4228     *pse = se;
4229     *pk = k;
4230 
4231     return err;
4232 }
4233 
get_arch_data(const MODEL * pmod,double ** pb,double ** pse,int * pk)4234 static int get_arch_data (const MODEL *pmod, double **pb,
4235 			  double **pse, int *pk)
4236 {
4237     double *ab = gretl_model_get_data(pmod, "arch_coeff");
4238     double *ase = gretl_model_get_data(pmod, "arch_sderr");
4239     int order = gretl_model_get_int(pmod, "arch_order");
4240     double *b, *se;
4241     int err = 0;
4242 
4243     if (ab != NULL && ase != NULL && order > 0) {
4244 	int i, k = order + 1;
4245 
4246 	b = malloc((pmod->ncoeff + k) * sizeof *b);
4247 	se = malloc((pmod->ncoeff + k) * sizeof *se);
4248 
4249 	if (b == NULL || se == NULL) {
4250 	    free(b);
4251 	    free(se);
4252 	    return E_ALLOC;
4253 	}
4254 
4255 	for (i=0; i<pmod->ncoeff; i++) {
4256 	    b[i] = pmod->coeff[i];
4257 	    se[i] = pmod->sderr[i];
4258 	}
4259 
4260 	for (i=0; i<k; i++) {
4261 	    b[pmod->ncoeff + i] = ab[i];
4262 	    se[pmod->ncoeff + i] = ase[i];
4263 	}
4264 
4265 	*pb = b;
4266 	*pse = se;
4267 	*pk = k;
4268     } else {
4269 	err = E_DATA;
4270     }
4271 
4272     return err;
4273 }
4274 
figure_colsep(int namelen,int ncols,int * w,int * colsep)4275 static void figure_colsep (int namelen, int ncols, int *w,
4276 			   int *colsep)
4277 {
4278     int j, n = namelen + ncols * *colsep;
4279 
4280     for (j=0; j<ncols; j++) {
4281 	n += w[j];
4282     }
4283     n = 68 - (n + 2);
4284     if (n > ncols) {
4285 	*colsep += 1;
4286     }
4287 }
4288 
print_sep_row(int namelen,int ncols,int * w,int colsep,PRN * prn)4289 static int print_sep_row (int namelen, int ncols, int *w,
4290 			  int colsep, PRN *prn)
4291 {
4292     int j, n;
4293 
4294     pputc(prn, '\n');
4295     bufspace(2, prn);
4296     n = namelen + ncols * colsep;
4297     for (j=0; j<ncols; j++) {
4298 	n += w[j];
4299     }
4300     for (j=0; j<n; j++) {
4301 	pputc(prn, '-');
4302     }
4303     pputc(prn, '\n');
4304 
4305     return n;
4306 }
4307 
put_asts(double pv,PRN * prn)4308 static void put_asts (double pv, PRN *prn)
4309 {
4310     if (pv < 0.01) {
4311 	pputs(prn, " ***");
4312     } else if (pv < 0.05) {
4313 	pputs(prn, " **");
4314     } else if (pv < 0.10) {
4315 	pputs(prn, " *");
4316     }
4317 }
4318 
alt_print_aux_coeffs(const double * b,const double * se,const char ** names,int nc,int df,int ci,PRN * prn)4319 static int alt_print_aux_coeffs (const double *b, const double *se,
4320 				 const char **names, int nc, int df,
4321 				 int ci, PRN *prn)
4322 {
4323     model_coeff mc;
4324     int i;
4325 
4326     for (i=0; i<nc; i++) {
4327 	if (na(b[i])) {
4328 	    return E_NAN;
4329 	}
4330     }
4331 
4332     alt_print_coeff_table_start(NULL, ci, prn);
4333 
4334     model_coeff_init(&mc);
4335 
4336     /* print row values */
4337     for (i=0; i<nc; i++) {
4338 	double sei = isnan(se[i]) ? NADBL : se[i];
4339 
4340 	mc.b = b[i];
4341 	mc.se = sei;
4342 	if (na(sei) || sei <= 0) {
4343 	    mc.tval = mc.pval = NADBL;
4344 	} else {
4345 	    mc.tval = b[i] / sei;
4346 	    mc.pval = coeff_pval(ci, mc.tval, df);
4347 	}
4348 	if (tex_format(prn)) {
4349 	    tex_escape_special(mc.name, names[i]);
4350 	} else {
4351 	    *mc.name = '\0';
4352 	    strncat(mc.name, names[i], MC_NAMELEN - 1);
4353 	}
4354 	alt_print_coeff(&mc, prn);
4355     }
4356 
4357     print_coeff_table_end(NULL, prn);
4358 
4359     return 0;
4360 }
4361 
plain_print_aux_coeffs(const double * b,const double * se,const char ** names,int nc,int df,int ci,PRN * prn)4362 static int plain_print_aux_coeffs (const double *b,
4363 				   const double *se,
4364 				   const char **names,
4365 				   int nc, int df, int ci,
4366 				   PRN *prn)
4367 {
4368     struct printval **vals, *vij;
4369     const char *headings[] = {
4370 	N_("coefficient"),
4371 	N_("std. error"),
4372 	N_("t-ratio"),
4373 	N_("p-value")
4374     };
4375     const char *head;
4376     int lmax[4] = {0};
4377     int rmax[4] = {0};
4378     int w[4], addoff[4] = {0};
4379     int hlen;
4380     double tval, pval;
4381     int namelen = 0;
4382     int colsep = 2;
4383     int n, d, i, j;
4384     int err = 0;
4385 
4386     vals = allocate_printvals(nc, 4);
4387     if (vals == NULL) {
4388 	return E_ALLOC;
4389     }
4390 
4391     if (df == 0 || ASYMPTOTIC_MODEL(ci)) {
4392 	headings[2] = N_("z");
4393     }
4394 
4395     for (i=0; i<nc; i++) {
4396 	double sei = isnan(se[i]) ? NADBL : se[i];
4397 
4398 	if (na(b[i])) {
4399 	    err = E_NAN;
4400 	    goto bailout;
4401 	}
4402 	n = char_len(names[i]);
4403 	if (n > namelen) {
4404 	    namelen = n;
4405 	}
4406 	if (na(sei) || sei <= 0) {
4407 	    tval = pval = NADBL;
4408 	} else {
4409 	    tval = b[i] / sei;
4410 	    pval = coeff_pval(ci, tval, df);
4411 	}
4412 	for (j=0; j<4; j++) {
4413 	    if (j < 2) {
4414 		/* coeff, standard error */
4415 		d = get_gretl_digits();
4416 		vals[i][j].x = (j == 0)? b[i] : sei;
4417 	    } else if (j == 2) {
4418 		/* t-ratio */
4419 		d = 4;
4420 		vals[i][j].x = tval;
4421 	    } else {
4422 		/* p-value */
4423 		d = -4;
4424 		vals[i][j].x = pval;
4425 	    }
4426 	    gretl_sprint_fullwidth_double(vals[i][j].x, d, vals[i][j].s, prn);
4427 	    get_number_dims(&vals[i][j], &lmax[j], &rmax[j]);
4428 	}
4429     }
4430 
4431     if (namelen < 8) {
4432 	namelen = 8;
4433     }
4434 
4435     /* figure appropriate column separation */
4436     for (j=0; j<4; j++) {
4437 	w[j] = lmax[j] + rmax[j];
4438 	head = _(headings[j]);
4439 	hlen = char_len(head);
4440 	if (hlen > w[j]) {
4441 	    addoff[j] = (hlen - w[j]) / 2;
4442 	    w[j] = hlen;
4443 	}
4444     }
4445     figure_colsep(namelen, 4, w, &colsep);
4446 
4447     /* print headings */
4448     bufspace(namelen + 2 + colsep, prn);
4449     for (j=0; j<4; j++) {
4450 	print_padded_head(_(headings[j]), w[j], prn);
4451 	if (j < 3) {
4452 	    bufspace(colsep, prn);
4453 	}
4454     }
4455 
4456     /* separator row */
4457     print_sep_row(namelen, 4, w, colsep, prn);
4458 
4459     /* print row values */
4460     for (i=0; i<nc; i++) {
4461 	pprintf(prn, "  %-*s", namelen, names[i]);
4462 	bufspace(colsep, prn);
4463 	for (j=0; j<4; j++) {
4464 	    vij = &vals[i][j];
4465 	    print_padded_value(vij, w[j], lmax[j], addoff[j], prn);
4466 	    if (j < 3) {
4467 		bufspace(colsep, prn);
4468 	    } else if (!na(vij->x)) {
4469 		put_asts(vij->x, prn);
4470 	    }
4471 	}
4472 	pputc(prn, '\n');
4473     }
4474 
4475  bailout:
4476 
4477     for (i=0; i<nc; i++) {
4478 	free(vals[i]);
4479     }
4480     free(vals);
4481 
4482     return err;
4483 }
4484 
4485 /* Called by external functions that want to print a coefficient
4486    table that does not reside in a MODEL.
4487 */
4488 
print_coeffs(const double * b,const double * se,const char ** names,int nc,int df,int ci,PRN * prn)4489 int print_coeffs (const double *b,
4490 		  const double *se,
4491 		  const char **names,
4492 		  int nc, int df, int ci,
4493 		  PRN *prn)
4494 {
4495     if (plain_format(prn)) {
4496 	return plain_print_aux_coeffs(b, se, names, nc, df, ci, prn);
4497     } else {
4498 	return alt_print_aux_coeffs(b, se, names, nc, df, ci, prn);
4499     }
4500 }
4501 
plain_print_mp_coeffs(const MODEL * pmod,const DATASET * dset,PRN * prn)4502 static int plain_print_mp_coeffs (const MODEL *pmod,
4503 				  const DATASET *dset,
4504 				  PRN *prn)
4505 {
4506     struct printval **vals;
4507     const char *headings[] = {
4508 	N_("coefficient"),
4509 	N_("std. error")
4510     };
4511     const double *b = pmod->coeff;
4512     const double *se = pmod->sderr;
4513     char **names = NULL;
4514     const char *head;
4515     int lmax[2] = {0};
4516     int rmax[2] = {0};
4517     int w[2], addoff[2] = {0};
4518     int hlen;
4519     int n, nc = pmod->ncoeff;
4520     int namelen = 0;
4521     int colsep = 2;
4522     int i, j, minus;
4523     int err = 0;
4524 
4525     vals = allocate_printvals(nc, 2);
4526     if (vals == NULL) {
4527 	return E_ALLOC;
4528     }
4529 
4530     names = strings_array_new_with_length(nc, 32);
4531     if (names == NULL) {
4532 	err = E_ALLOC;
4533 	goto bailout;
4534     }
4535 
4536     minus = (gretl_print_has_minus(prn))? MINUS_UTF : MINUS_HYPHEN;
4537 
4538     for (i=0; i<nc; i++) {
4539 	if (na(b[i])) {
4540 	    err = E_NAN;
4541 	    goto bailout;
4542 	}
4543 	gretl_model_get_param_name(pmod, dset, i, names[i]);
4544 	n = char_len(names[i]);
4545 	if (n > namelen) {
4546 	    namelen = n;
4547 	}
4548 	for (j=0; j<2; j++) {
4549 	    vals[i][j].x = (j==0)? b[i] : se[i];
4550 	    print_fifteen(vals[i][j].s, vals[i][j].x, minus);
4551 	    vals[i][j].lw = lmax[j] = 2;
4552 	    vals[i][j].rw = rmax[j] = 19;
4553 	}
4554     }
4555 
4556     if (namelen < 8) {
4557 	namelen = 8;
4558     }
4559 
4560     /* figure appropriate column separation */
4561 
4562     for (j=0; j<2; j++) {
4563 	w[j] = lmax[j] + rmax[j];
4564 	head = _(headings[j]);
4565 	hlen = char_len(head);
4566 	if (hlen > w[j]) {
4567 	    addoff[j] = (hlen - w[j]) / 2;
4568 	    w[j] = hlen;
4569 	}
4570     }
4571 
4572     figure_colsep(namelen, 2, w, &colsep);
4573 
4574     /* print headings */
4575 
4576     bufspace(namelen + 2 + colsep, prn);
4577     for (j=0; j<2; j++) {
4578 	print_padded_head(_(headings[j]), w[j], prn);
4579 	if (j < 3) {
4580 	    bufspace(colsep, prn);
4581 	}
4582     }
4583 
4584     /* separator row */
4585     print_sep_row(namelen, 2, w, colsep, prn);
4586 
4587     /* print row values */
4588 
4589     for (i=0; i<nc; i++) {
4590 	pprintf(prn, "  %-*s", namelen, names[i]);
4591 	bufspace(colsep, prn);
4592 	for (j=0; j<2; j++) {
4593 	    print_padded_value(&vals[i][j], w[j], lmax[j], addoff[j], prn);
4594 	    if (j == 0) {
4595 		bufspace(colsep, prn);
4596 	    }
4597 	}
4598 	pputc(prn, '\n');
4599     }
4600 
4601  bailout:
4602 
4603     for (i=0; i<nc; i++) {
4604 	free(vals[i]);
4605     }
4606     free(vals);
4607 
4608     strings_array_free(names, nc);
4609 
4610     return err;
4611 }
4612 
4613 static const char *
get_col_heading(const char ** S,int j,int slopes,int intervals)4614 get_col_heading (const char **S, int j, int slopes,
4615 		 int intervals)
4616 {
4617     if (j == 3 && slopes) {
4618 	return _(S[j+1]);
4619     } else if (intervals && (j == 1 || j == 2)) {
4620 	return _(S[j+4]);
4621     } else {
4622 	return _(S[j]);
4623     }
4624 }
4625 
4626 static void
print_count_offset(const MODEL * pmod,const DATASET * dset,struct printval * val,int namelen,int colsep,int w,int lmax,int addoff,PRN * prn)4627 print_count_offset (const MODEL *pmod, const DATASET *dset,
4628 		    struct printval *val, int namelen,
4629 		    int colsep, int w, int lmax,
4630 		    int addoff, PRN *prn)
4631 {
4632     int offvar = gretl_model_get_int(pmod, "offset_var");
4633 
4634     if (offvar > 0) {
4635 	char name[24];
4636 	int n;
4637 
4638 	sprintf(name, "log(%s)", dset->varname[offvar]);
4639 	n = strlen(name);
4640 	pprintf(prn, "\n  %-*s", namelen, name);
4641 	if (n > namelen) {
4642 	    colsep -= n - namelen;
4643 	}
4644 	if (colsep > 0) {
4645 	    bufspace(colsep, prn);
4646 	}
4647 	sprintf(val->s, "%.1f", 1.0);
4648 	val->lw = val->rw = 1;
4649 	print_padded_value(val, w, lmax, addoff, prn);
4650 	pputc(prn, '\n');
4651     }
4652 }
4653 
print_ar_sum(const MODEL * pmod,PRN * prn)4654 static void print_ar_sum (const MODEL *pmod, PRN *prn)
4655 {
4656     if (pmod->arinfo != NULL && pmod->arinfo->arlist[0] > 1) {
4657 	double arsum = 0.0;
4658 	int i;
4659 
4660 	for (i=0; i<pmod->arinfo->arlist[0]; i++) {
4661 	    arsum += pmod->arinfo->rho[i];
4662 	}
4663 	pprintf(prn, "\n%s = %#g\n", _("Sum of AR coefficients"), arsum);
4664     }
4665 }
4666 
mn_logit_coeffsep(char * sep,const MODEL * pmod,const DATASET * dset,int i)4667 static void mn_logit_coeffsep (char *sep, const MODEL *pmod,
4668 			       const DATASET *dset, int i)
4669 {
4670     const char *vname = gretl_model_get_depvar_name(pmod, dset);
4671     const gretl_matrix *y = gretl_model_get_data(pmod, "yvals");
4672     int val = (y != NULL)? y->val[i] : i;
4673 
4674     sprintf(sep, "%s = %d", vname, val);
4675 }
4676 
separator_wanted(int i,int seppos,char ** sepstr,const MODEL * pmod)4677 static int separator_wanted (int i, int seppos, char **sepstr,
4678 			     const MODEL *pmod)
4679 {
4680     int ret = 0;
4681 
4682     if (i == seppos) {
4683 	/* the straightforward criterion */
4684 	ret = 1;
4685     } else if (pmod->ci == MIDASREG && sepstr != NULL) {
4686 	const int *seplist = gretl_model_get_list(pmod, "seplist");
4687 	int j = 0;
4688 
4689 	if (seplist != NULL && (j = in_gretl_list(seplist, i)) > 0) {
4690 	    char *mtl = get_midas_term_line(pmod, j-1);
4691 
4692 	    if (*sepstr != NULL) {
4693 		/* avoid memory leak */
4694 		free(*sepstr);
4695 	    }
4696 	    *sepstr = mtl;
4697 	    if (*sepstr != NULL) {
4698 		ret = 1;
4699 	    }
4700 	}
4701     } else if (pmod->ci == BIPROBIT && i == pmod->ncoeff - 1) {
4702 	/* the last coefficient is biprobit rho */
4703 	ret = 1;
4704     }
4705 
4706     return ret;
4707 }
4708 
plain_print_coeffs(const MODEL * pmod,const DATASET * dset,PRN * prn)4709 static int plain_print_coeffs (const MODEL *pmod,
4710 			       const DATASET *dset,
4711 			       PRN *prn)
4712 {
4713     struct printval **vals, *vij;
4714     const char *headings[] = {
4715 	N_("coefficient"),
4716 	N_("std. error"),
4717 	N_("t-ratio"),
4718 	N_("p-value"),
4719 	N_("slope"),
4720 	N_("lower"),
4721 	N_("upper")
4722     };
4723     const double *b = pmod->coeff;
4724     const double *se = pmod->sderr;
4725     double *slopes = NULL;
4726     char **names = NULL;
4727     const char *head;
4728     char *sepstr = NULL;
4729     double *xb = NULL;
4730     double *xse = NULL;
4731     int seppos = -1, cblock = 0;
4732     int coeff_digits;
4733     int lmax[4] = {0};
4734     int rmax[4] = {0};
4735     int w[4], addoff[4] = {0};
4736     int hlen;
4737     double tval, pval = 0.0;
4738     int n, d, nc = pmod->ncoeff;
4739     int show_slope, adfnum = -1;
4740     int intervals = 0;
4741     int dotlen, namelen = 0;
4742     int colsep = 2;
4743     int ncols = 4;
4744     int i, j, k;
4745     int err = 0;
4746 
4747     if (model_use_zscore(pmod)) {
4748 	headings[2] = N_("z");
4749     }
4750 
4751     if (pmod->ci == AR || pmod->ci == ARCH) {
4752 	k = 0;
4753 	if (pmod->ci == AR) {
4754 	    err = get_ar_data(pmod, &xb, &xse, &k);
4755 	} else {
4756 	    err = get_arch_data(pmod, &xb, &xse, &k);
4757 	}
4758 	if (!err) {
4759 	    b = xb;
4760 	    se = xse;
4761 	    nc += k;
4762 	}
4763     } else if (pmod->ci == LAD) {
4764 	gretl_matrix *m = gretl_model_get_data(pmod, "coeff_intervals");
4765 
4766 	if (m != NULL) {
4767 	    se = m->val;
4768 	    intervals = 1;
4769 	    ncols = 3;
4770 	}
4771     } else if (NONLIST_MODEL(pmod->ci)) {
4772 	headings[0] = N_("estimate");
4773     }
4774 
4775     if (err) {
4776 	return err;
4777     }
4778 
4779     nc -= gretl_model_get_int(pmod, "skipdums");
4780 
4781     vals = allocate_printvals(nc, ncols);
4782     if (vals == NULL) {
4783 	return E_ALLOC;
4784     }
4785 
4786     names = strings_array_new_with_length(nc, 32);
4787     if (names == NULL) {
4788 	err = E_ALLOC;
4789 	goto bailout;
4790     }
4791 
4792     show_slope = binary_model(pmod) && !(pmod->opt & OPT_P);
4793     if (show_slope) {
4794 	slopes = gretl_model_get_data(pmod, "slopes");
4795     }
4796 
4797     if (pmod->aux == AUX_ADF || pmod->aux == AUX_DF) {
4798 	adfnum = gretl_model_get_int(pmod, "dfnum");
4799     }
4800 
4801     gretl_model_get_coeff_separator(pmod, &sepstr, &seppos);
4802     if (seppos == -1) {
4803 	if (pmod->ci == GARCH && pmod->list[0] > 4) {
4804 	    seppos = pmod->list[0] - 4;
4805 	} else if (pmod->ci == AR || pmod->ci == ARCH) {
4806 	    seppos = pmod->ncoeff;
4807 	} else if (multinomial_model(pmod)) {
4808 	    cblock = gretl_model_get_int(pmod, "cblock");
4809 	}
4810     }
4811 
4812     coeff_digits = get_gretl_digits();
4813 
4814     for (i=0; i<nc; i++) {
4815 	if (na(b[i])) {
4816 	    err = E_NAN;
4817 	    goto bailout;
4818 	}
4819 	gretl_model_get_param_name(pmod, dset, i, names[i]);
4820 	n = char_len(names[i]);
4821 	if (n > namelen) {
4822 	    namelen = n;
4823 	}
4824 	if (na(se[i]) || se[i] <= 0) {
4825 	    tval = pval = NADBL;
4826 	} else {
4827 	    tval = b[i] / se[i];
4828 	    if (slopes != NULL) {
4829 		/* last column: actually slope at mean */
4830 		pval = (pmod->list[i+2] == 0)? 0 : slopes[i];
4831 	    } else if (i == adfnum) {
4832 		/* special Dickey-Fuller p-value */
4833 		pval = gretl_model_get_double(pmod, "dfpval");
4834 	    } else if (!intervals) {
4835 		/* regular p-value */
4836 		pval = model_coeff_pval(pmod, tval);
4837 	    }
4838 	}
4839 	for (j=0; j<ncols; j++) {
4840 	    if (j < 2) {
4841 		/* coeff, standard error or lower c.i. limit */
4842 		d = coeff_digits;
4843 		vals[i][j].x = (j==0)? b[i] : se[i];
4844 	    } else if (j == 2) {
4845 		/* t-ratio or upper c.i. limit */
4846 		if (intervals) {
4847 		    d = coeff_digits;
4848 		    vals[i][j].x = se[i + nc];
4849 		} else {
4850 		    d = 4;
4851 		    vals[i][j].x = tval;
4852 		}
4853 	    } else {
4854 		/* p-value or slope */
4855 		d = (show_slope)? coeff_digits : -4;
4856 		vals[i][j].x = pval;
4857 	    }
4858 	    if (show_slope && j == 3 && pmod->list[i+2] == 0) {
4859 		/* don't show 'slope' for constant */
4860 		vals[i][j].s[0] = '\0';
4861 		vals[i][j].lw = vals[i][j].rw = 0;
4862 	    } else if (pmod->ci == DURATION && j > 1 &&
4863 		       !strcmp(names[i], "sigma")) {
4864 		/* suppress result for H0: sigma = 0 */
4865 		vals[i][j].x = NADBL;
4866 		vals[i][j].s[0] = '\0';
4867 		vals[i][j].lw = vals[i][j].rw = 0;
4868 	    } else {
4869 		gretl_sprint_fullwidth_double(vals[i][j].x, d, vals[i][j].s, prn);
4870 		get_number_dims(&vals[i][j], &lmax[j], &rmax[j]);
4871 	    }
4872 	}
4873     }
4874 
4875     if (namelen < 8) {
4876 	namelen = 8;
4877     } else if (namelen > NAMETRUNC) {
4878 	namelen = NAMETRUNC;
4879     }
4880 
4881     /* figure appropriate column separation */
4882 
4883     for (j=0; j<ncols; j++) {
4884 	w[j] = lmax[j] + rmax[j];
4885 	head = get_col_heading(headings, j, show_slope, intervals);
4886 	hlen = char_len(head);
4887 	if (hlen > w[j]) {
4888 	    addoff[j] = (hlen - w[j]) / 2;
4889 	    w[j] = hlen;
4890 	}
4891     }
4892 
4893     figure_colsep(namelen, ncols, w, &colsep);
4894 
4895     /* print headings */
4896 
4897     bufspace(namelen + 2 + colsep, prn);
4898     for (j=0; j<ncols; j++) {
4899 	head = get_col_heading(headings, j, show_slope, intervals);
4900 	print_padded_head(head, w[j], prn);
4901 	if (j < ncols - 1) {
4902 	    bufspace(colsep, prn);
4903 	}
4904     }
4905 
4906     /* separator row */
4907     dotlen = print_sep_row(namelen, ncols, w, colsep, prn);
4908 
4909     /* biprobit special: name of first dependent variable */
4910     if (pmod->ci == BIPROBIT) {
4911 	print_coeff_left_string(gretl_model_get_depvar_name(pmod, dset),
4912 				prn);
4913     }
4914 
4915     /* print row values */
4916 
4917     k = 0;
4918     for (i=0; i<nc; i++) {
4919 	char tmp[NAMETRUNC];
4920 	int namepad;
4921 
4922 	if (separator_wanted(i, seppos, &sepstr, pmod)) {
4923 	    if (pmod->ci == BIPROBIT) {
4924 		pputc(prn, '\n');
4925 		if (i == seppos) {
4926 		    print_coeff_left_string(sepstr, prn);
4927 		}
4928 	    } else {
4929 		print_coeff_separator(sepstr, dotlen, prn);
4930 	    }
4931 	    free(sepstr);
4932 	    sepstr = NULL;
4933 	} else if (cblock > 0 && i % cblock == 0) {
4934 	    char mnlsep[32];
4935 
4936 	    mn_logit_coeffsep(mnlsep, pmod, dset, ++k);
4937 	    print_coeff_separator(mnlsep, 0, prn);
4938 	}
4939 	maybe_trim_varname(tmp, names[i]);
4940 	namepad = namelen + strlen(tmp) - g_utf8_strlen(tmp, -1);
4941 	pprintf(prn, "  %-*s", namepad, tmp);
4942 	bufspace(colsep, prn);
4943 	for (j=0; j<ncols; j++) {
4944 	    vij = &vals[i][j];
4945 	    print_padded_value(vij, w[j], lmax[j], addoff[j], prn);
4946 	    if (j < ncols - 1) {
4947 		bufspace(colsep, prn);
4948 	    } else if (!show_slope && !na(vij->x)) {
4949 		put_asts(vij->x, prn);
4950 	    }
4951 	}
4952 	pputc(prn, '\n');
4953     }
4954 
4955     if (pmod->ci == AR) {
4956 	print_ar_sum(pmod, prn);
4957     } else if (COUNT_MODEL(pmod->ci)) {
4958 	print_count_offset(pmod, dset, &vals[0][0], namelen,
4959 			   colsep, w[0], lmax[0], addoff[0],
4960 			   prn);
4961     }
4962 
4963  bailout:
4964 
4965     for (i=0; i<nc; i++) {
4966 	free(vals[i]);
4967     }
4968     free(vals);
4969 
4970     strings_array_free(names, nc);
4971     free(xb);
4972     free(xse);
4973 
4974     return err;
4975 }
4976 
4977 static int
plain_print_coefficients(const MODEL * pmod,const DATASET * dset,PRN * prn)4978 plain_print_coefficients (const MODEL *pmod, const DATASET *dset, PRN *prn)
4979 {
4980     if (pmod->ncoeff == 0) {
4981 	return 0;
4982     } else if (gretl_model_get_data(pmod, "rq_sequence") != NULL) {
4983 	return print_rq_sequence(pmod, dset, prn);
4984     } else if (pmod->ci == MPOLS) {
4985 	return plain_print_mp_coeffs(pmod, dset, prn);
4986     } else {
4987 	return plain_print_coeffs(pmod, dset, prn);
4988     }
4989 }
4990 
alt_print_arch_terms(const MODEL * pmod,PRN * prn)4991 static void alt_print_arch_terms (const MODEL *pmod, PRN *prn)
4992 {
4993     double *a = gretl_model_get_data(pmod, "arch_coeff");
4994     double *se = gretl_model_get_data(pmod, "arch_sderr");
4995     int order = gretl_model_get_int(pmod, "arch_order");
4996 
4997     if (a != NULL && se != NULL && order > 0) {
4998 	model_coeff mc;
4999 	int i;
5000 
5001 	gretl_prn_newline(prn);
5002 
5003 	for (i=0; i<=order; i++) {
5004 	    model_coeff_init(&mc);
5005 	    mc.b = a[i];
5006 	    mc.se = se[i];
5007 	    mc.tval = a[i] / se[i];
5008 	    mc.pval = student_pvalue_2(pmod->nobs - (order + 1), mc.tval);
5009 
5010 	    if (tex_format(prn)) {
5011 		sprintf(mc.name, "$\\alpha_%d$", i);
5012 	    } else {
5013 		sprintf(mc.name, "alpha(%d)", i);
5014 	    }
5015 
5016 	    alt_print_coeff(&mc, prn);
5017 	}
5018     }
5019 }
5020 
5021 static void
alt_print_count_offset(const MODEL * pmod,const DATASET * dset,PRN * prn)5022 alt_print_count_offset (const MODEL *pmod, const DATASET *dset, PRN *prn)
5023 {
5024     int offvar = gretl_model_get_int(pmod, "offset_var");
5025 
5026     if (offvar > 0) {
5027 	char name[24];
5028 
5029 	sprintf(name, "log(%s)", dset->varname[offvar]);
5030 
5031 	if (plain_format(prn)) {
5032 	    pprintf(prn, "\n  %-13s         1.0\n", name);
5033 	} else if (rtf_format(prn)) {
5034 	    pputs(prn, RTF_COEFF_ROW);
5035 	    pprintf(prn, "\\ql %s\\cell\\qc 1.0\\cell", name);
5036 	    pputs(prn, "\\qc \\cell\\qc \\cell \\qc \\cell \\intbl \\row\n");
5037 	} else if (tex_format(prn)) {
5038 	    char tmp[48];
5039 
5040 	    tex_escape(tmp, name);
5041 	    pprintf(prn, "{\\rm %s} & \\multicolumn{1}{c}{1.0} \\\\\n", tmp);
5042 	}
5043     }
5044 }
5045 
5046 static int
alt_print_coefficients(const MODEL * pmod,const DATASET * dset,PRN * prn)5047 alt_print_coefficients (const MODEL *pmod, const DATASET *dset, PRN *prn)
5048 {
5049     gretl_matrix *intervals = NULL;
5050     char *sepstr = NULL;
5051     int seppos = -1;
5052     model_coeff mc;
5053     int adfnum = -1;
5054     int nc = pmod->ncoeff;
5055     int cols, gotnan = 0;
5056     int i, err = 0;
5057 
5058     if (gretl_model_get_data(pmod, "rq_sequence") != NULL) {
5059 	pputs(prn, "Sorry, not implemented yet!\n");
5060 	return 1;
5061     }
5062 
5063     cols = alt_print_coeff_table_start(pmod, pmod->ci, prn);
5064 
5065     if (pmod->ci == PANEL) {
5066 	nc = pmod->list[0] - 1;
5067     }
5068 
5069     gretl_model_get_coeff_separator(pmod, &sepstr, &seppos);
5070     if (seppos == -1 && pmod->ci == GARCH && pmod->list[0] > 4) {
5071 	seppos = pmod->list[0] - 4;
5072     }
5073 
5074     if (pmod->ci == LAD) {
5075 	intervals = gretl_model_get_data(pmod, "coeff_intervals");
5076     }
5077 
5078     if (pmod->aux == AUX_DF || pmod->aux == AUX_ADF) {
5079 	adfnum = gretl_model_get_int(pmod, "dfnum");
5080     }
5081 
5082     if (pmod->ci == BIPROBIT) {
5083 	print_coeff_left_string(gretl_model_get_depvar_name(pmod, dset),
5084 				prn);
5085     }
5086 
5087     for (i=0; i<nc; i++) {
5088 	err = prepare_model_coeff(pmod, dset, i, adfnum, &mc, prn);
5089 	if (err) gotnan = 1;
5090 	if (separator_wanted(i, seppos, &sepstr, pmod)) {
5091 	    if (pmod->ci == BIPROBIT) {
5092 		print_coeff_left_string(sepstr, prn);
5093 	    } else {
5094 		print_coeff_separator(sepstr, cols, prn);
5095 	    }
5096 	    free(sepstr);
5097 	    sepstr = NULL;
5098 	}
5099 	if (intervals != NULL) {
5100 	    mc.lo = gretl_matrix_get(intervals, i, 0);
5101 	    mc.hi = gretl_matrix_get(intervals, i, 1);
5102 	    mc.show_pval = 0;
5103 	}
5104 	alt_print_coeff(&mc, prn);
5105     }
5106 
5107     if (pmod->ci == AR) {
5108 	alt_print_rho_terms(pmod, prn);
5109     } else if (pmod->ci == ARCH) {
5110 	alt_print_arch_terms(pmod, prn);
5111     } else if (COUNT_MODEL(pmod->ci)) {
5112 	alt_print_count_offset(pmod, dset, prn);
5113     }
5114 
5115     return gotnan;
5116 }
5117 
print_rho(const ARINFO * arinfo,int c,int dfd,PRN * prn)5118 static void print_rho (const ARINFO *arinfo, int c, int dfd, PRN *prn)
5119 {
5120     double tval = arinfo->rho[c] / arinfo->sderr[c];
5121     char ustr[32];
5122 
5123     if (tex_format(prn)) {
5124 	char s1[32], s2[32], s3[32], s4[32];
5125 
5126 	tex_rl_double(arinfo->rho[c], s1);
5127 	tex_rl_double(arinfo->sderr[c], s2);
5128 	tex_rl_float(tval, s3, 4);
5129 	tex_rl_float(student_pvalue_2(dfd, tval), s4, 4);
5130 
5131 	sprintf(ustr, "$\\hat{u}_{t-%d}$", arinfo->arlist[c+1]);
5132 
5133 	pprintf(prn, "%s &\n"
5134 		"  %s &\n"
5135 		"    %s &\n"
5136 		"      %s &\n"
5137 		"        %s \\\\\n",
5138 		ustr, s1, s2, s3, s4);
5139     } else if (rtf_format(prn)) {
5140 	char pvalstr[16];
5141 	double pval;
5142 
5143 	pputs(prn, RTF_COEFF_ROW);
5144 	pprintf(prn, "\\ql u(-%d)\\cell", arinfo->arlist[c+1]);
5145 	rtf_print_double(arinfo->rho[c], prn);
5146 	rtf_print_double(arinfo->sderr[c], prn);
5147 	pprintf(prn, " \\qc %.4f\\cell", tval);
5148 	pval = student_pvalue_2(dfd, tval);
5149 	print_pval_str(pval, pvalstr);
5150 	pprintf(prn, " \\qc %s\\cell", pvalstr);
5151 	if (pval < 0.01) {
5152 	    pputs(prn, " \\ql ***\\cell");
5153 	} else if (pval < 0.05) {
5154 	    pputs(prn, " \\ql **\\cell");
5155 	} else if (pval < 0.10) {
5156 	    pputs(prn, " \\ql *\\cell");
5157 	} else {
5158 	    pputs(prn, " \\ql \\cell");
5159 	}
5160 	pputs(prn, " \\intbl \\row\n");
5161     }
5162 }
5163 
alt_print_rho_terms(const MODEL * pmod,PRN * prn)5164 static void alt_print_rho_terms (const MODEL *pmod, PRN *prn)
5165 {
5166     double xx = 0.0;
5167     int i, dfd;
5168 
5169     if (pmod->arinfo == NULL ||
5170 	pmod->arinfo->arlist == NULL ||
5171 	pmod->arinfo->rho == NULL ||
5172 	pmod->arinfo->sderr == NULL) {
5173 	return;
5174     }
5175 
5176     if (pmod->arinfo->arlist[0] > 1) {
5177 	dfd = pmod->dfd + (pmod->ncoeff - pmod->arinfo->arlist[0]);
5178     } else {
5179 	dfd = pmod->dfd;
5180     }
5181 
5182     for (i=1; i<=pmod->arinfo->arlist[0]; i++) {
5183 	print_rho(pmod->arinfo, i - 1, dfd, prn);
5184 	xx += pmod->arinfo->rho[i-1];
5185     }
5186 }
5187 
5188 const char *roots_hdr = N_("                        Real  Imaginary"
5189 			   "    Modulus  Frequency");
5190 const char *root_fmt = "%8s%3d%17.4f%11.4f%11.4f%11.4f\n";
5191 const char *roots_sep = "  -----------------------------------------"
5192     "------------------";
5193 
print_root(double rx,double ix,double mod,double fr,int i,int hline,PRN * prn)5194 static void print_root (double rx, double ix, double mod, double fr,
5195 			int i, int hline, PRN *prn)
5196 {
5197     if (plain_format(prn)) {
5198 	pprintf(prn, root_fmt, _("Root"), i, rx, ix, mod, fr);
5199     } else if (tex_format(prn)) {
5200 	pprintf(prn, "& %s & %d & $%.4f$ & $%.4f$ & $%.4f$ & $%.4f$ \\\\ ",
5201 		_("Root"), i, rx, ix, mod, fr);
5202 	if (hline) {
5203 	    pputs(prn, "\\hline\n");
5204 	} else {
5205 	    pputc(prn, '\n');
5206 	}
5207     } else if (rtf_format(prn)) {
5208 	pputs(prn, RTF_ROOT_ROW);
5209 	pprintf(prn, "\\ql \\cell \\ql %s %d \\cell"
5210 		" \\qr %.4f\\cell"
5211 		" \\qr %.4f\\cell"
5212 		" \\qr %.4f\\cell"
5213 		" \\qr %.4f\\cell \\intbl \\row\n",
5214 		_("Root"), i, rx, ix, mod, fr);
5215     }
5216 }
5217 
root_start(const char * tag,PRN * prn)5218 static void root_start (const char *tag, PRN *prn)
5219 {
5220     if (plain_format(prn)) {
5221 	pprintf(prn, "  %s\n", _(tag));
5222     } else if (tex_format(prn)) {
5223 	pprintf(prn, "%s \\\\ \n", _(tag));
5224     } else if (rtf_format(prn)) {
5225 	pputs(prn, RTF_ROOT_ROW);
5226 	pprintf(prn, "\\ql %s\\cell\\ql \\cell\\ql \\cell\\ql \\cell\\ql \\cell"
5227 		"\\ql\\cell \\intbl \\row\n", _(tag));
5228     }
5229 }
5230 
mod_and_freq(cmplx * c,double * mod,double * frq)5231 static void mod_and_freq (cmplx *c, double *mod, double *frq)
5232 {
5233     if (c->i != 0) {
5234 	*mod = sqrt(c->r * c->r + c->i * c->i);
5235     } else {
5236 	*mod = fabs(c->r);
5237     }
5238     *frq = atan2(c->i, c->r) / M_2PI;
5239 }
5240 
print_arma_roots(const MODEL * pmod,PRN * prn)5241 static void print_arma_roots (const MODEL *pmod, PRN *prn)
5242 {
5243     cmplx *roots = gretl_model_get_data(pmod, "roots");
5244 
5245     if (roots != NULL) {
5246 	int p = arma_model_nonseasonal_AR_order(pmod);
5247 	int q = arma_model_nonseasonal_MA_order(pmod);
5248 	int P = gretl_model_get_int(pmod, "arma_P");
5249 	int Q = gretl_model_get_int(pmod, "arma_Q");
5250 	int i, k, hline;
5251 	double mod, fr;
5252 
5253 	if (plain_format(prn)) {
5254 	    pprintf(prn, "%s\n%s\n", _(roots_hdr), roots_sep);
5255 	} else if (tex_format(prn)) {
5256 	    pputs(prn, "\n\\vspace{1em}\n\n");
5257 	    pputs(prn, "\\begin{tabular}{llrrrrr}\n");
5258 	    pprintf(prn, "& & & %s & %s & %s & %s \\\\ \\hline\n",
5259 		    _("Real"), _("Imaginary"), _("Modulus"), _("Frequency"));
5260 	} else if (rtf_format(prn)) {
5261 	    pputs(prn, "\n\\par\n{" RTF_ROOT_ROW);
5262 	    pprintf(prn, "\\qr \\cell \\qc \\cell"
5263 		    " \\qc {\\i %s}\\cell"
5264 		    " \\qc {\\i %s}\\cell"
5265 		    " \\qc {\\i %s}\\cell"
5266 		    " \\qc {\\i %s}\\cell \\intbl \\row\n",
5267 		    _("Real"), _("Imaginary"), _("Modulus"), _("Frequency"));
5268 	}
5269 
5270 	if (p > 0) {
5271 	    k = 1;
5272 	    root_start(N_("AR"), prn);
5273 	    for (i=0; i<p; i++) {
5274 		mod_and_freq(&roots[i], &mod, &fr);
5275 		if (i == p - 1 && q == 0 && P == 0 && Q == 0) {
5276 		    hline = 1;
5277 		} else {
5278 		    hline = 0;
5279 		}
5280 		print_root(roots[i].r, roots[i].i, mod, fr, k++, hline, prn);
5281 	    }
5282 	}
5283 
5284 	if (P > 0) {
5285 	    k = 1;
5286 	    root_start(N_("AR (seasonal)"), prn);
5287 	    for (i=p; i<p+P; i++) {
5288 		mod_and_freq(&roots[i], &mod, &fr);
5289 		if (i == p + P - 1 && q == 0 && Q == 0) {
5290 		    hline = 1;
5291 		} else {
5292 		    hline = 0;
5293 		}
5294 		print_root(roots[i].r, roots[i].i, mod, fr, k++, hline, prn);
5295 	    }
5296 	}
5297 
5298 	if (q > 0) {
5299 	    k = 1;
5300 	    root_start(N_("MA"), prn);
5301 	    for (i=p+P; i<p+P+q; i++) {
5302 		mod_and_freq(&roots[i], &mod, &fr);
5303 		if (i == p + P + q - 1 && Q == 0) {
5304 		    hline = 1;
5305 		} else {
5306 		    hline = 0;
5307 		}
5308 		print_root(roots[i].r, roots[i].i, mod, fr, k++, hline, prn);
5309 	    }
5310 	}
5311 
5312 	if (Q > 0) {
5313 	    k = 1;
5314 	    root_start(N_("MA (seasonal)"), prn);
5315 	    for (i=p+P+q; i<p+P+q+Q; i++) {
5316 		mod_and_freq(&roots[i], &mod, &fr);
5317 		if (i == p + P + q + Q - 1) {
5318 		    hline = 1;
5319 		} else {
5320 		    hline = 0;
5321 		}
5322 		print_root(roots[i].r, roots[i].i, mod, fr, k++, hline, prn);
5323 	    }
5324 	}
5325 
5326 	if (plain_format(prn)) {
5327 	    pprintf(prn, "%s\n\n", roots_sep);
5328 	} else if (tex_format(prn)) {
5329 	    pputs(prn, "\\end{tabular}\n");
5330 	} else if (rtf_format(prn)) {
5331 	    pputs(prn, "}\n");
5332 	}
5333     }
5334 }
5335 
print_heckit_stats(const MODEL * pmod,PRN * prn)5336 static void print_heckit_stats (const MODEL *pmod, PRN *prn)
5337 {
5338     int totobs = gretl_model_get_int(pmod, "totobs");
5339     int cenobs = totobs - pmod->nobs;
5340     double cenpc = (100.0 * cenobs) / totobs;
5341 
5342     ensure_vsep(prn);
5343 
5344     if (plain_format(prn)) {
5345 	pprintf(prn, "%s: %d\n", _("Total observations"), totobs);
5346 	pprintf(prn, "%s: %d (%.1f%%)\n", _("Censored observations"),
5347 		cenobs, cenpc);
5348 	pputc(prn, '\n');
5349     } else if (rtf_format(prn)) {
5350 	pprintf(prn, RTFTAB "%s: %d\n", _("Total observations"), totobs);
5351 	pprintf(prn, RTFTAB "%s: %d (%.1f%%)\n", _("Censored observations"),
5352 		cenobs, cenpc);
5353     } else if (tex_format(prn)) {
5354 	pprintf(prn, "%s: %d \\\\\n", _("Total observations"), totobs);
5355 	pprintf(prn, "%s: %d (%.1f\\%%) \\\\\n", _("Censored observations"),
5356 		cenobs, cenpc);
5357     }
5358 }
5359 
plain_print_act_pred(const int * ap,PRN * prn)5360 static void plain_print_act_pred (const int *ap, PRN *prn)
5361 {
5362     int leftlen;
5363     int numwidth = 1;
5364     int i, bign = 0;
5365 
5366     for (i=0; i<4; i++) {
5367 	if (ap[i] > bign) {
5368 	    bign = ap[i];
5369 	}
5370     }
5371 
5372     while (bign /= 10) {
5373 	numwidth++;
5374     }
5375 
5376     leftlen = char_len(_("Actual")) + 3;
5377 
5378     bufspace(leftlen + 2, prn);
5379     pputs(prn, _("Predicted"));
5380     pputc(prn, '\n');
5381     bufspace(leftlen + 3, prn);
5382     pprintf(prn, "%*d   %*d\n", numwidth, 0, numwidth, 1);
5383     bufspace(2, prn);
5384     pputs(prn, _("Actual"));
5385     pprintf(prn, " 0  %*d   %*d\n", numwidth, ap[0], numwidth, ap[1]);
5386     bufspace(leftlen, prn);
5387     pprintf(prn, "1  %*d   %*d\n", numwidth, ap[2], numwidth, ap[3]);
5388     pputc(prn, '\n');
5389 }
5390 
limdep_df(const MODEL * pmod)5391 static int limdep_df (const MODEL *pmod)
5392 {
5393     int df = gretl_model_get_int(pmod, "lr_df");
5394 
5395     if (df == 0) {
5396 	df = pmod->dfn;
5397     }
5398 
5399     return df;
5400 }
5401 
logit_probit_stats(const MODEL * pmod,PRN * prn)5402 static void logit_probit_stats (const MODEL *pmod, PRN *prn)
5403 {
5404     const int *act_pred = NULL;
5405     int binary, slopes, correct = 0;
5406     double pc_correct;
5407     int df = 0;
5408 
5409     if ((pmod->opt & OPT_M) || gretl_model_get_int(pmod, "ordered")) {
5410 	/* ordered logit/probit or multinomial logit */
5411 	binary = slopes = 0;
5412     } else {
5413 	binary = 1;
5414 	slopes = !(pmod->opt & OPT_P);
5415     }
5416 
5417     /* for overall likelihood ratio test */
5418     if (!na(pmod->chisq)) {
5419 	df = limdep_df(pmod);
5420     }
5421 
5422     if (binary) {
5423 	act_pred = gretl_model_get_data(pmod, "discrete_act_pred");
5424 	if (act_pred != NULL) {
5425 	    correct = act_pred[0] + act_pred[3];
5426 	}
5427     } else {
5428 	correct = gretl_model_get_int(pmod, "correct");
5429     }
5430 
5431     pc_correct = 100 * (double) correct / pmod->nobs;
5432 
5433     ensure_vsep(prn);
5434 
5435     if (plain_format(prn)) {
5436 	if (correct > 0) {
5437 	    pprintf(prn, "%s = %d (%.1f%%)\n",
5438 		    _("Number of cases 'correctly predicted'"),
5439 		    correct, pc_correct);
5440 	}
5441 	if (binary) {
5442 	    double fXb = gretl_model_get_double(pmod, "fXb");
5443 
5444 	    if (!na(fXb)) {
5445 		pprintf(prn, "f(beta'x) %s = %.3f\n", _("at mean of independent vars"),
5446 			fXb);
5447 	    }
5448 	}
5449 	if (df) {
5450 	    pprintf(prn, "%s: %s(%d) = %g [%.4f]\n",
5451 		    _("Likelihood ratio test"), _("Chi-square"),
5452 		    df, pmod->chisq, chisq_cdf_comp(df, pmod->chisq));
5453 	}
5454 	pputc(prn, '\n');
5455 	if (act_pred != NULL) {
5456 	    plain_print_act_pred(act_pred, prn);
5457 	}
5458     } else if (rtf_format(prn)) {
5459 	pputc(prn, '\n');
5460 	if (slopes) {
5461 	    pprintf(prn, "\\par {\\super *}%s\n", _("Evaluated at the mean"));
5462 	}
5463 	if (correct > 0) {
5464 	    pprintf(prn, "\\par %s = %d (%.1f%%)\n",
5465 		    _("Number of cases 'correctly predicted'"),
5466 		    correct, pc_correct);
5467 	}
5468 	if (binary) {
5469 	    pprintf(prn, "\\par f(beta'x) %s = %.3f\n", _("at mean of independent vars"),
5470 		    pmod->sdy);
5471 	}
5472 	if (df) {
5473 	    pprintf(prn, "\\par %s: %s(%d) = %g [%.4f]\n",
5474 		    _("Likelihood ratio test"), _("Chi-square"),
5475 		    df, pmod->chisq, chisq_cdf_comp(df, pmod->chisq));
5476 	}
5477 	pputc(prn, '\n');
5478     } else if (tex_format(prn)) {
5479 	if (slopes) {
5480 	    pprintf(prn, "\\begin{center}\n$^*$%s\n\\end{center}\n",
5481 		    _("Evaluated at the mean"));
5482 	}
5483 	if (correct > 0 || df) {
5484 	    pputs(prn, "\\vspace{1em}\n\\begin{raggedright}\n");
5485 	    if (correct > 0) {
5486 		pprintf(prn, "%s = %d (%.1f %s)\\\\\n",
5487 			_("Number of cases `correctly predicted'"),
5488 			correct, pc_correct, _("percent"));
5489 	    }
5490 	    if (df) {
5491 		pprintf(prn, "%s: $\\chi^2(%d)$ = %.3f [%.4f]\\\\\n",
5492 			_("Likelihood ratio test"),
5493 			df, pmod->chisq, chisq_cdf_comp(df, pmod->chisq));
5494 	    }
5495 	    pputs(prn, "\\end{raggedright}\n");
5496 	}
5497     }
5498 }
5499 
ols_print_anova(const MODEL * pmod,PRN * prn)5500 int ols_print_anova (const MODEL *pmod, PRN *prn)
5501 {
5502     double mst, msr, mse, rss;
5503     int n, c1, c2, c3;
5504 
5505     if (pmod->ci != OLS || !pmod->ifc ||
5506 	na(pmod->ess) || na(pmod->tss)) {
5507 	return E_NOTIMP;
5508     }
5509 
5510     pprintf(prn, "%s:\n\n", _("Analysis of Variance"));
5511 
5512     if (pmod->dfn == 0) {
5513 	/* degenerate model: const only */
5514 	rss = 0.0;
5515     } else {
5516 	rss = pmod->tss - pmod->ess;
5517     }
5518 
5519     c1 = g_utf8_strlen(_("Sum of squares"), -1);
5520     c2 = g_utf8_strlen(_("df"), -1);
5521     c3 = g_utf8_strlen(_("Mean square"), -1);
5522 
5523     c1 = (c1 < 35)? 35 : c1;
5524     c2 = (c2 > 8)? c2 + 1 : (c2 < 8)? 8 : c2;
5525     c3 = (c3 > 16)? c3 + 1 : (c3 < 16)? 16 : c3;
5526 
5527     /* header strings are right-aligned */
5528     n = g_utf8_strlen(_("Sum of squares"), -1);
5529     bufspace(c1 - n, prn);
5530     pputs(prn, _("Sum of squares"));
5531     n = g_utf8_strlen(_("df"), -1);
5532     bufspace(c2 + 1 - n, prn);
5533     pputs(prn, _("df"));
5534     n = g_utf8_strlen(_("Mean square"), -1);
5535     bufspace(c3 + 1 - n, prn);
5536     pputs(prn, _("Mean square"));
5537     pputs(prn, "\n\n");
5538     c1 = 16;
5539 
5540     /* Mean Square, regression */
5541     msr = rss / pmod->dfn;
5542     /* string left-aligned with initial offset of 2 */
5543     n = g_utf8_strlen(_("Regression"), -1);
5544     bufspace(2, prn);
5545     pputs(prn, _("Regression"));
5546     bufspace(16 - n, prn);
5547     if (pmod->dfn == 0) {
5548 	pprintf(prn, " %*g %*d %*s\n", c1, rss, c2, pmod->dfn, c3, _("undefined"));
5549     } else {
5550 	pprintf(prn, " %*g %*d %*g\n", c1, rss, c2, pmod->dfn, c3, msr);
5551     }
5552 
5553     /* Mean Square, errors */
5554     mse = pmod->ess / pmod->dfd;
5555     /* string left-aligned with initial offset of 2 */
5556     n = g_utf8_strlen(_("Residual"), -1);
5557     bufspace(2, prn);
5558     pputs(prn, _("Residual"));
5559     bufspace(16 - n, prn);
5560     pprintf(prn, " %*g %*d %*g\n", c1, pmod->ess, c2, pmod->dfd, c3, mse);
5561 
5562     /* Mean Square, total */
5563     mst = pmod->tss / (pmod->nobs - 1);
5564     /* string left-aligned with initial offset of 2 */
5565     n = g_utf8_strlen(_("Total"), -1);
5566     bufspace(2, prn);
5567     pputs(prn, _("Total"));
5568     bufspace(16 - n, prn);
5569     pprintf(prn, " %*g %*d %*g\n", c1, pmod->tss, c2, pmod->nobs - 1, c3, mst);
5570 
5571     pprintf(prn, "\n  R^2 = %g / %g = %.6f\n", rss, pmod->tss, rss / pmod->tss);
5572 
5573     if (pmod->dfn == 0) {
5574 	pprintf(prn, "  F(%d, %d) %s\n\n", pmod->dfn, pmod->dfd, _("undefined"));
5575 	return 0;
5576     }
5577 
5578     if (pmod->ess == 0 || rss == 0.0) {
5579 	pprintf(prn, "  F(%d, %d) = %g / %g (%s)\n\n", pmod->dfn, pmod->dfd,
5580 		msr, mse, _("undefined"));
5581     } else {
5582 	double F = msr / mse;
5583 	double pv = snedecor_cdf_comp(pmod->dfn, pmod->dfd, F);
5584 
5585 	pprintf(prn, "  F(%d, %d) = %g / %g = %g ",
5586 		pmod->dfn, pmod->dfd, msr, mse, F);
5587 	if (pv < .0001) {
5588 	    pprintf(prn, "[%s %.3g]\n\n", _("p-value"), pv);
5589 	} else if (!na(pv)) {
5590 	    pprintf(prn, "[%s %.4f]\n\n", _("p-value"), pv);
5591 	}
5592     }
5593 
5594     return 0;
5595 }
5596 
5597 /**
5598  * print_model_from_matrices:
5599  * @cs: k x 2 matrix containing coefficients and standard errors.
5600  * @adds: matrix containing an additional p statistics, or NULL.
5601  * @names: array of strings containing all required names.
5602  * @df: degrees of freedom, or 0 for asymptotic.
5603  * @opt: may contain OPT_I fpr printing the extra stats inline.
5604  * @prn: gretl printer.
5605  *
5606  * Prints to @prn the coefficient table and optional additional statistics
5607  * for a model estimated "by hand". Mainly useful for user-written functions.
5608  *
5609  * The number of string in the array @names must be >= k + p, where k is
5610  * the number of coefficients and p the number of additional statistics
5611  * given in @adds.
5612  *
5613  * Returns: 0 on success, non-zero on failure.
5614  */
5615 
print_model_from_matrices(const gretl_matrix * cs,const gretl_matrix * adds,gretl_array * names,int df,gretlopt opt,PRN * prn)5616 int print_model_from_matrices (const gretl_matrix *cs,
5617 			       const gretl_matrix *adds,
5618 			       gretl_array *names, int df,
5619 			       gretlopt opt, PRN *prn)
5620 {
5621     int k = gretl_matrix_rows(cs);
5622     int p = gretl_vector_get_length(adds);
5623     const double *b = cs->val;
5624     const double *se = b + k;
5625     char **S;
5626     int ns = 0;
5627 
5628     S = gretl_array_get_strings(names, &ns);
5629     if (S == NULL || ns < k + p) {
5630 	return E_NONCONF;
5631     }
5632 
5633     if (plain_format(prn)) {
5634 	/* newline here is useless for TeX and makes RTF choke */
5635 	pputc(prn, '\n');
5636     } else if (csv_format(prn)) {
5637 	set_csv_delim(prn);
5638     }
5639 
5640     model_format_start(prn);
5641 
5642     print_coeffs(b, se, (const char **) S, k, df, MODPRINT, prn);
5643 
5644     if (p > 0) {
5645 	print_model_stats_table(adds->val, (const char **) S + k,
5646 				p, opt, prn);
5647     }
5648 
5649     if (plain_format(prn)) {
5650 	pputc(prn, '\n');
5651     }
5652 
5653     model_format_end(prn);
5654 
5655     return 0;
5656 }
5657 
get_printmodel_opt(const MODEL * pmod,gretlopt opt)5658 gretlopt get_printmodel_opt (const MODEL *pmod,
5659 			     gretlopt opt)
5660 {
5661     gretlopt ret = OPT_NONE;
5662 
5663     /* Screen out any irrelevant and possible confusing
5664        options given with an estimation command, leaving
5665        only options that are intended for the printing
5666        routine.
5667     */
5668 
5669     /* first handle cases where we should not print */
5670     if (opt & OPT_Q) {
5671 	return OPT_Q;
5672     } else if (pmod->ci == MLE && (opt & OPT_A) && !(opt & OPT_V)) {
5673 	/* mle with the --auxiliary option */
5674 	return OPT_Q;
5675     }
5676 
5677     if (opt & OPT_O) {
5678 	ret |= OPT_O; /* show covariance matrix */
5679     }
5680     if (opt & OPT_S) {
5681 	ret |= OPT_S; /* "simple": reduced output */
5682     }
5683     if (pmod->ci == OLS && (opt & OPT_V)) {
5684 	ret |= OPT_V; /* anova (OLS only) */
5685     }
5686 
5687     return ret;
5688 }
5689